Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/ssl/quic/quic_rcidm.c
Line
Count
Source
1
/*
2
 * Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include "internal/quic_rcidm.h"
11
#include "internal/priority_queue.h"
12
#include "internal/list.h"
13
#include "internal/common.h"
14
15
/*
16
 * QUIC Remote Connection ID Manager
17
 * =================================
18
 *
19
 * We can receive an arbitrary number of RCIDs via NCID frames. Periodically, we
20
 * may desire (for example for anti-connection fingerprinting reasons, etc.)
21
 * to switch to a new RCID according to some arbitrary policy such as the number
22
 * of packets we have sent.
23
 *
24
 * When we do this we should move to the next RCID in the sequence of received
25
 * RCIDs ordered by sequence number. For example, if a peer sends us three NCID
26
 * frames with sequence numbers 10, 11, 12, we should seek to consume these
27
 * RCIDs in order.
28
 *
29
 * However, due to the possibility of packet reordering in the network, NCID
30
 * frames might be received out of order. Thus if a peer sends us NCID frames
31
 * with sequence numbers 12, 10, 11, we should still consume the RCID with
32
 * sequence number 10 before consuming the RCIDs with sequence numbers 11 or 12.
33
 *
34
 * We use a priority queue for this purpose.
35
 */
36
static void rcidm_update(QUIC_RCIDM *rcidm);
37
static void rcidm_set_preferred_rcid(QUIC_RCIDM *rcidm,
38
    const QUIC_CONN_ID *rcid);
39
40
11.4M
#define PACKETS_PER_RCID 10000
41
42
121k
#define INITIAL_SEQ_NUM 0
43
#define PREF_ADDR_SEQ_NUM 1
44
45
/*
46
 * RCID
47
 * ====
48
 *
49
 * The RCID structure is used to track RCIDs which have sequence numbers (i.e.,
50
 * INITIAL, PREF_ADDR and NCID type RCIDs). The RCIDs without sequence numbers
51
 * (Initial ODCIDs and Retry ODCIDs), hereafter referred to as unnumbered RCIDs,
52
 * can logically be viewed as their own type of RCID but are tracked separately
53
 * as singletons without needing a discrete structure.
54
 *
55
 * At any given time an RCID object is in one of these states:
56
 *
57
 *
58
 *      (start)
59
 *         |
60
 *       [add]
61
 *         |
62
 *    _____v_____                 ___________                 ____________
63
 *   |           |               |           |               |            |
64
 *   |  PENDING  | --[select]--> |  CURRENT  | --[retire]--> |  RETIRING  |
65
 *   |___________|               |___________|               |____________|
66
 *                                                                  |
67
 *                                                                [pop]
68
 *                                                                  |
69
 *                                                                  v
70
 *                                                                (fin)
71
 *
72
 *   The transition through the states is monotonic and irreversible.
73
 *   The RCID object is freed when it is popped.
74
 *
75
 *   PENDING
76
 *     Invariants:
77
 *       rcid->state == RCID_STATE_PENDING;
78
 *       rcid->pq_idx != SIZE_MAX (debug assert only);
79
 *       the RCID is not the current RCID, rcidm->cur_rcid != rcid;
80
 *       the RCID is in the priority queue;
81
 *       the RCID is not in the retiring_list.
82
 *
83
 *   CURRENT
84
 *     Invariants:
85
 *       rcid->state == RCID_STATE_CUR;
86
 *       rcid->pq_idx == SIZE_MAX (debug assert only);
87
 *       the RCID is the current RCID, rcidm->cur_rcid == rcid;
88
 *       the RCID is not in the priority queue;
89
 *       the RCID is not in the retiring_list.
90
 *
91
 *   RETIRING
92
 *     Invariants:
93
 *       rcid->state == RCID_STATE_RETIRING;
94
 *       rcid->pq_idx == SIZE_MAX (debug assert only);
95
 *       the RCID is not the current RCID, rcidm->cur_rcid != rcid;
96
 *       the RCID is not in the priority queue;
97
 *       the RCID is in the retiring_list.
98
 *
99
 *   Invariant: At most one RCID object is in the CURRENT state at any one time.
100
 *
101
 *      (If no RCID object is in the CURRENT state, this means either
102
 *       an unnumbered RCID is being used as the preferred RCID
103
 *       or we currently have no preferred RCID.)
104
 *
105
 *   All of the above states can be considered substates of the 'ACTIVE' state
106
 *   for an RCID as specified in RFC 9000. A CID only ceases to be active
107
 *   when we send a RETIRE_CONN_ID frame, which is the responsibility of the
108
 *   user of the RCIDM and happens after the above state machine is terminated.
109
 */
110
enum {
111
    RCID_STATE_PENDING,
112
    RCID_STATE_CUR,
113
    RCID_STATE_RETIRING
114
};
115
116
enum {
117
    RCID_TYPE_INITIAL, /* CID is from an peer INITIAL packet     (seq 0) */
118
    RCID_TYPE_PREF_ADDR, /* CID is from a preferred_address TPARAM (seq 1) */
119
    RCID_TYPE_NCID /* CID is from a NCID frame */
120
    /*
121
     * INITIAL_ODCID and RETRY_ODCID also conceptually exist but are tracked
122
     * separately.
123
     */
124
};
125
126
typedef struct rcid_st {
127
    OSSL_LIST_MEMBER(retiring, struct rcid_st); /* valid iff RETIRING */
128
129
    QUIC_CONN_ID cid; /* The actual CID string for this RCID */
130
    uint64_t seq_num;
131
    size_t pq_idx; /* Index of entry into priority queue */
132
    unsigned int state : 2; /* RCID_STATE_* */
133
    unsigned int type : 2; /* RCID_TYPE_* */
134
} RCID;
135
136
DEFINE_PRIORITY_QUEUE_OF(RCID);
137
606M
DEFINE_LIST_OF(retiring, RCID);
quic_rcidm.c:ossl_list_retiring_head
Line
Count
Source
137
DEFINE_LIST_OF(retiring, RCID);
quic_rcidm.c:ossl_list_retiring_next
Line
Count
Source
137
DEFINE_LIST_OF(retiring, RCID);
quic_rcidm.c:ossl_list_retiring_insert_tail
Line
Count
Source
137
DEFINE_LIST_OF(retiring, RCID);
quic_rcidm.c:ossl_list_retiring_prev
Line
Count
Source
137
DEFINE_LIST_OF(retiring, RCID);
quic_rcidm.c:ossl_list_retiring_remove
Line
Count
Source
137
DEFINE_LIST_OF(retiring, RCID);
138
606M
139
606M
/*
140
606M
 * RCID Manager
141
606M
 * ============
142
606M
 *
143
606M
 * The following "business logic" invariants also apply to the RCIDM
144
606M
 * as a whole:
145
606M
 *
146
606M
 *   Invariant: An RCID of INITIAL   type has a sequence number of 0.
147
606M
 *   Invariant: An RCID of PREF_ADDR type has a sequence number of 1.
148
606M
 *
149
606M
 *   Invariant: There is never more than one Initial ODCID
150
606M
 *              added throughout the lifetime of an RCIDM.
151
606M
 *   Invariant: There is never more than one Retry ODCID
152
606M
 *              added throughout the lifetime of an RCIDM.
153
606M
 *   Invariant: There is never more than one INITIAL RCID created
154
606M
 *              throughout the lifetime of an RCIDM.
155
606M
 *   Invariant: There is never more than one PREF_ADDR RCID created
156
606M
 *              throughout the lifetime of an RCIDM.
157
606M
 *   Invariant: No INITIAL or PREF_ADDR RCID may be added after
158
606M
 *              the handshake is completed.
159
606M
 *
160
606M
 */
161
606M
struct quic_rcidm_st {
162
606M
    /*
163
606M
     * The current RCID we prefer to use (value undefined if
164
606M
     * !have_preferred_rcid).
165
606M
     *
166
606M
     * This is preferentially set to a numbered RCID (represented by an RCID
167
606M
     * object) if we have one (in which case preferred_rcid == cur_rcid->cid);
168
606M
     * otherwise it is set to one of the unnumbered RCIDs (the Initial ODCID or
169
606M
     * Retry ODCID) if available (and cur_rcid == NULL).
170
606M
     */
171
606M
    QUIC_CONN_ID preferred_rcid;
172
606M
173
606M
    /*
174
606M
     * These are initialized if the corresponding added_ flags are set.
175
606M
     */
176
606M
    QUIC_CONN_ID initial_odcid, retry_odcid;
177
606M
178
606M
    /*
179
606M
     * Total number of packets sent since we last made a packet count-based RCID
180
606M
     * update decision.
181
606M
     */
182
606M
    uint64_t packets_sent;
183
606M
184
606M
    /* Number of post-handshake RCID changes we have performed. */
185
606M
    uint64_t num_changes;
186
606M
187
606M
    /*
188
606M
     * The Retire Prior To watermark value; max(retire_prior_to) of all received
189
606M
     * NCID frames.
190
606M
     */
191
606M
    uint64_t retire_prior_to;
192
606M
193
606M
    /* (SORT BY seq_num ASC) -> (RCID *) */
194
606M
    PRIORITY_QUEUE_OF(RCID) * rcids;
195
606M
196
606M
    /*
197
606M
     * Current RCID object we are using. This may differ from the first item in
198
606M
     * the priority queue if we received NCID frames out of order. For example
199
606M
     * if we get seq 5, switch to it immediately, then get seq 4, we want to
200
606M
     * keep using seq 5 until we decide to roll again rather than immediately
201
606M
     * switch to seq 4. Never points to an object on the retiring_list.
202
606M
     */
203
606M
    RCID *cur_rcid;
204
606M
205
606M
    /*
206
606M
     * When a RCID becomes pending-retirement, it is moved to the retiring_list,
207
606M
     * then freed when it is popped from the retired queue. We use a list for
208
606M
     * this rather than a priority queue as the order in which items are freed
209
606M
     * does not matter. We always append to the tail of the list in order to
210
606M
     * maintain the guarantee that the head (if present) only changes when a
211
606M
     * caller calls pop().
212
606M
     */
213
606M
    OSSL_LIST(retiring)
214
606M
    retiring_list;
215
606M
216
606M
    /* Number of entries on the retiring_list. */
217
606M
    size_t num_retiring;
218
606M
219
606M
    /* preferred_rcid has been changed? */
220
606M
    unsigned int preferred_rcid_changed : 1;
221
606M
222
606M
    /* Do we have any RCID we can use currently? */
223
606M
    unsigned int have_preferred_rcid : 1;
224
606M
225
606M
    /* QUIC handshake has been completed? */
226
606M
    unsigned int handshake_complete : 1;
227
606M
228
606M
    /* odcid was set (not necessarily still valid as a RCID)? */
229
606M
    unsigned int added_initial_odcid : 1;
230
606M
    /* retry_odcid was set (not necessarily still valid as a RCID?) */
231
606M
    unsigned int added_retry_odcid : 1;
232
606M
    /* An initial RCID was added as an RCID structure? */
233
606M
    unsigned int added_initial_rcid : 1;
234
606M
    /* Has a RCID roll been manually requested? */
235
606M
    unsigned int roll_requested : 1;
236
606M
};
237
606M
238
606M
/*
239
606M
 * Caller must periodically pop retired RCIDs and handle them. If the caller
240
606M
 * fails to do so, fail safely rather than start exhibiting integer rollover.
241
606M
 * Limit the total number of numbered RCIDs to an implausibly large but safe
242
606M
 * value.
243
606M
 */
244
606M
#define MAX_NUMBERED_RCIDS (SIZE_MAX / 2)
245
246
static void rcidm_transition_rcid(QUIC_RCIDM *rcidm, RCID *rcid,
247
    unsigned int state);
248
249
/* Check invariants of an RCID */
250
static void rcidm_check_rcid(QUIC_RCIDM *rcidm, RCID *rcid)
251
23.8M
{
252
23.8M
    assert(rcid->state == RCID_STATE_PENDING
253
23.8M
        || rcid->state == RCID_STATE_CUR
254
23.8M
        || rcid->state == RCID_STATE_RETIRING);
255
23.8M
    assert((rcid->state == RCID_STATE_PENDING)
256
23.8M
        == (rcid->pq_idx != SIZE_MAX));
257
23.8M
    assert((rcid->state == RCID_STATE_CUR)
258
23.8M
        == (rcidm->cur_rcid == rcid));
259
23.8M
    assert((ossl_list_retiring_next(rcid) != NULL
260
23.8M
               || ossl_list_retiring_prev(rcid) != NULL
261
23.8M
               || ossl_list_retiring_head(&rcidm->retiring_list) == rcid)
262
23.8M
        == (rcid->state == RCID_STATE_RETIRING));
263
23.8M
    assert(rcid->type != RCID_TYPE_INITIAL || rcid->seq_num == 0);
264
23.8M
    assert(rcid->type != RCID_TYPE_PREF_ADDR || rcid->seq_num == 1);
265
23.8M
    assert(rcid->seq_num <= OSSL_QUIC_VLINT_MAX);
266
23.8M
    assert(rcid->cid.id_len > 0 && rcid->cid.id_len <= QUIC_MAX_CONN_ID_LEN);
267
23.8M
    assert(rcid->seq_num >= rcidm->retire_prior_to
268
23.8M
        || rcid->state == RCID_STATE_RETIRING);
269
23.8M
    assert(rcidm->num_changes == 0 || rcidm->handshake_complete);
270
23.8M
    assert(rcid->state != RCID_STATE_RETIRING || rcidm->num_retiring > 0);
271
23.8M
}
272
273
static int rcid_cmp(const void *av, const void *bv)
274
74.3M
{
275
74.3M
    const RCID *a = av;
276
74.3M
    const RCID *b = bv;
277
278
74.3M
    if (a->seq_num < b->seq_num)
279
35.0M
        return -1;
280
39.3M
    if (a->seq_num > b->seq_num)
281
16.5M
        return 1;
282
22.7M
    return 0;
283
39.3M
}
284
285
QUIC_RCIDM *ossl_quic_rcidm_new(const QUIC_CONN_ID *initial_odcid)
286
6.18M
{
287
6.18M
    QUIC_RCIDM *rcidm;
288
289
6.18M
    if ((rcidm = OPENSSL_zalloc(sizeof(*rcidm))) == NULL)
290
0
        return NULL;
291
292
6.18M
    if ((rcidm->rcids = ossl_pqueue_RCID_new(rcid_cmp)) == NULL) {
293
0
        OPENSSL_free(rcidm);
294
0
        return NULL;
295
0
    }
296
297
6.18M
    if (initial_odcid != NULL) {
298
6.04M
        rcidm->initial_odcid = *initial_odcid;
299
6.04M
        rcidm->added_initial_odcid = 1;
300
6.04M
    }
301
302
6.18M
    rcidm_update(rcidm);
303
6.18M
    return rcidm;
304
6.18M
}
305
306
void ossl_quic_rcidm_free(QUIC_RCIDM *rcidm)
307
6.18M
{
308
6.18M
    RCID *rcid, *rnext;
309
310
6.18M
    if (rcidm == NULL)
311
0
        return;
312
313
6.18M
    OPENSSL_free(rcidm->cur_rcid);
314
8.44M
    while ((rcid = ossl_pqueue_RCID_pop(rcidm->rcids)) != NULL)
315
2.26M
        OPENSSL_free(rcid);
316
317
6.18M
    OSSL_LIST_FOREACH_DELSAFE(rcid, rnext, retiring, &rcidm->retiring_list)
318
4.53M
    OPENSSL_free(rcid);
319
320
6.18M
    ossl_pqueue_RCID_free(rcidm->rcids);
321
6.18M
    OPENSSL_free(rcidm);
322
6.18M
}
323
324
static void rcidm_set_preferred_rcid(QUIC_RCIDM *rcidm,
325
    const QUIC_CONN_ID *rcid)
326
16.8M
{
327
16.8M
    if (rcid == NULL) {
328
1.84M
        rcidm->preferred_rcid_changed = 1;
329
1.84M
        rcidm->have_preferred_rcid = 0;
330
1.84M
        return;
331
1.84M
    }
332
333
15.0M
    if (ossl_quic_conn_id_eq(&rcidm->preferred_rcid, rcid))
334
14.1M
        return;
335
336
920k
    rcidm->preferred_rcid = *rcid;
337
920k
    rcidm->preferred_rcid_changed = 1;
338
920k
    rcidm->have_preferred_rcid = 1;
339
920k
}
340
341
/*
342
 * RCID Lifecycle Management
343
 * =========================
344
 */
345
static RCID *rcidm_create_rcid(QUIC_RCIDM *rcidm, uint64_t seq_num,
346
    const QUIC_CONN_ID *cid,
347
    unsigned int type)
348
7.11M
{
349
7.11M
    RCID *rcid;
350
351
7.11M
    if (cid->id_len < 1 || cid->id_len > QUIC_MAX_CONN_ID_LEN
352
6.99M
        || seq_num > OSSL_QUIC_VLINT_MAX
353
6.97M
        || ossl_pqueue_RCID_num(rcidm->rcids) + rcidm->num_retiring
354
6.97M
            > MAX_NUMBERED_RCIDS)
355
133k
        return NULL;
356
357
6.97M
    if ((rcid = OPENSSL_zalloc(sizeof(*rcid))) == NULL)
358
0
        return NULL;
359
360
6.97M
    rcid->seq_num = seq_num;
361
6.97M
    rcid->cid = *cid;
362
6.97M
    rcid->type = type;
363
364
6.97M
    if (rcid->seq_num >= rcidm->retire_prior_to) {
365
5.56M
        rcid->state = RCID_STATE_PENDING;
366
367
5.56M
        if (!ossl_pqueue_RCID_push(rcidm->rcids, rcid, &rcid->pq_idx)) {
368
0
            OPENSSL_free(rcid);
369
0
            return NULL;
370
0
        }
371
5.56M
    } else {
372
        /* RCID is immediately retired upon creation. */
373
1.41M
        rcid->state = RCID_STATE_RETIRING;
374
1.41M
        rcid->pq_idx = SIZE_MAX;
375
1.41M
        ossl_list_retiring_insert_tail(&rcidm->retiring_list, rcid);
376
1.41M
        ++rcidm->num_retiring;
377
1.41M
    }
378
379
6.97M
    rcidm_check_rcid(rcidm, rcid);
380
6.97M
    return rcid;
381
6.97M
}
382
383
static void rcidm_transition_rcid(QUIC_RCIDM *rcidm, RCID *rcid,
384
    unsigned int state)
385
4.27M
{
386
4.27M
    unsigned int old_state = rcid->state;
387
388
4.27M
    assert(state >= old_state && state <= RCID_STATE_RETIRING);
389
4.27M
    rcidm_check_rcid(rcidm, rcid);
390
4.27M
    if (state == old_state)
391
0
        return;
392
393
4.27M
    if (rcidm->cur_rcid != NULL && state == RCID_STATE_CUR) {
394
884k
        rcidm_transition_rcid(rcidm, rcidm->cur_rcid, RCID_STATE_RETIRING);
395
884k
        assert(rcidm->cur_rcid == NULL);
396
884k
    }
397
398
4.27M
    if (old_state == RCID_STATE_PENDING) {
399
3.30M
        ossl_pqueue_RCID_remove(rcidm->rcids, rcid->pq_idx);
400
3.30M
        rcid->pq_idx = SIZE_MAX;
401
3.30M
    }
402
403
4.27M
    rcid->state = state;
404
405
4.27M
    if (state == RCID_STATE_CUR) {
406
1.14M
        rcidm->cur_rcid = rcid;
407
3.13M
    } else if (state == RCID_STATE_RETIRING) {
408
3.13M
        if (old_state == RCID_STATE_CUR)
409
978k
            rcidm->cur_rcid = NULL;
410
411
3.13M
        ossl_list_retiring_insert_tail(&rcidm->retiring_list, rcid);
412
3.13M
        ++rcidm->num_retiring;
413
3.13M
    }
414
415
4.27M
    rcidm_check_rcid(rcidm, rcid);
416
4.27M
}
417
418
static void rcidm_free_rcid(QUIC_RCIDM *rcidm, RCID *rcid)
419
17.9k
{
420
17.9k
    if (rcid == NULL)
421
0
        return;
422
423
17.9k
    rcidm_check_rcid(rcidm, rcid);
424
425
17.9k
    switch (rcid->state) {
426
0
    case RCID_STATE_PENDING:
427
0
        ossl_pqueue_RCID_remove(rcidm->rcids, rcid->pq_idx);
428
0
        break;
429
0
    case RCID_STATE_CUR:
430
0
        rcidm->cur_rcid = NULL;
431
0
        break;
432
17.9k
    case RCID_STATE_RETIRING:
433
17.9k
        ossl_list_retiring_remove(&rcidm->retiring_list, rcid);
434
17.9k
        --rcidm->num_retiring;
435
17.9k
        break;
436
0
    default:
437
0
        assert(0);
438
0
        break;
439
17.9k
    }
440
441
17.9k
    OPENSSL_free(rcid);
442
17.9k
}
443
444
static void rcidm_handle_retire_prior_to(QUIC_RCIDM *rcidm,
445
    uint64_t retire_prior_to)
446
6.88M
{
447
6.88M
    RCID *rcid;
448
449
6.88M
    if (retire_prior_to <= rcidm->retire_prior_to)
450
6.54M
        return;
451
452
    /*
453
     * Retire the current RCID (if any) if it is affected.
454
     */
455
338k
    if (rcidm->cur_rcid != NULL && rcidm->cur_rcid->seq_num < retire_prior_to)
456
94.0k
        rcidm_transition_rcid(rcidm, rcidm->cur_rcid, RCID_STATE_RETIRING);
457
458
    /*
459
     * Any other RCIDs needing retirement will be at the start of the priority
460
     * queue, so just stop once we see a higher sequence number exceeding the
461
     * threshold.
462
     */
463
2.49M
    while ((rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) != NULL
464
2.35M
        && rcid->seq_num < retire_prior_to)
465
2.16M
        rcidm_transition_rcid(rcidm, rcid, RCID_STATE_RETIRING);
466
467
338k
    rcidm->retire_prior_to = retire_prior_to;
468
338k
}
469
470
/*
471
 * Decision Logic
472
 * ==============
473
 */
474
475
static void rcidm_roll(QUIC_RCIDM *rcidm)
476
3.32M
{
477
3.32M
    RCID *rcid;
478
479
3.32M
    if ((rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) == NULL)
480
2.39M
        return;
481
482
935k
    rcidm_transition_rcid(rcidm, rcid, RCID_STATE_CUR);
483
484
935k
    ++rcidm->num_changes;
485
935k
    rcidm->roll_requested = 0;
486
487
935k
    if (rcidm->packets_sent >= PACKETS_PER_RCID)
488
176k
        rcidm->packets_sent %= PACKETS_PER_RCID;
489
759k
    else
490
759k
        rcidm->packets_sent = 0;
491
935k
}
492
493
static void rcidm_update(QUIC_RCIDM *rcidm)
494
16.8M
{
495
16.8M
    RCID *rcid;
496
497
    /*
498
     * If we have no current numbered RCID but have one or more pending, use it.
499
     */
500
16.8M
    if (rcidm->cur_rcid == NULL
501
8.80M
        && (rcid = ossl_pqueue_RCID_peek(rcidm->rcids)) != NULL) {
502
204k
        rcidm_transition_rcid(rcidm, rcid, RCID_STATE_CUR);
503
204k
        assert(rcidm->cur_rcid != NULL);
504
204k
    }
505
506
    /* Prefer use of any current numbered RCID we have, if possible. */
507
16.8M
    if (rcidm->cur_rcid != NULL) {
508
8.27M
        rcidm_check_rcid(rcidm, rcidm->cur_rcid);
509
8.27M
        rcidm_set_preferred_rcid(rcidm, &rcidm->cur_rcid->cid);
510
8.27M
        return;
511
8.27M
    }
512
513
    /*
514
     * If there are no RCIDs from NCID frames we can use, go through the various
515
     * kinds of bootstrapping RCIDs we can use in order of priority.
516
     */
517
8.60M
    if (rcidm->added_retry_odcid && !rcidm->handshake_complete) {
518
247k
        rcidm_set_preferred_rcid(rcidm, &rcidm->retry_odcid);
519
247k
        return;
520
247k
    }
521
522
8.35M
    if (rcidm->added_initial_odcid && !rcidm->handshake_complete) {
523
6.51M
        rcidm_set_preferred_rcid(rcidm, &rcidm->initial_odcid);
524
6.51M
        return;
525
6.51M
    }
526
527
    /* We don't know of any usable RCIDs */
528
1.84M
    rcidm_set_preferred_rcid(rcidm, NULL);
529
1.84M
}
530
531
static int rcidm_should_roll(QUIC_RCIDM *rcidm)
532
10.6M
{
533
    /*
534
     * Always switch as soon as possible if handshake completes;
535
     * and every n packets after handshake completes or the last roll; and
536
     * whenever manually requested.
537
     */
538
10.6M
    return rcidm->handshake_complete
539
5.59M
        && (rcidm->num_changes == 0
540
4.69M
            || rcidm->packets_sent >= PACKETS_PER_RCID
541
4.01M
            || rcidm->roll_requested);
542
10.6M
}
543
544
static void rcidm_tick(QUIC_RCIDM *rcidm)
545
10.6M
{
546
10.6M
    if (rcidm_should_roll(rcidm))
547
3.32M
        rcidm_roll(rcidm);
548
549
10.6M
    rcidm_update(rcidm);
550
10.6M
}
551
552
/*
553
 * Events
554
 * ======
555
 */
556
void ossl_quic_rcidm_on_handshake_complete(QUIC_RCIDM *rcidm)
557
320k
{
558
320k
    if (rcidm->handshake_complete)
559
195k
        return;
560
561
125k
    rcidm->handshake_complete = 1;
562
125k
    rcidm_tick(rcidm);
563
125k
}
564
565
void ossl_quic_rcidm_on_packet_sent(QUIC_RCIDM *rcidm, uint64_t num_packets)
566
862k
{
567
862k
    if (num_packets == 0)
568
113k
        return;
569
570
748k
    rcidm->packets_sent += num_packets;
571
748k
    rcidm_tick(rcidm);
572
748k
}
573
574
void ossl_quic_rcidm_request_roll(QUIC_RCIDM *rcidm)
575
2.71M
{
576
2.71M
    rcidm->roll_requested = 1;
577
2.71M
    rcidm_tick(rcidm);
578
2.71M
}
579
580
/*
581
 * Mutation Operations
582
 * ===================
583
 */
584
int ossl_quic_rcidm_add_from_initial(QUIC_RCIDM *rcidm,
585
    const QUIC_CONN_ID *rcid)
586
249k
{
587
249k
    RCID *rcid_obj;
588
589
249k
    if (rcidm->added_initial_rcid || rcidm->handshake_complete)
590
128k
        return 0;
591
592
121k
    rcid_obj = rcidm_create_rcid(rcidm, INITIAL_SEQ_NUM,
593
121k
        rcid, RCID_TYPE_INITIAL);
594
121k
    if (rcid_obj == NULL)
595
26.5k
        return 0;
596
597
94.4k
    rcidm->added_initial_rcid = 1;
598
94.4k
    rcidm_tick(rcidm);
599
94.4k
    return 1;
600
121k
}
601
602
int ossl_quic_rcidm_add_from_server_retry(QUIC_RCIDM *rcidm,
603
    const QUIC_CONN_ID *retry_odcid)
604
530k
{
605
530k
    if (rcidm->added_retry_odcid || rcidm->handshake_complete)
606
413k
        return 0;
607
608
117k
    rcidm->retry_odcid = *retry_odcid;
609
117k
    rcidm->added_retry_odcid = 1;
610
117k
    rcidm_tick(rcidm);
611
117k
    return 1;
612
530k
}
613
614
int ossl_quic_rcidm_add_from_ncid(QUIC_RCIDM *rcidm,
615
    const OSSL_QUIC_FRAME_NEW_CONN_ID *ncid)
616
6.98M
{
617
6.98M
    RCID *rcid;
618
619
6.98M
    rcid = rcidm_create_rcid(rcidm, ncid->seq_num, &ncid->conn_id, RCID_TYPE_NCID);
620
6.98M
    if (rcid == NULL)
621
107k
        return 0;
622
623
6.88M
    rcidm_handle_retire_prior_to(rcidm, ncid->retire_prior_to);
624
6.88M
    rcidm_tick(rcidm);
625
6.88M
    return 1;
626
6.98M
}
627
628
/*
629
 * Queries
630
 * =======
631
 */
632
633
static int rcidm_get_retire(QUIC_RCIDM *rcidm, uint64_t *seq_num, int peek)
634
131k
{
635
131k
    RCID *rcid = ossl_list_retiring_head(&rcidm->retiring_list);
636
637
131k
    if (rcid == NULL)
638
83.0k
        return 0;
639
640
48.6k
    if (seq_num != NULL)
641
48.6k
        *seq_num = rcid->seq_num;
642
643
48.6k
    if (!peek)
644
17.9k
        rcidm_free_rcid(rcidm, rcid);
645
646
48.6k
    return 1;
647
131k
}
648
649
int ossl_quic_rcidm_pop_retire_seq_num(QUIC_RCIDM *rcidm,
650
    uint64_t *seq_num)
651
83.0k
{
652
83.0k
    return rcidm_get_retire(rcidm, seq_num, /*peek=*/0);
653
83.0k
}
654
655
int ossl_quic_rcidm_peek_retire_seq_num(QUIC_RCIDM *rcidm,
656
    uint64_t *seq_num)
657
48.6k
{
658
48.6k
    return rcidm_get_retire(rcidm, seq_num, /*peek=*/1);
659
48.6k
}
660
661
int ossl_quic_rcidm_get_preferred_tx_dcid(QUIC_RCIDM *rcidm,
662
    QUIC_CONN_ID *tx_dcid)
663
34.8k
{
664
34.8k
    if (!rcidm->have_preferred_rcid)
665
15.3k
        return 0;
666
667
19.5k
    *tx_dcid = rcidm->preferred_rcid;
668
19.5k
    return 1;
669
34.8k
}
670
671
int ossl_quic_rcidm_get_preferred_tx_dcid_changed(QUIC_RCIDM *rcidm,
672
    int clear)
673
215k
{
674
215k
    int r = rcidm->preferred_rcid_changed;
675
676
215k
    if (clear)
677
196k
        rcidm->preferred_rcid_changed = 0;
678
679
215k
    return r;
680
215k
}
681
682
size_t ossl_quic_rcidm_get_num_active(const QUIC_RCIDM *rcidm)
683
0
{
684
0
    return ossl_pqueue_RCID_num(rcidm->rcids)
685
0
        + (rcidm->cur_rcid != NULL ? 1 : 0)
686
0
        + ossl_quic_rcidm_get_num_retiring(rcidm);
687
0
}
688
689
size_t ossl_quic_rcidm_get_num_retiring(const QUIC_RCIDM *rcidm)
690
0
{
691
0
    return rcidm->num_retiring;
692
0
}