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