blob: f98f5944ed06025db01a9a7eb664469003515f25 [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
Amaury Denoyelledb443382021-11-30 11:23:29 +0100559 stream->fin = (frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT);
560
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100561 stream->data = *buf;
562 *buf += stream->len;
563
564 return 1;
565}
566
567/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500568 * 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 +0100569 */
570static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
571 struct quic_frame *frm, struct quic_conn *conn)
572{
573 struct quic_max_data *max_data = &frm->max_data;
574
575 return quic_enc_int(buf, end, max_data->max_data);
576}
577
578/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
579 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
580 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100581static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100582 const unsigned char **buf, const unsigned char *end)
583{
584 struct quic_max_data *max_data = &frm->max_data;
585
586 return quic_dec_int(&max_data->max_data, buf, end);
587}
588
589/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500590 * 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 +0100591 */
592static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
593 struct quic_frame *frm, struct quic_conn *conn)
594{
595 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
596
597 return quic_enc_int(buf, end, max_stream_data->id) &&
598 quic_enc_int(buf, end, max_stream_data->max_stream_data);
599}
600
601/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
602 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
603 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100604static 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 +0100605 const unsigned char **buf, const unsigned char *end)
606{
607 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
608
609 return quic_dec_int(&max_stream_data->id, buf, end) &&
610 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
611}
612
613/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500614 * 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 +0100615 */
616static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
617 struct quic_frame *frm, struct quic_conn *conn)
618{
619 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
620
621 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
622}
623
624/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
625 * as end into <frm> frame.
626 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
627 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100628static 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 +0100629 const unsigned char **buf, const unsigned char *end)
630{
631 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
632
633 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
634}
635
636/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500637 * 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 +0100638 */
639static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
640 struct quic_frame *frm, struct quic_conn *conn)
641{
642 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
643
644 return quic_enc_int(buf, end, max_streams_uni->max_streams);
645}
646
647/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
648 * as end into <frm> frame.
649 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
650 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100651static 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 +0100652 const unsigned char **buf, const unsigned char *end)
653{
654 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
655
656 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
657}
658
659/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500660 * 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 +0100661 */
662static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
663 struct quic_frame *frm, struct quic_conn *conn)
664{
665 struct quic_data_blocked *data_blocked = &frm->data_blocked;
666
667 return quic_enc_int(buf, end, data_blocked->limit);
668}
669
670/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
671 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
672 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100673static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100674 const unsigned char **buf, const unsigned char *end)
675{
676 struct quic_data_blocked *data_blocked = &frm->data_blocked;
677
678 return quic_dec_int(&data_blocked->limit, buf, end);
679}
680
681/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500682 * 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 +0100683 */
684static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
685 struct quic_frame *frm, struct quic_conn *conn)
686{
687 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
688
689 return quic_enc_int(buf, end, stream_data_blocked->id) &&
690 quic_enc_int(buf, end, stream_data_blocked->limit);
691}
692
693/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
694 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
695 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100696static 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 +0100697 const unsigned char **buf, const unsigned char *end)
698{
699 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
700
701 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
702 quic_dec_int(&stream_data_blocked->limit, buf, end);
703}
704
705/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500706 * 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 +0100707 */
708static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
709 struct quic_frame *frm, struct quic_conn *conn)
710{
711 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
712
713 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
714}
715
716/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
717 * as end into <frm> frame.
718 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
719 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100720static 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 +0100721 const unsigned char **buf, const unsigned char *end)
722{
723 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
724
725 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
726}
727
728/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500729 * 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 +0100730 */
731static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
732 struct quic_frame *frm, struct quic_conn *conn)
733{
734 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
735
736 return quic_enc_int(buf, end, streams_blocked_uni->limit);
737}
738
739/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
740 * as end into <frm> frame.
741 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
742 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100743static 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 +0100744 const unsigned char **buf, const unsigned char *end)
745{
746 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
747
748 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
749}
750
751/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500752 * 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 +0100753 */
754static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
755 struct quic_frame *frm, struct quic_conn *conn)
756{
757 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
758
759 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
760 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
761 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
762 return 0;
763
764 *(*buf)++ = new_cid->cid.len;
765
766 if (new_cid->cid.len) {
767 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
768 *buf += new_cid->cid.len;
769 }
770 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
771 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
772
773 return 1;
774}
775
776/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
777 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
778 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100779static 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 +0100780 const unsigned char **buf, const unsigned char *end)
781{
782 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
783
784 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
785 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
786 return 0;
787
788 new_cid->cid.len = *(*buf)++;
789 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
790 return 0;
791
792 if (new_cid->cid.len) {
793 new_cid->cid.data = *buf;
794 *buf += new_cid->cid.len;
795 }
796 new_cid->stateless_reset_token = *buf;
797 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
798
799 return 1;
800}
801
802/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500803 * 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 +0100804 */
805static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
806 struct quic_frame *frm, struct quic_conn *conn)
807{
808 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
809
810 return quic_enc_int(buf, end, retire_connection_id->seq_num);
811}
812
813/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
814 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
815 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100816static 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 +0100817 const unsigned char **buf, const unsigned char *end)
818{
819 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
820
821 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
822}
823
824/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500825 * 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 +0100826 */
827static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
828 struct quic_frame *frm, struct quic_conn *conn)
829{
830 struct quic_path_challenge *path_challenge = &frm->path_challenge;
831
832 if (end - *buf < sizeof path_challenge->data)
833 return 0;
834
835 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
836 *buf += sizeof path_challenge->data;
837
838 return 1;
839}
840
841/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
842 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
843 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100844static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100845 const unsigned char **buf, const unsigned char *end)
846{
847 struct quic_path_challenge *path_challenge = &frm->path_challenge;
848
849 if (end - *buf < sizeof path_challenge->data)
850 return 0;
851
852 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
853 *buf += sizeof path_challenge->data;
854
855 return 1;
856}
857
858
859/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500860 * 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 +0100861 */
862static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
863 struct quic_frame *frm, struct quic_conn *conn)
864{
865 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
866
867 if (end - *buf < sizeof path_challenge_response->data)
868 return 0;
869
870 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
871 *buf += sizeof path_challenge_response->data;
872
873 return 1;
874}
875
876/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
877 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
878 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100879static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100880 const unsigned char **buf, const unsigned char *end)
881{
882 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
883
884 if (end - *buf < sizeof path_challenge_response->data)
885 return 0;
886
887 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
888 *buf += sizeof path_challenge_response->data;
889
890 return 1;
891}
892
893/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
894 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
895 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500896 * 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 +0100897 */
898static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
899 struct quic_frame *frm, struct quic_conn *conn)
900{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100901 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100902
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100903 if (!quic_enc_int(buf, end, cc->error_code) ||
904 !quic_enc_int(buf, end, cc->frame_type) ||
905 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
906 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100907 return 0;
908
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100909 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
910 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100911
912 return 1;
913}
914
915/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
916 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
917 * and another at QUIC layer.
918 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
919 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100920static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921 const unsigned char **buf, const unsigned char *end)
922{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100923 size_t plen;
924 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100926 if (!quic_dec_int(&cc->error_code, buf, end) ||
927 !quic_dec_int(&cc->frame_type, buf, end) ||
928 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
929 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 return 0;
931
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200932 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100933 memcpy(cc->reason_phrase, *buf, plen);
934 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935
936 return 1;
937}
938
939/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
940 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
941 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500942 * 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 +0100943 */
944static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
945 struct quic_frame *frm, struct quic_conn *conn)
946{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100947 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100948
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100949 if (!quic_enc_int(buf, end, cc->error_code) ||
950 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
951 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100952 return 0;
953
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100954 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
955 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956
957 return 1;
958}
959
960/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
961 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
962 * and another at QUIC layer.
963 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
964 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100965static 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 +0100966 const unsigned char **buf, const unsigned char *end)
967{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100968 size_t plen;
969 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100970
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100971 if (!quic_dec_int(&cc->error_code, buf, end) ||
972 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
973 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100974 return 0;
975
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200976 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100977 memcpy(cc->reason_phrase, *buf, plen);
978 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100979
980 return 1;
981}
982
983/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
984 * Always succeeds.
985 */
986static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
987 struct quic_frame *frm, struct quic_conn *conn)
988{
989 /* No field */
990 return 1;
991}
992
993/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
994 * Always succeed.
995 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100996static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100997 const unsigned char **buf, const unsigned char *end)
998{
999 /* No field */
1000 return 1;
1001}
1002
1003struct quic_frame_builder {
1004 int (*func)(unsigned char **buf, const unsigned char *end,
1005 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001006 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001007 unsigned char flags;
1008};
1009
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001010const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001011 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1012 [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 +02001013 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1014 [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 +02001015 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1016 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1017 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1018 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1019 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1020 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1021 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1023 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1024 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1025 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1026 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1027 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1028 [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, },
1029 [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, },
1030 [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, },
1031 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1032 [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, },
1033 [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, },
1034 [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, },
1035 [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, },
1036 [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, },
1037 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1038 [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 +02001039 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1040 [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 +02001041 [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 +01001042};
1043
1044struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001045 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1046 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001047 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001048 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001049};
1050
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001051const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001052 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1053 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1054 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1055 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1056 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1057 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1058 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1059 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1060 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1061 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1062 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1064 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1065 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1066 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1067 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1068 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1069 [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, },
1070 [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, },
1071 [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, },
1072 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1073 [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, },
1074 [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, },
1075 [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, },
1076 [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, },
1077 [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, },
1078 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1079 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1080 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1081 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1082 [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 +01001083};
1084
1085/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001086 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087 */
1088int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1089 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001090 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001091{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001092 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001093 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001095 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001097 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001098 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 }
1100
1101 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001102 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001103 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001104 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 }
1106
1107 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001108 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001109 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001110 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111 }
1112
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001113 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1114 if (!parser->func(frm, qc, buf, end)) {
1115 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001116 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117 }
1118
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001119 pkt->flags |= parser->flags;
1120
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001121 ret = 1;
1122 leave:
1123 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1124 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125}
1126
1127/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001128 * 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 +02001129 * The buffer is updated to point to one byte past the end of the built frame
1130 * only if succeeded.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001131 */
1132int qc_build_frm(unsigned char **buf, const unsigned char *end,
1133 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001134 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001135{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001136 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001137 const struct quic_frame_builder *builder;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001138 unsigned char *pos = *buf;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001139
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001140 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001141 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001142 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001143 /* 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 +02001144 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001145 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001146 }
1147
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001148 if (end <= pos) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001149 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001150 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001151 }
1152
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001153 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001154 *pos++ = frm->type;
1155 if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001156 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001157 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001158 }
1159
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001160 pkt->flags |= builder->flags;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001161 *buf = pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001162
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001163 ret = 1;
1164 leave:
1165 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1166 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001167}
1168