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