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