quic_demux.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "internal/quic_demux.h"
  2. #include "internal/quic_wire_pkt.h"
  3. #include "internal/common.h"
  4. #include <openssl/lhash.h>
  5. #define DEMUX_MAX_MSGS_PER_CALL 32
  6. void ossl_quic_urxe_remove(QUIC_URXE_LIST *l, QUIC_URXE *e)
  7. {
  8. /* Must be in list currently. */
  9. assert((e->prev != NULL || l->head == e)
  10. && (e->next != NULL || l->tail == e));
  11. if (e->prev != NULL)
  12. e->prev->next = e->next;
  13. if (e->next != NULL)
  14. e->next->prev = e->prev;
  15. if (e == l->head)
  16. l->head = e->next;
  17. if (e == l->tail)
  18. l->tail = e->prev;
  19. e->next = e->prev = NULL;
  20. }
  21. void ossl_quic_urxe_insert_head(QUIC_URXE_LIST *l, QUIC_URXE *e)
  22. {
  23. /* Must not be in list. */
  24. assert(e->prev == NULL && e->next == NULL);
  25. if (l->head == NULL) {
  26. l->head = l->tail = e;
  27. e->next = e->prev = NULL;
  28. return;
  29. }
  30. l->head->prev = e;
  31. e->next = l->head;
  32. e->prev = NULL;
  33. l->head = e;
  34. }
  35. void ossl_quic_urxe_insert_tail(QUIC_URXE_LIST *l, QUIC_URXE *e)
  36. {
  37. /* Must not be in list. */
  38. assert(e->prev == NULL && e->next == NULL);
  39. if (l->tail == NULL) {
  40. l->head = l->tail = e;
  41. e->next = e->prev = NULL;
  42. return;
  43. }
  44. l->tail->next = e;
  45. e->prev = l->tail;
  46. e->next = NULL;
  47. l->tail = e;
  48. }
  49. /* Structure used to track a given connection ID. */
  50. typedef struct quic_demux_conn_st QUIC_DEMUX_CONN;
  51. struct quic_demux_conn_st {
  52. QUIC_DEMUX_CONN *next; /* used when unregistering only */
  53. QUIC_CONN_ID dst_conn_id;
  54. ossl_quic_demux_cb_fn *cb;
  55. void *cb_arg;
  56. };
  57. DEFINE_LHASH_OF_EX(QUIC_DEMUX_CONN);
  58. static unsigned long demux_conn_hash(const QUIC_DEMUX_CONN *conn)
  59. {
  60. size_t i;
  61. unsigned long v = 0;
  62. assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
  63. for (i = 0; i < conn->dst_conn_id.id_len; ++i)
  64. v ^= ((unsigned long)conn->dst_conn_id.id[i])
  65. << ((i * 8) % (sizeof(unsigned long) * 8));
  66. return v;
  67. }
  68. static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
  69. {
  70. return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
  71. }
  72. struct quic_demux_st {
  73. /* The underlying transport BIO with datagram semantics. */
  74. BIO *net_bio;
  75. /*
  76. * QUIC short packets do not contain the length of the connection ID field,
  77. * therefore it must be known contextually. The demuxer requires connection
  78. * IDs of the same length to be used for all incoming packets.
  79. */
  80. size_t short_conn_id_len;
  81. /* Default URXE buffer size in bytes. */
  82. size_t default_urxe_alloc_len;
  83. /* Time retrieval callback. */
  84. OSSL_TIME (*now)(void *arg);
  85. void *now_arg;
  86. /* Hashtable mapping connection IDs to QUIC_DEMUX_CONN structures. */
  87. LHASH_OF(QUIC_DEMUX_CONN) *conns_by_id;
  88. /*
  89. * List of URXEs which are not currently in use (i.e., not filled with
  90. * unconsumed data). These are moved to the pending list as they are filled.
  91. */
  92. QUIC_URXE_LIST urx_free;
  93. size_t num_urx_free;
  94. /*
  95. * List of URXEs which are filled with received encrypted data. These are
  96. * removed from this list as we invoke the callbacks for each of them. They
  97. * are then not on any list managed by us; we forget about them until our
  98. * user calls ossl_quic_demux_release_urxe to return the URXE to us, at
  99. * which point we add it to the free list.
  100. */
  101. QUIC_URXE_LIST urx_pending;
  102. /* Whether to use local address support. */
  103. char use_local_addr;
  104. };
  105. QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
  106. size_t short_conn_id_len,
  107. size_t default_urxe_alloc_len,
  108. OSSL_TIME (*now)(void *arg),
  109. void *now_arg)
  110. {
  111. QUIC_DEMUX *demux;
  112. demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
  113. if (demux == NULL)
  114. return NULL;
  115. demux->net_bio = net_bio;
  116. demux->short_conn_id_len = short_conn_id_len;
  117. demux->default_urxe_alloc_len = default_urxe_alloc_len;
  118. demux->now = now;
  119. demux->now_arg = now_arg;
  120. demux->conns_by_id
  121. = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
  122. if (demux->conns_by_id == NULL) {
  123. OPENSSL_free(demux);
  124. return NULL;
  125. }
  126. if (net_bio != NULL
  127. && BIO_dgram_get_local_addr_cap(net_bio)
  128. && BIO_dgram_set_local_addr_enable(net_bio, 1))
  129. demux->use_local_addr = 1;
  130. return demux;
  131. }
  132. static void demux_free_conn_it(QUIC_DEMUX_CONN *conn, void *arg)
  133. {
  134. OPENSSL_free(conn);
  135. }
  136. static void demux_free_urxl(QUIC_URXE_LIST *l)
  137. {
  138. QUIC_URXE *e, *enext;
  139. for (e = l->head; e != NULL; e = enext) {
  140. enext = e->next;
  141. OPENSSL_free(e);
  142. }
  143. l->head = l->tail = NULL;
  144. }
  145. void ossl_quic_demux_free(QUIC_DEMUX *demux)
  146. {
  147. if (demux == NULL)
  148. return;
  149. /* Free all connection structures. */
  150. lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
  151. lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
  152. /* Free all URXEs we are holding. */
  153. demux_free_urxl(&demux->urx_free);
  154. demux_free_urxl(&demux->urx_pending);
  155. OPENSSL_free(demux);
  156. }
  157. static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
  158. const QUIC_CONN_ID *dst_conn_id)
  159. {
  160. QUIC_DEMUX_CONN key;
  161. if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
  162. return NULL;
  163. key.dst_conn_id = *dst_conn_id;
  164. return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
  165. }
  166. int ossl_quic_demux_register(QUIC_DEMUX *demux,
  167. const QUIC_CONN_ID *dst_conn_id,
  168. ossl_quic_demux_cb_fn *cb, void *cb_arg)
  169. {
  170. QUIC_DEMUX_CONN *conn;
  171. if (dst_conn_id == NULL
  172. || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
  173. || cb == NULL)
  174. return 0;
  175. /* Ensure not already registered. */
  176. if (demux_get_by_conn_id(demux, dst_conn_id) != NULL)
  177. /* Handler already registered with this connection ID. */
  178. return 0;
  179. conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
  180. if (conn == NULL)
  181. return 0;
  182. conn->dst_conn_id = *dst_conn_id;
  183. conn->cb = cb;
  184. conn->cb_arg = cb_arg;
  185. lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
  186. return 1;
  187. }
  188. static void demux_unregister(QUIC_DEMUX *demux,
  189. QUIC_DEMUX_CONN *conn)
  190. {
  191. lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
  192. OPENSSL_free(conn);
  193. }
  194. int ossl_quic_demux_unregister(QUIC_DEMUX *demux,
  195. const QUIC_CONN_ID *dst_conn_id)
  196. {
  197. QUIC_DEMUX_CONN *conn;
  198. if (dst_conn_id == NULL
  199. || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
  200. return 0;
  201. conn = demux_get_by_conn_id(demux, dst_conn_id);
  202. if (conn == NULL)
  203. return 0;
  204. demux_unregister(demux, conn);
  205. return 1;
  206. }
  207. struct unreg_arg {
  208. ossl_quic_demux_cb_fn *cb;
  209. void *cb_arg;
  210. QUIC_DEMUX_CONN *head;
  211. };
  212. static void demux_unregister_by_cb(QUIC_DEMUX_CONN *conn, void *arg_)
  213. {
  214. struct unreg_arg *arg = arg_;
  215. if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
  216. conn->next = arg->head;
  217. arg->head = conn;
  218. }
  219. }
  220. void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
  221. ossl_quic_demux_cb_fn *cb,
  222. void *cb_arg)
  223. {
  224. QUIC_DEMUX_CONN *conn, *cnext;
  225. struct unreg_arg arg = {0};
  226. arg.cb = cb;
  227. arg.cb_arg = cb_arg;
  228. lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
  229. demux_unregister_by_cb, &arg);
  230. for (conn = arg.head; conn != NULL; conn = cnext) {
  231. cnext = conn->next;
  232. demux_unregister(demux, conn);
  233. }
  234. }
  235. static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
  236. {
  237. QUIC_URXE *e;
  238. if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
  239. return NULL;
  240. e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
  241. if (e == NULL)
  242. return NULL;
  243. e->prev = e->next = NULL;
  244. e->alloc_len = alloc_len;
  245. e->data_len = 0;
  246. return e;
  247. }
  248. static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
  249. {
  250. QUIC_URXE *e;
  251. while (demux->num_urx_free < min_num_free) {
  252. e = demux_alloc_urxe(demux->default_urxe_alloc_len);
  253. if (e == NULL)
  254. return 0;
  255. ossl_quic_urxe_insert_tail(&demux->urx_free, e);
  256. ++demux->num_urx_free;
  257. }
  258. return 1;
  259. }
  260. /*
  261. * Receive datagrams from network, placing them into URXEs.
  262. *
  263. * Returns 1 on success or 0 on failure.
  264. *
  265. * Precondition: at least one URXE is free
  266. * Precondition: there are no pending URXEs
  267. */
  268. static int demux_recv(QUIC_DEMUX *demux)
  269. {
  270. BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
  271. size_t rd, i;
  272. QUIC_URXE *urxe = demux->urx_free.head, *unext;
  273. OSSL_TIME now;
  274. /* This should never be called when we have any pending URXE. */
  275. assert(demux->urx_pending.head == NULL);
  276. if (demux->net_bio == NULL)
  277. return 0;
  278. /*
  279. * Opportunistically receive as many messages as possible in a single
  280. * syscall, determined by how many free URXEs are available.
  281. */
  282. for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg); ++i, urxe = urxe->next) {
  283. if (urxe == NULL) {
  284. /* We need at least one URXE to receive into. */
  285. if (!ossl_assert(i > 0))
  286. return 0;
  287. break;
  288. }
  289. /* Ensure we zero any fields added to BIO_MSG at a later date. */
  290. memset(&msg[i], 0, sizeof(BIO_MSG));
  291. msg[i].data = ossl_quic_urxe_data(urxe);
  292. msg[i].data_len = urxe->alloc_len;
  293. msg[i].peer = &urxe->peer;
  294. if (demux->use_local_addr)
  295. msg[i].local = &urxe->local;
  296. else
  297. BIO_ADDR_clear(&urxe->local);
  298. }
  299. if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd))
  300. return 0;
  301. now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
  302. urxe = demux->urx_free.head;
  303. for (i = 0; i < rd; ++i, urxe = unext) {
  304. unext = urxe->next;
  305. /* Set URXE with actual length of received datagram. */
  306. urxe->data_len = msg[i].data_len;
  307. /* Time we received datagram. */
  308. urxe->time = now;
  309. /* Move from free list to pending list. */
  310. ossl_quic_urxe_remove(&demux->urx_free, urxe);
  311. --demux->num_urx_free;
  312. ossl_quic_urxe_insert_tail(&demux->urx_pending, urxe);
  313. }
  314. return 1;
  315. }
  316. /* Extract destination connection ID from the first packet in a datagram. */
  317. static int demux_identify_conn_id(QUIC_DEMUX *demux,
  318. QUIC_URXE *e,
  319. QUIC_CONN_ID *dst_conn_id)
  320. {
  321. return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
  322. e->data_len,
  323. demux->short_conn_id_len,
  324. dst_conn_id);
  325. }
  326. /* Identify the connection structure corresponding to a given URXE. */
  327. static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e)
  328. {
  329. QUIC_CONN_ID dst_conn_id;
  330. if (!demux_identify_conn_id(demux, e, &dst_conn_id))
  331. /*
  332. * Datagram is so badly malformed we can't get the DCID from the first
  333. * packet in it, so just give up.
  334. */
  335. return NULL;
  336. return demux_get_by_conn_id(demux, &dst_conn_id);
  337. }
  338. /* Process a single pending URXE. */
  339. static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
  340. {
  341. QUIC_DEMUX_CONN *conn;
  342. /* The next URXE we process should be at the head of the pending list. */
  343. if (!ossl_assert(e == demux->urx_pending.head))
  344. return 0;
  345. conn = demux_identify_conn(demux, e);
  346. if (conn == NULL) {
  347. /*
  348. * We could not identify a connection. We will never be able to process
  349. * this datagram, so get rid of it.
  350. */
  351. ossl_quic_urxe_remove(&demux->urx_pending, e);
  352. ossl_quic_urxe_insert_tail(&demux->urx_free, e);
  353. ++demux->num_urx_free;
  354. return 1; /* keep processing pending URXEs */
  355. }
  356. /*
  357. * Remove from list and invoke callback. The URXE now belongs to the
  358. * callback. (QUIC_DEMUX_CONN never has non-NULL cb.)
  359. */
  360. ossl_quic_urxe_remove(&demux->urx_pending, e);
  361. conn->cb(e, conn->cb_arg);
  362. return 1;
  363. }
  364. /* Process pending URXEs to generate callbacks. */
  365. static int demux_process_pending_urxl(QUIC_DEMUX *demux)
  366. {
  367. QUIC_URXE *e;
  368. while ((e = demux->urx_pending.head) != NULL)
  369. if (!demux_process_pending_urxe(demux, e))
  370. return 0;
  371. return 1;
  372. }
  373. /*
  374. * Drain the pending URXE list, processing any pending URXEs by making their
  375. * callbacks. If no URXEs are pending, a network read is attempted first.
  376. */
  377. int ossl_quic_demux_pump(QUIC_DEMUX *demux)
  378. {
  379. int ret;
  380. if (demux->urx_pending.head == NULL) {
  381. ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
  382. if (ret != 1)
  383. return 0;
  384. ret = demux_recv(demux);
  385. if (ret != 1)
  386. return 0;
  387. /*
  388. * If demux_recv returned successfully, we should always have something.
  389. */
  390. assert(demux->urx_pending.head != NULL);
  391. }
  392. return demux_process_pending_urxl(demux);
  393. }
  394. /* Artificially inject a packet into the demuxer for testing purposes. */
  395. int ossl_quic_demux_inject(QUIC_DEMUX *demux,
  396. const unsigned char *buf,
  397. size_t buf_len,
  398. const BIO_ADDR *peer,
  399. const BIO_ADDR *local)
  400. {
  401. int ret;
  402. QUIC_URXE *urxe;
  403. ret = demux_ensure_free_urxe(demux, 1);
  404. if (ret != 1)
  405. return 0;
  406. urxe = demux->urx_free.head;
  407. if (buf_len > urxe->alloc_len)
  408. return 0;
  409. memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
  410. urxe->data_len = buf_len;
  411. if (peer != NULL)
  412. urxe->peer = *peer;
  413. else
  414. BIO_ADDR_clear(&urxe->local);
  415. if (local != NULL)
  416. urxe->local = *local;
  417. else
  418. BIO_ADDR_clear(&urxe->local);
  419. /* Move from free list to pending list. */
  420. ossl_quic_urxe_remove(&demux->urx_free, urxe);
  421. --demux->num_urx_free;
  422. ossl_quic_urxe_insert_tail(&demux->urx_pending, urxe);
  423. return demux_process_pending_urxl(demux);
  424. }
  425. /* Called by our user to return a URXE to the free list. */
  426. void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
  427. QUIC_URXE *e)
  428. {
  429. assert(e->prev == NULL && e->next == NULL);
  430. ossl_quic_urxe_insert_tail(&demux->urx_free, e);
  431. ++demux->num_urx_free;
  432. }