7af86c8841066627c3e08c4230c6d2af3db51fff
[openssl.git] / ssl / quic / quic_rx_depack.c
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "internal/packet_quic.h"
11 #include "internal/nelem.h"
12 #include "internal/quic_wire.h"
13 #include "internal/quic_record_rx.h"
14 #include "internal/quic_ackm.h"
15 #include "internal/quic_rx_depack.h"
16 #include "internal/quic_error.h"
17 #include "internal/quic_fc.h"
18 #include "internal/quic_channel.h"
19 #include "internal/sockets.h"
20
21 #include "quic_local.h"
22 #include "quic_channel_local.h"
23 #include "../ssl_local.h"
24
25 /*
26  * Helper functions to process different frame types.
27  *
28  * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
29  * pointer argument, the few that aren't ACK eliciting will not.  This makes
30  * them a verifiable pattern against tables where this is specified.
31  */
32
33 static int depack_do_frame_padding(PACKET *pkt)
34 {
35     /* We ignore this frame */
36     ossl_quic_wire_decode_padding(pkt);
37     return 1;
38 }
39
40 static int depack_do_frame_ping(PACKET *pkt, QUIC_CHANNEL *ch,
41                                 OSSL_ACKM_RX_PKT *ackm_data)
42 {
43     /* We ignore this frame, apart from eliciting an ACK */
44     if (!ossl_quic_wire_decode_frame_ping(pkt)) {
45         ossl_quic_channel_raise_protocol_error(ch,
46                                                QUIC_ERR_FRAME_ENCODING_ERROR,
47                                                OSSL_QUIC_FRAME_TYPE_PING,
48                                                "decode error");
49         return 0;
50     }
51
52     /* This frame makes the packet ACK eliciting */
53     ackm_data->is_ack_eliciting = 1;
54     return 1;
55 }
56
57 static int depack_do_frame_ack(PACKET *pkt, QUIC_CHANNEL *ch,
58                                int packet_space, OSSL_TIME received,
59                                uint64_t frame_type)
60 {
61     OSSL_QUIC_FRAME_ACK ack;
62     OSSL_QUIC_ACK_RANGE *ack_ranges = NULL;
63     uint64_t total_ranges = 0;
64     uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
65
66     if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
67         /* In case sizeof(uint64_t) > sizeof(size_t) */
68         || total_ranges > SIZE_MAX / sizeof(ack_ranges[0])
69         || (ack_ranges = OPENSSL_zalloc(sizeof(ack_ranges[0])
70                                         * (size_t)total_ranges)) == NULL)
71         goto malformed;
72
73     ack.ack_ranges = ack_ranges;
74     ack.num_ack_ranges = (size_t)total_ranges;
75
76     if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
77         goto malformed;
78
79     if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
80                                    packet_space, received))
81         goto malformed;
82
83     OPENSSL_free(ack_ranges);
84     return 1;
85
86 malformed:
87     ossl_quic_channel_raise_protocol_error(ch,
88                                            QUIC_ERR_FRAME_ENCODING_ERROR,
89                                            frame_type,
90                                            "decode error");
91     OPENSSL_free(ack_ranges);
92     return 0;
93 }
94
95 static int depack_do_frame_reset_stream(PACKET *pkt,
96                                         QUIC_CHANNEL *ch,
97                                         OSSL_ACKM_RX_PKT *ackm_data)
98 {
99     OSSL_QUIC_FRAME_RESET_STREAM frame_data;
100     QUIC_STREAM *stream = NULL;
101
102     if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
103         ossl_quic_channel_raise_protocol_error(ch,
104                                                QUIC_ERR_FRAME_ENCODING_ERROR,
105                                                OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
106                                                "decode error");
107         return 0;
108     }
109
110     /* This frame makes the packet ACK eliciting */
111     ackm_data->is_ack_eliciting = 1;
112
113     stream = ossl_quic_stream_map_get_by_id(&ch->qsm, frame_data.stream_id);
114     if (stream == NULL) {
115         ossl_quic_channel_raise_protocol_error(ch,
116                                                QUIC_ERR_STREAM_STATE_ERROR,
117                                                OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
118                                                "RESET_STREAM frame for "
119                                                "nonexistent stream");
120         return 0;
121     }
122
123     if (stream->rstream == NULL) {
124         ossl_quic_channel_raise_protocol_error(ch,
125                                                QUIC_ERR_STREAM_STATE_ERROR,
126                                                OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
127                                                "RESET_STREAM frame for "
128                                                "TX only stream");
129         return 0;
130     }
131
132     stream->peer_reset_stream = 1;
133     ossl_quic_stream_map_update_state(&ch->qsm, stream);
134     return 1;
135 }
136
137 static int depack_do_frame_stop_sending(PACKET *pkt,
138                                         QUIC_CHANNEL *ch,
139                                         OSSL_ACKM_RX_PKT *ackm_data)
140 {
141     OSSL_QUIC_FRAME_STOP_SENDING frame_data;
142     QUIC_STREAM *stream = NULL;
143
144     if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
145         ossl_quic_channel_raise_protocol_error(ch,
146                                                QUIC_ERR_FRAME_ENCODING_ERROR,
147                                                OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
148                                                "decode error");
149         return 0;
150     }
151
152     /* This frame makes the packet ACK eliciting */
153     ackm_data->is_ack_eliciting = 1;
154
155     stream = ossl_quic_stream_map_get_by_id(&ch->qsm, frame_data.stream_id);
156     if (stream == NULL) {
157         ossl_quic_channel_raise_protocol_error(ch,
158                                                QUIC_ERR_STREAM_STATE_ERROR,
159                                                OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
160                                                "STOP_SENDING frame for "
161                                                "nonexistent stream");
162         return 0;
163     }
164
165     if (stream->sstream == NULL) {
166         ossl_quic_channel_raise_protocol_error(ch,
167                                                QUIC_ERR_STREAM_STATE_ERROR,
168                                                OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
169                                                "STOP_SENDING frame for "
170                                                "RX only stream");
171         return 0;
172     }
173
174     stream->peer_stop_sending = 1;
175     ossl_quic_stream_map_update_state(&ch->qsm, stream);
176     return 1;
177 }
178
179 static int depack_do_frame_crypto(PACKET *pkt, QUIC_CHANNEL *ch,
180                                   OSSL_QRX_PKT *parent_pkt,
181                                   OSSL_ACKM_RX_PKT *ackm_data)
182 {
183     OSSL_QUIC_FRAME_CRYPTO f;
184     QUIC_RSTREAM *rstream;
185
186     if (!ossl_quic_wire_decode_frame_crypto(pkt, &f)) {
187         ossl_quic_channel_raise_protocol_error(ch,
188                                                QUIC_ERR_FRAME_ENCODING_ERROR,
189                                                OSSL_QUIC_FRAME_TYPE_CRYPTO,
190                                                "decode error");
191         return 0;
192     }
193
194     /* This frame makes the packet ACK eliciting */
195     ackm_data->is_ack_eliciting = 1;
196
197     rstream = ch->crypto_recv[ackm_data->pkt_space];
198     if (!ossl_assert(rstream != NULL))
199         /*
200          * This should not happen; we should only have a NULL stream here if
201          * the EL has been discarded, and if the EL has been discarded we
202          * shouldn't be here.
203          */
204         return 0;
205
206     if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
207                                       f.offset, f.data, f.len, 0))
208         return 0;
209
210     return 1;
211 }
212
213 static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
214                                      OSSL_ACKM_RX_PKT *ackm_data)
215 {
216     const uint8_t *token;
217     size_t token_len;
218
219     if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
220         ossl_quic_channel_raise_protocol_error(ch,
221                                                QUIC_ERR_FRAME_ENCODING_ERROR,
222                                                OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
223                                                "decode error");
224         return 0;
225     }
226
227     /* This frame makes the packet ACK eliciting */
228     ackm_data->is_ack_eliciting = 1;
229
230     /* TODO(QUIC): ADD CODE to send |token| to the session manager */
231
232     return 1;
233 }
234
235 static int depack_do_frame_stream(PACKET *pkt, QUIC_CHANNEL *ch,
236                                   OSSL_QRX_PKT *parent_pkt,
237                                   OSSL_ACKM_RX_PKT *ackm_data,
238                                   uint64_t frame_type)
239 {
240     OSSL_QUIC_FRAME_STREAM frame_data;
241     QUIC_STREAM *stream;
242     uint64_t fce;
243
244     if (!ossl_quic_wire_decode_frame_stream(pkt, &frame_data)) {
245         ossl_quic_channel_raise_protocol_error(ch,
246                                                QUIC_ERR_FRAME_ENCODING_ERROR,
247                                                frame_type,
248                                                "decode error");
249         return 0;
250     }
251
252     /* This frame makes the packet ACK eliciting */
253     ackm_data->is_ack_eliciting = 1;
254
255     stream = ossl_quic_stream_map_get_by_id(&ch->qsm, frame_data.stream_id);
256     if (stream == NULL) {
257         ossl_quic_channel_raise_protocol_error(ch,
258                                                QUIC_ERR_STREAM_STATE_ERROR,
259                                                frame_type,
260                                                "STREAM frame for nonexistent "
261                                                "stream");
262         return 0;
263     }
264
265     if (stream->rstream == NULL) {
266         ossl_quic_channel_raise_protocol_error(ch,
267                                                QUIC_ERR_STREAM_STATE_ERROR,
268                                                frame_type,
269                                                "STREAM frame for TX only "
270                                                "stream");
271         return 0;
272     }
273
274     /* Notify stream flow controller. */
275     if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
276                                            frame_data.offset + frame_data.len,
277                                            frame_data.is_fin)) {
278         ossl_quic_channel_raise_protocol_error(ch,
279                                                QUIC_ERR_INTERNAL_ERROR,
280                                                frame_type,
281                                                "internal error (flow control)");
282         return 0;
283     }
284
285     /* Has a flow control error occurred? */
286     fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
287     if (fce != QUIC_ERR_NO_ERROR) {
288         ossl_quic_channel_raise_protocol_error(ch,
289                                                fce,
290                                                frame_type,
291                                                "flow control violation");
292         return 0;
293     }
294
295     /*
296      * The receive stream buffer may or may not choose to consume the data
297      * without copying by reffing the OSSL_QRX_PKT. In this case
298      * ossl_qrx_pkt_release() will be eventually called when the data is no
299      * longer needed.
300      */
301     if (!ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
302                                       frame_data.offset,
303                                       frame_data.data,
304                                       frame_data.len,
305                                       frame_data.is_fin))
306         return 0;
307
308     return 1;
309 }
310
311 static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
312                                     OSSL_ACKM_RX_PKT *ackm_data)
313 {
314     uint64_t max_data = 0;
315
316     if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
317         ossl_quic_channel_raise_protocol_error(ch,
318                                                QUIC_ERR_FRAME_ENCODING_ERROR,
319                                                OSSL_QUIC_FRAME_TYPE_MAX_DATA,
320                                                "decode error");
321         return 0;
322     }
323
324     /* This frame makes the packet ACK eliciting */
325     ackm_data->is_ack_eliciting = 1;
326
327     ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
328     ossl_quic_stream_map_update_state(&ch->qsm, ch->stream0);
329     return 1;
330 }
331
332 static int depack_do_frame_max_stream_data(PACKET *pkt,
333                                            QUIC_CHANNEL *ch,
334                                            OSSL_ACKM_RX_PKT *ackm_data)
335 {
336     uint64_t stream_id = 0;
337     uint64_t max_stream_data = 0;
338     QUIC_STREAM *stream;
339
340     if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
341                                                      &max_stream_data)) {
342         ossl_quic_channel_raise_protocol_error(ch,
343                                                QUIC_ERR_FRAME_ENCODING_ERROR,
344                                                OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
345                                                "decode error");
346         return 0;
347     }
348
349     /* This frame makes the packet ACK eliciting */
350     ackm_data->is_ack_eliciting = 1;
351
352     stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
353     if (stream == NULL) {
354         ossl_quic_channel_raise_protocol_error(ch,
355                                                QUIC_ERR_STREAM_STATE_ERROR,
356                                                OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
357                                                "MAX_STREAM_DATA for nonexistent "
358                                                "stream");
359         return 0;
360     }
361
362     if (stream->sstream == NULL) {
363         ossl_quic_channel_raise_protocol_error(ch,
364                                                QUIC_ERR_STREAM_STATE_ERROR,
365                                                OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
366                                                "MAX_STREAM_DATA for TX only "
367                                                "stream");
368         return 0;
369     }
370
371     ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
372     ossl_quic_stream_map_update_state(&ch->qsm, stream);
373     return 1;
374 }
375
376 static int depack_do_frame_max_streams(PACKET *pkt,
377                                        QUIC_CHANNEL *ch,
378                                        OSSL_ACKM_RX_PKT *ackm_data,
379                                        uint64_t frame_type)
380 {
381     uint64_t max_streams = 0;
382
383     if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
384         ossl_quic_channel_raise_protocol_error(ch,
385                                                QUIC_ERR_FRAME_ENCODING_ERROR,
386                                                frame_type,
387                                                "decode error");
388         return 0;
389     }
390
391     /* This frame makes the packet ACK eliciting */
392     ackm_data->is_ack_eliciting = 1;
393
394     if (max_streams > (((uint64_t)1) << 60)) {
395         ossl_quic_channel_raise_protocol_error(ch,
396                                                QUIC_ERR_FRAME_ENCODING_ERROR,
397                                                frame_type,
398                                                "invalid max streams value");
399         return 0;
400     }
401
402     switch (frame_type) {
403     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
404         if (max_streams > ch->max_local_streams_bidi)
405             ch->max_local_streams_bidi = max_streams;
406
407         /* Stream may now be able to send */
408         ossl_quic_stream_map_update_state(&ch->qsm,
409                                           ch->stream0);
410         break;
411     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
412         if (max_streams > ch->max_local_streams_uni)
413             ch->max_local_streams_uni = max_streams;
414
415         /* Stream may now be able to send */
416         ossl_quic_stream_map_update_state(&ch->qsm,
417                                           ch->stream0);
418         break;
419     default:
420         ossl_quic_channel_raise_protocol_error(ch,
421                                                QUIC_ERR_FRAME_ENCODING_ERROR,
422                                                frame_type,
423                                                "decode error");
424         return 0;
425     }
426
427     return 1;
428 }
429
430 static int depack_do_frame_data_blocked(PACKET *pkt,
431                                         QUIC_CHANNEL *ch,
432                                         OSSL_ACKM_RX_PKT *ackm_data)
433 {
434     uint64_t max_data = 0;
435
436     if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
437         ossl_quic_channel_raise_protocol_error(ch,
438                                                QUIC_ERR_FRAME_ENCODING_ERROR,
439                                                OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
440                                                "decode error");
441         return 0;
442     }
443
444     /* This frame makes the packet ACK eliciting */
445     ackm_data->is_ack_eliciting = 1;
446
447     /* No-op - informative/debugging frame. */
448     return 1;
449 }
450
451 static int depack_do_frame_stream_data_blocked(PACKET *pkt,
452                                                QUIC_CHANNEL *ch,
453                                                OSSL_ACKM_RX_PKT *ackm_data)
454 {
455     uint64_t stream_id = 0;
456     uint64_t max_data = 0;
457
458     if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
459                                                          &max_data)) {
460         ossl_quic_channel_raise_protocol_error(ch,
461                                                QUIC_ERR_FRAME_ENCODING_ERROR,
462                                                OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
463                                                "decode error");
464         return 0;
465     }
466
467     /* This frame makes the packet ACK eliciting */
468     ackm_data->is_ack_eliciting = 1;
469
470     /* No-op - informative/debugging frame. */
471     return 1;
472 }
473
474 static int depack_do_frame_streams_blocked(PACKET *pkt,
475                                            QUIC_CHANNEL *ch,
476                                            OSSL_ACKM_RX_PKT *ackm_data,
477                                            uint64_t frame_type)
478 {
479     uint64_t max_data = 0;
480
481     if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
482         ossl_quic_channel_raise_protocol_error(ch,
483                                                QUIC_ERR_FRAME_ENCODING_ERROR,
484                                                frame_type,
485                                                "decode error");
486         return 0;
487     }
488
489     /* This frame makes the packet ACK eliciting */
490     ackm_data->is_ack_eliciting = 1;
491
492     /* No-op - informative/debugging frame. */
493     return 1;
494 }
495
496 static int depack_do_frame_new_conn_id(PACKET *pkt,
497                                        QUIC_CHANNEL *ch,
498                                        OSSL_ACKM_RX_PKT *ackm_data)
499 {
500     OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
501
502     if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
503         ossl_quic_channel_raise_protocol_error(ch,
504                                                QUIC_ERR_FRAME_ENCODING_ERROR,
505                                                OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
506                                                "decode error");
507         return 0;
508     }
509
510     /* This frame makes the packet ACK eliciting */
511     ackm_data->is_ack_eliciting = 1;
512
513     /* TODO(QUIC): ADD CODE to send |frame_data.data| to the ch manager */
514
515     return 1;
516 }
517
518 static int depack_do_frame_retire_conn_id(PACKET *pkt,
519                                           QUIC_CHANNEL *ch,
520                                           OSSL_ACKM_RX_PKT *ackm_data)
521 {
522     uint64_t seq_num;
523
524     if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
525         ossl_quic_channel_raise_protocol_error(ch,
526                                                QUIC_ERR_FRAME_ENCODING_ERROR,
527                                                OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
528                                                "decode error");
529         return 0;
530     }
531
532     /* This frame makes the packet ACK eliciting */
533     ackm_data->is_ack_eliciting = 1;
534
535     /* TODO(QUIC): ADD CODE to send |seq_num| to the ch manager */
536     return 1;
537 }
538
539 static int depack_do_frame_path_challenge(PACKET *pkt,
540                                           QUIC_CHANNEL *ch,
541                                           OSSL_ACKM_RX_PKT *ackm_data)
542 {
543     uint64_t frame_data = 0;
544
545     if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
546         ossl_quic_channel_raise_protocol_error(ch,
547                                                QUIC_ERR_FRAME_ENCODING_ERROR,
548                                                OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
549                                                "decode error");
550         return 0;
551     }
552
553     /* This frame makes the packet ACK eliciting */
554     ackm_data->is_ack_eliciting = 1;
555
556     /* TODO(QUIC): ADD CODE to send |frame_data| to the ch manager */
557
558     return 1;
559 }
560
561 static int depack_do_frame_path_response(PACKET *pkt,
562                                          QUIC_CHANNEL *ch,
563                                          OSSL_ACKM_RX_PKT *ackm_data)
564 {
565     uint64_t frame_data = 0;
566
567     if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
568         ossl_quic_channel_raise_protocol_error(ch,
569                                                QUIC_ERR_FRAME_ENCODING_ERROR,
570                                                OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
571                                                "decode error");
572         return 0;
573     }
574
575     /* This frame makes the packet ACK eliciting */
576     ackm_data->is_ack_eliciting = 1;
577
578     /* TODO(QUIC): ADD CODE to send |frame_data| to the ch manager */
579
580     return 1;
581 }
582
583 static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch)
584 {
585     OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
586
587     if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data))
588         return 0;
589
590     ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
591     return 1;
592 }
593
594 static int depack_do_frame_handshake_done(PACKET *pkt,
595                                           QUIC_CHANNEL *ch,
596                                           OSSL_ACKM_RX_PKT *ackm_data)
597 {
598     if (!ossl_quic_wire_decode_frame_handshake_done(pkt))
599         return 0;
600
601     /* This frame makes the packet ACK eliciting */
602     ackm_data->is_ack_eliciting = 1;
603
604     ossl_quic_channel_on_handshake_confirmed(ch);
605     return 1;
606 }
607
608 /* Main frame processor */
609
610 static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
611                                  OSSL_QRX_PKT *parent_pkt, int packet_space,
612                                  OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
613 {
614     uint32_t pkt_type = parent_pkt->hdr->type;
615
616     while (PACKET_remaining(pkt) > 0) {
617         uint64_t frame_type;
618
619         if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type))
620             return 0;
621
622         switch (frame_type) {
623         case OSSL_QUIC_FRAME_TYPE_PING:
624             /* Allowed in all packet types */
625             if (!depack_do_frame_ping(pkt, ch, ackm_data))
626                 return 0;
627             break;
628         case OSSL_QUIC_FRAME_TYPE_PADDING:
629             /* Allowed in all packet types */
630             if (!depack_do_frame_padding(pkt))
631                 return 0;
632             break;
633
634         case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
635         case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
636             /* ACK frames are valid everywhere except in 0RTT packets */
637             if (pkt_type == QUIC_PKT_TYPE_0RTT) {
638                 ossl_quic_channel_raise_protocol_error(ch,
639                                                        QUIC_ERR_PROTOCOL_VIOLATION,
640                                                        frame_type,
641                                                        "ACK not valid in 0-RTT");
642                 return 0;
643             }
644             if (!depack_do_frame_ack(pkt, ch, packet_space, received,
645                                      frame_type))
646                 return 0;
647             break;
648
649         case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
650             /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
651             if (pkt_type != QUIC_PKT_TYPE_0RTT
652                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
653                 ossl_quic_channel_raise_protocol_error(ch,
654                                                        QUIC_ERR_PROTOCOL_VIOLATION,
655                                                        frame_type,
656                                                        "RESET_STREAM not valid in "
657                                                        "INITIAL/HANDSHAKE");
658                 return 0;
659             }
660             if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
661                 return 0;
662             break;
663         case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
664             /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
665             if (pkt_type != QUIC_PKT_TYPE_0RTT
666                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
667                 ossl_quic_channel_raise_protocol_error(ch,
668                                                        QUIC_ERR_PROTOCOL_VIOLATION,
669                                                        frame_type,
670                                                        "STOP_SENDING not valid in "
671                                                        "INITIAL/HANDSHAKE");
672                 return 0;
673             }
674             if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
675                 return 0;
676             break;
677         case OSSL_QUIC_FRAME_TYPE_CRYPTO:
678             /* CRYPTO frames are valid everywhere except in 0RTT packets */
679             if (pkt_type == QUIC_PKT_TYPE_0RTT) {
680                 ossl_quic_channel_raise_protocol_error(ch,
681                                                        QUIC_ERR_PROTOCOL_VIOLATION,
682                                                        frame_type,
683                                                        "CRYPTO frame not valid in 0-RTT");
684                 return 0;
685             }
686             if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data))
687                 return 0;
688             break;
689         case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
690             /* NEW_TOKEN frames are valid in 1RTT packets */
691             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
692                 ossl_quic_channel_raise_protocol_error(ch,
693                                                        QUIC_ERR_PROTOCOL_VIOLATION,
694                                                        frame_type,
695                                                        "NEW_TOKEN valid only in 1-RTT");
696                 return 0;
697             }
698             if (!depack_do_frame_new_token(pkt, ch, ackm_data))
699                 return 0;
700             break;
701
702         case OSSL_QUIC_FRAME_TYPE_STREAM:
703         case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
704         case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
705         case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
706         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
707         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
708         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
709         case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
710             /* STREAM frames are valid in 0RTT and 1RTT packets */
711             if (pkt_type != QUIC_PKT_TYPE_0RTT
712                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
713                 ossl_quic_channel_raise_protocol_error(ch,
714                                                        QUIC_ERR_PROTOCOL_VIOLATION,
715                                                        frame_type,
716                                                        "STREAM valid only in 0/1-RTT");
717                 return 0;
718             }
719             if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
720                                         frame_type))
721                 return 0;
722             break;
723
724         case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
725             /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
726             if (pkt_type != QUIC_PKT_TYPE_0RTT
727                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
728                 ossl_quic_channel_raise_protocol_error(ch,
729                                                        QUIC_ERR_PROTOCOL_VIOLATION,
730                                                        frame_type,
731                                                        "MAX_DATA valid only in 0/1-RTT");
732                 return 0;
733             }
734             if (!depack_do_frame_max_data(pkt, ch, ackm_data))
735                 return 0;
736             break;
737         case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
738             /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
739             if (pkt_type != QUIC_PKT_TYPE_0RTT
740                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
741                 ossl_quic_channel_raise_protocol_error(ch,
742                                                        QUIC_ERR_PROTOCOL_VIOLATION,
743                                                        frame_type,
744                                                        "MAX_STREAM_DATA valid only in 0/1-RTT");
745                 return 0;
746             }
747             if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
748                 return 0;
749             break;
750
751         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
752         case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
753             /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
754             if (pkt_type != QUIC_PKT_TYPE_0RTT
755                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
756                 ossl_quic_channel_raise_protocol_error(ch,
757                                                        QUIC_ERR_PROTOCOL_VIOLATION,
758                                                        frame_type,
759                                                        "MAX_STREAMS valid only in 0/1-RTT");
760                 return 0;
761             }
762             if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
763                                              frame_type))
764                 return 0;
765             break;
766
767         case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
768             /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
769             if (pkt_type != QUIC_PKT_TYPE_0RTT
770                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
771                 ossl_quic_channel_raise_protocol_error(ch,
772                                                        QUIC_ERR_PROTOCOL_VIOLATION,
773                                                        frame_type,
774                                                        "DATA_BLOCKED valid only in 0/1-RTT");
775                 return 0;
776             }
777             if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
778                 return 0;
779             break;
780         case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
781             /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
782             if (pkt_type != QUIC_PKT_TYPE_0RTT
783                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
784                 ossl_quic_channel_raise_protocol_error(ch,
785                                                        QUIC_ERR_PROTOCOL_VIOLATION,
786                                                        frame_type,
787                                                        "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
788                 return 0;
789             }
790             if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
791                 return 0;
792             break;
793
794         case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
795         case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
796             /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
797             if (pkt_type != QUIC_PKT_TYPE_0RTT
798                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
799                 ossl_quic_channel_raise_protocol_error(ch,
800                                                        QUIC_ERR_PROTOCOL_VIOLATION,
801                                                        frame_type,
802                                                        "STREAMS valid only in 0/1-RTT");
803                 return 0;
804             }
805             if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
806                                                  frame_type))
807                 return 0;
808             break;
809
810         case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
811             /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
812             if (pkt_type != QUIC_PKT_TYPE_0RTT
813                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
814                 ossl_quic_channel_raise_protocol_error(ch,
815                                                        QUIC_ERR_PROTOCOL_VIOLATION,
816                                                        frame_type,
817                                                        "NEW_CONN_ID valid only in 0/1-RTT");
818             }
819             if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
820                 return 0;
821             break;
822         case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
823             /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
824             if (pkt_type != QUIC_PKT_TYPE_0RTT
825                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
826                 ossl_quic_channel_raise_protocol_error(ch,
827                                                        QUIC_ERR_PROTOCOL_VIOLATION,
828                                                        frame_type,
829                                                        "RETIRE_CONN_ID valid only in 0/1-RTT");
830                 return 0;
831             }
832             if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
833                 return 0;
834             break;
835         case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
836             /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
837             if (pkt_type != QUIC_PKT_TYPE_0RTT
838                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
839                 ossl_quic_channel_raise_protocol_error(ch,
840                                                        QUIC_ERR_PROTOCOL_VIOLATION,
841                                                        frame_type,
842                                                        "PATH_CHALLENGE valid only in 0/1-RTT");
843                 return 0;
844             }
845             if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
846                 return 0;
847             break;
848         case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
849             /* PATH_RESPONSE frames are valid in 1RTT packets */
850             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
851                 ossl_quic_channel_raise_protocol_error(ch,
852                                                        QUIC_ERR_PROTOCOL_VIOLATION,
853                                                        frame_type,
854                                                        "PATH_CHALLENGE valid only in 1-RTT");
855                 return 0;
856             }
857             if (!depack_do_frame_path_response(pkt, ch, ackm_data))
858                 return 0;
859             break;
860
861         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
862             /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
863             if (pkt_type != QUIC_PKT_TYPE_0RTT
864                 && pkt_type != QUIC_PKT_TYPE_1RTT) {
865                 ossl_quic_channel_raise_protocol_error(ch,
866                                                        QUIC_ERR_PROTOCOL_VIOLATION,
867                                                        frame_type,
868                                                        "CONN_CLOSE (APP) valid only in 0/1-RTT");
869                 return 0;
870             }
871             /* FALLTHRU */
872         case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
873             /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
874             if (!depack_do_frame_conn_close(pkt, ch))
875                 return 0;
876             break;
877
878         case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
879             /* HANDSHAKE_DONE frames are valid in 1RTT packets */
880             if (pkt_type != QUIC_PKT_TYPE_1RTT) {
881                 ossl_quic_channel_raise_protocol_error(ch,
882                                                        QUIC_ERR_PROTOCOL_VIOLATION,
883                                                        frame_type,
884                                                        "HANDSHAKE_DONE valid only in 1-RTT");
885                 return 0;
886             }
887             if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
888                 return 0;
889             break;
890
891         default:
892             /* Unknown frame type */
893             ackm_data->is_ack_eliciting = 1;
894             ossl_quic_channel_raise_protocol_error(ch,
895                                                    QUIC_ERR_PROTOCOL_VIOLATION,
896                                                    frame_type,
897                                                    "Unknown frame type received");
898             return 0;
899         }
900     }
901
902     return 1;
903 }
904
905 QUIC_NEEDS_LOCK
906 int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
907 {
908     PACKET pkt;
909     OSSL_ACKM_RX_PKT ackm_data;
910     /*
911      * ok has three states:
912      * -1 error with ackm_data uninitialized
913      *  0 error with ackm_data initialized
914      *  1 success (ackm_data initialized)
915      */
916     int ok = -1;                  /* Assume the worst */
917
918     if (ch == NULL)
919         goto end;
920
921     /* Initialize |ackm_data| (and reinitialize |ok|)*/
922     memset(&ackm_data, 0, sizeof(ackm_data));
923     /*
924      * TODO(QUIC): ASSUMPTION: All packets that aren't special case have a
925      * packet number
926      */
927     ackm_data.pkt_num = qpacket->pn;
928     ackm_data.time = qpacket->time;
929     switch (qpacket->hdr->type) {
930     case QUIC_PKT_TYPE_INITIAL:
931         ackm_data.pkt_space = QUIC_PN_SPACE_INITIAL;
932         break;
933     case QUIC_PKT_TYPE_HANDSHAKE:
934         ackm_data.pkt_space = QUIC_PN_SPACE_HANDSHAKE;
935         break;
936     case QUIC_PKT_TYPE_0RTT:
937     case QUIC_PKT_TYPE_1RTT:
938         ackm_data.pkt_space = QUIC_PN_SPACE_APP;
939         break;
940     default:
941         /*
942          * Retry and Version Negotiation packets should not be passed to this
943          * function.
944          */
945         goto end;
946     }
947     ok = 0;                      /* Still assume the worst */
948
949     /* Now that special cases are out of the way, parse frames */
950     if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
951         || !depack_process_frames(ch, &pkt, qpacket,
952                                   ackm_data.pkt_space, qpacket->time,
953                                   &ackm_data))
954         goto end;
955
956     ok = 1;
957  end:
958     /*
959      * TODO(QUIC): ASSUMPTION: If this function is called at all, |qpacket| is
960      * a legitimate packet, even if its contents aren't.
961      * Therefore, we call ossl_ackm_on_rx_packet() unconditionally, as long as
962      * |ackm_data| has at least been initialized.
963      */
964     if (ok >= 0)
965         ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
966
967     return ok > 0;
968 }