2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/quic_demux.h"
11 #include "internal/quic_wire_pkt.h"
12 #include "internal/common.h"
13 #include <openssl/lhash.h>
14 #include <openssl/err.h>
16 #define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */
17 #define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */
18 #define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */
20 #define DEMUX_MAX_MSGS_PER_CALL 32
22 #define DEMUX_DEFAULT_MTU 1500
24 /* Structure used to track a given connection ID. */
25 typedef struct quic_demux_conn_st QUIC_DEMUX_CONN;
27 struct quic_demux_conn_st {
28 QUIC_DEMUX_CONN *next; /* used when unregistering only */
29 QUIC_CONN_ID dst_conn_id;
30 ossl_quic_demux_cb_fn *cb;
34 DEFINE_LHASH_OF_EX(QUIC_DEMUX_CONN);
36 static unsigned long demux_conn_hash(const QUIC_DEMUX_CONN *conn)
41 assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
43 for (i = 0; i < conn->dst_conn_id.id_len; ++i)
44 v ^= ((unsigned long)conn->dst_conn_id.id[i])
45 << ((i * 8) % (sizeof(unsigned long) * 8));
50 static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
52 return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
55 struct quic_demux_st {
56 /* The underlying transport BIO with datagram semantics. */
60 * QUIC short packets do not contain the length of the connection ID field,
61 * therefore it must be known contextually. The demuxer requires connection
62 * IDs of the same length to be used for all incoming packets.
64 size_t short_conn_id_len;
67 * Our current understanding of the upper bound on an incoming datagram size
72 /* Time retrieval callback. */
73 OSSL_TIME (*now)(void *arg);
76 /* Hashtable mapping connection IDs to QUIC_DEMUX_CONN structures. */
77 LHASH_OF(QUIC_DEMUX_CONN) *conns_by_id;
79 /* The default packet handler, if any. */
80 ossl_quic_demux_cb_fn *default_cb;
84 * List of URXEs which are not currently in use (i.e., not filled with
85 * unconsumed data). These are moved to the pending list as they are filled.
87 QUIC_URXE_LIST urx_free;
90 * List of URXEs which are filled with received encrypted data. These are
91 * removed from this list as we invoke the callbacks for each of them. They
92 * are then not on any list managed by us; we forget about them until our
93 * user calls ossl_quic_demux_release_urxe to return the URXE to us, at
94 * which point we add it to the free list.
96 QUIC_URXE_LIST urx_pending;
98 /* Whether to use local address support. */
102 QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
103 size_t short_conn_id_len,
104 OSSL_TIME (*now)(void *arg),
109 demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
113 demux->net_bio = net_bio;
114 demux->short_conn_id_len = short_conn_id_len;
115 /* We update this if possible when we get a BIO. */
116 demux->mtu = DEMUX_DEFAULT_MTU;
118 demux->now_arg = now_arg;
121 = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
122 if (demux->conns_by_id == NULL) {
128 && BIO_dgram_get_local_addr_cap(net_bio)
129 && BIO_dgram_set_local_addr_enable(net_bio, 1))
130 demux->use_local_addr = 1;
135 static void demux_free_conn_it(QUIC_DEMUX_CONN *conn, void *arg)
140 static void demux_free_urxl(QUIC_URXE_LIST *l)
142 QUIC_URXE *e, *enext;
144 for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
145 enext = ossl_list_urxe_next(e);
146 ossl_list_urxe_remove(l, e);
151 void ossl_quic_demux_free(QUIC_DEMUX *demux)
156 /* Free all connection structures. */
157 lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
158 lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
160 /* Free all URXEs we are holding. */
161 demux_free_urxl(&demux->urx_free);
162 demux_free_urxl(&demux->urx_pending);
167 void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)
171 demux->net_bio = net_bio;
173 if (net_bio != NULL) {
175 * Try to determine our MTU if possible. The BIO is not required to
176 * support this, in which case we remain at the last known MTU, or our
179 mtu = BIO_dgram_get_mtu(net_bio);
180 if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)
181 ossl_quic_demux_set_mtu(demux, mtu); /* best effort */
185 int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)
187 if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)
194 static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
195 const QUIC_CONN_ID *dst_conn_id)
199 if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
202 key.dst_conn_id = *dst_conn_id;
203 return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
206 int ossl_quic_demux_register(QUIC_DEMUX *demux,
207 const QUIC_CONN_ID *dst_conn_id,
208 ossl_quic_demux_cb_fn *cb, void *cb_arg)
210 QUIC_DEMUX_CONN *conn;
212 if (dst_conn_id == NULL
213 || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
217 /* Ensure not already registered. */
218 if (demux_get_by_conn_id(demux, dst_conn_id) != NULL)
219 /* Handler already registered with this connection ID. */
222 conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
226 conn->dst_conn_id = *dst_conn_id;
228 conn->cb_arg = cb_arg;
230 lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
234 static void demux_unregister(QUIC_DEMUX *demux,
235 QUIC_DEMUX_CONN *conn)
237 lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
241 int ossl_quic_demux_unregister(QUIC_DEMUX *demux,
242 const QUIC_CONN_ID *dst_conn_id)
244 QUIC_DEMUX_CONN *conn;
246 if (dst_conn_id == NULL
247 || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
250 conn = demux_get_by_conn_id(demux, dst_conn_id);
254 demux_unregister(demux, conn);
259 ossl_quic_demux_cb_fn *cb;
261 QUIC_DEMUX_CONN *head;
264 static void demux_unregister_by_cb(QUIC_DEMUX_CONN *conn, void *arg_)
266 struct unreg_arg *arg = arg_;
268 if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
269 conn->next = arg->head;
274 void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
275 ossl_quic_demux_cb_fn *cb,
278 QUIC_DEMUX_CONN *conn, *cnext;
279 struct unreg_arg arg = {0};
283 lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
284 demux_unregister_by_cb, &arg);
286 for (conn = arg.head; conn != NULL; conn = cnext) {
288 demux_unregister(demux, conn);
292 void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
293 ossl_quic_demux_cb_fn *cb,
296 demux->default_cb = cb;
297 demux->default_cb_arg = cb_arg;
300 static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
304 if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
307 e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
311 ossl_list_urxe_init_elem(e);
312 e->alloc_len = alloc_len;
317 static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
318 size_t new_alloc_len)
320 QUIC_URXE *e2, *prev;
322 if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE))
323 /* Never attempt to resize a URXE which is not on the free list. */
326 prev = ossl_list_urxe_prev(e);
327 ossl_list_urxe_remove(&demux->urx_free, e);
329 e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);
331 /* Failed to resize, abort. */
333 ossl_list_urxe_insert_head(&demux->urx_free, e);
335 ossl_list_urxe_insert_after(&demux->urx_free, prev, e);
341 ossl_list_urxe_insert_head(&demux->urx_free, e2);
343 ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);
345 e2->alloc_len = new_alloc_len;
349 static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
352 return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;
355 static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
359 while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
360 e = demux_alloc_urxe(demux->mtu);
364 ossl_list_urxe_insert_tail(&demux->urx_free, e);
365 e->demux_state = URXE_DEMUX_STATE_FREE;
372 * Receive datagrams from network, placing them into URXEs.
374 * Returns 1 on success or 0 on failure.
376 * Precondition: at least one URXE is free
377 * Precondition: there are no pending URXEs
379 static int demux_recv(QUIC_DEMUX *demux)
381 BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
383 QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
386 /* This should never be called when we have any pending URXE. */
387 assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
388 assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
390 if (demux->net_bio == NULL)
392 * If no BIO is plugged in, treat this as no datagram being available.
394 return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
397 * Opportunistically receive as many messages as possible in a single
398 * syscall, determined by how many free URXEs are available.
400 for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
401 ++i, urxe = ossl_list_urxe_next(urxe)) {
403 /* We need at least one URXE to receive into. */
404 if (!ossl_assert(i > 0))
405 return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
410 /* Ensure the URXE is big enough. */
411 urxe = demux_reserve_urxe(demux, urxe, demux->mtu);
413 /* Allocation error, fail. */
414 return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
416 /* Ensure we zero any fields added to BIO_MSG at a later date. */
417 memset(&msg[i], 0, sizeof(BIO_MSG));
418 msg[i].data = ossl_quic_urxe_data(urxe);
419 msg[i].data_len = urxe->alloc_len;
420 msg[i].peer = &urxe->peer;
421 BIO_ADDR_clear(&urxe->peer);
422 if (demux->use_local_addr)
423 msg[i].local = &urxe->local;
425 BIO_ADDR_clear(&urxe->local);
429 if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {
430 if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
431 /* Transient error, clear the error and stop. */
433 return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
435 /* Non-transient error, do not clear the error. */
436 ERR_clear_last_mark();
437 return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
441 ERR_clear_last_mark();
442 now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
444 urxe = ossl_list_urxe_head(&demux->urx_free);
445 for (i = 0; i < rd; ++i, urxe = unext) {
446 unext = ossl_list_urxe_next(urxe);
447 /* Set URXE with actual length of received datagram. */
448 urxe->data_len = msg[i].data_len;
449 /* Time we received datagram. */
451 /* Move from free list to pending list. */
452 ossl_list_urxe_remove(&demux->urx_free, urxe);
453 ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
454 urxe->demux_state = URXE_DEMUX_STATE_PENDING;
457 return QUIC_DEMUX_PUMP_RES_OK;
460 /* Extract destination connection ID from the first packet in a datagram. */
461 static int demux_identify_conn_id(QUIC_DEMUX *demux,
463 QUIC_CONN_ID *dst_conn_id)
465 return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
467 demux->short_conn_id_len,
471 /* Identify the connection structure corresponding to a given URXE. */
472 static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e)
474 QUIC_CONN_ID dst_conn_id;
476 if (!demux_identify_conn_id(demux, e, &dst_conn_id))
478 * Datagram is so badly malformed we can't get the DCID from the first
479 * packet in it, so just give up.
483 return demux_get_by_conn_id(demux, &dst_conn_id);
486 /* Process a single pending URXE. */
487 static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
489 QUIC_DEMUX_CONN *conn;
491 /* The next URXE we process should be at the head of the pending list. */
492 if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
495 assert(e->demux_state == URXE_DEMUX_STATE_PENDING);
497 conn = demux_identify_conn(demux, e);
500 * We could not identify a connection. If we have a default packet
501 * handler, pass it to the handler. Otherwise, we will never be able to
502 * process this datagram, so get rid of it.
504 ossl_list_urxe_remove(&demux->urx_pending, e);
505 if (demux->default_cb != NULL) {
506 /* Pass to default handler. */
507 e->demux_state = URXE_DEMUX_STATE_ISSUED;
508 demux->default_cb(e, demux->default_cb_arg);
511 ossl_list_urxe_insert_tail(&demux->urx_free, e);
512 e->demux_state = URXE_DEMUX_STATE_FREE;
514 return 1; /* keep processing pending URXEs */
518 * Remove from list and invoke callback. The URXE now belongs to the
519 * callback. (QUIC_DEMUX_CONN never has non-NULL cb.)
521 ossl_list_urxe_remove(&demux->urx_pending, e);
522 e->demux_state = URXE_DEMUX_STATE_ISSUED;
523 conn->cb(e, conn->cb_arg);
527 /* Process pending URXEs to generate callbacks. */
528 static int demux_process_pending_urxl(QUIC_DEMUX *demux)
532 while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
533 if (!demux_process_pending_urxe(demux, e))
540 * Drain the pending URXE list, processing any pending URXEs by making their
541 * callbacks. If no URXEs are pending, a network read is attempted first.
543 int ossl_quic_demux_pump(QUIC_DEMUX *demux)
547 if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
548 ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
550 return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
552 ret = demux_recv(demux);
553 if (ret != QUIC_DEMUX_PUMP_RES_OK)
557 * If demux_recv returned successfully, we should always have something.
559 assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
562 if (!demux_process_pending_urxl(demux))
563 return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
565 return QUIC_DEMUX_PUMP_RES_OK;
568 /* Artificially inject a packet into the demuxer for testing purposes. */
569 int ossl_quic_demux_inject(QUIC_DEMUX *demux,
570 const unsigned char *buf,
572 const BIO_ADDR *peer,
573 const BIO_ADDR *local)
578 ret = demux_ensure_free_urxe(demux, 1);
582 urxe = ossl_list_urxe_head(&demux->urx_free);
584 assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
586 urxe = demux_reserve_urxe(demux, urxe, buf_len);
590 memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
591 urxe->data_len = buf_len;
596 BIO_ADDR_clear(&urxe->peer);
599 urxe->local = *local;
601 BIO_ADDR_clear(&urxe->local);
604 = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
606 /* Move from free list to pending list. */
607 ossl_list_urxe_remove(&demux->urx_free, urxe);
608 ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
609 urxe->demux_state = URXE_DEMUX_STATE_PENDING;
611 return demux_process_pending_urxl(demux);
614 /* Called by our user to return a URXE to the free list. */
615 void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
618 assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
619 assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
620 ossl_list_urxe_insert_tail(&demux->urx_free, e);
621 e->demux_state = URXE_DEMUX_STATE_FREE;
624 void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,
627 assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
628 assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
629 ossl_list_urxe_insert_head(&demux->urx_pending, e);
630 e->demux_state = URXE_DEMUX_STATE_PENDING;