blob: 0adce4c1f79b86bff9a862038c680d1525a0f39a [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * Copyright 2019 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
3 *
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écaille010e5322021-12-23 15:19:15 +0100192 size_t plen = QUIC_MIN(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écaille010e5322021-12-23 15:19:15 +0100204 size_t plen = QUIC_MIN(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);
281 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
282 TRACE_PROTO("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))) {
291 prev_ar_node = eb64_entry(&prev_ar->node, struct quic_arng_node, first);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100292 TRACE_PROTO("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;
299 ar_node = eb64_entry(&ar->node, 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;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200504 size_t offset, block1, block2;
505 struct buffer b;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506
507 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200508 ((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 +0100509 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100510 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
511 return 0;
512
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200513 /* Buffer copy */
514 b = *stream->buf;
515 offset = (frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ?
516 stream->offset.key & (b_size(stream->buf) - 1): 0;
517 block1 = b_wrap(&b) - (b_orig(&b) + offset);
518 if (block1 > stream->len)
519 block1 = stream->len;
520 block2 = stream->len - block1;
521 memcpy(*buf, b_orig(&b) + offset, block1);
522 *buf += block1;
523 if (block2) {
524 memcpy(*buf, b_orig(&b), block2);
525 *buf += block2;
526 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100527
528 return 1;
529}
530
531/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
532 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
533 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100534static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 const unsigned char **buf, const unsigned char *end)
536{
537 struct quic_stream *stream = &frm->stream;
538
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100539 if (!quic_dec_int(&stream->id, buf, end))
540 return 0;
541
542 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100543 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200544 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100545 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200546 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100547 return 0;
548
549 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100550 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100551 stream->len = end - *buf;
552 }
553 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100554 return 0;
555
Amaury Denoyelledb443382021-11-30 11:23:29 +0100556 stream->fin = (frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT);
557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100558 stream->data = *buf;
559 *buf += stream->len;
560
561 return 1;
562}
563
564/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500565 * 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 +0100566 */
567static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
568 struct quic_frame *frm, struct quic_conn *conn)
569{
570 struct quic_max_data *max_data = &frm->max_data;
571
572 return quic_enc_int(buf, end, max_data->max_data);
573}
574
575/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
576 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
577 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100578static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100579 const unsigned char **buf, const unsigned char *end)
580{
581 struct quic_max_data *max_data = &frm->max_data;
582
583 return quic_dec_int(&max_data->max_data, buf, end);
584}
585
586/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500587 * 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 +0100588 */
589static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
590 struct quic_frame *frm, struct quic_conn *conn)
591{
592 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
593
594 return quic_enc_int(buf, end, max_stream_data->id) &&
595 quic_enc_int(buf, end, max_stream_data->max_stream_data);
596}
597
598/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
599 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
600 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100601static 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 +0100602 const unsigned char **buf, const unsigned char *end)
603{
604 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
605
606 return quic_dec_int(&max_stream_data->id, buf, end) &&
607 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
608}
609
610/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500611 * 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 +0100612 */
613static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
614 struct quic_frame *frm, struct quic_conn *conn)
615{
616 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
617
618 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
619}
620
621/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
622 * as end into <frm> frame.
623 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
624 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100625static 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 +0100626 const unsigned char **buf, const unsigned char *end)
627{
628 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
629
630 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
631}
632
633/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500634 * 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 +0100635 */
636static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
637 struct quic_frame *frm, struct quic_conn *conn)
638{
639 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
640
641 return quic_enc_int(buf, end, max_streams_uni->max_streams);
642}
643
644/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
645 * as end into <frm> frame.
646 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
647 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100648static 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 +0100649 const unsigned char **buf, const unsigned char *end)
650{
651 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
652
653 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
654}
655
656/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500657 * 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 +0100658 */
659static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
660 struct quic_frame *frm, struct quic_conn *conn)
661{
662 struct quic_data_blocked *data_blocked = &frm->data_blocked;
663
664 return quic_enc_int(buf, end, data_blocked->limit);
665}
666
667/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
668 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
669 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100670static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671 const unsigned char **buf, const unsigned char *end)
672{
673 struct quic_data_blocked *data_blocked = &frm->data_blocked;
674
675 return quic_dec_int(&data_blocked->limit, buf, end);
676}
677
678/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500679 * 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 +0100680 */
681static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
682 struct quic_frame *frm, struct quic_conn *conn)
683{
684 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
685
686 return quic_enc_int(buf, end, stream_data_blocked->id) &&
687 quic_enc_int(buf, end, stream_data_blocked->limit);
688}
689
690/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
691 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
692 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100693static 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 +0100694 const unsigned char **buf, const unsigned char *end)
695{
696 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
697
698 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
699 quic_dec_int(&stream_data_blocked->limit, buf, end);
700}
701
702/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500703 * 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 +0100704 */
705static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
706 struct quic_frame *frm, struct quic_conn *conn)
707{
708 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
709
710 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
711}
712
713/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
714 * as end into <frm> frame.
715 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
716 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100717static 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 +0100718 const unsigned char **buf, const unsigned char *end)
719{
720 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
721
722 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
723}
724
725/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500726 * 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 +0100727 */
728static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
729 struct quic_frame *frm, struct quic_conn *conn)
730{
731 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
732
733 return quic_enc_int(buf, end, streams_blocked_uni->limit);
734}
735
736/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
737 * as end into <frm> frame.
738 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
739 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100740static 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 +0100741 const unsigned char **buf, const unsigned char *end)
742{
743 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
744
745 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
746}
747
748/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500749 * 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 +0100750 */
751static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
752 struct quic_frame *frm, struct quic_conn *conn)
753{
754 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
755
756 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
757 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
758 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
759 return 0;
760
761 *(*buf)++ = new_cid->cid.len;
762
763 if (new_cid->cid.len) {
764 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
765 *buf += new_cid->cid.len;
766 }
767 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
768 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
769
770 return 1;
771}
772
773/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
774 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
775 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100776static 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 +0100777 const unsigned char **buf, const unsigned char *end)
778{
779 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
780
781 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
782 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
783 return 0;
784
785 new_cid->cid.len = *(*buf)++;
786 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
787 return 0;
788
789 if (new_cid->cid.len) {
790 new_cid->cid.data = *buf;
791 *buf += new_cid->cid.len;
792 }
793 new_cid->stateless_reset_token = *buf;
794 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
795
796 return 1;
797}
798
799/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500800 * 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 +0100801 */
802static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
803 struct quic_frame *frm, struct quic_conn *conn)
804{
805 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
806
807 return quic_enc_int(buf, end, retire_connection_id->seq_num);
808}
809
810/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
811 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
812 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100813static 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 +0100814 const unsigned char **buf, const unsigned char *end)
815{
816 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
817
818 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
819}
820
821/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500822 * 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 +0100823 */
824static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
825 struct quic_frame *frm, struct quic_conn *conn)
826{
827 struct quic_path_challenge *path_challenge = &frm->path_challenge;
828
829 if (end - *buf < sizeof path_challenge->data)
830 return 0;
831
832 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
833 *buf += sizeof path_challenge->data;
834
835 return 1;
836}
837
838/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
839 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
840 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100841static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100842 const unsigned char **buf, const unsigned char *end)
843{
844 struct quic_path_challenge *path_challenge = &frm->path_challenge;
845
846 if (end - *buf < sizeof path_challenge->data)
847 return 0;
848
849 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
850 *buf += sizeof path_challenge->data;
851
852 return 1;
853}
854
855
856/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500857 * 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 +0100858 */
859static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
860 struct quic_frame *frm, struct quic_conn *conn)
861{
862 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
863
864 if (end - *buf < sizeof path_challenge_response->data)
865 return 0;
866
867 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
868 *buf += sizeof path_challenge_response->data;
869
870 return 1;
871}
872
873/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
874 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
875 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100876static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100877 const unsigned char **buf, const unsigned char *end)
878{
879 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
880
881 if (end - *buf < sizeof path_challenge_response->data)
882 return 0;
883
884 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
885 *buf += sizeof path_challenge_response->data;
886
887 return 1;
888}
889
890/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
891 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
892 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500893 * 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 +0100894 */
895static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
896 struct quic_frame *frm, struct quic_conn *conn)
897{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100898 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100899
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100900 if (!quic_enc_int(buf, end, cc->error_code) ||
901 !quic_enc_int(buf, end, cc->frame_type) ||
902 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
903 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904 return 0;
905
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100906 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
907 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908
909 return 1;
910}
911
912/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
913 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
914 * and another at QUIC layer.
915 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
916 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100917static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100918 const unsigned char **buf, const unsigned char *end)
919{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100920 size_t plen;
921 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100923 if (!quic_dec_int(&cc->error_code, buf, end) ||
924 !quic_dec_int(&cc->frame_type, buf, end) ||
925 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
926 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 return 0;
928
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100929 plen = QUIC_MIN(cc->reason_phrase_len, sizeof cc->reason_phrase);
930 memcpy(cc->reason_phrase, *buf, plen);
931 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100932
933 return 1;
934}
935
936/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
937 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
938 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500939 * 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 +0100940 */
941static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
942 struct quic_frame *frm, struct quic_conn *conn)
943{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100944 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100945
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100946 if (!quic_enc_int(buf, end, cc->error_code) ||
947 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
948 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100949 return 0;
950
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100951 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
952 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100953
954 return 1;
955}
956
957/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
958 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
959 * and another at QUIC layer.
960 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
961 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100962static 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 +0100963 const unsigned char **buf, const unsigned char *end)
964{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100965 size_t plen;
966 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100967
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100968 if (!quic_dec_int(&cc->error_code, buf, end) ||
969 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
970 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100971 return 0;
972
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100973 plen = QUIC_MIN(cc->reason_phrase_len, sizeof cc->reason_phrase);
974 memcpy(cc->reason_phrase, *buf, plen);
975 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100976
977 return 1;
978}
979
980/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
981 * Always succeeds.
982 */
983static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
984 struct quic_frame *frm, struct quic_conn *conn)
985{
986 /* No field */
987 return 1;
988}
989
990/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
991 * Always succeed.
992 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100993static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100994 const unsigned char **buf, const unsigned char *end)
995{
996 /* No field */
997 return 1;
998}
999
1000struct quic_frame_builder {
1001 int (*func)(unsigned char **buf, const unsigned char *end,
1002 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001003 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001004 unsigned char flags;
1005};
1006
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001007const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001008 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1009 [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 +02001010 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1011 [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 +02001012 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1013 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1014 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1015 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1016 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1017 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1018 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1019 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1020 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1021 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1023 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1024 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1025 [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, },
1026 [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, },
1027 [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, },
1028 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1029 [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, },
1030 [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, },
1031 [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, },
1032 [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, },
1033 [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, },
1034 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1035 [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 +02001036 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1037 [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 +02001038 [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 +01001039};
1040
1041struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001042 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1043 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001044 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001045 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001046};
1047
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001048const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001049 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1050 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1051 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1052 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1053 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1054 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1055 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1056 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1057 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1058 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1059 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1060 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1061 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1062 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1064 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1065 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1066 [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, },
1067 [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, },
1068 [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, },
1069 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1070 [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, },
1071 [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, },
1072 [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, },
1073 [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, },
1074 [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, },
1075 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1076 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1077 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1078 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1079 [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 +01001080};
1081
1082/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001083 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084 */
1085int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1086 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001087 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001088{
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
1091 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001092 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093 return 0;
1094 }
1095
1096 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001097 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001098 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 return 0;
1100 }
1101
1102 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001103 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001104 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 return 0;
1106 }
1107
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001108 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1109 if (!parser->func(frm, qc, buf, end)) {
1110 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111 return 0;
1112 }
1113
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001114 pkt->flags |= parser->flags;
1115
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116 return 1;
1117}
1118
1119/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001120 * 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 +01001121 */
1122int qc_build_frm(unsigned char **buf, const unsigned char *end,
1123 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001124 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125{
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001126 const struct quic_frame_builder *builder;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001127
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001128 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001129 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001130 /* XXX This it a bug to send an unauthorized frame with such a packet type XXX */
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001131 TRACE_DEVEL("frame skipped", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001132 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001133 }
1134
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001135 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001136 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001137 return 0;
1138 }
1139
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001140 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141 *(*buf)++ = frm->type;
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001142 if (!quic_frame_builders[frm->type].func(buf, end, frm, qc)) {
1143 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144 return 0;
1145 }
1146
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001147 pkt->flags |= builder->flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001148
1149 return 1;
1150}
1151