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