blob: 6f299ba2d4e2d9390cba5ea485886de3eb053dd5 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01002 * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020010#include <string.h>
11
Frédéric Lécaille8090b512020-11-30 16:19:22 +010012#include <import/eb64tree.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020013#include <haproxy/quic_conn-t.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020014#include <haproxy/quic_enc.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <haproxy/quic_frame.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020016#include <haproxy/quic_tp-t.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010017#include <haproxy/trace.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010018
19#define TRACE_SOURCE &trace_quic
20
21const char *quic_frame_type_string(enum quic_frame_type ft)
22{
23 switch (ft) {
24 case QUIC_FT_PADDING:
25 return "PADDING";
26 case QUIC_FT_PING:
27 return "PING";
28 case QUIC_FT_ACK:
29 return "ACK";
30 case QUIC_FT_ACK_ECN:
31 return "ACK_ENC";
32 case QUIC_FT_RESET_STREAM:
33 return "RESET_STREAM";
34 case QUIC_FT_STOP_SENDING:
35 return "STOP_SENDING";
36 case QUIC_FT_CRYPTO:
37 return "CRYPTO";
38 case QUIC_FT_NEW_TOKEN:
39 return "NEW_TOKEN";
40
41 case QUIC_FT_STREAM_8:
42 return "STREAM_8";
43 case QUIC_FT_STREAM_9:
44 return "STREAM_9";
45 case QUIC_FT_STREAM_A:
46 return "STREAM_A";
47 case QUIC_FT_STREAM_B:
48 return "STREAM_B";
49 case QUIC_FT_STREAM_C:
50 return "STREAM_C";
51 case QUIC_FT_STREAM_D:
52 return "STREAM_D";
53 case QUIC_FT_STREAM_E:
54 return "STREAM_E";
55 case QUIC_FT_STREAM_F:
56 return "STREAM_F";
57
58 case QUIC_FT_MAX_DATA:
59 return "MAX_DATA";
60 case QUIC_FT_MAX_STREAM_DATA:
61 return "MAX_STREAM_DATA";
62 case QUIC_FT_MAX_STREAMS_BIDI:
63 return "MAX_STREAMS_BIDI";
64 case QUIC_FT_MAX_STREAMS_UNI:
65 return "MAX_STREAMS_UNI";
66 case QUIC_FT_DATA_BLOCKED:
67 return "DATA_BLOCKED";
68 case QUIC_FT_STREAM_DATA_BLOCKED:
69 return "STREAM_DATA_BLOCKED";
70 case QUIC_FT_STREAMS_BLOCKED_BIDI:
71 return "STREAMS_BLOCKED_BIDI";
72 case QUIC_FT_STREAMS_BLOCKED_UNI:
73 return "STREAMS_BLOCKED_UNI";
74 case QUIC_FT_NEW_CONNECTION_ID:
75 return "NEW_CONNECTION_ID";
76 case QUIC_FT_RETIRE_CONNECTION_ID:
77 return "RETIRE_CONNECTION_ID";
78 case QUIC_FT_PATH_CHALLENGE:
79 return "PATH_CHALLENGE";
80 case QUIC_FT_PATH_RESPONSE:
81 return "PATH_RESPONSE";
82 case QUIC_FT_CONNECTION_CLOSE:
83 return "CONNECTION_CLOSE";
84 case QUIC_FT_CONNECTION_CLOSE_APP:
85 return "CONNECTION_CLOSE_APP";
86 case QUIC_FT_HANDSHAKE_DONE:
87 return "HANDSHAKE_DONE";
88 default:
89 return "UNKNOWN";
90 }
91}
92
Frédéric Lécaille010e5322021-12-23 15:19:15 +010093static void chunk_cc_phrase_appendf(struct buffer *buf,
94 const unsigned char *phr, size_t phrlen)
95{
96 chunk_appendf(buf, " reason_phrase: '");
97 while (phrlen--)
98 chunk_appendf(buf, "%c", *phr++);
99 chunk_appendf(buf, "'");
100}
101
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100102/* Add traces to <buf> depending on <frm> frame type. */
103void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm)
104{
105 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
106 switch (frm->type) {
107 case QUIC_FT_CRYPTO:
108 {
109 const struct quic_crypto *cf = &frm->crypto;
110 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
111 (ull)cf->offset, (ull)cf->len);
112 break;
113 }
114 case QUIC_FT_RESET_STREAM:
115 {
116 const struct quic_reset_stream *rs = &frm->reset_stream;
117 chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu",
118 (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size);
119 break;
120 }
121 case QUIC_FT_STOP_SENDING:
122 {
123 const struct quic_stop_sending *s = &frm->stop_sending;
124 chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
125 (ull)s->id, (ull)s->app_error_code);
126 break;
127 }
128 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
129 {
130 const struct quic_stream *s = &frm->stream;
131 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
132 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
133 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
134 (ull)s->id, (ull)s->offset.key, (ull)s->len);
135 break;
136 }
137 case QUIC_FT_MAX_DATA:
138 {
139 const struct quic_max_data *s = &frm->max_data;
140 chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
141 break;
142 }
143 case QUIC_FT_MAX_STREAM_DATA:
144 {
145 const struct quic_max_stream_data *s = &frm->max_stream_data;
146 chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu",
147 (ull)s->id, (ull)s->max_stream_data);
148 break;
149 }
150 case QUIC_FT_MAX_STREAMS_BIDI:
151 {
152 const struct quic_max_streams *s = &frm->max_streams_bidi;
153 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
154 break;
155 }
156 case QUIC_FT_MAX_STREAMS_UNI:
157 {
158 const struct quic_max_streams *s = &frm->max_streams_uni;
159 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
160 break;
161 }
162 case QUIC_FT_DATA_BLOCKED:
163 {
164 const struct quic_data_blocked *s = &frm->data_blocked;
165 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
166 break;
167 }
168 case QUIC_FT_STREAM_DATA_BLOCKED:
169 {
170 const struct quic_stream_data_blocked *s = &frm->stream_data_blocked;
171 chunk_appendf(&trace_buf, " id=%llu limit=%llu",
172 (ull)s->id, (ull)s->limit);
173 break;
174 }
175 case QUIC_FT_STREAMS_BLOCKED_BIDI:
176 {
177 const struct quic_streams_blocked *s = &frm->streams_blocked_bidi;
178 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
179 break;
180 }
181 case QUIC_FT_STREAMS_BLOCKED_UNI:
182 {
183 const struct quic_streams_blocked *s = &frm->streams_blocked_uni;
184 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
185 break;
186 }
187 case QUIC_FT_RETIRE_CONNECTION_ID:
188 {
189 const struct quic_retire_connection_id *rci = &frm->retire_connection_id;
190 chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
191 break;
192 }
193 case QUIC_FT_CONNECTION_CLOSE:
194 {
195 const struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200196 size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100197 chunk_appendf(&trace_buf,
198 " error_code=%llu frame_type=%llu reason_phrase_len=%llu",
199 (ull)cc->error_code, (ull)cc->frame_type,
200 (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100201 if (plen)
202 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100203 break;
204 }
205 case QUIC_FT_CONNECTION_CLOSE_APP:
206 {
207 const struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200208 size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100209 chunk_appendf(&trace_buf,
210 " error_code=%llu reason_phrase_len=%llu",
211 (ull)cc->error_code, (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100212 if (plen)
213 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100214 break;
215 }
216 }
217}
218
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219/* Encode <frm> PADDING frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500220 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 */
222static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
223 struct quic_frame *frm, struct quic_conn *conn)
224{
225 struct quic_padding *padding = &frm->padding;
226
227 if (end - *buf < padding->len - 1)
228 return 0;
229
230 memset(*buf, 0, padding->len - 1);
231 *buf += padding->len - 1;
232
233 return 1;
234}
235
236/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
237 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
238 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100239static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100240 const unsigned char **buf, const unsigned char *end)
241{
242 const unsigned char *beg;
243 struct quic_padding *padding = &frm->padding;
244
245 beg = *buf;
246 padding->len = 1;
247 while (*buf < end && !**buf)
248 (*buf)++;
249 padding->len += *buf - beg;
250
251 return 1;
252}
253
254/* Encode a ACK frame into <buf> buffer.
255 * Always succeeds.
256 */
257static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
258 struct quic_frame *frm, struct quic_conn *conn)
259{
260 /* No field */
261 return 1;
262}
263
264/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
265 * Always succeeds.
266 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100267static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100268 const unsigned char **buf, const unsigned char *end)
269{
270 /* No field */
271 return 1;
272}
273
274/* Encode a ACK frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500275 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100276 */
277static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100278 struct quic_frame *frm, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100279{
280 struct quic_tx_ack *tx_ack = &frm->tx_ack;
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100281 struct eb64_node *ar, *prev_ar;
282 struct quic_arng_node *ar_node, *prev_ar_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100284 ar = eb64_last(&tx_ack->arngs->root);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200285 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200286 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100287 qc,, &ar_node->last, &ar_node->first.key);
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100288 if (!quic_enc_int(buf, end, ar_node->last) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289 !quic_enc_int(buf, end, tx_ack->ack_delay) ||
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100290 !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
291 !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292 return 0;
293
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100294 while ((prev_ar = eb64_prev(ar))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200295 prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200296 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,,
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100297 &prev_ar_node->last, &prev_ar_node->first.key);
298 if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
299 !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 return 0;
301
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100302 ar = prev_ar;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200303 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 }
305
306 return 1;
307}
308
309/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
310 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
311 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100312static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100313 const unsigned char **buf, const unsigned char *end)
314{
315 int ret;
316 struct quic_ack *ack = &frm->ack;
317
318 ret = quic_dec_int(&ack->largest_ack, buf, end);
319 if (!ret)
320 return 0;
321
322 ret = quic_dec_int(&ack->ack_delay, buf, end);
323 if (!ret)
324 return 0;
325
326 ret = quic_dec_int(&ack->ack_range_num, buf, end);
327 if (!ret)
328 return 0;
329
330 ret = quic_dec_int(&ack->first_ack_range, buf, end);
331 if (!ret)
332 return 0;
333
334 return 1;
335}
336
337/* Encode a ACK_ECN frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500338 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 */
340static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
341 struct quic_frame *frm, struct quic_conn *conn)
342{
343 struct quic_ack *ack = &frm->ack;
344
345 return quic_enc_int(buf, end, ack->largest_ack) &&
346 quic_enc_int(buf, end, ack->ack_delay) &&
347 quic_enc_int(buf, end, ack->first_ack_range) &&
348 quic_enc_int(buf, end, ack->ack_range_num);
349}
350
351/* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame.
352 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
353 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100354static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100355 const unsigned char **buf, const unsigned char *end)
356{
357 struct quic_ack *ack = &frm->ack;
358
359 return quic_dec_int(&ack->largest_ack, buf, end) &&
360 quic_dec_int(&ack->ack_delay, buf, end) &&
361 quic_dec_int(&ack->first_ack_range, buf, end) &&
362 quic_dec_int(&ack->ack_range_num, buf, end);
363}
364
365/* Encode a RESET_STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500366 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100367 */
368static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
369 struct quic_frame *frm, struct quic_conn *conn)
370{
371 struct quic_reset_stream *reset_stream = &frm->reset_stream;
372
373 return quic_enc_int(buf, end, reset_stream->id) &&
374 quic_enc_int(buf, end, reset_stream->app_error_code) &&
375 quic_enc_int(buf, end, reset_stream->final_size);
376}
377
378/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
379 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
380 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100381static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100382 const unsigned char **buf, const unsigned char *end)
383{
384 struct quic_reset_stream *reset_stream = &frm->reset_stream;
385
386 return quic_dec_int(&reset_stream->id, buf, end) &&
387 quic_dec_int(&reset_stream->app_error_code, buf, end) &&
388 quic_dec_int(&reset_stream->final_size, buf, end);
389}
390
391/* Encode a STOP_SENDING frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500392 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 */
394static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
395 struct quic_frame *frm, struct quic_conn *conn)
396{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100397 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100399 return quic_enc_int(buf, end, stop_sending->id) &&
400 quic_enc_int(buf, end, stop_sending->app_error_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401}
402
403/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
404 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
405 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100406static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100407 const unsigned char **buf, const unsigned char *end)
408{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100409 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100410
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100411 return quic_dec_int(&stop_sending->id, buf, end) &&
412 quic_dec_int(&stop_sending->app_error_code, buf, end);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413}
414
415/* Encode a CRYPTO frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500416 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 */
418static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
419 struct quic_frame *frm, struct quic_conn *conn)
420{
421 struct quic_crypto *crypto = &frm->crypto;
422 const struct quic_enc_level *qel = crypto->qel;
423 size_t offset, len;
424
425 if (!quic_enc_int(buf, end, crypto->offset) ||
426 !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
427 return 0;
428
429 len = crypto->len;
430 offset = crypto->offset;
431 while (len) {
432 int idx;
433 size_t to_copy;
434 const unsigned char *data;
435
436 idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
437 to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
438 if (to_copy > len)
439 to_copy = len;
440 data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
441 memcpy(*buf, data, to_copy);
442 *buf += to_copy;
443 offset += to_copy;
444 len -= to_copy;
445 }
446
447 return 1;
448}
449
450/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
451 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
452 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100453static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100454 const unsigned char **buf, const unsigned char *end)
455{
456 struct quic_crypto *crypto = &frm->crypto;
457
458 if (!quic_dec_int(&crypto->offset, buf, end) ||
459 !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
460 return 0;
461
462 crypto->data = *buf;
463 *buf += crypto->len;
464
465 return 1;
466}
467
468/* Encode a NEW_TOKEN frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500469 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470 */
471static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
472 struct quic_frame *frm, struct quic_conn *conn)
473{
474 struct quic_new_token *new_token = &frm->new_token;
475
476 if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
477 return 0;
478
479 memcpy(*buf, new_token->data, new_token->len);
480
481 return 1;
482}
483
484/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
485 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
486 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100487static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100488 const unsigned char **buf, const unsigned char *end)
489{
490 struct quic_new_token *new_token = &frm->new_token;
491
492 if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
493 return 0;
494
495 new_token->data = *buf;
496 *buf += new_token->len;
497
498 return 1;
499}
500
501/* Encode a STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500502 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 */
504static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
505 struct quic_frame *frm, struct quic_conn *conn)
506{
507 struct quic_stream *stream = &frm->stream;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100508 const unsigned char *wrap;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509
510 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200511 ((frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) && !quic_enc_int(buf, end, stream->offset.key)) ||
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100512 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100513 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
514 return 0;
515
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100516 wrap = (const unsigned char *)b_wrap(stream->buf);
517 if (stream->data + stream->len > wrap) {
518 size_t to_copy = wrap - stream->data;
519 memcpy(*buf, stream->data, to_copy);
520 *buf += to_copy;
521
522 to_copy = stream->len - to_copy;
523 memcpy(*buf, b_orig(stream->buf), to_copy);
524 *buf += to_copy;
525 }
526 else {
527 memcpy(*buf, stream->data, stream->len);
528 *buf += stream->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200529 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530
531 return 1;
532}
533
534/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
535 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
536 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100537static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100538 const unsigned char **buf, const unsigned char *end)
539{
540 struct quic_stream *stream = &frm->stream;
541
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100542 if (!quic_dec_int(&stream->id, buf, end))
543 return 0;
544
545 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100546 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200547 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100548 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200549 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100550 return 0;
551
552 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100553 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100554 stream->len = end - *buf;
555 }
556 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100557 return 0;
558
559 stream->data = *buf;
560 *buf += stream->len;
561
562 return 1;
563}
564
565/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500566 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100567 */
568static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
569 struct quic_frame *frm, struct quic_conn *conn)
570{
571 struct quic_max_data *max_data = &frm->max_data;
572
573 return quic_enc_int(buf, end, max_data->max_data);
574}
575
576/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
577 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
578 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100579static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100580 const unsigned char **buf, const unsigned char *end)
581{
582 struct quic_max_data *max_data = &frm->max_data;
583
584 return quic_dec_int(&max_data->max_data, buf, end);
585}
586
587/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500588 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589 */
590static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
591 struct quic_frame *frm, struct quic_conn *conn)
592{
593 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
594
595 return quic_enc_int(buf, end, max_stream_data->id) &&
596 quic_enc_int(buf, end, max_stream_data->max_stream_data);
597}
598
599/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
600 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
601 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100602static int quic_parse_max_stream_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603 const unsigned char **buf, const unsigned char *end)
604{
605 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
606
607 return quic_dec_int(&max_stream_data->id, buf, end) &&
608 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
609}
610
611/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500612 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 */
614static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
615 struct quic_frame *frm, struct quic_conn *conn)
616{
617 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
618
619 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
620}
621
622/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
623 * as end into <frm> frame.
624 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
625 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100626static int quic_parse_max_streams_bidi_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100627 const unsigned char **buf, const unsigned char *end)
628{
629 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
630
631 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
632}
633
634/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500635 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636 */
637static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
638 struct quic_frame *frm, struct quic_conn *conn)
639{
640 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
641
642 return quic_enc_int(buf, end, max_streams_uni->max_streams);
643}
644
645/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
646 * as end into <frm> frame.
647 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
648 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100649static int quic_parse_max_streams_uni_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650 const unsigned char **buf, const unsigned char *end)
651{
652 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
653
654 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
655}
656
657/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500658 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100659 */
660static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
661 struct quic_frame *frm, struct quic_conn *conn)
662{
663 struct quic_data_blocked *data_blocked = &frm->data_blocked;
664
665 return quic_enc_int(buf, end, data_blocked->limit);
666}
667
668/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
669 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
670 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100671static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100672 const unsigned char **buf, const unsigned char *end)
673{
674 struct quic_data_blocked *data_blocked = &frm->data_blocked;
675
676 return quic_dec_int(&data_blocked->limit, buf, end);
677}
678
679/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500680 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100681 */
682static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
683 struct quic_frame *frm, struct quic_conn *conn)
684{
685 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
686
687 return quic_enc_int(buf, end, stream_data_blocked->id) &&
688 quic_enc_int(buf, end, stream_data_blocked->limit);
689}
690
691/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
692 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
693 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100694static int quic_parse_stream_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100695 const unsigned char **buf, const unsigned char *end)
696{
697 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
698
699 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
700 quic_dec_int(&stream_data_blocked->limit, buf, end);
701}
702
703/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500704 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100705 */
706static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
707 struct quic_frame *frm, struct quic_conn *conn)
708{
709 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
710
711 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
712}
713
714/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
715 * as end into <frm> frame.
716 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
717 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100718static int quic_parse_streams_blocked_bidi_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100719 const unsigned char **buf, const unsigned char *end)
720{
721 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
722
723 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
724}
725
726/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500727 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100728 */
729static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
730 struct quic_frame *frm, struct quic_conn *conn)
731{
732 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
733
734 return quic_enc_int(buf, end, streams_blocked_uni->limit);
735}
736
737/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
738 * as end into <frm> frame.
739 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
740 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100741static int quic_parse_streams_blocked_uni_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100742 const unsigned char **buf, const unsigned char *end)
743{
744 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
745
746 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
747}
748
749/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500750 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100751 */
752static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
753 struct quic_frame *frm, struct quic_conn *conn)
754{
755 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
756
757 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
758 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
759 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
760 return 0;
761
762 *(*buf)++ = new_cid->cid.len;
763
764 if (new_cid->cid.len) {
765 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
766 *buf += new_cid->cid.len;
767 }
768 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
769 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
770
771 return 1;
772}
773
774/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
775 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
776 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100777static int quic_parse_new_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100778 const unsigned char **buf, const unsigned char *end)
779{
780 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
781
782 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
783 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
784 return 0;
785
786 new_cid->cid.len = *(*buf)++;
787 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
788 return 0;
789
790 if (new_cid->cid.len) {
791 new_cid->cid.data = *buf;
792 *buf += new_cid->cid.len;
793 }
794 new_cid->stateless_reset_token = *buf;
795 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
796
797 return 1;
798}
799
800/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500801 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100802 */
803static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
804 struct quic_frame *frm, struct quic_conn *conn)
805{
806 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
807
808 return quic_enc_int(buf, end, retire_connection_id->seq_num);
809}
810
811/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
812 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
813 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100814static int quic_parse_retire_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100815 const unsigned char **buf, const unsigned char *end)
816{
817 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
818
819 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
820}
821
822/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500823 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100824 */
825static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
826 struct quic_frame *frm, struct quic_conn *conn)
827{
828 struct quic_path_challenge *path_challenge = &frm->path_challenge;
829
830 if (end - *buf < sizeof path_challenge->data)
831 return 0;
832
833 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
834 *buf += sizeof path_challenge->data;
835
836 return 1;
837}
838
839/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
840 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
841 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100842static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100843 const unsigned char **buf, const unsigned char *end)
844{
845 struct quic_path_challenge *path_challenge = &frm->path_challenge;
846
847 if (end - *buf < sizeof path_challenge->data)
848 return 0;
849
850 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
851 *buf += sizeof path_challenge->data;
852
853 return 1;
854}
855
856
857/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500858 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 */
860static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
861 struct quic_frame *frm, struct quic_conn *conn)
862{
863 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
864
865 if (end - *buf < sizeof path_challenge_response->data)
866 return 0;
867
868 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
869 *buf += sizeof path_challenge_response->data;
870
871 return 1;
872}
873
874/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
875 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
876 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100877static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100878 const unsigned char **buf, const unsigned char *end)
879{
880 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
881
882 if (end - *buf < sizeof path_challenge_response->data)
883 return 0;
884
885 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
886 *buf += sizeof path_challenge_response->data;
887
888 return 1;
889}
890
891/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
892 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
893 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500894 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100895 */
896static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
897 struct quic_frame *frm, struct quic_conn *conn)
898{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100899 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100900
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100901 if (!quic_enc_int(buf, end, cc->error_code) ||
902 !quic_enc_int(buf, end, cc->frame_type) ||
903 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
904 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100905 return 0;
906
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100907 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
908 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100909
910 return 1;
911}
912
913/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
914 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
915 * and another at QUIC layer.
916 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
917 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100918static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 const unsigned char **buf, const unsigned char *end)
920{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100921 size_t plen;
922 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100924 if (!quic_dec_int(&cc->error_code, buf, end) ||
925 !quic_dec_int(&cc->frame_type, buf, end) ||
926 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
927 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100928 return 0;
929
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200930 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100931 memcpy(cc->reason_phrase, *buf, plen);
932 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933
934 return 1;
935}
936
937/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
938 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
939 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500940 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100941 */
942static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
943 struct quic_frame *frm, struct quic_conn *conn)
944{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100945 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100946
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100947 if (!quic_enc_int(buf, end, cc->error_code) ||
948 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
949 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100950 return 0;
951
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100952 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
953 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954
955 return 1;
956}
957
958/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
959 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
960 * and another at QUIC layer.
961 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
962 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100963static int quic_parse_connection_close_app_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964 const unsigned char **buf, const unsigned char *end)
965{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100966 size_t plen;
967 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100968
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100969 if (!quic_dec_int(&cc->error_code, buf, end) ||
970 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
971 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100972 return 0;
973
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200974 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100975 memcpy(cc->reason_phrase, *buf, plen);
976 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100977
978 return 1;
979}
980
981/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
982 * Always succeeds.
983 */
984static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
985 struct quic_frame *frm, struct quic_conn *conn)
986{
987 /* No field */
988 return 1;
989}
990
991/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
992 * Always succeed.
993 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100994static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995 const unsigned char **buf, const unsigned char *end)
996{
997 /* No field */
998 return 1;
999}
1000
1001struct quic_frame_builder {
1002 int (*func)(unsigned char **buf, const unsigned char *end,
1003 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001004 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001005 unsigned char flags;
1006};
1007
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001008const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001009 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1010 [QUIC_FT_PING] = { .func = quic_build_ping_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001011 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1012 [QUIC_FT_ACK_ECN] = { .func = quic_build_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001013 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1014 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1015 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1016 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1017 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1018 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1019 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1020 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1021 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1023 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1024 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1025 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1026 [QUIC_FT_MAX_STREAM_DATA] = { .func = quic_build_max_stream_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1027 [QUIC_FT_MAX_STREAMS_BIDI] = { .func = quic_build_max_streams_bidi_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1028 [QUIC_FT_MAX_STREAMS_UNI] = { .func = quic_build_max_streams_uni_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1029 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1030 [QUIC_FT_STREAM_DATA_BLOCKED] = { .func = quic_build_stream_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1031 [QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_build_streams_blocked_bidi_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1032 [QUIC_FT_STREAMS_BLOCKED_UNI] = { .func = quic_build_streams_blocked_uni_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1033 [QUIC_FT_NEW_CONNECTION_ID] = { .func = quic_build_new_connection_id_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1034 [QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_build_retire_connection_id_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1035 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1036 [QUIC_FT_PATH_RESPONSE] = { .func = quic_build_path_response_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001037 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1038 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_build_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001039 [QUIC_FT_HANDSHAKE_DONE] = { .func = quic_build_handshake_done_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001040};
1041
1042struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001043 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1044 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001045 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001046 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001047};
1048
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001049const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001050 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1051 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1052 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1053 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1054 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1055 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1056 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1057 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1058 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1059 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1060 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1061 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1062 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1064 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1065 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1066 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1067 [QUIC_FT_MAX_STREAM_DATA] = { .func = quic_parse_max_stream_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1068 [QUIC_FT_MAX_STREAMS_BIDI] = { .func = quic_parse_max_streams_bidi_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1069 [QUIC_FT_MAX_STREAMS_UNI] = { .func = quic_parse_max_streams_uni_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1070 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1071 [QUIC_FT_STREAM_DATA_BLOCKED] = { .func = quic_parse_stream_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1072 [QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_parse_streams_blocked_bidi_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1073 [QUIC_FT_STREAMS_BLOCKED_UNI] = { .func = quic_parse_streams_blocked_uni_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1074 [QUIC_FT_NEW_CONNECTION_ID] = { .func = quic_parse_new_connection_id_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1075 [QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_parse_retire_connection_id_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1076 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1077 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1078 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1079 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1080 [QUIC_FT_HANDSHAKE_DONE] = { .func = quic_parse_handshake_done_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081};
1082
1083/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001084 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 */
1086int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1087 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001088 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001090 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001091 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001092
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001093 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001095 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001096 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001097 }
1098
1099 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001100 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001101 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001102 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001103 }
1104
1105 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001106 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001107 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001108 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109 }
1110
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001111 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1112 if (!parser->func(frm, qc, buf, end)) {
1113 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001114 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001115 }
1116
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001117 pkt->flags |= parser->flags;
1118
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001119 ret = 1;
1120 leave:
1121 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1122 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001123}
1124
1125/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001126 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001127 * The buffer is updated to point to one byte past the end of the built frame
1128 * only if succeeded.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001129 */
1130int qc_build_frm(unsigned char **buf, const unsigned char *end,
1131 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001132 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001133{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001134 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001135 const struct quic_frame_builder *builder;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001136 unsigned char *pos = *buf;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001137
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001138 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001139 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001140 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001141 /* XXX This it a bug to send an unauthorized frame with such a packet type XXX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001142 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001143 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001144 }
1145
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001146 if (end <= pos) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001147 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001148 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001149 }
1150
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001151 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001152 *pos++ = frm->type;
1153 if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001154 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001155 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001156 }
1157
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001158 pkt->flags |= builder->flags;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001159 *buf = pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001160
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001161 ret = 1;
1162 leave:
1163 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1164 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001165}
1166