Coverage Report

Created: 2024-05-21 06:52

/src/openssl/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
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
/* 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
0
{
38
0
    size_t i;
39
0
    unsigned long v = 0;
40
41
0
    assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
42
43
0
    for (i = 0; i < conn->dst_conn_id.id_len; ++i)
44
0
        v ^= ((unsigned long)conn->dst_conn_id.id[i])
45
0
             << ((i * 8) % (sizeof(unsigned long) * 8));
46
47
0
    return v;
48
0
}
49
50
static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
51
0
{
52
0
    return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
53
0
}
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
0
{
111
0
    QUIC_DEMUX *demux;
112
113
0
    demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
114
0
    if (demux == NULL)
115
0
        return NULL;
116
117
0
    demux->net_bio                  = net_bio;
118
0
    demux->short_conn_id_len        = short_conn_id_len;
119
    /* We update this if possible when we get a BIO. */
120
0
    demux->mtu                      = DEMUX_DEFAULT_MTU;
121
0
    demux->now                      = now;
122
0
    demux->now_arg                  = now_arg;
123
124
0
    demux->conns_by_id
125
0
        = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
126
0
    if (demux->conns_by_id == NULL) {
127
0
        OPENSSL_free(demux);
128
0
        return NULL;
129
0
    }
130
131
0
    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
0
    return demux;
137
0
}
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
0
{
146
0
    QUIC_URXE *e, *enext;
147
148
0
    for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
149
0
        enext = ossl_list_urxe_next(e);
150
0
        ossl_list_urxe_remove(l, e);
151
0
        OPENSSL_free(e);
152
0
    }
153
0
}
154
155
void ossl_quic_demux_free(QUIC_DEMUX *demux)
156
0
{
157
0
    if (demux == NULL)
158
0
        return;
159
160
    /* Free all connection structures. */
161
0
    lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
162
0
    lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
163
164
    /* Free all URXEs we are holding. */
165
0
    demux_free_urxl(&demux->urx_free);
166
0
    demux_free_urxl(&demux->urx_pending);
167
168
0
    OPENSSL_free(demux);
169
0
}
170
171
void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)
172
0
{
173
0
    unsigned int mtu;
174
175
0
    demux->net_bio = net_bio;
176
177
0
    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
0
        mtu = BIO_dgram_get_mtu(net_bio);
184
0
        if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)
185
0
            ossl_quic_demux_set_mtu(demux, mtu); /* best effort */
186
0
    }
187
0
}
188
189
int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)
190
0
{
191
0
    if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)
192
0
        return 0;
193
194
0
    demux->mtu = mtu;
195
0
    return 1;
196
0
}
197
198
static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
199
                                             const QUIC_CONN_ID *dst_conn_id)
200
0
{
201
0
    QUIC_DEMUX_CONN key;
202
203
0
    if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
204
0
        return NULL;
205
206
0
    key.dst_conn_id = *dst_conn_id;
207
0
    return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
208
0
}
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
0
{
214
0
    QUIC_DEMUX_CONN *conn;
215
216
0
    if (dst_conn_id == NULL
217
0
        || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
218
0
        || cb == NULL)
219
0
        return 0;
220
221
    /* Ensure not already registered. */
222
0
    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
0
    conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
227
0
    if (conn == NULL)
228
0
        return 0;
229
230
0
    conn->dst_conn_id   = *dst_conn_id;
231
0
    conn->cb            = cb;
232
0
    conn->cb_arg        = cb_arg;
233
234
0
    lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
235
0
    return 1;
236
0
}
237
238
static void demux_unregister(QUIC_DEMUX *demux,
239
                             QUIC_DEMUX_CONN *conn)
240
0
{
241
0
    lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
242
0
    OPENSSL_free(conn);
243
0
}
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
0
{
270
0
    struct unreg_arg *arg = arg_;
271
272
0
    if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
273
0
        conn->next = arg->head;
274
0
        arg->head = conn;
275
0
    }
276
0
}
277
278
void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
279
                                      ossl_quic_demux_cb_fn *cb,
280
                                      void *cb_arg)
281
0
{
282
0
    QUIC_DEMUX_CONN *conn, *cnext;
283
0
    struct unreg_arg arg = {0};
284
0
    arg.cb      = cb;
285
0
    arg.cb_arg  = cb_arg;
286
287
0
    lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
288
0
                                 demux_unregister_by_cb, &arg);
289
290
0
    for (conn = arg.head; conn != NULL; conn = cnext) {
291
0
        cnext = conn->next;
292
0
        demux_unregister(demux, conn);
293
0
    }
294
0
}
295
296
void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
297
                                         ossl_quic_demux_cb_fn *cb,
298
                                         void *cb_arg)
299
0
{
300
0
    demux->default_cb       = cb;
301
0
    demux->default_cb_arg   = cb_arg;
302
0
}
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
0
{
308
0
    demux->reset_token_cb       = cb;
309
0
    demux->reset_token_cb_arg   = cb_arg;
310
0
}
311
312
static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
313
0
{
314
0
    QUIC_URXE *e;
315
316
0
    if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
317
0
        return NULL;
318
319
0
    e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
320
0
    if (e == NULL)
321
0
        return NULL;
322
323
0
    ossl_list_urxe_init_elem(e);
324
0
    e->alloc_len   = alloc_len;
325
0
    e->data_len    = 0;
326
0
    return e;
327
0
}
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
0
{
364
0
    return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;
365
0
}
366
367
static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
368
0
{
369
0
    QUIC_URXE *e;
370
371
0
    while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
372
0
        e = demux_alloc_urxe(demux->mtu);
373
0
        if (e == NULL)
374
0
            return 0;
375
376
0
        ossl_list_urxe_insert_tail(&demux->urx_free, e);
377
0
        e->demux_state = URXE_DEMUX_STATE_FREE;
378
0
    }
379
380
0
    return 1;
381
0
}
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
0
{
393
0
    BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
394
0
    size_t rd, i;
395
0
    QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
396
0
    OSSL_TIME now;
397
398
    /* This should never be called when we have any pending URXE. */
399
0
    assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
400
0
    assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
401
402
0
    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
0
    for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
413
0
            ++i, urxe = ossl_list_urxe_next(urxe)) {
414
0
        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
0
        urxe = demux_reserve_urxe(demux, urxe, demux->mtu);
424
0
        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
0
        memset(&msg[i], 0, sizeof(BIO_MSG));
430
0
        msg[i].data     = ossl_quic_urxe_data(urxe);
431
0
        msg[i].data_len = urxe->alloc_len;
432
0
        msg[i].peer     = &urxe->peer;
433
0
        BIO_ADDR_clear(&urxe->peer);
434
0
        if (demux->use_local_addr)
435
0
            msg[i].local = &urxe->local;
436
0
        else
437
0
            BIO_ADDR_clear(&urxe->local);
438
0
    }
439
440
0
    ERR_set_mark();
441
0
    if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {
442
0
        if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
443
            /* Transient error, clear the error and stop. */
444
0
            ERR_pop_to_mark();
445
0
            return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
446
0
        } 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
0
    }
452
453
0
    ERR_clear_last_mark();
454
0
    now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
455
456
0
    urxe = ossl_list_urxe_head(&demux->urx_free);
457
0
    for (i = 0; i < rd; ++i, urxe = unext) {
458
0
        unext = ossl_list_urxe_next(urxe);
459
        /* Set URXE with actual length of received datagram. */
460
0
        urxe->data_len      = msg[i].data_len;
461
        /* Time we received datagram. */
462
0
        urxe->time          = now;
463
        /* Move from free list to pending list. */
464
0
        ossl_list_urxe_remove(&demux->urx_free, urxe);
465
0
        ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
466
0
        urxe->demux_state = URXE_DEMUX_STATE_PENDING;
467
0
    }
468
469
0
    return QUIC_DEMUX_PUMP_RES_OK;
470
0
}
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
0
{
477
0
    return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
478
0
                                                  e->data_len,
479
0
                                                  demux->short_conn_id_len,
480
0
                                                  dst_conn_id);
481
0
}
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
0
{
486
0
    QUIC_CONN_ID dst_conn_id;
487
488
0
    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
0
        return NULL;
494
495
0
    return demux_get_by_conn_id(demux, &dst_conn_id);
496
0
}
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
0
{
504
0
    QUIC_DEMUX_CONN *conn;
505
0
    int r;
506
507
    /* The next URXE we process should be at the head of the pending list. */
508
0
    if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
509
0
        return 0;
510
511
0
    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
0
    if (demux->reset_token_cb != NULL) {
528
0
        r = demux->reset_token_cb(ossl_quic_urxe_data(e), e->data_len,
529
0
                                  demux->reset_token_cb_arg);
530
0
        if (r > 0)      /* Received a stateless reset */
531
0
            return -1;
532
0
        if (r < 0)      /* Error during stateless reset detection */
533
0
            return 0;
534
0
    }
535
536
0
    conn = demux_identify_conn(demux, e);
537
0
    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
0
        ossl_list_urxe_remove(&demux->urx_pending, e);
544
0
        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
0
        } else {
549
            /* Discard. */
550
0
            ossl_list_urxe_insert_tail(&demux->urx_free, e);
551
0
            e->demux_state = URXE_DEMUX_STATE_FREE;
552
0
        }
553
0
        return 1; /* keep processing pending URXEs */
554
0
    }
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
0
    ossl_list_urxe_remove(&demux->urx_pending, e);
561
0
    e->demux_state = URXE_DEMUX_STATE_ISSUED;
562
0
    conn->cb(e, conn->cb_arg);
563
0
    return 1;
564
0
}
565
566
/* Process pending URXEs to generate callbacks. */
567
static int demux_process_pending_urxl(QUIC_DEMUX *demux)
568
0
{
569
0
    QUIC_URXE *e;
570
0
    int ret;
571
572
0
    while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
573
0
        if ((ret = demux_process_pending_urxe(demux, e)) <= 0)
574
0
            return ret;
575
576
0
    return 1;
577
0
}
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
0
{
585
0
    int ret;
586
587
0
    if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
588
0
        ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
589
0
        if (ret != 1)
590
0
            return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
591
592
0
        ret = demux_recv(demux);
593
0
        if (ret != QUIC_DEMUX_PUMP_RES_OK)
594
0
            return ret;
595
596
        /*
597
         * If demux_recv returned successfully, we should always have something.
598
         */
599
0
        assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
600
0
    }
601
602
0
    if ((ret = demux_process_pending_urxl(demux)) <= 0)
603
0
        return ret == 0 ? QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL
604
0
                        : QUIC_DEMUX_PUMP_RES_STATELESS_RESET;
605
606
0
    return QUIC_DEMUX_PUMP_RES_OK;
607
0
}
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
0
{
659
0
    assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
660
0
    assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
661
0
    ossl_list_urxe_insert_tail(&demux->urx_free, e);
662
0
    e->demux_state = URXE_DEMUX_STATE_FREE;
663
0
}
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
}