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