/src/openssl32/ssl/quic/quic_demux.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022-2023 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_demux.h" |
11 | | #include "internal/quic_wire_pkt.h" |
12 | | #include "internal/common.h" |
13 | | #include <openssl/lhash.h> |
14 | | #include <openssl/err.h> |
15 | | |
16 | 5.91M | #define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */ |
17 | 5.19M | #define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */ |
18 | 845k | #define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */ |
19 | | |
20 | 17.7M | #define DEMUX_MAX_MSGS_PER_CALL 32 |
21 | | |
22 | 11.1k | #define DEMUX_DEFAULT_MTU 1500 |
23 | | |
24 | | /* Structure used to track a given connection ID. */ |
25 | | typedef struct quic_demux_conn_st QUIC_DEMUX_CONN; |
26 | | |
27 | | struct quic_demux_conn_st { |
28 | | QUIC_DEMUX_CONN *next; /* used when unregistering only */ |
29 | | QUIC_CONN_ID dst_conn_id; |
30 | | ossl_quic_demux_cb_fn *cb; |
31 | | void *cb_arg; |
32 | | }; |
33 | | |
34 | | DEFINE_LHASH_OF_EX(QUIC_DEMUX_CONN); |
35 | | |
36 | | static unsigned long demux_conn_hash(const QUIC_DEMUX_CONN *conn) |
37 | 882k | { |
38 | 882k | size_t i; |
39 | 882k | unsigned long v = 0; |
40 | | |
41 | 882k | assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN); |
42 | | |
43 | 896k | for (i = 0; i < conn->dst_conn_id.id_len; ++i) |
44 | 14.5k | v ^= ((unsigned long)conn->dst_conn_id.id[i]) |
45 | 14.5k | << ((i * 8) % (sizeof(unsigned long) * 8)); |
46 | | |
47 | 882k | return v; |
48 | 882k | } |
49 | | |
50 | | static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b) |
51 | 857k | { |
52 | 857k | return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id); |
53 | 857k | } |
54 | | |
55 | | struct quic_demux_st { |
56 | | /* The underlying transport BIO with datagram semantics. */ |
57 | | BIO *net_bio; |
58 | | |
59 | | /* |
60 | | * QUIC short packets do not contain the length of the connection ID field, |
61 | | * therefore it must be known contextually. The demuxer requires connection |
62 | | * IDs of the same length to be used for all incoming packets. |
63 | | */ |
64 | | size_t short_conn_id_len; |
65 | | |
66 | | /* |
67 | | * Our current understanding of the upper bound on an incoming datagram size |
68 | | * in bytes. |
69 | | */ |
70 | | size_t mtu; |
71 | | |
72 | | /* Time retrieval callback. */ |
73 | | OSSL_TIME (*now)(void *arg); |
74 | | void *now_arg; |
75 | | |
76 | | /* Hashtable mapping connection IDs to QUIC_DEMUX_CONN structures. */ |
77 | | LHASH_OF(QUIC_DEMUX_CONN) *conns_by_id; |
78 | | |
79 | | /* The default packet handler, if any. */ |
80 | | ossl_quic_demux_cb_fn *default_cb; |
81 | | void *default_cb_arg; |
82 | | |
83 | | /* The stateless reset token checker handler, if any. */ |
84 | | ossl_quic_stateless_reset_cb_fn *reset_token_cb; |
85 | | void *reset_token_cb_arg; |
86 | | |
87 | | /* |
88 | | * List of URXEs which are not currently in use (i.e., not filled with |
89 | | * unconsumed data). These are moved to the pending list as they are filled. |
90 | | */ |
91 | | QUIC_URXE_LIST urx_free; |
92 | | |
93 | | /* |
94 | | * List of URXEs which are filled with received encrypted data. These are |
95 | | * removed from this list as we invoke the callbacks for each of them. They |
96 | | * are then not on any list managed by us; we forget about them until our |
97 | | * user calls ossl_quic_demux_release_urxe to return the URXE to us, at |
98 | | * which point we add it to the free list. |
99 | | */ |
100 | | QUIC_URXE_LIST urx_pending; |
101 | | |
102 | | /* Whether to use local address support. */ |
103 | | char use_local_addr; |
104 | | }; |
105 | | |
106 | | QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio, |
107 | | size_t short_conn_id_len, |
108 | | OSSL_TIME (*now)(void *arg), |
109 | | void *now_arg) |
110 | 11.1k | { |
111 | 11.1k | QUIC_DEMUX *demux; |
112 | | |
113 | 11.1k | demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX)); |
114 | 11.1k | if (demux == NULL) |
115 | 0 | return NULL; |
116 | | |
117 | 11.1k | demux->net_bio = net_bio; |
118 | 11.1k | demux->short_conn_id_len = short_conn_id_len; |
119 | | /* We update this if possible when we get a BIO. */ |
120 | 11.1k | demux->mtu = DEMUX_DEFAULT_MTU; |
121 | 11.1k | demux->now = now; |
122 | 11.1k | demux->now_arg = now_arg; |
123 | | |
124 | 11.1k | demux->conns_by_id |
125 | 11.1k | = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp); |
126 | 11.1k | if (demux->conns_by_id == NULL) { |
127 | 0 | OPENSSL_free(demux); |
128 | 0 | return NULL; |
129 | 0 | } |
130 | | |
131 | 11.1k | if (net_bio != NULL |
132 | 11.1k | && BIO_dgram_get_local_addr_cap(net_bio) |
133 | 11.1k | && BIO_dgram_set_local_addr_enable(net_bio, 1)) |
134 | 0 | demux->use_local_addr = 1; |
135 | | |
136 | 11.1k | return demux; |
137 | 11.1k | } |
138 | | |
139 | | static void demux_free_conn_it(QUIC_DEMUX_CONN *conn, void *arg) |
140 | 0 | { |
141 | 0 | OPENSSL_free(conn); |
142 | 0 | } |
143 | | |
144 | | static void demux_free_urxl(QUIC_URXE_LIST *l) |
145 | 44.3k | { |
146 | 44.3k | QUIC_URXE *e, *enext; |
147 | | |
148 | 761k | for (e = ossl_list_urxe_head(l); e != NULL; e = enext) { |
149 | 716k | enext = ossl_list_urxe_next(e); |
150 | 716k | ossl_list_urxe_remove(l, e); |
151 | 716k | OPENSSL_free(e); |
152 | 716k | } |
153 | 44.3k | } |
154 | | |
155 | | void ossl_quic_demux_free(QUIC_DEMUX *demux) |
156 | 22.1k | { |
157 | 22.1k | if (demux == NULL) |
158 | 0 | return; |
159 | | |
160 | | /* Free all connection structures. */ |
161 | 22.1k | lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL); |
162 | 22.1k | lh_QUIC_DEMUX_CONN_free(demux->conns_by_id); |
163 | | |
164 | | /* Free all URXEs we are holding. */ |
165 | 22.1k | demux_free_urxl(&demux->urx_free); |
166 | 22.1k | demux_free_urxl(&demux->urx_pending); |
167 | | |
168 | 22.1k | OPENSSL_free(demux); |
169 | 22.1k | } |
170 | | |
171 | | void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio) |
172 | 22.1k | { |
173 | 22.1k | unsigned int mtu; |
174 | | |
175 | 22.1k | demux->net_bio = net_bio; |
176 | | |
177 | 22.1k | if (net_bio != NULL) { |
178 | | /* |
179 | | * Try to determine our MTU if possible. The BIO is not required to |
180 | | * support this, in which case we remain at the last known MTU, or our |
181 | | * initial default. |
182 | | */ |
183 | 22.1k | mtu = BIO_dgram_get_mtu(net_bio); |
184 | 22.1k | if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN) |
185 | 22.1k | ossl_quic_demux_set_mtu(demux, mtu); /* best effort */ |
186 | 22.1k | } |
187 | 22.1k | } |
188 | | |
189 | | int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu) |
190 | 22.1k | { |
191 | 22.1k | if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN) |
192 | 0 | return 0; |
193 | | |
194 | 22.1k | demux->mtu = mtu; |
195 | 22.1k | return 1; |
196 | 22.1k | } |
197 | | |
198 | | static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux, |
199 | | const QUIC_CONN_ID *dst_conn_id) |
200 | 859k | { |
201 | 859k | QUIC_DEMUX_CONN key; |
202 | | |
203 | 859k | if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN) |
204 | 0 | return NULL; |
205 | | |
206 | 859k | key.dst_conn_id = *dst_conn_id; |
207 | 859k | return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key); |
208 | 859k | } |
209 | | |
210 | | int ossl_quic_demux_register(QUIC_DEMUX *demux, |
211 | | const QUIC_CONN_ID *dst_conn_id, |
212 | | ossl_quic_demux_cb_fn *cb, void *cb_arg) |
213 | 11.1k | { |
214 | 11.1k | QUIC_DEMUX_CONN *conn; |
215 | | |
216 | 11.1k | if (dst_conn_id == NULL |
217 | 11.1k | || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN |
218 | 11.1k | || cb == NULL) |
219 | 0 | return 0; |
220 | | |
221 | | /* Ensure not already registered. */ |
222 | 11.1k | if (demux_get_by_conn_id(demux, dst_conn_id) != NULL) |
223 | | /* Handler already registered with this connection ID. */ |
224 | 0 | return 0; |
225 | | |
226 | 11.1k | conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN)); |
227 | 11.1k | if (conn == NULL) |
228 | 0 | return 0; |
229 | | |
230 | 11.1k | conn->dst_conn_id = *dst_conn_id; |
231 | 11.1k | conn->cb = cb; |
232 | 11.1k | conn->cb_arg = cb_arg; |
233 | | |
234 | 11.1k | lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn); |
235 | 11.1k | return 1; |
236 | 11.1k | } |
237 | | |
238 | | static void demux_unregister(QUIC_DEMUX *demux, |
239 | | QUIC_DEMUX_CONN *conn) |
240 | 11.1k | { |
241 | 11.1k | lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn); |
242 | 11.1k | OPENSSL_free(conn); |
243 | 11.1k | } |
244 | | |
245 | | int ossl_quic_demux_unregister(QUIC_DEMUX *demux, |
246 | | const QUIC_CONN_ID *dst_conn_id) |
247 | 0 | { |
248 | 0 | QUIC_DEMUX_CONN *conn; |
249 | |
|
250 | 0 | if (dst_conn_id == NULL |
251 | 0 | || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN) |
252 | 0 | return 0; |
253 | | |
254 | 0 | conn = demux_get_by_conn_id(demux, dst_conn_id); |
255 | 0 | if (conn == NULL) |
256 | 0 | return 0; |
257 | | |
258 | 0 | demux_unregister(demux, conn); |
259 | 0 | return 1; |
260 | 0 | } |
261 | | |
262 | | struct unreg_arg { |
263 | | ossl_quic_demux_cb_fn *cb; |
264 | | void *cb_arg; |
265 | | QUIC_DEMUX_CONN *head; |
266 | | }; |
267 | | |
268 | | static void demux_unregister_by_cb(QUIC_DEMUX_CONN *conn, void *arg_) |
269 | 11.1k | { |
270 | 11.1k | struct unreg_arg *arg = arg_; |
271 | | |
272 | 11.1k | if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) { |
273 | 11.1k | conn->next = arg->head; |
274 | 11.1k | arg->head = conn; |
275 | 11.1k | } |
276 | 11.1k | } |
277 | | |
278 | | void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux, |
279 | | ossl_quic_demux_cb_fn *cb, |
280 | | void *cb_arg) |
281 | 11.1k | { |
282 | 11.1k | QUIC_DEMUX_CONN *conn, *cnext; |
283 | 11.1k | struct unreg_arg arg = {0}; |
284 | 11.1k | arg.cb = cb; |
285 | 11.1k | arg.cb_arg = cb_arg; |
286 | | |
287 | 11.1k | lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, |
288 | 11.1k | demux_unregister_by_cb, &arg); |
289 | | |
290 | 22.3k | for (conn = arg.head; conn != NULL; conn = cnext) { |
291 | 11.1k | cnext = conn->next; |
292 | 11.1k | demux_unregister(demux, conn); |
293 | 11.1k | } |
294 | 11.1k | } |
295 | | |
296 | | void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux, |
297 | | ossl_quic_demux_cb_fn *cb, |
298 | | void *cb_arg) |
299 | 10.9k | { |
300 | 10.9k | demux->default_cb = cb; |
301 | 10.9k | demux->default_cb_arg = cb_arg; |
302 | 10.9k | } |
303 | | |
304 | | void ossl_quic_demux_set_stateless_reset_handler( |
305 | | QUIC_DEMUX *demux, |
306 | | ossl_quic_stateless_reset_cb_fn *cb, void *cb_arg) |
307 | 11.1k | { |
308 | 11.1k | demux->reset_token_cb = cb; |
309 | 11.1k | demux->reset_token_cb_arg = cb_arg; |
310 | 11.1k | } |
311 | | |
312 | | static QUIC_URXE *demux_alloc_urxe(size_t alloc_len) |
313 | 716k | { |
314 | 716k | QUIC_URXE *e; |
315 | | |
316 | 716k | if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE)) |
317 | 0 | return NULL; |
318 | | |
319 | 716k | e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len); |
320 | 716k | if (e == NULL) |
321 | 0 | return NULL; |
322 | | |
323 | 716k | ossl_list_urxe_init_elem(e); |
324 | 716k | e->alloc_len = alloc_len; |
325 | 716k | e->data_len = 0; |
326 | 716k | return e; |
327 | 716k | } |
328 | | |
329 | | static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e, |
330 | | size_t new_alloc_len) |
331 | 0 | { |
332 | 0 | QUIC_URXE *e2, *prev; |
333 | |
|
334 | 0 | if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE)) |
335 | | /* Never attempt to resize a URXE which is not on the free list. */ |
336 | 0 | return NULL; |
337 | | |
338 | 0 | prev = ossl_list_urxe_prev(e); |
339 | 0 | ossl_list_urxe_remove(&demux->urx_free, e); |
340 | |
|
341 | 0 | e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len); |
342 | 0 | if (e2 == NULL) { |
343 | | /* Failed to resize, abort. */ |
344 | 0 | if (prev == NULL) |
345 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e); |
346 | 0 | else |
347 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e); |
348 | |
|
349 | 0 | return NULL; |
350 | 0 | } |
351 | | |
352 | 0 | if (prev == NULL) |
353 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e2); |
354 | 0 | else |
355 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e2); |
356 | |
|
357 | 0 | e2->alloc_len = new_alloc_len; |
358 | 0 | return e2; |
359 | 0 | } |
360 | | |
361 | | static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e, |
362 | | size_t alloc_len) |
363 | 930M | { |
364 | 930M | return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e; |
365 | 930M | } |
366 | | |
367 | | static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free) |
368 | 29.0M | { |
369 | 29.0M | QUIC_URXE *e; |
370 | | |
371 | 29.8M | while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) { |
372 | 716k | e = demux_alloc_urxe(demux->mtu); |
373 | 716k | if (e == NULL) |
374 | 0 | return 0; |
375 | | |
376 | 716k | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
377 | 716k | e->demux_state = URXE_DEMUX_STATE_FREE; |
378 | 716k | } |
379 | | |
380 | 29.0M | return 1; |
381 | 29.0M | } |
382 | | |
383 | | /* |
384 | | * Receive datagrams from network, placing them into URXEs. |
385 | | * |
386 | | * Returns 1 on success or 0 on failure. |
387 | | * |
388 | | * Precondition: at least one URXE is free |
389 | | * Precondition: there are no pending URXEs |
390 | | */ |
391 | | static int demux_recv(QUIC_DEMUX *demux) |
392 | 29.0M | { |
393 | 29.0M | BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL]; |
394 | 29.0M | size_t rd, i; |
395 | 29.0M | QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext; |
396 | 29.0M | OSSL_TIME now; |
397 | | |
398 | | /* This should never be called when we have any pending URXE. */ |
399 | 29.0M | assert(ossl_list_urxe_head(&demux->urx_pending) == NULL); |
400 | 29.0M | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
401 | | |
402 | 29.0M | if (demux->net_bio == NULL) |
403 | | /* |
404 | | * If no BIO is plugged in, treat this as no datagram being available. |
405 | | */ |
406 | 0 | return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
407 | | |
408 | | /* |
409 | | * Opportunistically receive as many messages as possible in a single |
410 | | * syscall, determined by how many free URXEs are available. |
411 | | */ |
412 | 959M | for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg); |
413 | 930M | ++i, urxe = ossl_list_urxe_next(urxe)) { |
414 | 930M | if (urxe == NULL) { |
415 | | /* We need at least one URXE to receive into. */ |
416 | 0 | if (!ossl_assert(i > 0)) |
417 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
418 | | |
419 | 0 | break; |
420 | 0 | } |
421 | | |
422 | | /* Ensure the URXE is big enough. */ |
423 | 930M | urxe = demux_reserve_urxe(demux, urxe, demux->mtu); |
424 | 930M | if (urxe == NULL) |
425 | | /* Allocation error, fail. */ |
426 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
427 | | |
428 | | /* Ensure we zero any fields added to BIO_MSG at a later date. */ |
429 | 930M | memset(&msg[i], 0, sizeof(BIO_MSG)); |
430 | 930M | msg[i].data = ossl_quic_urxe_data(urxe); |
431 | 930M | msg[i].data_len = urxe->alloc_len; |
432 | 930M | msg[i].peer = &urxe->peer; |
433 | 930M | BIO_ADDR_clear(&urxe->peer); |
434 | 930M | if (demux->use_local_addr) |
435 | 0 | msg[i].local = &urxe->local; |
436 | 930M | else |
437 | 930M | BIO_ADDR_clear(&urxe->local); |
438 | 930M | } |
439 | | |
440 | 29.0M | ERR_set_mark(); |
441 | 29.0M | if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) { |
442 | 25.8M | if (BIO_err_is_non_fatal(ERR_peek_last_error())) { |
443 | | /* Transient error, clear the error and stop. */ |
444 | 25.8M | ERR_pop_to_mark(); |
445 | 25.8M | return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
446 | 25.8M | } else { |
447 | | /* Non-transient error, do not clear the error. */ |
448 | 0 | ERR_clear_last_mark(); |
449 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
450 | 0 | } |
451 | 25.8M | } |
452 | | |
453 | 3.25M | ERR_clear_last_mark(); |
454 | 3.25M | now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
455 | | |
456 | 3.25M | urxe = ossl_list_urxe_head(&demux->urx_free); |
457 | 8.45M | for (i = 0; i < rd; ++i, urxe = unext) { |
458 | 5.19M | unext = ossl_list_urxe_next(urxe); |
459 | | /* Set URXE with actual length of received datagram. */ |
460 | 5.19M | urxe->data_len = msg[i].data_len; |
461 | | /* Time we received datagram. */ |
462 | 5.19M | urxe->time = now; |
463 | | /* Move from free list to pending list. */ |
464 | 5.19M | ossl_list_urxe_remove(&demux->urx_free, urxe); |
465 | 5.19M | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
466 | 5.19M | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
467 | 5.19M | } |
468 | | |
469 | 3.25M | return QUIC_DEMUX_PUMP_RES_OK; |
470 | 29.0M | } |
471 | | |
472 | | /* Extract destination connection ID from the first packet in a datagram. */ |
473 | | static int demux_identify_conn_id(QUIC_DEMUX *demux, |
474 | | QUIC_URXE *e, |
475 | | QUIC_CONN_ID *dst_conn_id) |
476 | 5.19M | { |
477 | 5.19M | return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e), |
478 | 5.19M | e->data_len, |
479 | 5.19M | demux->short_conn_id_len, |
480 | 5.19M | dst_conn_id); |
481 | 5.19M | } |
482 | | |
483 | | /* Identify the connection structure corresponding to a given URXE. */ |
484 | | static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e) |
485 | 2.58M | { |
486 | 2.58M | QUIC_CONN_ID dst_conn_id; |
487 | | |
488 | 2.58M | if (!demux_identify_conn_id(demux, e, &dst_conn_id)) |
489 | | /* |
490 | | * Datagram is so badly malformed we can't get the DCID from the first |
491 | | * packet in it, so just give up. |
492 | | */ |
493 | 1.73M | return NULL; |
494 | | |
495 | 848k | return demux_get_by_conn_id(demux, &dst_conn_id); |
496 | 2.58M | } |
497 | | |
498 | | /* |
499 | | * Process a single pending URXE. |
500 | | * Returning 1 on success, 0 on failure and -1 on stateless reset. |
501 | | */ |
502 | | static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e) |
503 | 2.58M | { |
504 | 2.58M | QUIC_DEMUX_CONN *conn; |
505 | 2.58M | int r; |
506 | | |
507 | | /* The next URXE we process should be at the head of the pending list. */ |
508 | 2.58M | if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending))) |
509 | 0 | return 0; |
510 | | |
511 | 2.58M | assert(e->demux_state == URXE_DEMUX_STATE_PENDING); |
512 | | |
513 | | /* |
514 | | * Check if the packet ends with a stateless reset token and if it does |
515 | | * skip it after dropping the connection. |
516 | | * |
517 | | * RFC 9000 s. 10.3.1 Detecting a Stateless Reset |
518 | | * If the last 16 bytes of the datagram are identical in value to |
519 | | * a stateless reset token, the endpoint MUST enter the draining |
520 | | * period and not send any further packets on this connection. |
521 | | * |
522 | | * Returning a failure here causes the connection to enter the terminating |
523 | | * state which achieves the desired outcome. |
524 | | * |
525 | | * TODO(QUIC FUTURE): only try to match unparsable packets |
526 | | */ |
527 | 2.58M | if (demux->reset_token_cb != NULL) { |
528 | 2.58M | r = demux->reset_token_cb(ossl_quic_urxe_data(e), e->data_len, |
529 | 2.58M | demux->reset_token_cb_arg); |
530 | 2.58M | if (r > 0) /* Received a stateless reset */ |
531 | 1 | return -1; |
532 | 2.58M | if (r < 0) /* Error during stateless reset detection */ |
533 | 0 | return 0; |
534 | 2.58M | } |
535 | | |
536 | 2.58M | conn = demux_identify_conn(demux, e); |
537 | 2.58M | if (conn == NULL) { |
538 | | /* |
539 | | * We could not identify a connection. If we have a default packet |
540 | | * handler, pass it to the handler. Otherwise, we will never be able to |
541 | | * process this datagram, so get rid of it. |
542 | | */ |
543 | 1.73M | ossl_list_urxe_remove(&demux->urx_pending, e); |
544 | 1.73M | if (demux->default_cb != NULL) { |
545 | | /* Pass to default handler. */ |
546 | 0 | e->demux_state = URXE_DEMUX_STATE_ISSUED; |
547 | 0 | demux->default_cb(e, demux->default_cb_arg); |
548 | 1.73M | } else { |
549 | | /* Discard. */ |
550 | 1.73M | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
551 | 1.73M | e->demux_state = URXE_DEMUX_STATE_FREE; |
552 | 1.73M | } |
553 | 1.73M | return 1; /* keep processing pending URXEs */ |
554 | 1.73M | } |
555 | | |
556 | | /* |
557 | | * Remove from list and invoke callback. The URXE now belongs to the |
558 | | * callback. (QUIC_DEMUX_CONN never has non-NULL cb.) |
559 | | */ |
560 | 845k | ossl_list_urxe_remove(&demux->urx_pending, e); |
561 | 845k | e->demux_state = URXE_DEMUX_STATE_ISSUED; |
562 | 845k | conn->cb(e, conn->cb_arg); |
563 | 845k | return 1; |
564 | 2.58M | } |
565 | | |
566 | | /* Process pending URXEs to generate callbacks. */ |
567 | | static int demux_process_pending_urxl(QUIC_DEMUX *demux) |
568 | 3.25M | { |
569 | 3.25M | QUIC_URXE *e; |
570 | 3.25M | int ret; |
571 | | |
572 | 8.45M | while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL) |
573 | 5.19M | if ((ret = demux_process_pending_urxe(demux, e)) <= 0) |
574 | 1 | return ret; |
575 | | |
576 | 3.25M | return 1; |
577 | 3.25M | } |
578 | | |
579 | | /* |
580 | | * Drain the pending URXE list, processing any pending URXEs by making their |
581 | | * callbacks. If no URXEs are pending, a network read is attempted first. |
582 | | */ |
583 | | int ossl_quic_demux_pump(QUIC_DEMUX *demux) |
584 | 17.7M | { |
585 | 17.7M | int ret; |
586 | | |
587 | 17.7M | if (ossl_list_urxe_head(&demux->urx_pending) == NULL) { |
588 | 17.7M | ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL); |
589 | 17.7M | if (ret != 1) |
590 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
591 | | |
592 | 17.7M | ret = demux_recv(demux); |
593 | 17.7M | if (ret != QUIC_DEMUX_PUMP_RES_OK) |
594 | 16.3M | return ret; |
595 | | |
596 | | /* |
597 | | * If demux_recv returned successfully, we should always have something. |
598 | | */ |
599 | 1.39M | assert(ossl_list_urxe_head(&demux->urx_pending) != NULL); |
600 | 1.39M | } |
601 | | |
602 | 1.39M | if ((ret = demux_process_pending_urxl(demux)) <= 0) |
603 | 1 | return ret == 0 ? QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL |
604 | 1 | : QUIC_DEMUX_PUMP_RES_STATELESS_RESET; |
605 | | |
606 | 1.39M | return QUIC_DEMUX_PUMP_RES_OK; |
607 | 1.39M | } |
608 | | |
609 | | /* Artificially inject a packet into the demuxer for testing purposes. */ |
610 | | int ossl_quic_demux_inject(QUIC_DEMUX *demux, |
611 | | const unsigned char *buf, |
612 | | size_t buf_len, |
613 | | const BIO_ADDR *peer, |
614 | | const BIO_ADDR *local) |
615 | 0 | { |
616 | 0 | int ret; |
617 | 0 | QUIC_URXE *urxe; |
618 | |
|
619 | 0 | ret = demux_ensure_free_urxe(demux, 1); |
620 | 0 | if (ret != 1) |
621 | 0 | return 0; |
622 | | |
623 | 0 | urxe = ossl_list_urxe_head(&demux->urx_free); |
624 | |
|
625 | 0 | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
626 | | |
627 | 0 | urxe = demux_reserve_urxe(demux, urxe, buf_len); |
628 | 0 | if (urxe == NULL) |
629 | 0 | return 0; |
630 | | |
631 | 0 | memcpy(ossl_quic_urxe_data(urxe), buf, buf_len); |
632 | 0 | urxe->data_len = buf_len; |
633 | |
|
634 | 0 | if (peer != NULL) |
635 | 0 | urxe->peer = *peer; |
636 | 0 | else |
637 | 0 | BIO_ADDR_clear(&urxe->peer); |
638 | |
|
639 | 0 | if (local != NULL) |
640 | 0 | urxe->local = *local; |
641 | 0 | else |
642 | 0 | BIO_ADDR_clear(&urxe->local); |
643 | |
|
644 | 0 | urxe->time |
645 | 0 | = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
646 | | |
647 | | /* Move from free list to pending list. */ |
648 | 0 | ossl_list_urxe_remove(&demux->urx_free, urxe); |
649 | 0 | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
650 | 0 | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
651 | |
|
652 | 0 | return demux_process_pending_urxl(demux) > 0; |
653 | 0 | } |
654 | | |
655 | | /* Called by our user to return a URXE to the free list. */ |
656 | | void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux, |
657 | | QUIC_URXE *e) |
658 | 3.46M | { |
659 | 3.46M | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
660 | 3.46M | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
661 | 3.46M | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
662 | 3.46M | e->demux_state = URXE_DEMUX_STATE_FREE; |
663 | 3.46M | } |
664 | | |
665 | | void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux, |
666 | | QUIC_URXE *e) |
667 | 0 | { |
668 | 0 | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
669 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
670 | 0 | ossl_list_urxe_insert_head(&demux->urx_pending, e); |
671 | 0 | e->demux_state = URXE_DEMUX_STATE_PENDING; |
672 | 0 | } |
673 | | |
674 | | int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux) |
675 | 0 | { |
676 | 0 | return ossl_list_urxe_head(&demux->urx_pending) != NULL; |
677 | 0 | } |