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