Coverage Report

Created: 2025-08-11 07:04

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