Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl32/ssl/quic/quic_demux.c
Line
Count
Source
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
13.5M
#define URXE_DEMUX_STATE_FREE       0 /* on urx_free list */
17
11.8M
#define URXE_DEMUX_STATE_PENDING    1 /* on urx_pending list */
18
744k
#define URXE_DEMUX_STATE_ISSUED     2 /* on neither list */
19
20
19.2M
#define DEMUX_MAX_MSGS_PER_CALL    32
21
22
10.6k
#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
779k
{
38
779k
    size_t i;
39
779k
    unsigned long v = 0;
40
41
779k
    assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
42
43
798k
    for (i = 0; i < conn->dst_conn_id.id_len; ++i)
44
19.3k
        v ^= ((unsigned long)conn->dst_conn_id.id[i])
45
19.3k
             << ((i * 8) % (sizeof(unsigned long) * 8));
46
47
779k
    return v;
48
779k
}
49
50
static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
51
755k
{
52
755k
    return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
53
755k
}
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
10.6k
{
111
10.6k
    QUIC_DEMUX *demux;
112
113
10.6k
    demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
114
10.6k
    if (demux == NULL)
115
0
        return NULL;
116
117
10.6k
    demux->net_bio                  = net_bio;
118
10.6k
    demux->short_conn_id_len        = short_conn_id_len;
119
    /* We update this if possible when we get a BIO. */
120
10.6k
    demux->mtu                      = DEMUX_DEFAULT_MTU;
121
10.6k
    demux->now                      = now;
122
10.6k
    demux->now_arg                  = now_arg;
123
124
10.6k
    demux->conns_by_id
125
10.6k
        = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
126
10.6k
    if (demux->conns_by_id == NULL) {
127
0
        OPENSSL_free(demux);
128
0
        return NULL;
129
0
    }
130
131
10.6k
    if (net_bio != NULL
132
0
        && BIO_dgram_get_local_addr_cap(net_bio)
133
0
        && BIO_dgram_set_local_addr_enable(net_bio, 1))
134
0
        demux->use_local_addr = 1;
135
136
10.6k
    return demux;
137
10.6k
}
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
110k
{
146
110k
    QUIC_URXE *e, *enext;
147
148
1.88M
    for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
149
1.77M
        enext = ossl_list_urxe_next(e);
150
1.77M
        ossl_list_urxe_remove(l, e);
151
1.77M
        OPENSSL_free(e);
152
1.77M
    }
153
110k
}
154
155
void ossl_quic_demux_free(QUIC_DEMUX *demux)
156
55.0k
{
157
55.0k
    if (demux == NULL)
158
0
        return;
159
160
    /* Free all connection structures. */
161
55.0k
    lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
162
55.0k
    lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
163
164
    /* Free all URXEs we are holding. */
165
55.0k
    demux_free_urxl(&demux->urx_free);
166
55.0k
    demux_free_urxl(&demux->urx_pending);
167
168
55.0k
    OPENSSL_free(demux);
169
55.0k
}
170
171
void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)
172
55.0k
{
173
55.0k
    unsigned int mtu;
174
175
55.0k
    demux->net_bio = net_bio;
176
177
55.0k
    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
55.0k
        mtu = BIO_dgram_get_mtu(net_bio);
184
55.0k
        if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)
185
55.0k
            ossl_quic_demux_set_mtu(demux, mtu); /* best effort */
186
55.0k
    }
187
55.0k
}
188
189
int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)
190
55.0k
{
191
55.0k
    if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)
192
0
        return 0;
193
194
55.0k
    demux->mtu = mtu;
195
55.0k
    return 1;
196
55.0k
}
197
198
static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
199
                                             const QUIC_CONN_ID *dst_conn_id)
200
758k
{
201
758k
    QUIC_DEMUX_CONN key;
202
203
758k
    if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
204
0
        return NULL;
205
206
758k
    key.dst_conn_id = *dst_conn_id;
207
758k
    return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
208
758k
}
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
10.6k
{
214
10.6k
    QUIC_DEMUX_CONN *conn;
215
216
10.6k
    if (dst_conn_id == NULL
217
10.6k
        || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
218
10.6k
        || cb == NULL)
219
0
        return 0;
220
221
    /* Ensure not already registered. */
222
10.6k
    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
10.6k
    conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
227
10.6k
    if (conn == NULL)
228
0
        return 0;
229
230
10.6k
    conn->dst_conn_id   = *dst_conn_id;
231
10.6k
    conn->cb            = cb;
232
10.6k
    conn->cb_arg        = cb_arg;
233
234
10.6k
    lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
235
10.6k
    return 1;
236
10.6k
}
237
238
static void demux_unregister(QUIC_DEMUX *demux,
239
                             QUIC_DEMUX_CONN *conn)
240
10.6k
{
241
10.6k
    lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
242
10.6k
    OPENSSL_free(conn);
243
10.6k
}
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
10.6k
{
270
10.6k
    struct unreg_arg *arg = arg_;
271
272
10.6k
    if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
273
10.6k
        conn->next = arg->head;
274
10.6k
        arg->head = conn;
275
10.6k
    }
276
10.6k
}
277
278
void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
279
                                      ossl_quic_demux_cb_fn *cb,
280
                                      void *cb_arg)
281
10.6k
{
282
10.6k
    QUIC_DEMUX_CONN *conn, *cnext;
283
10.6k
    struct unreg_arg arg = {0};
284
10.6k
    arg.cb      = cb;
285
10.6k
    arg.cb_arg  = cb_arg;
286
287
10.6k
    lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
288
10.6k
                                 demux_unregister_by_cb, &arg);
289
290
21.3k
    for (conn = arg.head; conn != NULL; conn = cnext) {
291
10.6k
        cnext = conn->next;
292
10.6k
        demux_unregister(demux, conn);
293
10.6k
    }
294
10.6k
}
295
296
void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
297
                                         ossl_quic_demux_cb_fn *cb,
298
                                         void *cb_arg)
299
44.4k
{
300
44.4k
    demux->default_cb       = cb;
301
44.4k
    demux->default_cb_arg   = cb_arg;
302
44.4k
}
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
10.6k
{
308
10.6k
    demux->reset_token_cb       = cb;
309
10.6k
    demux->reset_token_cb_arg   = cb_arg;
310
10.6k
}
311
312
static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
313
1.77M
{
314
1.77M
    QUIC_URXE *e;
315
316
1.77M
    if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
317
0
        return NULL;
318
319
1.77M
    e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
320
1.77M
    if (e == NULL)
321
0
        return NULL;
322
323
1.77M
    ossl_list_urxe_init_elem(e);
324
1.77M
    e->alloc_len   = alloc_len;
325
1.77M
    e->data_len    = 0;
326
1.77M
    return e;
327
1.77M
}
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
    if (new_alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
342
0
        return NULL;
343
344
0
    e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);
345
0
    if (e2 == NULL) {
346
        /* Failed to resize, abort. */
347
0
        if (prev == NULL)
348
0
            ossl_list_urxe_insert_head(&demux->urx_free, e);
349
0
        else
350
0
            ossl_list_urxe_insert_after(&demux->urx_free, prev, e);
351
352
0
        return NULL;
353
0
    }
354
355
0
    if (prev == NULL)
356
0
        ossl_list_urxe_insert_head(&demux->urx_free, e2);
357
0
    else
358
0
        ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);
359
360
0
    e2->alloc_len = new_alloc_len;
361
0
    return e2;
362
0
}
363
364
static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
365
                                     size_t alloc_len)
366
2.36G
{
367
2.36G
    return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;
368
2.36G
}
369
370
static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
371
73.9M
{
372
73.9M
    QUIC_URXE *e;
373
374
75.6M
    while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
375
1.77M
        e = demux_alloc_urxe(demux->mtu);
376
1.77M
        if (e == NULL)
377
0
            return 0;
378
379
1.77M
        ossl_list_urxe_insert_tail(&demux->urx_free, e);
380
1.77M
        e->demux_state = URXE_DEMUX_STATE_FREE;
381
1.77M
    }
382
383
73.9M
    return 1;
384
73.9M
}
385
386
/*
387
 * Receive datagrams from network, placing them into URXEs.
388
 *
389
 * Returns 1 on success or 0 on failure.
390
 *
391
 * Precondition: at least one URXE is free
392
 * Precondition: there are no pending URXEs
393
 */
394
static int demux_recv(QUIC_DEMUX *demux)
395
73.9M
{
396
73.9M
    BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
397
73.9M
    size_t rd, i;
398
73.9M
    QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
399
73.9M
    OSSL_TIME now;
400
401
    /* This should never be called when we have any pending URXE. */
402
73.9M
    assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
403
73.9M
    assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
404
405
73.9M
    if (demux->net_bio == NULL)
406
        /*
407
         * If no BIO is plugged in, treat this as no datagram being available.
408
         */
409
0
        return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
410
411
    /*
412
     * Opportunistically receive as many messages as possible in a single
413
     * syscall, determined by how many free URXEs are available.
414
     */
415
2.43G
    for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
416
2.36G
            ++i, urxe = ossl_list_urxe_next(urxe)) {
417
2.36G
        if (urxe == NULL) {
418
            /* We need at least one URXE to receive into. */
419
0
            if (!ossl_assert(i > 0))
420
0
                return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
421
422
0
            break;
423
0
        }
424
425
        /* Ensure the URXE is big enough. */
426
2.36G
        urxe = demux_reserve_urxe(demux, urxe, demux->mtu);
427
2.36G
        if (urxe == NULL)
428
            /* Allocation error, fail. */
429
0
            return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
430
431
        /* Ensure we zero any fields added to BIO_MSG at a later date. */
432
2.36G
        memset(&msg[i], 0, sizeof(BIO_MSG));
433
2.36G
        msg[i].data     = ossl_quic_urxe_data(urxe);
434
2.36G
        msg[i].data_len = urxe->alloc_len;
435
2.36G
        msg[i].peer     = &urxe->peer;
436
2.36G
        BIO_ADDR_clear(&urxe->peer);
437
2.36G
        if (demux->use_local_addr)
438
0
            msg[i].local = &urxe->local;
439
2.36G
        else
440
2.36G
            BIO_ADDR_clear(&urxe->local);
441
2.36G
    }
442
443
73.9M
    ERR_set_mark();
444
73.9M
    if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {
445
65.1M
        if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
446
            /* Transient error, clear the error and stop. */
447
65.1M
            ERR_pop_to_mark();
448
65.1M
            return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
449
65.1M
        } else {
450
            /* Non-transient error, do not clear the error. */
451
0
            ERR_clear_last_mark();
452
0
            return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
453
0
        }
454
65.1M
    }
455
456
8.76M
    ERR_clear_last_mark();
457
8.76M
    now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
458
459
8.76M
    urxe = ossl_list_urxe_head(&demux->urx_free);
460
20.5M
    for (i = 0; i < rd; ++i, urxe = unext) {
461
11.8M
        unext = ossl_list_urxe_next(urxe);
462
        /* Set URXE with actual length of received datagram. */
463
11.8M
        urxe->data_len      = msg[i].data_len;
464
        /* Time we received datagram. */
465
11.8M
        urxe->time          = now;
466
        /* Move from free list to pending list. */
467
11.8M
        ossl_list_urxe_remove(&demux->urx_free, urxe);
468
11.8M
        ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
469
11.8M
        urxe->demux_state = URXE_DEMUX_STATE_PENDING;
470
11.8M
    }
471
472
8.76M
    return QUIC_DEMUX_PUMP_RES_OK;
473
73.9M
}
474
475
/* Extract destination connection ID from the first packet in a datagram. */
476
static int demux_identify_conn_id(QUIC_DEMUX *demux,
477
                                  QUIC_URXE *e,
478
                                  QUIC_CONN_ID *dst_conn_id)
479
11.8M
{
480
11.8M
    return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
481
11.8M
                                                  e->data_len,
482
11.8M
                                                  demux->short_conn_id_len,
483
11.8M
                                                  dst_conn_id);
484
11.8M
}
485
486
/* Identify the connection structure corresponding to a given URXE. */
487
static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e)
488
2.65M
{
489
2.65M
    QUIC_CONN_ID dst_conn_id;
490
491
2.65M
    if (!demux_identify_conn_id(demux, e, &dst_conn_id))
492
        /*
493
         * Datagram is so badly malformed we can't get the DCID from the first
494
         * packet in it, so just give up.
495
         */
496
1.90M
        return NULL;
497
498
747k
    return demux_get_by_conn_id(demux, &dst_conn_id);
499
2.65M
}
500
501
/*
502
 * Process a single pending URXE.
503
 * Returning 1 on success, 0 on failure and -1 on stateless reset.
504
 */
505
static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
506
2.65M
{
507
2.65M
    QUIC_DEMUX_CONN *conn;
508
2.65M
    int r;
509
510
    /* The next URXE we process should be at the head of the pending list. */
511
2.65M
    if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
512
0
        return 0;
513
514
2.65M
    assert(e->demux_state == URXE_DEMUX_STATE_PENDING);
515
516
    /*
517
     * Check if the packet ends with a stateless reset token and if it does
518
     * skip it after dropping the connection.
519
     *
520
     * RFC 9000 s. 10.3.1 Detecting a Stateless Reset
521
     *      If the last 16 bytes of the datagram are identical in value to
522
     *      a stateless reset token, the endpoint MUST enter the draining
523
     *      period and not send any further packets on this connection.
524
     *
525
     * Returning a failure here causes the connection to enter the terminating
526
     * state which achieves the desired outcome.
527
     *
528
     * TODO(QUIC FUTURE): only try to match unparsable packets
529
     */
530
2.65M
    if (demux->reset_token_cb != NULL) {
531
2.65M
        r = demux->reset_token_cb(ossl_quic_urxe_data(e), e->data_len,
532
2.65M
                                  demux->reset_token_cb_arg);
533
2.65M
        if (r > 0)      /* Received a stateless reset */
534
2
            return -1;
535
2.65M
        if (r < 0)      /* Error during stateless reset detection */
536
0
            return 0;
537
2.65M
    }
538
539
2.65M
    conn = demux_identify_conn(demux, e);
540
2.65M
    if (conn == NULL) {
541
        /*
542
         * We could not identify a connection. If we have a default packet
543
         * handler, pass it to the handler. Otherwise, we will never be able to
544
         * process this datagram, so get rid of it.
545
         */
546
1.90M
        ossl_list_urxe_remove(&demux->urx_pending, e);
547
1.90M
        if (demux->default_cb != NULL) {
548
            /* Pass to default handler. */
549
0
            e->demux_state = URXE_DEMUX_STATE_ISSUED;
550
0
            demux->default_cb(e, demux->default_cb_arg);
551
1.90M
        } else {
552
            /* Discard. */
553
1.90M
            ossl_list_urxe_insert_tail(&demux->urx_free, e);
554
1.90M
            e->demux_state = URXE_DEMUX_STATE_FREE;
555
1.90M
        }
556
1.90M
        return 1; /* keep processing pending URXEs */
557
1.90M
    }
558
559
    /*
560
     * Remove from list and invoke callback. The URXE now belongs to the
561
     * callback. (QUIC_DEMUX_CONN never has non-NULL cb.)
562
     */
563
744k
    ossl_list_urxe_remove(&demux->urx_pending, e);
564
744k
    e->demux_state = URXE_DEMUX_STATE_ISSUED;
565
744k
    conn->cb(e, conn->cb_arg);
566
744k
    return 1;
567
2.65M
}
568
569
/* Process pending URXEs to generate callbacks. */
570
static int demux_process_pending_urxl(QUIC_DEMUX *demux)
571
8.76M
{
572
8.76M
    QUIC_URXE *e;
573
8.76M
    int ret;
574
575
20.5M
    while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
576
11.8M
        if ((ret = demux_process_pending_urxe(demux, e)) <= 0)
577
2
            return ret;
578
579
8.76M
    return 1;
580
8.76M
}
581
582
/*
583
 * Drain the pending URXE list, processing any pending URXEs by making their
584
 * callbacks. If no URXEs are pending, a network read is attempted first.
585
 */
586
int ossl_quic_demux_pump(QUIC_DEMUX *demux)
587
19.2M
{
588
19.2M
    int ret;
589
590
19.2M
    if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
591
19.2M
        ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
592
19.2M
        if (ret != 1)
593
0
            return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
594
595
19.2M
        ret = demux_recv(demux);
596
19.2M
        if (ret != QUIC_DEMUX_PUMP_RES_OK)
597
17.9M
            return ret;
598
599
        /*
600
         * If demux_recv returned successfully, we should always have something.
601
         */
602
19.2M
        assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
603
1.30M
    }
604
605
1.30M
    if ((ret = demux_process_pending_urxl(demux)) <= 0)
606
2
        return ret == 0 ? QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL
607
2
                        : QUIC_DEMUX_PUMP_RES_STATELESS_RESET;
608
609
1.30M
    return QUIC_DEMUX_PUMP_RES_OK;
610
1.30M
}
611
612
/* Artificially inject a packet into the demuxer for testing purposes. */
613
int ossl_quic_demux_inject(QUIC_DEMUX *demux,
614
                           const unsigned char *buf,
615
                           size_t buf_len,
616
                           const BIO_ADDR *peer,
617
                           const BIO_ADDR *local)
618
0
{
619
0
    int ret;
620
0
    QUIC_URXE *urxe;
621
622
0
    ret = demux_ensure_free_urxe(demux, 1);
623
0
    if (ret != 1)
624
0
        return 0;
625
626
0
    urxe = ossl_list_urxe_head(&demux->urx_free);
627
628
0
    assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
629
630
0
    urxe = demux_reserve_urxe(demux, urxe, buf_len);
631
0
    if (urxe == NULL)
632
0
        return 0;
633
634
0
    memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
635
0
    urxe->data_len = buf_len;
636
637
0
    if (peer != NULL)
638
0
        urxe->peer = *peer;
639
0
    else
640
0
        BIO_ADDR_clear(&urxe->peer);
641
642
0
    if (local != NULL)
643
0
        urxe->local = *local;
644
0
    else
645
0
        BIO_ADDR_clear(&urxe->local);
646
647
0
    urxe->time
648
0
        = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
649
650
    /* Move from free list to pending list. */
651
0
    ossl_list_urxe_remove(&demux->urx_free, urxe);
652
0
    ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
653
0
    urxe->demux_state = URXE_DEMUX_STATE_PENDING;
654
655
0
    return demux_process_pending_urxl(demux) > 0;
656
0
}
657
658
/* Called by our user to return a URXE to the free list. */
659
void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
660
                                  QUIC_URXE *e)
661
9.90M
{
662
9.90M
    assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
663
9.90M
    assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
664
9.90M
    ossl_list_urxe_insert_tail(&demux->urx_free, e);
665
9.90M
    e->demux_state = URXE_DEMUX_STATE_FREE;
666
9.90M
}
667
668
void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,
669
                                   QUIC_URXE *e)
670
0
{
671
0
    assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
672
0
    assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
673
0
    ossl_list_urxe_insert_head(&demux->urx_pending, e);
674
0
    e->demux_state = URXE_DEMUX_STATE_PENDING;
675
0
}
676
677
int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux)
678
0
{
679
    return ossl_list_urxe_head(&demux->urx_pending) != NULL;
680
0
}