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