Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/ssl/quic/quic_txp.c
Line
Count
Source (jump to first uncovered line)
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_txp.h"
11
#include "internal/quic_fifd.h"
12
#include "internal/quic_stream_map.h"
13
#include "internal/quic_error.h"
14
#include "internal/common.h"
15
#include <openssl/err.h>
16
17
495k
#define MIN_CRYPTO_HDR_SIZE             3
18
19
0
#define MIN_FRAME_SIZE_HANDSHAKE_DONE   1
20
0
#define MIN_FRAME_SIZE_MAX_DATA         2
21
38.0M
#define MIN_FRAME_SIZE_ACK              5
22
495k
#define MIN_FRAME_SIZE_CRYPTO           (MIN_CRYPTO_HDR_SIZE + 1)
23
2.53k
#define MIN_FRAME_SIZE_STREAM           3 /* minimum useful size (for non-FIN) */
24
0
#define MIN_FRAME_SIZE_MAX_STREAMS_BIDI 2
25
0
#define MIN_FRAME_SIZE_MAX_STREAMS_UNI  2
26
27
/*
28
 * Packet Archetypes
29
 * =================
30
 */
31
32
/* Generate normal packets containing most frame types, subject to EL. */
33
4.65M
#define TX_PACKETISER_ARCHETYPE_NORMAL              0
34
35
/*
36
 * A probe packet is different in that:
37
 *   - It bypasses CC, but *is* counted as in flight for purposes of CC;
38
 *   - It must be ACK-eliciting.
39
 */
40
27.7M
#define TX_PACKETISER_ARCHETYPE_PROBE               1
41
42
/*
43
 * An ACK-only packet is different in that:
44
 *   - It bypasses CC, and is considered a 'non-inflight' packet;
45
 *   - It may not contain anything other than an ACK frame, not even padding.
46
 */
47
22.7M
#define TX_PACKETISER_ARCHETYPE_ACK_ONLY            2
48
49
54.2M
#define TX_PACKETISER_ARCHETYPE_NUM                 3
50
51
struct ossl_quic_tx_packetiser_st {
52
    OSSL_QUIC_TX_PACKETISER_ARGS args;
53
54
    /*
55
     * Opaque initial token blob provided by caller. TXP frees using the
56
     * callback when it is no longer needed.
57
     */
58
    const unsigned char             *initial_token;
59
    size_t                          initial_token_len;
60
    ossl_quic_initial_token_free_fn *initial_token_free_cb;
61
    void                            *initial_token_free_cb_arg;
62
63
    /* Subcomponents of the TXP that we own. */
64
    QUIC_FIFD       fifd;       /* QUIC Frame-in-Flight Dispatcher */
65
66
    /* Internal state. */
67
    uint64_t        next_pn[QUIC_PN_SPACE_NUM]; /* Next PN to use in given PN space. */
68
    OSSL_TIME       last_tx_time;               /* Last time a packet was generated, or 0. */
69
70
    /* Internal state - frame (re)generation flags. */
71
    unsigned int    want_handshake_done     : 1;
72
    unsigned int    want_max_data           : 1;
73
    unsigned int    want_max_streams_bidi   : 1;
74
    unsigned int    want_max_streams_uni    : 1;
75
76
    /* Internal state - frame (re)generation flags - per PN space. */
77
    unsigned int    want_ack                : QUIC_PN_SPACE_NUM;
78
    unsigned int    force_ack_eliciting     : QUIC_PN_SPACE_NUM;
79
80
    /*
81
     * Internal state - connection close terminal state.
82
     * Once this is set, it is not unset unlike other want_ flags - we keep
83
     * sending it in every packet.
84
     */
85
    unsigned int    want_conn_close         : 1;
86
87
    /* Has the handshake been completed? */
88
    unsigned int    handshake_complete      : 1;
89
90
    OSSL_QUIC_FRAME_CONN_CLOSE  conn_close_frame;
91
92
    /*
93
     * Counts of the number of bytes received and sent while in the closing
94
     * state.
95
     */
96
    uint64_t                        closing_bytes_recv;
97
    uint64_t                        closing_bytes_xmit;
98
99
    /* Internal state - packet assembly. */
100
    struct txp_el {
101
        unsigned char   *scratch;       /* scratch buffer for packet assembly */
102
        size_t          scratch_len;    /* number of bytes allocated for scratch */
103
        OSSL_QTX_IOVEC  *iovec;         /* scratch iovec array for use with QTX */
104
        size_t          alloc_iovec;    /* size of iovec array */
105
    } el[QUIC_ENC_LEVEL_NUM];
106
107
    /* Message callback related arguments */
108
    ossl_msg_cb msg_callback;
109
    void *msg_callback_arg;
110
    SSL *msg_callback_ssl;
111
112
    /* Callbacks. */
113
    void            (*ack_tx_cb)(const OSSL_QUIC_FRAME_ACK *ack,
114
                                 uint32_t pn_space,
115
                                 void *arg);
116
    void            *ack_tx_cb_arg;
117
};
118
119
/*
120
 * The TX helper records state used while generating frames into packets. It
121
 * enables serialization into the packet to be done "transactionally" where
122
 * serialization of a frame can be rolled back if it fails midway (e.g. if it
123
 * does not fit).
124
 */
125
struct tx_helper {
126
    OSSL_QUIC_TX_PACKETISER *txp;
127
    /*
128
     * The Maximum Packet Payload Length in bytes. This is the amount of
129
     * space we have to generate frames into.
130
     */
131
    size_t max_ppl;
132
    /*
133
     * Number of bytes we have generated so far.
134
     */
135
    size_t bytes_appended;
136
    /*
137
     * Number of scratch bytes in txp->scratch we have used so far. Some iovecs
138
     * will reference this scratch buffer. When we need to use more of it (e.g.
139
     * when we need to put frame headers somewhere), we append to the scratch
140
     * buffer, resizing if necessary, and increase this accordingly.
141
     */
142
    size_t scratch_bytes;
143
    /*
144
     * Bytes reserved in the MaxPPL budget. We keep this number of bytes spare
145
     * until reserve_allowed is set to 1. Currently this is always at most 1, as
146
     * a PING frame takes up one byte and this mechanism is only used to ensure
147
     * we can encode a PING frame if we have been asked to ensure a packet is
148
     * ACK-eliciting and we are unusure if we are going to add any other
149
     * ACK-eliciting frames before we reach our MaxPPL budget.
150
     */
151
    size_t reserve;
152
    /*
153
     * Number of iovecs we have currently appended. This is the number of
154
     * entries valid in txp->iovec.
155
     */
156
    size_t num_iovec;
157
    /* The EL this TX helper is being used for. */
158
    uint32_t enc_level;
159
    /*
160
     * Whether we are allowed to make use of the reserve bytes in our MaxPPL
161
     * budget. This is used to ensure we have room to append a PING frame later
162
     * if we need to. Once we know we will not need to append a PING frame, this
163
     * is set to 1.
164
     */
165
    unsigned int reserve_allowed : 1;
166
    /*
167
     * Set to 1 if we have appended a STREAM frame with an implicit length. If
168
     * this happens we should never append another frame after that frame as it
169
     * cannot be validly encoded. This is just a safety check.
170
     */
171
    unsigned int done_implicit : 1;
172
    struct {
173
        /*
174
         * The fields in this structure are valid if active is set, which means
175
         * that a serialization transaction is currently in progress.
176
         */
177
        unsigned char   *data;
178
        WPACKET         wpkt;
179
        unsigned int    active : 1;
180
    } txn;
181
};
182
183
static void tx_helper_rollback(struct tx_helper *h);
184
static int txp_el_ensure_iovec(struct txp_el *el, size_t num);
185
186
/* Initialises the TX helper. */
187
static int tx_helper_init(struct tx_helper *h, OSSL_QUIC_TX_PACKETISER *txp,
188
                          uint32_t enc_level, size_t max_ppl, size_t reserve)
189
19.0M
{
190
19.0M
    if (reserve > max_ppl)
191
0
        return 0;
192
193
19.0M
    h->txp                  = txp;
194
19.0M
    h->enc_level            = enc_level;
195
19.0M
    h->max_ppl              = max_ppl;
196
19.0M
    h->reserve              = reserve;
197
19.0M
    h->num_iovec            = 0;
198
19.0M
    h->bytes_appended       = 0;
199
19.0M
    h->scratch_bytes        = 0;
200
19.0M
    h->reserve_allowed      = 0;
201
19.0M
    h->done_implicit        = 0;
202
19.0M
    h->txn.data             = NULL;
203
19.0M
    h->txn.active           = 0;
204
205
19.0M
    if (max_ppl > h->txp->el[enc_level].scratch_len) {
206
51.6k
        unsigned char *scratch;
207
208
51.6k
        scratch = OPENSSL_realloc(h->txp->el[enc_level].scratch, max_ppl);
209
51.6k
        if (scratch == NULL)
210
0
            return 0;
211
212
51.6k
        h->txp->el[enc_level].scratch     = scratch;
213
51.6k
        h->txp->el[enc_level].scratch_len = max_ppl;
214
51.6k
    }
215
216
19.0M
    return 1;
217
19.0M
}
218
219
static void tx_helper_cleanup(struct tx_helper *h)
220
19.0M
{
221
19.0M
    if (h->txn.active)
222
0
        tx_helper_rollback(h);
223
224
19.0M
    h->txp = NULL;
225
19.0M
}
226
227
static void tx_helper_unrestrict(struct tx_helper *h)
228
19.1M
{
229
19.1M
    h->reserve_allowed = 1;
230
19.1M
}
231
232
/*
233
 * Append an extent of memory to the iovec list. The memory must remain
234
 * allocated until we finish generating the packet and call the QTX.
235
 *
236
 * In general, the buffers passed to this function will be from one of two
237
 * ranges:
238
 *
239
 *   - Application data contained in stream buffers managed elsewhere
240
 *     in the QUIC stack; or
241
 *
242
 *   - Control frame data appended into txp->scratch using tx_helper_begin and
243
 *     tx_helper_commit.
244
 *
245
 */
246
static int tx_helper_append_iovec(struct tx_helper *h,
247
                                  const unsigned char *buf,
248
                                  size_t buf_len)
249
1.23M
{
250
1.23M
    struct txp_el *el = &h->txp->el[h->enc_level];
251
252
1.23M
    if (buf_len == 0)
253
0
        return 1;
254
255
1.23M
    if (!ossl_assert(!h->done_implicit))
256
0
        return 0;
257
258
1.23M
    if (!txp_el_ensure_iovec(el, h->num_iovec + 1))
259
0
        return 0;
260
261
1.23M
    el->iovec[h->num_iovec].buf     = buf;
262
1.23M
    el->iovec[h->num_iovec].buf_len = buf_len;
263
264
1.23M
    ++h->num_iovec;
265
1.23M
    h->bytes_appended += buf_len;
266
1.23M
    return 1;
267
1.23M
}
268
269
/*
270
 * How many more bytes of space do we have left in our plaintext packet payload?
271
 */
272
static size_t tx_helper_get_space_left(struct tx_helper *h)
273
22.3M
{
274
22.3M
    return h->max_ppl
275
22.3M
        - (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
276
22.3M
}
277
278
/*
279
 * Begin a control frame serialization transaction. This allows the
280
 * serialization of the control frame to be backed out if it turns out it won't
281
 * fit. Write the control frame to the returned WPACKET. Ensure you always
282
 * call tx_helper_rollback or tx_helper_commit (or tx_helper_cleanup). Returns
283
 * NULL on failure.
284
 */
285
static WPACKET *tx_helper_begin(struct tx_helper *h)
286
2.71M
{
287
2.71M
    size_t space_left, len;
288
2.71M
    unsigned char *data;
289
2.71M
    struct txp_el *el = &h->txp->el[h->enc_level];
290
291
2.71M
    if (!ossl_assert(!h->txn.active))
292
0
        return NULL;
293
294
2.71M
    if (!ossl_assert(!h->done_implicit))
295
0
        return NULL;
296
297
2.71M
    data = (unsigned char *)el->scratch + h->scratch_bytes;
298
2.71M
    len  = el->scratch_len - h->scratch_bytes;
299
300
2.71M
    space_left = tx_helper_get_space_left(h);
301
2.71M
    if (!ossl_assert(space_left <= len))
302
0
        return NULL;
303
304
2.71M
    if (!WPACKET_init_static_len(&h->txn.wpkt, data, len, 0))
305
0
        return NULL;
306
307
2.71M
    if (!WPACKET_set_max_size(&h->txn.wpkt, space_left)) {
308
0
        WPACKET_cleanup(&h->txn.wpkt);
309
0
        return NULL;
310
0
    }
311
312
2.71M
    h->txn.data     = data;
313
2.71M
    h->txn.active   = 1;
314
2.71M
    return &h->txn.wpkt;
315
2.71M
}
316
317
static void tx_helper_end(struct tx_helper *h, int success)
318
2.71M
{
319
2.71M
    if (success)
320
1.08M
        WPACKET_finish(&h->txn.wpkt);
321
1.63M
    else
322
1.63M
        WPACKET_cleanup(&h->txn.wpkt);
323
324
2.71M
    h->txn.active       = 0;
325
2.71M
    h->txn.data         = NULL;
326
2.71M
}
327
328
/* Abort a control frame serialization transaction. */
329
static void tx_helper_rollback(struct tx_helper *h)
330
1.63M
{
331
1.63M
    if (!h->txn.active)
332
0
        return;
333
334
1.63M
    tx_helper_end(h, 0);
335
1.63M
}
336
337
/* Commit a control frame. */
338
static int tx_helper_commit(struct tx_helper *h)
339
1.08M
{
340
1.08M
    size_t l = 0;
341
342
1.08M
    if (!h->txn.active)
343
0
        return 0;
344
345
1.08M
    if (!WPACKET_get_total_written(&h->txn.wpkt, &l)) {
346
0
        tx_helper_end(h, 0);
347
0
        return 0;
348
0
    }
349
350
1.08M
    if (!tx_helper_append_iovec(h, h->txn.data, l)) {
351
0
        tx_helper_end(h, 0);
352
0
        return 0;
353
0
    }
354
355
1.08M
    if (h->txp->msg_callback != NULL && l > 0) {
356
0
        uint64_t ftype;
357
0
        int ctype = SSL3_RT_QUIC_FRAME_FULL;
358
0
        PACKET pkt;
359
360
0
        if (!PACKET_buf_init(&pkt, h->txn.data, l)
361
0
                || !ossl_quic_wire_peek_frame_header(&pkt, &ftype, NULL)) {
362
0
            tx_helper_end(h, 0);
363
0
            return 0;
364
0
        }
365
366
0
        if (ftype == OSSL_QUIC_FRAME_TYPE_PADDING)
367
0
            ctype = SSL3_RT_QUIC_FRAME_PADDING;
368
0
        else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(ftype)
369
0
                || ftype == OSSL_QUIC_FRAME_TYPE_CRYPTO)
370
0
            ctype = SSL3_RT_QUIC_FRAME_HEADER;
371
372
0
        h->txp->msg_callback(1, OSSL_QUIC1_VERSION, ctype, h->txn.data, l,
373
0
                             h->txp->msg_callback_ssl,
374
0
                             h->txp->msg_callback_arg);
375
0
    }
376
377
1.08M
    h->scratch_bytes += l;
378
1.08M
    tx_helper_end(h, 1);
379
1.08M
    return 1;
380
1.08M
}
381
382
struct archetype_data {
383
    unsigned int allow_ack                  : 1;
384
    unsigned int allow_ping                 : 1;
385
    unsigned int allow_crypto               : 1;
386
    unsigned int allow_handshake_done       : 1;
387
    unsigned int allow_path_challenge       : 1;
388
    unsigned int allow_path_response        : 1;
389
    unsigned int allow_new_conn_id          : 1;
390
    unsigned int allow_retire_conn_id       : 1;
391
    unsigned int allow_stream_rel           : 1;
392
    unsigned int allow_conn_fc              : 1;
393
    unsigned int allow_conn_close           : 1;
394
    unsigned int allow_cfq_other            : 1;
395
    unsigned int allow_new_token            : 1;
396
    unsigned int allow_force_ack_eliciting  : 1;
397
    unsigned int allow_padding              : 1;
398
    unsigned int require_ack_eliciting      : 1;
399
    unsigned int bypass_cc                  : 1;
400
};
401
402
struct txp_pkt_geom {
403
    size_t                  cmpl, cmppl, hwm, pkt_overhead;
404
    uint32_t                archetype;
405
    struct archetype_data   adata;
406
};
407
408
struct txp_pkt {
409
    struct tx_helper    h;
410
    int                 h_valid;
411
    QUIC_TXPIM_PKT      *tpkt;
412
    QUIC_STREAM         *stream_head;
413
    QUIC_PKT_HDR        phdr;
414
    struct txp_pkt_geom geom;
415
    int                 force_pad;
416
};
417
418
static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
419
                                       void *arg);
420
static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
421
                            QUIC_TXPIM_PKT *pkt, void *arg);
422
static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
423
                              QUIC_TXPIM_PKT *pkt, void *arg);
424
static void on_sstream_updated(uint64_t stream_id, void *arg);
425
static int sstream_is_pending(QUIC_SSTREAM *sstream);
426
static int txp_should_try_staging(OSSL_QUIC_TX_PACKETISER *txp,
427
                                  uint32_t enc_level,
428
                                  uint32_t archetype,
429
                                  uint64_t cc_limit,
430
                                  uint32_t *conn_close_enc_level);
431
static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp);
432
static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
433
                                     size_t pl,
434
                                     uint32_t enc_level,
435
                                     size_t hdr_len,
436
                                     size_t *r);
437
static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp);
438
static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp,
439
                               struct txp_pkt *pkt,
440
                               int chosen_for_conn_close);
441
static int txp_pkt_init(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp,
442
                        uint32_t enc_level, uint32_t archetype,
443
                        size_t running_total);
444
static void txp_pkt_cleanup(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp);
445
static int txp_pkt_postgen_update_pkt_overhead(struct txp_pkt *pkt,
446
                                               OSSL_QUIC_TX_PACKETISER *txp);
447
static int txp_pkt_append_padding(struct txp_pkt *pkt,
448
                                  OSSL_QUIC_TX_PACKETISER *txp, size_t num_bytes);
449
static int txp_pkt_commit(OSSL_QUIC_TX_PACKETISER *txp, struct txp_pkt *pkt,
450
                          uint32_t archetype, int *txpim_pkt_reffed);
451
static uint32_t txp_determine_archetype(OSSL_QUIC_TX_PACKETISER *txp,
452
                                        uint64_t cc_limit);
453
454
OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args)
455
21.1k
{
456
21.1k
    OSSL_QUIC_TX_PACKETISER *txp;
457
458
21.1k
    if (args == NULL
459
21.1k
        || args->qtx == NULL
460
21.1k
        || args->txpim == NULL
461
21.1k
        || args->cfq == NULL
462
21.1k
        || args->ackm == NULL
463
21.1k
        || args->qsm == NULL
464
21.1k
        || args->conn_txfc == NULL
465
21.1k
        || args->conn_rxfc == NULL
466
21.1k
        || args->max_streams_bidi_rxfc == NULL
467
21.1k
        || args->max_streams_uni_rxfc == NULL) {
468
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
469
0
        return NULL;
470
0
    }
471
472
21.1k
    txp = OPENSSL_zalloc(sizeof(*txp));
473
21.1k
    if (txp == NULL)
474
0
        return NULL;
475
476
21.1k
    txp->args           = *args;
477
21.1k
    txp->last_tx_time   = ossl_time_zero();
478
479
21.1k
    if (!ossl_quic_fifd_init(&txp->fifd,
480
21.1k
                             txp->args.cfq, txp->args.ackm, txp->args.txpim,
481
21.1k
                             get_sstream_by_id, txp,
482
21.1k
                             on_regen_notify, txp,
483
21.1k
                             on_confirm_notify, txp,
484
21.1k
                             on_sstream_updated, txp)) {
485
0
        OPENSSL_free(txp);
486
0
        return NULL;
487
0
    }
488
489
21.1k
    return txp;
490
21.1k
}
491
492
void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp)
493
21.1k
{
494
21.1k
    uint32_t enc_level;
495
496
21.1k
    if (txp == NULL)
497
0
        return;
498
499
21.1k
    ossl_quic_tx_packetiser_set_initial_token(txp, NULL, 0, NULL, NULL);
500
21.1k
    ossl_quic_fifd_cleanup(&txp->fifd);
501
21.1k
    OPENSSL_free(txp->conn_close_frame.reason);
502
503
21.1k
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
504
105k
         enc_level < QUIC_ENC_LEVEL_NUM;
505
84.7k
         ++enc_level) {
506
84.7k
        OPENSSL_free(txp->el[enc_level].iovec);
507
84.7k
        OPENSSL_free(txp->el[enc_level].scratch);
508
84.7k
    }
509
510
21.1k
    OPENSSL_free(txp);
511
21.1k
}
512
513
/*
514
 * Determine if an Initial packet token length is reasonable based on the
515
 * current MDPL, returning 1 if it is OK.
516
 *
517
 * The real PMTU to the peer could differ from our (pessimistic) understanding
518
 * of the PMTU, therefore it is possible we could receive an Initial token from
519
 * a server in a Retry packet which is bigger than the MDPL. In this case it is
520
 * impossible for us ever to make forward progress and we need to error out
521
 * and fail the connection attempt.
522
 *
523
 * The specific boundary condition is complex: for example, after the size of
524
 * the Initial token, there are the Initial packet header overheads and then
525
 * encryption/AEAD tag overheads. After that, the minimum room for frame data in
526
 * order to guarantee forward progress must be guaranteed. For example, a crypto
527
 * stream needs to always be able to serialize at least one byte in a CRYPTO
528
 * frame in order to make forward progress. Because the offset field of a CRYPTO
529
 * frame uses a variable-length integer, the number of bytes needed to ensure
530
 * this also varies.
531
 *
532
 * Rather than trying to get this boundary condition check actually right,
533
 * require a reasonable amount of slack to avoid pathological behaviours. (After
534
 * all, transmitting a CRYPTO stream one byte at a time is probably not
535
 * desirable anyway.)
536
 *
537
 * We choose 160 bytes as the required margin, which is double the rough
538
 * estimation of the minimum we would require to guarantee forward progress
539
 * under worst case packet overheads.
540
 */
541
814
#define TXP_REQUIRED_TOKEN_MARGIN       160
542
543
static int txp_check_token_len(size_t token_len, size_t mdpl)
544
21.5k
{
545
21.5k
    if (token_len == 0)
546
21.1k
        return 1;
547
548
409
    if (token_len >= mdpl)
549
2
        return 0;
550
551
407
    if (TXP_REQUIRED_TOKEN_MARGIN >= mdpl)
552
        /* (should not be possible because MDPL must be at least 1200) */
553
0
        return 0;
554
555
407
    if (token_len > mdpl - TXP_REQUIRED_TOKEN_MARGIN)
556
2
        return 0;
557
558
405
    return 1;
559
407
}
560
561
int ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
562
                                              const unsigned char *token,
563
                                              size_t token_len,
564
                                              ossl_quic_initial_token_free_fn *free_cb,
565
                                              void *free_cb_arg)
566
21.5k
{
567
21.5k
    if (!txp_check_token_len(token_len, txp_get_mdpl(txp)))
568
4
        return 0;
569
570
21.5k
    if (txp->initial_token != NULL && txp->initial_token_free_cb != NULL)
571
405
        txp->initial_token_free_cb(txp->initial_token, txp->initial_token_len,
572
405
                                   txp->initial_token_free_cb_arg);
573
574
21.5k
    txp->initial_token              = token;
575
21.5k
    txp->initial_token_len          = token_len;
576
21.5k
    txp->initial_token_free_cb      = free_cb;
577
21.5k
    txp->initial_token_free_cb_arg  = free_cb_arg;
578
21.5k
    return 1;
579
21.5k
}
580
581
int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
582
                                         const QUIC_CONN_ID *dcid)
583
21.7k
{
584
21.7k
    if (dcid == NULL) {
585
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
586
0
        return 0;
587
0
    }
588
589
21.7k
    txp->args.cur_dcid = *dcid;
590
21.7k
    return 1;
591
21.7k
}
592
593
int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
594
                                         const QUIC_CONN_ID *scid)
595
0
{
596
0
    if (scid == NULL) {
597
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
598
0
        return 0;
599
0
    }
600
601
0
    txp->args.cur_scid = *scid;
602
0
    return 1;
603
0
}
604
605
/* Change the destination L4 address the TXP uses to send datagrams. */
606
int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
607
                                     const BIO_ADDR *peer)
608
21.1k
{
609
21.1k
    if (peer == NULL) {
610
0
        BIO_ADDR_clear(&txp->args.peer);
611
0
        return 1;
612
0
    }
613
614
21.1k
    txp->args.peer = *peer;
615
21.1k
    return 1;
616
21.1k
}
617
618
void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
619
                                           void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
620
                                                      uint32_t pn_space,
621
                                                      void *arg),
622
                                           void *cb_arg)
623
21.1k
{
624
21.1k
    txp->ack_tx_cb      = cb;
625
21.1k
    txp->ack_tx_cb_arg  = cb_arg;
626
21.1k
}
627
628
int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
629
                                              uint32_t enc_level)
630
12.0k
{
631
12.0k
    if (enc_level >= QUIC_ENC_LEVEL_NUM) {
632
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
633
0
        return 0;
634
0
    }
635
636
12.0k
    if (enc_level != QUIC_ENC_LEVEL_0RTT)
637
12.0k
        txp->args.crypto[ossl_quic_enc_level_to_pn_space(enc_level)] = NULL;
638
639
12.0k
    return 1;
640
12.0k
}
641
642
void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp)
643
5.41k
{
644
5.41k
    txp->handshake_complete = 1;
645
5.41k
}
646
647
void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp)
648
0
{
649
0
    txp->want_handshake_done = 1;
650
0
}
651
652
void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
653
                                                    uint32_t pn_space)
654
7.07M
{
655
7.07M
    txp->force_ack_eliciting |= (1UL << pn_space);
656
7.07M
}
657
658
void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
659
                                          uint32_t pn_space)
660
8.27k
{
661
8.27k
    txp->want_ack |= (1UL << pn_space);
662
8.27k
}
663
664
0
#define TXP_ERR_INTERNAL     0  /* Internal (e.g. alloc) error */
665
38.0M
#define TXP_ERR_SUCCESS      1  /* Success */
666
#define TXP_ERR_SPACE        2  /* Not enough room for another packet */
667
#define TXP_ERR_INPUT        3  /* Invalid/malformed input */
668
669
/*
670
 * Generates a datagram by polling the various ELs to determine if they want to
671
 * generate any frames, and generating a datagram which coalesces packets for
672
 * any ELs which do.
673
 */
674
int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
675
                                     QUIC_TXP_STATUS *status)
676
27.5M
{
677
    /*
678
     * Called to generate one or more datagrams, each containing one or more
679
     * packets.
680
     *
681
     * There are some tricky things to note here:
682
     *
683
     *   - The TXP is only concerned with generating encrypted packets;
684
     *     other packets use a different path.
685
     *
686
     *   - Any datagram containing an Initial packet must have a payload length
687
     *     (DPL) of at least 1200 bytes. This padding need not necessarily be
688
     *     found in the Initial packet.
689
     *
690
     *     - It is desirable to be able to coalesce an Initial packet
691
     *       with a Handshake packet. Since, before generating the Handshake
692
     *       packet, we do not know how long it will be, we cannot know the
693
     *       correct amount of padding to ensure a DPL of at least 1200 bytes.
694
     *       Thus this padding must added to the Handshake packet (or whatever
695
     *       packet is the last in the datagram).
696
     *
697
     *     - However, at the time that we generate the Initial packet,
698
     *       we do not actually know for sure that we will be followed
699
     *       in the datagram by another packet. For example, suppose we have
700
     *       some queued data (e.g. crypto stream data for the HANDSHAKE EL)
701
     *       it looks like we will want to send on the HANDSHAKE EL.
702
     *       We could assume padding will be placed in the Handshake packet
703
     *       subsequently and avoid adding any padding to the Initial packet
704
     *       (which would leave no room for the Handshake packet in the
705
     *       datagram).
706
     *
707
     *       However, this is not actually a safe assumption. Suppose that we
708
     *       are using a link with a MDPL of 1200 bytes, the minimum allowed by
709
     *       QUIC. Suppose that the Initial packet consumes 1195 bytes in total.
710
     *       Since it is not possible to fit a Handshake packet in just 5 bytes,
711
     *       upon trying to add a Handshake packet after generating the Initial
712
     *       packet, we will discover we have no room to fit it! This is not a
713
     *       problem in itself as another datagram can be sent subsequently, but
714
     *       it is a problem because we were counting to use that packet to hold
715
     *       the essential padding. But if we have already finished encrypting
716
     *       the Initial packet, we cannot go and add padding to it anymore.
717
     *       This leaves us stuck.
718
     *
719
     * Because of this, we have to plan multiple packets simultaneously, such
720
     * that we can start generating a Handshake (or 0-RTT or 1-RTT, or so on)
721
     * packet while still having the option to go back and add padding to the
722
     * Initial packet if it turns out to be needed.
723
     *
724
     * Trying to predict ahead of time (e.g. during Initial packet generation)
725
     * whether we will successfully generate a subsequent packet is fraught with
726
     * error as it relies on a large number of variables:
727
     *
728
     *   - Do we have room to fit a packet header? (Consider that due to
729
     *     variable-length integer encoding this is highly variable and can even
730
     *     depend on payload length due to a variable-length Length field.)
731
     *
732
     *   - Can we fit even a single one of the frames we want to put in this
733
     *     packet in the packet? (Each frame type has a bespoke encoding. While
734
     *     our encodings of some frame types are adaptive based on the available
735
     *     room - e.g. STREAM frames - ultimately all frame types have some
736
     *     absolute minimum number of bytes to be successfully encoded. For
737
     *     example, if after an Initial packet there is enough room to encode
738
     *     only one byte of frame data, it is quite likely we can't send any of
739
     *     the frames we wanted to send.) While this is not strictly a problem
740
     *     because we could just fill the packet with padding frames, this is a
741
     *     pointless packet and is wasteful.
742
     *
743
     * Thus we adopt a multi-phase architecture:
744
     *
745
     *   1. Archetype Selection: Determine desired packet archetype.
746
     *
747
     *   2. Packet Staging: Generation of packet information and packet payload
748
     *      data (frame data) into staging areas.
749
     *
750
     *   3. Packet Adjustment: Adjustment of staged packets, adding padding to
751
     *      the staged packets if needed.
752
     *
753
     *   4. Commit: The packets are sent to the QTX and recorded as having been
754
     *      sent to the FIFM.
755
     *
756
     */
757
27.5M
    int res = 0, rc;
758
27.5M
    uint32_t archetype, enc_level;
759
27.5M
    uint32_t conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
760
27.5M
    struct txp_pkt pkt[QUIC_ENC_LEVEL_NUM];
761
27.5M
    size_t pkts_done = 0;
762
27.5M
    uint64_t cc_limit = txp->args.cc_method->get_tx_allowance(txp->args.cc_data);
763
27.5M
    int need_padding = 0, txpim_pkt_reffed;
764
765
27.5M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
766
137M
         enc_level < QUIC_ENC_LEVEL_NUM;
767
110M
         ++enc_level)
768
110M
        pkt[enc_level].h_valid = 0;
769
770
27.5M
    memset(status, 0, sizeof(*status));
771
772
    /*
773
     * Should not be needed, but a sanity check in case anyone else has been
774
     * using the QTX.
775
     */
776
27.5M
    ossl_qtx_finish_dgram(txp->args.qtx);
777
778
    /* 1. Archetype Selection */
779
27.5M
    archetype = txp_determine_archetype(txp, cc_limit);
780
781
    /* 2. Packet Staging */
782
27.5M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
783
137M
         enc_level < QUIC_ENC_LEVEL_NUM;
784
110M
         ++enc_level) {
785
110M
        size_t running_total = (enc_level > QUIC_ENC_LEVEL_INITIAL)
786
110M
            ? pkt[enc_level - 1].geom.hwm : 0;
787
788
110M
        pkt[enc_level].geom.hwm = running_total;
789
790
110M
        if (!txp_should_try_staging(txp, enc_level, archetype, cc_limit,
791
110M
                                    &conn_close_enc_level))
792
91.3M
            continue;
793
794
19.0M
        if (!txp_pkt_init(&pkt[enc_level], txp, enc_level, archetype,
795
19.0M
                          running_total))
796
            /*
797
             * If this fails this is not a fatal error - it means the geometry
798
             * planning determined there was not enough space for another
799
             * packet. So just proceed with what we've already planned for.
800
             */
801
22
            break;
802
803
19.0M
        rc = txp_generate_for_el(txp, &pkt[enc_level],
804
19.0M
                                 conn_close_enc_level == enc_level);
805
19.0M
        if (rc != TXP_ERR_SUCCESS)
806
0
            goto out;
807
808
19.0M
        if (pkt[enc_level].force_pad)
809
            /*
810
             * txp_generate_for_el emitted a frame which forces packet padding.
811
             */
812
3.90k
            need_padding = 1;
813
814
19.0M
        pkt[enc_level].geom.hwm = running_total
815
19.0M
            + pkt[enc_level].h.bytes_appended
816
19.0M
            + pkt[enc_level].geom.pkt_overhead;
817
19.0M
    }
818
819
    /* 3. Packet Adjustment */
820
27.5M
    if (pkt[QUIC_ENC_LEVEL_INITIAL].h_valid
821
27.5M
        && pkt[QUIC_ENC_LEVEL_INITIAL].h.bytes_appended > 0)
822
        /*
823
         * We have an Initial packet in this datagram, so we need to make sure
824
         * the total size of the datagram is adequate.
825
         */
826
525k
        need_padding = 1;
827
828
27.5M
    if (need_padding) {
829
528k
        size_t total_dgram_size = 0;
830
528k
        const size_t min_dpl = QUIC_MIN_INITIAL_DGRAM_LEN;
831
528k
        uint32_t pad_el = QUIC_ENC_LEVEL_NUM;
832
833
528k
        for (enc_level = QUIC_ENC_LEVEL_INITIAL;
834
2.64M
             enc_level < QUIC_ENC_LEVEL_NUM;
835
2.11M
             ++enc_level)
836
2.11M
            if (pkt[enc_level].h_valid && pkt[enc_level].h.bytes_appended > 0) {
837
590k
                if (pad_el == QUIC_ENC_LEVEL_NUM
838
                    /*
839
                     * We might not be able to add padding, for example if we
840
                     * are using the ACK_ONLY archetype.
841
                     */
842
590k
                    && pkt[enc_level].geom.adata.allow_padding
843
590k
                    && !pkt[enc_level].h.done_implicit)
844
266k
                    pad_el = enc_level;
845
846
590k
                txp_pkt_postgen_update_pkt_overhead(&pkt[enc_level], txp);
847
590k
                total_dgram_size += pkt[enc_level].geom.pkt_overhead
848
590k
                    + pkt[enc_level].h.bytes_appended;
849
590k
            }
850
851
528k
        if (pad_el != QUIC_ENC_LEVEL_NUM && total_dgram_size < min_dpl) {
852
265k
            size_t deficit = min_dpl - total_dgram_size;
853
854
265k
            if (!txp_pkt_append_padding(&pkt[pad_el], txp, deficit))
855
1
                goto out;
856
857
265k
            total_dgram_size += deficit;
858
859
            /*
860
             * Padding frames make a packet ineligible for being a non-inflight
861
             * packet.
862
             */
863
265k
            pkt[pad_el].tpkt->ackm_pkt.is_inflight = 1;
864
265k
        }
865
866
        /*
867
         * If we have failed to make a datagram of adequate size, for example
868
         * because we have a padding requirement but are using the ACK_ONLY
869
         * archetype (because we are CC limited), which precludes us from
870
         * sending padding, give up on generating the datagram - there is
871
         * nothing we can do.
872
         */
873
528k
        if (total_dgram_size < min_dpl) {
874
262k
            res = 1;
875
262k
            goto out;
876
262k
        }
877
528k
    }
878
879
    /* 4. Commit */
880
27.3M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
881
136M
         enc_level < QUIC_ENC_LEVEL_NUM;
882
109M
         ++enc_level) {
883
884
109M
        if (!pkt[enc_level].h_valid)
885
            /* Did not attempt to generate a packet for this EL. */
886
90.6M
            continue;
887
888
18.5M
        if (pkt[enc_level].h.bytes_appended == 0)
889
            /* Nothing was generated for this EL, so skip. */
890
18.1M
            continue;
891
892
469k
        rc = txp_pkt_commit(txp, &pkt[enc_level], archetype,
893
469k
                            &txpim_pkt_reffed);
894
469k
        if (rc) {
895
469k
            status->sent_ack_eliciting
896
469k
                = status->sent_ack_eliciting
897
469k
                || pkt[enc_level].tpkt->ackm_pkt.is_ack_eliciting;
898
899
469k
            if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE)
900
105k
                status->sent_handshake
901
105k
                    = (pkt[enc_level].h_valid
902
105k
                       && pkt[enc_level].h.bytes_appended > 0);
903
469k
        }
904
905
469k
        if (txpim_pkt_reffed)
906
469k
            pkt[enc_level].tpkt = NULL; /* don't free */
907
908
469k
        if (!rc)
909
0
            goto out;
910
911
469k
        ++pkts_done;
912
469k
    }
913
914
    /* Flush & Cleanup */
915
27.3M
    res = 1;
916
27.5M
out:
917
27.5M
    ossl_qtx_finish_dgram(txp->args.qtx);
918
919
27.5M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
920
137M
         enc_level < QUIC_ENC_LEVEL_NUM;
921
110M
         ++enc_level)
922
110M
        txp_pkt_cleanup(&pkt[enc_level], txp);
923
924
27.5M
    status->sent_pkt = pkts_done;
925
926
27.5M
    return res;
927
27.3M
}
928
929
static const struct archetype_data archetypes[QUIC_ENC_LEVEL_NUM][TX_PACKETISER_ARCHETYPE_NUM] = {
930
    /* EL 0(INITIAL) */
931
    {
932
        /* EL 0(INITIAL) - Archetype 0(NORMAL) */
933
        {
934
            /*allow_ack                       =*/ 1,
935
            /*allow_ping                      =*/ 1,
936
            /*allow_crypto                    =*/ 1,
937
            /*allow_handshake_done            =*/ 0,
938
            /*allow_path_challenge            =*/ 0,
939
            /*allow_path_response             =*/ 0,
940
            /*allow_new_conn_id               =*/ 0,
941
            /*allow_retire_conn_id            =*/ 0,
942
            /*allow_stream_rel                =*/ 0,
943
            /*allow_conn_fc                   =*/ 0,
944
            /*allow_conn_close                =*/ 1,
945
            /*allow_cfq_other                 =*/ 0,
946
            /*allow_new_token                 =*/ 0,
947
            /*allow_force_ack_eliciting       =*/ 1,
948
            /*allow_padding                   =*/ 1,
949
            /*require_ack_eliciting           =*/ 0,
950
            /*bypass_cc                       =*/ 0,
951
        },
952
        /* EL 0(INITIAL) - Archetype 1(PROBE) */
953
        {
954
            /*allow_ack                       =*/ 1,
955
            /*allow_ping                      =*/ 1,
956
            /*allow_crypto                    =*/ 1,
957
            /*allow_handshake_done            =*/ 0,
958
            /*allow_path_challenge            =*/ 0,
959
            /*allow_path_response             =*/ 0,
960
            /*allow_new_conn_id               =*/ 0,
961
            /*allow_retire_conn_id            =*/ 0,
962
            /*allow_stream_rel                =*/ 0,
963
            /*allow_conn_fc                   =*/ 0,
964
            /*allow_conn_close                =*/ 1,
965
            /*allow_cfq_other                 =*/ 0,
966
            /*allow_new_token                 =*/ 0,
967
            /*allow_force_ack_eliciting       =*/ 1,
968
            /*allow_padding                   =*/ 1,
969
            /*require_ack_eliciting           =*/ 1,
970
            /*bypass_cc                       =*/ 1,
971
        },
972
        /* EL 0(INITIAL) - Archetype 2(ACK_ONLY) */
973
        {
974
            /*allow_ack                       =*/ 1,
975
            /*allow_ping                      =*/ 0,
976
            /*allow_crypto                    =*/ 0,
977
            /*allow_handshake_done            =*/ 0,
978
            /*allow_path_challenge            =*/ 0,
979
            /*allow_path_response             =*/ 0,
980
            /*allow_new_conn_id               =*/ 0,
981
            /*allow_retire_conn_id            =*/ 0,
982
            /*allow_stream_rel                =*/ 0,
983
            /*allow_conn_fc                   =*/ 0,
984
            /*allow_conn_close                =*/ 0,
985
            /*allow_cfq_other                 =*/ 0,
986
            /*allow_new_token                 =*/ 0,
987
            /*allow_force_ack_eliciting       =*/ 1,
988
            /*allow_padding                   =*/ 0,
989
            /*require_ack_eliciting           =*/ 0,
990
            /*bypass_cc                       =*/ 1,
991
        },
992
    },
993
    /* EL 1(HANDSHAKE) */
994
    {
995
        /* EL 1(HANDSHAKE) - Archetype 0(NORMAL) */
996
        {
997
            /*allow_ack                       =*/ 1,
998
            /*allow_ping                      =*/ 1,
999
            /*allow_crypto                    =*/ 1,
1000
            /*allow_handshake_done            =*/ 0,
1001
            /*allow_path_challenge            =*/ 0,
1002
            /*allow_path_response             =*/ 0,
1003
            /*allow_new_conn_id               =*/ 0,
1004
            /*allow_retire_conn_id            =*/ 0,
1005
            /*allow_stream_rel                =*/ 0,
1006
            /*allow_conn_fc                   =*/ 0,
1007
            /*allow_conn_close                =*/ 1,
1008
            /*allow_cfq_other                 =*/ 0,
1009
            /*allow_new_token                 =*/ 0,
1010
            /*allow_force_ack_eliciting       =*/ 1,
1011
            /*allow_padding                   =*/ 1,
1012
            /*require_ack_eliciting           =*/ 0,
1013
            /*bypass_cc                       =*/ 0,
1014
        },
1015
        /* EL 1(HANDSHAKE) - Archetype 1(PROBE) */
1016
        {
1017
            /*allow_ack                       =*/ 1,
1018
            /*allow_ping                      =*/ 1,
1019
            /*allow_crypto                    =*/ 1,
1020
            /*allow_handshake_done            =*/ 0,
1021
            /*allow_path_challenge            =*/ 0,
1022
            /*allow_path_response             =*/ 0,
1023
            /*allow_new_conn_id               =*/ 0,
1024
            /*allow_retire_conn_id            =*/ 0,
1025
            /*allow_stream_rel                =*/ 0,
1026
            /*allow_conn_fc                   =*/ 0,
1027
            /*allow_conn_close                =*/ 1,
1028
            /*allow_cfq_other                 =*/ 0,
1029
            /*allow_new_token                 =*/ 0,
1030
            /*allow_force_ack_eliciting       =*/ 1,
1031
            /*allow_padding                   =*/ 1,
1032
            /*require_ack_eliciting           =*/ 1,
1033
            /*bypass_cc                       =*/ 1,
1034
        },
1035
        /* EL 1(HANDSHAKE) - Archetype 2(ACK_ONLY) */
1036
        {
1037
            /*allow_ack                       =*/ 1,
1038
            /*allow_ping                      =*/ 0,
1039
            /*allow_crypto                    =*/ 0,
1040
            /*allow_handshake_done            =*/ 0,
1041
            /*allow_path_challenge            =*/ 0,
1042
            /*allow_path_response             =*/ 0,
1043
            /*allow_new_conn_id               =*/ 0,
1044
            /*allow_retire_conn_id            =*/ 0,
1045
            /*allow_stream_rel                =*/ 0,
1046
            /*allow_conn_fc                   =*/ 0,
1047
            /*allow_conn_close                =*/ 0,
1048
            /*allow_cfq_other                 =*/ 0,
1049
            /*allow_new_token                 =*/ 0,
1050
            /*allow_force_ack_eliciting       =*/ 1,
1051
            /*allow_padding                   =*/ 0,
1052
            /*require_ack_eliciting           =*/ 0,
1053
            /*bypass_cc                       =*/ 1,
1054
        },
1055
    },
1056
    /* EL 2(0RTT) */
1057
    {
1058
        /* EL 2(0RTT) - Archetype 0(NORMAL) */
1059
        {
1060
            /*allow_ack                       =*/ 0,
1061
            /*allow_ping                      =*/ 1,
1062
            /*allow_crypto                    =*/ 0,
1063
            /*allow_handshake_done            =*/ 0,
1064
            /*allow_path_challenge            =*/ 0,
1065
            /*allow_path_response             =*/ 0,
1066
            /*allow_new_conn_id               =*/ 1,
1067
            /*allow_retire_conn_id            =*/ 1,
1068
            /*allow_stream_rel                =*/ 1,
1069
            /*allow_conn_fc                   =*/ 1,
1070
            /*allow_conn_close                =*/ 1,
1071
            /*allow_cfq_other                 =*/ 0,
1072
            /*allow_new_token                 =*/ 0,
1073
            /*allow_force_ack_eliciting       =*/ 0,
1074
            /*allow_padding                   =*/ 1,
1075
            /*require_ack_eliciting           =*/ 0,
1076
            /*bypass_cc                       =*/ 0,
1077
        },
1078
        /* EL 2(0RTT) - Archetype 1(PROBE) */
1079
        {
1080
            /*allow_ack                       =*/ 0,
1081
            /*allow_ping                      =*/ 1,
1082
            /*allow_crypto                    =*/ 0,
1083
            /*allow_handshake_done            =*/ 0,
1084
            /*allow_path_challenge            =*/ 0,
1085
            /*allow_path_response             =*/ 0,
1086
            /*allow_new_conn_id               =*/ 1,
1087
            /*allow_retire_conn_id            =*/ 1,
1088
            /*allow_stream_rel                =*/ 1,
1089
            /*allow_conn_fc                   =*/ 1,
1090
            /*allow_conn_close                =*/ 1,
1091
            /*allow_cfq_other                 =*/ 0,
1092
            /*allow_new_token                 =*/ 0,
1093
            /*allow_force_ack_eliciting       =*/ 0,
1094
            /*allow_padding                   =*/ 1,
1095
            /*require_ack_eliciting           =*/ 1,
1096
            /*bypass_cc                       =*/ 1,
1097
        },
1098
        /* EL 2(0RTT) - Archetype 2(ACK_ONLY) */
1099
        {
1100
            /*allow_ack                       =*/ 0,
1101
            /*allow_ping                      =*/ 0,
1102
            /*allow_crypto                    =*/ 0,
1103
            /*allow_handshake_done            =*/ 0,
1104
            /*allow_path_challenge            =*/ 0,
1105
            /*allow_path_response             =*/ 0,
1106
            /*allow_new_conn_id               =*/ 0,
1107
            /*allow_retire_conn_id            =*/ 0,
1108
            /*allow_stream_rel                =*/ 0,
1109
            /*allow_conn_fc                   =*/ 0,
1110
            /*allow_conn_close                =*/ 0,
1111
            /*allow_cfq_other                 =*/ 0,
1112
            /*allow_new_token                 =*/ 0,
1113
            /*allow_force_ack_eliciting       =*/ 0,
1114
            /*allow_padding                   =*/ 0,
1115
            /*require_ack_eliciting           =*/ 0,
1116
            /*bypass_cc                       =*/ 1,
1117
        },
1118
    },
1119
    /* EL 3(1RTT) */
1120
    {
1121
        /* EL 3(1RTT) - Archetype 0(NORMAL) */
1122
        {
1123
            /*allow_ack                       =*/ 1,
1124
            /*allow_ping                      =*/ 1,
1125
            /*allow_crypto                    =*/ 1,
1126
            /*allow_handshake_done            =*/ 1,
1127
            /*allow_path_challenge            =*/ 0,
1128
            /*allow_path_response             =*/ 1,
1129
            /*allow_new_conn_id               =*/ 1,
1130
            /*allow_retire_conn_id            =*/ 1,
1131
            /*allow_stream_rel                =*/ 1,
1132
            /*allow_conn_fc                   =*/ 1,
1133
            /*allow_conn_close                =*/ 1,
1134
            /*allow_cfq_other                 =*/ 1,
1135
            /*allow_new_token                 =*/ 1,
1136
            /*allow_force_ack_eliciting       =*/ 1,
1137
            /*allow_padding                   =*/ 1,
1138
            /*require_ack_eliciting           =*/ 0,
1139
            /*bypass_cc                       =*/ 0,
1140
        },
1141
        /* EL 3(1RTT) - Archetype 1(PROBE) */
1142
        {
1143
            /*allow_ack                       =*/ 1,
1144
            /*allow_ping                      =*/ 1,
1145
            /*allow_crypto                    =*/ 1,
1146
            /*allow_handshake_done            =*/ 1,
1147
            /*allow_path_challenge            =*/ 0,
1148
            /*allow_path_response             =*/ 1,
1149
            /*allow_new_conn_id               =*/ 1,
1150
            /*allow_retire_conn_id            =*/ 1,
1151
            /*allow_stream_rel                =*/ 1,
1152
            /*allow_conn_fc                   =*/ 1,
1153
            /*allow_conn_close                =*/ 1,
1154
            /*allow_cfq_other                 =*/ 1,
1155
            /*allow_new_token                 =*/ 1,
1156
            /*allow_force_ack_eliciting       =*/ 1,
1157
            /*allow_padding                   =*/ 1,
1158
            /*require_ack_eliciting           =*/ 1,
1159
            /*bypass_cc                       =*/ 1,
1160
        },
1161
        /* EL 3(1RTT) - Archetype 2(ACK_ONLY) */
1162
        {
1163
            /*allow_ack                       =*/ 1,
1164
            /*allow_ping                      =*/ 0,
1165
            /*allow_crypto                    =*/ 0,
1166
            /*allow_handshake_done            =*/ 0,
1167
            /*allow_path_challenge            =*/ 0,
1168
            /*allow_path_response             =*/ 0,
1169
            /*allow_new_conn_id               =*/ 0,
1170
            /*allow_retire_conn_id            =*/ 0,
1171
            /*allow_stream_rel                =*/ 0,
1172
            /*allow_conn_fc                   =*/ 0,
1173
            /*allow_conn_close                =*/ 0,
1174
            /*allow_cfq_other                 =*/ 0,
1175
            /*allow_new_token                 =*/ 0,
1176
            /*allow_force_ack_eliciting       =*/ 1,
1177
            /*allow_padding                   =*/ 0,
1178
            /*require_ack_eliciting           =*/ 0,
1179
            /*bypass_cc                       =*/ 1,
1180
        }
1181
    }
1182
};
1183
1184
static int txp_get_archetype_data(uint32_t enc_level,
1185
                                  uint32_t archetype,
1186
                                  struct archetype_data *a)
1187
54.2M
{
1188
54.2M
    if (enc_level >= QUIC_ENC_LEVEL_NUM
1189
54.2M
        || archetype >= TX_PACKETISER_ARCHETYPE_NUM)
1190
0
        return 0;
1191
1192
    /* No need to avoid copying this as it should not exceed one int in size. */
1193
54.2M
    *a = archetypes[enc_level][archetype];
1194
54.2M
    return 1;
1195
54.2M
}
1196
1197
static int txp_determine_geometry(OSSL_QUIC_TX_PACKETISER *txp,
1198
                                  uint32_t archetype,
1199
                                  uint32_t enc_level,
1200
                                  size_t running_total,
1201
                                  QUIC_PKT_HDR *phdr,
1202
                                  struct txp_pkt_geom *geom)
1203
19.0M
{
1204
19.0M
    size_t mdpl, cmpl, hdr_len;
1205
1206
    /* Get information about packet archetype. */
1207
19.0M
    if (!txp_get_archetype_data(enc_level, archetype, &geom->adata))
1208
0
       return 0;
1209
1210
    /* Assemble packet header. */
1211
19.0M
    phdr->type          = ossl_quic_enc_level_to_pkt_type(enc_level);
1212
19.0M
    phdr->spin_bit      = 0;
1213
19.0M
    phdr->pn_len        = txp_determine_pn_len(txp);
1214
19.0M
    phdr->partial       = 0;
1215
19.0M
    phdr->fixed         = 1;
1216
19.0M
    phdr->reserved      = 0;
1217
19.0M
    phdr->version       = QUIC_VERSION_1;
1218
19.0M
    phdr->dst_conn_id   = txp->args.cur_dcid;
1219
19.0M
    phdr->src_conn_id   = txp->args.cur_scid;
1220
1221
    /*
1222
     * We need to know the length of the payload to get an accurate header
1223
     * length for non-1RTT packets, because the Length field found in
1224
     * Initial/Handshake/0-RTT packets uses a variable-length encoding. However,
1225
     * we don't have a good idea of the length of our payload, because the
1226
     * length of the payload depends on the room in the datagram after fitting
1227
     * the header, which depends on the size of the header.
1228
     *
1229
     * In general, it does not matter if a packet is slightly shorter (because
1230
     * e.g. we predicted use of a 2-byte length field, but ended up only needing
1231
     * a 1-byte length field). However this does matter for Initial packets
1232
     * which must be at least 1200 bytes, which is also the assumed default MTU;
1233
     * therefore in many cases Initial packets will be padded to 1200 bytes,
1234
     * which means if we overestimated the header size, we will be short by a
1235
     * few bytes and the server will ignore the packet for being too short. In
1236
     * this case, however, such packets always *will* be padded to meet 1200
1237
     * bytes, which requires a 2-byte length field, so we don't actually need to
1238
     * worry about this. Thus we estimate the header length assuming a 2-byte
1239
     * length field here, which should in practice work well in all cases.
1240
     */
1241
19.0M
    phdr->len           = OSSL_QUIC_VLINT_2B_MAX - phdr->pn_len;
1242
1243
19.0M
    if (enc_level == QUIC_ENC_LEVEL_INITIAL) {
1244
11.7M
        phdr->token     = txp->initial_token;
1245
11.7M
        phdr->token_len = txp->initial_token_len;
1246
11.7M
    } else {
1247
7.24M
        phdr->token     = NULL;
1248
7.24M
        phdr->token_len = 0;
1249
7.24M
    }
1250
1251
19.0M
    hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(phdr->dst_conn_id.id_len,
1252
19.0M
                                                     phdr);
1253
19.0M
    if (hdr_len == 0)
1254
0
        return 0;
1255
1256
    /* MDPL: Maximum datagram payload length. */
1257
19.0M
    mdpl = txp_get_mdpl(txp);
1258
1259
    /*
1260
     * CMPL: Maximum encoded packet size we can put into this datagram given any
1261
     * previous packets coalesced into it.
1262
     */
1263
19.0M
    if (running_total > mdpl)
1264
        /* Should not be possible, but if it happens: */
1265
0
        cmpl = 0;
1266
19.0M
    else
1267
19.0M
        cmpl = mdpl - running_total;
1268
1269
    /* CMPPL: Maximum amount we can put into the current packet payload */
1270
19.0M
    if (!txp_determine_ppl_from_pl(txp, cmpl, enc_level, hdr_len, &geom->cmppl))
1271
22
        return 0;
1272
1273
19.0M
    geom->cmpl                  = cmpl;
1274
19.0M
    geom->pkt_overhead          = cmpl - geom->cmppl;
1275
19.0M
    geom->archetype             = archetype;
1276
19.0M
    return 1;
1277
19.0M
}
1278
1279
static uint32_t txp_determine_archetype(OSSL_QUIC_TX_PACKETISER *txp,
1280
                                        uint64_t cc_limit)
1281
27.5M
{
1282
27.5M
    OSSL_ACKM_PROBE_INFO *probe_info
1283
27.5M
        = ossl_ackm_get0_probe_request(txp->args.ackm);
1284
27.5M
    uint32_t pn_space;
1285
1286
    /*
1287
     * If ACKM has requested probe generation (e.g. due to PTO), we generate a
1288
     * Probe-archetype packet. Actually, we determine archetype on a
1289
     * per-datagram basis, so if any EL wants a probe, do a pass in which
1290
     * we try and generate a probe (if needed) for all ELs.
1291
     */
1292
27.5M
    if (probe_info->anti_deadlock_initial > 0
1293
27.5M
        || probe_info->anti_deadlock_handshake > 0)
1294
1.63k
        return TX_PACKETISER_ARCHETYPE_PROBE;
1295
1296
27.5M
    for (pn_space = QUIC_PN_SPACE_INITIAL;
1297
109M
         pn_space < QUIC_PN_SPACE_NUM;
1298
82.3M
         ++pn_space)
1299
82.4M
        if (probe_info->pto[pn_space] > 0)
1300
142k
            return TX_PACKETISER_ARCHETYPE_PROBE;
1301
1302
    /*
1303
     * If we are out of CC budget, we cannot send a normal packet,
1304
     * but we can do an ACK-only packet (potentially, if we
1305
     * want to send an ACK).
1306
     */
1307
27.4M
    if (cc_limit == 0)
1308
22.7M
        return TX_PACKETISER_ARCHETYPE_ACK_ONLY;
1309
1310
    /* All other packets. */
1311
4.65M
    return TX_PACKETISER_ARCHETYPE_NORMAL;
1312
27.4M
}
1313
1314
static int txp_should_try_staging(OSSL_QUIC_TX_PACKETISER *txp,
1315
                                  uint32_t enc_level,
1316
                                  uint32_t archetype,
1317
                                  uint64_t cc_limit,
1318
                                  uint32_t *conn_close_enc_level)
1319
110M
{
1320
110M
    struct archetype_data a;
1321
110M
    uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
1322
110M
    QUIC_CFQ_ITEM *cfq_item;
1323
1324
110M
    if (!ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level))
1325
75.5M
        return 0;
1326
1327
34.7M
    if (!txp_get_archetype_data(enc_level, archetype, &a))
1328
0
        return 0;
1329
1330
34.7M
    if (!a.bypass_cc && cc_limit == 0)
1331
        /* CC not allowing us to send. */
1332
0
        return 0;
1333
1334
    /*
1335
     * We can produce CONNECTION_CLOSE frames on any EL in principle, which
1336
     * means we need to choose which EL we would prefer to use. After a
1337
     * connection is fully established we have only one provisioned EL and this
1338
     * is a non-issue. Where multiple ELs are provisioned, it is possible the
1339
     * peer does not have the keys for the EL yet, which suggests in general it
1340
     * is preferable to use the lowest EL which is still provisioned.
1341
     *
1342
     * However (RFC 9000 s. 10.2.3 & 12.5) we are also required to not send
1343
     * application CONNECTION_CLOSE frames in non-1-RTT ELs, so as to not
1344
     * potentially leak application data on a connection which has yet to be
1345
     * authenticated. Thus when we have an application CONNECTION_CLOSE frame
1346
     * queued and need to send it on a non-1-RTT EL, we have to convert it
1347
     * into a transport CONNECTION_CLOSE frame which contains no application
1348
     * data. Since this loses information, it suggests we should use the 1-RTT
1349
     * EL to avoid this if possible, even if a lower EL is also available.
1350
     *
1351
     * At the same time, just because we have the 1-RTT EL provisioned locally
1352
     * does not necessarily mean the peer does, for example if a handshake
1353
     * CRYPTO frame has been lost. It is fairly important that CONNECTION_CLOSE
1354
     * is signalled in a way we know our peer can decrypt, as we stop processing
1355
     * connection retransmission logic for real after connection close and
1356
     * simply 'blindly' retransmit the same CONNECTION_CLOSE frame.
1357
     *
1358
     * This is not a major concern for clients, since if a client has a 1-RTT EL
1359
     * provisioned the server is guaranteed to also have a 1-RTT EL provisioned.
1360
     *
1361
     * TODO(QUIC SERVER): Revisit this when server support is added.
1362
     */
1363
34.7M
    if (*conn_close_enc_level > enc_level
1364
34.7M
        && *conn_close_enc_level != QUIC_ENC_LEVEL_1RTT)
1365
27.5M
        *conn_close_enc_level = enc_level;
1366
1367
    /* Do we need to send a PTO probe? */
1368
34.7M
    if (a.allow_force_ack_eliciting) {
1369
34.7M
        OSSL_ACKM_PROBE_INFO *probe_info
1370
34.7M
            = ossl_ackm_get0_probe_request(txp->args.ackm);
1371
1372
34.7M
        if ((enc_level == QUIC_ENC_LEVEL_INITIAL
1373
34.7M
             && probe_info->anti_deadlock_initial > 0)
1374
34.7M
            || (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
1375
34.7M
                && probe_info->anti_deadlock_handshake > 0)
1376
34.7M
            || probe_info->pto[pn_space] > 0)
1377
144k
            return 1;
1378
34.7M
    }
1379
1380
    /* Does the crypto stream for this EL want to produce anything? */
1381
34.6M
    if (a.allow_crypto && sstream_is_pending(txp->args.crypto[pn_space]))
1382
27.8k
        return 1;
1383
1384
    /* Does the ACKM for this PN space want to produce anything? */
1385
34.6M
    if (a.allow_ack && (ossl_ackm_is_ack_desired(txp->args.ackm, pn_space)
1386
34.6M
                        || (txp->want_ack & (1UL << pn_space)) != 0))
1387
2.00M
        return 1;
1388
1389
    /* Do we need to force emission of an ACK-eliciting packet? */
1390
32.6M
    if (a.allow_force_ack_eliciting
1391
32.6M
        && (txp->force_ack_eliciting & (1UL << pn_space)) != 0)
1392
16.8M
        return 1;
1393
1394
    /* Does the connection-level RXFC want to produce a frame? */
1395
15.7M
    if (a.allow_conn_fc && (txp->want_max_data
1396
2.08M
        || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0)))
1397
0
        return 1;
1398
1399
    /* Do we want to produce a MAX_STREAMS frame? */
1400
15.7M
    if (a.allow_conn_fc
1401
15.7M
        && (txp->want_max_streams_bidi
1402
2.08M
            || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc,
1403
2.08M
                                              0)
1404
2.08M
            || txp->want_max_streams_uni
1405
2.08M
            || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc,
1406
2.08M
                                              0)))
1407
0
        return 1;
1408
1409
    /* Do we want to produce a HANDSHAKE_DONE frame? */
1410
15.7M
    if (a.allow_handshake_done && txp->want_handshake_done)
1411
0
        return 1;
1412
1413
    /* Do we want to produce a CONNECTION_CLOSE frame? */
1414
15.7M
    if (a.allow_conn_close && txp->want_conn_close &&
1415
15.7M
        *conn_close_enc_level == enc_level)
1416
        /*
1417
         * This is a bit of a special case since CONNECTION_CLOSE can appear in
1418
         * most packet types, and when we decide we want to send it this status
1419
         * isn't tied to a specific EL. So if we want to send it, we send it
1420
         * only on the lowest non-dropped EL.
1421
         */
1422
2.60k
        return 1;
1423
1424
    /* Does the CFQ have any frames queued for this PN space? */
1425
15.7M
    if (enc_level != QUIC_ENC_LEVEL_0RTT)
1426
15.7M
        for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
1427
19.8M
             cfq_item != NULL;
1428
15.7M
             cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
1429
4.11M
            uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
1430
1431
4.11M
            switch (frame_type) {
1432
0
            case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1433
0
                if (a.allow_new_conn_id)
1434
0
                    return 1;
1435
0
                break;
1436
97.0k
            case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1437
97.0k
                if (a.allow_retire_conn_id)
1438
78
                    return 1;
1439
96.9k
                break;
1440
96.9k
            case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1441
0
                if (a.allow_new_token)
1442
0
                    return 1;
1443
0
                break;
1444
4.01M
            case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
1445
4.01M
                if (a.allow_path_response)
1446
283
                    return 1;
1447
4.01M
                break;
1448
4.01M
            default:
1449
0
                if (a.allow_cfq_other)
1450
0
                    return 1;
1451
0
                break;
1452
4.11M
            }
1453
4.11M
       }
1454
1455
15.7M
    if (a.allow_stream_rel && txp->handshake_complete) {
1456
2.08M
        QUIC_STREAM_ITER it;
1457
1458
        /* If there are any active streams, 0/1-RTT wants to produce a packet.
1459
         * Whether a stream is on the active list is required to be precise
1460
         * (i.e., a stream is never on the active list if we cannot produce a
1461
         * frame for it), and all stream-related frames are governed by
1462
         * a.allow_stream_rel (i.e., if we can send one type of stream-related
1463
         * frame, we can send any of them), so we don't need to inspect
1464
         * individual streams on the active list, just confirm that the active
1465
         * list is non-empty.
1466
         */
1467
2.08M
        ossl_quic_stream_iter_init(&it, txp->args.qsm, 0);
1468
2.08M
        if (it.stream != NULL)
1469
2.06k
            return 1;
1470
2.08M
    }
1471
1472
15.7M
    return 0;
1473
15.7M
}
1474
1475
static int sstream_is_pending(QUIC_SSTREAM *sstream)
1476
5.07M
{
1477
5.07M
    OSSL_QUIC_FRAME_STREAM hdr;
1478
5.07M
    OSSL_QTX_IOVEC iov[2];
1479
5.07M
    size_t num_iov = OSSL_NELEM(iov);
1480
1481
5.07M
    return ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov);
1482
5.07M
}
1483
1484
/* Determine how many bytes we should use for the encoded PN. */
1485
static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp)
1486
19.0M
{
1487
19.0M
    return 4; /* TODO(QUIC FUTURE) */
1488
19.0M
}
1489
1490
/* Determine plaintext packet payload length from payload length. */
1491
static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
1492
                                     size_t pl,
1493
                                     uint32_t enc_level,
1494
                                     size_t hdr_len,
1495
                                     size_t *r)
1496
19.0M
{
1497
19.0M
    if (pl < hdr_len)
1498
15
        return 0;
1499
1500
19.0M
    pl -= hdr_len;
1501
1502
19.0M
    if (!ossl_qtx_calculate_plaintext_payload_len(txp->args.qtx, enc_level,
1503
19.0M
                                                  pl, &pl))
1504
7
        return 0;
1505
1506
19.0M
    *r = pl;
1507
19.0M
    return 1;
1508
19.0M
}
1509
1510
static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp)
1511
19.0M
{
1512
19.0M
    return ossl_qtx_get_mdpl(txp->args.qtx);
1513
19.0M
}
1514
1515
static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
1516
                                       void *arg)
1517
49.2k
{
1518
49.2k
    OSSL_QUIC_TX_PACKETISER *txp = arg;
1519
49.2k
    QUIC_STREAM *s;
1520
1521
49.2k
    if (stream_id == UINT64_MAX)
1522
41.9k
        return txp->args.crypto[pn_space];
1523
1524
7.29k
    s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1525
7.29k
    if (s == NULL)
1526
0
        return NULL;
1527
1528
7.29k
    return s->sstream;
1529
7.29k
}
1530
1531
static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
1532
                            QUIC_TXPIM_PKT *pkt, void *arg)
1533
7.66k
{
1534
7.66k
    OSSL_QUIC_TX_PACKETISER *txp = arg;
1535
1536
7.66k
    switch (frame_type) {
1537
0
        case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1538
0
            txp->want_handshake_done = 1;
1539
0
            break;
1540
0
        case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1541
0
            txp->want_max_data = 1;
1542
0
            break;
1543
0
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1544
0
            txp->want_max_streams_bidi = 1;
1545
0
            break;
1546
0
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1547
0
            txp->want_max_streams_uni = 1;
1548
0
            break;
1549
5.19k
        case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1550
5.19k
            txp->want_ack |= (1UL << pkt->ackm_pkt.pkt_space);
1551
5.19k
            break;
1552
2.46k
        case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1553
2.46k
            {
1554
2.46k
                QUIC_STREAM *s
1555
2.46k
                    = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1556
1557
2.46k
                if (s == NULL)
1558
1.72k
                    return;
1559
1560
747
                s->want_max_stream_data = 1;
1561
747
                ossl_quic_stream_map_update_state(txp->args.qsm, s);
1562
747
            }
1563
0
            break;
1564
0
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1565
0
            {
1566
0
                QUIC_STREAM *s
1567
0
                    = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1568
1569
0
                if (s == NULL)
1570
0
                    return;
1571
1572
0
                ossl_quic_stream_map_schedule_stop_sending(txp->args.qsm, s);
1573
0
            }
1574
0
            break;
1575
0
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1576
0
            {
1577
0
                QUIC_STREAM *s
1578
0
                    = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1579
1580
0
                if (s == NULL)
1581
0
                    return;
1582
1583
0
                s->want_reset_stream = 1;
1584
0
                ossl_quic_stream_map_update_state(txp->args.qsm, s);
1585
0
            }
1586
0
            break;
1587
0
        default:
1588
0
            assert(0);
1589
0
            break;
1590
7.66k
    }
1591
7.66k
}
1592
1593
static int txp_need_ping(OSSL_QUIC_TX_PACKETISER *txp,
1594
                         uint32_t pn_space,
1595
                         const struct archetype_data *adata)
1596
37.9M
{
1597
37.9M
    return adata->allow_ping
1598
37.9M
        && (adata->require_ack_eliciting
1599
899k
            || (txp->force_ack_eliciting & (1UL << pn_space)) != 0);
1600
37.9M
}
1601
1602
static int txp_pkt_init(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp,
1603
                        uint32_t enc_level, uint32_t archetype,
1604
                        size_t running_total)
1605
19.0M
{
1606
19.0M
    uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
1607
1608
19.0M
    if (!txp_determine_geometry(txp, archetype, enc_level,
1609
19.0M
                                running_total, &pkt->phdr, &pkt->geom))
1610
22
        return 0;
1611
1612
    /*
1613
     * Initialise TX helper. If we must be ACK eliciting, reserve 1 byte for
1614
     * PING.
1615
     */
1616
19.0M
    if (!tx_helper_init(&pkt->h, txp, enc_level,
1617
19.0M
                        pkt->geom.cmppl,
1618
19.0M
                        txp_need_ping(txp, pn_space, &pkt->geom.adata) ? 1 : 0))
1619
0
        return 0;
1620
1621
19.0M
    pkt->h_valid            = 1;
1622
19.0M
    pkt->tpkt               = NULL;
1623
19.0M
    pkt->stream_head        = NULL;
1624
19.0M
    pkt->force_pad          = 0;
1625
19.0M
    return 1;
1626
19.0M
}
1627
1628
static void txp_pkt_cleanup(struct txp_pkt *pkt, OSSL_QUIC_TX_PACKETISER *txp)
1629
110M
{
1630
110M
    if (!pkt->h_valid)
1631
91.3M
        return;
1632
1633
19.0M
    tx_helper_cleanup(&pkt->h);
1634
19.0M
    pkt->h_valid = 0;
1635
1636
19.0M
    if (pkt->tpkt != NULL) {
1637
18.5M
        ossl_quic_txpim_pkt_release(txp->args.txpim, pkt->tpkt);
1638
18.5M
        pkt->tpkt = NULL;
1639
18.5M
    }
1640
19.0M
}
1641
1642
static int txp_pkt_postgen_update_pkt_overhead(struct txp_pkt *pkt,
1643
                                               OSSL_QUIC_TX_PACKETISER *txp)
1644
590k
{
1645
    /*
1646
     * After we have staged and generated our packets, but before we commit
1647
     * them, it is possible for the estimated packet overhead (packet header +
1648
     * AEAD tag size) to shrink slightly because we generated a short packet
1649
     * whose which can be represented in fewer bytes as a variable-length
1650
     * integer than we were (pessimistically) budgeting for. We need to account
1651
     * for this to ensure that we get our padding calculation exactly right.
1652
     *
1653
     * Update pkt_overhead to be accurate now that we know how much data is
1654
     * going in a packet.
1655
     */
1656
590k
    size_t hdr_len, ciphertext_len;
1657
1658
590k
    if (pkt->h.enc_level == QUIC_ENC_LEVEL_INITIAL)
1659
        /*
1660
         * Don't update overheads for the INITIAL EL - we have not finished
1661
         * appending padding to it and would potentially miscalculate the
1662
         * correct padding if we now update the pkt_overhead field to switch to
1663
         * e.g. a 1-byte length field in the packet header. Since we are padding
1664
         * to QUIC_MIN_INITIAL_DGRAM_LEN which requires a 2-byte length field,
1665
         * this is guaranteed to be moot anyway. See comment in
1666
         * txp_determine_geometry for more information.
1667
         */
1668
525k
        return 1;
1669
1670
65.0k
    if (!ossl_qtx_calculate_ciphertext_payload_len(txp->args.qtx, pkt->h.enc_level,
1671
65.0k
                                                   pkt->h.bytes_appended,
1672
65.0k
                                                   &ciphertext_len))
1673
0
        return 0;
1674
1675
65.0k
    pkt->phdr.len = ciphertext_len;
1676
1677
65.0k
    hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(pkt->phdr.dst_conn_id.id_len,
1678
65.0k
                                                     &pkt->phdr);
1679
1680
65.0k
    pkt->geom.pkt_overhead = hdr_len + ciphertext_len - pkt->h.bytes_appended;
1681
65.0k
    return 1;
1682
65.0k
}
1683
1684
static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
1685
                              QUIC_TXPIM_PKT *pkt, void *arg)
1686
0
{
1687
0
    OSSL_QUIC_TX_PACKETISER *txp = arg;
1688
1689
0
    switch (frame_type) {
1690
0
        case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1691
0
            {
1692
0
                QUIC_STREAM *s
1693
0
                    = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1694
1695
0
                if (s == NULL)
1696
0
                    return;
1697
1698
0
                s->acked_stop_sending = 1;
1699
0
                ossl_quic_stream_map_update_state(txp->args.qsm, s);
1700
0
            }
1701
0
            break;
1702
0
        case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1703
0
            {
1704
0
                QUIC_STREAM *s
1705
0
                    = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1706
1707
0
                if (s == NULL)
1708
0
                    return;
1709
1710
                /*
1711
                 * We must already be in RESET_SENT or RESET_RECVD if we are
1712
                 * here, so we don't need to check state here.
1713
                 */
1714
0
                ossl_quic_stream_map_notify_reset_stream_acked(txp->args.qsm, s);
1715
0
                ossl_quic_stream_map_update_state(txp->args.qsm, s);
1716
0
            }
1717
0
            break;
1718
0
        default:
1719
0
            assert(0);
1720
0
            break;
1721
0
    }
1722
0
}
1723
1724
static int txp_pkt_append_padding(struct txp_pkt *pkt,
1725
                                  OSSL_QUIC_TX_PACKETISER *txp, size_t num_bytes)
1726
265k
{
1727
265k
    WPACKET *wpkt;
1728
1729
265k
    if (num_bytes == 0)
1730
0
        return 1;
1731
1732
265k
    if (!ossl_assert(pkt->h_valid))
1733
0
        return 0;
1734
1735
265k
    if (!ossl_assert(pkt->tpkt != NULL))
1736
0
        return 0;
1737
1738
265k
    wpkt = tx_helper_begin(&pkt->h);
1739
265k
    if (wpkt == NULL)
1740
0
        return 0;
1741
1742
265k
    if (!ossl_quic_wire_encode_padding(wpkt, num_bytes)) {
1743
1
        tx_helper_rollback(&pkt->h);
1744
1
        return 0;
1745
1
    }
1746
1747
265k
    if (!tx_helper_commit(&pkt->h))
1748
0
        return 0;
1749
1750
265k
    pkt->tpkt->ackm_pkt.num_bytes      += num_bytes;
1751
    /* Cannot be non-inflight if we have a PADDING frame */
1752
265k
    pkt->tpkt->ackm_pkt.is_inflight     = 1;
1753
265k
    return 1;
1754
265k
}
1755
1756
static void on_sstream_updated(uint64_t stream_id, void *arg)
1757
13.0k
{
1758
13.0k
    OSSL_QUIC_TX_PACKETISER *txp = arg;
1759
13.0k
    QUIC_STREAM *s;
1760
1761
13.0k
    s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1762
13.0k
    if (s == NULL)
1763
12.2k
        return;
1764
1765
785
    ossl_quic_stream_map_update_state(txp->args.qsm, s);
1766
785
}
1767
1768
/*
1769
 * Returns 1 if we can send that many bytes in closing state, 0 otherwise.
1770
 * Also maintains the bytes sent state if it returns a success.
1771
 */
1772
static int try_commit_conn_close(OSSL_QUIC_TX_PACKETISER *txp, size_t n)
1773
10.5k
{
1774
10.5k
    int res;
1775
1776
    /* We can always send the first connection close frame */
1777
10.5k
    if (txp->closing_bytes_recv == 0)
1778
10.5k
        return 1;
1779
1780
    /*
1781
     * RFC 9000 s. 10.2.1 Closing Connection State:
1782
     *      To avoid being used for an amplification attack, such
1783
     *      endpoints MUST limit the cumulative size of packets it sends
1784
     *      to three times the cumulative size of the packets that are
1785
     *      received and attributed to the connection.
1786
     * and:
1787
     *      An endpoint in the closing state MUST either discard packets
1788
     *      received from an unvalidated address or limit the cumulative
1789
     *      size of packets it sends to an unvalidated address to three
1790
     *      times the size of packets it receives from that address.
1791
     */
1792
0
    res = txp->closing_bytes_xmit + n <= txp->closing_bytes_recv * 3;
1793
1794
    /*
1795
     * Attribute the bytes to the connection, if we are allowed to send them
1796
     * and this isn't the first closing frame.
1797
     */
1798
0
    if (res && txp->closing_bytes_recv != 0)
1799
0
        txp->closing_bytes_xmit += n;
1800
0
    return res;
1801
10.5k
}
1802
1803
void ossl_quic_tx_packetiser_record_received_closing_bytes(
1804
        OSSL_QUIC_TX_PACKETISER *txp, size_t n)
1805
0
{
1806
0
    txp->closing_bytes_recv += n;
1807
0
}
1808
1809
static int txp_generate_pre_token(OSSL_QUIC_TX_PACKETISER *txp,
1810
                                  struct txp_pkt *pkt,
1811
                                  int chosen_for_conn_close,
1812
                                  int *can_be_non_inflight)
1813
19.0M
{
1814
19.0M
    const uint32_t enc_level = pkt->h.enc_level;
1815
19.0M
    const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
1816
19.0M
    const struct archetype_data *a = &pkt->geom.adata;
1817
19.0M
    QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
1818
19.0M
    struct tx_helper *h = &pkt->h;
1819
19.0M
    const OSSL_QUIC_FRAME_ACK *ack;
1820
19.0M
    OSSL_QUIC_FRAME_ACK ack2;
1821
1822
19.0M
    tpkt->ackm_pkt.largest_acked = QUIC_PN_INVALID;
1823
1824
    /* ACK Frames (Regenerate) */
1825
19.0M
    if (a->allow_ack
1826
19.0M
        && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_ACK
1827
19.0M
        && (((txp->want_ack & (1UL << pn_space)) != 0)
1828
19.0M
            || ossl_ackm_is_ack_desired(txp->args.ackm, pn_space))
1829
19.0M
        && (ack = ossl_ackm_get_ack_frame(txp->args.ackm, pn_space)) != NULL) {
1830
2.01M
        WPACKET *wpkt = tx_helper_begin(h);
1831
1832
2.01M
        if (wpkt == NULL)
1833
0
            return 0;
1834
1835
        /* We do not currently support ECN */
1836
2.01M
        ack2 = *ack;
1837
2.01M
        ack2.ecn_present = 0;
1838
1839
2.01M
        if (ossl_quic_wire_encode_frame_ack(wpkt,
1840
2.01M
                                            txp->args.ack_delay_exponent,
1841
2.01M
                                            &ack2)) {
1842
384k
            if (!tx_helper_commit(h))
1843
0
                return 0;
1844
1845
384k
            tpkt->had_ack_frame = 1;
1846
1847
384k
            if (ack->num_ack_ranges > 0)
1848
384k
                tpkt->ackm_pkt.largest_acked = ack->ack_ranges[0].end;
1849
1850
384k
            if (txp->ack_tx_cb != NULL)
1851
384k
                txp->ack_tx_cb(&ack2, pn_space, txp->ack_tx_cb_arg);
1852
1.63M
        } else {
1853
1.63M
            tx_helper_rollback(h);
1854
1.63M
        }
1855
2.01M
    }
1856
1857
    /* CONNECTION_CLOSE Frames (Regenerate) */
1858
19.0M
    if (a->allow_conn_close && txp->want_conn_close && chosen_for_conn_close) {
1859
10.6k
        WPACKET *wpkt = tx_helper_begin(h);
1860
10.6k
        OSSL_QUIC_FRAME_CONN_CLOSE f, *pf = &txp->conn_close_frame;
1861
10.6k
        size_t l;
1862
1863
10.6k
        if (wpkt == NULL)
1864
0
            return 0;
1865
1866
        /*
1867
         * Application CONNECTION_CLOSE frames may only be sent in the
1868
         * Application PN space, as otherwise they may be sent before a
1869
         * connection is authenticated and leak application data. Therefore, if
1870
         * we need to send a CONNECTION_CLOSE frame in another PN space and were
1871
         * given an application CONNECTION_CLOSE frame, convert it into a
1872
         * transport CONNECTION_CLOSE frame, removing any sensitive application
1873
         * data.
1874
         *
1875
         * RFC 9000 s. 10.2.3: "A CONNECTION_CLOSE of type 0x1d MUST be replaced
1876
         * by a CONNECTION_CLOSE of type 0x1c when sending the frame in Initial
1877
         * or Handshake packets. Otherwise, information about the application
1878
         * state might be revealed. Endpoints MUST clear the value of the Reason
1879
         * Phrase field and SHOULD use the APPLICATION_ERROR code when
1880
         * converting to a CONNECTION_CLOSE of type 0x1c."
1881
         */
1882
10.6k
        if (pn_space != QUIC_PN_SPACE_APP && pf->is_app) {
1883
0
            pf = &f;
1884
0
            pf->is_app      = 0;
1885
0
            pf->frame_type  = 0;
1886
0
            pf->error_code  = QUIC_ERR_APPLICATION_ERROR;
1887
0
            pf->reason      = NULL;
1888
0
            pf->reason_len  = 0;
1889
0
        }
1890
1891
10.6k
        if (ossl_quic_wire_encode_frame_conn_close(wpkt, pf)
1892
10.6k
                && WPACKET_get_total_written(wpkt, &l)
1893
10.6k
                && try_commit_conn_close(txp, l)) {
1894
10.5k
            if (!tx_helper_commit(h))
1895
0
                return 0;
1896
1897
10.5k
            tpkt->had_conn_close = 1;
1898
10.5k
            *can_be_non_inflight = 0;
1899
10.5k
        } else {
1900
74
            tx_helper_rollback(h);
1901
74
        }
1902
10.6k
    }
1903
1904
19.0M
    return 1;
1905
19.0M
}
1906
1907
static int try_len(size_t space_left, size_t orig_len,
1908
                   size_t base_hdr_len, size_t lenbytes,
1909
                   uint64_t maxn, size_t *hdr_len, size_t *payload_len)
1910
125k
{
1911
125k
    size_t n;
1912
125k
    size_t maxn_ = maxn > SIZE_MAX ? SIZE_MAX : (size_t)maxn;
1913
1914
125k
    *hdr_len = base_hdr_len + lenbytes;
1915
1916
125k
    if (orig_len == 0 && space_left >= *hdr_len) {
1917
0
        *payload_len = 0;
1918
0
        return 1;
1919
0
    }
1920
1921
125k
    n = orig_len;
1922
125k
    if (n > maxn_)
1923
25.2k
        n = maxn_;
1924
125k
    if (n + *hdr_len > space_left)
1925
3.43k
        n = (space_left >= *hdr_len) ? space_left - *hdr_len : 0;
1926
1927
125k
    *payload_len = n;
1928
125k
    return n > 0;
1929
125k
}
1930
1931
static int determine_len(size_t space_left, size_t orig_len,
1932
                         size_t base_hdr_len,
1933
                         uint64_t *hlen, uint64_t *len)
1934
31.2k
{
1935
31.2k
    int ok = 0;
1936
31.2k
    size_t chosen_payload_len = 0;
1937
31.2k
    size_t chosen_hdr_len     = 0;
1938
31.2k
    size_t payload_len[4], hdr_len[4];
1939
31.2k
    int i, valid[4] = {0};
1940
1941
31.2k
    valid[0] = try_len(space_left, orig_len, base_hdr_len,
1942
31.2k
                       1, OSSL_QUIC_VLINT_1B_MAX,
1943
31.2k
                       &hdr_len[0], &payload_len[0]);
1944
31.2k
    valid[1] = try_len(space_left, orig_len, base_hdr_len,
1945
31.2k
                       2, OSSL_QUIC_VLINT_2B_MAX,
1946
31.2k
                       &hdr_len[1], &payload_len[1]);
1947
31.2k
    valid[2] = try_len(space_left, orig_len, base_hdr_len,
1948
31.2k
                       4, OSSL_QUIC_VLINT_4B_MAX,
1949
31.2k
                       &hdr_len[2], &payload_len[2]);
1950
31.2k
    valid[3] = try_len(space_left, orig_len, base_hdr_len,
1951
31.2k
                       8, OSSL_QUIC_VLINT_8B_MAX,
1952
31.2k
                       &hdr_len[3], &payload_len[3]);
1953
1954
156k
   for (i = OSSL_NELEM(valid) - 1; i >= 0; --i)
1955
125k
        if (valid[i] && payload_len[i] >= chosen_payload_len) {
1956
99.8k
            chosen_payload_len = payload_len[i];
1957
99.8k
            chosen_hdr_len     = hdr_len[i];
1958
99.8k
            ok                 = 1;
1959
99.8k
        }
1960
1961
31.2k
    *hlen = chosen_hdr_len;
1962
31.2k
    *len  = chosen_payload_len;
1963
31.2k
    return ok;
1964
31.2k
}
1965
1966
/*
1967
 * Given a CRYPTO frame header with accurate chdr->len and a budget
1968
 * (space_left), try to find the optimal value of chdr->len to fill as much of
1969
 * the budget as possible. This is slightly hairy because larger values of
1970
 * chdr->len cause larger encoded sizes of the length field of the frame, which
1971
 * in turn mean less space available for payload data. We check all possible
1972
 * encodings and choose the optimal encoding.
1973
 */
1974
static int determine_crypto_len(struct tx_helper *h,
1975
                                OSSL_QUIC_FRAME_CRYPTO *chdr,
1976
                                size_t space_left,
1977
                                uint64_t *hlen,
1978
                                uint64_t *len)
1979
27.9k
{
1980
27.9k
    size_t orig_len;
1981
27.9k
    size_t base_hdr_len; /* CRYPTO header length without length field */
1982
1983
27.9k
    if (chdr->len > SIZE_MAX)
1984
0
        return 0;
1985
1986
27.9k
    orig_len = (size_t)chdr->len;
1987
1988
27.9k
    chdr->len = 0;
1989
27.9k
    base_hdr_len = ossl_quic_wire_get_encoded_frame_len_crypto_hdr(chdr);
1990
27.9k
    chdr->len = orig_len;
1991
27.9k
    if (base_hdr_len == 0)
1992
0
        return 0;
1993
1994
27.9k
    --base_hdr_len;
1995
1996
27.9k
    return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
1997
27.9k
}
1998
1999
static int determine_stream_len(struct tx_helper *h,
2000
                                OSSL_QUIC_FRAME_STREAM *shdr,
2001
                                size_t space_left,
2002
                                uint64_t *hlen,
2003
                                uint64_t *len)
2004
3.33k
{
2005
3.33k
    size_t orig_len;
2006
3.33k
    size_t base_hdr_len; /* STREAM header length without length field */
2007
2008
3.33k
    if (shdr->len > SIZE_MAX)
2009
0
        return 0;
2010
2011
3.33k
    orig_len = (size_t)shdr->len;
2012
2013
3.33k
    shdr->len = 0;
2014
3.33k
    base_hdr_len = ossl_quic_wire_get_encoded_frame_len_stream_hdr(shdr);
2015
3.33k
    shdr->len = orig_len;
2016
3.33k
    if (base_hdr_len == 0)
2017
0
        return 0;
2018
2019
3.33k
    if (shdr->has_explicit_len)
2020
926
        --base_hdr_len;
2021
2022
3.33k
    return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
2023
3.33k
}
2024
2025
static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
2026
                                      struct txp_pkt *pkt,
2027
                                      int *have_ack_eliciting)
2028
467k
{
2029
467k
    const uint32_t enc_level = pkt->h.enc_level;
2030
467k
    const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
2031
467k
    QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
2032
467k
    struct tx_helper *h = &pkt->h;
2033
467k
    size_t num_stream_iovec;
2034
467k
    OSSL_QUIC_FRAME_STREAM shdr = {0};
2035
467k
    OSSL_QUIC_FRAME_CRYPTO chdr = {0};
2036
467k
    OSSL_QTX_IOVEC iov[2];
2037
467k
    uint64_t hdr_bytes;
2038
467k
    WPACKET *wpkt;
2039
467k
    QUIC_TXPIM_CHUNK chunk = {0};
2040
467k
    size_t i, space_left;
2041
2042
495k
    for (i = 0;; ++i) {
2043
495k
        space_left = tx_helper_get_space_left(h);
2044
2045
495k
        if (space_left < MIN_FRAME_SIZE_CRYPTO)
2046
314
            return 1; /* no point trying */
2047
2048
        /* Do we have any CRYPTO data waiting? */
2049
494k
        num_stream_iovec = OSSL_NELEM(iov);
2050
494k
        if (!ossl_quic_sstream_get_stream_frame(txp->args.crypto[pn_space],
2051
494k
                                                i, &shdr, iov,
2052
494k
                                                &num_stream_iovec))
2053
466k
            return 1; /* nothing to do */
2054
2055
        /* Convert STREAM frame header to CRYPTO frame header */
2056
27.9k
        chdr.offset = shdr.offset;
2057
27.9k
        chdr.len    = shdr.len;
2058
2059
27.9k
        if (chdr.len == 0)
2060
0
            return 1; /* nothing to do */
2061
2062
        /* Find best fit (header length, payload length) combination. */
2063
27.9k
        if (!determine_crypto_len(h, &chdr, space_left, &hdr_bytes,
2064
27.9k
                                  &chdr.len))
2065
0
            return 1; /* can't fit anything */
2066
2067
        /*
2068
         * Truncate IOVs to match our chosen length.
2069
         *
2070
         * The length cannot be more than SIZE_MAX because this length comes
2071
         * from our send stream buffer.
2072
         */
2073
27.9k
        ossl_quic_sstream_adjust_iov((size_t)chdr.len, iov, num_stream_iovec);
2074
2075
        /*
2076
         * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
2077
         * the stream data.)
2078
         */
2079
27.9k
        if (!txp_el_ensure_iovec(&txp->el[enc_level], h->num_iovec + 3))
2080
0
            return 0; /* alloc error */
2081
2082
        /* Encode the header. */
2083
27.9k
        wpkt = tx_helper_begin(h);
2084
27.9k
        if (wpkt == NULL)
2085
0
            return 0; /* alloc error */
2086
2087
27.9k
        if (!ossl_quic_wire_encode_frame_crypto_hdr(wpkt, &chdr)) {
2088
0
            tx_helper_rollback(h);
2089
0
            return 1; /* can't fit */
2090
0
        }
2091
2092
27.9k
        if (!tx_helper_commit(h))
2093
0
            return 0; /* alloc error */
2094
2095
        /* Add payload iovecs to the helper (infallible). */
2096
55.8k
        for (i = 0; i < num_stream_iovec; ++i)
2097
27.9k
            tx_helper_append_iovec(h, iov[i].buf, iov[i].buf_len);
2098
2099
27.9k
        *have_ack_eliciting = 1;
2100
27.9k
        tx_helper_unrestrict(h); /* no longer need PING */
2101
2102
        /* Log chunk to TXPIM. */
2103
27.9k
        chunk.stream_id = UINT64_MAX; /* crypto stream */
2104
27.9k
        chunk.start     = chdr.offset;
2105
27.9k
        chunk.end       = chdr.offset + chdr.len - 1;
2106
27.9k
        chunk.has_fin   = 0; /* Crypto stream never ends */
2107
27.9k
        if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
2108
0
            return 0; /* alloc error */
2109
27.9k
    }
2110
467k
}
2111
2112
struct chunk_info {
2113
    OSSL_QUIC_FRAME_STREAM shdr;
2114
    uint64_t orig_len;
2115
    OSSL_QTX_IOVEC iov[2];
2116
    size_t num_stream_iovec;
2117
    int valid;
2118
};
2119
2120
static int txp_plan_stream_chunk(OSSL_QUIC_TX_PACKETISER *txp,
2121
                                 struct tx_helper *h,
2122
                                 QUIC_SSTREAM *sstream,
2123
                                 QUIC_TXFC *stream_txfc,
2124
                                 size_t skip,
2125
                                 struct chunk_info *chunk,
2126
                                 uint64_t consumed)
2127
6.15k
{
2128
6.15k
    uint64_t fc_credit, fc_swm, fc_limit;
2129
2130
6.15k
    chunk->num_stream_iovec = OSSL_NELEM(chunk->iov);
2131
6.15k
    chunk->valid = ossl_quic_sstream_get_stream_frame(sstream, skip,
2132
6.15k
                                                      &chunk->shdr,
2133
6.15k
                                                      chunk->iov,
2134
6.15k
                                                      &chunk->num_stream_iovec);
2135
6.15k
    if (!chunk->valid)
2136
2.45k
        return 1;
2137
2138
3.69k
    if (!ossl_assert(chunk->shdr.len > 0 || chunk->shdr.is_fin))
2139
        /* Should only have 0-length chunk if FIN */
2140
0
        return 0;
2141
2142
3.69k
    chunk->orig_len = chunk->shdr.len;
2143
2144
    /* Clamp according to connection and stream-level TXFC. */
2145
3.69k
    fc_credit   = ossl_quic_txfc_get_credit(stream_txfc, consumed);
2146
3.69k
    fc_swm      = ossl_quic_txfc_get_swm(stream_txfc);
2147
3.69k
    fc_limit    = fc_swm + fc_credit;
2148
2149
3.69k
    if (chunk->shdr.len > 0 && chunk->shdr.offset + chunk->shdr.len > fc_limit) {
2150
1.34k
        chunk->shdr.len = (fc_limit <= chunk->shdr.offset)
2151
1.34k
            ? 0 : fc_limit - chunk->shdr.offset;
2152
1.34k
        chunk->shdr.is_fin = 0;
2153
1.34k
    }
2154
2155
3.69k
    if (chunk->shdr.len == 0 && !chunk->shdr.is_fin) {
2156
        /*
2157
         * Nothing to do due to TXFC. Since SSTREAM returns chunks in ascending
2158
         * order of offset we don't need to check any later chunks, so stop
2159
         * iterating here.
2160
         */
2161
1.14k
        chunk->valid = 0;
2162
1.14k
        return 1;
2163
1.14k
    }
2164
2165
2.55k
    return 1;
2166
3.69k
}
2167
2168
/*
2169
 * Returns 0 on fatal error (e.g. allocation failure), 1 on success.
2170
 * *packet_full is set to 1 if there is no longer enough room for another STREAM
2171
 * frame.
2172
 */
2173
static int txp_generate_stream_frames(OSSL_QUIC_TX_PACKETISER *txp,
2174
                                      struct txp_pkt *pkt,
2175
                                      uint64_t id,
2176
                                      QUIC_SSTREAM *sstream,
2177
                                      QUIC_TXFC *stream_txfc,
2178
                                      QUIC_STREAM *next_stream,
2179
                                      int *have_ack_eliciting,
2180
                                      int *packet_full,
2181
                                      uint64_t *new_credit_consumed,
2182
                                      uint64_t conn_consumed)
2183
3.61k
{
2184
3.61k
    int rc = 0;
2185
3.61k
    struct chunk_info chunks[2] = {0};
2186
3.61k
    const uint32_t enc_level = pkt->h.enc_level;
2187
3.61k
    QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
2188
3.61k
    struct tx_helper *h = &pkt->h;
2189
3.61k
    OSSL_QUIC_FRAME_STREAM *shdr;
2190
3.61k
    WPACKET *wpkt;
2191
3.61k
    QUIC_TXPIM_CHUNK chunk;
2192
3.61k
    size_t i, j, space_left;
2193
3.61k
    int can_fill_payload, use_explicit_len;
2194
3.61k
    int could_have_following_chunk;
2195
3.61k
    uint64_t orig_len;
2196
3.61k
    uint64_t hdr_len_implicit, payload_len_implicit;
2197
3.61k
    uint64_t hdr_len_explicit, payload_len_explicit;
2198
3.61k
    uint64_t fc_swm, fc_new_hwm;
2199
2200
3.61k
    fc_swm      = ossl_quic_txfc_get_swm(stream_txfc);
2201
3.61k
    fc_new_hwm  = fc_swm;
2202
2203
    /*
2204
     * Load the first two chunks if any offered by the send stream. We retrieve
2205
     * the next chunk in advance so we can determine if we need to send any more
2206
     * chunks from the same stream after this one, which is needed when
2207
     * determining when we can use an implicit length in a STREAM frame.
2208
     */
2209
8.58k
    for (i = 0; i < 2; ++i) {
2210
6.10k
        if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i, &chunks[i],
2211
6.10k
                                   conn_consumed))
2212
0
            goto err;
2213
2214
6.10k
        if (i == 0 && !chunks[i].valid) {
2215
            /* No chunks, nothing to do. */
2216
1.13k
            rc = 1;
2217
1.13k
            goto err;
2218
1.13k
        }
2219
6.10k
    }
2220
2221
4.18k
    for (i = 0;; ++i) {
2222
4.18k
        space_left = tx_helper_get_space_left(h);
2223
2224
4.18k
        if (!chunks[i % 2].valid) {
2225
            /* Out of chunks; we're done. */
2226
1.64k
            rc = 1;
2227
1.64k
            goto err;
2228
1.64k
        }
2229
2230
2.53k
        if (space_left < MIN_FRAME_SIZE_STREAM) {
2231
127
            *packet_full = 1;
2232
127
            rc = 1;
2233
127
            goto err;
2234
127
        }
2235
2236
2.41k
        if (!ossl_assert(!h->done_implicit))
2237
            /*
2238
             * Logic below should have ensured we didn't append an
2239
             * implicit-length unless we filled the packet or didn't have
2240
             * another stream to handle, so this should not be possible.
2241
             */
2242
0
            goto err;
2243
2244
2.41k
        shdr = &chunks[i % 2].shdr;
2245
2.41k
        orig_len = chunks[i % 2].orig_len;
2246
2.41k
        if (i > 0)
2247
            /* Load next chunk for lookahead. */
2248
55
            if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i + 1,
2249
55
                                       &chunks[(i + 1) % 2], conn_consumed))
2250
0
                goto err;
2251
2252
        /*
2253
         * Find best fit (header length, payload length) combination for if we
2254
         * use an implicit length.
2255
         */
2256
2.41k
        shdr->has_explicit_len = 0;
2257
2.41k
        hdr_len_implicit = payload_len_implicit = 0;
2258
2.41k
        if (!determine_stream_len(h, shdr, space_left,
2259
2.41k
                                  &hdr_len_implicit, &payload_len_implicit)) {
2260
12
            *packet_full = 1;
2261
12
            rc = 1;
2262
12
            goto err; /* can't fit anything */
2263
12
        }
2264
2265
        /*
2266
         * If there is a next stream, we don't use the implicit length so we can
2267
         * add more STREAM frames after this one, unless there is enough data
2268
         * for this STREAM frame to fill the packet.
2269
         */
2270
2.39k
        can_fill_payload = (hdr_len_implicit + payload_len_implicit
2271
2.39k
                            >= space_left);
2272
2273
        /*
2274
         * Is there is a stream after this one, or another chunk pending
2275
         * transmission in this stream?
2276
         */
2277
2.39k
        could_have_following_chunk
2278
2.39k
            = (next_stream != NULL || chunks[(i + 1) % 2].valid);
2279
2280
        /* Choose between explicit or implicit length representations. */
2281
2.39k
        use_explicit_len = !((can_fill_payload || !could_have_following_chunk)
2282
2.39k
                             && !pkt->force_pad);
2283
2284
2.39k
        if (use_explicit_len) {
2285
            /*
2286
             * Find best fit (header length, payload length) combination for if
2287
             * we use an explicit length.
2288
             */
2289
926
            shdr->has_explicit_len = 1;
2290
926
            hdr_len_explicit = payload_len_explicit = 0;
2291
926
            if (!determine_stream_len(h, shdr, space_left,
2292
926
                                      &hdr_len_explicit, &payload_len_explicit)) {
2293
0
                *packet_full = 1;
2294
0
                rc = 1;
2295
0
                goto err; /* can't fit anything */
2296
0
            }
2297
2298
926
            shdr->len = payload_len_explicit;
2299
1.47k
        } else {
2300
1.47k
            *packet_full = 1;
2301
1.47k
            shdr->has_explicit_len = 0;
2302
1.47k
            shdr->len = payload_len_implicit;
2303
1.47k
        }
2304
2305
        /* If this is a FIN, don't keep filling the packet with more FINs. */
2306
2.39k
        if (shdr->is_fin)
2307
0
            chunks[(i + 1) % 2].valid = 0;
2308
2309
        /*
2310
         * We are now committed to our length (shdr->len can't change).
2311
         * If we truncated the chunk, clear the FIN bit.
2312
         */
2313
2.39k
        if (shdr->len < orig_len)
2314
698
            shdr->is_fin = 0;
2315
2316
        /* Truncate IOVs to match our chosen length. */
2317
2.39k
        ossl_quic_sstream_adjust_iov((size_t)shdr->len, chunks[i % 2].iov,
2318
2.39k
                                     chunks[i % 2].num_stream_iovec);
2319
2320
        /*
2321
         * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
2322
         * the stream data.)
2323
         */
2324
2.39k
        if (!txp_el_ensure_iovec(&txp->el[enc_level], h->num_iovec + 3))
2325
0
            goto err; /* alloc error */
2326
2327
        /* Encode the header. */
2328
2.39k
        wpkt = tx_helper_begin(h);
2329
2.39k
        if (wpkt == NULL)
2330
0
            goto err; /* alloc error */
2331
2332
2.39k
        shdr->stream_id = id;
2333
2.39k
        if (!ossl_assert(ossl_quic_wire_encode_frame_stream_hdr(wpkt, shdr))) {
2334
            /* (Should not be possible.) */
2335
0
            tx_helper_rollback(h);
2336
0
            *packet_full = 1;
2337
0
            rc = 1;
2338
0
            goto err; /* can't fit */
2339
0
        }
2340
2341
2.39k
        if (!tx_helper_commit(h))
2342
0
            goto err; /* alloc error */
2343
2344
        /* Add payload iovecs to the helper (infallible). */
2345
4.79k
        for (j = 0; j < chunks[i % 2].num_stream_iovec; ++j)
2346
2.39k
            tx_helper_append_iovec(h, chunks[i % 2].iov[j].buf,
2347
2.39k
                                   chunks[i % 2].iov[j].buf_len);
2348
2349
2.39k
        *have_ack_eliciting = 1;
2350
2.39k
        tx_helper_unrestrict(h); /* no longer need PING */
2351
2.39k
        if (!shdr->has_explicit_len)
2352
1.47k
            h->done_implicit = 1;
2353
2354
        /* Log new TXFC credit which was consumed. */
2355
2.39k
        if (shdr->len > 0 && shdr->offset + shdr->len > fc_new_hwm)
2356
1.67k
            fc_new_hwm = shdr->offset + shdr->len;
2357
2358
        /* Log chunk to TXPIM. */
2359
2.39k
        chunk.stream_id         = shdr->stream_id;
2360
2.39k
        chunk.start             = shdr->offset;
2361
2.39k
        chunk.end               = shdr->offset + shdr->len - 1;
2362
2.39k
        chunk.has_fin           = shdr->is_fin;
2363
2.39k
        chunk.has_stop_sending  = 0;
2364
2.39k
        chunk.has_reset_stream  = 0;
2365
2.39k
        if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
2366
0
            goto err; /* alloc error */
2367
2368
2.39k
        if (shdr->len < orig_len) {
2369
            /*
2370
             * If we did not serialize all of this chunk we definitely do not
2371
             * want to try the next chunk
2372
             */
2373
698
            rc = 1;
2374
698
            goto err;
2375
698
        }
2376
2.39k
    }
2377
2378
3.61k
err:
2379
3.61k
    *new_credit_consumed = fc_new_hwm - fc_swm;
2380
3.61k
    return rc;
2381
2.48k
}
2382
2383
static void txp_enlink_tmp(QUIC_STREAM **tmp_head, QUIC_STREAM *stream)
2384
6.73k
{
2385
6.73k
    stream->txp_next = *tmp_head;
2386
6.73k
    *tmp_head = stream;
2387
6.73k
}
2388
2389
static int txp_generate_stream_related(OSSL_QUIC_TX_PACKETISER *txp,
2390
                                       struct txp_pkt *pkt,
2391
                                       int *have_ack_eliciting,
2392
                                       QUIC_STREAM **tmp_head)
2393
97.7k
{
2394
97.7k
    QUIC_STREAM_ITER it;
2395
97.7k
    WPACKET *wpkt;
2396
97.7k
    uint64_t cwm;
2397
97.7k
    QUIC_STREAM *stream, *snext;
2398
97.7k
    struct tx_helper *h = &pkt->h;
2399
97.7k
    uint64_t conn_consumed = 0;
2400
2401
97.7k
    for (ossl_quic_stream_iter_init(&it, txp->args.qsm, 1);
2402
102k
         it.stream != NULL;) {
2403
2404
6.73k
        stream = it.stream;
2405
6.73k
        ossl_quic_stream_iter_next(&it);
2406
6.73k
        snext = it.stream;
2407
2408
6.73k
        stream->txp_sent_fc                  = 0;
2409
6.73k
        stream->txp_sent_stop_sending        = 0;
2410
6.73k
        stream->txp_sent_reset_stream        = 0;
2411
6.73k
        stream->txp_blocked                  = 0;
2412
6.73k
        stream->txp_txfc_new_credit_consumed = 0;
2413
2414
        /* Stream Abort Frames (STOP_SENDING, RESET_STREAM) */
2415
6.73k
        if (stream->want_stop_sending) {
2416
0
            OSSL_QUIC_FRAME_STOP_SENDING f;
2417
2418
0
            wpkt = tx_helper_begin(h);
2419
0
            if (wpkt == NULL)
2420
0
                return 0; /* alloc error */
2421
2422
0
            f.stream_id         = stream->id;
2423
0
            f.app_error_code    = stream->stop_sending_aec;
2424
0
            if (!ossl_quic_wire_encode_frame_stop_sending(wpkt, &f)) {
2425
0
                tx_helper_rollback(h); /* can't fit */
2426
0
                txp_enlink_tmp(tmp_head, stream);
2427
0
                break;
2428
0
            }
2429
2430
0
            if (!tx_helper_commit(h))
2431
0
                return 0; /* alloc error */
2432
2433
0
            *have_ack_eliciting = 1;
2434
0
            tx_helper_unrestrict(h); /* no longer need PING */
2435
0
            stream->txp_sent_stop_sending = 1;
2436
0
        }
2437
2438
6.73k
        if (stream->want_reset_stream) {
2439
3.02k
            OSSL_QUIC_FRAME_RESET_STREAM f;
2440
2441
3.02k
            if (!ossl_assert(stream->send_state == QUIC_SSTREAM_STATE_RESET_SENT))
2442
0
                return 0;
2443
2444
3.02k
            wpkt = tx_helper_begin(h);
2445
3.02k
            if (wpkt == NULL)
2446
0
                return 0; /* alloc error */
2447
2448
3.02k
            f.stream_id         = stream->id;
2449
3.02k
            f.app_error_code    = stream->reset_stream_aec;
2450
3.02k
            if (!ossl_quic_stream_send_get_final_size(stream, &f.final_size))
2451
0
                return 0; /* should not be possible */
2452
2453
3.02k
            if (!ossl_quic_wire_encode_frame_reset_stream(wpkt, &f)) {
2454
146
                tx_helper_rollback(h); /* can't fit */
2455
146
                txp_enlink_tmp(tmp_head, stream);
2456
146
                break;
2457
146
            }
2458
2459
2.87k
            if (!tx_helper_commit(h))
2460
0
                return 0; /* alloc error */
2461
2462
2.87k
            *have_ack_eliciting = 1;
2463
2.87k
            tx_helper_unrestrict(h); /* no longer need PING */
2464
2.87k
            stream->txp_sent_reset_stream = 1;
2465
2466
            /*
2467
             * The final size of the stream as indicated by RESET_STREAM is used
2468
             * to ensure a consistent view of flow control state by both
2469
             * parties; if we happen to send a RESET_STREAM that consumes more
2470
             * flow control credit, make sure we account for that.
2471
             */
2472
2.87k
            if (!ossl_assert(f.final_size <= ossl_quic_txfc_get_swm(&stream->txfc)))
2473
0
                return 0;
2474
2475
2.87k
            stream->txp_txfc_new_credit_consumed
2476
2.87k
                = f.final_size - ossl_quic_txfc_get_swm(&stream->txfc);
2477
2.87k
        }
2478
2479
        /*
2480
         * Stream Flow Control Frames (MAX_STREAM_DATA)
2481
         *
2482
         * RFC 9000 s. 13.3: "An endpoint SHOULD stop sending MAX_STREAM_DATA
2483
         * frames when the receiving part of the stream enters a "Size Known" or
2484
         * "Reset Recvd" state." -- In practice, RECV is the only state
2485
         * in which it makes sense to generate more MAX_STREAM_DATA frames.
2486
         */
2487
6.58k
        if (stream->recv_state == QUIC_RSTREAM_STATE_RECV
2488
6.58k
            && (stream->want_max_stream_data
2489
5.87k
                || ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 0))) {
2490
2491
551
            wpkt = tx_helper_begin(h);
2492
551
            if (wpkt == NULL)
2493
0
                return 0; /* alloc error */
2494
2495
551
            cwm = ossl_quic_rxfc_get_cwm(&stream->rxfc);
2496
2497
551
            if (!ossl_quic_wire_encode_frame_max_stream_data(wpkt, stream->id,
2498
551
                                                             cwm)) {
2499
90
                tx_helper_rollback(h); /* can't fit */
2500
90
                txp_enlink_tmp(tmp_head, stream);
2501
90
                break;
2502
90
            }
2503
2504
461
            if (!tx_helper_commit(h))
2505
0
                return 0; /* alloc error */
2506
2507
461
            *have_ack_eliciting = 1;
2508
461
            tx_helper_unrestrict(h); /* no longer need PING */
2509
461
            stream->txp_sent_fc = 1;
2510
461
        }
2511
2512
        /*
2513
         * Stream Data Frames (STREAM)
2514
         *
2515
         * RFC 9000 s. 3.3: A sender MUST NOT send a STREAM [...] frame for a
2516
         * stream in the "Reset Sent" state [or any terminal state]. We don't
2517
         * send any more STREAM frames if we are sending, have sent, or are
2518
         * planning to send, RESET_STREAM. The other terminal state is Data
2519
         * Recvd, but txp_generate_stream_frames() is guaranteed to generate
2520
         * nothing in this case.
2521
         */
2522
6.49k
        if (ossl_quic_stream_has_send_buffer(stream)
2523
6.49k
            && !ossl_quic_stream_send_is_reset(stream)) {
2524
3.61k
            int packet_full = 0;
2525
2526
3.61k
            if (!ossl_assert(!stream->want_reset_stream))
2527
0
                return 0;
2528
2529
3.61k
            if (!txp_generate_stream_frames(txp, pkt,
2530
3.61k
                                            stream->id, stream->sstream,
2531
3.61k
                                            &stream->txfc,
2532
3.61k
                                            snext,
2533
3.61k
                                            have_ack_eliciting,
2534
3.61k
                                            &packet_full,
2535
3.61k
                                            &stream->txp_txfc_new_credit_consumed,
2536
3.61k
                                            conn_consumed)) {
2537
                /* Fatal error (allocation, etc.) */
2538
0
                txp_enlink_tmp(tmp_head, stream);
2539
0
                return 0;
2540
0
            }
2541
3.61k
            conn_consumed += stream->txp_txfc_new_credit_consumed;
2542
2543
3.61k
            if (packet_full) {
2544
1.61k
                txp_enlink_tmp(tmp_head, stream);
2545
1.61k
                break;
2546
1.61k
            }
2547
3.61k
        }
2548
2549
4.88k
        txp_enlink_tmp(tmp_head, stream);
2550
4.88k
    }
2551
2552
97.7k
    return 1;
2553
97.7k
}
2554
2555
static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp,
2556
                               struct txp_pkt *pkt,
2557
                               int chosen_for_conn_close)
2558
19.0M
{
2559
19.0M
    int rc = TXP_ERR_SUCCESS;
2560
19.0M
    const uint32_t enc_level = pkt->h.enc_level;
2561
19.0M
    const uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
2562
19.0M
    int have_ack_eliciting = 0, done_pre_token = 0;
2563
19.0M
    const struct archetype_data a = pkt->geom.adata;
2564
    /*
2565
     * Cleared if we encode any non-ACK-eliciting frame type which rules out the
2566
     * packet being a non-inflight frame. This means any non-ACK ACK-eliciting
2567
     * frame, even PADDING frames. ACK eliciting frames always cause a packet to
2568
     * become ineligible for non-inflight treatment so it is not necessary to
2569
     * clear this in cases where have_ack_eliciting is set, as it is ignored in
2570
     * that case.
2571
     */
2572
19.0M
    int can_be_non_inflight = 1;
2573
19.0M
    QUIC_CFQ_ITEM *cfq_item;
2574
19.0M
    QUIC_TXPIM_PKT *tpkt = NULL;
2575
19.0M
    struct tx_helper *h = &pkt->h;
2576
2577
    /* Maximum PN reached? */
2578
19.0M
    if (!ossl_quic_pn_valid(txp->next_pn[pn_space]))
2579
0
        goto fatal_err;
2580
2581
19.0M
    if (!ossl_assert(pkt->tpkt == NULL))
2582
0
        goto fatal_err;
2583
2584
19.0M
    if ((pkt->tpkt = tpkt = ossl_quic_txpim_pkt_alloc(txp->args.txpim)) == NULL)
2585
0
        goto fatal_err;
2586
2587
    /*
2588
     * Frame Serialization
2589
     * ===================
2590
     *
2591
     * We now serialize frames into the packet in descending order of priority.
2592
     */
2593
2594
    /* HANDSHAKE_DONE (Regenerate) */
2595
19.0M
    if (a.allow_handshake_done && txp->want_handshake_done
2596
19.0M
        && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_HANDSHAKE_DONE) {
2597
0
        WPACKET *wpkt = tx_helper_begin(h);
2598
2599
0
        if (wpkt == NULL)
2600
0
            goto fatal_err;
2601
2602
0
        if (ossl_quic_wire_encode_frame_handshake_done(wpkt)) {
2603
0
            tpkt->had_handshake_done_frame = 1;
2604
0
            have_ack_eliciting             = 1;
2605
2606
0
            if (!tx_helper_commit(h))
2607
0
                goto fatal_err;
2608
2609
0
            tx_helper_unrestrict(h); /* no longer need PING */
2610
0
        } else {
2611
0
            tx_helper_rollback(h);
2612
0
        }
2613
0
    }
2614
2615
    /* MAX_DATA (Regenerate) */
2616
19.0M
    if (a.allow_conn_fc
2617
19.0M
        && (txp->want_max_data
2618
97.7k
            || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0))
2619
19.0M
        && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_DATA) {
2620
0
        WPACKET *wpkt = tx_helper_begin(h);
2621
0
        uint64_t cwm = ossl_quic_rxfc_get_cwm(txp->args.conn_rxfc);
2622
2623
0
        if (wpkt == NULL)
2624
0
            goto fatal_err;
2625
2626
0
        if (ossl_quic_wire_encode_frame_max_data(wpkt, cwm)) {
2627
0
            tpkt->had_max_data_frame = 1;
2628
0
            have_ack_eliciting       = 1;
2629
2630
0
            if (!tx_helper_commit(h))
2631
0
                goto fatal_err;
2632
2633
0
            tx_helper_unrestrict(h); /* no longer need PING */
2634
0
        } else {
2635
0
            tx_helper_rollback(h);
2636
0
        }
2637
0
    }
2638
2639
    /* MAX_STREAMS_BIDI (Regenerate) */
2640
19.0M
    if (a.allow_conn_fc
2641
19.0M
        && (txp->want_max_streams_bidi
2642
97.7k
            || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 0))
2643
19.0M
        && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_STREAMS_BIDI) {
2644
0
        WPACKET *wpkt = tx_helper_begin(h);
2645
0
        uint64_t max_streams
2646
0
            = ossl_quic_rxfc_get_cwm(txp->args.max_streams_bidi_rxfc);
2647
2648
0
        if (wpkt == NULL)
2649
0
            goto fatal_err;
2650
2651
0
        if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/0,
2652
0
                                                    max_streams)) {
2653
0
            tpkt->had_max_streams_bidi_frame = 1;
2654
0
            have_ack_eliciting               = 1;
2655
2656
0
            if (!tx_helper_commit(h))
2657
0
                goto fatal_err;
2658
2659
0
            tx_helper_unrestrict(h); /* no longer need PING */
2660
0
        } else {
2661
0
            tx_helper_rollback(h);
2662
0
        }
2663
0
    }
2664
2665
    /* MAX_STREAMS_UNI (Regenerate) */
2666
19.0M
    if (a.allow_conn_fc
2667
19.0M
        && (txp->want_max_streams_uni
2668
97.7k
            || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 0))
2669
19.0M
        && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_MAX_STREAMS_UNI) {
2670
0
        WPACKET *wpkt = tx_helper_begin(h);
2671
0
        uint64_t max_streams
2672
0
            = ossl_quic_rxfc_get_cwm(txp->args.max_streams_uni_rxfc);
2673
2674
0
        if (wpkt == NULL)
2675
0
            goto fatal_err;
2676
2677
0
        if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/1,
2678
0
                                                    max_streams)) {
2679
0
            tpkt->had_max_streams_uni_frame = 1;
2680
0
            have_ack_eliciting              = 1;
2681
2682
0
            if (!tx_helper_commit(h))
2683
0
                goto fatal_err;
2684
2685
0
            tx_helper_unrestrict(h); /* no longer need PING */
2686
0
        } else {
2687
0
            tx_helper_rollback(h);
2688
0
        }
2689
0
    }
2690
2691
    /* GCR Frames */
2692
19.0M
    for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
2693
217M
         cfq_item != NULL;
2694
198M
         cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
2695
198M
        uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
2696
198M
        const unsigned char *encoded = ossl_quic_cfq_item_get_encoded(cfq_item);
2697
198M
        size_t encoded_len = ossl_quic_cfq_item_get_encoded_len(cfq_item);
2698
2699
198M
        switch (frame_type) {
2700
0
            case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
2701
0
                if (!a.allow_new_conn_id)
2702
0
                    continue;
2703
0
                break;
2704
1.74M
            case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
2705
1.74M
                if (!a.allow_retire_conn_id)
2706
1.73M
                    continue;
2707
5.89k
                break;
2708
5.89k
            case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
2709
0
                if (!a.allow_new_token)
2710
0
                    continue;
2711
2712
                /*
2713
                 * NEW_TOKEN frames are handled via GCR, but some
2714
                 * Regenerate-strategy frames should come before them (namely
2715
                 * ACK, CONNECTION_CLOSE, PATH_CHALLENGE and PATH_RESPONSE). If
2716
                 * we find a NEW_TOKEN frame, do these now. If there are no
2717
                 * NEW_TOKEN frames in the GCR queue we will handle these below.
2718
                 */
2719
0
                if (!done_pre_token)
2720
0
                    if (txp_generate_pre_token(txp, pkt,
2721
0
                                               chosen_for_conn_close,
2722
0
                                               &can_be_non_inflight))
2723
0
                        done_pre_token = 1;
2724
2725
0
                break;
2726
196M
            case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
2727
196M
                if (!a.allow_path_response)
2728
196M
                    continue;
2729
2730
                /*
2731
                 * RFC 9000 s. 8.2.2: An endpoint MUST expand datagrams that
2732
                 * contain a PATH_RESPONSE frame to at least the smallest
2733
                 * allowed maximum datagram size of 1200 bytes.
2734
                 */
2735
115k
                pkt->force_pad = 1;
2736
115k
                break;
2737
0
            default:
2738
0
                if (!a.allow_cfq_other)
2739
0
                    continue;
2740
0
                break;
2741
198M
        }
2742
2743
        /*
2744
         * If the frame is too big, don't try to schedule any more GCR frames in
2745
         * this packet rather than sending subsequent ones out of order.
2746
         */
2747
121k
        if (encoded_len > tx_helper_get_space_left(h))
2748
285
            break;
2749
2750
121k
        if (!tx_helper_append_iovec(h, encoded, encoded_len))
2751
0
            goto fatal_err;
2752
2753
121k
        ossl_quic_txpim_pkt_add_cfq_item(tpkt, cfq_item);
2754
2755
121k
        if (ossl_quic_frame_type_is_ack_eliciting(frame_type)) {
2756
121k
            have_ack_eliciting = 1;
2757
121k
            tx_helper_unrestrict(h); /* no longer need PING */
2758
121k
        }
2759
121k
    }
2760
2761
    /*
2762
     * If we didn't generate ACK, CONNECTION_CLOSE, PATH_CHALLENGE or
2763
     * PATH_RESPONSE (as desired) before, do so now.
2764
     */
2765
19.0M
    if (!done_pre_token)
2766
19.0M
        if (txp_generate_pre_token(txp, pkt,
2767
19.0M
                                   chosen_for_conn_close,
2768
19.0M
                                   &can_be_non_inflight))
2769
19.0M
            done_pre_token = 1;
2770
2771
    /* CRYPTO Frames */
2772
19.0M
    if (a.allow_crypto)
2773
467k
        if (!txp_generate_crypto_frames(txp, pkt, &have_ack_eliciting))
2774
0
            goto fatal_err;
2775
2776
    /* Stream-specific frames */
2777
19.0M
    if (a.allow_stream_rel && txp->handshake_complete)
2778
97.7k
        if (!txp_generate_stream_related(txp, pkt,
2779
97.7k
                                         &have_ack_eliciting,
2780
97.7k
                                         &pkt->stream_head))
2781
0
            goto fatal_err;
2782
2783
    /* PING */
2784
19.0M
    tx_helper_unrestrict(h);
2785
2786
19.0M
    if (!have_ack_eliciting && txp_need_ping(txp, pn_space, &a)) {
2787
392k
        WPACKET *wpkt;
2788
2789
392k
        assert(h->reserve > 0);
2790
392k
        wpkt = tx_helper_begin(h);
2791
392k
        if (wpkt == NULL)
2792
0
            goto fatal_err;
2793
2794
392k
        if (!ossl_quic_wire_encode_frame_ping(wpkt)
2795
392k
            || !tx_helper_commit(h))
2796
            /*
2797
             * We treat a request to be ACK-eliciting as a requirement, so this
2798
             * is an error.
2799
             */
2800
0
            goto fatal_err;
2801
2802
392k
        have_ack_eliciting = 1;
2803
392k
    }
2804
2805
    /* PADDING is added by ossl_quic_tx_packetiser_generate(). */
2806
2807
    /*
2808
     * ACKM Data
2809
     * =========
2810
     */
2811
19.0M
    if (have_ack_eliciting)
2812
426k
        can_be_non_inflight = 0;
2813
2814
    /* ACKM Data */
2815
19.0M
    tpkt->ackm_pkt.num_bytes        = h->bytes_appended + pkt->geom.pkt_overhead;
2816
19.0M
    tpkt->ackm_pkt.pkt_num          = txp->next_pn[pn_space];
2817
    /* largest_acked is set in txp_generate_pre_token */
2818
19.0M
    tpkt->ackm_pkt.pkt_space        = pn_space;
2819
19.0M
    tpkt->ackm_pkt.is_inflight      = !can_be_non_inflight;
2820
19.0M
    tpkt->ackm_pkt.is_ack_eliciting = have_ack_eliciting;
2821
19.0M
    tpkt->ackm_pkt.is_pto_probe     = 0;
2822
19.0M
    tpkt->ackm_pkt.is_mtu_probe     = 0;
2823
19.0M
    tpkt->ackm_pkt.time             = txp->args.now(txp->args.now_arg);
2824
2825
    /* Done. */
2826
19.0M
    return rc;
2827
2828
0
fatal_err:
2829
    /*
2830
     * Handler for fatal errors, i.e. errors causing us to abort the entire
2831
     * packet rather than just one frame. Examples of such errors include
2832
     * allocation errors.
2833
     */
2834
0
    if (tpkt != NULL) {
2835
0
        ossl_quic_txpim_pkt_release(txp->args.txpim, tpkt);
2836
0
        pkt->tpkt = NULL;
2837
0
    }
2838
0
    return TXP_ERR_INTERNAL;
2839
19.0M
}
2840
2841
/*
2842
 * Commits and queues a packet for transmission. There is no backing out after
2843
 * this.
2844
 *
2845
 * This:
2846
 *
2847
 *   - Sends the packet to the QTX for encryption and transmission;
2848
 *
2849
 *   - Records the packet as having been transmitted in FIFM. ACKM is informed,
2850
 *     etc. and the TXPIM record is filed.
2851
 *
2852
 *   - Informs various subsystems of frames that were sent and clears frame
2853
 *     wanted flags so that we do not generate the same frames again.
2854
 *
2855
 * Assumptions:
2856
 *
2857
 *   - pkt is a txp_pkt for the correct EL;
2858
 *
2859
 *   - pkt->tpkt is valid;
2860
 *
2861
 *   - pkt->tpkt->ackm_pkt has been fully filled in;
2862
 *
2863
 *   - Stream chunk records have been appended to pkt->tpkt for STREAM and
2864
 *     CRYPTO frames, but not for RESET_STREAM or STOP_SENDING frames;
2865
 *
2866
 *   - The chosen stream list for the packet can be fully walked from
2867
 *     pkt->stream_head using stream->txp_next;
2868
 *
2869
 *   - pkt->has_ack_eliciting is set correctly.
2870
 *
2871
 */
2872
static int txp_pkt_commit(OSSL_QUIC_TX_PACKETISER *txp,
2873
                          struct txp_pkt *pkt,
2874
                          uint32_t archetype,
2875
                          int *txpim_pkt_reffed)
2876
469k
{
2877
469k
    int rc = 1;
2878
469k
    uint32_t enc_level = pkt->h.enc_level;
2879
469k
    uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
2880
469k
    QUIC_TXPIM_PKT *tpkt = pkt->tpkt;
2881
469k
    QUIC_STREAM *stream;
2882
469k
    OSSL_QTX_PKT txpkt;
2883
469k
    struct archetype_data a;
2884
2885
469k
    *txpim_pkt_reffed = 0;
2886
2887
    /* Cannot send a packet with an empty payload. */
2888
469k
    if (pkt->h.bytes_appended == 0)
2889
0
        return 0;
2890
2891
469k
    if (!txp_get_archetype_data(enc_level, archetype, &a))
2892
0
        return 0;
2893
2894
    /* Packet Information for QTX */
2895
469k
    txpkt.hdr       = &pkt->phdr;
2896
469k
    txpkt.iovec     = txp->el[enc_level].iovec;
2897
469k
    txpkt.num_iovec = pkt->h.num_iovec;
2898
469k
    txpkt.local     = NULL;
2899
469k
    txpkt.peer      = BIO_ADDR_family(&txp->args.peer) == AF_UNSPEC
2900
469k
        ? NULL : &txp->args.peer;
2901
469k
    txpkt.pn        = txp->next_pn[pn_space];
2902
469k
    txpkt.flags     = OSSL_QTX_PKT_FLAG_COALESCE; /* always try to coalesce */
2903
2904
    /* Generate TXPIM chunks representing STOP_SENDING and RESET_STREAM frames. */
2905
475k
    for (stream = pkt->stream_head; stream != NULL; stream = stream->txp_next)
2906
5.69k
        if (stream->txp_sent_stop_sending || stream->txp_sent_reset_stream) {
2907
            /* Log STOP_SENDING/RESET_STREAM chunk to TXPIM. */
2908
2.87k
            QUIC_TXPIM_CHUNK chunk;
2909
2910
2.87k
            chunk.stream_id         = stream->id;
2911
2.87k
            chunk.start             = UINT64_MAX;
2912
2.87k
            chunk.end               = 0;
2913
2.87k
            chunk.has_fin           = 0;
2914
2.87k
            chunk.has_stop_sending  = stream->txp_sent_stop_sending;
2915
2.87k
            chunk.has_reset_stream  = stream->txp_sent_reset_stream;
2916
2.87k
            if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
2917
0
                return 0; /* alloc error */
2918
2.87k
        }
2919
2920
    /* Dispatch to FIFD. */
2921
469k
    if (!ossl_quic_fifd_pkt_commit(&txp->fifd, tpkt))
2922
0
        return 0;
2923
2924
    /*
2925
     * Transmission and Post-Packet Generation Bookkeeping
2926
     * ===================================================
2927
     *
2928
     * No backing out anymore - at this point the ACKM has recorded the packet
2929
     * as having been sent, so we need to increment our next PN counter, or
2930
     * the ACKM will complain when we try to record a duplicate packet with
2931
     * the same PN later. At this point actually sending the packet may still
2932
     * fail. In this unlikely event it will simply be handled as though it
2933
     * were a lost packet.
2934
     */
2935
469k
    ++txp->next_pn[pn_space];
2936
469k
    *txpim_pkt_reffed = 1;
2937
2938
    /* Send the packet. */
2939
469k
    if (!ossl_qtx_write_pkt(txp->args.qtx, &txpkt))
2940
0
        return 0;
2941
2942
    /*
2943
     * Record FC and stream abort frames as sent; deactivate streams which no
2944
     * longer have anything to do.
2945
     */
2946
475k
    for (stream = pkt->stream_head; stream != NULL; stream = stream->txp_next) {
2947
5.69k
        if (stream->txp_sent_fc) {
2948
461
            stream->want_max_stream_data = 0;
2949
461
            ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 1);
2950
461
        }
2951
2952
5.69k
        if (stream->txp_sent_stop_sending)
2953
0
            stream->want_stop_sending = 0;
2954
2955
5.69k
        if (stream->txp_sent_reset_stream)
2956
2.87k
            stream->want_reset_stream = 0;
2957
2958
5.69k
        if (stream->txp_txfc_new_credit_consumed > 0) {
2959
1.67k
            if (!ossl_assert(ossl_quic_txfc_consume_credit(&stream->txfc,
2960
1.67k
                                                           stream->txp_txfc_new_credit_consumed)))
2961
                /*
2962
                 * Should not be possible, but we should continue with our
2963
                 * bookkeeping as we have already committed the packet to the
2964
                 * FIFD. Just change the value we return.
2965
                 */
2966
0
                rc = 0;
2967
2968
1.67k
            stream->txp_txfc_new_credit_consumed = 0;
2969
1.67k
        }
2970
2971
        /*
2972
         * If we no longer need to generate any flow control (MAX_STREAM_DATA),
2973
         * STOP_SENDING or RESET_STREAM frames, nor any STREAM frames (because
2974
         * the stream is drained of data or TXFC-blocked), we can mark the
2975
         * stream as inactive.
2976
         */
2977
5.69k
        ossl_quic_stream_map_update_state(txp->args.qsm, stream);
2978
2979
5.69k
        if (ossl_quic_stream_has_send_buffer(stream)
2980
5.69k
            && !ossl_quic_sstream_has_pending(stream->sstream)
2981
5.69k
            && ossl_quic_sstream_get_final_size(stream->sstream, NULL))
2982
            /*
2983
             * Transition to DATA_SENT if stream has a final size and we have
2984
             * sent all data.
2985
             */
2986
0
            ossl_quic_stream_map_notify_all_data_sent(txp->args.qsm, stream);
2987
5.69k
    }
2988
2989
    /* We have now sent the packet, so update state accordingly. */
2990
469k
    if (tpkt->ackm_pkt.is_ack_eliciting)
2991
426k
        txp->force_ack_eliciting &= ~(1UL << pn_space);
2992
2993
469k
    if (tpkt->had_handshake_done_frame)
2994
0
        txp->want_handshake_done = 0;
2995
2996
469k
    if (tpkt->had_max_data_frame) {
2997
0
        txp->want_max_data = 0;
2998
0
        ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 1);
2999
0
    }
3000
3001
469k
    if (tpkt->had_max_streams_bidi_frame) {
3002
0
        txp->want_max_streams_bidi = 0;
3003
0
        ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 1);
3004
0
    }
3005
3006
469k
    if (tpkt->had_max_streams_uni_frame) {
3007
0
        txp->want_max_streams_uni = 0;
3008
0
        ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 1);
3009
0
    }
3010
3011
469k
    if (tpkt->had_ack_frame)
3012
71.5k
        txp->want_ack &= ~(1UL << pn_space);
3013
3014
469k
    if (tpkt->had_conn_close)
3015
10.5k
        txp->want_conn_close = 0;
3016
3017
    /*
3018
     * Decrement probe request counts if we have sent a packet that meets
3019
     * the requirement of a probe, namely being ACK-eliciting.
3020
     */
3021
469k
    if (tpkt->ackm_pkt.is_ack_eliciting) {
3022
426k
        OSSL_ACKM_PROBE_INFO *probe_info
3023
426k
            = ossl_ackm_get0_probe_request(txp->args.ackm);
3024
3025
426k
        if (enc_level == QUIC_ENC_LEVEL_INITIAL
3026
426k
            && probe_info->anti_deadlock_initial > 0)
3027
712
            --probe_info->anti_deadlock_initial;
3028
3029
426k
        if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
3030
426k
            && probe_info->anti_deadlock_handshake > 0)
3031
918
            --probe_info->anti_deadlock_handshake;
3032
3033
426k
        if (a.allow_force_ack_eliciting /* (i.e., not for 0-RTT) */
3034
426k
            && probe_info->pto[pn_space] > 0)
3035
142k
            --probe_info->pto[pn_space];
3036
426k
    }
3037
3038
469k
    return rc;
3039
469k
}
3040
3041
/* Ensure the iovec array is at least num elements long. */
3042
static int txp_el_ensure_iovec(struct txp_el *el, size_t num)
3043
1.26M
{
3044
1.26M
    OSSL_QTX_IOVEC *iovec;
3045
3046
1.26M
    if (el->alloc_iovec >= num)
3047
1.22M
        return 1;
3048
3049
39.3k
    num = el->alloc_iovec != 0 ? el->alloc_iovec * 2 : 8;
3050
3051
39.3k
    iovec = OPENSSL_realloc(el->iovec, sizeof(OSSL_QTX_IOVEC) * num);
3052
39.3k
    if (iovec == NULL)
3053
0
        return 0;
3054
3055
39.3k
    el->iovec          = iovec;
3056
39.3k
    el->alloc_iovec    = num;
3057
39.3k
    return 1;
3058
39.3k
}
3059
3060
int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
3061
                                                const OSSL_QUIC_FRAME_CONN_CLOSE *f)
3062
10.9k
{
3063
10.9k
    char *reason = NULL;
3064
10.9k
    size_t reason_len = f->reason_len;
3065
10.9k
    size_t max_reason_len = txp_get_mdpl(txp) / 2;
3066
3067
10.9k
    if (txp->want_conn_close)
3068
0
        return 0;
3069
3070
    /*
3071
     * Arbitrarily limit the length of the reason length string to half of the
3072
     * MDPL.
3073
     */
3074
10.9k
    if (reason_len > max_reason_len)
3075
0
        reason_len = max_reason_len;
3076
3077
10.9k
    if (reason_len > 0) {
3078
10.9k
        reason = OPENSSL_memdup(f->reason, reason_len);
3079
10.9k
        if (reason == NULL)
3080
0
            return 0;
3081
10.9k
    }
3082
3083
10.9k
    txp->conn_close_frame               = *f;
3084
10.9k
    txp->conn_close_frame.reason        = reason;
3085
10.9k
    txp->conn_close_frame.reason_len    = reason_len;
3086
10.9k
    txp->want_conn_close                = 1;
3087
10.9k
    return 1;
3088
10.9k
}
3089
3090
void ossl_quic_tx_packetiser_set_msg_callback(OSSL_QUIC_TX_PACKETISER *txp,
3091
                                              ossl_msg_cb msg_callback,
3092
                                              SSL *msg_callback_ssl)
3093
21.1k
{
3094
21.1k
    txp->msg_callback = msg_callback;
3095
21.1k
    txp->msg_callback_ssl = msg_callback_ssl;
3096
21.1k
}
3097
3098
void ossl_quic_tx_packetiser_set_msg_callback_arg(OSSL_QUIC_TX_PACKETISER *txp,
3099
                                                  void *msg_callback_arg)
3100
21.1k
{
3101
21.1k
    txp->msg_callback_arg = msg_callback_arg;
3102
21.1k
}
3103
3104
QUIC_PN ossl_quic_tx_packetiser_get_next_pn(OSSL_QUIC_TX_PACKETISER *txp,
3105
                                            uint32_t pn_space)
3106
8.27k
{
3107
8.27k
    if (pn_space >= QUIC_PN_SPACE_NUM)
3108
0
        return UINT64_MAX;
3109
3110
8.27k
    return txp->next_pn[pn_space];
3111
8.27k
}
3112
3113
OSSL_TIME ossl_quic_tx_packetiser_get_deadline(OSSL_QUIC_TX_PACKETISER *txp)
3114
27.1M
{
3115
    /*
3116
     * TXP-specific deadline computations which rely on TXP innards. This is in
3117
     * turn relied on by the QUIC_CHANNEL code to determine the channel event
3118
     * handling deadline.
3119
     */
3120
27.1M
    OSSL_TIME deadline = ossl_time_infinite();
3121
27.1M
    uint32_t enc_level, pn_space;
3122
3123
    /*
3124
     * ACK generation is not CC-gated - packets containing only ACKs are allowed
3125
     * to bypass CC. We want to generate ACK frames even if we are currently
3126
     * restricted by CC so the peer knows we have received data. The generate
3127
     * call will take care of selecting the correct packet archetype.
3128
     */
3129
27.1M
    for (enc_level = QUIC_ENC_LEVEL_INITIAL;
3130
135M
         enc_level < QUIC_ENC_LEVEL_NUM;
3131
108M
         ++enc_level)
3132
108M
        if (ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level)) {
3133
34.2M
            pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
3134
34.2M
            deadline = ossl_time_min(deadline,
3135
34.2M
                                     ossl_ackm_get_ack_deadline(txp->args.ackm, pn_space));
3136
34.2M
        }
3137
3138
    /* When will CC let us send more? */
3139
27.1M
    if (txp->args.cc_method->get_tx_allowance(txp->args.cc_data) == 0)
3140
22.7M
        deadline = ossl_time_min(deadline,
3141
22.7M
                                 txp->args.cc_method->get_wakeup_deadline(txp->args.cc_data));
3142
3143
27.1M
    return deadline;
3144
27.1M
}