/src/openssl/ssl/quic/quic_stream_map.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_stream_map.h" |
11 | | #include "internal/nelem.h" |
12 | | |
13 | | /* |
14 | | * QUIC Stream Map |
15 | | * =============== |
16 | | */ |
17 | | DEFINE_LHASH_OF_EX(QUIC_STREAM); |
18 | | |
19 | | static void shutdown_flush_done(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs); |
20 | | |
21 | | /* Circular list management. */ |
22 | | static void list_insert_tail(QUIC_STREAM_LIST_NODE *l, |
23 | | QUIC_STREAM_LIST_NODE *n) |
24 | 0 | { |
25 | | /* Must not be in list. */ |
26 | 0 | assert(n->prev == NULL && n->next == NULL |
27 | 0 | && l->prev != NULL && l->next != NULL); |
28 | | |
29 | 0 | n->prev = l->prev; |
30 | 0 | n->prev->next = n; |
31 | 0 | l->prev = n; |
32 | 0 | n->next = l; |
33 | 0 | } |
34 | | |
35 | | static void list_remove(QUIC_STREAM_LIST_NODE *l, |
36 | | QUIC_STREAM_LIST_NODE *n) |
37 | 0 | { |
38 | 0 | assert(n->prev != NULL && n->next != NULL |
39 | 0 | && n->prev != n && n->next != n); |
40 | | |
41 | 0 | n->prev->next = n->next; |
42 | 0 | n->next->prev = n->prev; |
43 | 0 | n->next = n->prev = NULL; |
44 | 0 | } |
45 | | |
46 | | static QUIC_STREAM *list_next(QUIC_STREAM_LIST_NODE *l, QUIC_STREAM_LIST_NODE *n, |
47 | | size_t off) |
48 | 0 | { |
49 | 0 | assert(n->prev != NULL && n->next != NULL |
50 | 0 | && (n == l || (n->prev != n && n->next != n)) |
51 | 0 | && l->prev != NULL && l->next != NULL); |
52 | | |
53 | 0 | n = n->next; |
54 | |
|
55 | 0 | if (n == l) |
56 | 0 | n = n->next; |
57 | 0 | if (n == l) |
58 | 0 | return NULL; |
59 | | |
60 | 0 | assert(n != NULL); |
61 | | |
62 | 0 | return (QUIC_STREAM *)(((char *)n) - off); |
63 | 0 | } |
64 | | |
65 | 0 | #define active_next(l, s) list_next((l), &(s)->active_node, \ |
66 | 0 | offsetof(QUIC_STREAM, active_node)) |
67 | | #define accept_next(l, s) list_next((l), &(s)->accept_node, \ |
68 | | offsetof(QUIC_STREAM, accept_node)) |
69 | 0 | #define ready_for_gc_next(l, s) list_next((l), &(s)->ready_for_gc_node, \ |
70 | 0 | offsetof(QUIC_STREAM, ready_for_gc_node)) |
71 | 0 | #define accept_head(l) list_next((l), (l), \ |
72 | 0 | offsetof(QUIC_STREAM, accept_node)) |
73 | 0 | #define ready_for_gc_head(l) list_next((l), (l), \ |
74 | 0 | offsetof(QUIC_STREAM, ready_for_gc_node)) |
75 | | |
76 | | static unsigned long hash_stream(const QUIC_STREAM *s) |
77 | 0 | { |
78 | 0 | return (unsigned long)s->id; |
79 | 0 | } |
80 | | |
81 | | static int cmp_stream(const QUIC_STREAM *a, const QUIC_STREAM *b) |
82 | 0 | { |
83 | 0 | if (a->id < b->id) |
84 | 0 | return -1; |
85 | 0 | if (a->id > b->id) |
86 | 0 | return 1; |
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | | int ossl_quic_stream_map_init(QUIC_STREAM_MAP *qsm, |
91 | | uint64_t (*get_stream_limit_cb)(int uni, void *arg), |
92 | | void *get_stream_limit_cb_arg, |
93 | | QUIC_RXFC *max_streams_bidi_rxfc, |
94 | | QUIC_RXFC *max_streams_uni_rxfc, |
95 | | int is_server) |
96 | 0 | { |
97 | 0 | qsm->map = lh_QUIC_STREAM_new(hash_stream, cmp_stream); |
98 | 0 | qsm->active_list.prev = qsm->active_list.next = &qsm->active_list; |
99 | 0 | qsm->accept_list.prev = qsm->accept_list.next = &qsm->accept_list; |
100 | 0 | qsm->ready_for_gc_list.prev = qsm->ready_for_gc_list.next |
101 | 0 | = &qsm->ready_for_gc_list; |
102 | 0 | qsm->rr_stepping = 1; |
103 | 0 | qsm->rr_counter = 0; |
104 | 0 | qsm->rr_cur = NULL; |
105 | |
|
106 | 0 | qsm->num_accept_bidi = 0; |
107 | 0 | qsm->num_accept_uni = 0; |
108 | 0 | qsm->num_shutdown_flush = 0; |
109 | |
|
110 | 0 | qsm->get_stream_limit_cb = get_stream_limit_cb; |
111 | 0 | qsm->get_stream_limit_cb_arg = get_stream_limit_cb_arg; |
112 | 0 | qsm->max_streams_bidi_rxfc = max_streams_bidi_rxfc; |
113 | 0 | qsm->max_streams_uni_rxfc = max_streams_uni_rxfc; |
114 | 0 | qsm->is_server = is_server; |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | static void release_each(QUIC_STREAM *stream, void *arg) |
119 | 0 | { |
120 | 0 | QUIC_STREAM_MAP *qsm = arg; |
121 | |
|
122 | 0 | ossl_quic_stream_map_release(qsm, stream); |
123 | 0 | } |
124 | | |
125 | | void ossl_quic_stream_map_cleanup(QUIC_STREAM_MAP *qsm) |
126 | 0 | { |
127 | 0 | ossl_quic_stream_map_visit(qsm, release_each, qsm); |
128 | |
|
129 | 0 | lh_QUIC_STREAM_free(qsm->map); |
130 | 0 | qsm->map = NULL; |
131 | 0 | } |
132 | | |
133 | | void ossl_quic_stream_map_visit(QUIC_STREAM_MAP *qsm, |
134 | | void (*visit_cb)(QUIC_STREAM *stream, void *arg), |
135 | | void *visit_cb_arg) |
136 | 0 | { |
137 | 0 | lh_QUIC_STREAM_doall_arg(qsm->map, visit_cb, visit_cb_arg); |
138 | 0 | } |
139 | | |
140 | | QUIC_STREAM *ossl_quic_stream_map_alloc(QUIC_STREAM_MAP *qsm, |
141 | | uint64_t stream_id, |
142 | | int type) |
143 | 0 | { |
144 | 0 | QUIC_STREAM *s; |
145 | 0 | QUIC_STREAM key; |
146 | |
|
147 | 0 | key.id = stream_id; |
148 | |
|
149 | 0 | s = lh_QUIC_STREAM_retrieve(qsm->map, &key); |
150 | 0 | if (s != NULL) |
151 | 0 | return NULL; |
152 | | |
153 | 0 | s = OPENSSL_zalloc(sizeof(*s)); |
154 | 0 | if (s == NULL) |
155 | 0 | return NULL; |
156 | | |
157 | 0 | s->id = stream_id; |
158 | 0 | s->type = type; |
159 | 0 | s->as_server = qsm->is_server; |
160 | 0 | s->send_state = (ossl_quic_stream_is_local_init(s) |
161 | 0 | || ossl_quic_stream_is_bidi(s)) |
162 | 0 | ? QUIC_SSTREAM_STATE_READY |
163 | 0 | : QUIC_SSTREAM_STATE_NONE; |
164 | 0 | s->recv_state = (!ossl_quic_stream_is_local_init(s) |
165 | 0 | || ossl_quic_stream_is_bidi(s)) |
166 | 0 | ? QUIC_RSTREAM_STATE_RECV |
167 | 0 | : QUIC_RSTREAM_STATE_NONE; |
168 | |
|
169 | 0 | s->send_final_size = UINT64_MAX; |
170 | |
|
171 | 0 | lh_QUIC_STREAM_insert(qsm->map, s); |
172 | 0 | return s; |
173 | 0 | } |
174 | | |
175 | | void ossl_quic_stream_map_release(QUIC_STREAM_MAP *qsm, QUIC_STREAM *stream) |
176 | 0 | { |
177 | 0 | if (stream == NULL) |
178 | 0 | return; |
179 | | |
180 | 0 | if (stream->active_node.next != NULL) |
181 | 0 | list_remove(&qsm->active_list, &stream->active_node); |
182 | 0 | if (stream->accept_node.next != NULL) |
183 | 0 | list_remove(&qsm->accept_list, &stream->accept_node); |
184 | 0 | if (stream->ready_for_gc_node.next != NULL) |
185 | 0 | list_remove(&qsm->ready_for_gc_list, &stream->ready_for_gc_node); |
186 | |
|
187 | 0 | ossl_quic_sstream_free(stream->sstream); |
188 | 0 | stream->sstream = NULL; |
189 | |
|
190 | 0 | ossl_quic_rstream_free(stream->rstream); |
191 | 0 | stream->rstream = NULL; |
192 | |
|
193 | 0 | lh_QUIC_STREAM_delete(qsm->map, stream); |
194 | 0 | OPENSSL_free(stream); |
195 | 0 | } |
196 | | |
197 | | QUIC_STREAM *ossl_quic_stream_map_get_by_id(QUIC_STREAM_MAP *qsm, |
198 | | uint64_t stream_id) |
199 | 0 | { |
200 | 0 | QUIC_STREAM key; |
201 | |
|
202 | 0 | key.id = stream_id; |
203 | |
|
204 | 0 | return lh_QUIC_STREAM_retrieve(qsm->map, &key); |
205 | 0 | } |
206 | | |
207 | | static void stream_map_mark_active(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s) |
208 | 0 | { |
209 | 0 | if (s->active) |
210 | 0 | return; |
211 | | |
212 | 0 | list_insert_tail(&qsm->active_list, &s->active_node); |
213 | |
|
214 | 0 | if (qsm->rr_cur == NULL) |
215 | 0 | qsm->rr_cur = s; |
216 | |
|
217 | 0 | s->active = 1; |
218 | 0 | } |
219 | | |
220 | | static void stream_map_mark_inactive(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s) |
221 | 0 | { |
222 | 0 | if (!s->active) |
223 | 0 | return; |
224 | | |
225 | 0 | if (qsm->rr_cur == s) |
226 | 0 | qsm->rr_cur = active_next(&qsm->active_list, s); |
227 | 0 | if (qsm->rr_cur == s) |
228 | 0 | qsm->rr_cur = NULL; |
229 | |
|
230 | 0 | list_remove(&qsm->active_list, &s->active_node); |
231 | |
|
232 | 0 | s->active = 0; |
233 | 0 | } |
234 | | |
235 | | void ossl_quic_stream_map_set_rr_stepping(QUIC_STREAM_MAP *qsm, size_t stepping) |
236 | 0 | { |
237 | 0 | qsm->rr_stepping = stepping; |
238 | 0 | qsm->rr_counter = 0; |
239 | 0 | } |
240 | | |
241 | | static int stream_has_data_to_send(QUIC_STREAM *s) |
242 | 0 | { |
243 | 0 | OSSL_QUIC_FRAME_STREAM shdr; |
244 | 0 | OSSL_QTX_IOVEC iov[2]; |
245 | 0 | size_t num_iov; |
246 | 0 | uint64_t fc_credit, fc_swm, fc_limit; |
247 | |
|
248 | 0 | switch (s->send_state) { |
249 | 0 | case QUIC_SSTREAM_STATE_READY: |
250 | 0 | case QUIC_SSTREAM_STATE_SEND: |
251 | 0 | case QUIC_SSTREAM_STATE_DATA_SENT: |
252 | | /* |
253 | | * We can still have data to send in DATA_SENT due to retransmissions, |
254 | | * etc. |
255 | | */ |
256 | 0 | break; |
257 | 0 | default: |
258 | 0 | return 0; /* Nothing to send. */ |
259 | 0 | } |
260 | | |
261 | | /* |
262 | | * We cannot determine if we have data to send simply by checking if |
263 | | * ossl_quic_txfc_get_credit() is zero, because we may also have older |
264 | | * stream data we need to retransmit. The SSTREAM returns older data first, |
265 | | * so we do a simple comparison of the next chunk the SSTREAM wants to send |
266 | | * against the TXFC CWM. |
267 | | */ |
268 | 0 | num_iov = OSSL_NELEM(iov); |
269 | 0 | if (!ossl_quic_sstream_get_stream_frame(s->sstream, 0, &shdr, iov, |
270 | 0 | &num_iov)) |
271 | 0 | return 0; |
272 | | |
273 | 0 | fc_credit = ossl_quic_txfc_get_credit(&s->txfc, 0); |
274 | 0 | fc_swm = ossl_quic_txfc_get_swm(&s->txfc); |
275 | 0 | fc_limit = fc_swm + fc_credit; |
276 | |
|
277 | 0 | return (shdr.is_fin && shdr.len == 0) || shdr.offset < fc_limit; |
278 | 0 | } |
279 | | |
280 | | static ossl_unused int qsm_send_part_permits_gc(const QUIC_STREAM *qs) |
281 | 0 | { |
282 | 0 | switch (qs->send_state) { |
283 | 0 | case QUIC_SSTREAM_STATE_NONE: |
284 | 0 | case QUIC_SSTREAM_STATE_DATA_RECVD: |
285 | 0 | case QUIC_SSTREAM_STATE_RESET_RECVD: |
286 | 0 | return 1; |
287 | 0 | default: |
288 | 0 | return 0; |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | static int qsm_ready_for_gc(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs) |
293 | 0 | { |
294 | 0 | int recv_stream_fully_drained = 0; /* TODO(QUIC FUTURE): Optimisation */ |
295 | | |
296 | | /* |
297 | | * If sstream has no FIN, we auto-reset it at marked-for-deletion time, so |
298 | | * we don't need to worry about that here. |
299 | | */ |
300 | 0 | assert(!qs->deleted |
301 | 0 | || !ossl_quic_stream_has_send(qs) |
302 | 0 | || ossl_quic_stream_send_is_reset(qs) |
303 | 0 | || ossl_quic_stream_send_get_final_size(qs, NULL)); |
304 | | |
305 | 0 | return |
306 | 0 | qs->deleted |
307 | 0 | && (!ossl_quic_stream_has_recv(qs) |
308 | 0 | || recv_stream_fully_drained |
309 | 0 | || qs->acked_stop_sending) |
310 | 0 | && (!ossl_quic_stream_has_send(qs) |
311 | 0 | || qs->send_state == QUIC_SSTREAM_STATE_DATA_RECVD |
312 | 0 | || qs->send_state == QUIC_SSTREAM_STATE_RESET_RECVD); |
313 | 0 | } |
314 | | |
315 | | int ossl_quic_stream_map_is_local_allowed_by_stream_limit(QUIC_STREAM_MAP *qsm, |
316 | | uint64_t stream_ordinal, |
317 | | int is_uni) |
318 | 0 | { |
319 | 0 | uint64_t stream_limit; |
320 | |
|
321 | 0 | if (qsm->get_stream_limit_cb == NULL) |
322 | 0 | return 1; |
323 | | |
324 | 0 | stream_limit = qsm->get_stream_limit_cb(is_uni, qsm->get_stream_limit_cb_arg); |
325 | 0 | return stream_ordinal < stream_limit; |
326 | 0 | } |
327 | | |
328 | | void ossl_quic_stream_map_update_state(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s) |
329 | 0 | { |
330 | 0 | int should_be_active, allowed_by_stream_limit = 1; |
331 | |
|
332 | 0 | if (ossl_quic_stream_is_server_init(s) == qsm->is_server) { |
333 | 0 | int is_uni = !ossl_quic_stream_is_bidi(s); |
334 | 0 | uint64_t stream_ordinal = s->id >> 2; |
335 | |
|
336 | 0 | allowed_by_stream_limit |
337 | 0 | = ossl_quic_stream_map_is_local_allowed_by_stream_limit(qsm, |
338 | 0 | stream_ordinal, |
339 | 0 | is_uni); |
340 | 0 | } |
341 | |
|
342 | 0 | if (s->send_state == QUIC_SSTREAM_STATE_DATA_SENT |
343 | 0 | && ossl_quic_sstream_is_totally_acked(s->sstream)) |
344 | 0 | ossl_quic_stream_map_notify_totally_acked(qsm, s); |
345 | 0 | else if (s->shutdown_flush |
346 | 0 | && s->send_state == QUIC_SSTREAM_STATE_SEND |
347 | 0 | && ossl_quic_sstream_is_totally_acked(s->sstream)) |
348 | 0 | shutdown_flush_done(qsm, s); |
349 | |
|
350 | 0 | if (!s->ready_for_gc) { |
351 | 0 | s->ready_for_gc = qsm_ready_for_gc(qsm, s); |
352 | 0 | if (s->ready_for_gc) |
353 | 0 | list_insert_tail(&qsm->ready_for_gc_list, &s->ready_for_gc_node); |
354 | 0 | } |
355 | |
|
356 | 0 | should_be_active |
357 | 0 | = allowed_by_stream_limit |
358 | 0 | && !s->ready_for_gc |
359 | 0 | && ((ossl_quic_stream_has_recv(s) |
360 | 0 | && !ossl_quic_stream_recv_is_reset(s) |
361 | 0 | && (s->recv_state == QUIC_RSTREAM_STATE_RECV |
362 | 0 | && (s->want_max_stream_data |
363 | 0 | || ossl_quic_rxfc_has_cwm_changed(&s->rxfc, 0)))) |
364 | 0 | || s->want_stop_sending |
365 | 0 | || s->want_reset_stream |
366 | 0 | || (!s->peer_stop_sending && stream_has_data_to_send(s))); |
367 | |
|
368 | 0 | if (should_be_active) |
369 | 0 | stream_map_mark_active(qsm, s); |
370 | 0 | else |
371 | 0 | stream_map_mark_inactive(qsm, s); |
372 | 0 | } |
373 | | |
374 | | /* |
375 | | * Stream Send Part State Management |
376 | | * ================================= |
377 | | */ |
378 | | |
379 | | int ossl_quic_stream_map_ensure_send_part_id(QUIC_STREAM_MAP *qsm, |
380 | | QUIC_STREAM *qs) |
381 | 0 | { |
382 | 0 | switch (qs->send_state) { |
383 | 0 | case QUIC_SSTREAM_STATE_NONE: |
384 | | /* Stream without send part - caller error. */ |
385 | 0 | return 0; |
386 | | |
387 | 0 | case QUIC_SSTREAM_STATE_READY: |
388 | | /* |
389 | | * We always allocate a stream ID upfront, so we don't need to do it |
390 | | * here. |
391 | | */ |
392 | 0 | qs->send_state = QUIC_SSTREAM_STATE_SEND; |
393 | 0 | return 1; |
394 | | |
395 | 0 | default: |
396 | | /* Nothing to do. */ |
397 | 0 | return 1; |
398 | 0 | } |
399 | 0 | } |
400 | | |
401 | | int ossl_quic_stream_map_notify_all_data_sent(QUIC_STREAM_MAP *qsm, |
402 | | QUIC_STREAM *qs) |
403 | 0 | { |
404 | 0 | switch (qs->send_state) { |
405 | 0 | default: |
406 | | /* Wrong state - caller error. */ |
407 | 0 | case QUIC_SSTREAM_STATE_NONE: |
408 | | /* Stream without send part - caller error. */ |
409 | 0 | return 0; |
410 | | |
411 | 0 | case QUIC_SSTREAM_STATE_SEND: |
412 | 0 | if (!ossl_quic_sstream_get_final_size(qs->sstream, &qs->send_final_size)) |
413 | 0 | return 0; |
414 | | |
415 | 0 | qs->send_state = QUIC_SSTREAM_STATE_DATA_SENT; |
416 | 0 | return 1; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | | static void shutdown_flush_done(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs) |
421 | 0 | { |
422 | 0 | if (!qs->shutdown_flush) |
423 | 0 | return; |
424 | | |
425 | 0 | assert(qsm->num_shutdown_flush > 0); |
426 | 0 | qs->shutdown_flush = 0; |
427 | 0 | --qsm->num_shutdown_flush; |
428 | 0 | } |
429 | | |
430 | | int ossl_quic_stream_map_notify_totally_acked(QUIC_STREAM_MAP *qsm, |
431 | | QUIC_STREAM *qs) |
432 | 0 | { |
433 | 0 | switch (qs->send_state) { |
434 | 0 | default: |
435 | | /* Wrong state - caller error. */ |
436 | 0 | case QUIC_SSTREAM_STATE_NONE: |
437 | | /* Stream without send part - caller error. */ |
438 | 0 | return 0; |
439 | | |
440 | 0 | case QUIC_SSTREAM_STATE_DATA_SENT: |
441 | 0 | qs->send_state = QUIC_SSTREAM_STATE_DATA_RECVD; |
442 | | /* We no longer need a QUIC_SSTREAM in this state. */ |
443 | 0 | ossl_quic_sstream_free(qs->sstream); |
444 | 0 | qs->sstream = NULL; |
445 | |
|
446 | 0 | shutdown_flush_done(qsm, qs); |
447 | 0 | return 1; |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | | int ossl_quic_stream_map_reset_stream_send_part(QUIC_STREAM_MAP *qsm, |
452 | | QUIC_STREAM *qs, |
453 | | uint64_t aec) |
454 | 0 | { |
455 | 0 | switch (qs->send_state) { |
456 | 0 | default: |
457 | 0 | case QUIC_SSTREAM_STATE_NONE: |
458 | | /* |
459 | | * RESET_STREAM pertains to sending part only, so we cannot reset a |
460 | | * receive-only stream. |
461 | | */ |
462 | 0 | case QUIC_SSTREAM_STATE_DATA_RECVD: |
463 | | /* |
464 | | * RFC 9000 s. 3.3: A sender MUST NOT [...] send RESET_STREAM from a |
465 | | * terminal state. If the stream has already finished normally and the |
466 | | * peer has acknowledged this, we cannot reset it. |
467 | | */ |
468 | 0 | return 0; |
469 | | |
470 | 0 | case QUIC_SSTREAM_STATE_READY: |
471 | 0 | if (!ossl_quic_stream_map_ensure_send_part_id(qsm, qs)) |
472 | 0 | return 0; |
473 | | |
474 | | /* FALLTHROUGH */ |
475 | 0 | case QUIC_SSTREAM_STATE_SEND: |
476 | | /* |
477 | | * If we already have a final size (e.g. because we are coming from |
478 | | * DATA_SENT), we have to be consistent with that, so don't change it. |
479 | | * If we don't already have a final size, determine a final size value. |
480 | | * This is the value which we will end up using for a RESET_STREAM frame |
481 | | * for flow control purposes. We could send the stream size (total |
482 | | * number of bytes appended to QUIC_SSTREAM by the application), but it |
483 | | * is in our interest to exclude any bytes we have not actually |
484 | | * transmitted yet, to avoid unnecessarily consuming flow control |
485 | | * credit. We can get this from the TXFC. |
486 | | */ |
487 | 0 | qs->send_final_size = ossl_quic_txfc_get_swm(&qs->txfc); |
488 | | |
489 | | /* FALLTHROUGH */ |
490 | 0 | case QUIC_SSTREAM_STATE_DATA_SENT: |
491 | 0 | qs->reset_stream_aec = aec; |
492 | 0 | qs->want_reset_stream = 1; |
493 | 0 | qs->send_state = QUIC_SSTREAM_STATE_RESET_SENT; |
494 | |
|
495 | 0 | ossl_quic_sstream_free(qs->sstream); |
496 | 0 | qs->sstream = NULL; |
497 | |
|
498 | 0 | shutdown_flush_done(qsm, qs); |
499 | 0 | ossl_quic_stream_map_update_state(qsm, qs); |
500 | 0 | return 1; |
501 | | |
502 | 0 | case QUIC_SSTREAM_STATE_RESET_SENT: |
503 | 0 | case QUIC_SSTREAM_STATE_RESET_RECVD: |
504 | | /* |
505 | | * Idempotent - no-op. In any case, do not send RESET_STREAM again - as |
506 | | * mentioned, we must not send it from a terminal state. |
507 | | */ |
508 | 0 | return 1; |
509 | 0 | } |
510 | 0 | } |
511 | | |
512 | | int ossl_quic_stream_map_notify_reset_stream_acked(QUIC_STREAM_MAP *qsm, |
513 | | QUIC_STREAM *qs) |
514 | 0 | { |
515 | 0 | switch (qs->send_state) { |
516 | 0 | default: |
517 | | /* Wrong state - caller error. */ |
518 | 0 | case QUIC_SSTREAM_STATE_NONE: |
519 | | /* Stream without send part - caller error. */ |
520 | 0 | return 0; |
521 | | |
522 | 0 | case QUIC_SSTREAM_STATE_RESET_SENT: |
523 | 0 | qs->send_state = QUIC_SSTREAM_STATE_RESET_RECVD; |
524 | 0 | return 1; |
525 | | |
526 | 0 | case QUIC_SSTREAM_STATE_RESET_RECVD: |
527 | | /* Already in the correct state. */ |
528 | 0 | return 1; |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | /* |
533 | | * Stream Receive Part State Management |
534 | | * ==================================== |
535 | | */ |
536 | | |
537 | | int ossl_quic_stream_map_notify_size_known_recv_part(QUIC_STREAM_MAP *qsm, |
538 | | QUIC_STREAM *qs, |
539 | | uint64_t final_size) |
540 | 0 | { |
541 | 0 | switch (qs->recv_state) { |
542 | 0 | default: |
543 | | /* Wrong state - caller error. */ |
544 | 0 | case QUIC_RSTREAM_STATE_NONE: |
545 | | /* Stream without receive part - caller error. */ |
546 | 0 | return 0; |
547 | | |
548 | 0 | case QUIC_RSTREAM_STATE_RECV: |
549 | 0 | qs->recv_state = QUIC_RSTREAM_STATE_SIZE_KNOWN; |
550 | 0 | return 1; |
551 | 0 | } |
552 | 0 | } |
553 | | |
554 | | int ossl_quic_stream_map_notify_totally_received(QUIC_STREAM_MAP *qsm, |
555 | | QUIC_STREAM *qs) |
556 | 0 | { |
557 | 0 | switch (qs->recv_state) { |
558 | 0 | default: |
559 | | /* Wrong state - caller error. */ |
560 | 0 | case QUIC_RSTREAM_STATE_NONE: |
561 | | /* Stream without receive part - caller error. */ |
562 | 0 | return 0; |
563 | | |
564 | 0 | case QUIC_RSTREAM_STATE_SIZE_KNOWN: |
565 | 0 | qs->recv_state = QUIC_RSTREAM_STATE_DATA_RECVD; |
566 | 0 | qs->want_stop_sending = 0; |
567 | 0 | return 1; |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | | int ossl_quic_stream_map_notify_totally_read(QUIC_STREAM_MAP *qsm, |
572 | | QUIC_STREAM *qs) |
573 | 0 | { |
574 | 0 | switch (qs->recv_state) { |
575 | 0 | default: |
576 | | /* Wrong state - caller error. */ |
577 | 0 | case QUIC_RSTREAM_STATE_NONE: |
578 | | /* Stream without receive part - caller error. */ |
579 | 0 | return 0; |
580 | | |
581 | 0 | case QUIC_RSTREAM_STATE_DATA_RECVD: |
582 | 0 | qs->recv_state = QUIC_RSTREAM_STATE_DATA_READ; |
583 | | |
584 | | /* QUIC_RSTREAM is no longer needed */ |
585 | 0 | ossl_quic_rstream_free(qs->rstream); |
586 | 0 | qs->rstream = NULL; |
587 | 0 | return 1; |
588 | 0 | } |
589 | 0 | } |
590 | | |
591 | | int ossl_quic_stream_map_notify_reset_recv_part(QUIC_STREAM_MAP *qsm, |
592 | | QUIC_STREAM *qs, |
593 | | uint64_t app_error_code, |
594 | | uint64_t final_size) |
595 | 0 | { |
596 | 0 | uint64_t prev_final_size; |
597 | |
|
598 | 0 | switch (qs->recv_state) { |
599 | 0 | default: |
600 | 0 | case QUIC_RSTREAM_STATE_NONE: |
601 | | /* Stream without receive part - caller error. */ |
602 | 0 | return 0; |
603 | | |
604 | 0 | case QUIC_RSTREAM_STATE_RECV: |
605 | 0 | case QUIC_RSTREAM_STATE_SIZE_KNOWN: |
606 | 0 | case QUIC_RSTREAM_STATE_DATA_RECVD: |
607 | 0 | if (ossl_quic_stream_recv_get_final_size(qs, &prev_final_size) |
608 | 0 | && prev_final_size != final_size) |
609 | | /* Cannot change previous final size. */ |
610 | 0 | return 0; |
611 | | |
612 | 0 | qs->recv_state = QUIC_RSTREAM_STATE_RESET_RECVD; |
613 | 0 | qs->peer_reset_stream_aec = app_error_code; |
614 | | |
615 | | /* RFC 9000 s. 3.3: No point sending STOP_SENDING if already reset. */ |
616 | 0 | qs->want_stop_sending = 0; |
617 | | |
618 | | /* QUIC_RSTREAM is no longer needed */ |
619 | 0 | ossl_quic_rstream_free(qs->rstream); |
620 | 0 | qs->rstream = NULL; |
621 | |
|
622 | 0 | ossl_quic_stream_map_update_state(qsm, qs); |
623 | 0 | return 1; |
624 | | |
625 | 0 | case QUIC_RSTREAM_STATE_DATA_READ: |
626 | | /* |
627 | | * If we already retired the FIN to the application this is moot |
628 | | * - just ignore. |
629 | | */ |
630 | 0 | case QUIC_RSTREAM_STATE_RESET_RECVD: |
631 | 0 | case QUIC_RSTREAM_STATE_RESET_READ: |
632 | | /* Could be a reordered/retransmitted frame - just ignore. */ |
633 | 0 | return 1; |
634 | 0 | } |
635 | 0 | } |
636 | | |
637 | | int ossl_quic_stream_map_notify_app_read_reset_recv_part(QUIC_STREAM_MAP *qsm, |
638 | | QUIC_STREAM *qs) |
639 | 0 | { |
640 | 0 | switch (qs->recv_state) { |
641 | 0 | default: |
642 | | /* Wrong state - caller error. */ |
643 | 0 | case QUIC_RSTREAM_STATE_NONE: |
644 | | /* Stream without receive part - caller error. */ |
645 | 0 | return 0; |
646 | | |
647 | 0 | case QUIC_RSTREAM_STATE_RESET_RECVD: |
648 | 0 | qs->recv_state = QUIC_RSTREAM_STATE_RESET_READ; |
649 | 0 | return 1; |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | | int ossl_quic_stream_map_stop_sending_recv_part(QUIC_STREAM_MAP *qsm, |
654 | | QUIC_STREAM *qs, |
655 | | uint64_t aec) |
656 | 0 | { |
657 | 0 | if (qs->stop_sending) |
658 | 0 | return 0; |
659 | | |
660 | 0 | switch (qs->recv_state) { |
661 | 0 | default: |
662 | 0 | case QUIC_RSTREAM_STATE_NONE: |
663 | | /* Send-only stream, so this makes no sense. */ |
664 | 0 | case QUIC_RSTREAM_STATE_DATA_RECVD: |
665 | 0 | case QUIC_RSTREAM_STATE_DATA_READ: |
666 | | /* |
667 | | * Not really any point in STOP_SENDING if we already received all data. |
668 | | */ |
669 | 0 | case QUIC_RSTREAM_STATE_RESET_RECVD: |
670 | 0 | case QUIC_RSTREAM_STATE_RESET_READ: |
671 | | /* |
672 | | * RFC 9000 s. 3.5: "STOP_SENDING SHOULD only be sent for a stream that |
673 | | * has not been reset by the peer." |
674 | | * |
675 | | * No point in STOP_SENDING if the peer already reset their send part. |
676 | | */ |
677 | 0 | return 0; |
678 | | |
679 | 0 | case QUIC_RSTREAM_STATE_RECV: |
680 | 0 | case QUIC_RSTREAM_STATE_SIZE_KNOWN: |
681 | | /* |
682 | | * RFC 9000 s. 3.5: "If the stream is in the Recv or Size Known state, |
683 | | * the transport SHOULD signal this by sending a STOP_SENDING frame to |
684 | | * prompt closure of the stream in the opposite direction." |
685 | | * |
686 | | * Note that it does make sense to send STOP_SENDING for a receive part |
687 | | * of a stream which has a known size (because we have received a FIN) |
688 | | * but which still has other (previous) stream data yet to be received. |
689 | | */ |
690 | 0 | break; |
691 | 0 | } |
692 | | |
693 | 0 | qs->stop_sending = 1; |
694 | 0 | qs->stop_sending_aec = aec; |
695 | 0 | return ossl_quic_stream_map_schedule_stop_sending(qsm, qs); |
696 | 0 | } |
697 | | |
698 | | /* Called to mark STOP_SENDING for generation, or regeneration after loss. */ |
699 | | int ossl_quic_stream_map_schedule_stop_sending(QUIC_STREAM_MAP *qsm, QUIC_STREAM *qs) |
700 | 0 | { |
701 | 0 | if (!qs->stop_sending) |
702 | 0 | return 0; |
703 | | |
704 | | /* |
705 | | * Ignore the call as a no-op if already scheduled, or in a state |
706 | | * where it makes no sense to send STOP_SENDING. |
707 | | */ |
708 | 0 | if (qs->want_stop_sending) |
709 | 0 | return 1; |
710 | | |
711 | 0 | switch (qs->recv_state) { |
712 | 0 | default: |
713 | 0 | return 1; /* ignore */ |
714 | 0 | case QUIC_RSTREAM_STATE_RECV: |
715 | 0 | case QUIC_RSTREAM_STATE_SIZE_KNOWN: |
716 | | /* |
717 | | * RFC 9000 s. 3.5: "An endpoint is expected to send another |
718 | | * STOP_SENDING frame if a packet containing a previous STOP_SENDING is |
719 | | * lost. However, once either all stream data or a RESET_STREAM frame |
720 | | * has been received for the stream -- that is, the stream is in any |
721 | | * state other than "Recv" or "Size Known" -- sending a STOP_SENDING |
722 | | * frame is unnecessary." |
723 | | */ |
724 | 0 | break; |
725 | 0 | } |
726 | | |
727 | 0 | qs->want_stop_sending = 1; |
728 | 0 | ossl_quic_stream_map_update_state(qsm, qs); |
729 | 0 | return 1; |
730 | 0 | } |
731 | | |
732 | | QUIC_STREAM *ossl_quic_stream_map_peek_accept_queue(QUIC_STREAM_MAP *qsm) |
733 | 0 | { |
734 | 0 | return accept_head(&qsm->accept_list); |
735 | 0 | } |
736 | | |
737 | | void ossl_quic_stream_map_push_accept_queue(QUIC_STREAM_MAP *qsm, |
738 | | QUIC_STREAM *s) |
739 | 0 | { |
740 | 0 | list_insert_tail(&qsm->accept_list, &s->accept_node); |
741 | 0 | if (ossl_quic_stream_is_bidi(s)) |
742 | 0 | ++qsm->num_accept_bidi; |
743 | 0 | else |
744 | 0 | ++qsm->num_accept_uni; |
745 | 0 | } |
746 | | |
747 | | static QUIC_RXFC *qsm_get_max_streams_rxfc(QUIC_STREAM_MAP *qsm, QUIC_STREAM *s) |
748 | 0 | { |
749 | 0 | return ossl_quic_stream_is_bidi(s) |
750 | 0 | ? qsm->max_streams_bidi_rxfc |
751 | 0 | : qsm->max_streams_uni_rxfc; |
752 | 0 | } |
753 | | |
754 | | void ossl_quic_stream_map_remove_from_accept_queue(QUIC_STREAM_MAP *qsm, |
755 | | QUIC_STREAM *s, |
756 | | OSSL_TIME rtt) |
757 | 0 | { |
758 | 0 | QUIC_RXFC *max_streams_rxfc; |
759 | |
|
760 | 0 | list_remove(&qsm->accept_list, &s->accept_node); |
761 | 0 | if (ossl_quic_stream_is_bidi(s)) |
762 | 0 | --qsm->num_accept_bidi; |
763 | 0 | else |
764 | 0 | --qsm->num_accept_uni; |
765 | |
|
766 | 0 | if ((max_streams_rxfc = qsm_get_max_streams_rxfc(qsm, s)) != NULL) |
767 | 0 | (void)ossl_quic_rxfc_on_retire(max_streams_rxfc, 1, rtt); |
768 | 0 | } |
769 | | |
770 | | size_t ossl_quic_stream_map_get_accept_queue_len(QUIC_STREAM_MAP *qsm, int is_uni) |
771 | 0 | { |
772 | 0 | return is_uni ? qsm->num_accept_uni : qsm->num_accept_bidi; |
773 | 0 | } |
774 | | |
775 | | size_t ossl_quic_stream_map_get_total_accept_queue_len(QUIC_STREAM_MAP *qsm) |
776 | 0 | { |
777 | 0 | return ossl_quic_stream_map_get_accept_queue_len(qsm, /*is_uni=*/0) |
778 | 0 | + ossl_quic_stream_map_get_accept_queue_len(qsm, /*is_uni=*/1); |
779 | 0 | } |
780 | | |
781 | | void ossl_quic_stream_map_gc(QUIC_STREAM_MAP *qsm) |
782 | 0 | { |
783 | 0 | QUIC_STREAM *qs, *qs_head, *qsn = NULL; |
784 | |
|
785 | 0 | for (qs = qs_head = ready_for_gc_head(&qsm->ready_for_gc_list); |
786 | 0 | qs != NULL && qs != qs_head; |
787 | 0 | qs = qsn) |
788 | 0 | { |
789 | 0 | qsn = ready_for_gc_next(&qsm->ready_for_gc_list, qs); |
790 | |
|
791 | 0 | ossl_quic_stream_map_release(qsm, qs); |
792 | 0 | } |
793 | 0 | } |
794 | | |
795 | | static int eligible_for_shutdown_flush(QUIC_STREAM *qs) |
796 | 0 | { |
797 | | /* |
798 | | * We only care about servicing the send part of a stream (if any) during |
799 | | * shutdown flush. We make sure we flush a stream if it is either |
800 | | * non-terminated or was terminated normally such as via |
801 | | * SSL_stream_conclude. A stream which was terminated via a reset is not |
802 | | * flushed, and we will have thrown away the send buffer in that case |
803 | | * anyway. |
804 | | */ |
805 | 0 | switch (qs->send_state) { |
806 | 0 | case QUIC_SSTREAM_STATE_SEND: |
807 | 0 | case QUIC_SSTREAM_STATE_DATA_SENT: |
808 | 0 | return !ossl_quic_sstream_is_totally_acked(qs->sstream); |
809 | 0 | default: |
810 | 0 | return 0; |
811 | 0 | } |
812 | 0 | } |
813 | | |
814 | | static void begin_shutdown_flush_each(QUIC_STREAM *qs, void *arg) |
815 | 0 | { |
816 | 0 | QUIC_STREAM_MAP *qsm = arg; |
817 | |
|
818 | 0 | if (!eligible_for_shutdown_flush(qs) || qs->shutdown_flush) |
819 | 0 | return; |
820 | | |
821 | 0 | qs->shutdown_flush = 1; |
822 | 0 | ++qsm->num_shutdown_flush; |
823 | 0 | } |
824 | | |
825 | | void ossl_quic_stream_map_begin_shutdown_flush(QUIC_STREAM_MAP *qsm) |
826 | 0 | { |
827 | 0 | qsm->num_shutdown_flush = 0; |
828 | |
|
829 | 0 | ossl_quic_stream_map_visit(qsm, begin_shutdown_flush_each, qsm); |
830 | 0 | } |
831 | | |
832 | | int ossl_quic_stream_map_is_shutdown_flush_finished(QUIC_STREAM_MAP *qsm) |
833 | 0 | { |
834 | 0 | return qsm->num_shutdown_flush == 0; |
835 | 0 | } |
836 | | |
837 | | /* |
838 | | * QUIC Stream Iterator |
839 | | * ==================== |
840 | | */ |
841 | | void ossl_quic_stream_iter_init(QUIC_STREAM_ITER *it, QUIC_STREAM_MAP *qsm, |
842 | | int advance_rr) |
843 | 0 | { |
844 | 0 | it->qsm = qsm; |
845 | 0 | it->stream = it->first_stream = qsm->rr_cur; |
846 | 0 | if (advance_rr && it->stream != NULL |
847 | 0 | && ++qsm->rr_counter >= qsm->rr_stepping) { |
848 | 0 | qsm->rr_counter = 0; |
849 | 0 | qsm->rr_cur = active_next(&qsm->active_list, qsm->rr_cur); |
850 | 0 | } |
851 | 0 | } |
852 | | |
853 | | void ossl_quic_stream_iter_next(QUIC_STREAM_ITER *it) |
854 | 0 | { |
855 | 0 | if (it->stream == NULL) |
856 | 0 | return; |
857 | | |
858 | 0 | it->stream = active_next(&it->qsm->active_list, it->stream); |
859 | 0 | if (it->stream == it->first_stream) |
860 | 0 | it->stream = NULL; |
861 | 0 | } |