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