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