Coverage Report

Created: 2025-12-31 06:58

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