/src/openssl/ssl/quic/quic_demux.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022-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_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 | 0 | #define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */ |
17 | 0 | #define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */ |
18 | 0 | #define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */ |
19 | | |
20 | 0 | #define DEMUX_MAX_MSGS_PER_CALL 32 |
21 | | |
22 | 0 | #define DEMUX_DEFAULT_MTU 1500 |
23 | | |
24 | | struct quic_demux_st { |
25 | | /* The underlying transport BIO with datagram semantics. */ |
26 | | BIO *net_bio; |
27 | | |
28 | | /* |
29 | | * QUIC short packets do not contain the length of the connection ID field, |
30 | | * therefore it must be known contextually. The demuxer requires connection |
31 | | * IDs of the same length to be used for all incoming packets. |
32 | | */ |
33 | | size_t short_conn_id_len; |
34 | | |
35 | | /* |
36 | | * Our current understanding of the upper bound on an incoming datagram size |
37 | | * in bytes. |
38 | | */ |
39 | | size_t mtu; |
40 | | |
41 | | /* The datagram_id to use for the next datagram we receive. */ |
42 | | uint64_t next_datagram_id; |
43 | | |
44 | | /* Time retrieval callback. */ |
45 | | OSSL_TIME (*now)(void *arg); |
46 | | void *now_arg; |
47 | | |
48 | | /* The default packet handler, if any. */ |
49 | | ossl_quic_demux_cb_fn *default_cb; |
50 | | void *default_cb_arg; |
51 | | |
52 | | /* |
53 | | * List of URXEs which are not currently in use (i.e., not filled with |
54 | | * unconsumed data). These are moved to the pending list as they are filled. |
55 | | */ |
56 | | QUIC_URXE_LIST urx_free; |
57 | | |
58 | | /* |
59 | | * List of URXEs which are filled with received encrypted data. These are |
60 | | * removed from this list as we invoke the callbacks for each of them. They |
61 | | * are then not on any list managed by us; we forget about them until our |
62 | | * user calls ossl_quic_demux_release_urxe to return the URXE to us, at |
63 | | * which point we add it to the free list. |
64 | | */ |
65 | | QUIC_URXE_LIST urx_pending; |
66 | | |
67 | | /* Whether to use local address support. */ |
68 | | char use_local_addr; |
69 | | }; |
70 | | |
71 | | QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio, |
72 | | size_t short_conn_id_len, |
73 | | OSSL_TIME (*now)(void *arg), |
74 | | void *now_arg) |
75 | 0 | { |
76 | 0 | QUIC_DEMUX *demux; |
77 | |
|
78 | 0 | demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX)); |
79 | 0 | if (demux == NULL) |
80 | 0 | return NULL; |
81 | | |
82 | 0 | demux->net_bio = net_bio; |
83 | 0 | demux->short_conn_id_len = short_conn_id_len; |
84 | | /* We update this if possible when we get a BIO. */ |
85 | 0 | demux->mtu = DEMUX_DEFAULT_MTU; |
86 | 0 | demux->now = now; |
87 | 0 | demux->now_arg = now_arg; |
88 | |
|
89 | 0 | if (net_bio != NULL |
90 | 0 | && BIO_dgram_get_local_addr_cap(net_bio) |
91 | 0 | && BIO_dgram_set_local_addr_enable(net_bio, 1)) |
92 | 0 | demux->use_local_addr = 1; |
93 | |
|
94 | 0 | return demux; |
95 | 0 | } |
96 | | |
97 | | static void demux_free_urxl(QUIC_URXE_LIST *l) |
98 | 0 | { |
99 | 0 | QUIC_URXE *e, *enext; |
100 | |
|
101 | 0 | for (e = ossl_list_urxe_head(l); e != NULL; e = enext) { |
102 | 0 | enext = ossl_list_urxe_next(e); |
103 | 0 | ossl_list_urxe_remove(l, e); |
104 | 0 | OPENSSL_free(e); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | void ossl_quic_demux_free(QUIC_DEMUX *demux) |
109 | 0 | { |
110 | 0 | if (demux == NULL) |
111 | 0 | return; |
112 | | |
113 | | /* Free all URXEs we are holding. */ |
114 | 0 | demux_free_urxl(&demux->urx_free); |
115 | 0 | demux_free_urxl(&demux->urx_pending); |
116 | |
|
117 | 0 | OPENSSL_free(demux); |
118 | 0 | } |
119 | | |
120 | | void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio) |
121 | 0 | { |
122 | 0 | unsigned int mtu; |
123 | |
|
124 | 0 | demux->net_bio = net_bio; |
125 | |
|
126 | 0 | if (net_bio != NULL) { |
127 | | /* |
128 | | * Try to determine our MTU if possible. The BIO is not required to |
129 | | * support this, in which case we remain at the last known MTU, or our |
130 | | * initial default. |
131 | | */ |
132 | 0 | mtu = BIO_dgram_get_mtu(net_bio); |
133 | 0 | if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN) |
134 | 0 | ossl_quic_demux_set_mtu(demux, mtu); /* best effort */ |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | | int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu) |
139 | 0 | { |
140 | 0 | if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN) |
141 | 0 | return 0; |
142 | | |
143 | 0 | demux->mtu = mtu; |
144 | 0 | return 1; |
145 | 0 | } |
146 | | |
147 | | void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux, |
148 | | ossl_quic_demux_cb_fn *cb, |
149 | | void *cb_arg) |
150 | 0 | { |
151 | 0 | demux->default_cb = cb; |
152 | 0 | demux->default_cb_arg = cb_arg; |
153 | 0 | } |
154 | | |
155 | | static QUIC_URXE *demux_alloc_urxe(size_t alloc_len) |
156 | 0 | { |
157 | 0 | QUIC_URXE *e; |
158 | |
|
159 | 0 | if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE)) |
160 | 0 | return NULL; |
161 | | |
162 | 0 | e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len); |
163 | 0 | if (e == NULL) |
164 | 0 | return NULL; |
165 | | |
166 | 0 | ossl_list_urxe_init_elem(e); |
167 | 0 | e->alloc_len = alloc_len; |
168 | 0 | e->data_len = 0; |
169 | 0 | return e; |
170 | 0 | } |
171 | | |
172 | | static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e, |
173 | | size_t new_alloc_len) |
174 | 0 | { |
175 | 0 | QUIC_URXE *e2, *prev; |
176 | |
|
177 | 0 | if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE)) |
178 | | /* Never attempt to resize a URXE which is not on the free list. */ |
179 | 0 | return NULL; |
180 | | |
181 | 0 | prev = ossl_list_urxe_prev(e); |
182 | 0 | ossl_list_urxe_remove(&demux->urx_free, e); |
183 | |
|
184 | 0 | if (new_alloc_len >= SIZE_MAX - sizeof(QUIC_URXE)) |
185 | 0 | return NULL; |
186 | | |
187 | 0 | e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len); |
188 | 0 | if (e2 == NULL) { |
189 | | /* Failed to resize, abort. */ |
190 | 0 | if (prev == NULL) |
191 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e); |
192 | 0 | else |
193 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e); |
194 | |
|
195 | 0 | return NULL; |
196 | 0 | } |
197 | | |
198 | 0 | if (prev == NULL) |
199 | 0 | ossl_list_urxe_insert_head(&demux->urx_free, e2); |
200 | 0 | else |
201 | 0 | ossl_list_urxe_insert_after(&demux->urx_free, prev, e2); |
202 | |
|
203 | 0 | e2->alloc_len = new_alloc_len; |
204 | 0 | return e2; |
205 | 0 | } |
206 | | |
207 | | static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e, |
208 | | size_t alloc_len) |
209 | 0 | { |
210 | 0 | return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e; |
211 | 0 | } |
212 | | |
213 | | static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free) |
214 | 0 | { |
215 | 0 | QUIC_URXE *e; |
216 | |
|
217 | 0 | while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) { |
218 | 0 | e = demux_alloc_urxe(demux->mtu); |
219 | 0 | if (e == NULL) |
220 | 0 | return 0; |
221 | | |
222 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
223 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
224 | 0 | } |
225 | | |
226 | 0 | return 1; |
227 | 0 | } |
228 | | |
229 | | /* |
230 | | * Receive datagrams from network, placing them into URXEs. |
231 | | * |
232 | | * Returns 1 on success or 0 on failure. |
233 | | * |
234 | | * Precondition: at least one URXE is free |
235 | | * Precondition: there are no pending URXEs |
236 | | */ |
237 | | static int demux_recv(QUIC_DEMUX *demux) |
238 | 0 | { |
239 | 0 | BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL]; |
240 | 0 | size_t rd, i; |
241 | 0 | QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext; |
242 | 0 | OSSL_TIME now; |
243 | | |
244 | | /* This should never be called when we have any pending URXE. */ |
245 | 0 | assert(ossl_list_urxe_head(&demux->urx_pending) == NULL); |
246 | 0 | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
247 | |
|
248 | 0 | if (demux->net_bio == NULL) |
249 | | /* |
250 | | * If no BIO is plugged in, treat this as no datagram being available. |
251 | | */ |
252 | 0 | return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
253 | | |
254 | | /* |
255 | | * Opportunistically receive as many messages as possible in a single |
256 | | * syscall, determined by how many free URXEs are available. |
257 | | */ |
258 | 0 | for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg); |
259 | 0 | ++i, urxe = ossl_list_urxe_next(urxe)) { |
260 | 0 | if (urxe == NULL) { |
261 | | /* We need at least one URXE to receive into. */ |
262 | 0 | if (!ossl_assert(i > 0)) |
263 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
264 | | |
265 | 0 | break; |
266 | 0 | } |
267 | | |
268 | | /* Ensure the URXE is big enough. */ |
269 | 0 | urxe = demux_reserve_urxe(demux, urxe, demux->mtu); |
270 | 0 | if (urxe == NULL) |
271 | | /* Allocation error, fail. */ |
272 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
273 | | |
274 | | /* Ensure we zero any fields added to BIO_MSG at a later date. */ |
275 | 0 | memset(&msg[i], 0, sizeof(BIO_MSG)); |
276 | 0 | msg[i].data = ossl_quic_urxe_data(urxe); |
277 | 0 | msg[i].data_len = urxe->alloc_len; |
278 | 0 | msg[i].peer = &urxe->peer; |
279 | 0 | BIO_ADDR_clear(&urxe->peer); |
280 | 0 | if (demux->use_local_addr) |
281 | 0 | msg[i].local = &urxe->local; |
282 | 0 | else |
283 | 0 | BIO_ADDR_clear(&urxe->local); |
284 | 0 | } |
285 | | |
286 | 0 | ERR_set_mark(); |
287 | 0 | if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) { |
288 | 0 | if (BIO_err_is_non_fatal(ERR_peek_last_error())) { |
289 | | /* Transient error, clear the error and stop. */ |
290 | 0 | ERR_pop_to_mark(); |
291 | 0 | return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL; |
292 | 0 | } else { |
293 | | /* Non-transient error, do not clear the error. */ |
294 | 0 | ERR_clear_last_mark(); |
295 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | ERR_clear_last_mark(); |
300 | 0 | now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
301 | |
|
302 | 0 | urxe = ossl_list_urxe_head(&demux->urx_free); |
303 | 0 | for (i = 0; i < rd; ++i, urxe = unext) { |
304 | 0 | unext = ossl_list_urxe_next(urxe); |
305 | | /* Set URXE with actual length of received datagram. */ |
306 | 0 | urxe->data_len = msg[i].data_len; |
307 | | /* Time we received datagram. */ |
308 | 0 | urxe->time = now; |
309 | 0 | urxe->datagram_id = demux->next_datagram_id++; |
310 | | /* Move from free list to pending list. */ |
311 | 0 | ossl_list_urxe_remove(&demux->urx_free, urxe); |
312 | 0 | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
313 | 0 | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
314 | 0 | } |
315 | |
|
316 | 0 | return QUIC_DEMUX_PUMP_RES_OK; |
317 | 0 | } |
318 | | |
319 | | /* Extract destination connection ID from the first packet in a datagram. */ |
320 | | static int demux_identify_conn_id(QUIC_DEMUX *demux, |
321 | | QUIC_URXE *e, |
322 | | QUIC_CONN_ID *dst_conn_id) |
323 | 0 | { |
324 | 0 | return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e), |
325 | 0 | e->data_len, |
326 | 0 | demux->short_conn_id_len, |
327 | 0 | dst_conn_id); |
328 | 0 | } |
329 | | |
330 | | /* |
331 | | * Process a single pending URXE. |
332 | | * Returning 1 on success, 0 on failure. |
333 | | */ |
334 | | static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e) |
335 | 0 | { |
336 | 0 | QUIC_CONN_ID dst_conn_id; |
337 | 0 | int dst_conn_id_ok = 0; |
338 | | |
339 | | /* The next URXE we process should be at the head of the pending list. */ |
340 | 0 | if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending))) |
341 | 0 | return 0; |
342 | | |
343 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_PENDING); |
344 | | |
345 | | /* Determine the DCID of the first packet in the datagram. */ |
346 | 0 | dst_conn_id_ok = demux_identify_conn_id(demux, e, &dst_conn_id); |
347 | |
|
348 | 0 | ossl_list_urxe_remove(&demux->urx_pending, e); |
349 | 0 | if (demux->default_cb != NULL) { |
350 | | /* |
351 | | * Pass to default handler for routing. The URXE now belongs to the |
352 | | * callback. |
353 | | */ |
354 | 0 | e->demux_state = URXE_DEMUX_STATE_ISSUED; |
355 | 0 | demux->default_cb(e, demux->default_cb_arg, |
356 | 0 | dst_conn_id_ok ? &dst_conn_id : NULL); |
357 | 0 | } else { |
358 | | /* Discard. */ |
359 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
360 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
361 | 0 | } |
362 | |
|
363 | 0 | return 1; /* keep processing pending URXEs */ |
364 | 0 | } |
365 | | |
366 | | /* Process pending URXEs to generate callbacks. */ |
367 | | static int demux_process_pending_urxl(QUIC_DEMUX *demux) |
368 | 0 | { |
369 | 0 | QUIC_URXE *e; |
370 | 0 | int ret; |
371 | |
|
372 | 0 | while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL) |
373 | 0 | if ((ret = demux_process_pending_urxe(demux, e)) <= 0) |
374 | 0 | return ret; |
375 | | |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | | /* |
380 | | * Drain the pending URXE list, processing any pending URXEs by making their |
381 | | * callbacks. If no URXEs are pending, a network read is attempted first. |
382 | | */ |
383 | | int ossl_quic_demux_pump(QUIC_DEMUX *demux) |
384 | 0 | { |
385 | 0 | int ret; |
386 | |
|
387 | 0 | if (ossl_list_urxe_head(&demux->urx_pending) == NULL) { |
388 | 0 | ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL); |
389 | 0 | if (ret != 1) |
390 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
391 | | |
392 | 0 | ret = demux_recv(demux); |
393 | 0 | if (ret != QUIC_DEMUX_PUMP_RES_OK) |
394 | 0 | return ret; |
395 | | |
396 | | /* |
397 | | * If demux_recv returned successfully, we should always have something. |
398 | | */ |
399 | 0 | assert(ossl_list_urxe_head(&demux->urx_pending) != NULL); |
400 | 0 | } |
401 | | |
402 | 0 | if ((ret = demux_process_pending_urxl(demux)) <= 0) |
403 | 0 | return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL; |
404 | | |
405 | 0 | return QUIC_DEMUX_PUMP_RES_OK; |
406 | 0 | } |
407 | | |
408 | | /* Artificially inject a packet into the demuxer for testing purposes. */ |
409 | | int ossl_quic_demux_inject(QUIC_DEMUX *demux, |
410 | | const unsigned char *buf, |
411 | | size_t buf_len, |
412 | | const BIO_ADDR *peer, |
413 | | const BIO_ADDR *local) |
414 | 0 | { |
415 | 0 | int ret; |
416 | 0 | QUIC_URXE *urxe; |
417 | |
|
418 | 0 | ret = demux_ensure_free_urxe(demux, 1); |
419 | 0 | if (ret != 1) |
420 | 0 | return 0; |
421 | | |
422 | 0 | urxe = ossl_list_urxe_head(&demux->urx_free); |
423 | |
|
424 | 0 | assert(urxe->demux_state == URXE_DEMUX_STATE_FREE); |
425 | |
|
426 | 0 | urxe = demux_reserve_urxe(demux, urxe, buf_len); |
427 | 0 | if (urxe == NULL) |
428 | 0 | return 0; |
429 | | |
430 | 0 | memcpy(ossl_quic_urxe_data(urxe), buf, buf_len); |
431 | 0 | urxe->data_len = buf_len; |
432 | |
|
433 | 0 | if (peer != NULL) |
434 | 0 | urxe->peer = *peer; |
435 | 0 | else |
436 | 0 | BIO_ADDR_clear(&urxe->peer); |
437 | |
|
438 | 0 | if (local != NULL) |
439 | 0 | urxe->local = *local; |
440 | 0 | else |
441 | 0 | BIO_ADDR_clear(&urxe->local); |
442 | |
|
443 | 0 | urxe->time |
444 | 0 | = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero(); |
445 | | |
446 | | /* Move from free list to pending list. */ |
447 | 0 | ossl_list_urxe_remove(&demux->urx_free, urxe); |
448 | 0 | urxe->datagram_id = demux->next_datagram_id++; |
449 | 0 | ossl_list_urxe_insert_tail(&demux->urx_pending, urxe); |
450 | 0 | urxe->demux_state = URXE_DEMUX_STATE_PENDING; |
451 | |
|
452 | 0 | return demux_process_pending_urxl(demux) > 0; |
453 | 0 | } |
454 | | |
455 | | /* Called by our user to return a URXE to the free list. */ |
456 | | void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux, |
457 | | QUIC_URXE *e) |
458 | 0 | { |
459 | 0 | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
460 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
461 | 0 | ossl_list_urxe_insert_tail(&demux->urx_free, e); |
462 | 0 | e->demux_state = URXE_DEMUX_STATE_FREE; |
463 | 0 | } |
464 | | |
465 | | void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux, |
466 | | QUIC_URXE *e) |
467 | 0 | { |
468 | 0 | assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL); |
469 | 0 | assert(e->demux_state == URXE_DEMUX_STATE_ISSUED); |
470 | 0 | ossl_list_urxe_insert_head(&demux->urx_pending, e); |
471 | 0 | e->demux_state = URXE_DEMUX_STATE_PENDING; |
472 | 0 | } |
473 | | |
474 | | int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux) |
475 | 0 | { |
476 | | return ossl_list_urxe_head(&demux->urx_pending) != NULL; |
477 | 0 | } |