2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/quic_txp.h"
11 #include "internal/quic_fifd.h"
12 #include "internal/quic_stream_map.h"
13 #include "internal/quic_error.h"
14 #include "internal/common.h"
15 #include <openssl/err.h>
17 #define MIN_CRYPTO_HDR_SIZE 3
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
27 struct ossl_quic_tx_packetiser_st {
28 OSSL_QUIC_TX_PACKETISER_ARGS args;
31 * Opaque initial token blob provided by caller. TXP frees using the
32 * callback when it is no longer needed.
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;
39 /* Subcomponents of the TXP that we own. */
40 QUIC_FIFD fifd; /* QUIC Frame-in-Flight Dispatcher */
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. */
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;
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;
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.
61 unsigned int want_conn_close : 1;
63 /* Has the handshake been completed? */
64 unsigned int handshake_complete : 1;
66 OSSL_QUIC_FRAME_CONN_CLOSE conn_close_frame;
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 */
74 /* Message callback related arguments */
75 ossl_msg_cb msg_callback;
76 void *msg_callback_arg;
77 SSL *msg_callback_ssl;
80 void (*ack_tx_cb)(const OSSL_QUIC_FRAME_ACK *ack,
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
93 OSSL_QUIC_TX_PACKETISER *txp;
95 * The Maximum Packet Payload Length in bytes. This is the amount of
96 * space we have to generate frames into.
100 * Number of bytes we have generated so far.
102 size_t bytes_appended;
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.
109 size_t scratch_bytes;
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.
120 * Number of iovecs we have currently appended. This is the number of
121 * entries valid in txp->iovec.
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
130 unsigned int reserve_allowed : 1;
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.
136 unsigned int done_implicit : 1;
139 * The fields in this structure are valid if active is set, which means
140 * that a serialization transaction is currently in progress.
144 unsigned int active : 1;
148 static void tx_helper_rollback(struct tx_helper *h);
149 static int txp_ensure_iovec(OSSL_QUIC_TX_PACKETISER *txp, size_t num);
151 /* Initialises the TX helper. */
152 static int tx_helper_init(struct tx_helper *h, OSSL_QUIC_TX_PACKETISER *txp,
153 size_t max_ppl, size_t reserve)
155 if (reserve > max_ppl)
159 h->max_ppl = max_ppl;
160 h->reserve = reserve;
162 h->bytes_appended = 0;
163 h->scratch_bytes = 0;
164 h->reserve_allowed = 0;
165 h->done_implicit = 0;
169 if (max_ppl > h->txp->scratch_len) {
170 unsigned char *scratch;
172 scratch = OPENSSL_realloc(h->txp->scratch, max_ppl);
176 h->txp->scratch = scratch;
177 h->txp->scratch_len = max_ppl;
183 static void tx_helper_cleanup(struct tx_helper *h)
186 tx_helper_rollback(h);
191 static void tx_helper_unrestrict(struct tx_helper *h)
193 h->reserve_allowed = 1;
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.
200 * In general, the buffers passed to this function will be from one of two
203 * - Application data contained in stream buffers managed elsewhere
204 * in the QUIC stack; or
206 * - Control frame data appended into txp->scratch using tx_helper_begin and
210 static int tx_helper_append_iovec(struct tx_helper *h,
211 const unsigned char *buf,
217 if (!ossl_assert(!h->done_implicit))
220 if (!txp_ensure_iovec(h->txp, h->num_iovec + 1))
223 h->txp->iovec[h->num_iovec].buf = buf;
224 h->txp->iovec[h->num_iovec].buf_len = buf_len;
227 h->bytes_appended += buf_len;
232 * How many more bytes of space do we have left in our plaintext packet payload?
234 static size_t tx_helper_get_space_left(struct tx_helper *h)
237 - (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
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
247 static WPACKET *tx_helper_begin(struct tx_helper *h)
249 size_t space_left, len;
252 if (!ossl_assert(!h->txn.active))
255 if (!ossl_assert(!h->done_implicit))
258 data = (unsigned char *)h->txp->scratch + h->scratch_bytes;
259 len = h->txp->scratch_len - h->scratch_bytes;
261 space_left = tx_helper_get_space_left(h);
262 if (!ossl_assert(space_left <= len))
265 if (!WPACKET_init_static_len(&h->txn.wpkt, data, len, 0))
268 if (!WPACKET_set_max_size(&h->txn.wpkt, space_left)) {
269 WPACKET_cleanup(&h->txn.wpkt);
278 static void tx_helper_end(struct tx_helper *h, int success)
281 WPACKET_finish(&h->txn.wpkt);
283 WPACKET_cleanup(&h->txn.wpkt);
289 /* Abort a control frame serialization transaction. */
290 static void tx_helper_rollback(struct tx_helper *h)
298 /* Commit a control frame. */
299 static int tx_helper_commit(struct tx_helper *h)
306 if (!WPACKET_get_total_written(&h->txn.wpkt, &l)) {
311 if (!tx_helper_append_iovec(h, h->txn.data, l)) {
316 if (h->txp->msg_callback != NULL && l > 0) {
318 int ctype = SSL3_RT_QUIC_FRAME_FULL;
321 if (!PACKET_buf_init(&pkt, h->txn.data, l)
322 || !ossl_quic_wire_peek_frame_header(&pkt, &ftype, NULL)) {
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;
333 h->txp->msg_callback(1, OSSL_QUIC1_VERSION, ctype, h->txn.data, l,
334 h->txp->msg_callback_ssl,
335 h->txp->msg_callback_arg);
338 h->scratch_bytes += l;
343 static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
345 static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
346 QUIC_TXPIM_PKT *pkt, void *arg);
347 static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
348 QUIC_TXPIM_PKT *pkt, void *arg);
349 static void on_sstream_updated(uint64_t stream_id, void *arg);
350 static int sstream_is_pending(QUIC_SSTREAM *sstream);
351 static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
354 uint32_t *conn_close_enc_level);
355 static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
358 int is_last_in_dgram,
359 int dgram_contains_initial,
360 int chosen_for_conn_close,
361 QUIC_TXP_STATUS *status);
362 static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp);
363 static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
368 static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp);
369 static int txp_generate_for_el_actual(OSSL_QUIC_TX_PACKETISER *txp,
376 int chosen_for_conn_close,
377 QUIC_TXP_STATUS *status);
379 OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args)
381 OSSL_QUIC_TX_PACKETISER *txp;
385 || args->txpim == NULL
387 || args->ackm == NULL
389 || args->conn_txfc == NULL
390 || args->conn_rxfc == NULL
391 || args->max_streams_bidi_rxfc == NULL
392 || args->max_streams_uni_rxfc == NULL) {
393 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
397 txp = OPENSSL_zalloc(sizeof(*txp));
402 txp->last_tx_time = ossl_time_zero();
404 if (!ossl_quic_fifd_init(&txp->fifd,
405 txp->args.cfq, txp->args.ackm, txp->args.txpim,
406 get_sstream_by_id, txp,
407 on_regen_notify, txp,
408 on_confirm_notify, txp,
409 on_sstream_updated, txp)) {
417 void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp)
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);
430 void ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
431 const unsigned char *token,
433 ossl_quic_initial_token_free_fn *free_cb,
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);
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;
446 int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
447 const QUIC_CONN_ID *dcid)
450 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
454 txp->args.cur_dcid = *dcid;
458 int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
459 const QUIC_CONN_ID *scid)
462 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
466 txp->args.cur_scid = *scid;
470 /* Change the destination L4 address the TXP uses to send datagrams. */
471 int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
472 const BIO_ADDR *peer)
475 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
479 txp->args.peer = *peer;
483 void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
484 void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
490 txp->ack_tx_cb_arg = cb_arg;
493 int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
496 if (enc_level >= QUIC_ENC_LEVEL_NUM) {
497 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
501 if (enc_level != QUIC_ENC_LEVEL_0RTT)
502 txp->args.crypto[ossl_quic_enc_level_to_pn_space(enc_level)] = NULL;
507 void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp)
509 txp->handshake_complete = 1;
512 void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp)
514 txp->want_handshake_done = 1;
517 void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
520 txp->force_ack_eliciting |= (1UL << pn_space);
523 void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
526 txp->want_ack |= (1UL << pn_space);
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 */
534 int ossl_quic_tx_packetiser_has_pending(OSSL_QUIC_TX_PACKETISER *txp,
538 uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
539 int bypass_cc = ((flags & TX_PACKETISER_BYPASS_CC) != 0);
544 || txp->args.cc_method->get_tx_allowance(txp->args.cc_data) > 0);
546 for (enc_level = QUIC_ENC_LEVEL_INITIAL;
547 enc_level < QUIC_ENC_LEVEL_NUM;
549 if (txp_el_pending(txp, enc_level, archetype, cc_can_send,
550 &conn_close_enc_level))
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
561 int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
563 QUIC_TXP_STATUS *status)
565 uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
566 int have_pkt_for_el[QUIC_ENC_LEVEL_NUM], is_last_in_dgram, cc_can_send;
567 size_t num_el_in_dgram = 0, pkts_done = 0;
570 status->sent_ack_eliciting = 0;
573 * If CC says we cannot send we still may be able to send any queued probes.
575 cc_can_send = (txp->args.cc_method->get_tx_allowance(txp->args.cc_data) > 0);
577 for (enc_level = QUIC_ENC_LEVEL_INITIAL;
578 enc_level < QUIC_ENC_LEVEL_NUM;
580 have_pkt_for_el[enc_level] = txp_el_pending(txp, enc_level, archetype,
582 &conn_close_enc_level);
583 if (have_pkt_for_el[enc_level])
587 if (num_el_in_dgram == 0)
588 return TX_PACKETISER_RES_NO_PKT;
591 * Should not be needed, but a sanity check in case anyone else has been
594 ossl_qtx_finish_dgram(txp->args.qtx);
596 for (enc_level = QUIC_ENC_LEVEL_INITIAL;
597 enc_level < QUIC_ENC_LEVEL_NUM;
599 if (!have_pkt_for_el[enc_level])
602 is_last_in_dgram = (pkts_done + 1 == num_el_in_dgram);
603 rc = txp_generate_for_el(txp, enc_level, archetype, cc_can_send,
605 have_pkt_for_el[QUIC_ENC_LEVEL_INITIAL],
606 enc_level == conn_close_enc_level,
609 if (rc != TXP_ERR_SUCCESS) {
611 * If we already successfully did at least one, make sure we report
612 * this via the return code.
617 return TX_PACKETISER_RES_FAILURE;
623 ossl_qtx_finish_dgram(txp->args.qtx);
624 return TX_PACKETISER_RES_SENT_PKT;
627 struct 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;
644 static const struct archetype_data archetypes[QUIC_ENC_LEVEL_NUM][TX_PACKETISER_ARCHETYPE_NUM] = {
647 /* EL 0(INITIAL) - Archetype 0(NORMAL) */
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,
660 /*allow_cfq_other =*/ 0,
661 /*allow_new_token =*/ 0,
662 /*allow_force_ack_eliciting =*/ 1,
664 /* EL 0(INITIAL) - Archetype 1(ACK_AND_PING_ONLY) */
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,
682 /* EL 1(HANDSHAKE) */
684 /* EL 1(HANDSHAKE) - Archetype 0(NORMAL) */
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,
697 /*allow_cfq_other =*/ 0,
698 /*allow_new_token =*/ 0,
699 /*allow_force_ack_eliciting =*/ 1,
701 /* EL 1(HANDSHAKE) - Archetype 1(ACK_AND_PING_ONLY) */
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,
721 /* EL 2(0RTT) - Archetype 0(NORMAL) */
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,
738 /* EL 2(0RTT) - Archetype 1(ACK_AND_PING_ONLY) */
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,
758 /* EL 3(1RTT) - Archetype 0(NORMAL) */
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,
775 /* EL 3(1RTT) - Archetype 1(ACK_AND_PING_ONLY) */
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,
795 static int txp_get_archetype_data(uint32_t enc_level,
797 struct archetype_data *a)
799 if (enc_level >= QUIC_ENC_LEVEL_NUM
800 || archetype >= TX_PACKETISER_ARCHETYPE_NUM)
803 /* No need to avoid copying this as it should not exceed one int in size. */
804 *a = archetypes[enc_level][archetype];
809 * Returns 1 if the given EL wants to produce one or more frames.
810 * Always returns 0 if the given EL is discarded.
812 static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
815 uint32_t *conn_close_enc_level)
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;
821 if (!ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level))
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.
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.
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.
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.
851 * TODO(QUIC): Revisit this when server support is added.
853 if (*conn_close_enc_level > enc_level
854 && *conn_close_enc_level != QUIC_ENC_LEVEL_1RTT)
855 *conn_close_enc_level = enc_level;
857 if (!txp_get_archetype_data(enc_level, archetype, &a))
860 /* Do we need to send a PTO probe? */
861 if (a.allow_force_ack_eliciting) {
862 OSSL_ACKM_PROBE_INFO *probe_info
863 = ossl_ackm_get0_probe_request(txp->args.ackm);
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)
874 /* If CC says we cannot currently send, we can only send probes. */
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]))
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))
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)
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)))
896 /* Do we want to produce a MAX_STREAMS frame? */
898 && (txp->want_max_streams_bidi
899 || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc,
901 || txp->want_max_streams_uni
902 || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc,
906 /* Do we want to produce a HANDSHAKE_DONE frame? */
907 if (a.allow_handshake_done && txp->want_handshake_done)
910 /* Do we want to produce a CONNECTION_CLOSE frame? */
911 if (a.allow_conn_close && txp->want_conn_close &&
912 *conn_close_enc_level == enc_level)
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.
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);
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);
928 switch (frame_type) {
929 case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
930 if (a.allow_new_conn_id)
933 case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
934 if (a.allow_retire_conn_id)
937 case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
938 if (a.allow_new_token)
942 if (a.allow_cfq_other)
948 if (a.allow_stream_rel && txp->handshake_complete) {
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
960 ossl_quic_stream_iter_init(&it, txp->args.qsm, 0);
961 if (it.stream != NULL)
968 static int sstream_is_pending(QUIC_SSTREAM *sstream)
970 OSSL_QUIC_FRAME_STREAM hdr;
971 OSSL_QTX_IOVEC iov[2];
972 size_t num_iov = OSSL_NELEM(iov);
974 return ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov);
978 * Generates a packet for a given EL, coalescing it into the current datagram.
980 * is_last_in_dgram and dgram_contains_initial are used to determine padding
983 * Returns TXP_ERR_* value.
985 static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
988 int is_last_in_dgram,
989 int dgram_contains_initial,
990 int chosen_for_conn_close,
991 QUIC_TXP_STATUS *status)
993 int must_pad = dgram_contains_initial && is_last_in_dgram;
994 size_t min_dpl, min_pl, min_ppl, cmpl, cmppl, running_total;
995 size_t mdpl, hdr_len, pkt_overhead, cc_limit;
999 /* Determine the limit CC imposes on what we can send. */
1002 * If we are called when we cannot send, this must be because we want to
1003 * generate a probe. In this circumstance, don't clamp based on CC, but
1004 * don't add application data and limit ourselves to generating a small
1005 * packet containing only PING and ACK frames.
1007 cc_limit = SIZE_MAX;
1008 archetype = TX_PACKETISER_ARCHETYPE_ACK_AND_PING_ONLY;
1010 /* Allow CC to clamp how much we can send. */
1011 cc_limit_ = txp->args.cc_method->get_tx_allowance(txp->args.cc_data);
1012 cc_limit = (cc_limit_ > SIZE_MAX ? SIZE_MAX : (size_t)cc_limit_);
1015 /* Assemble packet header. */
1016 phdr.type = ossl_quic_enc_level_to_pkt_type(enc_level);
1018 phdr.pn_len = txp_determine_pn_len(txp);
1022 phdr.version = QUIC_VERSION_1;
1023 phdr.dst_conn_id = txp->args.cur_dcid;
1024 phdr.src_conn_id = txp->args.cur_scid;
1027 * We need to know the length of the payload to get an accurate header
1028 * length for non-1RTT packets, because the Length field found in
1029 * Initial/Handshake/0-RTT packets uses a variable-length encoding. However,
1030 * we don't have a good idea of the length of our payload, because the
1031 * length of the payload depends on the room in the datagram after fitting
1032 * the header, which depends on the size of the header.
1034 * In general, it does not matter if a packet is slightly shorter (because
1035 * e.g. we predicted use of a 2-byte length field, but ended up only needing
1036 * a 1-byte length field). However this does matter for Initial packets
1037 * which must be at least 1200 bytes, which is also the assumed default MTU;
1038 * therefore in many cases Initial packets will be padded to 1200 bytes,
1039 * which means if we overestimated the header size, we will be short by a
1040 * few bytes and the server will ignore the packet for being too short. In
1041 * this case, however, such packets always *will* be padded to meet 1200
1042 * bytes, which requires a 2-byte length field, so we don't actually need to
1043 * worry about this. Thus we estimate the header length assuming a 2-byte
1044 * length field here, which should in practice work well in all cases.
1046 phdr.len = OSSL_QUIC_VLINT_2B_MAX - phdr.pn_len;
1048 if (enc_level == QUIC_ENC_LEVEL_INITIAL) {
1049 phdr.token = txp->initial_token;
1050 phdr.token_len = txp->initial_token_len;
1056 hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(phdr.dst_conn_id.id_len,
1059 return TXP_ERR_INPUT;
1061 /* MinDPL: Minimum total datagram payload length. */
1062 min_dpl = must_pad ? QUIC_MIN_INITIAL_DGRAM_LEN : 0;
1064 /* How much data is already in the current datagram? */
1065 running_total = ossl_qtx_get_cur_dgram_len_bytes(txp->args.qtx);
1067 /* MinPL: Minimum length of the fully encoded packet. */
1068 min_pl = running_total < min_dpl ? min_dpl - running_total : 0;
1069 if ((uint64_t)min_pl > cc_limit)
1071 * Congestion control does not allow us to send a packet of adequate
1074 return TXP_ERR_SPACE;
1076 /* MinPPL: Minimum plaintext payload length needed to meet MinPL. */
1077 if (!txp_determine_ppl_from_pl(txp, min_pl, enc_level, hdr_len, &min_ppl))
1078 /* MinPL is less than a valid packet size, so just use a MinPPL of 0. */
1081 /* MDPL: Maximum datagram payload length. */
1082 mdpl = txp_get_mdpl(txp);
1085 * CMPL: Maximum encoded packet size we can put into this datagram given any
1086 * previous packets coalesced into it.
1088 if (running_total > mdpl)
1089 /* Should not be possible, but if it happens: */
1092 cmpl = mdpl - running_total;
1094 /* Clamp CMPL based on congestion control limit. */
1095 if (cmpl > cc_limit)
1098 /* CMPPL: Maximum amount we can put into the current datagram payload. */
1099 if (!txp_determine_ppl_from_pl(txp, cmpl, enc_level, hdr_len, &cmppl))
1100 return TXP_ERR_SPACE;
1102 /* Packet overhead (size of headers, AEAD tag, etc.) */
1103 pkt_overhead = cmpl - cmppl;
1105 return txp_generate_for_el_actual(txp, enc_level, archetype, min_ppl, cmppl,
1106 pkt_overhead, &phdr,
1107 chosen_for_conn_close,
1111 /* Determine how many bytes we should use for the encoded PN. */
1112 static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp)
1114 return 4; /* TODO(QUIC) */
1117 /* Determine plaintext packet payload length from payload length. */
1118 static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
1129 if (!ossl_qtx_calculate_plaintext_payload_len(txp->args.qtx, enc_level,
1137 static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp)
1139 return ossl_qtx_get_mdpl(txp->args.qtx);
1142 static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
1145 OSSL_QUIC_TX_PACKETISER *txp = arg;
1148 if (stream_id == UINT64_MAX)
1149 return txp->args.crypto[pn_space];
1151 s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1158 static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
1159 QUIC_TXPIM_PKT *pkt, void *arg)
1161 OSSL_QUIC_TX_PACKETISER *txp = arg;
1163 switch (frame_type) {
1164 case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
1165 txp->want_handshake_done = 1;
1167 case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
1168 txp->want_max_data = 1;
1170 case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
1171 txp->want_max_streams_bidi = 1;
1173 case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
1174 txp->want_max_streams_uni = 1;
1176 case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
1177 txp->want_ack |= (1UL << pkt->ackm_pkt.pkt_space);
1179 case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
1182 = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1187 s->want_max_stream_data = 1;
1188 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1191 case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1194 = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1199 ossl_quic_stream_map_schedule_stop_sending(txp->args.qsm, s);
1202 case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1205 = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1210 s->want_reset_stream = 1;
1211 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1220 static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
1221 QUIC_TXPIM_PKT *pkt, void *arg)
1223 OSSL_QUIC_TX_PACKETISER *txp = arg;
1225 switch (frame_type) {
1226 case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
1229 = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1234 s->acked_stop_sending = 1;
1235 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1238 case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
1241 = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1247 * We must already be in RESET_SENT or RESET_RECVD if we are
1248 * here, so we don't need to check state here.
1250 ossl_quic_stream_map_notify_reset_stream_acked(txp->args.qsm, s);
1251 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1260 static void on_sstream_updated(uint64_t stream_id, void *arg)
1262 OSSL_QUIC_TX_PACKETISER *txp = arg;
1265 s = ossl_quic_stream_map_get_by_id(txp->args.qsm, stream_id);
1269 ossl_quic_stream_map_update_state(txp->args.qsm, s);
1272 static int txp_generate_pre_token(OSSL_QUIC_TX_PACKETISER *txp,
1273 struct tx_helper *h,
1274 QUIC_TXPIM_PKT *tpkt,
1276 struct archetype_data *a,
1277 int chosen_for_conn_close)
1279 const OSSL_QUIC_FRAME_ACK *ack;
1280 OSSL_QUIC_FRAME_ACK ack2;
1282 tpkt->ackm_pkt.largest_acked = QUIC_PN_INVALID;
1284 /* ACK Frames (Regenerate) */
1286 && tx_helper_get_space_left(h) >= MIN_FRAME_SIZE_ACK
1288 || ossl_ackm_is_ack_desired(txp->args.ackm, pn_space))
1289 && (ack = ossl_ackm_get_ack_frame(txp->args.ackm, pn_space)) != NULL) {
1290 WPACKET *wpkt = tx_helper_begin(h);
1295 /* We do not currently support ECN */
1297 ack2.ecn_present = 0;
1299 if (ossl_quic_wire_encode_frame_ack(wpkt,
1300 txp->args.ack_delay_exponent,
1302 if (!tx_helper_commit(h))
1305 tpkt->had_ack_frame = 1;
1307 if (ack->num_ack_ranges > 0)
1308 tpkt->ackm_pkt.largest_acked = ack->ack_ranges[0].end;
1310 if (txp->ack_tx_cb != NULL)
1311 txp->ack_tx_cb(&ack2, pn_space, txp->ack_tx_cb_arg);
1313 tx_helper_rollback(h);
1317 /* CONNECTION_CLOSE Frames (Regenerate) */
1318 if (a->allow_conn_close && txp->want_conn_close && chosen_for_conn_close) {
1319 WPACKET *wpkt = tx_helper_begin(h);
1320 OSSL_QUIC_FRAME_CONN_CLOSE f, *pf = &txp->conn_close_frame;
1326 * Application CONNECTION_CLOSE frames may only be sent in the
1327 * Application PN space, as otherwise they may be sent before a
1328 * connection is authenticated and leak application data. Therefore, if
1329 * we need to send a CONNECTION_CLOSE frame in another PN space and were
1330 * given an application CONNECTION_CLOSE frame, convert it into a
1331 * transport CONNECTION_CLOSE frame, removing any sensitive application
1334 * RFC 9000 s. 10.2.3: "A CONNECTION_CLOSE of type 0x1d MUST be replaced
1335 * by a CONNECTION_CLOSE of type 0x1c when sending the frame in Initial
1336 * or Handshake packets. Otherwise, information about the application
1337 * state might be revealed. Endpoints MUST clear the value of the Reason
1338 * Phrase field and SHOULD use the APPLICATION_ERROR code when
1339 * converting to a CONNECTION_CLOSE of type 0x1c."
1341 if (pn_space != QUIC_PN_SPACE_APP && pf->is_app) {
1345 pf->error_code = QUIC_ERR_APPLICATION_ERROR;
1350 if (ossl_quic_wire_encode_frame_conn_close(wpkt, pf)) {
1351 if (!tx_helper_commit(h))
1354 tx_helper_rollback(h);
1361 static int try_len(size_t space_left, size_t orig_len,
1362 size_t base_hdr_len, size_t lenbytes,
1363 uint64_t maxn, size_t *hdr_len, size_t *payload_len)
1366 size_t maxn_ = maxn > SIZE_MAX ? SIZE_MAX : (size_t)maxn;
1368 *hdr_len = base_hdr_len + lenbytes;
1370 if (orig_len == 0 && space_left >= *hdr_len) {
1378 if (n + *hdr_len > space_left)
1379 n = (space_left >= *hdr_len) ? space_left - *hdr_len : 0;
1385 static int determine_len(size_t space_left, size_t orig_len,
1386 size_t base_hdr_len,
1387 uint64_t *hlen, uint64_t *len)
1390 size_t chosen_payload_len = 0;
1391 size_t chosen_hdr_len = 0;
1392 size_t payload_len[4], hdr_len[4];
1393 int i, valid[4] = {0};
1395 valid[0] = try_len(space_left, orig_len, base_hdr_len,
1396 1, OSSL_QUIC_VLINT_1B_MAX,
1397 &hdr_len[0], &payload_len[0]);
1398 valid[1] = try_len(space_left, orig_len, base_hdr_len,
1399 2, OSSL_QUIC_VLINT_2B_MAX,
1400 &hdr_len[1], &payload_len[1]);
1401 valid[2] = try_len(space_left, orig_len, base_hdr_len,
1402 4, OSSL_QUIC_VLINT_4B_MAX,
1403 &hdr_len[2], &payload_len[2]);
1404 valid[3] = try_len(space_left, orig_len, base_hdr_len,
1405 8, OSSL_QUIC_VLINT_8B_MAX,
1406 &hdr_len[3], &payload_len[3]);
1408 for (i = OSSL_NELEM(valid) - 1; i >= 0; --i)
1409 if (valid[i] && payload_len[i] >= chosen_payload_len) {
1410 chosen_payload_len = payload_len[i];
1411 chosen_hdr_len = hdr_len[i];
1415 *hlen = chosen_hdr_len;
1416 *len = chosen_payload_len;
1421 * Given a CRYPTO frame header with accurate chdr->len and a budget
1422 * (space_left), try to find the optimal value of chdr->len to fill as much of
1423 * the budget as possible. This is slightly hairy because larger values of
1424 * chdr->len cause larger encoded sizes of the length field of the frame, which
1425 * in turn mean less space available for payload data. We check all possible
1426 * encodings and choose the optimal encoding.
1428 static int determine_crypto_len(struct tx_helper *h,
1429 OSSL_QUIC_FRAME_CRYPTO *chdr,
1435 size_t base_hdr_len; /* CRYPTO header length without length field */
1437 if (chdr->len > SIZE_MAX)
1440 orig_len = (size_t)chdr->len;
1443 base_hdr_len = ossl_quic_wire_get_encoded_frame_len_crypto_hdr(chdr);
1444 chdr->len = orig_len;
1445 if (base_hdr_len == 0)
1450 return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
1453 static int determine_stream_len(struct tx_helper *h,
1454 OSSL_QUIC_FRAME_STREAM *shdr,
1460 size_t base_hdr_len; /* STREAM header length without length field */
1462 if (shdr->len > SIZE_MAX)
1465 orig_len = (size_t)shdr->len;
1468 base_hdr_len = ossl_quic_wire_get_encoded_frame_len_stream_hdr(shdr);
1469 shdr->len = orig_len;
1470 if (base_hdr_len == 0)
1473 if (shdr->has_explicit_len)
1476 return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
1479 static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
1480 struct tx_helper *h,
1482 QUIC_TXPIM_PKT *tpkt,
1483 int *have_ack_eliciting)
1485 size_t num_stream_iovec;
1486 OSSL_QUIC_FRAME_STREAM shdr = {0};
1487 OSSL_QUIC_FRAME_CRYPTO chdr = {0};
1488 OSSL_QTX_IOVEC iov[2];
1491 QUIC_TXPIM_CHUNK chunk = {0};
1492 size_t i, space_left;
1495 space_left = tx_helper_get_space_left(h);
1497 if (space_left < MIN_FRAME_SIZE_CRYPTO)
1498 return 1; /* no point trying */
1500 /* Do we have any CRYPTO data waiting? */
1501 num_stream_iovec = OSSL_NELEM(iov);
1502 if (!ossl_quic_sstream_get_stream_frame(txp->args.crypto[pn_space],
1505 return 1; /* nothing to do */
1507 /* Convert STREAM frame header to CRYPTO frame header */
1508 chdr.offset = shdr.offset;
1509 chdr.len = shdr.len;
1512 return 1; /* nothing to do */
1514 /* Find best fit (header length, payload length) combination. */
1515 if (!determine_crypto_len(h, &chdr, space_left, &hdr_bytes,
1517 return 1; /* can't fit anything */
1520 * Truncate IOVs to match our chosen length.
1522 * The length cannot be more than SIZE_MAX because this length comes
1523 * from our send stream buffer.
1525 ossl_quic_sstream_adjust_iov((size_t)chdr.len, iov, num_stream_iovec);
1528 * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
1529 * the the stream data.)
1531 if (!txp_ensure_iovec(txp, h->num_iovec + 3))
1532 return 0; /* alloc error */
1534 /* Encode the header. */
1535 wpkt = tx_helper_begin(h);
1537 return 0; /* alloc error */
1539 if (!ossl_quic_wire_encode_frame_crypto_hdr(wpkt, &chdr)) {
1540 tx_helper_rollback(h);
1541 return 1; /* can't fit */
1544 if (!tx_helper_commit(h))
1545 return 0; /* alloc error */
1547 /* Add payload iovecs to the helper (infallible). */
1548 for (i = 0; i < num_stream_iovec; ++i)
1549 tx_helper_append_iovec(h, iov[i].buf, iov[i].buf_len);
1551 *have_ack_eliciting = 1;
1552 tx_helper_unrestrict(h); /* no longer need PING */
1554 /* Log chunk to TXPIM. */
1555 chunk.stream_id = UINT64_MAX; /* crypto stream */
1556 chunk.start = chdr.offset;
1557 chunk.end = chdr.offset + chdr.len - 1;
1558 chunk.has_fin = 0; /* Crypto stream never ends */
1559 if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
1560 return 0; /* alloc error */
1565 OSSL_QUIC_FRAME_STREAM shdr;
1566 OSSL_QTX_IOVEC iov[2];
1567 size_t num_stream_iovec;
1571 static int txp_plan_stream_chunk(OSSL_QUIC_TX_PACKETISER *txp,
1572 struct tx_helper *h,
1573 QUIC_SSTREAM *sstream,
1574 QUIC_TXFC *stream_txfc,
1576 struct chunk_info *chunk)
1578 uint64_t fc_credit, fc_swm, fc_limit;
1580 chunk->num_stream_iovec = OSSL_NELEM(chunk->iov);
1581 chunk->valid = ossl_quic_sstream_get_stream_frame(sstream, skip,
1584 &chunk->num_stream_iovec);
1588 if (!ossl_assert(chunk->shdr.len > 0 || chunk->shdr.is_fin))
1589 /* Should only have 0-length chunk if FIN */
1592 /* Clamp according to connection and stream-level TXFC. */
1593 fc_credit = ossl_quic_txfc_get_credit(stream_txfc);
1594 fc_swm = ossl_quic_txfc_get_swm(stream_txfc);
1595 fc_limit = fc_swm + fc_credit;
1597 if (chunk->shdr.len > 0 && chunk->shdr.offset + chunk->shdr.len > fc_limit) {
1598 chunk->shdr.len = (fc_limit <= chunk->shdr.offset)
1599 ? 0 : fc_limit - chunk->shdr.offset;
1600 chunk->shdr.is_fin = 0;
1603 if (chunk->shdr.len == 0 && !chunk->shdr.is_fin) {
1605 * Nothing to do due to TXFC. Since SSTREAM returns chunks in ascending
1606 * order of offset we don't need to check any later chunks, so stop
1617 * Returns 0 on fatal error (e.g. allocation failure), 1 on success.
1618 * *packet_full is set to 1 if there is no longer enough room for another STREAM
1619 * frame, and *stream_drained is set to 1 if all stream buffers have now been
1622 static int txp_generate_stream_frames(OSSL_QUIC_TX_PACKETISER *txp,
1623 struct tx_helper *h,
1625 QUIC_TXPIM_PKT *tpkt,
1627 QUIC_SSTREAM *sstream,
1628 QUIC_TXFC *stream_txfc,
1629 QUIC_STREAM *next_stream,
1631 int *have_ack_eliciting,
1633 int *stream_drained,
1634 uint64_t *new_credit_consumed)
1637 struct chunk_info chunks[2] = {0};
1639 OSSL_QUIC_FRAME_STREAM *shdr;
1641 QUIC_TXPIM_CHUNK chunk;
1642 size_t i, j, space_left;
1643 int needs_padding_if_implicit, can_fill_payload, use_explicit_len;
1644 int could_have_following_chunk;
1646 uint64_t hdr_len_implicit, payload_len_implicit;
1647 uint64_t hdr_len_explicit, payload_len_explicit;
1648 uint64_t fc_swm, fc_new_hwm;
1650 fc_swm = ossl_quic_txfc_get_swm(stream_txfc);
1651 fc_new_hwm = fc_swm;
1654 * Load the first two chunks if any offered by the send stream. We retrieve
1655 * the next chunk in advance so we can determine if we need to send any more
1656 * chunks from the same stream after this one, which is needed when
1657 * determining when we can use an implicit length in a STREAM frame.
1659 for (i = 0; i < 2; ++i) {
1660 if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i, &chunks[i]))
1663 if (i == 0 && !chunks[i].valid) {
1664 /* No chunks, nothing to do. */
1665 *stream_drained = 1;
1672 space_left = tx_helper_get_space_left(h);
1674 if (!chunks[i % 2].valid) {
1675 /* Out of chunks; we're done. */
1676 *stream_drained = 1;
1681 if (space_left < MIN_FRAME_SIZE_STREAM) {
1687 if (!ossl_assert(!h->done_implicit))
1689 * Logic below should have ensured we didn't append an
1690 * implicit-length unless we filled the packet or didn't have
1691 * another stream to handle, so this should not be possible.
1695 shdr = &chunks[i % 2].shdr;
1696 orig_len = shdr->len;
1698 /* Load next chunk for lookahead. */
1699 if (!txp_plan_stream_chunk(txp, h, sstream, stream_txfc, i + 1,
1700 &chunks[(i + 1) % 2]))
1704 * Find best fit (header length, payload length) combination for if we
1705 * use an implicit length.
1707 shdr->has_explicit_len = 0;
1708 hdr_len_implicit = payload_len_implicit = 0;
1709 if (!determine_stream_len(h, shdr, space_left,
1710 &hdr_len_implicit, &payload_len_implicit)) {
1713 goto err; /* can't fit anything */
1717 * If using the implicit-length representation would need padding, we
1720 needs_padding_if_implicit = (h->bytes_appended + hdr_len_implicit
1721 + payload_len_implicit < min_ppl);
1724 * If there is a next stream, we don't use the implicit length so we can
1725 * add more STREAM frames after this one, unless there is enough data
1726 * for this STREAM frame to fill the packet.
1728 can_fill_payload = (hdr_len_implicit + payload_len_implicit
1732 * Is there is a stream after this one, or another chunk pending
1733 * transmission in this stream?
1735 could_have_following_chunk
1736 = (next_stream != NULL || chunks[(i + 1) % 2].valid);
1738 /* Choose between explicit or implicit length representations. */
1739 use_explicit_len = !((can_fill_payload || !could_have_following_chunk)
1740 && !needs_padding_if_implicit);
1742 if (use_explicit_len) {
1744 * Find best fit (header length, payload length) combination for if
1745 * we use an explicit length.
1747 shdr->has_explicit_len = 1;
1748 hdr_len_explicit = payload_len_explicit = 0;
1749 if (!determine_stream_len(h, shdr, space_left,
1750 &hdr_len_explicit, &payload_len_explicit)) {
1753 goto err; /* can't fit anything */
1756 shdr->len = payload_len_explicit;
1758 shdr->has_explicit_len = 0;
1759 shdr->len = payload_len_implicit;
1762 /* If this is a FIN, don't keep filling the packet with more FINs. */
1764 chunks[(i + 1) % 2].valid = 0;
1766 /* Truncate IOVs to match our chosen length. */
1767 ossl_quic_sstream_adjust_iov((size_t)shdr->len, chunks[i % 2].iov,
1768 chunks[i % 2].num_stream_iovec);
1771 * Ensure we have enough iovecs allocated (1 for the header, up to 2 for
1772 * the the stream data.)
1774 if (!txp_ensure_iovec(txp, h->num_iovec + 3))
1775 goto err; /* alloc error */
1777 /* Encode the header. */
1778 wpkt = tx_helper_begin(h);
1780 goto err; /* alloc error */
1782 shdr->stream_id = id;
1783 if (!ossl_assert(ossl_quic_wire_encode_frame_stream_hdr(wpkt, shdr))) {
1784 /* (Should not be possible.) */
1785 tx_helper_rollback(h);
1788 goto err; /* can't fit */
1791 if (!tx_helper_commit(h))
1792 goto err; /* alloc error */
1794 /* Add payload iovecs to the helper (infallible). */
1795 for (j = 0; j < chunks[i % 2].num_stream_iovec; ++j)
1796 tx_helper_append_iovec(h, chunks[i % 2].iov[j].buf,
1797 chunks[i % 2].iov[j].buf_len);
1799 *have_ack_eliciting = 1;
1800 tx_helper_unrestrict(h); /* no longer need PING */
1801 if (!shdr->has_explicit_len)
1802 h->done_implicit = 1;
1804 /* Log new TXFC credit which was consumed. */
1805 if (shdr->len > 0 && shdr->offset + shdr->len > fc_new_hwm)
1806 fc_new_hwm = shdr->offset + shdr->len;
1808 /* Log chunk to TXPIM. */
1809 chunk.stream_id = shdr->stream_id;
1810 chunk.start = shdr->offset;
1811 chunk.end = shdr->offset + shdr->len - 1;
1812 chunk.has_fin = shdr->is_fin;
1813 chunk.has_stop_sending = 0;
1814 chunk.has_reset_stream = 0;
1815 if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
1816 goto err; /* alloc error */
1818 if (shdr->len < orig_len) {
1820 * If we did not serialize all of this chunk we definitely do not
1821 * want to try the next chunk (and we must not mark the stream
1830 *new_credit_consumed = fc_new_hwm - fc_swm;
1834 static void txp_enlink_tmp(QUIC_STREAM **tmp_head, QUIC_STREAM *stream)
1836 stream->txp_next = *tmp_head;
1840 static int txp_generate_stream_related(OSSL_QUIC_TX_PACKETISER *txp,
1841 struct tx_helper *h,
1843 QUIC_TXPIM_PKT *tpkt,
1845 int *have_ack_eliciting,
1846 QUIC_STREAM **tmp_head)
1848 QUIC_STREAM_ITER it;
1851 QUIC_STREAM *stream, *snext;
1853 for (ossl_quic_stream_iter_init(&it, txp->args.qsm, 1);
1854 it.stream != NULL;) {
1857 ossl_quic_stream_iter_next(&it);
1860 stream->txp_sent_fc = 0;
1861 stream->txp_sent_stop_sending = 0;
1862 stream->txp_sent_reset_stream = 0;
1863 stream->txp_drained = 0;
1864 stream->txp_blocked = 0;
1865 stream->txp_txfc_new_credit_consumed = 0;
1867 /* Stream Abort Frames (STOP_SENDING, RESET_STREAM) */
1868 if (stream->want_stop_sending) {
1869 OSSL_QUIC_FRAME_STOP_SENDING f;
1871 wpkt = tx_helper_begin(h);
1873 return 0; /* alloc error */
1875 f.stream_id = stream->id;
1876 f.app_error_code = stream->stop_sending_aec;
1877 if (!ossl_quic_wire_encode_frame_stop_sending(wpkt, &f)) {
1878 tx_helper_rollback(h); /* can't fit */
1879 txp_enlink_tmp(tmp_head, stream);
1883 if (!tx_helper_commit(h))
1884 return 0; /* alloc error */
1886 *have_ack_eliciting = 1;
1887 tx_helper_unrestrict(h); /* no longer need PING */
1888 stream->txp_sent_stop_sending = 1;
1891 if (stream->want_reset_stream) {
1892 OSSL_QUIC_FRAME_RESET_STREAM f;
1894 if (!ossl_assert(stream->send_state == QUIC_SSTREAM_STATE_RESET_SENT))
1897 wpkt = tx_helper_begin(h);
1899 return 0; /* alloc error */
1901 f.stream_id = stream->id;
1902 f.app_error_code = stream->reset_stream_aec;
1903 if (!ossl_quic_stream_send_get_final_size(stream, &f.final_size))
1904 return 0; /* should not be possible */
1906 if (!ossl_quic_wire_encode_frame_reset_stream(wpkt, &f)) {
1907 tx_helper_rollback(h); /* can't fit */
1908 txp_enlink_tmp(tmp_head, stream);
1912 if (!tx_helper_commit(h))
1913 return 0; /* alloc error */
1915 *have_ack_eliciting = 1;
1916 tx_helper_unrestrict(h); /* no longer need PING */
1917 stream->txp_sent_reset_stream = 1;
1920 * The final size of the stream as indicated by RESET_STREAM is used
1921 * to ensure a consistent view of flow control state by both
1922 * parties; if we happen to send a RESET_STREAM that consumes more
1923 * flow control credit, make sure we account for that.
1925 if (!ossl_assert(f.final_size <= ossl_quic_txfc_get_swm(&stream->txfc)))
1928 stream->txp_txfc_new_credit_consumed
1929 = f.final_size - ossl_quic_txfc_get_swm(&stream->txfc);
1933 * Stream Flow Control Frames (MAX_STREAM_DATA)
1935 * RFC 9000 s. 13.3: "An endpoint SHOULD stop sending MAX_STREAM_DATA
1936 * frames when the receiving part of the stream enters a "Size Known" or
1937 * "Reset Recvd" state." -- In practice, RECV is the only state
1938 * in which it makes sense to generate more MAX_STREAM_DATA frames.
1940 if (stream->recv_state == QUIC_RSTREAM_STATE_RECV
1941 && (stream->want_max_stream_data
1942 || ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 0))) {
1944 wpkt = tx_helper_begin(h);
1946 return 0; /* alloc error */
1948 cwm = ossl_quic_rxfc_get_cwm(&stream->rxfc);
1950 if (!ossl_quic_wire_encode_frame_max_stream_data(wpkt, stream->id,
1952 tx_helper_rollback(h); /* can't fit */
1953 txp_enlink_tmp(tmp_head, stream);
1957 if (!tx_helper_commit(h))
1958 return 0; /* alloc error */
1960 *have_ack_eliciting = 1;
1961 tx_helper_unrestrict(h); /* no longer need PING */
1962 stream->txp_sent_fc = 1;
1966 * Stream Data Frames (STREAM)
1968 * RFC 9000 s. 3.3: A sender MUST NOT send a STREAM [...] frame for a
1969 * stream in the "Reset Sent" state [or any terminal state]. We don't
1970 * send any more STREAM frames if we are sending, have sent, or are
1971 * planning to send, RESET_STREAM. The other terminal state is Data
1972 * Recvd, but txp_generate_stream_frames() is guaranteed to generate
1973 * nothing in this case.
1975 if (ossl_quic_stream_has_send_buffer(stream)
1976 && !ossl_quic_stream_send_is_reset(stream)) {
1977 int packet_full = 0, stream_drained = 0;
1979 if (!ossl_assert(!stream->want_reset_stream))
1982 if (!txp_generate_stream_frames(txp, h, pn_space, tpkt,
1983 stream->id, stream->sstream,
1989 &stream->txp_txfc_new_credit_consumed)) {
1990 /* Fatal error (allocation, etc.) */
1991 txp_enlink_tmp(tmp_head, stream);
1996 stream->txp_drained = 1;
1999 txp_enlink_tmp(tmp_head, stream);
2004 txp_enlink_tmp(tmp_head, stream);
2011 * Generates a packet for a given EL with the given minimum and maximum
2012 * plaintext packet payload lengths. Returns TXP_ERR_* value.
2014 static int txp_generate_for_el_actual(OSSL_QUIC_TX_PACKETISER *txp,
2019 size_t pkt_overhead,
2021 int chosen_for_conn_close,
2022 QUIC_TXP_STATUS *status)
2024 int rc = TXP_ERR_SUCCESS;
2025 struct archetype_data a;
2026 uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
2028 int have_helper = 0, have_ack_eliciting = 0, done_pre_token = 0;
2029 int require_ack_eliciting = 0;
2030 QUIC_CFQ_ITEM *cfq_item;
2031 QUIC_TXPIM_PKT *tpkt = NULL;
2033 QUIC_STREAM *tmp_head = NULL, *stream;
2034 OSSL_ACKM_PROBE_INFO *probe_info
2035 = ossl_ackm_get0_probe_request(txp->args.ackm);
2037 if (!txp_get_archetype_data(enc_level, archetype, &a))
2040 if (a.allow_force_ack_eliciting) {
2042 * Make this packet ACK-eliciting if it has been explicitly requested,
2043 * or if ACKM has requested a probe for this PN space.
2045 if ((txp->force_ack_eliciting & (1UL << pn_space)) != 0
2046 || (enc_level == QUIC_ENC_LEVEL_INITIAL
2047 && probe_info->anti_deadlock_initial > 0)
2048 || (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
2049 && probe_info->anti_deadlock_handshake > 0)
2050 || probe_info->pto[pn_space] > 0)
2051 require_ack_eliciting = 1;
2054 /* Minimum cannot be bigger than maximum. */
2055 if (min_ppl > max_ppl)
2058 /* Maximum PN reached? */
2059 if (!ossl_quic_pn_valid(txp->next_pn[pn_space]))
2062 if ((tpkt = ossl_quic_txpim_pkt_alloc(txp->args.txpim)) == NULL)
2066 * Initialise TX helper. If we must be ACK eliciting, reserve 1 byte for
2069 if (!tx_helper_init(&h, txp, max_ppl, require_ack_eliciting ? 1 : 0))
2075 * Frame Serialization
2076 * ===================
2078 * We now serialize frames into the packet in descending order of priority.
2081 /* HANDSHAKE_DONE (Regenerate) */
2082 if (a.allow_handshake_done && txp->want_handshake_done
2083 && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_HANDSHAKE_DONE) {
2084 WPACKET *wpkt = tx_helper_begin(&h);
2089 if (ossl_quic_wire_encode_frame_handshake_done(wpkt)) {
2090 tpkt->had_handshake_done_frame = 1;
2091 have_ack_eliciting = 1;
2093 if (!tx_helper_commit(&h))
2096 tx_helper_unrestrict(&h); /* no longer need PING */
2098 tx_helper_rollback(&h);
2102 /* MAX_DATA (Regenerate) */
2104 && (txp->want_max_data
2105 || ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0))
2106 && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_DATA) {
2107 WPACKET *wpkt = tx_helper_begin(&h);
2108 uint64_t cwm = ossl_quic_rxfc_get_cwm(txp->args.conn_rxfc);
2113 if (ossl_quic_wire_encode_frame_max_data(wpkt, cwm)) {
2114 tpkt->had_max_data_frame = 1;
2115 have_ack_eliciting = 1;
2117 if (!tx_helper_commit(&h))
2120 tx_helper_unrestrict(&h); /* no longer need PING */
2122 tx_helper_rollback(&h);
2126 /* MAX_STREAMS_BIDI (Regenerate) */
2128 && (txp->want_max_streams_bidi
2129 || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 0))
2130 && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_STREAMS_BIDI) {
2131 WPACKET *wpkt = tx_helper_begin(&h);
2132 uint64_t max_streams
2133 = ossl_quic_rxfc_get_cwm(txp->args.max_streams_bidi_rxfc);
2138 if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/0,
2140 tpkt->had_max_streams_bidi_frame = 1;
2141 have_ack_eliciting = 1;
2143 if (!tx_helper_commit(&h))
2146 tx_helper_unrestrict(&h); /* no longer need PING */
2148 tx_helper_rollback(&h);
2152 /* MAX_STREAMS_UNI (Regenerate) */
2154 && (txp->want_max_streams_uni
2155 || ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 0))
2156 && tx_helper_get_space_left(&h) >= MIN_FRAME_SIZE_MAX_STREAMS_UNI) {
2157 WPACKET *wpkt = tx_helper_begin(&h);
2158 uint64_t max_streams
2159 = ossl_quic_rxfc_get_cwm(txp->args.max_streams_uni_rxfc);
2164 if (ossl_quic_wire_encode_frame_max_streams(wpkt, /*is_uni=*/1,
2166 tpkt->had_max_streams_uni_frame = 1;
2167 have_ack_eliciting = 1;
2169 if (!tx_helper_commit(&h))
2172 tx_helper_unrestrict(&h); /* no longer need PING */
2174 tx_helper_rollback(&h);
2179 for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
2181 cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
2182 uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
2183 const unsigned char *encoded = ossl_quic_cfq_item_get_encoded(cfq_item);
2184 size_t encoded_len = ossl_quic_cfq_item_get_encoded_len(cfq_item);
2186 switch (frame_type) {
2187 case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
2188 if (!a.allow_new_conn_id)
2191 case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
2192 if (!a.allow_retire_conn_id)
2195 case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
2196 if (!a.allow_new_token)
2200 * NEW_TOKEN frames are handled via GCR, but some
2201 * Regenerate-strategy frames should come before them (namely
2202 * ACK, CONNECTION_CLOSE, PATH_CHALLENGE and PATH_RESPONSE). If
2203 * we find a NEW_TOKEN frame, do these now. If there are no
2204 * NEW_TOKEN frames in the GCR queue we will handle these below.
2206 if (!done_pre_token)
2207 if (txp_generate_pre_token(txp, &h, tpkt, pn_space, &a,
2208 chosen_for_conn_close))
2213 if (!a.allow_cfq_other)
2219 * If the frame is too big, don't try to schedule any more GCR frames in
2220 * this packet rather than sending subsequent ones out of order.
2222 if (encoded_len > tx_helper_get_space_left(&h))
2225 if (!tx_helper_append_iovec(&h, encoded, encoded_len))
2228 ossl_quic_txpim_pkt_add_cfq_item(tpkt, cfq_item);
2230 if (ossl_quic_frame_type_is_ack_eliciting(frame_type)) {
2231 have_ack_eliciting = 1;
2232 tx_helper_unrestrict(&h); /* no longer need PING */
2237 * If we didn't generate ACK, CONNECTION_CLOSE, PATH_CHALLENGE or
2238 * PATH_RESPONSE (as desired) before, do so now.
2240 if (!done_pre_token)
2241 if (txp_generate_pre_token(txp, &h, tpkt, pn_space, &a,
2242 chosen_for_conn_close))
2247 if (!txp_generate_crypto_frames(txp, &h, pn_space, tpkt,
2248 &have_ack_eliciting))
2251 /* Stream-specific frames */
2252 if (a.allow_stream_rel && txp->handshake_complete)
2253 if (!txp_generate_stream_related(txp, &h, pn_space, tpkt, min_ppl,
2254 &have_ack_eliciting,
2259 tx_helper_unrestrict(&h);
2261 if (require_ack_eliciting && !have_ack_eliciting && a.allow_ping) {
2264 wpkt = tx_helper_begin(&h);
2268 if (!ossl_quic_wire_encode_frame_ping(wpkt)
2269 || !tx_helper_commit(&h))
2271 * We treat a request to be ACK-eliciting as a requirement, so this
2276 have_ack_eliciting = 1;
2280 if (h.bytes_appended < min_ppl) {
2281 WPACKET *wpkt = tx_helper_begin(&h);
2285 if (!ossl_quic_wire_encode_padding(wpkt, min_ppl - h.bytes_appended)
2286 || !tx_helper_commit(&h))
2295 tpkt->ackm_pkt.num_bytes = h.bytes_appended + pkt_overhead;
2296 tpkt->ackm_pkt.pkt_num = txp->next_pn[pn_space];
2297 /* largest_acked is set in txp_generate_pre_token */
2298 tpkt->ackm_pkt.pkt_space = pn_space;
2299 tpkt->ackm_pkt.is_inflight = 1;
2300 tpkt->ackm_pkt.is_ack_eliciting = have_ack_eliciting;
2301 tpkt->ackm_pkt.is_pto_probe = 0;
2302 tpkt->ackm_pkt.is_mtu_probe = 0;
2303 tpkt->ackm_pkt.time = txp->args.now(txp->args.now_arg);
2305 /* Packet Information for QTX */
2307 pkt.iovec = txp->iovec;
2308 pkt.num_iovec = h.num_iovec;
2310 pkt.peer = BIO_ADDR_family(&txp->args.peer) == AF_UNSPEC
2311 ? NULL : &txp->args.peer;
2312 pkt.pn = txp->next_pn[pn_space];
2313 pkt.flags = OSSL_QTX_PKT_FLAG_COALESCE; /* always try to coalesce */
2315 if (!ossl_assert(h.bytes_appended > 0))
2318 /* Generate TXPIM chunks representing STOP_SENDING and RESET_STREAM frames. */
2319 for (stream = tmp_head; stream != NULL; stream = stream->txp_next)
2320 if (stream->txp_sent_stop_sending || stream->txp_sent_reset_stream) {
2321 /* Log STOP_SENDING chunk to TXPIM. */
2322 QUIC_TXPIM_CHUNK chunk;
2324 chunk.stream_id = stream->id;
2325 chunk.start = UINT64_MAX;
2328 chunk.has_stop_sending = stream->txp_sent_stop_sending;
2329 chunk.has_reset_stream = stream->txp_sent_reset_stream;
2330 if (!ossl_quic_txpim_pkt_append_chunk(tpkt, &chunk))
2331 return 0; /* alloc error */
2334 /* Dispatch to FIFD. */
2335 if (!ossl_quic_fifd_pkt_commit(&txp->fifd, tpkt))
2338 /* Send the packet. */
2339 if (!ossl_qtx_write_pkt(txp->args.qtx, &pkt))
2342 ++txp->next_pn[pn_space];
2345 * Record FC and stream abort frames as sent; deactivate streams which no
2346 * longer have anything to do.
2348 for (stream = tmp_head; stream != NULL; stream = stream->txp_next) {
2349 if (stream->txp_sent_fc) {
2350 stream->want_max_stream_data = 0;
2351 ossl_quic_rxfc_has_cwm_changed(&stream->rxfc, 1);
2354 if (stream->txp_sent_stop_sending)
2355 stream->want_stop_sending = 0;
2357 if (stream->txp_sent_reset_stream)
2358 stream->want_reset_stream = 0;
2360 if (stream->txp_txfc_new_credit_consumed > 0) {
2361 if (!ossl_assert(ossl_quic_txfc_consume_credit(&stream->txfc,
2362 stream->txp_txfc_new_credit_consumed)))
2364 * Should not be possible, but we should continue with our
2365 * bookkeeping as we have already committed the packet to the
2366 * FIFD. Just change the value we return.
2368 rc = TXP_ERR_INTERNAL;
2370 stream->txp_txfc_new_credit_consumed = 0;
2374 * If we no longer need to generate any flow control (MAX_STREAM_DATA),
2375 * STOP_SENDING or RESET_STREAM frames, nor any STREAM frames (because
2376 * the stream is drained of data or TXFC-blocked), we can mark the
2377 * stream as inactive.
2379 ossl_quic_stream_map_update_state(txp->args.qsm, stream);
2381 if (stream->txp_drained) {
2382 assert(!ossl_quic_sstream_has_pending(stream->sstream));
2385 * Transition to DATA_SENT if stream has a final size and we have
2388 if (ossl_quic_sstream_get_final_size(stream->sstream, NULL))
2389 ossl_quic_stream_map_notify_all_data_sent(txp->args.qsm, stream);
2393 /* We have now sent the packet, so update state accordingly. */
2394 if (have_ack_eliciting)
2395 txp->force_ack_eliciting &= ~(1UL << pn_space);
2397 if (tpkt->had_handshake_done_frame)
2398 txp->want_handshake_done = 0;
2400 if (tpkt->had_max_data_frame) {
2401 txp->want_max_data = 0;
2402 ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 1);
2405 if (tpkt->had_max_streams_bidi_frame) {
2406 txp->want_max_streams_bidi = 0;
2407 ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc, 1);
2410 if (tpkt->had_max_streams_uni_frame) {
2411 txp->want_max_streams_uni = 0;
2412 ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc, 1);
2415 if (tpkt->had_ack_frame)
2416 txp->want_ack &= ~(1UL << pn_space);
2419 * Decrement probe request counts if we have sent a packet that meets
2420 * the requirement of a probe, namely being ACK-eliciting.
2422 if (have_ack_eliciting) {
2423 if (enc_level == QUIC_ENC_LEVEL_INITIAL
2424 && probe_info->anti_deadlock_initial > 0)
2425 --probe_info->anti_deadlock_initial;
2427 if (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
2428 && probe_info->anti_deadlock_handshake > 0)
2429 --probe_info->anti_deadlock_handshake;
2431 if (a.allow_force_ack_eliciting /* (i.e., not for 0-RTT) */
2432 && probe_info->pto[pn_space] > 0)
2433 --probe_info->pto[pn_space];
2436 status->sent_ack_eliciting = 1;
2439 tx_helper_cleanup(&h);
2444 * Handler for fatal errors, i.e. errors causing us to abort the entire
2445 * packet rather than just one frame. Examples of such errors include
2446 * allocation errors.
2449 tx_helper_cleanup(&h);
2451 ossl_quic_txpim_pkt_release(txp->args.txpim, tpkt);
2452 return TXP_ERR_INTERNAL;
2455 /* Ensure the iovec array is at least num elements long. */
2456 static int txp_ensure_iovec(OSSL_QUIC_TX_PACKETISER *txp, size_t num)
2458 OSSL_QTX_IOVEC *iovec;
2460 if (txp->alloc_iovec >= num)
2463 num = txp->alloc_iovec != 0 ? txp->alloc_iovec * 2 : 8;
2465 iovec = OPENSSL_realloc(txp->iovec, sizeof(OSSL_QTX_IOVEC) * num);
2470 txp->alloc_iovec = num;
2474 int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
2475 const OSSL_QUIC_FRAME_CONN_CLOSE *f)
2477 char *reason = NULL;
2478 size_t reason_len = f->reason_len;
2479 size_t max_reason_len = txp_get_mdpl(txp) / 2;
2481 if (txp->want_conn_close)
2485 * Arbitrarily limit the length of the reason length string to half of the
2488 if (reason_len > max_reason_len)
2489 reason_len = max_reason_len;
2491 if (reason_len > 0) {
2492 reason = OPENSSL_memdup(f->reason, reason_len);
2497 txp->conn_close_frame = *f;
2498 txp->conn_close_frame.reason = reason;
2499 txp->conn_close_frame.reason_len = reason_len;
2500 txp->want_conn_close = 1;
2504 void ossl_quic_tx_packetiser_set_msg_callback(OSSL_QUIC_TX_PACKETISER *txp,
2505 ossl_msg_cb msg_callback,
2506 SSL *msg_callback_ssl)
2508 txp->msg_callback = msg_callback;
2509 txp->msg_callback_ssl = msg_callback_ssl;
2512 void ossl_quic_tx_packetiser_set_msg_callback_arg(OSSL_QUIC_TX_PACKETISER *txp,
2513 void *msg_callback_arg)
2515 txp->msg_callback_arg = msg_callback_arg;
2518 QUIC_PN ossl_quic_tx_packetiser_get_next_pn(OSSL_QUIC_TX_PACKETISER *txp,
2521 if (pn_space >= QUIC_PN_SPACE_NUM)
2524 return txp->next_pn[pn_space];