blob: 1f98d30f4f151fddd8b44a71ac427694b4be3ecb [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:
Frédéric Lécaille904caac2023-03-06 15:34:19 +010034 return "ACK_ECN";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010035 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200112 const struct qf_crypto *cf = &frm->crypto;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100113 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
114 (ull)cf->offset, (ull)cf->len);
115 break;
116 }
117 case QUIC_FT_RESET_STREAM:
118 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200119 const struct qf_reset_stream *rs = &frm->reset_stream;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100120 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200126 const struct qf_stop_sending *s = &frm->stop_sending;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100127 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200133 const struct qf_stream *s = &frm->stream;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100134 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200142 const struct qf_max_data *s = &frm->max_data;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100143 chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
144 break;
145 }
146 case QUIC_FT_MAX_STREAM_DATA:
147 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200148 const struct qf_max_stream_data *s = &frm->max_stream_data;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100149 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200155 const struct qf_max_streams *s = &frm->max_streams_bidi;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100156 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
157 break;
158 }
159 case QUIC_FT_MAX_STREAMS_UNI:
160 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200161 const struct qf_max_streams *s = &frm->max_streams_uni;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100162 chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
163 break;
164 }
165 case QUIC_FT_DATA_BLOCKED:
166 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200167 const struct qf_data_blocked *s = &frm->data_blocked;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100168 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
169 break;
170 }
171 case QUIC_FT_STREAM_DATA_BLOCKED:
172 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200173 const struct qf_stream_data_blocked *s = &frm->stream_data_blocked;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100174 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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200180 const struct qf_streams_blocked *s = &frm->streams_blocked_bidi;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100181 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
182 break;
183 }
184 case QUIC_FT_STREAMS_BLOCKED_UNI:
185 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200186 const struct qf_streams_blocked *s = &frm->streams_blocked_uni;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100187 chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
188 break;
189 }
190 case QUIC_FT_RETIRE_CONNECTION_ID:
191 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200192 const struct qf_retire_connection_id *rci = &frm->retire_connection_id;
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100193 chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
194 break;
195 }
196 case QUIC_FT_CONNECTION_CLOSE:
197 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200198 const struct qf_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 {
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200210 const struct qf_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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200228 struct qf_padding *padding = &frm->padding;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229
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;
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200246 struct qf_padding *padding = &frm->padding;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100247
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200283 struct qf_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écaille8f991942023-03-24 15:14:45 +0100289 TRACE_PROTO("TX 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écaille8f991942023-03-24 15:14:45 +0100299 TRACE_PROTO("TX 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;
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200319 struct qf_ack *ack = &frm->ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200346 struct qf_ack *ack = &frm->ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100347
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200360 struct qf_ack *ack = &frm->ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100361
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200374 struct qf_reset_stream *reset_stream = &frm->reset_stream;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100375
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200387 struct qf_reset_stream *reset_stream = &frm->reset_stream;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100388
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200400 struct qf_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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200412 struct qf_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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200424 struct qf_crypto *crypto = &frm->crypto;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100425 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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200459 struct qf_crypto *crypto = &frm->crypto;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100460
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200477 struct qf_new_token *new_token = &frm->new_token;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100478
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200493 struct qf_new_token *new_token = &frm->new_token;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200510 struct qf_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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200547 struct qf_stream *stream = &frm->stream;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100548
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200578 struct qf_max_data *max_data = &frm->max_data;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100579
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200589 struct qf_max_data *max_data = &frm->max_data;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100590
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200600 struct qf_max_stream_data *max_stream_data = &frm->max_stream_data;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100601
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200612 struct qf_max_stream_data *max_stream_data = &frm->max_stream_data;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200624 struct qf_max_streams *max_streams_bidi = &frm->max_streams_bidi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100625
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200636 struct qf_max_streams *max_streams_bidi = &frm->max_streams_bidi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200647 struct qf_max_streams *max_streams_uni = &frm->max_streams_uni;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100648
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200659 struct qf_max_streams *max_streams_uni = &frm->max_streams_uni;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100660
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200670 struct qf_data_blocked *data_blocked = &frm->data_blocked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200681 struct qf_data_blocked *data_blocked = &frm->data_blocked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100682
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200692 struct qf_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100693
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200704 struct qf_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100705
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200716 struct qf_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100717
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200728 struct qf_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100729
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200739 struct qf_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100740
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200751 struct qf_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100752
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200762 struct qf_new_connection_id *new_cid = &frm->new_connection_id;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100763
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200787 struct qf_new_connection_id *new_cid = &frm->new_connection_id;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100788
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200813 struct qf_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100814
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200824 struct qf_retire_connection_id *retire_connection_id = &frm->retire_connection_id;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100825
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200835 struct qf_path_challenge *path_challenge = &frm->path_challenge;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200852 struct qf_path_challenge *path_challenge = &frm->path_challenge;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100853
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200870 struct qf_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100871
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200887 struct qf_path_challenge_response *path_challenge_response = &frm->path_challenge_response;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100888
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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200906 struct qf_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;
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200929 struct qf_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{
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200952 struct qf_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;
Amaury Denoyelle888c5f22023-04-24 14:26:30 +0200974 struct qf_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 if (!parser->func(frm, qc, buf, end)) {
1119 TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001120 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121 }
1122
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001123 TRACE_PROTO("RX frm", QUIC_EV_CONN_PSTRM, qc, frm);
1124
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001125 pkt->flags |= parser->flags;
1126
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001127 ret = 1;
1128 leave:
1129 TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
1130 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001131}
1132
1133/* Encode <frm> QUIC frame into <buf> buffer.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001134 * 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 +02001135 * The buffer is updated to point to one byte past the end of the built frame
1136 * only if succeeded.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001137 */
1138int qc_build_frm(unsigned char **buf, const unsigned char *end,
1139 struct quic_frame *frm, struct quic_tx_packet *pkt,
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001140 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141{
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001142 int ret = 0;
Frédéric Lécaille2cb130c2021-09-17 17:05:44 +02001143 const struct quic_frame_builder *builder;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001144 unsigned char *pos = *buf;
Frédéric Lécaillef7fe9652020-12-09 14:56:18 +01001145
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001146 TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001147 builder = &quic_frame_builders[frm->type];
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001148 if (!(builder->mask & (1U << pkt->type))) {
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001149 /* 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 +02001150 TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillece2ecc92022-02-02 14:37:37 +01001151 BUG_ON(!(builder->mask & (1U << pkt->type)));
Frédéric Lécaillef5821dc2021-07-30 14:42:33 +02001152 }
1153
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001154 if (end <= pos) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001155 TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001156 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001157 }
1158
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001159 TRACE_PROTO("TX frm", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001160 *pos++ = frm->type;
1161 if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
Frédéric Lécaille8f991942023-03-24 15:14:45 +01001162 TRACE_ERROR("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001163 goto leave;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164 }
1165
Frédéric Lécailledc2593e2021-09-17 16:57:14 +02001166 pkt->flags |= builder->flags;
Frédéric Lécailleb8047de2022-08-23 17:40:09 +02001167 *buf = pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001168
Frédéric Lécaillea8b2f842022-08-10 17:56:45 +02001169 ret = 1;
1170 leave:
1171 TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
1172 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001173}
1174
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001175/* Detach all duplicated frames from <frm> reflist. */
1176void qc_frm_unref(struct quic_frame *frm, struct quic_conn *qc)
1177{
1178 struct quic_frame *f, *tmp;
1179
1180 TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
1181
1182 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1183 f->origin = NULL;
1184 LIST_DEL_INIT(&f->ref);
1185 if (f->pkt) {
1186 TRACE_DEVEL("remove frame reference",
1187 QUIC_EV_CONN_PRSAFRM, qc, f, &f->pkt->pn_node.key);
1188 }
1189 else {
1190 TRACE_DEVEL("remove frame reference for unsent frame",
1191 QUIC_EV_CONN_PRSAFRM, qc, f);
1192 }
1193 }
1194
1195 TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
1196}