QUIC TXP: Fix handling of FIN stream chunks
[openssl.git] / ssl / quic / quic_txp.c
1 /*
2  * Copyright 2022 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/common.h"
14 #include <openssl/err.h>
15
16 #define MIN_CRYPTO_HDR_SIZE             3
17
18 #define MIN_FRAME_SIZE_HANDSHAKE_DONE   1
19 #define MIN_FRAME_SIZE_MAX_DATA         2
20 #define MIN_FRAME_SIZE_ACK              5
21 #define MIN_FRAME_SIZE_CRYPTO           (MIN_CRYPTO_HDR_SIZE + 1)
22 #define MIN_FRAME_SIZE_STREAM           3 /* minimum useful size (for non-FIN) */
23 #define MIN_FRAME_SIZE_MAX_STREAMS_BIDI 2
24 #define MIN_FRAME_SIZE_MAX_STREAMS_UNI  2
25
26 struct ossl_quic_tx_packetiser_st {
27     OSSL_QUIC_TX_PACKETISER_ARGS args;
28
29     /*
30      * Opaque initial token blob provided by caller. TXP frees using the
31      * callback when it is no longer needed.
32      */
33     const unsigned char             *initial_token;
34     size_t                          initial_token_len;
35     ossl_quic_initial_token_free_fn *initial_token_free_cb;
36     void                            *initial_token_free_cb_arg;
37
38     /* Subcomponents of the TXP that we own. */
39     QUIC_FIFD       fifd;       /* QUIC Frame-in-Flight Dispatcher */
40
41     /* Internal state. */
42     uint64_t        next_pn[QUIC_PN_SPACE_NUM]; /* Next PN to use in given PN space. */
43     OSSL_TIME       last_tx_time;               /* Last time a packet was generated, or 0. */
44
45     /* Internal state - frame (re)generation flags. */
46     unsigned int    want_handshake_done     : 1;
47     unsigned int    want_max_data           : 1;
48     unsigned int    want_max_streams_bidi   : 1;
49     unsigned int    want_max_streams_uni    : 1;
50
51     /* Internal state - frame (re)generation flags - per PN space. */
52     unsigned int    want_ack                : QUIC_PN_SPACE_NUM;
53     unsigned int    force_ack_eliciting     : QUIC_PN_SPACE_NUM;
54
55     /*
56      * Internal state - connection close terminal state.
57      * Once this is set, it is not unset unlike other want_ flags - we keep
58      * sending it in every packet.
59      */
60     unsigned int    want_conn_close         : 1;
61
62     /* Has the handshake been completed? */
63     unsigned int    handshake_complete      : 1;
64
65     OSSL_QUIC_FRAME_CONN_CLOSE  conn_close_frame;
66
67     /* Internal state - packet assembly. */
68     unsigned char   *scratch;       /* scratch buffer for packet assembly */
69     size_t          scratch_len;    /* number of bytes allocated for scratch */
70     OSSL_QTX_IOVEC  *iovec;         /* scratch iovec array for use with QTX */
71     size_t          alloc_iovec;    /* size of iovec array */
72 };
73
74 /*
75  * The TX helper records state used while generating frames into packets. It
76  * enables serialization into the packet to be done "transactionally" where
77  * serialization of a frame can be rolled back if it fails midway (e.g. if it
78  * does not fit).
79  */
80 struct tx_helper {
81     OSSL_QUIC_TX_PACKETISER *txp;
82     /*
83      * The Maximum Packet Payload Length in bytes. This is the amount of
84      * space we have to generate frames into.
85      */
86     size_t max_ppl;
87     /*
88      * Number of bytes we have generated so far.
89      */
90     size_t bytes_appended;
91     /*
92      * Number of scratch bytes in txp->scratch we have used so far. Some iovecs
93      * will reference this scratch buffer. When we need to use more of it (e.g.
94      * when we need to put frame headers somewhere), we append to the scratch
95      * buffer, resizing if necessary, and increase this accordingly.
96      */
97     size_t scratch_bytes;
98     /*
99      * Bytes reserved in the MaxPPL budget. We keep this number of bytes spare
100      * until reserve_allowed is set to 1. Currently this is always at most 1, as
101      * a PING frame takes up one byte and this mechanism is only used to ensure
102      * we can encode a PING frame if we have been asked to ensure a packet is
103      * ACK-eliciting and we are unusure if we are going to add any other
104      * ACK-eliciting frames before we reach our MaxPPL budget.
105      */
106     size_t reserve;
107     /*
108      * Number of iovecs we have currently appended. This is the number of
109      * entries valid in txp->iovec.
110      */
111     size_t num_iovec;
112     /*
113      * Whether we are allowed to make use of the reserve bytes in our MaxPPL
114      * budget. This is used to ensure we have room to append a PING frame later
115      * if we need to. Once we know we will not need to append a PING frame, this
116      * is set to 1.
117      */
118     unsigned int reserve_allowed : 1;
119     /*
120      * Set to 1 if we have appended a STREAM frame with an implicit length. If
121      * this happens we should never append another frame after that frame as it
122      * cannot be validly encoded. This is just a safety check.
123      */
124     unsigned int done_implicit : 1;
125     struct {
126         /*
127          * The fields in this structure are valid if active is set, which means
128          * that a serialization transaction is currently in progress.
129          */
130         unsigned char   *data;
131         WPACKET         wpkt;
132         unsigned int    active : 1;
133     } txn;
134 };
135
136 static void tx_helper_rollback(struct tx_helper *h);
137 static int txp_ensure_iovec(OSSL_QUIC_TX_PACKETISER *txp, size_t num);
138
139 /* Initialises the TX helper. */
140 static int tx_helper_init(struct tx_helper *h, OSSL_QUIC_TX_PACKETISER *txp,
141                           size_t max_ppl, size_t reserve)
142 {
143     if (reserve > max_ppl)
144         return 0;
145
146     h->txp                  = txp;
147     h->max_ppl              = max_ppl;
148     h->reserve              = reserve;
149     h->num_iovec            = 0;
150     h->bytes_appended       = 0;
151     h->scratch_bytes        = 0;
152     h->reserve_allowed      = 0;
153     h->done_implicit        = 0;
154     h->txn.data             = NULL;
155     h->txn.active           = 0;
156
157     if (max_ppl > h->txp->scratch_len) {
158         unsigned char *scratch;
159
160         scratch = OPENSSL_realloc(h->txp->scratch, max_ppl);
161         if (scratch == NULL)
162             return 0;
163
164         h->txp->scratch     = scratch;
165         h->txp->scratch_len = max_ppl;
166     }
167
168     return 1;
169 }
170
171 static void tx_helper_cleanup(struct tx_helper *h)
172 {
173     if (h->txn.active)
174         tx_helper_rollback(h);
175
176     h->txp = NULL;
177 }
178
179 static void tx_helper_unrestrict(struct tx_helper *h)
180 {
181     h->reserve_allowed = 1;
182 }
183
184 /*
185  * Append an extent of memory to the iovec list. The memory must remain
186  * allocated until we finish generating the packet and call the QTX.
187  *
188  * In general, the buffers passed to this function will be from one of two
189  * ranges:
190  *
191  *   - Application data contained in stream buffers managed elsewhere
192  *     in the QUIC stack; or
193  *
194  *   - Control frame data appended into txp->scratch using tx_helper_begin and
195  *     tx_helper_commit.
196  *
197  */
198 static int tx_helper_append_iovec(struct tx_helper *h,
199                                   const unsigned char *buf,
200                                   size_t buf_len)
201 {
202     if (buf_len == 0)
203         return 1;
204
205     if (!ossl_assert(!h->done_implicit))
206         return 0;
207
208     if (!txp_ensure_iovec(h->txp, h->num_iovec + 1))
209         return 0;
210
211     h->txp->iovec[h->num_iovec].buf     = buf;
212     h->txp->iovec[h->num_iovec].buf_len = buf_len;
213
214     ++h->num_iovec;
215     h->bytes_appended += buf_len;
216     return 1;
217 }
218
219 /*
220  * How many more bytes of space do we have left in our plaintext packet payload?
221  */
222 static size_t tx_helper_get_space_left(struct tx_helper *h)
223 {
224     return h->max_ppl
225         - (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
226 }
227
228 /*
229  * Begin a control frame serialization transaction. This allows the
230  * serialization of the control frame to be backed out if it turns out it won't
231  * fit. Write the control frame to the returned WPACKET. Ensure you always
232  * call tx_helper_rollback or tx_helper_commit (or tx_helper_cleanup). Returns
233  * NULL on failure.
234  */
235 static WPACKET *tx_helper_begin(struct tx_helper *h)
236 {
237     size_t space_left, len;
238     unsigned char *data;
239
240     if (!ossl_assert(!h->txn.active))
241         return NULL;
242
243     if (!ossl_assert(!h->done_implicit))
244         return NULL;
245
246     data = (unsigned char *)h->txp->scratch + h->scratch_bytes;
247     len  = h->txp->scratch_len - h->scratch_bytes;
248
249     space_left = tx_helper_get_space_left(h);
250     if (!ossl_assert(space_left <= len))
251         return NULL;
252
253     if (!WPACKET_init_static_len(&h->txn.wpkt, data, len, 0))
254         return NULL;
255
256     if (!WPACKET_set_max_size(&h->txn.wpkt, space_left)) {
257         WPACKET_cleanup(&h->txn.wpkt);
258         return NULL;
259     }
260
261     h->txn.data     = data;
262     h->txn.active   = 1;
263     return &h->txn.wpkt;
264 }
265
266 static void tx_helper_end(struct tx_helper *h, int success)
267 {
268     if (success)
269         WPACKET_finish(&h->txn.wpkt);
270     else
271         WPACKET_cleanup(&h->txn.wpkt);
272
273     h->txn.active       = 0;
274     h->txn.data         = NULL;
275 }
276
277 /* Abort a control frame serialization transaction. */
278 static void tx_helper_rollback(struct tx_helper *h)
279 {
280     if (!h->txn.active)
281         return;
282
283     tx_helper_end(h, 0);
284 }
285
286 /* Commit a control frame. */
287 static int tx_helper_commit(struct tx_helper *h)
288 {
289     size_t l = 0;
290
291     if (!h->txn.active)
292         return 0;
293
294     if (!WPACKET_get_total_written(&h->txn.wpkt, &l)) {
295         tx_helper_end(h, 0);
296         return 0;
297     }
298
299     if (!tx_helper_append_iovec(h, h->txn.data, l)) {
300         tx_helper_end(h, 0);
301         return 0;
302     }
303
304     h->scratch_bytes += l;
305     tx_helper_end(h, 1);
306     return 1;
307 }
308
309 static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
310                                        void *arg);
311 static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
312                             QUIC_TXPIM_PKT *pkt, void *arg);
313 static int sstream_is_pending(QUIC_SSTREAM *sstream);
314 static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
315                           uint32_t archetype,
316                           uint32_t *conn_close_enc_level);
317 static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
318                                uint32_t archetype,
319                                int is_last_in_dgram,
320                                int dgram_contains_initial,
321                                int chosen_for_conn_close);
322 static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp);
323 static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
324                                      size_t pl,
325                                      uint32_t enc_level,
326                                      size_t hdr_len,
327                                      size_t *r);
328 static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp);
329 static int txp_generate_for_el_actual(OSSL_QUIC_TX_PACKETISER *txp,
330                                       uint32_t enc_level,
331                                       uint32_t archetype,
332                                       size_t min_ppl,
333                                       size_t max_ppl,
334                                       size_t pkt_overhead,
335                                       QUIC_PKT_HDR *phdr,
336                                       int chosen_for_conn_close);
337
338 OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args)
339 {
340     OSSL_QUIC_TX_PACKETISER *txp;
341
342     if (args == NULL
343         || args->qtx == NULL
344         || args->txpim == NULL
345         || args->cfq == NULL
346         || args->ackm == NULL
347         || args->qsm == NULL
348         || args->conn_txfc == NULL
349         || args->conn_rxfc == NULL) {
350         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
351         return NULL;
352     }
353
354     txp = OPENSSL_zalloc(sizeof(*txp));
355     if (txp == NULL)
356         return NULL;
357
358     txp->args           = *args;
359     txp->last_tx_time   = ossl_time_zero();
360
361     if (!ossl_quic_fifd_init(&txp->fifd,
362                              txp->args.cfq, txp->args.ackm, txp->args.txpim,
363                              get_sstream_by_id, txp,
364                              on_regen_notify, txp)) {
365         OPENSSL_free(txp);
366         return NULL;
367     }
368
369     return txp;
370 }
371
372 void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp)
373 {
374     if (txp == NULL)
375         return;
376
377     ossl_quic_tx_packetiser_set_initial_token(txp, NULL, 0, NULL, NULL);
378     ossl_quic_fifd_cleanup(&txp->fifd);
379     OPENSSL_free(txp->iovec);
380     OPENSSL_free(txp->conn_close_frame.reason);
381     OPENSSL_free(txp->scratch);
382     OPENSSL_free(txp);
383 }
384
385 void ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
386                                                const unsigned char *token,
387                                                size_t token_len,
388                                                ossl_quic_initial_token_free_fn *free_cb,
389                                                void *free_cb_arg)
390 {
391     if (txp->initial_token != NULL && txp->initial_token_free_cb != NULL)
392         txp->initial_token_free_cb(txp->initial_token, txp->initial_token_len,
393                                    txp->initial_token_free_cb_arg);
394
395     txp->initial_token              = token;
396     txp->initial_token_len          = token_len;
397     txp->initial_token_free_cb      = free_cb;
398     txp->initial_token_free_cb_arg  = free_cb_arg;
399 }
400
401 int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
402                                          const QUIC_CONN_ID *dcid)
403 {
404     if (dcid == NULL) {
405         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
406         return 0;
407     }
408
409     txp->args.cur_dcid = *dcid;
410     return 1;
411 }
412
413 int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
414                                          const QUIC_CONN_ID *scid)
415 {
416     if (scid == NULL) {
417         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
418         return 0;
419     }
420
421     txp->args.cur_scid = *scid;
422     return 1;
423 }
424
425 /* Change the destination L4 address the TXP uses to send datagrams. */
426 int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
427                                      const BIO_ADDR *peer)
428 {
429     if (peer == NULL) {
430         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
431         return 0;
432     }
433
434     txp->args.peer = *peer;
435     return 1;
436 }
437
438 int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
439                                               uint32_t enc_level)
440 {
441     if (enc_level >= QUIC_ENC_LEVEL_NUM) {
442         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
443         return 0;
444     }
445
446     if (enc_level != QUIC_ENC_LEVEL_0RTT)
447         txp->args.crypto[ossl_quic_enc_level_to_pn_space(enc_level)] = NULL;
448
449     return 1;
450 }
451
452 void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp)
453 {
454     txp->handshake_complete = 1;
455 }
456
457 void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp)
458 {
459     txp->want_handshake_done = 1;
460 }
461
462 void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
463                                                     uint32_t pn_space)
464 {
465     txp->force_ack_eliciting |= (1UL << pn_space);
466 }
467
468 #define TXP_ERR_INTERNAL     0  /* Internal (e.g. alloc) error */
469 #define TXP_ERR_SUCCESS      1  /* Success */
470 #define TXP_ERR_SPACE        2  /* Not enough room for another packet */
471 #define TXP_ERR_INPUT        3  /* Invalid/malformed input */
472
473 int ossl_quic_tx_packetiser_has_pending(OSSL_QUIC_TX_PACKETISER *txp,
474                                         uint32_t archetype,
475                                         uint32_t flags)
476 {
477     uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
478     int bypass_cc = ((flags & TX_PACKETISER_BYPASS_CC) != 0);
479
480     if (!bypass_cc && !txp->args.cc_method->can_send(txp->args.cc_data))
481         return 0;
482
483     for (enc_level = QUIC_ENC_LEVEL_INITIAL;
484          enc_level < QUIC_ENC_LEVEL_NUM;
485          ++enc_level)
486         if (txp_el_pending(txp, enc_level, archetype, &conn_close_enc_level))
487             return 1;
488
489     return 0;
490 }
491
492 /*
493  * Generates a datagram by polling the various ELs to determine if they want to
494  * generate any frames, and generating a datagram which coalesces packets for
495  * any ELs which do.
496  */
497 int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
498                                      uint32_t archetype)
499 {
500     uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
501     int have_pkt_for_el[QUIC_ENC_LEVEL_NUM], is_last_in_dgram;
502     size_t num_el_in_dgram = 0, pkts_done = 0;
503     int rc;
504
505     if (!txp->args.cc_method->can_send(txp->args.cc_data))
506         return TX_PACKETISER_RES_NO_PKT;
507
508     for (enc_level = QUIC_ENC_LEVEL_INITIAL;
509          enc_level < QUIC_ENC_LEVEL_NUM;
510          ++enc_level) {
511         have_pkt_for_el[enc_level] = txp_el_pending(txp, enc_level, archetype,
512                                                     &conn_close_enc_level);
513         if (have_pkt_for_el[enc_level])
514             ++num_el_in_dgram;
515     }
516
517     if (num_el_in_dgram == 0)
518         return TX_PACKETISER_RES_NO_PKT;
519
520     /*
521      * Should not be needed, but a sanity check in case anyone else has been
522      * using the QTX.
523      */
524     ossl_qtx_finish_dgram(txp->args.qtx);
525
526     for (enc_level = QUIC_ENC_LEVEL_INITIAL;
527          enc_level < QUIC_ENC_LEVEL_NUM;
528          ++enc_level) {
529         if (!have_pkt_for_el[enc_level])
530             continue;
531
532         is_last_in_dgram = (pkts_done + 1 == num_el_in_dgram);
533         rc = txp_generate_for_el(txp, enc_level, archetype, is_last_in_dgram,
534                                  have_pkt_for_el[QUIC_ENC_LEVEL_INITIAL],
535                                  enc_level == conn_close_enc_level);
536
537         if (rc != TXP_ERR_SUCCESS) {
538             /*
539              * If we already successfully did at least one, make sure we report
540              * this via the return code.
541              */
542             if (pkts_done > 0)
543                 break;
544             else
545                 return TX_PACKETISER_RES_FAILURE;
546         }
547
548         ++pkts_done;
549     }
550
551     ossl_qtx_finish_dgram(txp->args.qtx);
552     return TX_PACKETISER_RES_SENT_PKT;
553 }
554
555 struct archetype_data {
556     unsigned int allow_ack                  : 1;
557     unsigned int allow_ping                 : 1;
558     unsigned int allow_crypto               : 1;
559     unsigned int allow_handshake_done       : 1;
560     unsigned int allow_path_challenge       : 1;
561     unsigned int allow_path_response        : 1;
562     unsigned int allow_new_conn_id          : 1;
563     unsigned int allow_retire_conn_id       : 1;
564     unsigned int allow_stream_rel           : 1;
565     unsigned int allow_conn_fc              : 1;
566     unsigned int allow_conn_close           : 1;
567     unsigned int allow_cfq_other            : 1;
568     unsigned int allow_new_token            : 1;
569     unsigned int allow_force_ack_eliciting  : 1;
570 };
571
572 static const struct archetype_data archetypes[QUIC_ENC_LEVEL_NUM][TX_PACKETISER_ARCHETYPE_NUM] = {
573     /* EL 0(INITIAL) */
574     {
575         /* EL 0(INITIAL) - Archetype 0(NORMAL) */
576         {
577             /*allow_ack                       =*/ 1,
578             /*allow_ping                      =*/ 1,
579             /*allow_crypto                    =*/ 1,
580             /*allow_handshake_done            =*/ 0,
581             /*allow_path_challenge            =*/ 0,
582             /*allow_path_response             =*/ 0,
583             /*allow_new_conn_id               =*/ 0,
584             /*allow_retire_conn_id            =*/ 0,
585             /*allow_stream_rel                =*/ 0,
586             /*allow_conn_fc                   =*/ 0,
587             /*allow_conn_close                =*/ 1,
588             /*allow_cfq_other                 =*/ 1,
589             /*allow_new_token                 =*/ 0,
590             /*allow_force_ack_eliciting       =*/ 1,
591         },
592         /* EL 0(INITIAL) - Archetype 1(ACK_ONLY) */
593         {
594             /*allow_ack                       =*/ 1,
595             /*allow_ping                      =*/ 0,
596             /*allow_crypto                    =*/ 0,
597             /*allow_handshake_done            =*/ 0,
598             /*allow_path_challenge            =*/ 0,
599             /*allow_path_response             =*/ 0,
600             /*allow_new_conn_id               =*/ 0,
601             /*allow_retire_conn_id            =*/ 0,
602             /*allow_stream_rel                =*/ 0,
603             /*allow_conn_fc                   =*/ 0,
604             /*allow_conn_close                =*/ 0,
605             /*allow_cfq_other                 =*/ 0,
606             /*allow_new_token                 =*/ 0,
607             /*allow_force_ack_eliciting       =*/ 1,
608         },
609     },
610     /* EL 1(HANDSHAKE) */
611     {
612         /* EL 1(HANDSHAKE) - Archetype 0(NORMAL) */
613         {
614             /*allow_ack                       =*/ 1,
615             /*allow_ping                      =*/ 1,
616             /*allow_crypto                    =*/ 1,
617             /*allow_handshake_done            =*/ 0,
618             /*allow_path_challenge            =*/ 0,
619             /*allow_path_response             =*/ 0,
620             /*allow_new_conn_id               =*/ 0,
621             /*allow_retire_conn_id            =*/ 0,
622             /*allow_stream_rel                =*/ 0,
623             /*allow_conn_fc                   =*/ 0,
624             /*allow_conn_close                =*/ 1,
625             /*allow_cfq_other                 =*/ 1,
626             /*allow_new_token                 =*/ 0,
627             /*allow_force_ack_eliciting       =*/ 1,
628         },
629         /* EL 1(HANDSHAKE) - Archetype 1(ACK_ONLY) */
630         {
631             /*allow_ack                       =*/ 1,
632             /*allow_ping                      =*/ 0,
633             /*allow_crypto                    =*/ 0,
634             /*allow_handshake_done            =*/ 0,
635             /*allow_path_challenge            =*/ 0,
636             /*allow_path_response             =*/ 0,
637             /*allow_new_conn_id               =*/ 0,
638             /*allow_retire_conn_id            =*/ 0,
639             /*allow_stream_rel                =*/ 0,
640             /*allow_conn_fc                   =*/ 0,
641             /*allow_conn_close                =*/ 0,
642             /*allow_cfq_other                 =*/ 0,
643             /*allow_new_token                 =*/ 0,
644             /*allow_force_ack_eliciting       =*/ 1,
645         },
646     },
647     /* EL 2(0RTT) */
648     {
649         /* EL 2(0RTT) - Archetype 0(NORMAL) */
650         {
651             /*allow_ack                       =*/ 0,
652             /*allow_ping                      =*/ 1,
653             /*allow_crypto                    =*/ 0,
654             /*allow_handshake_done            =*/ 0,
655             /*allow_path_challenge            =*/ 0,
656             /*allow_path_response             =*/ 0,
657             /*allow_new_conn_id               =*/ 1,
658             /*allow_retire_conn_id            =*/ 1,
659             /*allow_stream_rel                =*/ 1,
660             /*allow_conn_fc                   =*/ 1,
661             /*allow_conn_close                =*/ 1,
662             /*allow_cfq_other                 =*/ 0,
663             /*allow_new_token                 =*/ 0,
664             /*allow_force_ack_eliciting       =*/ 0,
665         },
666         /* EL 2(0RTT) - Archetype 1(ACK_ONLY) */
667         {
668             /*allow_ack                       =*/ 0,
669             /*allow_ping                      =*/ 0,
670             /*allow_crypto                    =*/ 0,
671             /*allow_handshake_done            =*/ 0,
672             /*allow_path_challenge            =*/ 0,
673             /*allow_path_response             =*/ 0,
674             /*allow_new_conn_id               =*/ 0,
675             /*allow_retire_conn_id            =*/ 0,
676             /*allow_stream_rel                =*/ 0,
677             /*allow_conn_fc                   =*/ 0,
678             /*allow_conn_close                =*/ 0,
679             /*allow_cfq_other                 =*/ 0,
680             /*allow_new_token                 =*/ 0,
681             /*allow_force_ack_eliciting       =*/ 0,
682         },
683     },
684     /* EL 3(1RTT) */
685     {
686         /* EL 3(1RTT) - Archetype 0(NORMAL) */
687         {
688             /*allow_ack                       =*/ 1,
689             /*allow_ping                      =*/ 1,
690             /*allow_crypto                    =*/ 1,
691             /*allow_handshake_done            =*/ 1,
692             /*allow_path_challenge            =*/ 0,
693             /*allow_path_response             =*/ 0,
694             /*allow_new_conn_id               =*/ 1,
695             /*allow_retire_conn_id            =*/ 1,
696             /*allow_stream_rel                =*/ 1,
697             /*allow_conn_fc                   =*/ 1,
698             /*allow_conn_close                =*/ 1,
699             /*allow_cfq_other                 =*/ 1,
700             /*allow_new_token                 =*/ 1,
701             /*allow_force_ack_eliciting       =*/ 1,
702         },
703         /* EL 3(1RTT) - Archetype 1(ACK_ONLY) */
704         {
705             /*allow_ack                       =*/ 1,
706             /*allow_ping                      =*/ 0,
707             /*allow_crypto                    =*/ 0,
708             /*allow_handshake_done            =*/ 0,
709             /*allow_path_challenge            =*/ 0,
710             /*allow_path_response             =*/ 0,
711             /*allow_new_conn_id               =*/ 0,
712             /*allow_retire_conn_id            =*/ 0,
713             /*allow_stream_rel                =*/ 0,
714             /*allow_conn_fc                   =*/ 0,
715             /*allow_conn_close                =*/ 0,
716             /*allow_cfq_other                 =*/ 0,
717             /*allow_new_token                 =*/ 0,
718             /*allow_force_ack_eliciting       =*/ 1,
719         }
720     }
721 };
722
723 static int txp_get_archetype_data(uint32_t enc_level,
724                                   uint32_t archetype,
725                                   struct archetype_data *a)
726 {
727     if (enc_level >= QUIC_ENC_LEVEL_NUM
728         || archetype >= TX_PACKETISER_ARCHETYPE_NUM)
729         return 0;
730
731     /* No need to avoid copying this as it should not exceed one int in size. */
732     *a = archetypes[enc_level][archetype];
733     return 1;
734 }
735
736 /*
737  * Returns 1 if the given EL wants to produce one or more frames.
738  * Always returns 0 if the given EL is discarded.
739  */
740 static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
741                           uint32_t archetype,
742                           uint32_t *conn_close_enc_level)
743 {
744     struct archetype_data a;
745     uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
746     QUIC_CFQ_ITEM *cfq_item;
747
748     if (!ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level))
749         return 0;
750
751     if (*conn_close_enc_level > enc_level)
752         *conn_close_enc_level = enc_level;
753
754     if (!txp_get_archetype_data(enc_level, archetype, &a))
755         return 0;
756
757     /* Does the crypto stream for this EL want to produce anything? */
758     if (a.allow_crypto && sstream_is_pending(txp->args.crypto[pn_space]))
759         return 1;
760
761     /* Does the ACKM for this PN space want to produce anything? */
762     if (a.allow_ack && (ossl_ackm_is_ack_desired(txp->args.ackm, pn_space)
763                         || (txp->want_ack & (1UL << pn_space)) != 0))
764         return 1;
765
766     /* Do we need to force emission of an ACK-eliciting packet? */
767     if (a.allow_force_ack_eliciting
768         && (txp->force_ack_eliciting & (1UL << pn_space)) != 0)
769         return 1;
770
771     /* Does the connection-level RXFC want to produce a frame? */
772     if (a.allow_conn_fc && (txp->want_max_data
773         || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0)))
774         return 1;
775
776     /* Do we want to produce a MAX_STREAMS frame? */
777     if (a.allow_conn_fc && (txp->want_max_streams_bidi
778                             || txp->want_max_streams_uni))
779         return 1;
780
781     /* Do we want to produce a HANDSHAKE_DONE frame? */
782     if (a.allow_handshake_done && txp->want_handshake_done)
783         return 1;
784
785     /* Do we want to produce a CONNECTION_CLOSE frame? */
786     if (a.allow_conn_close && txp->want_conn_close &&
787         *conn_close_enc_level == enc_level)
788         /*
789          * This is a bit of a special case since CONNECTION_CLOSE can appear in
790          * most packet types, and when we decide we want to send it this status
791          * isn't tied to a specific EL. So if we want to send it, we send it
792          * only on the lowest non-dropped EL.
793          */
794         return 1;
795
796     /* Does the CFQ have any frames queued for this PN space? */
797     if (enc_level != QUIC_ENC_LEVEL_0RTT)
798         for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
799              cfq_item != NULL;
800              cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
801             uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
802
803             switch (frame_type) {
804             case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
805                 if (a.allow_new_conn_id)
806                     return 1;
807                 break;
808             case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
809                 if (a.allow_retire_conn_id)
810                     return 1;
811                 break;
812             case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
813                 if (a.allow_new_token)
814                     return 1;
815                 break;
816             default:
817                 if (a.allow_cfq_other)
818                     return 1;
819                 break;
820             }
821        }
822
823     if (a.allow_stream_rel && txp->handshake_complete) {
824         QUIC_STREAM_ITER it;
825
826         /* If there are any active streams, 0/1-RTT wants to produce a packet.
827          * Whether a stream is on the active list is required to be precise
828          * (i.e., a stream is never on the active list if we cannot produce a
829          * frame for it), and all stream-related frames are governed by
830          * a.allow_stream_rel (i.e., if we can send one type of stream-related
831          * frame, we can send any of them), so we don't need to inspect
832          * individual streams on the active list, just confirm that the active
833          * list is non-empty.
834          */
835         ossl_quic_stream_iter_init(&it, txp->args.qsm, 0);
836         if (it.stream != NULL)
837             return 1;
838     }
839
840     return 0;
841 }
842
843 static int sstream_is_pending(QUIC_SSTREAM *sstream)
844 {
845     OSSL_QUIC_FRAME_STREAM hdr;
846     OSSL_QTX_IOVEC iov[2];
847     size_t num_iov = OSSL_NELEM(iov);
848
849     return ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov);
850 }
851
852 /*
853  * Generates a packet for a given EL, coalescing it into the current datagram.
854  *
855  * is_last_in_dgram and dgram_contains_initial are used to determine padding
856  * requirements.
857  *
858  * Returns TXP_ERR_* value.
859  */
860 static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
861                                uint32_t archetype,
862                                int is_last_in_dgram,
863                                int dgram_contains_initial,
864                                int chosen_for_conn_close)
865 {
866     int must_pad = dgram_contains_initial && is_last_in_dgram;
867     size_t min_dpl, min_pl, min_ppl, cmpl, cmppl, running_total;
868     size_t mdpl, hdr_len, pkt_overhead, cc_limit;
869     uint64_t cc_limit_;
870     QUIC_PKT_HDR phdr;
871     OSSL_TIME time_since_last;
872
873     /* Determine the limit CC imposes on what we can send. */
874     if (ossl_time_is_zero(txp->last_tx_time))
875         time_since_last = ossl_time_zero();
876     else
877         time_since_last = ossl_time_subtract(txp->args.now(txp->args.now_arg),
878                                              txp->last_tx_time);
879
880     cc_limit_ = txp->args.cc_method->get_send_allowance(txp->args.cc_data,
881                                                         time_since_last,
882                                                         ossl_time_is_zero(time_since_last));
883
884     cc_limit = (cc_limit_ > SIZE_MAX ? SIZE_MAX : (size_t)cc_limit_);
885
886     /* Assemble packet header. */
887     phdr.type           = ossl_quic_enc_level_to_pkt_type(enc_level);
888     phdr.spin_bit       = 0;
889     phdr.pn_len         = txp_determine_pn_len(txp);
890     phdr.partial        = 0;
891     phdr.fixed          = 1;
892     phdr.version        = QUIC_VERSION_1;
893     phdr.dst_conn_id    = txp->args.cur_dcid;
894     phdr.src_conn_id    = txp->args.cur_scid;
895
896     /*
897      * We need to know the length of the payload to get an accurate header
898      * length for non-1RTT packets, because the Length field found in
899      * Initial/Handshake/0-RTT packets uses a variable-length encoding. However,
900      * we don't have a good idea of the length of our payload, because the
901      * length of the payload depends on the room in the datagram after fitting
902      * the header, which depends on the size of the header.
903      *
904      * In general, it does not matter if a packet is slightly shorter (because
905      * e.g. we predicted use of a 2-byte length field, but ended up only needing
906      * a 1-byte length field). However this does matter for Initial packets
907      * which must be at least 1200 bytes, which is also the assumed default MTU;
908      * therefore in many cases Initial packets will be padded to 1200 bytes,
909      * which means if we overestimated the header size, we will be short by a
910      * few bytes and the server will ignore the packet for being too short. In
911      * this case, however, such packets always *will* be padded to meet 1200
912      * bytes, which requires a 2-byte length field, so we don't actually need to
913      * worry about this. Thus we estimate the header length assuming a 2-byte
914      * length field here, which should in practice work well in all cases.
915      */
916     phdr.len            = OSSL_QUIC_VLINT_2B_MAX - phdr.pn_len;
917
918     if (enc_level == QUIC_ENC_LEVEL_INITIAL) {
919         phdr.token      = txp->initial_token;
920         phdr.token_len  = txp->initial_token_len;
921     } else {
922         phdr.token      = NULL;
923         phdr.token_len  = 0;
924     }
925
926     hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(phdr.dst_conn_id.id_len,
927                                                      &phdr);
928     if (hdr_len == 0)
929         return TXP_ERR_INPUT;
930
931     /* MinDPL: Minimum total datagram payload length. */
932     min_dpl = must_pad ? QUIC_MIN_INITIAL_DGRAM_LEN : 0;
933
934     /* How much data is already in the current datagram? */
935     running_total = ossl_qtx_get_cur_dgram_len_bytes(txp->args.qtx);
936
937     /* MinPL: Minimum length of the fully encoded packet. */
938     min_pl = running_total < min_dpl ? min_dpl - running_total : 0;
939     if ((uint64_t)min_pl > cc_limit)
940         /*
941          * Congestion control does not allow us to send a packet of adequate
942          * size.
943          */
944         return TXP_ERR_SPACE;
945
946     /* MinPPL: Minimum plaintext payload length needed to meet MinPL. */
947     if (!txp_determine_ppl_from_pl(txp, min_pl, enc_level, hdr_len, &min_ppl))
948         /* MinPL is less than a valid packet size, so just use a MinPPL of 0. */
949         min_ppl = 0;
950
951     /* MDPL: Maximum datagram payload length. */
952     mdpl = txp_get_mdpl(txp);
953
954     /*
955      * CMPL: Maximum encoded packet size we can put into this datagram given any
956      * previous packets coalesced into it.
957      */
958     if (running_total > mdpl)
959         /* Should not be possible, but if it happens: */
960         cmpl = 0;
961     else
962         cmpl = mdpl - running_total;
963
964     /* Clamp CMPL based on congestion control limit. */
965     if (cmpl > cc_limit)
966         cmpl = cc_limit;
967
968     /* CMPPL: Maximum amount we can put into the current datagram payload. */
969     if (!txp_determine_ppl_from_pl(txp, cmpl, enc_level, hdr_len, &cmppl))
970         return TXP_ERR_SPACE;
971
972     /* Packet overhead (size of headers, AEAD tag, etc.) */
973     pkt_overhead = cmpl - cmppl;
974
975     return txp_generate_for_el_actual(txp, enc_level, archetype, min_ppl, cmppl,
976                                       pkt_overhead, &phdr,
977                                       chosen_for_conn_close);
978 }
979
980 /* Determine how many bytes we should use for the encoded PN. */
981 static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp)
982 {
983     return 4; /* TODO(QUIC) */
984 }
985
986 /* Determine plaintext packet payload length from payload length. */
987 static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
988                                      size_t pl,
989                                      uint32_t enc_level,
990                                      size_t hdr_len,
991                                      size_t *r)
992 {
993     if (pl < hdr_len)
994         return 0;
995
996     pl -= hdr_len;
997
998     if (!ossl_qtx_calculate_plaintext_payload_len(txp->args.qtx, enc_level,
999                                                   pl, &pl))
1000         return 0;
1001
1002     *r = pl;
1003     return 1;
1004 }
1005
1006 static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp)
1007 {
1008     return ossl_qtx_get_mdpl(txp->args.qtx);
1009 }
1010
1011 static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
1012                                        void *arg)
1013 {
1014     OSSL_QUIC_TX_PACKETISER *txp = arg;
1015     QUIC_STREAM *s;
1016
1017     if (stream_id == UINT64_MAX)
1018         return txp->args.crypto[pn_space];
1019
1020     s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1021     if (s == NULL)
1022         return NULL;
1023
1024     return s->sstream;
1025 }
1026
1027 static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
1028                             QUIC_TXPIM_PKT *pkt, void *arg)
1029 {
1030     OSSL_QUIC_TX_PACKETISER *txp = arg;
1031
1032     switch (frame_type) {
1033         case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1034             txp->want_handshake_done = 1;
1035             break;
1036         case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1037             txp->want_max_data = 1;
1038             break;
1039         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1040             txp->want_max_streams_bidi = 1;
1041             break;
1042         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1043             txp->want_max_streams_uni = 1;
1044             break;
1045         case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1046             txp->want_ack |= (1UL << pkt->ackm_pkt.pkt_space);
1047             break;
1048         case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1049             {
1050                 QUIC_STREAM *s
1051                     = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1052
1053                 if (s == NULL)
1054                     return;
1055
1056                 s->want_max_stream_data = 1;
1057                 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1058             }
1059             break;
1060         case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1061             {
1062                 QUIC_STREAM *s
1063                     = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1064
1065                 if (s == NULL)
1066                     return;
1067
1068                 s->want_stop_sending = 1;
1069                 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1070             }
1071             break;
1072         case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1073             {
1074                 QUIC_STREAM *s
1075                     = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1076
1077                 if (s == NULL)
1078                     return;
1079
1080                 s->want_reset_stream = 1;
1081                 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1082             }
1083             break;
1084         default:
1085             assert(0);
1086             break;
1087     }
1088 }
1089
1090 static int txp_generate_pre_token(OSSL_QUIC_TX_PACKETISER *txp,
1091                                   struct tx_helper *h,
1092                                   QUIC_TXPIM_PKT *tpkt,
1093                                   uint32_t pn_space,
1094                                   struct archetype_data *a,
1095                                   int chosen_for_conn_close)
1096 {
1097     const OSSL_QUIC_FRAME_ACK *ack;
1098     OSSL_QUIC_FRAME_ACK ack2;
1099
1100     tpkt->ackm_pkt.largest_acked = QUIC_PN_INVALID;
1101
1102     /* ACK Frames (Regenerate) */
1103     if (a->allow_ack
1104         && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_ACK
1105         && (txp->want_ack
1106             || ossl_ackm_is_ack_desired(txp->args.ackm, pn_space))
1107         && (ack = ossl_ackm_get_ack_frame(txp->args.ackm, pn_space)) != NULL) {
1108         WPACKET *wpkt = tx_helper_begin(h);
1109
1110         if (wpkt == NULL)
1111             return 0;
1112
1113         /* We do not currently support ECN */
1114         ack2 = *ack;
1115         ack2.ecn_present = 0;
1116
1117         if (ossl_quic_wire_encode_frame_ack(wpkt,
1118                                             txp->args.ack_delay_exponent,
1119                                             &ack2)) {
1120             if (!tx_helper_commit(h))
1121                 return 0;
1122
1123             tpkt->had_ack_frame = 1;
1124
1125             if (ack->num_ack_ranges > 0)
1126                 tpkt->ackm_pkt.largest_acked = ack->ack_ranges[0].end;
1127         } else {
1128             tx_helper_rollback(h);
1129         }
1130     }
1131
1132     /* CONNECTION_CLOSE Frames (Regenerate) */
1133     if (a->allow_conn_close && txp->want_conn_close && chosen_for_conn_close) {
1134         WPACKET *wpkt = tx_helper_begin(h);
1135
1136         if (wpkt == NULL)
1137             return 0;
1138
1139         if (ossl_quic_wire_encode_frame_conn_close(wpkt,
1140                                                    &txp->conn_close_frame)) {
1141             if (!tx_helper_commit(h))
1142                 return 0;
1143         } else {
1144             tx_helper_rollback(h);
1145         }
1146     }
1147
1148     return 1;
1149 }
1150
1151 static int try_len(size_t space_left, size_t orig_len,
1152                    size_t base_hdr_len, size_t lenbytes,
1153                    uint64_t maxn, size_t *hdr_len, size_t *payload_len)
1154 {
1155     size_t n;
1156     size_t maxn_ = maxn > SIZE_MAX ? SIZE_MAX : (size_t)maxn;
1157
1158     *hdr_len = base_hdr_len + lenbytes;
1159
1160     if (orig_len == 0 && space_left >= *hdr_len) {
1161         *payload_len = 0;
1162         return 1;
1163     }
1164
1165     n = orig_len;
1166     if (n > maxn_)
1167         n = maxn_;
1168     if (n + *hdr_len > space_left)
1169         n = (space_left >= *hdr_len) ? space_left - *hdr_len : 0;
1170
1171     *payload_len = n;
1172     return n > 0;
1173 }
1174
1175 static int determine_len(size_t space_left, size_t orig_len,
1176                          size_t base_hdr_len,
1177                          uint64_t *hlen, uint64_t *len)
1178 {
1179     int ok = 0;
1180     size_t chosen_payload_len = 0;
1181     size_t chosen_hdr_len     = 0;
1182     size_t payload_len[4], hdr_len[4];
1183     int i, valid[4] = {0};
1184
1185     valid[0] = try_len(space_left, orig_len, base_hdr_len,
1186                        1, OSSL_QUIC_VLINT_1B_MAX,
1187                        &hdr_len[0], &payload_len[0]);
1188     valid[1] = try_len(space_left, orig_len, base_hdr_len,
1189                        2, OSSL_QUIC_VLINT_2B_MAX,
1190                        &hdr_len[1], &payload_len[1]);
1191     valid[2] = try_len(space_left, orig_len, base_hdr_len,
1192                        4, OSSL_QUIC_VLINT_4B_MAX,
1193                        &hdr_len[2], &payload_len[2]);
1194     valid[3] = try_len(space_left, orig_len, base_hdr_len,
1195                        8, OSSL_QUIC_VLINT_8B_MAX,
1196                        &hdr_len[3], &payload_len[3]);
1197
1198    for (i = OSSL_NELEM(valid) - 1; i >= 0; --i)
1199         if (valid[i] && payload_len[i] >= chosen_payload_len) {
1200             chosen_payload_len = payload_len[i];
1201             chosen_hdr_len     = hdr_len[i];
1202             ok                 = 1;
1203         }
1204
1205     *hlen = chosen_hdr_len;
1206     *len  = chosen_payload_len;
1207     return ok;
1208 }
1209
1210 /*
1211  * Given a CRYPTO frame header with accurate chdr->len and a budget
1212  * (space_left), try to find the optimal value of chdr->len to fill as much of
1213  * the budget as possible. This is slightly hairy because larger values of
1214  * chdr->len cause larger encoded sizes of the length field of the frame, which
1215  * in turn mean less space available for payload data. We check all possible
1216  * encodings and choose the optimal encoding.
1217  */
1218 static int determine_crypto_len(struct tx_helper *h,
1219                                 OSSL_QUIC_FRAME_CRYPTO *chdr,
1220                                 size_t space_left,
1221                                 uint64_t *hlen,
1222                                 uint64_t *len)
1223 {
1224     size_t orig_len;
1225     size_t base_hdr_len; /* CRYPTO header length without length field */
1226
1227     if (chdr->len > SIZE_MAX)
1228         return 0;
1229
1230     orig_len = (size_t)chdr->len;
1231
1232     chdr->len = 0;
1233     base_hdr_len = ossl_quic_wire_get_encoded_frame_len_crypto_hdr(chdr);
1234     chdr->len = orig_len;
1235     if (base_hdr_len == 0)
1236         return 0;
1237
1238     --base_hdr_len;
1239
1240     return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
1241 }
1242
1243 static int determine_stream_len(struct tx_helper *h,
1244                                 OSSL_QUIC_FRAME_STREAM *shdr,
1245                                 size_t space_left,
1246                                 uint64_t *hlen,
1247                                 uint64_t *len)
1248 {
1249     size_t orig_len;
1250     size_t base_hdr_len; /* STREAM header length without length field */
1251
1252     if (shdr->len > SIZE_MAX)
1253         return 0;
1254
1255     orig_len = (size_t)shdr->len;
1256
1257     shdr->len = 0;
1258     base_hdr_len = ossl_quic_wire_get_encoded_frame_len_stream_hdr(shdr);
1259     shdr->len = orig_len;
1260     if (base_hdr_len == 0)
1261         return 0;
1262
1263     if (shdr->has_explicit_len)
1264         --base_hdr_len;
1265
1266     return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
1267 }
1268
1269 static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
1270                                       struct tx_helper *h,
1271                                       uint32_t pn_space,
1272                                       QUIC_TXPIM_PKT *tpkt,
1273                                       int *have_ack_eliciting)
1274 {
1275     size_t num_stream_iovec;
1276     OSSL_QUIC_FRAME_STREAM shdr = {0};
1277     OSSL_QUIC_FRAME_CRYPTO chdr = {0};
1278     OSSL_QTX_IOVEC iov[2];
1279     uint64_t hdr_bytes;
1280     WPACKET *wpkt;
1281     QUIC_TXPIM_CHUNK chunk = {0};
1282     size_t i, space_left;
1283
1284     for (i = 0;; ++i) {
1285         space_left = tx_helper_get_space_left(h);
1286
1287         if (space_left < MIN_FRAME_SIZE_CRYPTO)
1288             return 1; /* no point trying */
1289
1290         /* Do we have any CRYPTO data waiting? */
1291         num_stream_iovec = OSSL_NELEM(iov);
1292         if (!ossl_quic_sstream_get_stream_frame(txp->args.crypto[pn_space],
1293                                                 i, &shdr, iov,
1294                                                 &num_stream_iovec))
1295             return 1; /* nothing to do */
1296
1297         /* Convert STREAM frame header to CRYPTO frame header */
1298         chdr.offset = shdr.offset;
1299         chdr.len    = shdr.len;
1300
1301         if (chdr.len == 0)
1302             return 1; /* nothing to do */
1303
1304         /* Find best fit (header length, payload length) combination. */
1305         if (!determine_crypto_len(h, &chdr, space_left, &hdr_bytes,
1306                                   &chdr.len))
1307             return 1; /* can't fit anything */
1308
1309         /*
1310          * Truncate IOVs to match our chosen length.
1311          *
1312          * The length cannot be more than SIZE_MAX because this length comes
1313          * from our send stream buffer.
1314          */
1315         ossl_quic_sstream_adjust_iov((size_t)chdr.len, iov, num_stream_iovec);
1316
1317         /*
1318          * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
1319          * the the stream data.)
1320          */
1321         if (!txp_ensure_iovec(txp, h->num_iovec + 3))
1322             return 0; /* alloc error */
1323
1324         /* Encode the header. */
1325         wpkt = tx_helper_begin(h);
1326         if (wpkt == NULL)
1327             return 0; /* alloc error */
1328
1329         if (!ossl_quic_wire_encode_frame_crypto_hdr(wpkt, &chdr)) {
1330             tx_helper_rollback(h);
1331             return 1; /* can't fit */
1332         }
1333
1334         if (!tx_helper_commit(h))
1335             return 0; /* alloc error */
1336
1337         /* Add payload iovecs to the helper (infallible). */
1338         for (i = 0; i < num_stream_iovec; ++i)
1339             tx_helper_append_iovec(h, iov[i].buf, iov[i].buf_len);
1340
1341         *have_ack_eliciting = 1;
1342         tx_helper_unrestrict(h); /* no longer need PING */
1343
1344         /* Log chunk to TXPIM. */
1345         chunk.stream_id = UINT64_MAX; /* crypto stream */
1346         chunk.start     = chdr.offset;
1347         chunk.end       = chdr.offset + chdr.len - 1;
1348         chunk.has_fin   = 0; /* Crypto stream never ends */
1349         if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
1350             return 0; /* alloc error */
1351     }
1352 }
1353
1354 struct chunk_info {
1355     OSSL_QUIC_FRAME_STREAM shdr;
1356     OSSL_QTX_IOVEC iov[2];
1357     size_t num_stream_iovec;
1358     int valid;
1359 };
1360
1361 static int txp_plan_stream_chunk(OSSL_QUIC_TX_PACKETISER *txp,
1362                                  struct tx_helper *h,
1363                                  QUIC_SSTREAM *sstream,
1364                                  QUIC_TXFC *stream_txfc,
1365                                  size_t skip,
1366                                  struct chunk_info *chunk)
1367 {
1368     uint64_t fc_credit, fc_swm, fc_limit;
1369
1370     chunk->num_stream_iovec = OSSL_NELEM(chunk->iov);
1371     chunk->valid = ossl_quic_sstream_get_stream_frame(sstream, skip,
1372                                                       &chunk->shdr,
1373                                                       chunk->iov,
1374                                                       &chunk->num_stream_iovec);
1375     if (!chunk->valid)
1376         return 1;
1377
1378     if (!ossl_assert(chunk->shdr.len > 0 || chunk->shdr.is_fin))
1379         /* Should only have 0-length chunk if FIN */
1380         return 0;
1381
1382     /* Clamp according to connection and stream-level TXFC. */
1383     fc_credit   = ossl_quic_txfc_get_credit(stream_txfc);
1384     fc_swm      = ossl_quic_txfc_get_swm(stream_txfc);
1385     fc_limit    = fc_swm + fc_credit;
1386
1387     if (chunk->shdr.len > 0 && chunk->shdr.offset + chunk->shdr.len > fc_limit) {
1388         chunk->shdr.len = (fc_limit <= chunk->shdr.offset)
1389             ? 0 : fc_limit - chunk->shdr.offset;
1390         chunk->shdr.is_fin = 0;
1391     }
1392
1393     if (chunk->shdr.len == 0 && !chunk->shdr.is_fin) {
1394         /*
1395          * Nothing to do due to TXFC. Since SSTREAM returns chunks in ascending
1396          * order of offset we don't need to check any later chunks, so stop
1397          * iterating here.
1398          */
1399         chunk->valid = 0;
1400         return 1;
1401     }
1402
1403     return 1;
1404 }
1405
1406 /*
1407  * Returns 0 on fatal error (e.g. allocation failure), 1 on success.
1408  * *packet_full is set to 1 if there is no longer enough room for another STREAM
1409  * frame, and *stream_drained is set to 1 if all stream buffers have now been
1410  * sent.
1411  */
1412 static int txp_generate_stream_frames(OSSL_QUIC_TX_PACKETISER *txp,
1413                                       struct tx_helper *h,
1414                                       uint32_t pn_space,
1415                                       QUIC_TXPIM_PKT *tpkt,
1416                                       uint64_t id,
1417                                       QUIC_SSTREAM *sstream,
1418                                       QUIC_TXFC *stream_txfc,
1419                                       QUIC_STREAM *next_stream,
1420                                       size_t min_ppl,
1421                                       int *have_ack_eliciting,
1422                                       int *packet_full,
1423                                       int *stream_drained,
1424                                       uint64_t *new_credit_consumed)
1425 {
1426     int rc = 0;
1427     struct chunk_info chunks[2] = {0};
1428
1429     OSSL_QUIC_FRAME_STREAM *shdr;
1430     WPACKET *wpkt;
1431     QUIC_TXPIM_CHUNK chunk;
1432     size_t i, j, space_left;
1433     int needs_padding_if_implicit, can_fill_payload, use_explicit_len;
1434     int could_have_following_chunk;
1435     uint64_t orig_len;
1436     uint64_t hdr_len_implicit, payload_len_implicit;
1437     uint64_t hdr_len_explicit, payload_len_explicit;
1438     uint64_t fc_swm, fc_new_hwm;
1439
1440     fc_swm      = ossl_quic_txfc_get_swm(stream_txfc);
1441     fc_new_hwm  = fc_swm;
1442
1443     /*
1444      * Load the first two chunks if any offered by the send stream. We retrieve
1445      * the next chunk in advance so we can determine if we need to send any more
1446      * chunks from the same stream after this one, which is needed when
1447      * determining when we can use an implicit length in a STREAM frame.
1448      */
1449     for (i = 0; i < 2; ++i) {
1450         if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i, &chunks[i]))
1451             goto err;
1452
1453         if (i == 0 && !chunks[i].valid) {
1454             /* No chunks, nothing to do. */
1455             *stream_drained = 1;
1456             rc = 1;
1457             goto err;
1458         }
1459     }
1460
1461     for (i = 0;; ++i) {
1462         space_left = tx_helper_get_space_left(h);
1463
1464         if (!chunks[i % 2].valid) {
1465             /* Out of chunks; we're done. */
1466             *stream_drained = 1;
1467             rc = 1;
1468             goto err;
1469         }
1470
1471         if (space_left < MIN_FRAME_SIZE_STREAM) {
1472             *packet_full = 1;
1473             rc = 1;
1474             goto err;
1475         }
1476
1477         if (!ossl_assert(!h->done_implicit))
1478             /*
1479              * Logic below should have ensured we didn't append an
1480              * implicit-length unless we filled the packet or didn't have
1481              * another stream to handle, so this should not be possible.
1482              */
1483             goto err;
1484
1485         shdr = &chunks[i % 2].shdr;
1486         orig_len = shdr->len;
1487         if (i > 0)
1488             /* Load next chunk for lookahead. */
1489             if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i + 1,
1490                                        &chunks[(i + 1) % 2]))
1491                 goto err;
1492
1493         /*
1494          * Find best fit (header length, payload length) combination for if we
1495          * use an implicit length.
1496          */
1497         shdr->has_explicit_len = 0;
1498         hdr_len_implicit = payload_len_implicit = 0;
1499         if (!determine_stream_len(h, shdr, space_left,
1500                                   &hdr_len_implicit, &payload_len_implicit)) {
1501             *packet_full = 1;
1502             rc = 1;
1503             goto err; /* can't fit anything */
1504         }
1505
1506         /*
1507          * If using the implicit-length representation would need padding, we
1508          * can't use it.
1509          */
1510         needs_padding_if_implicit = (h->bytes_appended + hdr_len_implicit
1511                                      + payload_len_implicit < min_ppl);
1512
1513         /*
1514          * If there is a next stream, we don't use the implicit length so we can
1515          * add more STREAM frames after this one, unless there is enough data
1516          * for this STREAM frame to fill the packet.
1517          */
1518         can_fill_payload = (hdr_len_implicit + payload_len_implicit
1519                             >= space_left);
1520
1521         /*
1522          * Is there is a stream after this one, or another chunk pending
1523          * transmission in this stream?
1524          */
1525         could_have_following_chunk
1526             = (next_stream != NULL || chunks[(i + 1) % 2].valid);
1527
1528         /* Choose between explicit or implicit length representations. */
1529         use_explicit_len = !((can_fill_payload || !could_have_following_chunk)
1530                              && !needs_padding_if_implicit);
1531
1532         if (use_explicit_len) {
1533             /*
1534              * Find best fit (header length, payload length) combination for if
1535              * we use an explicit length.
1536              */
1537             shdr->has_explicit_len = 1;
1538             hdr_len_explicit = payload_len_explicit = 0;
1539             if (!determine_stream_len(h, shdr, space_left,
1540                                       &hdr_len_explicit, &payload_len_explicit)) {
1541                 *packet_full = 1;
1542                 rc = 1;
1543                 goto err; /* can't fit anything */
1544             }
1545
1546             shdr->len = payload_len_explicit;
1547         } else {
1548             shdr->has_explicit_len = 0;
1549             shdr->len = payload_len_implicit;
1550         }
1551
1552         /* If this is a FIN, don't keep filling the packet with more FINs. */
1553         if (shdr->is_fin)
1554             chunks[(i + 1) % 2].valid = 0;
1555
1556         /* Truncate IOVs to match our chosen length. */
1557         ossl_quic_sstream_adjust_iov((size_t)shdr->len, chunks[i % 2].iov,
1558                                      chunks[i % 2].num_stream_iovec);
1559
1560         /*
1561          * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
1562          * the the stream data.)
1563          */
1564         if (!txp_ensure_iovec(txp, h->num_iovec + 3))
1565             goto err; /* alloc error */
1566
1567         /* Encode the header. */
1568         wpkt = tx_helper_begin(h);
1569         if (wpkt == NULL)
1570             goto err; /* alloc error */
1571
1572         shdr->stream_id = id;
1573         if (!ossl_assert(ossl_quic_wire_encode_frame_stream_hdr(wpkt, shdr))) {
1574             /* (Should not be possible.) */
1575             tx_helper_rollback(h);
1576             *packet_full = 1;
1577             rc = 1;
1578             goto err; /* can't fit */
1579         }
1580
1581         if (!tx_helper_commit(h))
1582             goto err; /* alloc error */
1583
1584         /* Add payload iovecs to the helper (infallible). */
1585         for (j = 0; j < chunks[i % 2].num_stream_iovec; ++j)
1586             tx_helper_append_iovec(h, chunks[i % 2].iov[j].buf,
1587                                    chunks[i % 2].iov[j].buf_len);
1588
1589         *have_ack_eliciting = 1;
1590         tx_helper_unrestrict(h); /* no longer need PING */
1591         if (!shdr->has_explicit_len)
1592             h->done_implicit = 1;
1593
1594         /* Log new TXFC credit which was consumed. */
1595         if (shdr->len > 0 && shdr->offset + shdr->len > fc_new_hwm)
1596             fc_new_hwm = shdr->offset + shdr->len;
1597
1598         /* Log chunk to TXPIM. */
1599         chunk.stream_id         = shdr->stream_id;
1600         chunk.start             = shdr->offset;
1601         chunk.end               = shdr->offset + shdr->len - 1;
1602         chunk.has_fin           = shdr->is_fin;
1603         chunk.has_stop_sending  = 0;
1604         chunk.has_reset_stream  = 0;
1605         if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
1606             goto err; /* alloc error */
1607
1608         if (shdr->len < orig_len) {
1609             /*
1610              * If we did not serialize all of this chunk we definitely do not
1611              * want to try the next chunk (and we must not mark the stream
1612              * as drained).
1613              */
1614             rc = 1;
1615             goto err;
1616         }
1617     }
1618
1619 err:
1620     *new_credit_consumed = fc_new_hwm - fc_swm;
1621     return rc;
1622 }
1623
1624 static void txp_enlink_tmp(QUIC_STREAM **tmp_head, QUIC_STREAM *stream)
1625 {
1626     stream->txp_next = *tmp_head;
1627     *tmp_head = stream;
1628 }
1629
1630 static int txp_generate_stream_related(OSSL_QUIC_TX_PACKETISER *txp,
1631                                        struct tx_helper *h,
1632                                        uint32_t pn_space,
1633                                        QUIC_TXPIM_PKT *tpkt,
1634                                        size_t min_ppl,
1635                                        int *have_ack_eliciting,
1636                                        QUIC_STREAM **tmp_head)
1637 {
1638     QUIC_STREAM_ITER it;
1639     void *rstream;
1640     WPACKET *wpkt;
1641     uint64_t cwm;
1642     QUIC_STREAM *stream, *snext;
1643
1644     for (ossl_quic_stream_iter_init(&it, txp->args.qsm, 1);
1645          it.stream != NULL;) {
1646
1647         stream = it.stream;
1648         ossl_quic_stream_iter_next(&it);
1649         snext = it.stream;
1650
1651         stream->txp_sent_fc                  = 0;
1652         stream->txp_sent_stop_sending        = 0;
1653         stream->txp_sent_reset_stream        = 0;
1654         stream->txp_drained                  = 0;
1655         stream->txp_blocked                  = 0;
1656         stream->txp_txfc_new_credit_consumed = 0;
1657
1658         rstream = stream->rstream;
1659
1660         /* Stream Abort Frames (STOP_SENDING, RESET_STREAM) */
1661         if (stream->want_stop_sending) {
1662             OSSL_QUIC_FRAME_STOP_SENDING f;
1663
1664             wpkt = tx_helper_begin(h);
1665             if (wpkt == NULL)
1666                 return 0; /* alloc error */
1667
1668             f.stream_id         = stream->id;
1669             f.app_error_code    = stream->stop_sending_aec;
1670             if (!ossl_quic_wire_encode_frame_stop_sending(wpkt, &f)) {
1671                 tx_helper_rollback(h); /* can't fit */
1672                 txp_enlink_tmp(tmp_head, stream);
1673                 break;
1674             }
1675
1676             if (!tx_helper_commit(h))
1677                 return 0; /* alloc error */
1678
1679             *have_ack_eliciting = 1;
1680             tx_helper_unrestrict(h); /* no longer need PING */
1681             stream->txp_sent_stop_sending = 1;
1682         }
1683
1684         if (stream->want_reset_stream) {
1685             OSSL_QUIC_FRAME_RESET_STREAM f;
1686
1687             wpkt = tx_helper_begin(h);
1688             if (wpkt == NULL)
1689                 return 0; /* alloc error */
1690
1691             f.stream_id         = stream->id;
1692             f.app_error_code    = stream->reset_stream_aec;
1693             f.final_size        = ossl_quic_sstream_get_cur_size(stream->sstream);
1694             if (!ossl_quic_wire_encode_frame_reset_stream(wpkt, &f)) {
1695                 tx_helper_rollback(h); /* can't fit */
1696                 txp_enlink_tmp(tmp_head, stream);
1697                 break;
1698             }
1699
1700             if (!tx_helper_commit(h))
1701                 return 0; /* alloc error */
1702
1703             *have_ack_eliciting = 1;
1704             tx_helper_unrestrict(h); /* no longer need PING */
1705             stream->txp_sent_reset_stream = 1;
1706         }
1707
1708         /* Stream Flow Control Frames (MAX_STREAM_DATA) */
1709         if (rstream != NULL
1710             && (stream->want_max_stream_data
1711                 || ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 0))) {
1712
1713             wpkt = tx_helper_begin(h);
1714             if (wpkt == NULL)
1715                 return 0; /* alloc error */
1716
1717             cwm = ossl_quic_rxfc_get_cwm(&stream->rxfc);
1718
1719             if (!ossl_quic_wire_encode_frame_max_stream_data(wpkt, stream->id,
1720                                                              cwm)) {
1721                 tx_helper_rollback(h); /* can't fit */
1722                 txp_enlink_tmp(tmp_head, stream);
1723                 break;
1724             }
1725
1726             if (!tx_helper_commit(h))
1727                 return 0; /* alloc error */
1728
1729             *have_ack_eliciting = 1;
1730             tx_helper_unrestrict(h); /* no longer need PING */
1731             stream->txp_sent_fc = 1;
1732         }
1733
1734         /* Stream Data Frames (STREAM) */
1735         if (stream->sstream != NULL) {
1736             int packet_full = 0, stream_drained = 0;
1737
1738             if (!txp_generate_stream_frames(txp, h, pn_space, tpkt,
1739                                             stream->id, stream->sstream,
1740                                             &stream->txfc,
1741                                             snext, min_ppl,
1742                                             have_ack_eliciting,
1743                                             &packet_full,
1744                                             &stream_drained,
1745                                             &stream->txp_txfc_new_credit_consumed)) {
1746                 /* Fatal error (allocation, etc.) */
1747                 txp_enlink_tmp(tmp_head, stream);
1748                 return 0;
1749             }
1750
1751             if (stream_drained)
1752                 stream->txp_drained = 1;
1753
1754             if (packet_full) {
1755                 txp_enlink_tmp(tmp_head, stream);
1756                 break;
1757             }
1758         }
1759
1760         txp_enlink_tmp(tmp_head, stream);
1761     }
1762
1763     return 1;
1764 }
1765
1766 /*
1767  * Generates a packet for a given EL with the given minimum and maximum
1768  * plaintext packet payload lengths. Returns TXP_ERR_* value.
1769  */
1770 static int txp_generate_for_el_actual(OSSL_QUIC_TX_PACKETISER *txp,
1771                                       uint32_t enc_level,
1772                                       uint32_t archetype,
1773                                       size_t min_ppl,
1774                                       size_t max_ppl,
1775                                       size_t pkt_overhead,
1776                                       QUIC_PKT_HDR *phdr,
1777                                       int chosen_for_conn_close)
1778 {
1779     int rc = TXP_ERR_SUCCESS;
1780     struct archetype_data a;
1781     uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
1782     struct tx_helper h;
1783     int have_helper = 0, have_ack_eliciting = 0, done_pre_token = 0;
1784     int require_ack_eliciting;
1785     QUIC_CFQ_ITEM *cfq_item;
1786     QUIC_TXPIM_PKT *tpkt = NULL;
1787     OSSL_QTX_PKT pkt;
1788     QUIC_STREAM *tmp_head = NULL, *stream;
1789
1790     if (!txp_get_archetype_data(enc_level, archetype, &a))
1791         goto fatal_err;
1792
1793     require_ack_eliciting
1794         = (a.allow_force_ack_eliciting
1795            && (txp->force_ack_eliciting & (1UL << pn_space)));
1796
1797     /* Minimum cannot be bigger than maximum. */
1798     if (min_ppl > max_ppl)
1799         goto fatal_err;
1800
1801     /* Maximum PN reached? */
1802     if (txp->next_pn[pn_space] >= (((QUIC_PN)1) << 62))
1803         goto fatal_err;
1804
1805     if ((tpkt = ossl_quic_txpim_pkt_alloc(txp->args.txpim)) == NULL)
1806         goto fatal_err;
1807
1808     /*
1809      * Initialise TX helper. If we must be ACK eliciting, reserve 1 byte for
1810      * PING.
1811      */
1812     if (!tx_helper_init(&h, txp, max_ppl, require_ack_eliciting ? 1 : 0))
1813         goto fatal_err;
1814
1815     have_helper = 1;
1816
1817     /*
1818      * Frame Serialization
1819      * ===================
1820      *
1821      * We now serialize frames into the packet in descending order of priority.
1822      */
1823
1824     /* HANDSHAKE_DONE (Regenerate) */
1825     if (a.allow_handshake_done && txp->want_handshake_done
1826         && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_HANDSHAKE_DONE) {
1827         WPACKET *wpkt = tx_helper_begin(&h);
1828
1829         if (wpkt == NULL)
1830             goto fatal_err;
1831
1832         if (ossl_quic_wire_encode_frame_handshake_done(wpkt)) {
1833             tpkt->had_handshake_done_frame = 1;
1834             have_ack_eliciting             = 1;
1835
1836             if (!tx_helper_commit(&h))
1837                 goto fatal_err;
1838
1839             tx_helper_unrestrict(&h); /* no longer need PING */
1840         } else {
1841             tx_helper_rollback(&h);
1842         }
1843     }
1844
1845     /* MAX_DATA (Regenerate) */
1846     if (a.allow_conn_fc
1847         && (txp->want_max_data
1848             || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0))
1849         && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_DATA) {
1850         WPACKET *wpkt = tx_helper_begin(&h);
1851         uint64_t cwm = ossl_quic_rxfc_get_cwm(txp->args.conn_rxfc);
1852
1853         if (wpkt == NULL)
1854             goto fatal_err;
1855
1856         if (ossl_quic_wire_encode_frame_max_data(wpkt, cwm)) {
1857             tpkt->had_max_data_frame = 1;
1858             have_ack_eliciting       = 1;
1859
1860             if (!tx_helper_commit(&h))
1861                 goto fatal_err;
1862
1863             tx_helper_unrestrict(&h); /* no longer need PING */
1864         } else {
1865             tx_helper_rollback(&h);
1866         }
1867     }
1868
1869     /* MAX_STREAMS_BIDI (Regenerate) */
1870     /*
1871      * TODO(STREAMS): Once we support multiple streams, add stream count FC
1872      * and plug this in.
1873      */
1874     if (a.allow_conn_fc
1875         && txp->want_max_streams_bidi
1876         && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_STREAMS_BIDI) {
1877         WPACKET *wpkt = tx_helper_begin(&h);
1878         uint64_t max_streams = 1; /* TODO */
1879
1880         if (wpkt == NULL)
1881             goto fatal_err;
1882
1883         if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/0,
1884                                                     max_streams)) {
1885             tpkt->had_max_streams_bidi_frame = 1;
1886             have_ack_eliciting               = 1;
1887
1888             if (!tx_helper_commit(&h))
1889                 goto fatal_err;
1890
1891             tx_helper_unrestrict(&h); /* no longer need PING */
1892         } else {
1893             tx_helper_rollback(&h);
1894         }
1895     }
1896
1897     /* MAX_STREAMS_UNI (Regenerate) */
1898     if (a.allow_conn_fc
1899         && txp->want_max_streams_uni
1900         && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_STREAMS_UNI) {
1901         WPACKET *wpkt = tx_helper_begin(&h);
1902         uint64_t max_streams = 0; /* TODO */
1903
1904         if (wpkt == NULL)
1905             goto fatal_err;
1906
1907         if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/1,
1908                                                     max_streams)) {
1909             tpkt->had_max_streams_uni_frame = 1;
1910             have_ack_eliciting              = 1;
1911
1912             if (!tx_helper_commit(&h))
1913                 goto fatal_err;
1914
1915             tx_helper_unrestrict(&h); /* no longer need PING */
1916         } else {
1917             tx_helper_rollback(&h);
1918         }
1919     }
1920
1921     /* GCR Frames */
1922     for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
1923          cfq_item != NULL;
1924          cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
1925         uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
1926         const unsigned char *encoded = ossl_quic_cfq_item_get_encoded(cfq_item);
1927         size_t encoded_len = ossl_quic_cfq_item_get_encoded_len(cfq_item);
1928
1929         switch (frame_type) {
1930             case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
1931                 if (!a.allow_new_conn_id)
1932                     continue;
1933                 break;
1934             case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
1935                 if (!a.allow_retire_conn_id)
1936                     continue;
1937                 break;
1938             case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
1939                 if (!a.allow_new_token)
1940                     continue;
1941
1942                 /*
1943                  * NEW_TOKEN frames are handled via GCR, but some
1944                  * Regenerate-strategy frames should come before them (namely
1945                  * ACK, CONNECTION_CLOSE, PATH_CHALLENGE and PATH_RESPONSE). If
1946                  * we find a NEW_TOKEN frame, do these now. If there are no
1947                  * NEW_TOKEN frames in the GCR queue we will handle these below.
1948                  */
1949                 if (!done_pre_token)
1950                     if (txp_generate_pre_token(txp, &h, tpkt, pn_space, &a,
1951                                                chosen_for_conn_close))
1952                         done_pre_token = 1;
1953
1954                 break;
1955             default:
1956                 if (!a.allow_cfq_other)
1957                     continue;
1958                 break;
1959         }
1960
1961         /*
1962          * If the frame is too big, don't try to schedule any more GCR frames in
1963          * this packet rather than sending subsequent ones out of order.
1964          */
1965         if (encoded_len > tx_helper_get_space_left(&h))
1966             break;
1967
1968         if (!tx_helper_append_iovec(&h, encoded, encoded_len))
1969             goto fatal_err;
1970
1971         ossl_quic_txpim_pkt_add_cfq_item(tpkt, cfq_item);
1972
1973         if (ossl_quic_frame_type_is_ack_eliciting(frame_type)) {
1974             have_ack_eliciting = 1;
1975             tx_helper_unrestrict(&h); /* no longer need PING */
1976         }
1977     }
1978
1979     /*
1980      * If we didn't generate ACK, CONNECTION_CLOSE, PATH_CHALLENGE or
1981      * PATH_RESPONSE (as desired) before, do so now.
1982      */
1983     if (!done_pre_token)
1984         if (txp_generate_pre_token(txp, &h, tpkt, pn_space, &a,
1985                                    chosen_for_conn_close))
1986             done_pre_token = 1;
1987
1988     /* CRYPTO Frames */
1989     if (a.allow_crypto)
1990         if (!txp_generate_crypto_frames(txp, &h, pn_space, tpkt,
1991                                         &have_ack_eliciting))
1992             goto fatal_err;
1993
1994     /* Stream-specific frames */
1995     if (a.allow_stream_rel && txp->handshake_complete)
1996         if (!txp_generate_stream_related(txp, &h, pn_space, tpkt, min_ppl,
1997                                          &have_ack_eliciting,
1998                                          &tmp_head))
1999             goto fatal_err;
2000
2001     /* PING */
2002     tx_helper_unrestrict(&h);
2003
2004     if (require_ack_eliciting && !have_ack_eliciting && a.allow_ping) {
2005         WPACKET *wpkt;
2006
2007         wpkt = tx_helper_begin(&h);
2008         if (wpkt == NULL)
2009             goto fatal_err;
2010
2011         if (!ossl_quic_wire_encode_frame_ping(wpkt)
2012             || !tx_helper_commit(&h))
2013             /*
2014              * We treat a request to be ACK-eliciting as a requirement, so this
2015              * is an error.
2016              */
2017             goto fatal_err;
2018
2019         have_ack_eliciting = 1;
2020     }
2021
2022     /* PADDING */
2023     if (h.bytes_appended < min_ppl) {
2024         WPACKET *wpkt = tx_helper_begin(&h);
2025         if (wpkt == NULL)
2026             goto fatal_err;
2027
2028         if (!ossl_quic_wire_encode_padding(wpkt, min_ppl - h.bytes_appended)
2029             || !tx_helper_commit(&h))
2030             goto fatal_err;
2031     }
2032
2033     /*
2034      * Dispatch
2035      * ========
2036      */
2037     /* ACKM Data */
2038     tpkt->ackm_pkt.num_bytes        = h.bytes_appended + pkt_overhead;
2039     tpkt->ackm_pkt.pkt_num          = txp->next_pn[pn_space];
2040     /* largest_acked is set in txp_generate_pre_token */
2041     tpkt->ackm_pkt.pkt_space        = pn_space;
2042     tpkt->ackm_pkt.is_inflight      = 1;
2043     tpkt->ackm_pkt.is_ack_eliciting = have_ack_eliciting;
2044     tpkt->ackm_pkt.is_pto_probe     = 0;
2045     tpkt->ackm_pkt.is_mtu_probe     = 0;
2046     tpkt->ackm_pkt.time             = ossl_time_now();
2047
2048     /* Packet Information for QTX */
2049     pkt.hdr         = phdr;
2050     pkt.iovec       = txp->iovec;
2051     pkt.num_iovec   = h.num_iovec;
2052     pkt.local       = NULL;
2053     pkt.peer        = BIO_ADDR_family(&txp->args.peer) == AF_UNSPEC
2054         ? NULL : &txp->args.peer;
2055     pkt.pn          = txp->next_pn[pn_space];
2056     pkt.flags       = OSSL_QTX_PKT_FLAG_COALESCE; /* always try to coalesce */
2057
2058     /* Do TX key update if needed. */
2059     if (enc_level == QUIC_ENC_LEVEL_1RTT) {
2060         uint64_t cur_pkt_count, max_pkt_count;
2061
2062         cur_pkt_count = ossl_qtx_get_cur_epoch_pkt_count(txp->args.qtx, enc_level);
2063         max_pkt_count = ossl_qtx_get_max_epoch_pkt_count(txp->args.qtx, enc_level);
2064
2065         if (cur_pkt_count >= max_pkt_count / 2)
2066             if (!ossl_qtx_trigger_key_update(txp->args.qtx))
2067                 goto fatal_err;
2068     }
2069
2070     if (!ossl_assert(h.bytes_appended > 0))
2071         goto fatal_err;
2072
2073     /* Generate TXPIM chunks representing STOP_SENDING and RESET_STREAM frames. */
2074     for (stream = tmp_head; stream != NULL; stream = stream->txp_next)
2075         if (stream->txp_sent_stop_sending || stream->txp_sent_reset_stream) {
2076             /* Log STOP_SENDING chunk to TXPIM. */
2077             QUIC_TXPIM_CHUNK chunk;
2078
2079             chunk.stream_id         = stream->id;
2080             chunk.start             = UINT64_MAX;
2081             chunk.end               = 0;
2082             chunk.has_fin           = 0;
2083             chunk.has_stop_sending  = stream->txp_sent_stop_sending;
2084             chunk.has_reset_stream  = stream->txp_sent_reset_stream;
2085             if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
2086                 return 0; /* alloc error */
2087         }
2088
2089     /* Dispatch to FIFD. */
2090     if (!ossl_quic_fifd_pkt_commit(&txp->fifd, tpkt))
2091         goto fatal_err;
2092
2093     /* Send the packet. */
2094     if (!ossl_qtx_write_pkt(txp->args.qtx, &pkt))
2095         goto fatal_err;
2096
2097     ++txp->next_pn[pn_space];
2098
2099     /*
2100      * Record FC and stream abort frames as sent; deactivate streams which no
2101      * longer have anything to do.
2102      */
2103     for (stream = tmp_head; stream != NULL; stream = stream->txp_next) {
2104         if (stream->txp_sent_fc) {
2105             stream->want_max_stream_data = 0;
2106             ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 1);
2107         }
2108
2109         if (stream->txp_sent_stop_sending)
2110             stream->want_stop_sending = 0;
2111
2112         if (stream->txp_sent_reset_stream)
2113             stream->want_reset_stream = 0;
2114
2115         if (stream->txp_txfc_new_credit_consumed > 0) {
2116             if (!ossl_assert(ossl_quic_txfc_consume_credit(&stream->txfc,
2117                                                            stream->txp_txfc_new_credit_consumed)))
2118                 /*
2119                  * Should not be possible, but we should continue with our
2120                  * bookkeeping as we have already committed the packet to the
2121                  * FIFD. Just change the value we return.
2122                  */
2123                 rc = TXP_ERR_INTERNAL;
2124
2125             stream->txp_txfc_new_credit_consumed = 0;
2126         }
2127
2128         /*
2129          * If we no longer need to generate any flow control (MAX_STREAM_DATA),
2130          * STOP_SENDING or RESET_STREAM frames, nor any STREAM frames (because
2131          * the stream is drained of data or TXFC-blocked), we can mark the
2132          * stream as inactive.
2133          */
2134         ossl_quic_stream_map_update_state(txp->args.qsm, stream);
2135
2136         if (stream->txp_drained)
2137             assert(!ossl_quic_sstream_has_pending(stream->sstream));
2138     }
2139
2140     /* We have now sent the packet, so update state accordingly. */
2141     if (have_ack_eliciting)
2142         txp->force_ack_eliciting &= ~(1UL << pn_space);
2143
2144     if (tpkt->had_handshake_done_frame)
2145         txp->want_handshake_done = 0;
2146
2147     if (tpkt->had_max_data_frame) {
2148         txp->want_max_data = 0;
2149         ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 1);
2150     }
2151
2152     if (tpkt->had_max_streams_bidi_frame)
2153         txp->want_max_streams_bidi = 0;
2154
2155     if (tpkt->had_max_streams_uni_frame)
2156         txp->want_max_streams_uni = 0;
2157
2158     if (tpkt->had_ack_frame)
2159         txp->want_ack &= ~(1UL << pn_space);
2160
2161     /* Done. */
2162     tx_helper_cleanup(&h);
2163     return rc;
2164
2165 fatal_err:
2166     /*
2167      * Handler for fatal errors, i.e. errors causing us to abort the entire
2168      * packet rather than just one frame. Examples of such errors include
2169      * allocation errors.
2170      */
2171     if (have_helper)
2172         tx_helper_cleanup(&h);
2173     if (tpkt != NULL)
2174         ossl_quic_txpim_pkt_release(txp->args.txpim, tpkt);
2175     return TXP_ERR_INTERNAL;
2176 }
2177
2178 /* Ensure the iovec array is at least num elements long. */
2179 static int txp_ensure_iovec(OSSL_QUIC_TX_PACKETISER *txp, size_t num)
2180 {
2181     OSSL_QTX_IOVEC *iovec;
2182
2183     if (txp->alloc_iovec >= num)
2184         return 1;
2185
2186     num = txp->alloc_iovec != 0 ? txp->alloc_iovec * 2 : 8;
2187
2188     iovec = OPENSSL_realloc(txp->iovec, sizeof(OSSL_QTX_IOVEC) * num);
2189     if (iovec == NULL)
2190         return 0;
2191
2192     txp->iovec          = iovec;
2193     txp->alloc_iovec    = num;
2194     return 1;
2195 }
2196
2197 int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
2198                                                 const OSSL_QUIC_FRAME_CONN_CLOSE *f)
2199 {
2200     char *reason = NULL;
2201     size_t reason_len = f->reason_len;
2202     size_t max_reason_len = txp_get_mdpl(txp) / 2;
2203
2204     if (txp->want_conn_close)
2205         return 0;
2206
2207     /*
2208      * Arbitrarily limit the length of the reason length string to half of the
2209      * MDPL.
2210      */
2211     if (reason_len > max_reason_len)
2212         reason_len = max_reason_len;
2213
2214     if (reason_len > 0) {
2215         reason = OPENSSL_memdup(f->reason, reason_len);
2216         if (reason == NULL)
2217             return 0;
2218     }
2219
2220     txp->conn_close_frame               = *f;
2221     txp->conn_close_frame.reason        = reason;
2222     txp->conn_close_frame.reason_len    = reason_len;
2223     txp->want_conn_close                = 1;
2224     return 1;
2225 }