Path: blob/main/crypto/openssl/ssl/quic/quic_rcidm.c
48262 views
/*1* Copyright 2023-2024 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_rcidm.h"10#include "internal/priority_queue.h"11#include "internal/list.h"12#include "internal/common.h"1314/*15* QUIC Remote Connection ID Manager16* =================================17*18* We can receive an arbitrary number of RCIDs via NCID frames. Periodically, we19* may desire (for example for anti-connection fingerprinting reasons, etc.)20* to switch to a new RCID according to some arbitrary policy such as the number21* of packets we have sent.22*23* When we do this we should move to the next RCID in the sequence of received24* RCIDs ordered by sequence number. For example, if a peer sends us three NCID25* frames with sequence numbers 10, 11, 12, we should seek to consume these26* RCIDs in order.27*28* However, due to the possibility of packet reordering in the network, NCID29* frames might be received out of order. Thus if a peer sends us NCID frames30* with sequence numbers 12, 10, 11, we should still consume the RCID with31* sequence number 10 before consuming the RCIDs with sequence numbers 11 or 12.32*33* We use a priority queue for this purpose.34*/35static void rcidm_update(QUIC_RCIDM *rcidm);36static void rcidm_set_preferred_rcid(QUIC_RCIDM *rcidm,37const QUIC_CONN_ID *rcid);3839#define PACKETS_PER_RCID 100004041#define INITIAL_SEQ_NUM 042#define PREF_ADDR_SEQ_NUM 14344/*45* RCID46* ====47*48* The RCID structure is used to track RCIDs which have sequence numbers (i.e.,49* INITIAL, PREF_ADDR and NCID type RCIDs). The RCIDs without sequence numbers50* (Initial ODCIDs and Retry ODCIDs), hereafter referred to as unnumbered RCIDs,51* can logically be viewed as their own type of RCID but are tracked separately52* as singletons without needing a discrete structure.53*54* At any given time an RCID object is in one of these states:55*56*57* (start)58* |59* [add]60* |61* _____v_____ ___________ ____________62* | | | | | |63* | PENDING | --[select]--> | CURRENT | --[retire]--> | RETIRING |64* |___________| |___________| |____________|65* |66* [pop]67* |68* v69* (fin)70*71* The transition through the states is monotonic and irreversible.72* The RCID object is freed when it is popped.73*74* PENDING75* Invariants:76* rcid->state == RCID_STATE_PENDING;77* rcid->pq_idx != SIZE_MAX (debug assert only);78* the RCID is not the current RCID, rcidm->cur_rcid != rcid;79* the RCID is in the priority queue;80* the RCID is not in the retiring_list.81*82* CURRENT83* Invariants:84* rcid->state == RCID_STATE_CUR;85* rcid->pq_idx == SIZE_MAX (debug assert only);86* the RCID is the current RCID, rcidm->cur_rcid == rcid;87* the RCID is not in the priority queue;88* the RCID is not in the retiring_list.89*90* RETIRING91* Invariants:92* rcid->state == RCID_STATE_RETIRING;93* rcid->pq_idx == SIZE_MAX (debug assert only);94* the RCID is not the current RCID, rcidm->cur_rcid != rcid;95* the RCID is not in the priority queue;96* the RCID is in the retiring_list.97*98* Invariant: At most one RCID object is in the CURRENT state at any one time.99*100* (If no RCID object is in the CURRENT state, this means either101* an unnumbered RCID is being used as the preferred RCID102* or we currently have no preferred RCID.)103*104* All of the above states can be considered substates of the 'ACTIVE' state105* for an RCID as specified in RFC 9000. A CID only ceases to be active106* when we send a RETIRE_CONN_ID frame, which is the responsibility of the107* user of the RCIDM and happens after the above state machine is terminated.108*/109enum {110RCID_STATE_PENDING,111RCID_STATE_CUR,112RCID_STATE_RETIRING113};114115enum {116RCID_TYPE_INITIAL, /* CID is from an peer INITIAL packet (seq 0) */117RCID_TYPE_PREF_ADDR, /* CID is from a preferred_address TPARAM (seq 1) */118RCID_TYPE_NCID /* CID is from a NCID frame */119/*120* INITIAL_ODCID and RETRY_ODCID also conceptually exist but are tracked121* separately.122*/123};124125typedef struct rcid_st {126OSSL_LIST_MEMBER(retiring, struct rcid_st); /* valid iff RETIRING */127128QUIC_CONN_ID cid; /* The actual CID string for this RCID */129uint64_t seq_num;130size_t pq_idx; /* Index of entry into priority queue */131unsigned int state : 2; /* RCID_STATE_* */132unsigned int type : 2; /* RCID_TYPE_* */133} RCID;134135DEFINE_PRIORITY_QUEUE_OF(RCID);136DEFINE_LIST_OF(retiring, RCID);137138/*139* RCID Manager140* ============141*142* The following "business logic" invariants also apply to the RCIDM143* as a whole:144*145* Invariant: An RCID of INITIAL type has a sequence number of 0.146* Invariant: An RCID of PREF_ADDR type has a sequence number of 1.147*148* Invariant: There is never more than one Initial ODCID149* added throughout the lifetime of an RCIDM.150* Invariant: There is never more than one Retry ODCID151* added throughout the lifetime of an RCIDM.152* Invariant: There is never more than one INITIAL RCID created153* throughout the lifetime of an RCIDM.154* Invariant: There is never more than one PREF_ADDR RCID created155* throughout the lifetime of an RCIDM.156* Invariant: No INITIAL or PREF_ADDR RCID may be added after157* the handshake is completed.158*159*/160struct quic_rcidm_st {161/*162* The current RCID we prefer to use (value undefined if163* !have_preferred_rcid).164*165* This is preferentially set to a numbered RCID (represented by an RCID166* object) if we have one (in which case preferred_rcid == cur_rcid->cid);167* otherwise it is set to one of the unnumbered RCIDs (the Initial ODCID or168* Retry ODCID) if available (and cur_rcid == NULL).169*/170QUIC_CONN_ID preferred_rcid;171172/*173* These are initialized if the corresponding added_ flags are set.174*/175QUIC_CONN_ID initial_odcid, retry_odcid;176177/*178* Total number of packets sent since we last made a packet count-based RCID179* update decision.180*/181uint64_t packets_sent;182183/* Number of post-handshake RCID changes we have performed. */184uint64_t num_changes;185186/*187* The Retire Prior To watermark value; max(retire_prior_to) of all received188* NCID frames.189*/190uint64_t retire_prior_to;191192/* (SORT BY seq_num ASC) -> (RCID *) */193PRIORITY_QUEUE_OF(RCID) *rcids;194195/*196* Current RCID object we are using. This may differ from the first item in197* the priority queue if we received NCID frames out of order. For example198* if we get seq 5, switch to it immediately, then get seq 4, we want to199* keep using seq 5 until we decide to roll again rather than immediately200* switch to seq 4. Never points to an object on the retiring_list.201*/202RCID *cur_rcid;203204/*205* When a RCID becomes pending-retirement, it is moved to the retiring_list,206* then freed when it is popped from the retired queue. We use a list for207* this rather than a priority queue as the order in which items are freed208* does not matter. We always append to the tail of the list in order to209* maintain the guarantee that the head (if present) only changes when a210* caller calls pop().211*/212OSSL_LIST(retiring) retiring_list;213214/* Number of entries on the retiring_list. */215size_t num_retiring;216217/* preferred_rcid has been changed? */218unsigned int preferred_rcid_changed : 1;219220/* Do we have any RCID we can use currently? */221unsigned int have_preferred_rcid : 1;222223/* QUIC handshake has been completed? */224unsigned int handshake_complete : 1;225226/* odcid was set (not necessarily still valid as a RCID)? */227unsigned int added_initial_odcid : 1;228/* retry_odcid was set (not necessarily still valid as a RCID?) */229unsigned int added_retry_odcid : 1;230/* An initial RCID was added as an RCID structure? */231unsigned int added_initial_rcid : 1;232/* Has a RCID roll been manually requested? */233unsigned int roll_requested : 1;234};235236/*237* Caller must periodically pop retired RCIDs and handle them. If the caller238* fails to do so, fail safely rather than start exhibiting integer rollover.239* Limit the total number of numbered RCIDs to an implausibly large but safe240* value.241*/242#define MAX_NUMBERED_RCIDS (SIZE_MAX / 2)243244static void rcidm_transition_rcid(QUIC_RCIDM *rcidm, RCID *rcid,245unsigned int state);246247/* Check invariants of an RCID */248static void rcidm_check_rcid(QUIC_RCIDM *rcidm, RCID *rcid)249{250assert(rcid->state == RCID_STATE_PENDING251|| rcid->state == RCID_STATE_CUR252|| rcid->state == RCID_STATE_RETIRING);253assert((rcid->state == RCID_STATE_PENDING)254== (rcid->pq_idx != SIZE_MAX));255assert((rcid->state == RCID_STATE_CUR)256== (rcidm->cur_rcid == rcid));257assert((ossl_list_retiring_next(rcid) != NULL258|| ossl_list_retiring_prev(rcid) != NULL259|| ossl_list_retiring_head(&rcidm->retiring_list) == rcid)260== (rcid->state == RCID_STATE_RETIRING));261assert(rcid->type != RCID_TYPE_INITIAL || rcid->seq_num == 0);262assert(rcid->type != RCID_TYPE_PREF_ADDR || rcid->seq_num == 1);263assert(rcid->seq_num <= OSSL_QUIC_VLINT_MAX);264assert(rcid->cid.id_len > 0 && rcid->cid.id_len <= QUIC_MAX_CONN_ID_LEN);265assert(rcid->seq_num >= rcidm->retire_prior_to266|| rcid->state == RCID_STATE_RETIRING);267assert(rcidm->num_changes == 0 || rcidm->handshake_complete);268assert(rcid->state != RCID_STATE_RETIRING || rcidm->num_retiring > 0);269}270271static int rcid_cmp(const RCID *a, const RCID *b)272{273if (a->seq_num < b->seq_num)274return -1;275if (a->seq_num > b->seq_num)276return 1;277return 0;278}279280QUIC_RCIDM *ossl_quic_rcidm_new(const QUIC_CONN_ID *initial_odcid)281{282QUIC_RCIDM *rcidm;283284if ((rcidm = OPENSSL_zalloc(sizeof(*rcidm))) == NULL)285return NULL;286287if ((rcidm->rcids = ossl_pqueue_RCID_new(rcid_cmp)) == NULL) {288OPENSSL_free(rcidm);289return NULL;290}291292if (initial_odcid != NULL) {293rcidm->initial_odcid = *initial_odcid;294rcidm->added_initial_odcid = 1;295}296297rcidm_update(rcidm);298return rcidm;299}300301void ossl_quic_rcidm_free(QUIC_RCIDM *rcidm)302{303RCID *rcid, *rnext;304305if (rcidm == NULL)306return;307308OPENSSL_free(rcidm->cur_rcid);309while ((rcid = ossl_pqueue_RCID_pop(rcidm->rcids)) != NULL)310OPENSSL_free(rcid);311312OSSL_LIST_FOREACH_DELSAFE(rcid, rnext, retiring, &rcidm->retiring_list)313OPENSSL_free(rcid);314315ossl_pqueue_RCID_free(rcidm->rcids);316OPENSSL_free(rcidm);317}318319static void rcidm_set_preferred_rcid(QUIC_RCIDM *rcidm,320const QUIC_CONN_ID *rcid)321{322if (rcid == NULL) {323rcidm->preferred_rcid_changed = 1;324rcidm->have_preferred_rcid = 0;325return;326}327328if (ossl_quic_conn_id_eq(&rcidm->preferred_rcid, rcid))329return;330331rcidm->preferred_rcid = *rcid;332rcidm->preferred_rcid_changed = 1;333rcidm->have_preferred_rcid = 1;334}335336/*337* RCID Lifecycle Management338* =========================339*/340static RCID *rcidm_create_rcid(QUIC_RCIDM *rcidm, uint64_t seq_num,341const QUIC_CONN_ID *cid,342unsigned int type)343{344RCID *rcid;345346if (cid->id_len < 1 || cid->id_len > QUIC_MAX_CONN_ID_LEN347|| seq_num > OSSL_QUIC_VLINT_MAX348|| ossl_pqueue_RCID_num(rcidm->rcids) + rcidm->num_retiring349> MAX_NUMBERED_RCIDS)350return NULL;351352if ((rcid = OPENSSL_zalloc(sizeof(*rcid))) == NULL)353return NULL;354355rcid->seq_num = seq_num;356rcid->cid = *cid;357rcid->type = type;358359if (rcid->seq_num >= rcidm->retire_prior_to) {360rcid->state = RCID_STATE_PENDING;361362if (!ossl_pqueue_RCID_push(rcidm->rcids, rcid, &rcid->pq_idx)) {363OPENSSL_free(rcid);364return NULL;365}366} else {367/* RCID is immediately retired upon creation. */368rcid->state = RCID_STATE_RETIRING;369rcid->pq_idx = SIZE_MAX;370ossl_list_retiring_insert_tail(&rcidm->retiring_list, rcid);371++rcidm->num_retiring;372}373374rcidm_check_rcid(rcidm, rcid);375return rcid;376}377378static void rcidm_transition_rcid(QUIC_RCIDM *rcidm, RCID *rcid,379unsigned int state)380{381unsigned int old_state = rcid->state;382383assert(state >= old_state && state <= RCID_STATE_RETIRING);384rcidm_check_rcid(rcidm, rcid);385if (state == old_state)386return;387388if (rcidm->cur_rcid != NULL && state == RCID_STATE_CUR) {389rcidm_transition_rcid(rcidm, rcidm->cur_rcid, RCID_STATE_RETIRING);390assert(rcidm->cur_rcid == NULL);391}392393if (old_state == RCID_STATE_PENDING) {394ossl_pqueue_RCID_remove(rcidm->rcids, rcid->pq_idx);395rcid->pq_idx = SIZE_MAX;396}397398rcid->state = state;399400if (state == RCID_STATE_CUR) {401rcidm->cur_rcid = rcid;402} else if (state == RCID_STATE_RETIRING) {403if (old_state == RCID_STATE_CUR)404rcidm->cur_rcid = NULL;405406ossl_list_retiring_insert_tail(&rcidm->retiring_list, rcid);407++rcidm->num_retiring;408}409410rcidm_check_rcid(rcidm, rcid);411}412413static void rcidm_free_rcid(QUIC_RCIDM *rcidm, RCID *rcid)414{415if (rcid == NULL)416return;417418rcidm_check_rcid(rcidm, rcid);419420switch (rcid->state) {421case RCID_STATE_PENDING:422ossl_pqueue_RCID_remove(rcidm->rcids, rcid->pq_idx);423break;424case RCID_STATE_CUR:425rcidm->cur_rcid = NULL;426break;427case RCID_STATE_RETIRING:428ossl_list_retiring_remove(&rcidm->retiring_list, rcid);429--rcidm->num_retiring;430break;431default:432assert(0);433break;434}435436OPENSSL_free(rcid);437}438439static void rcidm_handle_retire_prior_to(QUIC_RCIDM *rcidm,440uint64_t retire_prior_to)441{442RCID *rcid;443444if (retire_prior_to <= rcidm->retire_prior_to)445return;446447/*448* Retire the current RCID (if any) if it is affected.449*/450if (rcidm->cur_rcid != NULL && rcidm->cur_rcid->seq_num < retire_prior_to)451rcidm_transition_rcid(rcidm, rcidm->cur_rcid, RCID_STATE_RETIRING);452453/*454* Any other RCIDs needing retirement will be at the start of the priority455* queue, so just stop once we see a higher sequence number exceeding the456* threshold.457*/458while ((rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) != NULL459&& rcid->seq_num < retire_prior_to)460rcidm_transition_rcid(rcidm, rcid, RCID_STATE_RETIRING);461462rcidm->retire_prior_to = retire_prior_to;463}464465/*466* Decision Logic467* ==============468*/469470static void rcidm_roll(QUIC_RCIDM *rcidm)471{472RCID *rcid;473474if ((rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) == NULL)475return;476477rcidm_transition_rcid(rcidm, rcid, RCID_STATE_CUR);478479++rcidm->num_changes;480rcidm->roll_requested = 0;481482if (rcidm->packets_sent >= PACKETS_PER_RCID)483rcidm->packets_sent %= PACKETS_PER_RCID;484else485rcidm->packets_sent = 0;486}487488static void rcidm_update(QUIC_RCIDM *rcidm)489{490RCID *rcid;491492/*493* If we have no current numbered RCID but have one or more pending, use it.494*/495if (rcidm->cur_rcid == NULL496&& (rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) != NULL) {497rcidm_transition_rcid(rcidm, rcid, RCID_STATE_CUR);498assert(rcidm->cur_rcid != NULL);499}500501/* Prefer use of any current numbered RCID we have, if possible. */502if (rcidm->cur_rcid != NULL) {503rcidm_check_rcid(rcidm, rcidm->cur_rcid);504rcidm_set_preferred_rcid(rcidm, &rcidm->cur_rcid->cid);505return;506}507508/*509* If there are no RCIDs from NCID frames we can use, go through the various510* kinds of bootstrapping RCIDs we can use in order of priority.511*/512if (rcidm->added_retry_odcid && !rcidm->handshake_complete) {513rcidm_set_preferred_rcid(rcidm, &rcidm->retry_odcid);514return;515}516517if (rcidm->added_initial_odcid && !rcidm->handshake_complete) {518rcidm_set_preferred_rcid(rcidm, &rcidm->initial_odcid);519return;520}521522/* We don't know of any usable RCIDs */523rcidm_set_preferred_rcid(rcidm, NULL);524}525526static int rcidm_should_roll(QUIC_RCIDM *rcidm)527{528/*529* Always switch as soon as possible if handshake completes;530* and every n packets after handshake completes or the last roll; and531* whenever manually requested.532*/533return rcidm->handshake_complete534&& (rcidm->num_changes == 0535|| rcidm->packets_sent >= PACKETS_PER_RCID536|| rcidm->roll_requested);537}538539static void rcidm_tick(QUIC_RCIDM *rcidm)540{541if (rcidm_should_roll(rcidm))542rcidm_roll(rcidm);543544rcidm_update(rcidm);545}546547/*548* Events549* ======550*/551void ossl_quic_rcidm_on_handshake_complete(QUIC_RCIDM *rcidm)552{553if (rcidm->handshake_complete)554return;555556rcidm->handshake_complete = 1;557rcidm_tick(rcidm);558}559560void ossl_quic_rcidm_on_packet_sent(QUIC_RCIDM *rcidm, uint64_t num_packets)561{562if (num_packets == 0)563return;564565rcidm->packets_sent += num_packets;566rcidm_tick(rcidm);567}568569void ossl_quic_rcidm_request_roll(QUIC_RCIDM *rcidm)570{571rcidm->roll_requested = 1;572rcidm_tick(rcidm);573}574575/*576* Mutation Operations577* ===================578*/579int ossl_quic_rcidm_add_from_initial(QUIC_RCIDM *rcidm,580const QUIC_CONN_ID *rcid)581{582RCID *rcid_obj;583584if (rcidm->added_initial_rcid || rcidm->handshake_complete)585return 0;586587rcid_obj = rcidm_create_rcid(rcidm, INITIAL_SEQ_NUM,588rcid, RCID_TYPE_INITIAL);589if (rcid_obj == NULL)590return 0;591592rcidm->added_initial_rcid = 1;593rcidm_tick(rcidm);594return 1;595}596597int ossl_quic_rcidm_add_from_server_retry(QUIC_RCIDM *rcidm,598const QUIC_CONN_ID *retry_odcid)599{600if (rcidm->added_retry_odcid || rcidm->handshake_complete)601return 0;602603rcidm->retry_odcid = *retry_odcid;604rcidm->added_retry_odcid = 1;605rcidm_tick(rcidm);606return 1;607}608609int ossl_quic_rcidm_add_from_ncid(QUIC_RCIDM *rcidm,610const OSSL_QUIC_FRAME_NEW_CONN_ID *ncid)611{612RCID *rcid;613614rcid = rcidm_create_rcid(rcidm, ncid->seq_num, &ncid->conn_id, RCID_TYPE_NCID);615if (rcid == NULL)616return 0;617618rcidm_handle_retire_prior_to(rcidm, ncid->retire_prior_to);619rcidm_tick(rcidm);620return 1;621}622623/*624* Queries625* =======626*/627628static int rcidm_get_retire(QUIC_RCIDM *rcidm, uint64_t *seq_num, int peek)629{630RCID *rcid = ossl_list_retiring_head(&rcidm->retiring_list);631632if (rcid == NULL)633return 0;634635if (seq_num != NULL)636*seq_num = rcid->seq_num;637638if (!peek)639rcidm_free_rcid(rcidm, rcid);640641return 1;642}643644int ossl_quic_rcidm_pop_retire_seq_num(QUIC_RCIDM *rcidm,645uint64_t *seq_num)646{647return rcidm_get_retire(rcidm, seq_num, /*peek=*/0);648}649650int ossl_quic_rcidm_peek_retire_seq_num(QUIC_RCIDM *rcidm,651uint64_t *seq_num)652{653return rcidm_get_retire(rcidm, seq_num, /*peek=*/1);654}655656int ossl_quic_rcidm_get_preferred_tx_dcid(QUIC_RCIDM *rcidm,657QUIC_CONN_ID *tx_dcid)658{659if (!rcidm->have_preferred_rcid)660return 0;661662*tx_dcid = rcidm->preferred_rcid;663return 1;664}665666int ossl_quic_rcidm_get_preferred_tx_dcid_changed(QUIC_RCIDM *rcidm,667int clear)668{669int r = rcidm->preferred_rcid_changed;670671if (clear)672rcidm->preferred_rcid_changed = 0;673674return r;675}676677size_t ossl_quic_rcidm_get_num_active(const QUIC_RCIDM *rcidm)678{679return ossl_pqueue_RCID_num(rcidm->rcids)680+ (rcidm->cur_rcid != NULL ? 1 : 0)681+ ossl_quic_rcidm_get_num_retiring(rcidm);682}683684size_t ossl_quic_rcidm_get_num_retiring(const QUIC_RCIDM *rcidm)685{686return rcidm->num_retiring;687}688689690