Coverage Report

Created: 2025-08-28 07:07

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