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>
15 #define DEMUX_MAX_MSGS_PER_CALL 32
17 /* Structure used to track a given connection ID. */
18 typedef struct quic_demux_conn_st QUIC_DEMUX_CONN;
20 struct quic_demux_conn_st {
21 QUIC_DEMUX_CONN *next; /* used when unregistering only */
22 QUIC_CONN_ID dst_conn_id;
23 ossl_quic_demux_cb_fn *cb;
27 DEFINE_LHASH_OF_EX(QUIC_DEMUX_CONN);
29 static unsigned long demux_conn_hash(const QUIC_DEMUX_CONN *conn)
34 assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
36 for (i = 0; i < conn->dst_conn_id.id_len; ++i)
37 v ^= ((unsigned long)conn->dst_conn_id.id[i])
38 << ((i * 8) % (sizeof(unsigned long) * 8));
43 static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
45 return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
48 struct quic_demux_st {
49 /* The underlying transport BIO with datagram semantics. */
53 * QUIC short packets do not contain the length of the connection ID field,
54 * therefore it must be known contextually. The demuxer requires connection
55 * IDs of the same length to be used for all incoming packets.
57 size_t short_conn_id_len;
59 /* Default URXE buffer size in bytes. */
60 size_t default_urxe_alloc_len;
62 /* Time retrieval callback. */
63 OSSL_TIME (*now)(void *arg);
66 /* Hashtable mapping connection IDs to QUIC_DEMUX_CONN structures. */
67 LHASH_OF(QUIC_DEMUX_CONN) *conns_by_id;
70 * List of URXEs which are not currently in use (i.e., not filled with
71 * unconsumed data). These are moved to the pending list as they are filled.
73 QUIC_URXE_LIST urx_free;
76 * List of URXEs which are filled with received encrypted data. These are
77 * removed from this list as we invoke the callbacks for each of them. They
78 * are then not on any list managed by us; we forget about them until our
79 * user calls ossl_quic_demux_release_urxe to return the URXE to us, at
80 * which point we add it to the free list.
82 QUIC_URXE_LIST urx_pending;
84 /* Whether to use local address support. */
88 QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
89 size_t short_conn_id_len,
90 size_t default_urxe_alloc_len,
91 OSSL_TIME (*now)(void *arg),
96 demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
100 demux->net_bio = net_bio;
101 demux->short_conn_id_len = short_conn_id_len;
102 demux->default_urxe_alloc_len = default_urxe_alloc_len;
104 demux->now_arg = now_arg;
107 = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
108 if (demux->conns_by_id == NULL) {
114 && BIO_dgram_get_local_addr_cap(net_bio)
115 && BIO_dgram_set_local_addr_enable(net_bio, 1))
116 demux->use_local_addr = 1;
121 static void demux_free_conn_it(QUIC_DEMUX_CONN *conn, void *arg)
126 static void demux_free_urxl(QUIC_URXE_LIST *l)
128 QUIC_URXE *e, *enext;
130 for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
131 enext = ossl_list_urxe_next(e);
132 ossl_list_urxe_remove(l, e);
137 void ossl_quic_demux_free(QUIC_DEMUX *demux)
142 /* Free all connection structures. */
143 lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
144 lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
146 /* Free all URXEs we are holding. */
147 demux_free_urxl(&demux->urx_free);
148 demux_free_urxl(&demux->urx_pending);
153 static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
154 const QUIC_CONN_ID *dst_conn_id)
158 if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
161 key.dst_conn_id = *dst_conn_id;
162 return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
165 int ossl_quic_demux_register(QUIC_DEMUX *demux,
166 const QUIC_CONN_ID *dst_conn_id,
167 ossl_quic_demux_cb_fn *cb, void *cb_arg)
169 QUIC_DEMUX_CONN *conn;
171 if (dst_conn_id == NULL
172 || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
176 /* Ensure not already registered. */
177 if (demux_get_by_conn_id(demux, dst_conn_id) != NULL)
178 /* Handler already registered with this connection ID. */
181 conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
185 conn->dst_conn_id = *dst_conn_id;
187 conn->cb_arg = cb_arg;
189 lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
193 static void demux_unregister(QUIC_DEMUX *demux,
194 QUIC_DEMUX_CONN *conn)
196 lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
200 int ossl_quic_demux_unregister(QUIC_DEMUX *demux,
201 const QUIC_CONN_ID *dst_conn_id)
203 QUIC_DEMUX_CONN *conn;
205 if (dst_conn_id == NULL
206 || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
209 conn = demux_get_by_conn_id(demux, dst_conn_id);
213 demux_unregister(demux, conn);
218 ossl_quic_demux_cb_fn *cb;
220 QUIC_DEMUX_CONN *head;
223 static void demux_unregister_by_cb(QUIC_DEMUX_CONN *conn, void *arg_)
225 struct unreg_arg *arg = arg_;
227 if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
228 conn->next = arg->head;
233 void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
234 ossl_quic_demux_cb_fn *cb,
237 QUIC_DEMUX_CONN *conn, *cnext;
238 struct unreg_arg arg = {0};
242 lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
243 demux_unregister_by_cb, &arg);
245 for (conn = arg.head; conn != NULL; conn = cnext) {
247 demux_unregister(demux, conn);
251 static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
255 if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
258 e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
262 ossl_list_urxe_init_elem(e);
263 e->alloc_len = alloc_len;
268 static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
272 while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
273 e = demux_alloc_urxe(demux->default_urxe_alloc_len);
277 ossl_list_urxe_insert_tail(&demux->urx_free, e);
284 * Receive datagrams from network, placing them into URXEs.
286 * Returns 1 on success or 0 on failure.
288 * Precondition: at least one URXE is free
289 * Precondition: there are no pending URXEs
291 static int demux_recv(QUIC_DEMUX *demux)
293 BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
295 QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
298 /* This should never be called when we have any pending URXE. */
299 assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
301 if (demux->net_bio == NULL)
305 * Opportunistically receive as many messages as possible in a single
306 * syscall, determined by how many free URXEs are available.
308 for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
309 ++i, urxe = ossl_list_urxe_next(urxe)) {
311 /* We need at least one URXE to receive into. */
312 if (!ossl_assert(i > 0))
318 /* Ensure we zero any fields added to BIO_MSG at a later date. */
319 memset(&msg[i], 0, sizeof(BIO_MSG));
320 msg[i].data = ossl_quic_urxe_data(urxe);
321 msg[i].data_len = urxe->alloc_len;
322 msg[i].peer = &urxe->peer;
323 if (demux->use_local_addr)
324 msg[i].local = &urxe->local;
326 BIO_ADDR_clear(&urxe->local);
329 if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd))
332 now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
334 urxe = ossl_list_urxe_head(&demux->urx_free);
335 for (i = 0; i < rd; ++i, urxe = unext) {
336 unext = ossl_list_urxe_next(urxe);
337 /* Set URXE with actual length of received datagram. */
338 urxe->data_len = msg[i].data_len;
339 /* Time we received datagram. */
341 /* Move from free list to pending list. */
342 ossl_list_urxe_remove(&demux->urx_free, urxe);
343 ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
349 /* Extract destination connection ID from the first packet in a datagram. */
350 static int demux_identify_conn_id(QUIC_DEMUX *demux,
352 QUIC_CONN_ID *dst_conn_id)
354 return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
356 demux->short_conn_id_len,
360 /* Identify the connection structure corresponding to a given URXE. */
361 static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e)
363 QUIC_CONN_ID dst_conn_id;
365 if (!demux_identify_conn_id(demux, e, &dst_conn_id))
367 * Datagram is so badly malformed we can't get the DCID from the first
368 * packet in it, so just give up.
372 return demux_get_by_conn_id(demux, &dst_conn_id);
375 /* Process a single pending URXE. */
376 static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
378 QUIC_DEMUX_CONN *conn;
380 /* The next URXE we process should be at the head of the pending list. */
381 if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
384 conn = demux_identify_conn(demux, e);
387 * We could not identify a connection. We will never be able to process
388 * this datagram, so get rid of it.
390 ossl_list_urxe_remove(&demux->urx_pending, e);
391 ossl_list_urxe_insert_tail(&demux->urx_free, e);
392 return 1; /* keep processing pending URXEs */
396 * Remove from list and invoke callback. The URXE now belongs to the
397 * callback. (QUIC_DEMUX_CONN never has non-NULL cb.)
399 ossl_list_urxe_remove(&demux->urx_pending, e);
400 conn->cb(e, conn->cb_arg);
404 /* Process pending URXEs to generate callbacks. */
405 static int demux_process_pending_urxl(QUIC_DEMUX *demux)
409 while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
410 if (!demux_process_pending_urxe(demux, e))
417 * Drain the pending URXE list, processing any pending URXEs by making their
418 * callbacks. If no URXEs are pending, a network read is attempted first.
420 int ossl_quic_demux_pump(QUIC_DEMUX *demux)
424 if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
425 ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
429 ret = demux_recv(demux);
434 * If demux_recv returned successfully, we should always have something.
436 assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
439 return demux_process_pending_urxl(demux);
442 /* Artificially inject a packet into the demuxer for testing purposes. */
443 int ossl_quic_demux_inject(QUIC_DEMUX *demux,
444 const unsigned char *buf,
446 const BIO_ADDR *peer,
447 const BIO_ADDR *local)
452 ret = demux_ensure_free_urxe(demux, 1);
456 urxe = ossl_list_urxe_head(&demux->urx_free);
457 if (buf_len > urxe->alloc_len)
460 memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
461 urxe->data_len = buf_len;
466 BIO_ADDR_clear(&urxe->local);
469 urxe->local = *local;
471 BIO_ADDR_clear(&urxe->local);
473 /* Move from free list to pending list. */
474 ossl_list_urxe_remove(&demux->urx_free, urxe);
475 ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
477 return demux_process_pending_urxl(demux);
480 /* Called by our user to return a URXE to the free list. */
481 void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
484 assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
485 ossl_list_urxe_insert_tail(&demux->urx_free, e);