blob: 7194e6fda390c5c28c7ea956a7b4aa256f9fa1c1 [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
89/* Encode <frm> PADDING frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050090 * 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 +010091 */
92static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
93 struct quic_frame *frm, struct quic_conn *conn)
94{
95 struct quic_padding *padding = &frm->padding;
96
97 if (end - *buf < padding->len - 1)
98 return 0;
99
100 memset(*buf, 0, padding->len - 1);
101 *buf += padding->len - 1;
102
103 return 1;
104}
105
106/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
107 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
108 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100109static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100110 const unsigned char **buf, const unsigned char *end)
111{
112 const unsigned char *beg;
113 struct quic_padding *padding = &frm->padding;
114
115 beg = *buf;
116 padding->len = 1;
117 while (*buf < end && !**buf)
118 (*buf)++;
119 padding->len += *buf - beg;
120
121 return 1;
122}
123
124/* Encode a ACK frame into <buf> buffer.
125 * Always succeeds.
126 */
127static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
128 struct quic_frame *frm, struct quic_conn *conn)
129{
130 /* No field */
131 return 1;
132}
133
134/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
135 * Always succeeds.
136 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100137static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100138 const unsigned char **buf, const unsigned char *end)
139{
140 /* No field */
141 return 1;
142}
143
144/* Encode a ACK frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500145 * 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 +0100146 */
147static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
148 struct quic_frame *frm, struct quic_conn *conn)
149{
150 struct quic_tx_ack *tx_ack = &frm->tx_ack;
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100151 struct eb64_node *ar, *prev_ar;
152 struct quic_arng_node *ar_node, *prev_ar_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100153
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100154 ar = eb64_last(&tx_ack->arngs->root);
155 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
156 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
157 conn->conn,, &ar_node->last, &ar_node->first.key);
158 if (!quic_enc_int(buf, end, ar_node->last) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159 !quic_enc_int(buf, end, tx_ack->ack_delay) ||
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100160 !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
161 !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162 return 0;
163
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100164 while ((prev_ar = eb64_prev(ar))) {
165 prev_ar_node = eb64_entry(&prev_ar->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM, conn->conn,,
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100167 &prev_ar_node->last, &prev_ar_node->first.key);
168 if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
169 !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100170 return 0;
171
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100172 ar = prev_ar;
173 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174 }
175
176 return 1;
177}
178
179/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
180 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
181 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100182static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100183 const unsigned char **buf, const unsigned char *end)
184{
185 int ret;
186 struct quic_ack *ack = &frm->ack;
187
188 ret = quic_dec_int(&ack->largest_ack, buf, end);
189 if (!ret)
190 return 0;
191
192 ret = quic_dec_int(&ack->ack_delay, buf, end);
193 if (!ret)
194 return 0;
195
196 ret = quic_dec_int(&ack->ack_range_num, buf, end);
197 if (!ret)
198 return 0;
199
200 ret = quic_dec_int(&ack->first_ack_range, buf, end);
201 if (!ret)
202 return 0;
203
204 return 1;
205}
206
207/* Encode a ACK_ECN frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500208 * 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 +0100209 */
210static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
211 struct quic_frame *frm, struct quic_conn *conn)
212{
213 struct quic_ack *ack = &frm->ack;
214
215 return quic_enc_int(buf, end, ack->largest_ack) &&
216 quic_enc_int(buf, end, ack->ack_delay) &&
217 quic_enc_int(buf, end, ack->first_ack_range) &&
218 quic_enc_int(buf, end, ack->ack_range_num);
219}
220
221/* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame.
222 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
223 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100224static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100225 const unsigned char **buf, const unsigned char *end)
226{
227 struct quic_ack *ack = &frm->ack;
228
229 return quic_dec_int(&ack->largest_ack, buf, end) &&
230 quic_dec_int(&ack->ack_delay, buf, end) &&
231 quic_dec_int(&ack->first_ack_range, buf, end) &&
232 quic_dec_int(&ack->ack_range_num, buf, end);
233}
234
235/* Encode a RESET_STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500236 * 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 +0100237 */
238static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
239 struct quic_frame *frm, struct quic_conn *conn)
240{
241 struct quic_reset_stream *reset_stream = &frm->reset_stream;
242
243 return quic_enc_int(buf, end, reset_stream->id) &&
244 quic_enc_int(buf, end, reset_stream->app_error_code) &&
245 quic_enc_int(buf, end, reset_stream->final_size);
246}
247
248/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
249 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
250 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100251static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100252 const unsigned char **buf, const unsigned char *end)
253{
254 struct quic_reset_stream *reset_stream = &frm->reset_stream;
255
256 return quic_dec_int(&reset_stream->id, buf, end) &&
257 quic_dec_int(&reset_stream->app_error_code, buf, end) &&
258 quic_dec_int(&reset_stream->final_size, buf, end);
259}
260
261/* Encode a STOP_SENDING frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500262 * 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 +0100263 */
264static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
265 struct quic_frame *frm, struct quic_conn *conn)
266{
267 struct quic_stop_sending_frame *stop_sending_frame = &frm->stop_sending_frame;
268
269 return quic_enc_int(buf, end, stop_sending_frame->id) &&
270 quic_enc_int(buf, end, stop_sending_frame->app_error_code);
271}
272
273/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
274 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
275 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100276static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100277 const unsigned char **buf, const unsigned char *end)
278{
279 struct quic_stop_sending_frame *stop_sending_frame = &frm->stop_sending_frame;
280
281 return quic_dec_int(&stop_sending_frame->id, buf, end) &&
282 quic_dec_int(&stop_sending_frame->app_error_code, buf, end);
283}
284
285/* Encode a CRYPTO frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500286 * 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 +0100287 */
288static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
289 struct quic_frame *frm, struct quic_conn *conn)
290{
291 struct quic_crypto *crypto = &frm->crypto;
292 const struct quic_enc_level *qel = crypto->qel;
293 size_t offset, len;
294
295 if (!quic_enc_int(buf, end, crypto->offset) ||
296 !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
297 return 0;
298
299 len = crypto->len;
300 offset = crypto->offset;
301 while (len) {
302 int idx;
303 size_t to_copy;
304 const unsigned char *data;
305
306 idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
307 to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
308 if (to_copy > len)
309 to_copy = len;
310 data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
311 memcpy(*buf, data, to_copy);
312 *buf += to_copy;
313 offset += to_copy;
314 len -= to_copy;
315 }
316
317 return 1;
318}
319
320/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
321 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
322 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100323static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 const unsigned char **buf, const unsigned char *end)
325{
326 struct quic_crypto *crypto = &frm->crypto;
327
328 if (!quic_dec_int(&crypto->offset, buf, end) ||
329 !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
330 return 0;
331
332 crypto->data = *buf;
333 *buf += crypto->len;
334
335 return 1;
336}
337
338/* Encode a NEW_TOKEN frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500339 * 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 +0100340 */
341static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
342 struct quic_frame *frm, struct quic_conn *conn)
343{
344 struct quic_new_token *new_token = &frm->new_token;
345
346 if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
347 return 0;
348
349 memcpy(*buf, new_token->data, new_token->len);
350
351 return 1;
352}
353
354/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
355 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
356 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100357static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100358 const unsigned char **buf, const unsigned char *end)
359{
360 struct quic_new_token *new_token = &frm->new_token;
361
362 if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
363 return 0;
364
365 new_token->data = *buf;
366 *buf += new_token->len;
367
368 return 1;
369}
370
371/* Encode a STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500372 * 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 +0100373 */
374static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
375 struct quic_frame *frm, struct quic_conn *conn)
376{
377 struct quic_stream *stream = &frm->stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200378 size_t offset, block1, block2;
379 struct buffer b;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100380
381 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200382 ((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 +0100383 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100384 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
385 return 0;
386
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200387 /* Buffer copy */
388 b = *stream->buf;
389 offset = (frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ?
390 stream->offset.key & (b_size(stream->buf) - 1): 0;
391 block1 = b_wrap(&b) - (b_orig(&b) + offset);
392 if (block1 > stream->len)
393 block1 = stream->len;
394 block2 = stream->len - block1;
395 memcpy(*buf, b_orig(&b) + offset, block1);
396 *buf += block1;
397 if (block2) {
398 memcpy(*buf, b_orig(&b), block2);
399 *buf += block2;
400 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401
402 return 1;
403}
404
405/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
406 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
407 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100408static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100409 const unsigned char **buf, const unsigned char *end)
410{
411 struct quic_stream *stream = &frm->stream;
412
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100413 if (!quic_dec_int(&stream->id, buf, end))
414 return 0;
415
416 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100417 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200418 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100419 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200420 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100421 return 0;
422
423 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100424 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100425 stream->len = end - *buf;
426 }
427 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100428 return 0;
429
430 stream->data = *buf;
431 *buf += stream->len;
432
433 return 1;
434}
435
436/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500437 * 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 +0100438 */
439static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
440 struct quic_frame *frm, struct quic_conn *conn)
441{
442 struct quic_max_data *max_data = &frm->max_data;
443
444 return quic_enc_int(buf, end, max_data->max_data);
445}
446
447/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
448 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
449 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100450static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100451 const unsigned char **buf, const unsigned char *end)
452{
453 struct quic_max_data *max_data = &frm->max_data;
454
455 return quic_dec_int(&max_data->max_data, buf, end);
456}
457
458/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500459 * 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 +0100460 */
461static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
462 struct quic_frame *frm, struct quic_conn *conn)
463{
464 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
465
466 return quic_enc_int(buf, end, max_stream_data->id) &&
467 quic_enc_int(buf, end, max_stream_data->max_stream_data);
468}
469
470/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
471 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
472 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100473static 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 +0100474 const unsigned char **buf, const unsigned char *end)
475{
476 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
477
478 return quic_dec_int(&max_stream_data->id, buf, end) &&
479 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
480}
481
482/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500483 * 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 +0100484 */
485static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
486 struct quic_frame *frm, struct quic_conn *conn)
487{
488 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
489
490 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
491}
492
493/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
494 * as end into <frm> frame.
495 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
496 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100497static 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 +0100498 const unsigned char **buf, const unsigned char *end)
499{
500 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
501
502 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
503}
504
505/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500506 * 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 +0100507 */
508static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
509 struct quic_frame *frm, struct quic_conn *conn)
510{
511 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
512
513 return quic_enc_int(buf, end, max_streams_uni->max_streams);
514}
515
516/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
517 * as end into <frm> frame.
518 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
519 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100520static 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 +0100521 const unsigned char **buf, const unsigned char *end)
522{
523 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
524
525 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
526}
527
528/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500529 * 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 +0100530 */
531static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
532 struct quic_frame *frm, struct quic_conn *conn)
533{
534 struct quic_data_blocked *data_blocked = &frm->data_blocked;
535
536 return quic_enc_int(buf, end, data_blocked->limit);
537}
538
539/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
540 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
541 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100542static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100543 const unsigned char **buf, const unsigned char *end)
544{
545 struct quic_data_blocked *data_blocked = &frm->data_blocked;
546
547 return quic_dec_int(&data_blocked->limit, buf, end);
548}
549
550/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500551 * 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 +0100552 */
553static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
554 struct quic_frame *frm, struct quic_conn *conn)
555{
556 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
557
558 return quic_enc_int(buf, end, stream_data_blocked->id) &&
559 quic_enc_int(buf, end, stream_data_blocked->limit);
560}
561
562/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
563 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
564 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100565static 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 +0100566 const unsigned char **buf, const unsigned char *end)
567{
568 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
569
570 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
571 quic_dec_int(&stream_data_blocked->limit, buf, end);
572}
573
574/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500575 * 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 +0100576 */
577static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
578 struct quic_frame *frm, struct quic_conn *conn)
579{
580 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
581
582 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
583}
584
585/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
586 * as end into <frm> frame.
587 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
588 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100589static 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 +0100590 const unsigned char **buf, const unsigned char *end)
591{
592 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
593
594 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
595}
596
597/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500598 * 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 +0100599 */
600static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
601 struct quic_frame *frm, struct quic_conn *conn)
602{
603 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
604
605 return quic_enc_int(buf, end, streams_blocked_uni->limit);
606}
607
608/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
609 * as end into <frm> frame.
610 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
611 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100612static 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 +0100613 const unsigned char **buf, const unsigned char *end)
614{
615 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
616
617 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
618}
619
620/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500621 * 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 +0100622 */
623static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
624 struct quic_frame *frm, struct quic_conn *conn)
625{
626 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
627
628 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
629 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
630 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
631 return 0;
632
633 *(*buf)++ = new_cid->cid.len;
634
635 if (new_cid->cid.len) {
636 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
637 *buf += new_cid->cid.len;
638 }
639 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
640 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
641
642 return 1;
643}
644
645/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> 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_new_connection_id_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_new_connection_id *new_cid = &frm->new_connection_id;
652
653 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
654 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
655 return 0;
656
657 new_cid->cid.len = *(*buf)++;
658 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
659 return 0;
660
661 if (new_cid->cid.len) {
662 new_cid->cid.data = *buf;
663 *buf += new_cid->cid.len;
664 }
665 new_cid->stateless_reset_token = *buf;
666 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
667
668 return 1;
669}
670
671/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500672 * 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 +0100673 */
674static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
675 struct quic_frame *frm, struct quic_conn *conn)
676{
677 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
678
679 return quic_enc_int(buf, end, retire_connection_id->seq_num);
680}
681
682/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
683 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
684 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100685static 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 +0100686 const unsigned char **buf, const unsigned char *end)
687{
688 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
689
690 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
691}
692
693/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500694 * 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 +0100695 */
696static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
697 struct quic_frame *frm, struct quic_conn *conn)
698{
699 struct quic_path_challenge *path_challenge = &frm->path_challenge;
700
701 if (end - *buf < sizeof path_challenge->data)
702 return 0;
703
704 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
705 *buf += sizeof path_challenge->data;
706
707 return 1;
708}
709
710/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
711 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
712 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100713static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100714 const unsigned char **buf, const unsigned char *end)
715{
716 struct quic_path_challenge *path_challenge = &frm->path_challenge;
717
718 if (end - *buf < sizeof path_challenge->data)
719 return 0;
720
721 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
722 *buf += sizeof path_challenge->data;
723
724 return 1;
725}
726
727
728/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500729 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100730 */
731static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
732 struct quic_frame *frm, struct quic_conn *conn)
733{
734 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
735
736 if (end - *buf < sizeof path_challenge_response->data)
737 return 0;
738
739 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
740 *buf += sizeof path_challenge_response->data;
741
742 return 1;
743}
744
745/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
746 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
747 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100748static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100749 const unsigned char **buf, const unsigned char *end)
750{
751 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
752
753 if (end - *buf < sizeof path_challenge_response->data)
754 return 0;
755
756 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
757 *buf += sizeof path_challenge_response->data;
758
759 return 1;
760}
761
762/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
763 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
764 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500765 * 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 +0100766 */
767static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
768 struct quic_frame *frm, struct quic_conn *conn)
769{
770 struct quic_connection_close *connection_close = &frm->connection_close;
771
772 if (!quic_enc_int(buf, end, connection_close->error_code) ||
773 !quic_enc_int(buf, end, connection_close->frame_type) ||
774 !quic_enc_int(buf, end, connection_close->reason_phrase_len) ||
775 end - *buf < connection_close->reason_phrase_len)
776 return 0;
777
778 memcpy(*buf, connection_close->reason_phrase, connection_close->reason_phrase_len);
779 *buf += connection_close->reason_phrase_len;
780
781 return 1;
782}
783
784/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
785 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
786 * and another at QUIC layer.
787 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
788 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100789static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100790 const unsigned char **buf, const unsigned char *end)
791{
792 struct quic_connection_close *connection_close = &frm->connection_close;
793
794 if (!quic_dec_int(&connection_close->error_code, buf, end) ||
795 !quic_dec_int(&connection_close->frame_type, buf, end) ||
796 !quic_dec_int(&connection_close->reason_phrase_len, buf, end) ||
797 end - *buf < connection_close->reason_phrase_len)
798 return 0;
799
800 if (connection_close->reason_phrase_len) {
801 memcpy(connection_close->reason_phrase, *buf, connection_close->reason_phrase_len);
802 *buf += connection_close->reason_phrase_len;
803 }
804
805 return 1;
806}
807
808/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
809 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
810 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500811 * 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 +0100812 */
813static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
814 struct quic_frame *frm, struct quic_conn *conn)
815{
816 struct quic_connection_close_app *connection_close_app = &frm->connection_close_app;
817
818 if (!quic_enc_int(buf, end, connection_close_app->error_code) ||
819 !quic_enc_int(buf, end, connection_close_app->reason_phrase_len) ||
820 end - *buf < connection_close_app->reason_phrase_len)
821 return 0;
822
823 if (connection_close_app->reason_phrase_len) {
824 memcpy(*buf, connection_close_app->reason_phrase, connection_close_app->reason_phrase_len);
825 *buf += connection_close_app->reason_phrase_len;
826 }
827
828 return 1;
829}
830
831/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
832 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
833 * and another at QUIC layer.
834 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
835 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100836static 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 +0100837 const unsigned char **buf, const unsigned char *end)
838{
839 struct quic_connection_close_app *connection_close_app = &frm->connection_close_app;
840
841 if (!quic_dec_int(&connection_close_app->error_code, buf, end) ||
842 !quic_dec_int(&connection_close_app->reason_phrase_len, buf, end) ||
843 end - *buf < connection_close_app->reason_phrase_len)
844 return 0;
845
846 memcpy(connection_close_app->reason_phrase, *buf, connection_close_app->reason_phrase_len);
847 *buf += connection_close_app->reason_phrase_len;
848
849 return 1;
850}
851
852/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
853 * Always succeeds.
854 */
855static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
856 struct quic_frame *frm, struct quic_conn *conn)
857{
858 /* No field */
859 return 1;
860}
861
862/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
863 * Always succeed.
864 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100865static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 const unsigned char **buf, const unsigned char *end)
867{
868 /* No field */
869 return 1;
870}
871
872struct quic_frame_builder {
873 int (*func)(unsigned char **buf, const unsigned char *end,
874 struct quic_frame *frm, struct quic_conn *conn);
875 unsigned char flags;
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +0200876 unsigned char mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100877};
878
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +0200879const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +0200880 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
881 [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 +0200882 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
883 [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 +0200884 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
885 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
886 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
887 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
888 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
889 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
890 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
891 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
892 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
893 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
894 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
895 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
896 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
897 [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, },
898 [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, },
899 [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, },
900 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
901 [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, },
902 [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, },
903 [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, },
904 [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, },
905 [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, },
906 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
907 [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 +0200908 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
909 [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 +0200910 [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 +0100911};
912
913struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100914 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
915 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +0100916 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 unsigned char mask;
918};
919
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +0200920const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +0100921 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
922 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
923 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
924 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
925 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
926 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
927 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
928 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
929 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
930 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
931 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
932 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
933 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
934 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
935 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
936 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
937 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
938 [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, },
939 [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, },
940 [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, },
941 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
942 [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, },
943 [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, },
944 [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, },
945 [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, },
946 [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, },
947 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
948 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
949 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
950 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
951 [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 +0100952};
953
954/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500955 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 */
957int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
958 const unsigned char **buf, const unsigned char *end,
959 struct quic_conn *conn)
960{
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +0200961 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100962
963 if (end <= *buf) {
964 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, conn->conn);
965 return 0;
966 }
967
968 frm->type = *(*buf)++;
969 if (frm->type > QUIC_FT_MAX) {
970 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, conn->conn, frm);
971 return 0;
972 }
973
974 parser = &quic_frame_parsers[frm->type];
975 if (!(parser->mask & (1 << pkt->type))) {
976 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, conn->conn, frm);
977 return 0;
978 }
979
980 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, conn->conn, frm);
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100981 if (!parser->func(frm, conn, buf, end)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100982 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, conn->conn, frm);
983 return 0;
984 }
985
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +0100986 pkt->flags |= parser->flags;
987
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100988 return 1;
989}
990
991/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500992 * 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 +0100993 */
994int qc_build_frm(unsigned char **buf, const unsigned char *end,
995 struct quic_frame *frm, struct quic_tx_packet *pkt,
996 struct quic_conn *conn)
997{
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +0200998 const struct quic_frame_builder *builder;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +0100999
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001000 builder = &quic_frame_builders[frm->type];
1001 if (!(builder->mask & (1 << pkt->type))) {
1002 /* XXX This it a bug to send an unauthorized frame with such a packet type XXX */
1003 TRACE_DEVEL("frame skipped", QUIC_EV_CONN_BFRM, conn->conn, frm);
1004 BUG_ON(!(builder->mask & (1 << pkt->type)));
1005 }
1006
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001007 if (end <= *buf) {
1008 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, conn->conn, frm);
1009 return 0;
1010 }
1011
1012 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, conn->conn, frm);
1013 *(*buf)++ = frm->type;
1014 if (!quic_frame_builders[frm->type].func(buf, end, frm, conn)) {
1015 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, conn->conn, frm);
1016 return 0;
1017 }
1018
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001019 pkt->flags |= builder->flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001020
1021 return 1;
1022}
1023