/src/openssl35/ssl/quic/quic_lcidm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023-2025 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_lcidm.h" |
11 | | #include "internal/quic_types.h" |
12 | | #include "internal/quic_vlint.h" |
13 | | #include "internal/common.h" |
14 | | #include "crypto/siphash.h" |
15 | | #include <openssl/lhash.h> |
16 | | #include <openssl/rand.h> |
17 | | #include <openssl/err.h> |
18 | | |
19 | | /* |
20 | | * QUIC Local Connection ID Manager |
21 | | * ================================ |
22 | | */ |
23 | | |
24 | | typedef struct quic_lcidm_conn_st QUIC_LCIDM_CONN; |
25 | | |
26 | | enum { |
27 | | LCID_TYPE_ODCID, /* This LCID is the ODCID from the peer */ |
28 | | LCID_TYPE_INITIAL, /* This is our Initial SCID */ |
29 | | LCID_TYPE_NCID /* This LCID was issued via a NCID frame */ |
30 | | }; |
31 | | |
32 | | typedef struct quic_lcid_st { |
33 | | QUIC_CONN_ID cid; |
34 | | uint64_t seq_num; |
35 | | |
36 | | /* copy of the hash key from lcidm */ |
37 | | uint64_t *hash_key; |
38 | | |
39 | | /* Back-pointer to the owning QUIC_LCIDM_CONN structure. */ |
40 | | QUIC_LCIDM_CONN *conn; |
41 | | |
42 | | /* LCID_TYPE_* */ |
43 | | unsigned int type : 2; |
44 | | } QUIC_LCID; |
45 | | |
46 | | DEFINE_LHASH_OF_EX(QUIC_LCID); |
47 | | DEFINE_LHASH_OF_EX(QUIC_LCIDM_CONN); |
48 | | |
49 | | struct quic_lcidm_conn_st { |
50 | | size_t num_active_lcid; |
51 | | LHASH_OF(QUIC_LCID) *lcids; |
52 | | void *opaque; |
53 | | QUIC_LCID *odcid_lcid_obj; |
54 | | uint64_t next_seq_num; |
55 | | |
56 | | /* Have we enrolled an ODCID? */ |
57 | | unsigned int done_odcid : 1; |
58 | | }; |
59 | | |
60 | | struct quic_lcidm_st { |
61 | | OSSL_LIB_CTX *libctx; |
62 | | uint64_t hash_key[2]; /* random key for siphash */ |
63 | | LHASH_OF(QUIC_LCID) *lcids; /* (QUIC_CONN_ID) -> (QUIC_LCID *) */ |
64 | | LHASH_OF(QUIC_LCIDM_CONN) *conns; /* (void *opaque) -> (QUIC_LCIDM_CONN *) */ |
65 | | size_t lcid_len; /* Length in bytes for all LCIDs */ |
66 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
67 | | QUIC_CONN_ID next_lcid; |
68 | | #endif |
69 | | }; |
70 | | |
71 | | static unsigned long lcid_hash(const QUIC_LCID *lcid_obj) |
72 | 7.11M | { |
73 | 7.11M | SIPHASH siphash = { |
74 | 7.11M | 0, |
75 | 7.11M | }; |
76 | 7.11M | unsigned long hashval = 0; |
77 | 7.11M | unsigned char digest[SIPHASH_MIN_DIGEST_SIZE]; |
78 | | |
79 | | /* Use a supported SipHash digest size (8 or 16); 8 is sufficient here. */ |
80 | 7.11M | if (!SipHash_set_hash_size(&siphash, SIPHASH_MIN_DIGEST_SIZE)) |
81 | 0 | goto out; |
82 | 7.11M | if (!SipHash_Init(&siphash, (uint8_t *)lcid_obj->hash_key, 0, 0)) |
83 | 0 | goto out; |
84 | 7.11M | SipHash_Update(&siphash, lcid_obj->cid.id, lcid_obj->cid.id_len); |
85 | 7.11M | if (!SipHash_Final(&siphash, digest, SIPHASH_MIN_DIGEST_SIZE)) |
86 | 0 | goto out; |
87 | | |
88 | | /* |
89 | | * Truncate the 64-bit SipHash digest into an unsigned long. |
90 | | */ |
91 | 7.11M | memcpy(&hashval, digest, sizeof(hashval) < sizeof(digest) ? sizeof(hashval) : sizeof(digest)); |
92 | 7.11M | out: |
93 | 7.11M | return hashval; |
94 | 7.11M | } |
95 | | |
96 | | static int lcid_comp(const QUIC_LCID *a, const QUIC_LCID *b) |
97 | 11.6M | { |
98 | 11.6M | return !ossl_quic_conn_id_eq(&a->cid, &b->cid); |
99 | 11.6M | } |
100 | | |
101 | | static unsigned long lcidm_conn_hash(const QUIC_LCIDM_CONN *conn) |
102 | 3.77M | { |
103 | 3.77M | return (unsigned long)(uintptr_t)conn->opaque; |
104 | 3.77M | } |
105 | | |
106 | | static int lcidm_conn_comp(const QUIC_LCIDM_CONN *a, const QUIC_LCIDM_CONN *b) |
107 | 3.43M | { |
108 | 3.43M | return a->opaque != b->opaque; |
109 | 3.43M | } |
110 | | |
111 | | QUIC_LCIDM *ossl_quic_lcidm_new(OSSL_LIB_CTX *libctx, size_t lcid_len) |
112 | 33.9k | { |
113 | 33.9k | QUIC_LCIDM *lcidm = NULL; |
114 | | |
115 | 33.9k | if (lcid_len > QUIC_MAX_CONN_ID_LEN) |
116 | 0 | goto err; |
117 | | |
118 | 33.9k | if ((lcidm = OPENSSL_zalloc(sizeof(*lcidm))) == NULL) |
119 | 0 | goto err; |
120 | | |
121 | | /* generate a random key for the hash tables hash function */ |
122 | 33.9k | if (!RAND_bytes_ex(libctx, (unsigned char *)&lcidm->hash_key, |
123 | 33.9k | sizeof(uint64_t) * 2, 0)) |
124 | 0 | goto err; |
125 | | |
126 | 33.9k | if ((lcidm->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL) |
127 | 0 | goto err; |
128 | | |
129 | 33.9k | if ((lcidm->conns = lh_QUIC_LCIDM_CONN_new(lcidm_conn_hash, |
130 | 33.9k | lcidm_conn_comp)) |
131 | 33.9k | == NULL) |
132 | 0 | goto err; |
133 | | |
134 | 33.9k | lcidm->libctx = libctx; |
135 | 33.9k | lcidm->lcid_len = lcid_len; |
136 | 33.9k | return lcidm; |
137 | | |
138 | 0 | err: |
139 | 0 | if (lcidm != NULL) { |
140 | 0 | lh_QUIC_LCID_free(lcidm->lcids); |
141 | 0 | lh_QUIC_LCIDM_CONN_free(lcidm->conns); |
142 | 0 | OPENSSL_free(lcidm); |
143 | 0 | } |
144 | 0 | return NULL; |
145 | 33.9k | } |
146 | | |
147 | | static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn); |
148 | | |
149 | | static void lcidm_delete_conn_(QUIC_LCIDM_CONN *conn, void *arg) |
150 | 71.8k | { |
151 | 71.8k | lcidm_delete_conn((QUIC_LCIDM *)arg, conn); |
152 | 71.8k | } |
153 | | |
154 | | void ossl_quic_lcidm_free(QUIC_LCIDM *lcidm) |
155 | 57.5k | { |
156 | 57.5k | if (lcidm == NULL) |
157 | 19 | return; |
158 | | |
159 | | /* |
160 | | * Calling OPENSSL_lh_delete during a doall call is unsafe with our |
161 | | * current LHASH implementation for several reasons: |
162 | | * |
163 | | * - firstly, because deletes can cause the hashtable to be contracted, |
164 | | * resulting in rehashing which might cause items in later buckets to |
165 | | * move to earlier buckets, which might cause doall to skip an item, |
166 | | * resulting in a memory leak; |
167 | | * |
168 | | * - secondly, because doall in general is not safe across hashtable |
169 | | * size changes, as it caches hashtable size and pointer values |
170 | | * while operating. |
171 | | * |
172 | | * The fix for this is to disable hashtable contraction using the following |
173 | | * call, which guarantees that no rehashing will occur so long as we only |
174 | | * call delete and not insert. |
175 | | */ |
176 | 57.5k | lh_QUIC_LCIDM_CONN_set_down_load(lcidm->conns, 0); |
177 | | |
178 | 57.5k | lh_QUIC_LCIDM_CONN_doall_arg(lcidm->conns, lcidm_delete_conn_, lcidm); |
179 | | |
180 | 57.5k | lh_QUIC_LCID_free(lcidm->lcids); |
181 | 57.5k | lh_QUIC_LCIDM_CONN_free(lcidm->conns); |
182 | 57.5k | OPENSSL_free(lcidm); |
183 | 57.5k | } |
184 | | |
185 | | static QUIC_LCID *lcidm_get0_lcid(const QUIC_LCIDM *lcidm, const QUIC_CONN_ID *lcid) |
186 | 4.83M | { |
187 | 4.83M | QUIC_LCID key; |
188 | | |
189 | 4.83M | key.cid = *lcid; |
190 | 4.83M | key.hash_key = (uint64_t *)lcidm->hash_key; |
191 | | |
192 | 4.83M | if (key.cid.id_len > QUIC_MAX_CONN_ID_LEN) |
193 | 0 | return NULL; |
194 | | |
195 | 4.83M | return lh_QUIC_LCID_retrieve(lcidm->lcids, &key); |
196 | 4.83M | } |
197 | | |
198 | | static QUIC_LCIDM_CONN *lcidm_get0_conn(const QUIC_LCIDM *lcidm, void *opaque) |
199 | 3.25M | { |
200 | 3.25M | QUIC_LCIDM_CONN key; |
201 | | |
202 | 3.25M | key.opaque = opaque; |
203 | | |
204 | 3.25M | return lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key); |
205 | 3.25M | } |
206 | | |
207 | | static QUIC_LCIDM_CONN *lcidm_upsert_conn(const QUIC_LCIDM *lcidm, void *opaque) |
208 | 3.25M | { |
209 | 3.25M | QUIC_LCIDM_CONN *conn = lcidm_get0_conn(lcidm, opaque); |
210 | | |
211 | 3.25M | if (conn != NULL) |
212 | 3.11M | return conn; |
213 | | |
214 | 139k | if ((conn = OPENSSL_zalloc(sizeof(*conn))) == NULL) |
215 | 0 | goto err; |
216 | | |
217 | 139k | if ((conn->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL) |
218 | 0 | goto err; |
219 | | |
220 | 139k | conn->opaque = opaque; |
221 | | |
222 | 139k | lh_QUIC_LCIDM_CONN_insert(lcidm->conns, conn); |
223 | 139k | if (lh_QUIC_LCIDM_CONN_error(lcidm->conns)) |
224 | 0 | goto err; |
225 | | |
226 | 139k | return conn; |
227 | | |
228 | 0 | err: |
229 | 0 | if (conn != NULL) { |
230 | 0 | lh_QUIC_LCID_free(conn->lcids); |
231 | 0 | OPENSSL_free(conn); |
232 | 0 | } |
233 | 0 | return NULL; |
234 | 139k | } |
235 | | |
236 | | static void lcidm_delete_conn_lcid(QUIC_LCIDM *lcidm, QUIC_LCID *lcid_obj) |
237 | 2.94M | { |
238 | 2.94M | lh_QUIC_LCID_delete(lcidm->lcids, lcid_obj); |
239 | 2.94M | lh_QUIC_LCID_delete(lcid_obj->conn->lcids, lcid_obj); |
240 | 2.94M | assert(lcid_obj->conn->num_active_lcid > 0); |
241 | 2.94M | --lcid_obj->conn->num_active_lcid; |
242 | 2.94M | OPENSSL_free(lcid_obj); |
243 | 2.94M | } |
244 | | |
245 | | /* doall_arg wrapper */ |
246 | | static void lcidm_delete_conn_lcid_(QUIC_LCID *lcid_obj, void *arg) |
247 | 2.92M | { |
248 | 2.92M | lcidm_delete_conn_lcid((QUIC_LCIDM *)arg, lcid_obj); |
249 | 2.92M | } |
250 | | |
251 | | static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn) |
252 | 139k | { |
253 | | /* See comment in ossl_quic_lcidm_free */ |
254 | 139k | lh_QUIC_LCID_set_down_load(conn->lcids, 0); |
255 | | |
256 | 139k | lh_QUIC_LCID_doall_arg(conn->lcids, lcidm_delete_conn_lcid_, lcidm); |
257 | 139k | lh_QUIC_LCIDM_CONN_delete(lcidm->conns, conn); |
258 | 139k | lh_QUIC_LCID_free(conn->lcids); |
259 | 139k | OPENSSL_free(conn); |
260 | 139k | } |
261 | | |
262 | | static QUIC_LCID *lcidm_conn_new_lcid(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn, |
263 | | const QUIC_CONN_ID *lcid) |
264 | 2.94M | { |
265 | 2.94M | QUIC_LCID *lcid_obj = NULL; |
266 | | |
267 | 2.94M | if (lcid->id_len > QUIC_MAX_CONN_ID_LEN) |
268 | 0 | return NULL; |
269 | | |
270 | 2.94M | if ((lcid_obj = OPENSSL_zalloc(sizeof(*lcid_obj))) == NULL) |
271 | 0 | goto err; |
272 | | |
273 | 2.94M | lcid_obj->cid = *lcid; |
274 | 2.94M | lcid_obj->conn = conn; |
275 | 2.94M | lcid_obj->hash_key = lcidm->hash_key; |
276 | | |
277 | 2.94M | lh_QUIC_LCID_insert(conn->lcids, lcid_obj); |
278 | 2.94M | if (lh_QUIC_LCID_error(conn->lcids)) |
279 | 0 | goto err; |
280 | | |
281 | 2.94M | lh_QUIC_LCID_insert(lcidm->lcids, lcid_obj); |
282 | 2.94M | if (lh_QUIC_LCID_error(lcidm->lcids)) { |
283 | 0 | lh_QUIC_LCID_delete(conn->lcids, lcid_obj); |
284 | 0 | goto err; |
285 | 0 | } |
286 | | |
287 | 2.94M | ++conn->num_active_lcid; |
288 | 2.94M | return lcid_obj; |
289 | | |
290 | 0 | err: |
291 | 0 | OPENSSL_free(lcid_obj); |
292 | 0 | return NULL; |
293 | 2.94M | } |
294 | | |
295 | | size_t ossl_quic_lcidm_get_lcid_len(const QUIC_LCIDM *lcidm) |
296 | 0 | { |
297 | 0 | return lcidm->lcid_len; |
298 | 0 | } |
299 | | |
300 | | size_t ossl_quic_lcidm_get_num_active_lcid(const QUIC_LCIDM *lcidm, |
301 | | void *opaque) |
302 | 0 | { |
303 | 0 | QUIC_LCIDM_CONN *conn; |
304 | |
|
305 | 0 | conn = lcidm_get0_conn(lcidm, opaque); |
306 | 0 | if (conn == NULL) |
307 | 0 | return 0; |
308 | | |
309 | 0 | return conn->num_active_lcid; |
310 | 0 | } |
311 | | |
312 | | static int lcidm_generate_cid(QUIC_LCIDM *lcidm, |
313 | | QUIC_CONN_ID *cid) |
314 | 4.13M | { |
315 | 4.13M | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
316 | 4.13M | int i; |
317 | | |
318 | 4.13M | lcidm->next_lcid.id_len = (unsigned char)lcidm->lcid_len; |
319 | 4.13M | *cid = lcidm->next_lcid; |
320 | | |
321 | 4.14M | for (i = lcidm->lcid_len - 1; i >= 0; --i) |
322 | 3.29M | if (++lcidm->next_lcid.id[i] != 0) |
323 | 3.28M | break; |
324 | | |
325 | 4.13M | return 1; |
326 | | #else |
327 | | return ossl_quic_gen_rand_conn_id(lcidm->libctx, lcidm->lcid_len, cid); |
328 | | #endif |
329 | 4.13M | } |
330 | | |
331 | | static int lcidm_generate(QUIC_LCIDM *lcidm, |
332 | | void *opaque, |
333 | | unsigned int type, |
334 | | QUIC_CONN_ID *lcid_out, |
335 | | uint64_t *seq_num) |
336 | 3.18M | { |
337 | 3.18M | QUIC_LCIDM_CONN *conn; |
338 | 3.18M | QUIC_LCID key, *lcid_obj; |
339 | 3.18M | size_t i; |
340 | 4.28M | #define MAX_RETRIES 8 |
341 | | |
342 | 3.18M | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
343 | 0 | return 0; |
344 | | |
345 | 3.18M | if ((type == LCID_TYPE_INITIAL && conn->next_seq_num > 0) |
346 | 3.08M | || conn->next_seq_num > OSSL_QUIC_VLINT_MAX) |
347 | 103k | return 0; |
348 | | |
349 | 3.08M | i = 0; |
350 | 4.28M | do { |
351 | 4.28M | if (i++ >= MAX_RETRIES) |
352 | | /* |
353 | | * Too many retries; should not happen but if it does, don't loop |
354 | | * endlessly. |
355 | | */ |
356 | 148k | return 0; |
357 | | |
358 | 4.13M | if (!lcidm_generate_cid(lcidm, lcid_out)) |
359 | 0 | return 0; |
360 | | |
361 | 4.13M | key.cid = *lcid_out; |
362 | 4.13M | key.hash_key = lcidm->hash_key; |
363 | | |
364 | | /* If a collision occurs, retry. */ |
365 | 4.13M | } while (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL); |
366 | | |
367 | 2.93M | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid_out)) == NULL) |
368 | 0 | return 0; |
369 | | |
370 | 2.93M | lcid_obj->seq_num = conn->next_seq_num; |
371 | 2.93M | lcid_obj->type = type; |
372 | | |
373 | 2.93M | if (seq_num != NULL) |
374 | 2.86M | *seq_num = lcid_obj->seq_num; |
375 | | |
376 | 2.93M | ++conn->next_seq_num; |
377 | 2.93M | return 1; |
378 | 2.93M | } |
379 | | |
380 | | int ossl_quic_lcidm_enrol_odcid(QUIC_LCIDM *lcidm, |
381 | | void *opaque, |
382 | | const QUIC_CONN_ID *initial_odcid) |
383 | 754k | { |
384 | 754k | QUIC_LCIDM_CONN *conn; |
385 | 754k | QUIC_LCID key, *lcid_obj; |
386 | | |
387 | 754k | if (initial_odcid == NULL || initial_odcid->id_len < QUIC_MIN_ODCID_LEN |
388 | 23.5k | || initial_odcid->id_len > QUIC_MAX_CONN_ID_LEN) |
389 | 730k | return 0; |
390 | | |
391 | 23.5k | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
392 | 0 | return 0; |
393 | | |
394 | 23.5k | if (conn->done_odcid) |
395 | 9.12k | return 0; |
396 | | |
397 | 14.3k | key.cid = *initial_odcid; |
398 | 14.3k | key.hash_key = lcidm->hash_key; |
399 | 14.3k | if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL) |
400 | 2.36k | return 0; |
401 | | |
402 | 12.0k | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, initial_odcid)) == NULL) |
403 | 0 | return 0; |
404 | | |
405 | 12.0k | lcid_obj->seq_num = LCIDM_ODCID_SEQ_NUM; |
406 | 12.0k | lcid_obj->type = LCID_TYPE_ODCID; |
407 | | |
408 | 12.0k | conn->odcid_lcid_obj = lcid_obj; |
409 | 12.0k | conn->done_odcid = 1; |
410 | 12.0k | return 1; |
411 | 12.0k | } |
412 | | |
413 | | int ossl_quic_lcidm_generate_initial(QUIC_LCIDM *lcidm, |
414 | | void *opaque, |
415 | | QUIC_CONN_ID *initial_lcid) |
416 | 200k | { |
417 | 200k | return lcidm_generate(lcidm, opaque, LCID_TYPE_INITIAL, |
418 | 200k | initial_lcid, NULL); |
419 | 200k | } |
420 | | |
421 | | int ossl_quic_lcidm_bind_channel(QUIC_LCIDM *lcidm, void *opaque, |
422 | | const QUIC_CONN_ID *lcid) |
423 | 0 | { |
424 | 0 | QUIC_LCIDM_CONN *conn; |
425 | 0 | QUIC_LCID *lcid_obj; |
426 | | |
427 | | /* |
428 | | * the plan is simple: |
429 | | * make sure the lcid is still unused. |
430 | | * do the same business as ossl_quic_lcidm_gnerate_initial() does, |
431 | | * except we will use lcid instead of generating a new one. |
432 | | */ |
433 | 0 | if (ossl_quic_lcidm_lookup(lcidm, lcid, NULL, NULL) != 0) |
434 | 0 | return 0; |
435 | | |
436 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
437 | 0 | return 0; |
438 | | |
439 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid)) == NULL) { |
440 | 0 | lcidm_delete_conn(lcidm, conn); |
441 | 0 | return 0; |
442 | 0 | } |
443 | | |
444 | 0 | lcid_obj->seq_num = conn->next_seq_num; |
445 | 0 | lcid_obj->type = LCID_TYPE_INITIAL; |
446 | 0 | conn->next_seq_num++; |
447 | |
|
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | | int ossl_quic_lcidm_generate(QUIC_LCIDM *lcidm, |
452 | | void *opaque, |
453 | | OSSL_QUIC_FRAME_NEW_CONN_ID *ncid_frame) |
454 | 2.98M | { |
455 | 2.98M | ncid_frame->seq_num = 0; |
456 | 2.98M | ncid_frame->retire_prior_to = 0; |
457 | | |
458 | 2.98M | return lcidm_generate(lcidm, opaque, LCID_TYPE_NCID, |
459 | 2.98M | &ncid_frame->conn_id, |
460 | 2.98M | &ncid_frame->seq_num); |
461 | 2.98M | } |
462 | | |
463 | | int ossl_quic_lcidm_retire_odcid(QUIC_LCIDM *lcidm, void *opaque) |
464 | 40.5k | { |
465 | 40.5k | QUIC_LCIDM_CONN *conn; |
466 | | |
467 | 40.5k | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
468 | 0 | return 0; |
469 | | |
470 | 40.5k | if (conn->odcid_lcid_obj == NULL) |
471 | 34.3k | return 0; |
472 | | |
473 | 6.21k | lcidm_delete_conn_lcid(lcidm, conn->odcid_lcid_obj); |
474 | 6.21k | conn->odcid_lcid_obj = NULL; |
475 | 6.21k | return 1; |
476 | 40.5k | } |
477 | | |
478 | | struct retire_args { |
479 | | QUIC_LCID *earliest_seq_num_lcid_obj; |
480 | | uint64_t earliest_seq_num, retire_prior_to; |
481 | | }; |
482 | | |
483 | | static void retire_for_conn(QUIC_LCID *lcid_obj, void *arg) |
484 | 8.27M | { |
485 | 8.27M | struct retire_args *args = arg; |
486 | | |
487 | | /* ODCID LCID cannot be retired via this API */ |
488 | 8.27M | if (lcid_obj->type == LCID_TYPE_ODCID |
489 | 8.25M | || lcid_obj->seq_num >= args->retire_prior_to) |
490 | 2.23M | return; |
491 | | |
492 | 6.04M | if (lcid_obj->seq_num < args->earliest_seq_num) { |
493 | 406k | args->earliest_seq_num = lcid_obj->seq_num; |
494 | 406k | args->earliest_seq_num_lcid_obj = lcid_obj; |
495 | 406k | } |
496 | 6.04M | } |
497 | | |
498 | | int ossl_quic_lcidm_retire(QUIC_LCIDM *lcidm, |
499 | | void *opaque, |
500 | | uint64_t retire_prior_to, |
501 | | const QUIC_CONN_ID *containing_pkt_dcid, |
502 | | QUIC_CONN_ID *retired_lcid, |
503 | | uint64_t *retired_seq_num, |
504 | | int *did_retire) |
505 | 132k | { |
506 | 132k | QUIC_LCIDM_CONN key, *conn; |
507 | 132k | struct retire_args args = { 0 }; |
508 | | |
509 | 132k | key.opaque = opaque; |
510 | | |
511 | 132k | if (did_retire == NULL) |
512 | 0 | return 0; |
513 | | |
514 | 132k | *did_retire = 0; |
515 | 132k | if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL) |
516 | 23.3k | return 1; |
517 | | |
518 | 108k | args.retire_prior_to = retire_prior_to; |
519 | 108k | args.earliest_seq_num = UINT64_MAX; |
520 | | |
521 | 108k | lh_QUIC_LCID_doall_arg(conn->lcids, retire_for_conn, &args); |
522 | 108k | if (args.earliest_seq_num_lcid_obj == NULL) |
523 | 87.2k | return 1; |
524 | | |
525 | 21.5k | if (containing_pkt_dcid != NULL |
526 | 0 | && ossl_quic_conn_id_eq(&args.earliest_seq_num_lcid_obj->cid, |
527 | 0 | containing_pkt_dcid)) |
528 | 0 | return 0; |
529 | | |
530 | 21.5k | *did_retire = 1; |
531 | 21.5k | if (retired_lcid != NULL) |
532 | 21.5k | *retired_lcid = args.earliest_seq_num_lcid_obj->cid; |
533 | 21.5k | if (retired_seq_num != NULL) |
534 | 21.5k | *retired_seq_num = args.earliest_seq_num_lcid_obj->seq_num; |
535 | | |
536 | 21.5k | lcidm_delete_conn_lcid(lcidm, args.earliest_seq_num_lcid_obj); |
537 | 21.5k | return 1; |
538 | 21.5k | } |
539 | | |
540 | | int ossl_quic_lcidm_cull(QUIC_LCIDM *lcidm, void *opaque) |
541 | 106k | { |
542 | 106k | QUIC_LCIDM_CONN key, *conn; |
543 | | |
544 | 106k | key.opaque = opaque; |
545 | | |
546 | 106k | if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL) |
547 | 38.7k | return 0; |
548 | | |
549 | 67.3k | lcidm_delete_conn(lcidm, conn); |
550 | 67.3k | return 1; |
551 | 106k | } |
552 | | |
553 | | int ossl_quic_lcidm_lookup(QUIC_LCIDM *lcidm, |
554 | | const QUIC_CONN_ID *lcid, |
555 | | uint64_t *seq_num, |
556 | | void **opaque) |
557 | 4.83M | { |
558 | 4.83M | QUIC_LCID *lcid_obj; |
559 | | |
560 | 4.83M | if (lcid == NULL) |
561 | 0 | return 0; |
562 | | |
563 | 4.83M | if ((lcid_obj = lcidm_get0_lcid(lcidm, lcid)) == NULL) |
564 | 378k | return 0; |
565 | | |
566 | 4.45M | if (seq_num != NULL) |
567 | 20.3k | *seq_num = lcid_obj->seq_num; |
568 | | |
569 | 4.45M | if (opaque != NULL) |
570 | 4.45M | *opaque = lcid_obj->conn->opaque; |
571 | | |
572 | 4.45M | return 1; |
573 | 4.83M | } |
574 | | |
575 | | int ossl_quic_lcidm_debug_remove(QUIC_LCIDM *lcidm, |
576 | | const QUIC_CONN_ID *lcid) |
577 | 0 | { |
578 | 0 | QUIC_LCID key, *lcid_obj; |
579 | |
|
580 | 0 | key.cid = *lcid; |
581 | 0 | key.hash_key = lcidm->hash_key; |
582 | 0 | if ((lcid_obj = lh_QUIC_LCID_retrieve(lcidm->lcids, &key)) == NULL) |
583 | 0 | return 0; |
584 | | |
585 | 0 | lcidm_delete_conn_lcid(lcidm, lcid_obj); |
586 | 0 | return 1; |
587 | 0 | } |
588 | | |
589 | | int ossl_quic_lcidm_debug_add(QUIC_LCIDM *lcidm, void *opaque, |
590 | | const QUIC_CONN_ID *lcid, |
591 | | uint64_t seq_num) |
592 | 0 | { |
593 | 0 | QUIC_LCIDM_CONN *conn; |
594 | 0 | QUIC_LCID key, *lcid_obj; |
595 | |
|
596 | 0 | if (lcid == NULL || lcid->id_len > QUIC_MAX_CONN_ID_LEN) |
597 | 0 | return 0; |
598 | | |
599 | 0 | if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL) |
600 | 0 | return 0; |
601 | | |
602 | 0 | key.cid = *lcid; |
603 | 0 | key.hash_key = lcidm->hash_key; |
604 | 0 | if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL) |
605 | 0 | return 0; |
606 | | |
607 | 0 | if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid)) == NULL) |
608 | 0 | return 0; |
609 | | |
610 | 0 | lcid_obj->seq_num = seq_num; |
611 | 0 | lcid_obj->type = LCID_TYPE_NCID; |
612 | 0 | return 1; |
613 | 0 | } |
614 | | |
615 | | int ossl_quic_lcidm_get_unused_cid(QUIC_LCIDM *lcidm, QUIC_CONN_ID *cid) |
616 | 0 | { |
617 | 0 | int i; |
618 | |
|
619 | 0 | for (i = 0; i < 10; i++) { |
620 | 0 | if (lcidm_generate_cid(lcidm, cid) |
621 | 0 | && lcidm_get0_lcid(lcidm, cid) == NULL) |
622 | 0 | return 1; /* not found <=> radomly generated cid is unused */ |
623 | 0 | } |
624 | | |
625 | 0 | return 0; |
626 | 0 | } |