Path: blob/main/crypto/openssl/ssl/quic/quic_demux.c
48261 views
/*1* Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#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>1415#define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */16#define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */17#define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */1819#define DEMUX_MAX_MSGS_PER_CALL 322021#define DEMUX_DEFAULT_MTU 15002223struct quic_demux_st {24/* The underlying transport BIO with datagram semantics. */25BIO *net_bio;2627/*28* QUIC short packets do not contain the length of the connection ID field,29* therefore it must be known contextually. The demuxer requires connection30* IDs of the same length to be used for all incoming packets.31*/32size_t short_conn_id_len;3334/*35* Our current understanding of the upper bound on an incoming datagram size36* in bytes.37*/38size_t mtu;3940/* The datagram_id to use for the next datagram we receive. */41uint64_t next_datagram_id;4243/* Time retrieval callback. */44OSSL_TIME (*now)(void *arg);45void *now_arg;4647/* The default packet handler, if any. */48ossl_quic_demux_cb_fn *default_cb;49void *default_cb_arg;5051/*52* List of URXEs which are not currently in use (i.e., not filled with53* unconsumed data). These are moved to the pending list as they are filled.54*/55QUIC_URXE_LIST urx_free;5657/*58* List of URXEs which are filled with received encrypted data. These are59* removed from this list as we invoke the callbacks for each of them. They60* are then not on any list managed by us; we forget about them until our61* user calls ossl_quic_demux_release_urxe to return the URXE to us, at62* which point we add it to the free list.63*/64QUIC_URXE_LIST urx_pending;6566/* Whether to use local address support. */67char use_local_addr;68};6970QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,71size_t short_conn_id_len,72OSSL_TIME (*now)(void *arg),73void *now_arg)74{75QUIC_DEMUX *demux;7677demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));78if (demux == NULL)79return NULL;8081demux->net_bio = net_bio;82demux->short_conn_id_len = short_conn_id_len;83/* We update this if possible when we get a BIO. */84demux->mtu = DEMUX_DEFAULT_MTU;85demux->now = now;86demux->now_arg = now_arg;8788if (net_bio != NULL89&& BIO_dgram_get_local_addr_cap(net_bio)90&& BIO_dgram_set_local_addr_enable(net_bio, 1))91demux->use_local_addr = 1;9293return demux;94}9596static void demux_free_urxl(QUIC_URXE_LIST *l)97{98QUIC_URXE *e, *enext;99100for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {101enext = ossl_list_urxe_next(e);102ossl_list_urxe_remove(l, e);103OPENSSL_free(e);104}105}106107void ossl_quic_demux_free(QUIC_DEMUX *demux)108{109if (demux == NULL)110return;111112/* Free all URXEs we are holding. */113demux_free_urxl(&demux->urx_free);114demux_free_urxl(&demux->urx_pending);115116OPENSSL_free(demux);117}118119void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)120{121unsigned int mtu;122123demux->net_bio = net_bio;124125if (net_bio != NULL) {126/*127* Try to determine our MTU if possible. The BIO is not required to128* support this, in which case we remain at the last known MTU, or our129* initial default.130*/131mtu = BIO_dgram_get_mtu(net_bio);132if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)133ossl_quic_demux_set_mtu(demux, mtu); /* best effort */134}135}136137int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)138{139if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)140return 0;141142demux->mtu = mtu;143return 1;144}145146void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,147ossl_quic_demux_cb_fn *cb,148void *cb_arg)149{150demux->default_cb = cb;151demux->default_cb_arg = cb_arg;152}153154static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)155{156QUIC_URXE *e;157158if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))159return NULL;160161e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);162if (e == NULL)163return NULL;164165ossl_list_urxe_init_elem(e);166e->alloc_len = alloc_len;167e->data_len = 0;168return e;169}170171static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,172size_t new_alloc_len)173{174QUIC_URXE *e2, *prev;175176if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE))177/* Never attempt to resize a URXE which is not on the free list. */178return NULL;179180prev = ossl_list_urxe_prev(e);181ossl_list_urxe_remove(&demux->urx_free, e);182183e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);184if (e2 == NULL) {185/* Failed to resize, abort. */186if (prev == NULL)187ossl_list_urxe_insert_head(&demux->urx_free, e);188else189ossl_list_urxe_insert_after(&demux->urx_free, prev, e);190191return NULL;192}193194if (prev == NULL)195ossl_list_urxe_insert_head(&demux->urx_free, e2);196else197ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);198199e2->alloc_len = new_alloc_len;200return e2;201}202203static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,204size_t alloc_len)205{206return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;207}208209static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)210{211QUIC_URXE *e;212213while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {214e = demux_alloc_urxe(demux->mtu);215if (e == NULL)216return 0;217218ossl_list_urxe_insert_tail(&demux->urx_free, e);219e->demux_state = URXE_DEMUX_STATE_FREE;220}221222return 1;223}224225/*226* Receive datagrams from network, placing them into URXEs.227*228* Returns 1 on success or 0 on failure.229*230* Precondition: at least one URXE is free231* Precondition: there are no pending URXEs232*/233static int demux_recv(QUIC_DEMUX *demux)234{235BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];236size_t rd, i;237QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;238OSSL_TIME now;239240/* This should never be called when we have any pending URXE. */241assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);242assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);243244if (demux->net_bio == NULL)245/*246* If no BIO is plugged in, treat this as no datagram being available.247*/248return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;249250/*251* Opportunistically receive as many messages as possible in a single252* syscall, determined by how many free URXEs are available.253*/254for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);255++i, urxe = ossl_list_urxe_next(urxe)) {256if (urxe == NULL) {257/* We need at least one URXE to receive into. */258if (!ossl_assert(i > 0))259return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;260261break;262}263264/* Ensure the URXE is big enough. */265urxe = demux_reserve_urxe(demux, urxe, demux->mtu);266if (urxe == NULL)267/* Allocation error, fail. */268return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;269270/* Ensure we zero any fields added to BIO_MSG at a later date. */271memset(&msg[i], 0, sizeof(BIO_MSG));272msg[i].data = ossl_quic_urxe_data(urxe);273msg[i].data_len = urxe->alloc_len;274msg[i].peer = &urxe->peer;275BIO_ADDR_clear(&urxe->peer);276if (demux->use_local_addr)277msg[i].local = &urxe->local;278else279BIO_ADDR_clear(&urxe->local);280}281282ERR_set_mark();283if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {284if (BIO_err_is_non_fatal(ERR_peek_last_error())) {285/* Transient error, clear the error and stop. */286ERR_pop_to_mark();287return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;288} else {289/* Non-transient error, do not clear the error. */290ERR_clear_last_mark();291return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;292}293}294295ERR_clear_last_mark();296now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();297298urxe = ossl_list_urxe_head(&demux->urx_free);299for (i = 0; i < rd; ++i, urxe = unext) {300unext = ossl_list_urxe_next(urxe);301/* Set URXE with actual length of received datagram. */302urxe->data_len = msg[i].data_len;303/* Time we received datagram. */304urxe->time = now;305urxe->datagram_id = demux->next_datagram_id++;306/* Move from free list to pending list. */307ossl_list_urxe_remove(&demux->urx_free, urxe);308ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);309urxe->demux_state = URXE_DEMUX_STATE_PENDING;310}311312return QUIC_DEMUX_PUMP_RES_OK;313}314315/* Extract destination connection ID from the first packet in a datagram. */316static int demux_identify_conn_id(QUIC_DEMUX *demux,317QUIC_URXE *e,318QUIC_CONN_ID *dst_conn_id)319{320return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),321e->data_len,322demux->short_conn_id_len,323dst_conn_id);324}325326/*327* Process a single pending URXE.328* Returning 1 on success, 0 on failure.329*/330static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)331{332QUIC_CONN_ID dst_conn_id;333int dst_conn_id_ok = 0;334335/* The next URXE we process should be at the head of the pending list. */336if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))337return 0;338339assert(e->demux_state == URXE_DEMUX_STATE_PENDING);340341/* Determine the DCID of the first packet in the datagram. */342dst_conn_id_ok = demux_identify_conn_id(demux, e, &dst_conn_id);343344ossl_list_urxe_remove(&demux->urx_pending, e);345if (demux->default_cb != NULL) {346/*347* Pass to default handler for routing. The URXE now belongs to the348* callback.349*/350e->demux_state = URXE_DEMUX_STATE_ISSUED;351demux->default_cb(e, demux->default_cb_arg,352dst_conn_id_ok ? &dst_conn_id : NULL);353} else {354/* Discard. */355ossl_list_urxe_insert_tail(&demux->urx_free, e);356e->demux_state = URXE_DEMUX_STATE_FREE;357}358359return 1; /* keep processing pending URXEs */360}361362/* Process pending URXEs to generate callbacks. */363static int demux_process_pending_urxl(QUIC_DEMUX *demux)364{365QUIC_URXE *e;366int ret;367368while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)369if ((ret = demux_process_pending_urxe(demux, e)) <= 0)370return ret;371372return 1;373}374375/*376* Drain the pending URXE list, processing any pending URXEs by making their377* callbacks. If no URXEs are pending, a network read is attempted first.378*/379int ossl_quic_demux_pump(QUIC_DEMUX *demux)380{381int ret;382383if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {384ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);385if (ret != 1)386return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;387388ret = demux_recv(demux);389if (ret != QUIC_DEMUX_PUMP_RES_OK)390return ret;391392/*393* If demux_recv returned successfully, we should always have something.394*/395assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);396}397398if ((ret = demux_process_pending_urxl(demux)) <= 0)399return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;400401return QUIC_DEMUX_PUMP_RES_OK;402}403404/* Artificially inject a packet into the demuxer for testing purposes. */405int ossl_quic_demux_inject(QUIC_DEMUX *demux,406const unsigned char *buf,407size_t buf_len,408const BIO_ADDR *peer,409const BIO_ADDR *local)410{411int ret;412QUIC_URXE *urxe;413414ret = demux_ensure_free_urxe(demux, 1);415if (ret != 1)416return 0;417418urxe = ossl_list_urxe_head(&demux->urx_free);419420assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);421422urxe = demux_reserve_urxe(demux, urxe, buf_len);423if (urxe == NULL)424return 0;425426memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);427urxe->data_len = buf_len;428429if (peer != NULL)430urxe->peer = *peer;431else432BIO_ADDR_clear(&urxe->peer);433434if (local != NULL)435urxe->local = *local;436else437BIO_ADDR_clear(&urxe->local);438439urxe->time440= demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();441442/* Move from free list to pending list. */443ossl_list_urxe_remove(&demux->urx_free, urxe);444urxe->datagram_id = demux->next_datagram_id++;445ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);446urxe->demux_state = URXE_DEMUX_STATE_PENDING;447448return demux_process_pending_urxl(demux) > 0;449}450451/* Called by our user to return a URXE to the free list. */452void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,453QUIC_URXE *e)454{455assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);456assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);457ossl_list_urxe_insert_tail(&demux->urx_free, e);458e->demux_state = URXE_DEMUX_STATE_FREE;459}460461void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,462QUIC_URXE *e)463{464assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);465assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);466ossl_list_urxe_insert_head(&demux->urx_pending, e);467e->demux_state = URXE_DEMUX_STATE_PENDING;468}469470int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux)471{472return ossl_list_urxe_head(&demux->urx_pending) != NULL;473}474475476