blob: f590be2922746f72101e33e8668d8adf51a45555 [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
Frédéric Lécaille8090b512020-11-30 16:19:22 +010010#include <import/eb64tree.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010011#include <haproxy/quic_frame.h>
12#include <haproxy/trace.h>
13#include <haproxy/xprt_quic.h>
14
15#define TRACE_SOURCE &trace_quic
16
17const char *quic_frame_type_string(enum quic_frame_type ft)
18{
19 switch (ft) {
20 case QUIC_FT_PADDING:
21 return "PADDING";
22 case QUIC_FT_PING:
23 return "PING";
24 case QUIC_FT_ACK:
25 return "ACK";
26 case QUIC_FT_ACK_ECN:
27 return "ACK_ENC";
28 case QUIC_FT_RESET_STREAM:
29 return "RESET_STREAM";
30 case QUIC_FT_STOP_SENDING:
31 return "STOP_SENDING";
32 case QUIC_FT_CRYPTO:
33 return "CRYPTO";
34 case QUIC_FT_NEW_TOKEN:
35 return "NEW_TOKEN";
36
37 case QUIC_FT_STREAM_8:
38 return "STREAM_8";
39 case QUIC_FT_STREAM_9:
40 return "STREAM_9";
41 case QUIC_FT_STREAM_A:
42 return "STREAM_A";
43 case QUIC_FT_STREAM_B:
44 return "STREAM_B";
45 case QUIC_FT_STREAM_C:
46 return "STREAM_C";
47 case QUIC_FT_STREAM_D:
48 return "STREAM_D";
49 case QUIC_FT_STREAM_E:
50 return "STREAM_E";
51 case QUIC_FT_STREAM_F:
52 return "STREAM_F";
53
54 case QUIC_FT_MAX_DATA:
55 return "MAX_DATA";
56 case QUIC_FT_MAX_STREAM_DATA:
57 return "MAX_STREAM_DATA";
58 case QUIC_FT_MAX_STREAMS_BIDI:
59 return "MAX_STREAMS_BIDI";
60 case QUIC_FT_MAX_STREAMS_UNI:
61 return "MAX_STREAMS_UNI";
62 case QUIC_FT_DATA_BLOCKED:
63 return "DATA_BLOCKED";
64 case QUIC_FT_STREAM_DATA_BLOCKED:
65 return "STREAM_DATA_BLOCKED";
66 case QUIC_FT_STREAMS_BLOCKED_BIDI:
67 return "STREAMS_BLOCKED_BIDI";
68 case QUIC_FT_STREAMS_BLOCKED_UNI:
69 return "STREAMS_BLOCKED_UNI";
70 case QUIC_FT_NEW_CONNECTION_ID:
71 return "NEW_CONNECTION_ID";
72 case QUIC_FT_RETIRE_CONNECTION_ID:
73 return "RETIRE_CONNECTION_ID";
74 case QUIC_FT_PATH_CHALLENGE:
75 return "PATH_CHALLENGE";
76 case QUIC_FT_PATH_RESPONSE:
77 return "PATH_RESPONSE";
78 case QUIC_FT_CONNECTION_CLOSE:
79 return "CONNECTION_CLOSE";
80 case QUIC_FT_CONNECTION_CLOSE_APP:
81 return "CONNECTION_CLOSE_APP";
82 case QUIC_FT_HANDSHAKE_DONE:
83 return "HANDSHAKE_DONE";
84 default:
85 return "UNKNOWN";
86 }
87}
88
Frédéric Lécaille010e5322021-12-23 15:19:15 +010089static void chunk_cc_phrase_appendf(struct buffer *buf,
90 const unsigned char *phr, size_t phrlen)
91{
92 chunk_appendf(buf, " reason_phrase: '");
93 while (phrlen--)
94 chunk_appendf(buf, "%c", *phr++);
95 chunk_appendf(buf, "'");
96}
97
Frédéric Lécaille1ede8232021-12-23 14:11:25 +010098/* Add traces to <buf> depending on <frm> frame type. */
99void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm)
100{
101 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
102 switch (frm->type) {
103 case QUIC_FT_CRYPTO:
104 {
105 const struct quic_crypto *cf = &frm->crypto;
106 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
107 (ull)cf->offset, (ull)cf->len);
108 break;
109 }
110 case QUIC_FT_RESET_STREAM:
111 {
112 const struct quic_reset_stream *rs = &frm->reset_stream;
113 chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu",
114 (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size);
115 break;
116 }
117 case QUIC_FT_STOP_SENDING:
118 {
119 const struct quic_stop_sending *s = &frm->stop_sending;
120 chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
121 (ull)s->id, (ull)s->app_error_code);
122 break;
123 }
124 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
125 {
126 const struct quic_stream *s = &frm->stream;
127 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
128 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
129 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
130 (ull)s->id, (ull)s->offset.key, (ull)s->len);
131 break;
132 }
133 case QUIC_FT_MAX_DATA:
134 {
135 const struct quic_max_data *s = &frm->max_data;
136 chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
137 break;
138 }
139 case QUIC_FT_MAX_STREAM_DATA:
140 {
141 const struct quic_max_stream_data *s = &frm->max_stream_data;
142 chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu",
143 (ull)s->id, (ull)s->max_stream_data);
144 break;
145 }
146 case QUIC_FT_MAX_STREAMS_BIDI:
147 {
148 const struct quic_max_streams *s = &frm->max_streams_bidi;
149 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
150 break;
151 }
152 case QUIC_FT_MAX_STREAMS_UNI:
153 {
154 const struct quic_max_streams *s = &frm->max_streams_uni;
155 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
156 break;
157 }
158 case QUIC_FT_DATA_BLOCKED:
159 {
160 const struct quic_data_blocked *s = &frm->data_blocked;
161 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
162 break;
163 }
164 case QUIC_FT_STREAM_DATA_BLOCKED:
165 {
166 const struct quic_stream_data_blocked *s = &frm->stream_data_blocked;
167 chunk_appendf(&trace_buf, " id=%llu limit=%llu",
168 (ull)s->id, (ull)s->limit);
169 break;
170 }
171 case QUIC_FT_STREAMS_BLOCKED_BIDI:
172 {
173 const struct quic_streams_blocked *s = &frm->streams_blocked_bidi;
174 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
175 break;
176 }
177 case QUIC_FT_STREAMS_BLOCKED_UNI:
178 {
179 const struct quic_streams_blocked *s = &frm->streams_blocked_uni;
180 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
181 break;
182 }
183 case QUIC_FT_RETIRE_CONNECTION_ID:
184 {
185 const struct quic_retire_connection_id *rci = &frm->retire_connection_id;
186 chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
187 break;
188 }
189 case QUIC_FT_CONNECTION_CLOSE:
190 {
191 const struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200192 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 +0100193 chunk_appendf(&trace_buf,
194 " error_code=%llu frame_type=%llu reason_phrase_len=%llu",
195 (ull)cc->error_code, (ull)cc->frame_type,
196 (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100197 if (plen)
198 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100199 break;
200 }
201 case QUIC_FT_CONNECTION_CLOSE_APP:
202 {
203 const struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200204 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 +0100205 chunk_appendf(&trace_buf,
206 " error_code=%llu reason_phrase_len=%llu",
207 (ull)cc->error_code, (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100208 if (plen)
209 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100210 break;
211 }
212 }
213}
214
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215/* Encode <frm> PADDING frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500216 * 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 +0100217 */
218static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
219 struct quic_frame *frm, struct quic_conn *conn)
220{
221 struct quic_padding *padding = &frm->padding;
222
223 if (end - *buf < padding->len - 1)
224 return 0;
225
226 memset(*buf, 0, padding->len - 1);
227 *buf += padding->len - 1;
228
229 return 1;
230}
231
232/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
233 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
234 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100235static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100236 const unsigned char **buf, const unsigned char *end)
237{
238 const unsigned char *beg;
239 struct quic_padding *padding = &frm->padding;
240
241 beg = *buf;
242 padding->len = 1;
243 while (*buf < end && !**buf)
244 (*buf)++;
245 padding->len += *buf - beg;
246
247 return 1;
248}
249
250/* Encode a ACK frame into <buf> buffer.
251 * Always succeeds.
252 */
253static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
254 struct quic_frame *frm, struct quic_conn *conn)
255{
256 /* No field */
257 return 1;
258}
259
260/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
261 * Always succeeds.
262 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100263static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100264 const unsigned char **buf, const unsigned char *end)
265{
266 /* No field */
267 return 1;
268}
269
270/* Encode a ACK frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500271 * 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 +0100272 */
273static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100274 struct quic_frame *frm, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100275{
276 struct quic_tx_ack *tx_ack = &frm->tx_ack;
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100277 struct eb64_node *ar, *prev_ar;
278 struct quic_arng_node *ar_node, *prev_ar_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100279
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100280 ar = eb64_last(&tx_ack->arngs->root);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200281 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200282 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100283 qc,, &ar_node->last, &ar_node->first.key);
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100284 if (!quic_enc_int(buf, end, ar_node->last) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 !quic_enc_int(buf, end, tx_ack->ack_delay) ||
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100286 !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
287 !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288 return 0;
289
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100290 while ((prev_ar = eb64_prev(ar))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200291 prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200292 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,,
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100293 &prev_ar_node->last, &prev_ar_node->first.key);
294 if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
295 !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 return 0;
297
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100298 ar = prev_ar;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200299 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 }
301
302 return 1;
303}
304
305/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
306 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
307 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100308static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100309 const unsigned char **buf, const unsigned char *end)
310{
311 int ret;
312 struct quic_ack *ack = &frm->ack;
313
314 ret = quic_dec_int(&ack->largest_ack, buf, end);
315 if (!ret)
316 return 0;
317
318 ret = quic_dec_int(&ack->ack_delay, buf, end);
319 if (!ret)
320 return 0;
321
322 ret = quic_dec_int(&ack->ack_range_num, buf, end);
323 if (!ret)
324 return 0;
325
326 ret = quic_dec_int(&ack->first_ack_range, buf, end);
327 if (!ret)
328 return 0;
329
330 return 1;
331}
332
333/* Encode a ACK_ECN frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500334 * 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 +0100335 */
336static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
337 struct quic_frame *frm, struct quic_conn *conn)
338{
339 struct quic_ack *ack = &frm->ack;
340
341 return quic_enc_int(buf, end, ack->largest_ack) &&
342 quic_enc_int(buf, end, ack->ack_delay) &&
343 quic_enc_int(buf, end, ack->first_ack_range) &&
344 quic_enc_int(buf, end, ack->ack_range_num);
345}
346
347/* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame.
348 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
349 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100350static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100351 const unsigned char **buf, const unsigned char *end)
352{
353 struct quic_ack *ack = &frm->ack;
354
355 return quic_dec_int(&ack->largest_ack, buf, end) &&
356 quic_dec_int(&ack->ack_delay, buf, end) &&
357 quic_dec_int(&ack->first_ack_range, buf, end) &&
358 quic_dec_int(&ack->ack_range_num, buf, end);
359}
360
361/* Encode a RESET_STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500362 * 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 +0100363 */
364static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
365 struct quic_frame *frm, struct quic_conn *conn)
366{
367 struct quic_reset_stream *reset_stream = &frm->reset_stream;
368
369 return quic_enc_int(buf, end, reset_stream->id) &&
370 quic_enc_int(buf, end, reset_stream->app_error_code) &&
371 quic_enc_int(buf, end, reset_stream->final_size);
372}
373
374/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
375 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
376 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100377static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100378 const unsigned char **buf, const unsigned char *end)
379{
380 struct quic_reset_stream *reset_stream = &frm->reset_stream;
381
382 return quic_dec_int(&reset_stream->id, buf, end) &&
383 quic_dec_int(&reset_stream->app_error_code, buf, end) &&
384 quic_dec_int(&reset_stream->final_size, buf, end);
385}
386
387/* Encode a STOP_SENDING frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500388 * 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 +0100389 */
390static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
391 struct quic_frame *frm, struct quic_conn *conn)
392{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100393 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100395 return quic_enc_int(buf, end, stop_sending->id) &&
396 quic_enc_int(buf, end, stop_sending->app_error_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397}
398
399/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
400 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
401 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100402static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100403 const unsigned char **buf, const unsigned char *end)
404{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100405 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100406
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100407 return quic_dec_int(&stop_sending->id, buf, end) &&
408 quic_dec_int(&stop_sending->app_error_code, buf, end);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100409}
410
411/* Encode a CRYPTO frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500412 * 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 +0100413 */
414static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
415 struct quic_frame *frm, struct quic_conn *conn)
416{
417 struct quic_crypto *crypto = &frm->crypto;
418 const struct quic_enc_level *qel = crypto->qel;
419 size_t offset, len;
420
421 if (!quic_enc_int(buf, end, crypto->offset) ||
422 !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
423 return 0;
424
425 len = crypto->len;
426 offset = crypto->offset;
427 while (len) {
428 int idx;
429 size_t to_copy;
430 const unsigned char *data;
431
432 idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
433 to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
434 if (to_copy > len)
435 to_copy = len;
436 data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
437 memcpy(*buf, data, to_copy);
438 *buf += to_copy;
439 offset += to_copy;
440 len -= to_copy;
441 }
442
443 return 1;
444}
445
446/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
447 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
448 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100449static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100450 const unsigned char **buf, const unsigned char *end)
451{
452 struct quic_crypto *crypto = &frm->crypto;
453
454 if (!quic_dec_int(&crypto->offset, buf, end) ||
455 !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
456 return 0;
457
458 crypto->data = *buf;
459 *buf += crypto->len;
460
461 return 1;
462}
463
464/* Encode a NEW_TOKEN frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500465 * 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 +0100466 */
467static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
468 struct quic_frame *frm, struct quic_conn *conn)
469{
470 struct quic_new_token *new_token = &frm->new_token;
471
472 if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
473 return 0;
474
475 memcpy(*buf, new_token->data, new_token->len);
476
477 return 1;
478}
479
480/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
481 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
482 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100483static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100484 const unsigned char **buf, const unsigned char *end)
485{
486 struct quic_new_token *new_token = &frm->new_token;
487
488 if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
489 return 0;
490
491 new_token->data = *buf;
492 *buf += new_token->len;
493
494 return 1;
495}
496
497/* Encode a STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500498 * 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 +0100499 */
500static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
501 struct quic_frame *frm, struct quic_conn *conn)
502{
503 struct quic_stream *stream = &frm->stream;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100504 const unsigned char *wrap;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100505
506 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200507 ((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 +0100508 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
510 return 0;
511
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100512 wrap = (const unsigned char *)b_wrap(stream->buf);
513 if (stream->data + stream->len > wrap) {
514 size_t to_copy = wrap - stream->data;
515 memcpy(*buf, stream->data, to_copy);
516 *buf += to_copy;
517
518 to_copy = stream->len - to_copy;
519 memcpy(*buf, b_orig(stream->buf), to_copy);
520 *buf += to_copy;
521 }
522 else {
523 memcpy(*buf, stream->data, stream->len);
524 *buf += stream->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200525 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526
527 return 1;
528}
529
530/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
531 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
532 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100533static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 const unsigned char **buf, const unsigned char *end)
535{
536 struct quic_stream *stream = &frm->stream;
537
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100538 if (!quic_dec_int(&stream->id, buf, end))
539 return 0;
540
541 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100542 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200543 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100544 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200545 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100546 return 0;
547
548 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100549 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100550 stream->len = end - *buf;
551 }
552 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100553 return 0;
554
Amaury Denoyelledb443382021-11-30 11:23:29 +0100555 stream->fin = (frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT);
556
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100557 stream->data = *buf;
558 *buf += stream->len;
559
560 return 1;
561}
562
563/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500564 * 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 +0100565 */
566static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
567 struct quic_frame *frm, struct quic_conn *conn)
568{
569 struct quic_max_data *max_data = &frm->max_data;
570
571 return quic_enc_int(buf, end, max_data->max_data);
572}
573
574/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
575 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
576 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100577static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 const unsigned char **buf, const unsigned char *end)
579{
580 struct quic_max_data *max_data = &frm->max_data;
581
582 return quic_dec_int(&max_data->max_data, buf, end);
583}
584
585/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500586 * 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 +0100587 */
588static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
589 struct quic_frame *frm, struct quic_conn *conn)
590{
591 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
592
593 return quic_enc_int(buf, end, max_stream_data->id) &&
594 quic_enc_int(buf, end, max_stream_data->max_stream_data);
595}
596
597/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
598 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
599 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100600static 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 +0100601 const unsigned char **buf, const unsigned char *end)
602{
603 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
604
605 return quic_dec_int(&max_stream_data->id, buf, end) &&
606 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
607}
608
609/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500610 * 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 +0100611 */
612static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
613 struct quic_frame *frm, struct quic_conn *conn)
614{
615 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
616
617 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
618}
619
620/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
621 * as end into <frm> frame.
622 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
623 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100624static 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 +0100625 const unsigned char **buf, const unsigned char *end)
626{
627 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
628
629 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
630}
631
632/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500633 * 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 +0100634 */
635static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
636 struct quic_frame *frm, struct quic_conn *conn)
637{
638 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
639
640 return quic_enc_int(buf, end, max_streams_uni->max_streams);
641}
642
643/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
644 * as end into <frm> frame.
645 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
646 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100647static 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 +0100648 const unsigned char **buf, const unsigned char *end)
649{
650 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
651
652 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
653}
654
655/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500656 * 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 +0100657 */
658static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
659 struct quic_frame *frm, struct quic_conn *conn)
660{
661 struct quic_data_blocked *data_blocked = &frm->data_blocked;
662
663 return quic_enc_int(buf, end, data_blocked->limit);
664}
665
666/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
667 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
668 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100669static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100670 const unsigned char **buf, const unsigned char *end)
671{
672 struct quic_data_blocked *data_blocked = &frm->data_blocked;
673
674 return quic_dec_int(&data_blocked->limit, buf, end);
675}
676
677/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500678 * 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 +0100679 */
680static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
681 struct quic_frame *frm, struct quic_conn *conn)
682{
683 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
684
685 return quic_enc_int(buf, end, stream_data_blocked->id) &&
686 quic_enc_int(buf, end, stream_data_blocked->limit);
687}
688
689/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
690 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
691 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100692static 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 +0100693 const unsigned char **buf, const unsigned char *end)
694{
695 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
696
697 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
698 quic_dec_int(&stream_data_blocked->limit, buf, end);
699}
700
701/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500702 * 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 +0100703 */
704static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
705 struct quic_frame *frm, struct quic_conn *conn)
706{
707 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
708
709 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
710}
711
712/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
713 * as end into <frm> frame.
714 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
715 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100716static 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 +0100717 const unsigned char **buf, const unsigned char *end)
718{
719 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
720
721 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
722}
723
724/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500725 * 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 +0100726 */
727static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
728 struct quic_frame *frm, struct quic_conn *conn)
729{
730 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
731
732 return quic_enc_int(buf, end, streams_blocked_uni->limit);
733}
734
735/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
736 * as end into <frm> frame.
737 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
738 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100739static 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 +0100740 const unsigned char **buf, const unsigned char *end)
741{
742 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
743
744 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
745}
746
747/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500748 * 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 +0100749 */
750static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
751 struct quic_frame *frm, struct quic_conn *conn)
752{
753 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
754
755 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
756 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
757 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
758 return 0;
759
760 *(*buf)++ = new_cid->cid.len;
761
762 if (new_cid->cid.len) {
763 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
764 *buf += new_cid->cid.len;
765 }
766 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
767 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
768
769 return 1;
770}
771
772/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
773 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
774 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100775static 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 +0100776 const unsigned char **buf, const unsigned char *end)
777{
778 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
779
780 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
781 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
782 return 0;
783
784 new_cid->cid.len = *(*buf)++;
785 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
786 return 0;
787
788 if (new_cid->cid.len) {
789 new_cid->cid.data = *buf;
790 *buf += new_cid->cid.len;
791 }
792 new_cid->stateless_reset_token = *buf;
793 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
794
795 return 1;
796}
797
798/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500799 * 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 +0100800 */
801static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
802 struct quic_frame *frm, struct quic_conn *conn)
803{
804 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
805
806 return quic_enc_int(buf, end, retire_connection_id->seq_num);
807}
808
809/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
810 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
811 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100812static 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 +0100813 const unsigned char **buf, const unsigned char *end)
814{
815 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
816
817 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
818}
819
820/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500821 * 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 +0100822 */
823static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
824 struct quic_frame *frm, struct quic_conn *conn)
825{
826 struct quic_path_challenge *path_challenge = &frm->path_challenge;
827
828 if (end - *buf < sizeof path_challenge->data)
829 return 0;
830
831 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
832 *buf += sizeof path_challenge->data;
833
834 return 1;
835}
836
837/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
838 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
839 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100840static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841 const unsigned char **buf, const unsigned char *end)
842{
843 struct quic_path_challenge *path_challenge = &frm->path_challenge;
844
845 if (end - *buf < sizeof path_challenge->data)
846 return 0;
847
848 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
849 *buf += sizeof path_challenge->data;
850
851 return 1;
852}
853
854
855/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500856 * 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 +0100857 */
858static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
859 struct quic_frame *frm, struct quic_conn *conn)
860{
861 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
862
863 if (end - *buf < sizeof path_challenge_response->data)
864 return 0;
865
866 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
867 *buf += sizeof path_challenge_response->data;
868
869 return 1;
870}
871
872/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
873 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
874 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100875static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100876 const unsigned char **buf, const unsigned char *end)
877{
878 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
879
880 if (end - *buf < sizeof path_challenge_response->data)
881 return 0;
882
883 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
884 *buf += sizeof path_challenge_response->data;
885
886 return 1;
887}
888
889/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
890 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
891 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500892 * 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 +0100893 */
894static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
895 struct quic_frame *frm, struct quic_conn *conn)
896{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100897 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100899 if (!quic_enc_int(buf, end, cc->error_code) ||
900 !quic_enc_int(buf, end, cc->frame_type) ||
901 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
902 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903 return 0;
904
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100905 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
906 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100907
908 return 1;
909}
910
911/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
912 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
913 * and another at QUIC layer.
914 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
915 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100916static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 const unsigned char **buf, const unsigned char *end)
918{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100919 size_t plen;
920 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100922 if (!quic_dec_int(&cc->error_code, buf, end) ||
923 !quic_dec_int(&cc->frame_type, buf, end) ||
924 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
925 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926 return 0;
927
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200928 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100929 memcpy(cc->reason_phrase, *buf, plen);
930 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931
932 return 1;
933}
934
935/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
936 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
937 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500938 * 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 +0100939 */
940static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
941 struct quic_frame *frm, struct quic_conn *conn)
942{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100943 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100944
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100945 if (!quic_enc_int(buf, end, cc->error_code) ||
946 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
947 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100948 return 0;
949
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100950 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
951 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100952
953 return 1;
954}
955
956/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
957 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
958 * and another at QUIC layer.
959 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
960 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100961static 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 +0100962 const unsigned char **buf, const unsigned char *end)
963{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100964 size_t plen;
965 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100966
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100967 if (!quic_dec_int(&cc->error_code, buf, end) ||
968 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
969 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100970 return 0;
971
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200972 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100973 memcpy(cc->reason_phrase, *buf, plen);
974 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100975
976 return 1;
977}
978
979/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
980 * Always succeeds.
981 */
982static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
983 struct quic_frame *frm, struct quic_conn *conn)
984{
985 /* No field */
986 return 1;
987}
988
989/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
990 * Always succeed.
991 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100992static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100993 const unsigned char **buf, const unsigned char *end)
994{
995 /* No field */
996 return 1;
997}
998
999struct quic_frame_builder {
1000 int (*func)(unsigned char **buf, const unsigned char *end,
1001 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001002 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001003 unsigned char flags;
1004};
1005
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001006const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001007 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1008 [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 +02001009 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1010 [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 +02001011 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1012 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1013 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1014 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1015 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1016 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1017 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1018 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1019 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1020 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1021 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1023 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1024 [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, },
1025 [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, },
1026 [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, },
1027 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1028 [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, },
1029 [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, },
1030 [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, },
1031 [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, },
1032 [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, },
1033 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1034 [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 +02001035 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1036 [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 +02001037 [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 +01001038};
1039
1040struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001041 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1042 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001043 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001044 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001045};
1046
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001047const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001048 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1049 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1050 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1051 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1052 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1053 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1054 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1055 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1056 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1057 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1058 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1059 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1060 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1061 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1062 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1064 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1065 [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, },
1066 [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, },
1067 [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, },
1068 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1069 [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, },
1070 [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, },
1071 [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, },
1072 [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, },
1073 [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, },
1074 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1075 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1076 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1077 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1078 [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 +01001079};
1080
1081/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001082 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001083 */
1084int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1085 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001086 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001088 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001089 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001090
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001091 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001092 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001093 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001094 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095 }
1096
1097 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001098 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001099 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001100 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 }
1102
1103 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001104 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001105 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001106 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 }
1108
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001109 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1110 if (!parser->func(frm, qc, buf, end)) {
1111 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001112 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113 }
1114
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001115 pkt->flags |= parser->flags;
1116
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001117 ret = 1;
1118 leave:
1119 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1120 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121}
1122
1123/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001124 * 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 +01001125 */
1126int qc_build_frm(unsigned char **buf, const unsigned char *end,
1127 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001128 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001129{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001130 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001131 const struct quic_frame_builder *builder;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001132
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001133 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001134 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001135 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001136 /* 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 +02001137 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001138 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001139 }
1140
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001142 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001143 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144 }
1145
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001146 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147 *(*buf)++ = frm->type;
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001148 if (!quic_frame_builders[frm->type].func(buf, end, frm, qc)) {
1149 TRACE_DEVEL("frame building error", 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écailledc2593e2021-09-17 16:57:14 +02001153 pkt->flags |= builder->flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001154
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001155 ret = 1;
1156 leave:
1157 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1158 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001159}
1160