Path: blob/main/crypto/openssl/ssl/quic/quic_demux.c
109353 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);182183if (new_alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))184return NULL;185186e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);187if (e2 == NULL) {188/* Failed to resize, abort. */189if (prev == NULL)190ossl_list_urxe_insert_head(&demux->urx_free, e);191else192ossl_list_urxe_insert_after(&demux->urx_free, prev, e);193194return NULL;195}196197if (prev == NULL)198ossl_list_urxe_insert_head(&demux->urx_free, e2);199else200ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);201202e2->alloc_len = new_alloc_len;203return e2;204}205206static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,207size_t alloc_len)208{209return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;210}211212static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)213{214QUIC_URXE *e;215216while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {217e = demux_alloc_urxe(demux->mtu);218if (e == NULL)219return 0;220221ossl_list_urxe_insert_tail(&demux->urx_free, e);222e->demux_state = URXE_DEMUX_STATE_FREE;223}224225return 1;226}227228/*229* Receive datagrams from network, placing them into URXEs.230*231* Returns 1 on success or 0 on failure.232*233* Precondition: at least one URXE is free234* Precondition: there are no pending URXEs235*/236static int demux_recv(QUIC_DEMUX *demux)237{238BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];239size_t rd, i;240QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;241OSSL_TIME now;242243/* This should never be called when we have any pending URXE. */244assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);245assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);246247if (demux->net_bio == NULL)248/*249* If no BIO is plugged in, treat this as no datagram being available.250*/251return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;252253/*254* Opportunistically receive as many messages as possible in a single255* syscall, determined by how many free URXEs are available.256*/257for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);258++i, urxe = ossl_list_urxe_next(urxe)) {259if (urxe == NULL) {260/* We need at least one URXE to receive into. */261if (!ossl_assert(i > 0))262return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;263264break;265}266267/* Ensure the URXE is big enough. */268urxe = demux_reserve_urxe(demux, urxe, demux->mtu);269if (urxe == NULL)270/* Allocation error, fail. */271return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;272273/* Ensure we zero any fields added to BIO_MSG at a later date. */274memset(&msg[i], 0, sizeof(BIO_MSG));275msg[i].data = ossl_quic_urxe_data(urxe);276msg[i].data_len = urxe->alloc_len;277msg[i].peer = &urxe->peer;278BIO_ADDR_clear(&urxe->peer);279if (demux->use_local_addr)280msg[i].local = &urxe->local;281else282BIO_ADDR_clear(&urxe->local);283}284285ERR_set_mark();286if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {287if (BIO_err_is_non_fatal(ERR_peek_last_error())) {288/* Transient error, clear the error and stop. */289ERR_pop_to_mark();290return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;291} else {292/* Non-transient error, do not clear the error. */293ERR_clear_last_mark();294return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;295}296}297298ERR_clear_last_mark();299now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();300301urxe = ossl_list_urxe_head(&demux->urx_free);302for (i = 0; i < rd; ++i, urxe = unext) {303unext = ossl_list_urxe_next(urxe);304/* Set URXE with actual length of received datagram. */305urxe->data_len = msg[i].data_len;306/* Time we received datagram. */307urxe->time = now;308urxe->datagram_id = demux->next_datagram_id++;309/* Move from free list to pending list. */310ossl_list_urxe_remove(&demux->urx_free, urxe);311ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);312urxe->demux_state = URXE_DEMUX_STATE_PENDING;313}314315return QUIC_DEMUX_PUMP_RES_OK;316}317318/* Extract destination connection ID from the first packet in a datagram. */319static int demux_identify_conn_id(QUIC_DEMUX *demux,320QUIC_URXE *e,321QUIC_CONN_ID *dst_conn_id)322{323return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),324e->data_len,325demux->short_conn_id_len,326dst_conn_id);327}328329/*330* Process a single pending URXE.331* Returning 1 on success, 0 on failure.332*/333static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)334{335QUIC_CONN_ID dst_conn_id;336int dst_conn_id_ok = 0;337338/* The next URXE we process should be at the head of the pending list. */339if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))340return 0;341342assert(e->demux_state == URXE_DEMUX_STATE_PENDING);343344/* Determine the DCID of the first packet in the datagram. */345dst_conn_id_ok = demux_identify_conn_id(demux, e, &dst_conn_id);346347ossl_list_urxe_remove(&demux->urx_pending, e);348if (demux->default_cb != NULL) {349/*350* Pass to default handler for routing. The URXE now belongs to the351* callback.352*/353e->demux_state = URXE_DEMUX_STATE_ISSUED;354demux->default_cb(e, demux->default_cb_arg,355dst_conn_id_ok ? &dst_conn_id : NULL);356} else {357/* Discard. */358ossl_list_urxe_insert_tail(&demux->urx_free, e);359e->demux_state = URXE_DEMUX_STATE_FREE;360}361362return 1; /* keep processing pending URXEs */363}364365/* Process pending URXEs to generate callbacks. */366static int demux_process_pending_urxl(QUIC_DEMUX *demux)367{368QUIC_URXE *e;369int ret;370371while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)372if ((ret = demux_process_pending_urxe(demux, e)) <= 0)373return ret;374375return 1;376}377378/*379* Drain the pending URXE list, processing any pending URXEs by making their380* callbacks. If no URXEs are pending, a network read is attempted first.381*/382int ossl_quic_demux_pump(QUIC_DEMUX *demux)383{384int ret;385386if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {387ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);388if (ret != 1)389return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;390391ret = demux_recv(demux);392if (ret != QUIC_DEMUX_PUMP_RES_OK)393return ret;394395/*396* If demux_recv returned successfully, we should always have something.397*/398assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);399}400401if ((ret = demux_process_pending_urxl(demux)) <= 0)402return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;403404return QUIC_DEMUX_PUMP_RES_OK;405}406407/* Artificially inject a packet into the demuxer for testing purposes. */408int ossl_quic_demux_inject(QUIC_DEMUX *demux,409const unsigned char *buf,410size_t buf_len,411const BIO_ADDR *peer,412const BIO_ADDR *local)413{414int ret;415QUIC_URXE *urxe;416417ret = demux_ensure_free_urxe(demux, 1);418if (ret != 1)419return 0;420421urxe = ossl_list_urxe_head(&demux->urx_free);422423assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);424425urxe = demux_reserve_urxe(demux, urxe, buf_len);426if (urxe == NULL)427return 0;428429memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);430urxe->data_len = buf_len;431432if (peer != NULL)433urxe->peer = *peer;434else435BIO_ADDR_clear(&urxe->peer);436437if (local != NULL)438urxe->local = *local;439else440BIO_ADDR_clear(&urxe->local);441442urxe->time443= demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();444445/* Move from free list to pending list. */446ossl_list_urxe_remove(&demux->urx_free, urxe);447urxe->datagram_id = demux->next_datagram_id++;448ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);449urxe->demux_state = URXE_DEMUX_STATE_PENDING;450451return demux_process_pending_urxl(demux) > 0;452}453454/* Called by our user to return a URXE to the free list. */455void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,456QUIC_URXE *e)457{458assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);459assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);460ossl_list_urxe_insert_tail(&demux->urx_free, e);461e->demux_state = URXE_DEMUX_STATE_FREE;462}463464void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,465QUIC_URXE *e)466{467assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);468assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);469ossl_list_urxe_insert_head(&demux->urx_pending, e);470e->demux_state = URXE_DEMUX_STATE_PENDING;471}472473int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux)474{475return ossl_list_urxe_head(&demux->urx_pending) != NULL;476}477478479