blob: 0d8bc87f5b892656c25d0626113db36fc4bd6a4e [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01002 * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020010#include <string.h>
11
Frédéric Lécaille8090b512020-11-30 16:19:22 +010012#include <import/eb64tree.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020013#include <haproxy/quic_conn-t.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020014#include <haproxy/quic_enc.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <haproxy/quic_frame.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020016#include <haproxy/quic_tp-t.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010017#include <haproxy/trace.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010018
19#define TRACE_SOURCE &trace_quic
20
21const char *quic_frame_type_string(enum quic_frame_type ft)
22{
23 switch (ft) {
24 case QUIC_FT_PADDING:
25 return "PADDING";
26 case QUIC_FT_PING:
27 return "PING";
28 case QUIC_FT_ACK:
29 return "ACK";
30 case QUIC_FT_ACK_ECN:
31 return "ACK_ENC";
32 case QUIC_FT_RESET_STREAM:
33 return "RESET_STREAM";
34 case QUIC_FT_STOP_SENDING:
35 return "STOP_SENDING";
36 case QUIC_FT_CRYPTO:
37 return "CRYPTO";
38 case QUIC_FT_NEW_TOKEN:
39 return "NEW_TOKEN";
40
41 case QUIC_FT_STREAM_8:
42 return "STREAM_8";
43 case QUIC_FT_STREAM_9:
44 return "STREAM_9";
45 case QUIC_FT_STREAM_A:
46 return "STREAM_A";
47 case QUIC_FT_STREAM_B:
48 return "STREAM_B";
49 case QUIC_FT_STREAM_C:
50 return "STREAM_C";
51 case QUIC_FT_STREAM_D:
52 return "STREAM_D";
53 case QUIC_FT_STREAM_E:
54 return "STREAM_E";
55 case QUIC_FT_STREAM_F:
56 return "STREAM_F";
57
58 case QUIC_FT_MAX_DATA:
59 return "MAX_DATA";
60 case QUIC_FT_MAX_STREAM_DATA:
61 return "MAX_STREAM_DATA";
62 case QUIC_FT_MAX_STREAMS_BIDI:
63 return "MAX_STREAMS_BIDI";
64 case QUIC_FT_MAX_STREAMS_UNI:
65 return "MAX_STREAMS_UNI";
66 case QUIC_FT_DATA_BLOCKED:
67 return "DATA_BLOCKED";
68 case QUIC_FT_STREAM_DATA_BLOCKED:
69 return "STREAM_DATA_BLOCKED";
70 case QUIC_FT_STREAMS_BLOCKED_BIDI:
71 return "STREAMS_BLOCKED_BIDI";
72 case QUIC_FT_STREAMS_BLOCKED_UNI:
73 return "STREAMS_BLOCKED_UNI";
74 case QUIC_FT_NEW_CONNECTION_ID:
75 return "NEW_CONNECTION_ID";
76 case QUIC_FT_RETIRE_CONNECTION_ID:
77 return "RETIRE_CONNECTION_ID";
78 case QUIC_FT_PATH_CHALLENGE:
79 return "PATH_CHALLENGE";
80 case QUIC_FT_PATH_RESPONSE:
81 return "PATH_RESPONSE";
82 case QUIC_FT_CONNECTION_CLOSE:
83 return "CONNECTION_CLOSE";
84 case QUIC_FT_CONNECTION_CLOSE_APP:
85 return "CONNECTION_CLOSE_APP";
86 case QUIC_FT_HANDSHAKE_DONE:
87 return "HANDSHAKE_DONE";
88 default:
89 return "UNKNOWN";
90 }
91}
92
Frédéric Lécaille010e5322021-12-23 15:19:15 +010093static void chunk_cc_phrase_appendf(struct buffer *buf,
94 const unsigned char *phr, size_t phrlen)
95{
96 chunk_appendf(buf, " reason_phrase: '");
97 while (phrlen--)
98 chunk_appendf(buf, "%c", *phr++);
99 chunk_appendf(buf, "'");
100}
101
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100102/* Add traces to <buf> depending on <frm> frame type. */
103void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm)
104{
105 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
106 switch (frm->type) {
107 case QUIC_FT_CRYPTO:
108 {
109 const struct quic_crypto *cf = &frm->crypto;
110 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
111 (ull)cf->offset, (ull)cf->len);
112 break;
113 }
114 case QUIC_FT_RESET_STREAM:
115 {
116 const struct quic_reset_stream *rs = &frm->reset_stream;
117 chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu",
118 (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size);
119 break;
120 }
121 case QUIC_FT_STOP_SENDING:
122 {
123 const struct quic_stop_sending *s = &frm->stop_sending;
124 chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
125 (ull)s->id, (ull)s->app_error_code);
126 break;
127 }
128 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
129 {
130 const struct quic_stream *s = &frm->stream;
131 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
132 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
133 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
134 (ull)s->id, (ull)s->offset.key, (ull)s->len);
135 break;
136 }
137 case QUIC_FT_MAX_DATA:
138 {
139 const struct quic_max_data *s = &frm->max_data;
140 chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
141 break;
142 }
143 case QUIC_FT_MAX_STREAM_DATA:
144 {
145 const struct quic_max_stream_data *s = &frm->max_stream_data;
146 chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu",
147 (ull)s->id, (ull)s->max_stream_data);
148 break;
149 }
150 case QUIC_FT_MAX_STREAMS_BIDI:
151 {
152 const struct quic_max_streams *s = &frm->max_streams_bidi;
153 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
154 break;
155 }
156 case QUIC_FT_MAX_STREAMS_UNI:
157 {
158 const struct quic_max_streams *s = &frm->max_streams_uni;
159 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
160 break;
161 }
162 case QUIC_FT_DATA_BLOCKED:
163 {
164 const struct quic_data_blocked *s = &frm->data_blocked;
165 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
166 break;
167 }
168 case QUIC_FT_STREAM_DATA_BLOCKED:
169 {
170 const struct quic_stream_data_blocked *s = &frm->stream_data_blocked;
171 chunk_appendf(&trace_buf, " id=%llu limit=%llu",
172 (ull)s->id, (ull)s->limit);
173 break;
174 }
175 case QUIC_FT_STREAMS_BLOCKED_BIDI:
176 {
177 const struct quic_streams_blocked *s = &frm->streams_blocked_bidi;
178 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
179 break;
180 }
181 case QUIC_FT_STREAMS_BLOCKED_UNI:
182 {
183 const struct quic_streams_blocked *s = &frm->streams_blocked_uni;
184 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
185 break;
186 }
187 case QUIC_FT_RETIRE_CONNECTION_ID:
188 {
189 const struct quic_retire_connection_id *rci = &frm->retire_connection_id;
190 chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
191 break;
192 }
193 case QUIC_FT_CONNECTION_CLOSE:
194 {
195 const struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200196 size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100197 chunk_appendf(&trace_buf,
198 " error_code=%llu frame_type=%llu reason_phrase_len=%llu",
199 (ull)cc->error_code, (ull)cc->frame_type,
200 (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100201 if (plen)
202 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100203 break;
204 }
205 case QUIC_FT_CONNECTION_CLOSE_APP:
206 {
207 const struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200208 size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100209 chunk_appendf(&trace_buf,
210 " error_code=%llu reason_phrase_len=%llu",
211 (ull)cc->error_code, (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100212 if (plen)
213 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100214 break;
215 }
216 }
217}
218
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219/* Encode <frm> PADDING frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500220 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 */
222static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
223 struct quic_frame *frm, struct quic_conn *conn)
224{
225 struct quic_padding *padding = &frm->padding;
226
227 if (end - *buf < padding->len - 1)
228 return 0;
229
230 memset(*buf, 0, padding->len - 1);
231 *buf += padding->len - 1;
232
233 return 1;
234}
235
236/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
237 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
238 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100239static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100240 const unsigned char **buf, const unsigned char *end)
241{
242 const unsigned char *beg;
243 struct quic_padding *padding = &frm->padding;
244
245 beg = *buf;
246 padding->len = 1;
247 while (*buf < end && !**buf)
248 (*buf)++;
249 padding->len += *buf - beg;
250
251 return 1;
252}
253
254/* Encode a ACK frame into <buf> buffer.
255 * Always succeeds.
256 */
257static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
258 struct quic_frame *frm, struct quic_conn *conn)
259{
260 /* No field */
261 return 1;
262}
263
264/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
265 * Always succeeds.
266 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100267static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100268 const unsigned char **buf, const unsigned char *end)
269{
270 /* No field */
271 return 1;
272}
273
274/* Encode a ACK frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500275 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100276 */
277static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100278 struct quic_frame *frm, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100279{
280 struct quic_tx_ack *tx_ack = &frm->tx_ack;
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100281 struct eb64_node *ar, *prev_ar;
282 struct quic_arng_node *ar_node, *prev_ar_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100284 ar = eb64_last(&tx_ack->arngs->root);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200285 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200286 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100287 qc,, &ar_node->last, &ar_node->first.key);
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100288 if (!quic_enc_int(buf, end, ar_node->last) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289 !quic_enc_int(buf, end, tx_ack->ack_delay) ||
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100290 !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
291 !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292 return 0;
293
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100294 while ((prev_ar = eb64_prev(ar))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200295 prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200296 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,,
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100297 &prev_ar_node->last, &prev_ar_node->first.key);
298 if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
299 !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 return 0;
301
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100302 ar = prev_ar;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200303 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 }
305
306 return 1;
307}
308
309/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
310 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
311 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100312static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100313 const unsigned char **buf, const unsigned char *end)
314{
315 int ret;
316 struct quic_ack *ack = &frm->ack;
317
318 ret = quic_dec_int(&ack->largest_ack, buf, end);
319 if (!ret)
320 return 0;
321
322 ret = quic_dec_int(&ack->ack_delay, buf, end);
323 if (!ret)
324 return 0;
325
326 ret = quic_dec_int(&ack->ack_range_num, buf, end);
327 if (!ret)
328 return 0;
329
330 ret = quic_dec_int(&ack->first_ack_range, buf, end);
331 if (!ret)
332 return 0;
333
334 return 1;
335}
336
337/* Encode a ACK_ECN frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500338 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 */
340static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
341 struct quic_frame *frm, struct quic_conn *conn)
342{
343 struct quic_ack *ack = &frm->ack;
344
345 return quic_enc_int(buf, end, ack->largest_ack) &&
346 quic_enc_int(buf, end, ack->ack_delay) &&
347 quic_enc_int(buf, end, ack->first_ack_range) &&
348 quic_enc_int(buf, end, ack->ack_range_num);
349}
350
351/* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame.
352 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
353 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100354static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100355 const unsigned char **buf, const unsigned char *end)
356{
357 struct quic_ack *ack = &frm->ack;
358
359 return quic_dec_int(&ack->largest_ack, buf, end) &&
360 quic_dec_int(&ack->ack_delay, buf, end) &&
361 quic_dec_int(&ack->first_ack_range, buf, end) &&
362 quic_dec_int(&ack->ack_range_num, buf, end);
363}
364
365/* Encode a RESET_STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500366 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100367 */
368static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
369 struct quic_frame *frm, struct quic_conn *conn)
370{
371 struct quic_reset_stream *reset_stream = &frm->reset_stream;
372
373 return quic_enc_int(buf, end, reset_stream->id) &&
374 quic_enc_int(buf, end, reset_stream->app_error_code) &&
375 quic_enc_int(buf, end, reset_stream->final_size);
376}
377
378/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
379 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
380 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100381static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100382 const unsigned char **buf, const unsigned char *end)
383{
384 struct quic_reset_stream *reset_stream = &frm->reset_stream;
385
386 return quic_dec_int(&reset_stream->id, buf, end) &&
387 quic_dec_int(&reset_stream->app_error_code, buf, end) &&
388 quic_dec_int(&reset_stream->final_size, buf, end);
389}
390
391/* Encode a STOP_SENDING frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500392 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 */
394static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
395 struct quic_frame *frm, struct quic_conn *conn)
396{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100397 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100399 return quic_enc_int(buf, end, stop_sending->id) &&
400 quic_enc_int(buf, end, stop_sending->app_error_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401}
402
403/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
404 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
405 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100406static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100407 const unsigned char **buf, const unsigned char *end)
408{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100409 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100410
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100411 return quic_dec_int(&stop_sending->id, buf, end) &&
412 quic_dec_int(&stop_sending->app_error_code, buf, end);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413}
414
415/* Encode a CRYPTO frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500416 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 */
418static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
419 struct quic_frame *frm, struct quic_conn *conn)
420{
421 struct quic_crypto *crypto = &frm->crypto;
422 const struct quic_enc_level *qel = crypto->qel;
423 size_t offset, len;
424
425 if (!quic_enc_int(buf, end, crypto->offset) ||
426 !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
427 return 0;
428
429 len = crypto->len;
430 offset = crypto->offset;
431 while (len) {
432 int idx;
433 size_t to_copy;
434 const unsigned char *data;
435
436 idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
437 to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
438 if (to_copy > len)
439 to_copy = len;
440 data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
441 memcpy(*buf, data, to_copy);
442 *buf += to_copy;
443 offset += to_copy;
444 len -= to_copy;
445 }
446
447 return 1;
448}
449
450/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
451 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
452 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100453static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100454 const unsigned char **buf, const unsigned char *end)
455{
456 struct quic_crypto *crypto = &frm->crypto;
457
458 if (!quic_dec_int(&crypto->offset, buf, end) ||
459 !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
460 return 0;
461
462 crypto->data = *buf;
463 *buf += crypto->len;
464
465 return 1;
466}
467
468/* Encode a NEW_TOKEN frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500469 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470 */
471static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
472 struct quic_frame *frm, struct quic_conn *conn)
473{
474 struct quic_new_token *new_token = &frm->new_token;
475
476 if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
477 return 0;
478
479 memcpy(*buf, new_token->data, new_token->len);
480
481 return 1;
482}
483
484/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
485 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
486 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100487static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100488 const unsigned char **buf, const unsigned char *end)
489{
490 struct quic_new_token *new_token = &frm->new_token;
491
492 if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
493 return 0;
494
495 new_token->data = *buf;
496 *buf += new_token->len;
497
498 return 1;
499}
500
501/* Encode a STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500502 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 */
504static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
505 struct quic_frame *frm, struct quic_conn *conn)
506{
507 struct quic_stream *stream = &frm->stream;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100508 const unsigned char *wrap;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509
Amaury Denoyelle1dac0182023-02-02 16:45:07 +0100510 /* Caller must set OFF bit if and only if a non-null offset is used. */
511 BUG_ON(!!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) !=
512 !!stream->offset.key);
513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200515 ((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 +0100516 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
518 return 0;
519
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100520 wrap = (const unsigned char *)b_wrap(stream->buf);
521 if (stream->data + stream->len > wrap) {
522 size_t to_copy = wrap - stream->data;
523 memcpy(*buf, stream->data, to_copy);
524 *buf += to_copy;
525
526 to_copy = stream->len - to_copy;
527 memcpy(*buf, b_orig(stream->buf), to_copy);
528 *buf += to_copy;
529 }
530 else {
531 memcpy(*buf, stream->data, stream->len);
532 *buf += stream->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200533 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534
535 return 1;
536}
537
538/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
539 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
540 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100541static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100542 const unsigned char **buf, const unsigned char *end)
543{
544 struct quic_stream *stream = &frm->stream;
545
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100546 if (!quic_dec_int(&stream->id, buf, end))
547 return 0;
548
549 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100550 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200551 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100552 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200553 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100554 return 0;
555
556 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100557 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100558 stream->len = end - *buf;
559 }
560 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100561 return 0;
562
563 stream->data = *buf;
564 *buf += stream->len;
565
566 return 1;
567}
568
569/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500570 * 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 +0100571 */
572static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
573 struct quic_frame *frm, struct quic_conn *conn)
574{
575 struct quic_max_data *max_data = &frm->max_data;
576
577 return quic_enc_int(buf, end, max_data->max_data);
578}
579
580/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
581 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
582 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100583static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 const unsigned char **buf, const unsigned char *end)
585{
586 struct quic_max_data *max_data = &frm->max_data;
587
588 return quic_dec_int(&max_data->max_data, buf, end);
589}
590
591/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500592 * 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 +0100593 */
594static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
595 struct quic_frame *frm, struct quic_conn *conn)
596{
597 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
598
599 return quic_enc_int(buf, end, max_stream_data->id) &&
600 quic_enc_int(buf, end, max_stream_data->max_stream_data);
601}
602
603/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
604 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
605 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100606static 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 +0100607 const unsigned char **buf, const unsigned char *end)
608{
609 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
610
611 return quic_dec_int(&max_stream_data->id, buf, end) &&
612 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
613}
614
615/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500616 * 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 +0100617 */
618static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
619 struct quic_frame *frm, struct quic_conn *conn)
620{
621 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
622
623 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
624}
625
626/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
627 * as end into <frm> frame.
628 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
629 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100630static 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 +0100631 const unsigned char **buf, const unsigned char *end)
632{
633 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
634
635 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
636}
637
638/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500639 * 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 +0100640 */
641static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
642 struct quic_frame *frm, struct quic_conn *conn)
643{
644 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
645
646 return quic_enc_int(buf, end, max_streams_uni->max_streams);
647}
648
649/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
650 * as end into <frm> frame.
651 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
652 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100653static 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 +0100654 const unsigned char **buf, const unsigned char *end)
655{
656 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
657
658 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
659}
660
661/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500662 * 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 +0100663 */
664static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
665 struct quic_frame *frm, struct quic_conn *conn)
666{
667 struct quic_data_blocked *data_blocked = &frm->data_blocked;
668
669 return quic_enc_int(buf, end, data_blocked->limit);
670}
671
672/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
673 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
674 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100675static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100676 const unsigned char **buf, const unsigned char *end)
677{
678 struct quic_data_blocked *data_blocked = &frm->data_blocked;
679
680 return quic_dec_int(&data_blocked->limit, buf, end);
681}
682
683/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500684 * 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 +0100685 */
686static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
687 struct quic_frame *frm, struct quic_conn *conn)
688{
689 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
690
691 return quic_enc_int(buf, end, stream_data_blocked->id) &&
692 quic_enc_int(buf, end, stream_data_blocked->limit);
693}
694
695/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
696 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
697 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100698static 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 +0100699 const unsigned char **buf, const unsigned char *end)
700{
701 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
702
703 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
704 quic_dec_int(&stream_data_blocked->limit, buf, end);
705}
706
707/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500708 * 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 +0100709 */
710static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
711 struct quic_frame *frm, struct quic_conn *conn)
712{
713 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
714
715 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
716}
717
718/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
719 * as end into <frm> frame.
720 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
721 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100722static 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 +0100723 const unsigned char **buf, const unsigned char *end)
724{
725 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
726
727 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
728}
729
730/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500731 * 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 +0100732 */
733static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
734 struct quic_frame *frm, struct quic_conn *conn)
735{
736 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
737
738 return quic_enc_int(buf, end, streams_blocked_uni->limit);
739}
740
741/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
742 * as end into <frm> frame.
743 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
744 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100745static 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 +0100746 const unsigned char **buf, const unsigned char *end)
747{
748 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
749
750 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
751}
752
753/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500754 * 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 +0100755 */
756static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
757 struct quic_frame *frm, struct quic_conn *conn)
758{
759 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
760
761 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
762 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
763 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
764 return 0;
765
766 *(*buf)++ = new_cid->cid.len;
767
768 if (new_cid->cid.len) {
769 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
770 *buf += new_cid->cid.len;
771 }
772 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
773 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
774
775 return 1;
776}
777
778/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
779 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
780 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100781static 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 +0100782 const unsigned char **buf, const unsigned char *end)
783{
784 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
785
786 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
787 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
788 return 0;
789
790 new_cid->cid.len = *(*buf)++;
791 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
792 return 0;
793
794 if (new_cid->cid.len) {
795 new_cid->cid.data = *buf;
796 *buf += new_cid->cid.len;
797 }
798 new_cid->stateless_reset_token = *buf;
799 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
800
801 return 1;
802}
803
804/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500805 * 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 +0100806 */
807static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
808 struct quic_frame *frm, struct quic_conn *conn)
809{
810 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
811
812 return quic_enc_int(buf, end, retire_connection_id->seq_num);
813}
814
815/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
816 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
817 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100818static 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 +0100819 const unsigned char **buf, const unsigned char *end)
820{
821 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
822
823 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
824}
825
826/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500827 * 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 +0100828 */
829static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
830 struct quic_frame *frm, struct quic_conn *conn)
831{
832 struct quic_path_challenge *path_challenge = &frm->path_challenge;
833
834 if (end - *buf < sizeof path_challenge->data)
835 return 0;
836
837 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
838 *buf += sizeof path_challenge->data;
839
840 return 1;
841}
842
843/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
844 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
845 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100846static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847 const unsigned char **buf, const unsigned char *end)
848{
849 struct quic_path_challenge *path_challenge = &frm->path_challenge;
850
851 if (end - *buf < sizeof path_challenge->data)
852 return 0;
853
854 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
855 *buf += sizeof path_challenge->data;
856
857 return 1;
858}
859
860
861/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500862 * 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 +0100863 */
864static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
865 struct quic_frame *frm, struct quic_conn *conn)
866{
867 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
868
869 if (end - *buf < sizeof path_challenge_response->data)
870 return 0;
871
872 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
873 *buf += sizeof path_challenge_response->data;
874
875 return 1;
876}
877
878/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
879 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
880 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100881static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100882 const unsigned char **buf, const unsigned char *end)
883{
884 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
885
886 if (end - *buf < sizeof path_challenge_response->data)
887 return 0;
888
889 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
890 *buf += sizeof path_challenge_response->data;
891
892 return 1;
893}
894
895/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
896 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
897 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500898 * 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 +0100899 */
900static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
901 struct quic_frame *frm, struct quic_conn *conn)
902{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100903 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100905 if (!quic_enc_int(buf, end, cc->error_code) ||
906 !quic_enc_int(buf, end, cc->frame_type) ||
907 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
908 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100909 return 0;
910
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100911 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
912 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913
914 return 1;
915}
916
917/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
918 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
919 * and another at QUIC layer.
920 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
921 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100922static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 const unsigned char **buf, const unsigned char *end)
924{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100925 size_t plen;
926 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100928 if (!quic_dec_int(&cc->error_code, buf, end) ||
929 !quic_dec_int(&cc->frame_type, buf, end) ||
930 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
931 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100932 return 0;
933
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200934 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100935 memcpy(cc->reason_phrase, *buf, plen);
936 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937
938 return 1;
939}
940
941/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
942 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
943 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500944 * 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 +0100945 */
946static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
947 struct quic_frame *frm, struct quic_conn *conn)
948{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100949 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100950
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100951 if (!quic_enc_int(buf, end, cc->error_code) ||
952 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
953 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954 return 0;
955
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100956 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
957 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100958
959 return 1;
960}
961
962/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
963 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
964 * and another at QUIC layer.
965 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
966 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100967static 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 +0100968 const unsigned char **buf, const unsigned char *end)
969{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100970 size_t plen;
971 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100972
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100973 if (!quic_dec_int(&cc->error_code, buf, end) ||
974 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
975 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100976 return 0;
977
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200978 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100979 memcpy(cc->reason_phrase, *buf, plen);
980 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100981
982 return 1;
983}
984
985/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
986 * Always succeeds.
987 */
988static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
989 struct quic_frame *frm, struct quic_conn *conn)
990{
991 /* No field */
992 return 1;
993}
994
995/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
996 * Always succeed.
997 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100998static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100999 const unsigned char **buf, const unsigned char *end)
1000{
1001 /* No field */
1002 return 1;
1003}
1004
1005struct quic_frame_builder {
1006 int (*func)(unsigned char **buf, const unsigned char *end,
1007 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001008 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001009 unsigned char flags;
1010};
1011
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001012const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001013 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1014 [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 +02001015 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1016 [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 +02001017 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1018 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1019 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1020 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1021 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1023 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1024 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1025 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1026 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1027 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1028 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1029 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1030 [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, },
1031 [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, },
1032 [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, },
1033 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1034 [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, },
1035 [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, },
1036 [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, },
1037 [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, },
1038 [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, },
1039 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1040 [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 +02001041 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1042 [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 +02001043 [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 +01001044};
1045
1046struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001047 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1048 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001049 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001050 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001051};
1052
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001053const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001054 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1055 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1056 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1057 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1058 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1059 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1060 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1061 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1062 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1064 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1065 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1066 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1067 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1068 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1069 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1070 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1071 [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, },
1072 [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, },
1073 [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, },
1074 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1075 [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, },
1076 [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, },
1077 [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, },
1078 [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, },
1079 [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, },
1080 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1081 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1082 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1083 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1084 [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 +01001085};
1086
1087/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001088 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 */
1090int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1091 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001092 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001094 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001095 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001097 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001099 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001100 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 }
1102
1103 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001104 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001105 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001106 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 }
1108
1109 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001110 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001111 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001112 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113 }
1114
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001115 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1116 if (!parser->func(frm, qc, buf, end)) {
1117 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001118 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001119 }
1120
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001121 pkt->flags |= parser->flags;
1122
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001123 ret = 1;
1124 leave:
1125 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1126 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127}
1128
1129/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001130 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001131 * The buffer is updated to point to one byte past the end of the built frame
1132 * only if succeeded.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001133 */
1134int qc_build_frm(unsigned char **buf, const unsigned char *end,
1135 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001136 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001137{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001138 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001139 const struct quic_frame_builder *builder;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001140 unsigned char *pos = *buf;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001141
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001142 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001143 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001144 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001145 /* XXX This it a bug to send an unauthorized frame with such a packet type XXX */
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001146 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001147 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001148 }
1149
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001150 if (end <= pos) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001151 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001152 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001153 }
1154
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001155 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001156 *pos++ = frm->type;
1157 if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001158 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001159 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001160 }
1161
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001162 pkt->flags |= builder->flags;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001163 *buf = pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001165 ret = 1;
1166 leave:
1167 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1168 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001169}
1170