blob: 530a6407703d27fb0a6646145c6f81cca0772680 [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 Denoyelle40c24f12023-01-27 17:47:49 +010013#include <haproxy/buf-t.h>
14#include <haproxy/chunk.h>
15#include <haproxy/pool.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020016#include <haproxy/quic_conn-t.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020017#include <haproxy/quic_enc.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010018#include <haproxy/quic_frame.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020019#include <haproxy/quic_tp-t.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010020#include <haproxy/trace.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010021
22#define TRACE_SOURCE &trace_quic
23
24const char *quic_frame_type_string(enum quic_frame_type ft)
25{
26 switch (ft) {
27 case QUIC_FT_PADDING:
28 return "PADDING";
29 case QUIC_FT_PING:
30 return "PING";
31 case QUIC_FT_ACK:
32 return "ACK";
33 case QUIC_FT_ACK_ECN:
34 return "ACK_ENC";
35 case QUIC_FT_RESET_STREAM:
36 return "RESET_STREAM";
37 case QUIC_FT_STOP_SENDING:
38 return "STOP_SENDING";
39 case QUIC_FT_CRYPTO:
40 return "CRYPTO";
41 case QUIC_FT_NEW_TOKEN:
42 return "NEW_TOKEN";
43
44 case QUIC_FT_STREAM_8:
45 return "STREAM_8";
46 case QUIC_FT_STREAM_9:
47 return "STREAM_9";
48 case QUIC_FT_STREAM_A:
49 return "STREAM_A";
50 case QUIC_FT_STREAM_B:
51 return "STREAM_B";
52 case QUIC_FT_STREAM_C:
53 return "STREAM_C";
54 case QUIC_FT_STREAM_D:
55 return "STREAM_D";
56 case QUIC_FT_STREAM_E:
57 return "STREAM_E";
58 case QUIC_FT_STREAM_F:
59 return "STREAM_F";
60
61 case QUIC_FT_MAX_DATA:
62 return "MAX_DATA";
63 case QUIC_FT_MAX_STREAM_DATA:
64 return "MAX_STREAM_DATA";
65 case QUIC_FT_MAX_STREAMS_BIDI:
66 return "MAX_STREAMS_BIDI";
67 case QUIC_FT_MAX_STREAMS_UNI:
68 return "MAX_STREAMS_UNI";
69 case QUIC_FT_DATA_BLOCKED:
70 return "DATA_BLOCKED";
71 case QUIC_FT_STREAM_DATA_BLOCKED:
72 return "STREAM_DATA_BLOCKED";
73 case QUIC_FT_STREAMS_BLOCKED_BIDI:
74 return "STREAMS_BLOCKED_BIDI";
75 case QUIC_FT_STREAMS_BLOCKED_UNI:
76 return "STREAMS_BLOCKED_UNI";
77 case QUIC_FT_NEW_CONNECTION_ID:
78 return "NEW_CONNECTION_ID";
79 case QUIC_FT_RETIRE_CONNECTION_ID:
80 return "RETIRE_CONNECTION_ID";
81 case QUIC_FT_PATH_CHALLENGE:
82 return "PATH_CHALLENGE";
83 case QUIC_FT_PATH_RESPONSE:
84 return "PATH_RESPONSE";
85 case QUIC_FT_CONNECTION_CLOSE:
86 return "CONNECTION_CLOSE";
87 case QUIC_FT_CONNECTION_CLOSE_APP:
88 return "CONNECTION_CLOSE_APP";
89 case QUIC_FT_HANDSHAKE_DONE:
90 return "HANDSHAKE_DONE";
91 default:
92 return "UNKNOWN";
93 }
94}
95
Frédéric Lécaille010e5322021-12-23 15:19:15 +010096static void chunk_cc_phrase_appendf(struct buffer *buf,
97 const unsigned char *phr, size_t phrlen)
98{
99 chunk_appendf(buf, " reason_phrase: '");
100 while (phrlen--)
101 chunk_appendf(buf, "%c", *phr++);
102 chunk_appendf(buf, "'");
103}
104
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100105/* Add traces to <buf> depending on <frm> frame type. */
106void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm)
107{
108 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
109 switch (frm->type) {
110 case QUIC_FT_CRYPTO:
111 {
112 const struct quic_crypto *cf = &frm->crypto;
113 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
114 (ull)cf->offset, (ull)cf->len);
115 break;
116 }
117 case QUIC_FT_RESET_STREAM:
118 {
119 const struct quic_reset_stream *rs = &frm->reset_stream;
120 chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu",
121 (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size);
122 break;
123 }
124 case QUIC_FT_STOP_SENDING:
125 {
126 const struct quic_stop_sending *s = &frm->stop_sending;
127 chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
128 (ull)s->id, (ull)s->app_error_code);
129 break;
130 }
131 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
132 {
133 const struct quic_stream *s = &frm->stream;
134 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
135 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
136 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
137 (ull)s->id, (ull)s->offset.key, (ull)s->len);
138 break;
139 }
140 case QUIC_FT_MAX_DATA:
141 {
142 const struct quic_max_data *s = &frm->max_data;
143 chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
144 break;
145 }
146 case QUIC_FT_MAX_STREAM_DATA:
147 {
148 const struct quic_max_stream_data *s = &frm->max_stream_data;
149 chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu",
150 (ull)s->id, (ull)s->max_stream_data);
151 break;
152 }
153 case QUIC_FT_MAX_STREAMS_BIDI:
154 {
155 const struct quic_max_streams *s = &frm->max_streams_bidi;
156 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
157 break;
158 }
159 case QUIC_FT_MAX_STREAMS_UNI:
160 {
161 const struct quic_max_streams *s = &frm->max_streams_uni;
162 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
163 break;
164 }
165 case QUIC_FT_DATA_BLOCKED:
166 {
167 const struct quic_data_blocked *s = &frm->data_blocked;
168 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
169 break;
170 }
171 case QUIC_FT_STREAM_DATA_BLOCKED:
172 {
173 const struct quic_stream_data_blocked *s = &frm->stream_data_blocked;
174 chunk_appendf(&trace_buf, " id=%llu limit=%llu",
175 (ull)s->id, (ull)s->limit);
176 break;
177 }
178 case QUIC_FT_STREAMS_BLOCKED_BIDI:
179 {
180 const struct quic_streams_blocked *s = &frm->streams_blocked_bidi;
181 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
182 break;
183 }
184 case QUIC_FT_STREAMS_BLOCKED_UNI:
185 {
186 const struct quic_streams_blocked *s = &frm->streams_blocked_uni;
187 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
188 break;
189 }
190 case QUIC_FT_RETIRE_CONNECTION_ID:
191 {
192 const struct quic_retire_connection_id *rci = &frm->retire_connection_id;
193 chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
194 break;
195 }
196 case QUIC_FT_CONNECTION_CLOSE:
197 {
198 const struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200199 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 +0100200 chunk_appendf(&trace_buf,
201 " error_code=%llu frame_type=%llu reason_phrase_len=%llu",
202 (ull)cc->error_code, (ull)cc->frame_type,
203 (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100204 if (plen)
205 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100206 break;
207 }
208 case QUIC_FT_CONNECTION_CLOSE_APP:
209 {
210 const struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200211 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 +0100212 chunk_appendf(&trace_buf,
213 " error_code=%llu reason_phrase_len=%llu",
214 (ull)cc->error_code, (ull)cc->reason_phrase_len);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100215 if (plen)
216 chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100217 break;
218 }
219 }
220}
221
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222/* Encode <frm> PADDING frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500223 * 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 +0100224 */
225static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
226 struct quic_frame *frm, struct quic_conn *conn)
227{
228 struct quic_padding *padding = &frm->padding;
229
230 if (end - *buf < padding->len - 1)
231 return 0;
232
233 memset(*buf, 0, padding->len - 1);
234 *buf += padding->len - 1;
235
236 return 1;
237}
238
239/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
240 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
241 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100242static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100243 const unsigned char **buf, const unsigned char *end)
244{
245 const unsigned char *beg;
246 struct quic_padding *padding = &frm->padding;
247
248 beg = *buf;
249 padding->len = 1;
250 while (*buf < end && !**buf)
251 (*buf)++;
252 padding->len += *buf - beg;
253
254 return 1;
255}
256
257/* Encode a ACK frame into <buf> buffer.
258 * Always succeeds.
259 */
260static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
261 struct quic_frame *frm, struct quic_conn *conn)
262{
263 /* No field */
264 return 1;
265}
266
267/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
268 * Always succeeds.
269 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100270static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100271 const unsigned char **buf, const unsigned char *end)
272{
273 /* No field */
274 return 1;
275}
276
277/* Encode a ACK frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500278 * 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 +0100279 */
280static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100281 struct quic_frame *frm, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100282{
283 struct quic_tx_ack *tx_ack = &frm->tx_ack;
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100284 struct eb64_node *ar, *prev_ar;
285 struct quic_arng_node *ar_node, *prev_ar_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100287 ar = eb64_last(&tx_ack->arngs->root);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200288 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200289 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100290 qc,, &ar_node->last, &ar_node->first.key);
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100291 if (!quic_enc_int(buf, end, ar_node->last) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292 !quic_enc_int(buf, end, tx_ack->ack_delay) ||
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100293 !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
294 !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100295 return 0;
296
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100297 while ((prev_ar = eb64_prev(ar))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200298 prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +0200299 TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,,
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100300 &prev_ar_node->last, &prev_ar_node->first.key);
301 if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
302 !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303 return 0;
304
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100305 ar = prev_ar;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +0200306 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100307 }
308
309 return 1;
310}
311
312/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
313 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
314 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100315static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 const unsigned char **buf, const unsigned char *end)
317{
318 int ret;
319 struct quic_ack *ack = &frm->ack;
320
321 ret = quic_dec_int(&ack->largest_ack, buf, end);
322 if (!ret)
323 return 0;
324
325 ret = quic_dec_int(&ack->ack_delay, buf, end);
326 if (!ret)
327 return 0;
328
329 ret = quic_dec_int(&ack->ack_range_num, buf, end);
330 if (!ret)
331 return 0;
332
333 ret = quic_dec_int(&ack->first_ack_range, buf, end);
334 if (!ret)
335 return 0;
336
337 return 1;
338}
339
340/* Encode a ACK_ECN frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500341 * 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 +0100342 */
343static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
344 struct quic_frame *frm, struct quic_conn *conn)
345{
346 struct quic_ack *ack = &frm->ack;
347
348 return quic_enc_int(buf, end, ack->largest_ack) &&
349 quic_enc_int(buf, end, ack->ack_delay) &&
350 quic_enc_int(buf, end, ack->first_ack_range) &&
351 quic_enc_int(buf, end, ack->ack_range_num);
352}
353
354/* Parse an ACK_ECN 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_ack_ecn_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_ack *ack = &frm->ack;
361
362 return quic_dec_int(&ack->largest_ack, buf, end) &&
363 quic_dec_int(&ack->ack_delay, buf, end) &&
364 quic_dec_int(&ack->first_ack_range, buf, end) &&
365 quic_dec_int(&ack->ack_range_num, buf, end);
366}
367
368/* Encode a RESET_STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500369 * 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 +0100370 */
371static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
372 struct quic_frame *frm, struct quic_conn *conn)
373{
374 struct quic_reset_stream *reset_stream = &frm->reset_stream;
375
376 return quic_enc_int(buf, end, reset_stream->id) &&
377 quic_enc_int(buf, end, reset_stream->app_error_code) &&
378 quic_enc_int(buf, end, reset_stream->final_size);
379}
380
381/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
382 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
383 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100384static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100385 const unsigned char **buf, const unsigned char *end)
386{
387 struct quic_reset_stream *reset_stream = &frm->reset_stream;
388
389 return quic_dec_int(&reset_stream->id, buf, end) &&
390 quic_dec_int(&reset_stream->app_error_code, buf, end) &&
391 quic_dec_int(&reset_stream->final_size, buf, end);
392}
393
394/* Encode a STOP_SENDING frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500395 * 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 +0100396 */
397static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
398 struct quic_frame *frm, struct quic_conn *conn)
399{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100400 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100402 return quic_enc_int(buf, end, stop_sending->id) &&
403 quic_enc_int(buf, end, stop_sending->app_error_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404}
405
406/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
407 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
408 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100409static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100410 const unsigned char **buf, const unsigned char *end)
411{
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100412 struct quic_stop_sending *stop_sending = &frm->stop_sending;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413
Frédéric Lécaille1d2faa22021-12-10 14:42:02 +0100414 return quic_dec_int(&stop_sending->id, buf, end) &&
415 quic_dec_int(&stop_sending->app_error_code, buf, end);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416}
417
418/* Encode a CRYPTO frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500419 * 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 +0100420 */
421static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
422 struct quic_frame *frm, struct quic_conn *conn)
423{
424 struct quic_crypto *crypto = &frm->crypto;
425 const struct quic_enc_level *qel = crypto->qel;
426 size_t offset, len;
427
428 if (!quic_enc_int(buf, end, crypto->offset) ||
429 !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
430 return 0;
431
432 len = crypto->len;
433 offset = crypto->offset;
434 while (len) {
435 int idx;
436 size_t to_copy;
437 const unsigned char *data;
438
439 idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
440 to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
441 if (to_copy > len)
442 to_copy = len;
443 data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
444 memcpy(*buf, data, to_copy);
445 *buf += to_copy;
446 offset += to_copy;
447 len -= to_copy;
448 }
449
450 return 1;
451}
452
453/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
454 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
455 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100456static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100457 const unsigned char **buf, const unsigned char *end)
458{
459 struct quic_crypto *crypto = &frm->crypto;
460
461 if (!quic_dec_int(&crypto->offset, buf, end) ||
462 !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
463 return 0;
464
465 crypto->data = *buf;
466 *buf += crypto->len;
467
468 return 1;
469}
470
471/* Encode a NEW_TOKEN frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500472 * 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 +0100473 */
474static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
475 struct quic_frame *frm, struct quic_conn *conn)
476{
477 struct quic_new_token *new_token = &frm->new_token;
478
479 if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
480 return 0;
481
482 memcpy(*buf, new_token->data, new_token->len);
483
484 return 1;
485}
486
487/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
488 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
489 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100490static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100491 const unsigned char **buf, const unsigned char *end)
492{
493 struct quic_new_token *new_token = &frm->new_token;
494
495 if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
496 return 0;
497
498 new_token->data = *buf;
499 *buf += new_token->len;
500
501 return 1;
502}
503
504/* Encode a STREAM frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500505 * 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 +0100506 */
507static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
508 struct quic_frame *frm, struct quic_conn *conn)
509{
510 struct quic_stream *stream = &frm->stream;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100511 const unsigned char *wrap;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512
Amaury Denoyelle1dac0182023-02-02 16:45:07 +0100513 /* Caller must set OFF bit if and only if a non-null offset is used. */
514 BUG_ON(!!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) !=
515 !!stream->offset.key);
516
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 if (!quic_enc_int(buf, end, stream->id) ||
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200518 ((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 +0100519 ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100520 (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
521 return 0;
522
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100523 wrap = (const unsigned char *)b_wrap(stream->buf);
524 if (stream->data + stream->len > wrap) {
525 size_t to_copy = wrap - stream->data;
526 memcpy(*buf, stream->data, to_copy);
527 *buf += to_copy;
528
529 to_copy = stream->len - to_copy;
530 memcpy(*buf, b_orig(stream->buf), to_copy);
531 *buf += to_copy;
532 }
533 else {
534 memcpy(*buf, stream->data, stream->len);
535 *buf += stream->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200536 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100537
538 return 1;
539}
540
541/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
542 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
543 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100544static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545 const unsigned char **buf, const unsigned char *end)
546{
547 struct quic_stream *stream = &frm->stream;
548
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100549 if (!quic_dec_int(&stream->id, buf, end))
550 return 0;
551
552 /* Offset parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100553 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200554 stream->offset.key = 0;
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100555 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200556 else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100557 return 0;
558
559 /* Length parsing */
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100560 if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
Frédéric Lécaille129a3512020-12-31 10:57:04 +0100561 stream->len = end - *buf;
562 }
563 else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100564 return 0;
565
566 stream->data = *buf;
567 *buf += stream->len;
568
569 return 1;
570}
571
572/* Encode a MAX_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500573 * 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 +0100574 */
575static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
576 struct quic_frame *frm, struct quic_conn *conn)
577{
578 struct quic_max_data *max_data = &frm->max_data;
579
580 return quic_enc_int(buf, end, max_data->max_data);
581}
582
583/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
584 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
585 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100586static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 const unsigned char **buf, const unsigned char *end)
588{
589 struct quic_max_data *max_data = &frm->max_data;
590
591 return quic_dec_int(&max_data->max_data, buf, end);
592}
593
594/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500595 * 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 +0100596 */
597static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
598 struct quic_frame *frm, struct quic_conn *conn)
599{
600 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
601
602 return quic_enc_int(buf, end, max_stream_data->id) &&
603 quic_enc_int(buf, end, max_stream_data->max_stream_data);
604}
605
606/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
607 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
608 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100609static 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 +0100610 const unsigned char **buf, const unsigned char *end)
611{
612 struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;
613
614 return quic_dec_int(&max_stream_data->id, buf, end) &&
615 quic_dec_int(&max_stream_data->max_stream_data, buf, end);
616}
617
618/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500619 * 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 +0100620 */
621static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
622 struct quic_frame *frm, struct quic_conn *conn)
623{
624 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
625
626 return quic_enc_int(buf, end, max_streams_bidi->max_streams);
627}
628
629/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
630 * as end into <frm> frame.
631 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
632 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100633static 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 +0100634 const unsigned char **buf, const unsigned char *end)
635{
636 struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;
637
638 return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
639}
640
641/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500642 * 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 +0100643 */
644static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
645 struct quic_frame *frm, struct quic_conn *conn)
646{
647 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
648
649 return quic_enc_int(buf, end, max_streams_uni->max_streams);
650}
651
652/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
653 * as end into <frm> frame.
654 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
655 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100656static 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 +0100657 const unsigned char **buf, const unsigned char *end)
658{
659 struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;
660
661 return quic_dec_int(&max_streams_uni->max_streams, buf, end);
662}
663
664/* Encode a DATA_BLOCKED frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500665 * 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 +0100666 */
667static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
668 struct quic_frame *frm, struct quic_conn *conn)
669{
670 struct quic_data_blocked *data_blocked = &frm->data_blocked;
671
672 return quic_enc_int(buf, end, data_blocked->limit);
673}
674
675/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
676 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
677 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100678static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679 const unsigned char **buf, const unsigned char *end)
680{
681 struct quic_data_blocked *data_blocked = &frm->data_blocked;
682
683 return quic_dec_int(&data_blocked->limit, buf, end);
684}
685
686/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500687 * 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 +0100688 */
689static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
690 struct quic_frame *frm, struct quic_conn *conn)
691{
692 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
693
694 return quic_enc_int(buf, end, stream_data_blocked->id) &&
695 quic_enc_int(buf, end, stream_data_blocked->limit);
696}
697
698/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
699 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
700 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100701static 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 +0100702 const unsigned char **buf, const unsigned char *end)
703{
704 struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
705
706 return quic_dec_int(&stream_data_blocked->id, buf, end) &&
707 quic_dec_int(&stream_data_blocked->limit, buf, end);
708}
709
710/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500711 * 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 +0100712 */
713static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
714 struct quic_frame *frm, struct quic_conn *conn)
715{
716 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
717
718 return quic_enc_int(buf, end, streams_blocked_bidi->limit);
719}
720
721/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
722 * as end into <frm> frame.
723 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
724 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100725static 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 +0100726 const unsigned char **buf, const unsigned char *end)
727{
728 struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
729
730 return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
731}
732
733/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500734 * 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 +0100735 */
736static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
737 struct quic_frame *frm, struct quic_conn *conn)
738{
739 struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
740
741 return quic_enc_int(buf, end, streams_blocked_uni->limit);
742}
743
744/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
745 * 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_streams_blocked_uni_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_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
752
753 return quic_dec_int(&streams_blocked_uni->limit, buf, end);
754}
755
756/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500757 * 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 +0100758 */
759static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
760 struct quic_frame *frm, struct quic_conn *conn)
761{
762 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
763
764 if (!quic_enc_int(buf, end, new_cid->seq_num) ||
765 !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
766 end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
767 return 0;
768
769 *(*buf)++ = new_cid->cid.len;
770
771 if (new_cid->cid.len) {
772 memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
773 *buf += new_cid->cid.len;
774 }
775 memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
776 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
777
778 return 1;
779}
780
781/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
782 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
783 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100784static 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 +0100785 const unsigned char **buf, const unsigned char *end)
786{
787 struct quic_new_connection_id *new_cid = &frm->new_connection_id;
788
789 if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
790 !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
791 return 0;
792
793 new_cid->cid.len = *(*buf)++;
794 if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
795 return 0;
796
797 if (new_cid->cid.len) {
798 new_cid->cid.data = *buf;
799 *buf += new_cid->cid.len;
800 }
801 new_cid->stateless_reset_token = *buf;
802 *buf += QUIC_STATELESS_RESET_TOKEN_LEN;
803
804 return 1;
805}
806
807/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500808 * 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 +0100809 */
810static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
811 struct quic_frame *frm, struct quic_conn *conn)
812{
813 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
814
815 return quic_enc_int(buf, end, retire_connection_id->seq_num);
816}
817
818/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
819 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
820 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100821static 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 +0100822 const unsigned char **buf, const unsigned char *end)
823{
824 struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
825
826 return quic_dec_int(&retire_connection_id->seq_num, buf, end);
827}
828
829/* Encode a PATH_CHALLENGE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500830 * 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 +0100831 */
832static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
833 struct quic_frame *frm, struct quic_conn *conn)
834{
835 struct quic_path_challenge *path_challenge = &frm->path_challenge;
836
837 if (end - *buf < sizeof path_challenge->data)
838 return 0;
839
840 memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
841 *buf += sizeof path_challenge->data;
842
843 return 1;
844}
845
846/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
847 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
848 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100849static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100850 const unsigned char **buf, const unsigned char *end)
851{
852 struct quic_path_challenge *path_challenge = &frm->path_challenge;
853
854 if (end - *buf < sizeof path_challenge->data)
855 return 0;
856
857 memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
858 *buf += sizeof path_challenge->data;
859
860 return 1;
861}
862
863
864/* Encode a PATH_RESPONSE frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500865 * 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 +0100866 */
867static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
868 struct quic_frame *frm, struct quic_conn *conn)
869{
870 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
871
872 if (end - *buf < sizeof path_challenge_response->data)
873 return 0;
874
875 memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
876 *buf += sizeof path_challenge_response->data;
877
878 return 1;
879}
880
881/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
882 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
883 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100884static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100885 const unsigned char **buf, const unsigned char *end)
886{
887 struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
888
889 if (end - *buf < sizeof path_challenge_response->data)
890 return 0;
891
892 memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
893 *buf += sizeof path_challenge_response->data;
894
895 return 1;
896}
897
898/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
899 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
900 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500901 * 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 +0100902 */
903static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
904 struct quic_frame *frm, struct quic_conn *conn)
905{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100906 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100907
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100908 if (!quic_enc_int(buf, end, cc->error_code) ||
909 !quic_enc_int(buf, end, cc->frame_type) ||
910 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
911 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100912 return 0;
913
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100914 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
915 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100916
917 return 1;
918}
919
920/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
921 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
922 * and another at QUIC layer.
923 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
924 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100925static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926 const unsigned char **buf, const unsigned char *end)
927{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100928 size_t plen;
929 struct quic_connection_close *cc = &frm->connection_close;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100931 if (!quic_dec_int(&cc->error_code, buf, end) ||
932 !quic_dec_int(&cc->frame_type, buf, end) ||
933 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
934 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 return 0;
936
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200937 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100938 memcpy(cc->reason_phrase, *buf, plen);
939 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940
941 return 1;
942}
943
944/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
945 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
946 * and another at QUIC layer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500947 * 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 +0100948 */
949static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
950 struct quic_frame *frm, struct quic_conn *conn)
951{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100952 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100953
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100954 if (!quic_enc_int(buf, end, cc->error_code) ||
955 !quic_enc_int(buf, end, cc->reason_phrase_len) ||
956 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100957 return 0;
958
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100959 memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
960 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100961
962 return 1;
963}
964
965/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
966 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
967 * and another at QUIC layer.
968 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
969 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +0100970static 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 +0100971 const unsigned char **buf, const unsigned char *end)
972{
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100973 size_t plen;
974 struct quic_connection_close_app *cc = &frm->connection_close_app;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100975
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100976 if (!quic_dec_int(&cc->error_code, buf, end) ||
977 !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
978 end - *buf < cc->reason_phrase_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100979 return 0;
980
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200981 plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
Frédéric Lécaille010e5322021-12-23 15:19:15 +0100982 memcpy(cc->reason_phrase, *buf, plen);
983 *buf += cc->reason_phrase_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100984
985 return 1;
986}
987
988/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
989 * Always succeeds.
990 */
991static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
992 struct quic_frame *frm, struct quic_conn *conn)
993{
994 /* No field */
995 return 1;
996}
997
998/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
999 * Always succeed.
1000 */
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001001static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001002 const unsigned char **buf, const unsigned char *end)
1003{
1004 /* No field */
1005 return 1;
1006}
1007
1008struct quic_frame_builder {
1009 int (*func)(unsigned char **buf, const unsigned char *end,
1010 struct quic_frame *frm, struct quic_conn *conn);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001011 uint32_t mask;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012 unsigned char flags;
1013};
1014
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001015const struct quic_frame_builder quic_frame_builders[] = {
Frédéric Lécaille156a59b2021-09-17 16:51:51 +02001016 [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1017 [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 +02001018 [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1019 [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 +02001020 [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1021 [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1022 [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1023 [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1024 [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1025 [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1026 [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1027 [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1028 [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1029 [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1030 [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1031 [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1032 [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1033 [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, },
1034 [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, },
1035 [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, },
1036 [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1037 [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, },
1038 [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, },
1039 [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, },
1040 [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, },
1041 [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, },
1042 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1043 [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 +02001044 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1045 [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 +02001046 [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 +01001047};
1048
1049struct quic_frame_parser {
Frédéric Lécaille50044ad2020-12-29 11:42:08 +01001050 int (*func)(struct quic_frame *frm, struct quic_conn *qc,
1051 const unsigned char **buf, const unsigned char *end);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001052 uint32_t mask;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001053 unsigned char flags;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001054};
1055
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001056const struct quic_frame_parser quic_frame_parsers[] = {
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001057 [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1058 [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1059 [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1060 [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1061 [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1062 [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1063 [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
1064 [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
1065 [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1066 [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1067 [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1068 [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1069 [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1070 [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1071 [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1072 [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1073 [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1074 [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, },
1075 [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, },
1076 [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, },
1077 [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1078 [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, },
1079 [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, },
1080 [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, },
1081 [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, },
1082 [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, },
1083 [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1084 [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1085 [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
1086 [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
1087 [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 +01001088};
1089
1090/* Decode a QUIC frame from <buf> buffer into <frm> frame.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001091 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001092 */
1093int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
1094 const unsigned char **buf, const unsigned char *end,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001095 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001097 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001098 const struct quic_frame_parser *parser;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001100 TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 if (end <= *buf) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001102 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001103 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104 }
1105
1106 frm->type = *(*buf)++;
Frédéric Lécaille0c80e692022-02-15 10:27:34 +01001107 if (frm->type >= QUIC_FT_MAX) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001108 TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001109 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001110 }
1111
1112 parser = &quic_frame_parsers[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001113 if (!(parser->mask & (1U << pkt->type))) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001114 TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001115 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116 }
1117
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001118 TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
1119 if (!parser->func(frm, qc, buf, end)) {
1120 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001121 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122 }
1123
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001124 pkt->flags |= parser->flags;
1125
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001126 ret = 1;
1127 leave:
1128 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1129 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001130}
1131
1132/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001133 * 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 +02001134 * The buffer is updated to point to one byte past the end of the built frame
1135 * only if succeeded.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001136 */
1137int qc_build_frm(unsigned char **buf, const unsigned char *end,
1138 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001139 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001140{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001141 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001142 const struct quic_frame_builder *builder;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001143 unsigned char *pos = *buf;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001144
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001145 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001146 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001147 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001148 /* 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 +02001149 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001150 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001151 }
1152
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001153 if (end <= pos) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001154 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001155 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001156 }
1157
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001158 TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001159 *pos++ = frm->type;
1160 if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001161 TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001162 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001163 }
1164
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001165 pkt->flags |= builder->flags;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001166 *buf = pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001167
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001168 ret = 1;
1169 leave:
1170 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1171 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001172}
1173
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001174/* Detach all duplicated frames from <frm> reflist. */
1175void qc_frm_unref(struct quic_frame *frm, struct quic_conn *qc)
1176{
1177 struct quic_frame *f, *tmp;
1178
1179 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1180
1181 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1182 f->origin = NULL;
1183 LIST_DEL_INIT(&f->ref);
1184 if (f->pkt) {
1185 TRACE_DEVEL("remove frame reference",
1186 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1187 }
1188 else {
1189 TRACE_DEVEL("remove frame reference for unsent frame",
1190 QUIC_EV_CONN_PRSAFRM, qc, f);
1191 }
1192 }
1193
1194 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1195}