2
0

quic_demux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/quic_demux.h"
  10. #include "internal/quic_wire_pkt.h"
  11. #include "internal/common.h"
  12. #include <openssl/lhash.h>
  13. #include <openssl/err.h>
  14. #define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */
  15. #define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */
  16. #define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */
  17. #define DEMUX_MAX_MSGS_PER_CALL 32
  18. #define DEMUX_DEFAULT_MTU 1500
  19. struct quic_demux_st {
  20. /* The underlying transport BIO with datagram semantics. */
  21. BIO *net_bio;
  22. /*
  23. * QUIC short packets do not contain the length of the connection ID field,
  24. * therefore it must be known contextually. The demuxer requires connection
  25. * IDs of the same length to be used for all incoming packets.
  26. */
  27. size_t short_conn_id_len;
  28. /*
  29. * Our current understanding of the upper bound on an incoming datagram size
  30. * in bytes.
  31. */
  32. size_t mtu;
  33. /* The datagram_id to use for the next datagram we receive. */
  34. uint64_t next_datagram_id;
  35. /* Time retrieval callback. */
  36. OSSL_TIME (*now)(void *arg);
  37. void *now_arg;
  38. /* The default packet handler, if any. */
  39. ossl_quic_demux_cb_fn *default_cb;
  40. void *default_cb_arg;
  41. /*
  42. * List of URXEs which are not currently in use (i.e., not filled with
  43. * unconsumed data). These are moved to the pending list as they are filled.
  44. */
  45. QUIC_URXE_LIST urx_free;
  46. /*
  47. * List of URXEs which are filled with received encrypted data. These are
  48. * removed from this list as we invoke the callbacks for each of them. They
  49. * are then not on any list managed by us; we forget about them until our
  50. * user calls ossl_quic_demux_release_urxe to return the URXE to us, at
  51. * which point we add it to the free list.
  52. */
  53. QUIC_URXE_LIST urx_pending;
  54. /* Whether to use local address support. */
  55. char use_local_addr;
  56. };
  57. QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
  58. size_t short_conn_id_len,
  59. OSSL_TIME (*now)(void *arg),
  60. void *now_arg)
  61. {
  62. QUIC_DEMUX *demux;
  63. demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
  64. if (demux == NULL)
  65. return NULL;
  66. demux->net_bio = net_bio;
  67. demux->short_conn_id_len = short_conn_id_len;
  68. /* We update this if possible when we get a BIO. */
  69. demux->mtu = DEMUX_DEFAULT_MTU;
  70. demux->now = now;
  71. demux->now_arg = now_arg;
  72. if (net_bio != NULL
  73. && BIO_dgram_get_local_addr_cap(net_bio)
  74. && BIO_dgram_set_local_addr_enable(net_bio, 1))
  75. demux->use_local_addr = 1;
  76. return demux;
  77. }
  78. static void demux_free_urxl(QUIC_URXE_LIST *l)
  79. {
  80. QUIC_URXE *e, *enext;
  81. for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
  82. enext = ossl_list_urxe_next(e);
  83. ossl_list_urxe_remove(l, e);
  84. OPENSSL_free(e);
  85. }
  86. }
  87. void ossl_quic_demux_free(QUIC_DEMUX *demux)
  88. {
  89. if (demux == NULL)
  90. return;
  91. /* Free all URXEs we are holding. */
  92. demux_free_urxl(&demux->urx_free);
  93. demux_free_urxl(&demux->urx_pending);
  94. OPENSSL_free(demux);
  95. }
  96. void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)
  97. {
  98. unsigned int mtu;
  99. demux->net_bio = net_bio;
  100. if (net_bio != NULL) {
  101. /*
  102. * Try to determine our MTU if possible. The BIO is not required to
  103. * support this, in which case we remain at the last known MTU, or our
  104. * initial default.
  105. */
  106. mtu = BIO_dgram_get_mtu(net_bio);
  107. if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)
  108. ossl_quic_demux_set_mtu(demux, mtu); /* best effort */
  109. }
  110. }
  111. int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)
  112. {
  113. if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)
  114. return 0;
  115. demux->mtu = mtu;
  116. return 1;
  117. }
  118. void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
  119. ossl_quic_demux_cb_fn *cb,
  120. void *cb_arg)
  121. {
  122. demux->default_cb = cb;
  123. demux->default_cb_arg = cb_arg;
  124. }
  125. static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
  126. {
  127. QUIC_URXE *e;
  128. if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
  129. return NULL;
  130. e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
  131. if (e == NULL)
  132. return NULL;
  133. ossl_list_urxe_init_elem(e);
  134. e->alloc_len = alloc_len;
  135. e->data_len = 0;
  136. return e;
  137. }
  138. static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
  139. size_t new_alloc_len)
  140. {
  141. QUIC_URXE *e2, *prev;
  142. if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE))
  143. /* Never attempt to resize a URXE which is not on the free list. */
  144. return NULL;
  145. prev = ossl_list_urxe_prev(e);
  146. ossl_list_urxe_remove(&demux->urx_free, e);
  147. e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);
  148. if (e2 == NULL) {
  149. /* Failed to resize, abort. */
  150. if (prev == NULL)
  151. ossl_list_urxe_insert_head(&demux->urx_free, e);
  152. else
  153. ossl_list_urxe_insert_after(&demux->urx_free, prev, e);
  154. return NULL;
  155. }
  156. if (prev == NULL)
  157. ossl_list_urxe_insert_head(&demux->urx_free, e2);
  158. else
  159. ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);
  160. e2->alloc_len = new_alloc_len;
  161. return e2;
  162. }
  163. static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
  164. size_t alloc_len)
  165. {
  166. return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;
  167. }
  168. static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
  169. {
  170. QUIC_URXE *e;
  171. while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
  172. e = demux_alloc_urxe(demux->mtu);
  173. if (e == NULL)
  174. return 0;
  175. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  176. e->demux_state = URXE_DEMUX_STATE_FREE;
  177. }
  178. return 1;
  179. }
  180. /*
  181. * Receive datagrams from network, placing them into URXEs.
  182. *
  183. * Returns 1 on success or 0 on failure.
  184. *
  185. * Precondition: at least one URXE is free
  186. * Precondition: there are no pending URXEs
  187. */
  188. static int demux_recv(QUIC_DEMUX *demux)
  189. {
  190. BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
  191. size_t rd, i;
  192. QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
  193. OSSL_TIME now;
  194. /* This should never be called when we have any pending URXE. */
  195. assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
  196. assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
  197. if (demux->net_bio == NULL)
  198. /*
  199. * If no BIO is plugged in, treat this as no datagram being available.
  200. */
  201. return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
  202. /*
  203. * Opportunistically receive as many messages as possible in a single
  204. * syscall, determined by how many free URXEs are available.
  205. */
  206. for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
  207. ++i, urxe = ossl_list_urxe_next(urxe)) {
  208. if (urxe == NULL) {
  209. /* We need at least one URXE to receive into. */
  210. if (!ossl_assert(i > 0))
  211. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  212. break;
  213. }
  214. /* Ensure the URXE is big enough. */
  215. urxe = demux_reserve_urxe(demux, urxe, demux->mtu);
  216. if (urxe == NULL)
  217. /* Allocation error, fail. */
  218. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  219. /* Ensure we zero any fields added to BIO_MSG at a later date. */
  220. memset(&msg[i], 0, sizeof(BIO_MSG));
  221. msg[i].data = ossl_quic_urxe_data(urxe);
  222. msg[i].data_len = urxe->alloc_len;
  223. msg[i].peer = &urxe->peer;
  224. BIO_ADDR_clear(&urxe->peer);
  225. if (demux->use_local_addr)
  226. msg[i].local = &urxe->local;
  227. else
  228. BIO_ADDR_clear(&urxe->local);
  229. }
  230. ERR_set_mark();
  231. if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {
  232. if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
  233. /* Transient error, clear the error and stop. */
  234. ERR_pop_to_mark();
  235. return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
  236. } else {
  237. /* Non-transient error, do not clear the error. */
  238. ERR_clear_last_mark();
  239. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  240. }
  241. }
  242. ERR_clear_last_mark();
  243. now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
  244. urxe = ossl_list_urxe_head(&demux->urx_free);
  245. for (i = 0; i < rd; ++i, urxe = unext) {
  246. unext = ossl_list_urxe_next(urxe);
  247. /* Set URXE with actual length of received datagram. */
  248. urxe->data_len = msg[i].data_len;
  249. /* Time we received datagram. */
  250. urxe->time = now;
  251. urxe->datagram_id = demux->next_datagram_id++;
  252. /* Move from free list to pending list. */
  253. ossl_list_urxe_remove(&demux->urx_free, urxe);
  254. ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
  255. urxe->demux_state = URXE_DEMUX_STATE_PENDING;
  256. }
  257. return QUIC_DEMUX_PUMP_RES_OK;
  258. }
  259. /* Extract destination connection ID from the first packet in a datagram. */
  260. static int demux_identify_conn_id(QUIC_DEMUX *demux,
  261. QUIC_URXE *e,
  262. QUIC_CONN_ID *dst_conn_id)
  263. {
  264. return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
  265. e->data_len,
  266. demux->short_conn_id_len,
  267. dst_conn_id);
  268. }
  269. /*
  270. * Process a single pending URXE.
  271. * Returning 1 on success, 0 on failure.
  272. */
  273. static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
  274. {
  275. QUIC_CONN_ID dst_conn_id;
  276. int dst_conn_id_ok = 0;
  277. /* The next URXE we process should be at the head of the pending list. */
  278. if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
  279. return 0;
  280. assert(e->demux_state == URXE_DEMUX_STATE_PENDING);
  281. /* Determine the DCID of the first packet in the datagram. */
  282. dst_conn_id_ok = demux_identify_conn_id(demux, e, &dst_conn_id);
  283. ossl_list_urxe_remove(&demux->urx_pending, e);
  284. if (demux->default_cb != NULL) {
  285. /*
  286. * Pass to default handler for routing. The URXE now belongs to the
  287. * callback.
  288. */
  289. e->demux_state = URXE_DEMUX_STATE_ISSUED;
  290. demux->default_cb(e, demux->default_cb_arg,
  291. dst_conn_id_ok ? &dst_conn_id : NULL);
  292. } else {
  293. /* Discard. */
  294. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  295. e->demux_state = URXE_DEMUX_STATE_FREE;
  296. }
  297. return 1; /* keep processing pending URXEs */
  298. }
  299. /* Process pending URXEs to generate callbacks. */
  300. static int demux_process_pending_urxl(QUIC_DEMUX *demux)
  301. {
  302. QUIC_URXE *e;
  303. int ret;
  304. while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
  305. if ((ret = demux_process_pending_urxe(demux, e)) <= 0)
  306. return ret;
  307. return 1;
  308. }
  309. /*
  310. * Drain the pending URXE list, processing any pending URXEs by making their
  311. * callbacks. If no URXEs are pending, a network read is attempted first.
  312. */
  313. int ossl_quic_demux_pump(QUIC_DEMUX *demux)
  314. {
  315. int ret;
  316. if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
  317. ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
  318. if (ret != 1)
  319. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  320. ret = demux_recv(demux);
  321. if (ret != QUIC_DEMUX_PUMP_RES_OK)
  322. return ret;
  323. /*
  324. * If demux_recv returned successfully, we should always have something.
  325. */
  326. assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
  327. }
  328. if ((ret = demux_process_pending_urxl(demux)) <= 0)
  329. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  330. return QUIC_DEMUX_PUMP_RES_OK;
  331. }
  332. /* Artificially inject a packet into the demuxer for testing purposes. */
  333. int ossl_quic_demux_inject(QUIC_DEMUX *demux,
  334. const unsigned char *buf,
  335. size_t buf_len,
  336. const BIO_ADDR *peer,
  337. const BIO_ADDR *local)
  338. {
  339. int ret;
  340. QUIC_URXE *urxe;
  341. ret = demux_ensure_free_urxe(demux, 1);
  342. if (ret != 1)
  343. return 0;
  344. urxe = ossl_list_urxe_head(&demux->urx_free);
  345. assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
  346. urxe = demux_reserve_urxe(demux, urxe, buf_len);
  347. if (urxe == NULL)
  348. return 0;
  349. memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
  350. urxe->data_len = buf_len;
  351. if (peer != NULL)
  352. urxe->peer = *peer;
  353. else
  354. BIO_ADDR_clear(&urxe->peer);
  355. if (local != NULL)
  356. urxe->local = *local;
  357. else
  358. BIO_ADDR_clear(&urxe->local);
  359. urxe->time
  360. = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
  361. /* Move from free list to pending list. */
  362. ossl_list_urxe_remove(&demux->urx_free, urxe);
  363. ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
  364. urxe->demux_state = URXE_DEMUX_STATE_PENDING;
  365. return demux_process_pending_urxl(demux) > 0;
  366. }
  367. /* Called by our user to return a URXE to the free list. */
  368. void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
  369. QUIC_URXE *e)
  370. {
  371. assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
  372. assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
  373. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  374. e->demux_state = URXE_DEMUX_STATE_FREE;
  375. }
  376. void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,
  377. QUIC_URXE *e)
  378. {
  379. assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
  380. assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
  381. ossl_list_urxe_insert_head(&demux->urx_pending, e);
  382. e->demux_state = URXE_DEMUX_STATE_PENDING;
  383. }
  384. int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux)
  385. {
  386. return ossl_list_urxe_head(&demux->urx_pending) != NULL;
  387. }