Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 2 | * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | */ |
| 9 | |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 12 | #include <import/eb64tree.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 13 | #include <haproxy/quic_conn-t.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 14 | #include <haproxy/quic_enc.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 15 | #include <haproxy/quic_frame.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 16 | #include <haproxy/quic_tp-t.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 17 | #include <haproxy/trace.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 18 | |
| 19 | #define TRACE_SOURCE &trace_quic |
| 20 | |
| 21 | const char *quic_frame_type_string(enum quic_frame_type ft) |
| 22 | { |
| 23 | switch (ft) { |
| 24 | case QUIC_FT_PADDING: |
| 25 | return "PADDING"; |
| 26 | case QUIC_FT_PING: |
| 27 | return "PING"; |
| 28 | case QUIC_FT_ACK: |
| 29 | return "ACK"; |
| 30 | case QUIC_FT_ACK_ECN: |
| 31 | return "ACK_ENC"; |
| 32 | case QUIC_FT_RESET_STREAM: |
| 33 | return "RESET_STREAM"; |
| 34 | case QUIC_FT_STOP_SENDING: |
| 35 | return "STOP_SENDING"; |
| 36 | case QUIC_FT_CRYPTO: |
| 37 | return "CRYPTO"; |
| 38 | case QUIC_FT_NEW_TOKEN: |
| 39 | return "NEW_TOKEN"; |
| 40 | |
| 41 | case QUIC_FT_STREAM_8: |
| 42 | return "STREAM_8"; |
| 43 | case QUIC_FT_STREAM_9: |
| 44 | return "STREAM_9"; |
| 45 | case QUIC_FT_STREAM_A: |
| 46 | return "STREAM_A"; |
| 47 | case QUIC_FT_STREAM_B: |
| 48 | return "STREAM_B"; |
| 49 | case QUIC_FT_STREAM_C: |
| 50 | return "STREAM_C"; |
| 51 | case QUIC_FT_STREAM_D: |
| 52 | return "STREAM_D"; |
| 53 | case QUIC_FT_STREAM_E: |
| 54 | return "STREAM_E"; |
| 55 | case QUIC_FT_STREAM_F: |
| 56 | return "STREAM_F"; |
| 57 | |
| 58 | case QUIC_FT_MAX_DATA: |
| 59 | return "MAX_DATA"; |
| 60 | case QUIC_FT_MAX_STREAM_DATA: |
| 61 | return "MAX_STREAM_DATA"; |
| 62 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 63 | return "MAX_STREAMS_BIDI"; |
| 64 | case QUIC_FT_MAX_STREAMS_UNI: |
| 65 | return "MAX_STREAMS_UNI"; |
| 66 | case QUIC_FT_DATA_BLOCKED: |
| 67 | return "DATA_BLOCKED"; |
| 68 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 69 | return "STREAM_DATA_BLOCKED"; |
| 70 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 71 | return "STREAMS_BLOCKED_BIDI"; |
| 72 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 73 | return "STREAMS_BLOCKED_UNI"; |
| 74 | case QUIC_FT_NEW_CONNECTION_ID: |
| 75 | return "NEW_CONNECTION_ID"; |
| 76 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 77 | return "RETIRE_CONNECTION_ID"; |
| 78 | case QUIC_FT_PATH_CHALLENGE: |
| 79 | return "PATH_CHALLENGE"; |
| 80 | case QUIC_FT_PATH_RESPONSE: |
| 81 | return "PATH_RESPONSE"; |
| 82 | case QUIC_FT_CONNECTION_CLOSE: |
| 83 | return "CONNECTION_CLOSE"; |
| 84 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 85 | return "CONNECTION_CLOSE_APP"; |
| 86 | case QUIC_FT_HANDSHAKE_DONE: |
| 87 | return "HANDSHAKE_DONE"; |
| 88 | default: |
| 89 | return "UNKNOWN"; |
| 90 | } |
| 91 | } |
| 92 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 93 | static void chunk_cc_phrase_appendf(struct buffer *buf, |
| 94 | const unsigned char *phr, size_t phrlen) |
| 95 | { |
| 96 | chunk_appendf(buf, " reason_phrase: '"); |
| 97 | while (phrlen--) |
| 98 | chunk_appendf(buf, "%c", *phr++); |
| 99 | chunk_appendf(buf, "'"); |
| 100 | } |
| 101 | |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 102 | /* Add traces to <buf> depending on <frm> frame type. */ |
| 103 | void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm) |
| 104 | { |
| 105 | chunk_appendf(buf, " %s", quic_frame_type_string(frm->type)); |
| 106 | switch (frm->type) { |
| 107 | case QUIC_FT_CRYPTO: |
| 108 | { |
| 109 | const struct quic_crypto *cf = &frm->crypto; |
| 110 | chunk_appendf(buf, " cfoff=%llu cflen=%llu", |
| 111 | (ull)cf->offset, (ull)cf->len); |
| 112 | break; |
| 113 | } |
| 114 | case QUIC_FT_RESET_STREAM: |
| 115 | { |
| 116 | const struct quic_reset_stream *rs = &frm->reset_stream; |
| 117 | chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu", |
| 118 | (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size); |
| 119 | break; |
| 120 | } |
| 121 | case QUIC_FT_STOP_SENDING: |
| 122 | { |
| 123 | const struct quic_stop_sending *s = &frm->stop_sending; |
| 124 | chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu", |
| 125 | (ull)s->id, (ull)s->app_error_code); |
| 126 | break; |
| 127 | } |
| 128 | case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F: |
| 129 | { |
| 130 | const struct quic_stream *s = &frm->stream; |
| 131 | chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu", |
| 132 | !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT), |
| 133 | !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT), |
| 134 | (ull)s->id, (ull)s->offset.key, (ull)s->len); |
| 135 | break; |
| 136 | } |
| 137 | case QUIC_FT_MAX_DATA: |
| 138 | { |
| 139 | const struct quic_max_data *s = &frm->max_data; |
| 140 | chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data); |
| 141 | break; |
| 142 | } |
| 143 | case QUIC_FT_MAX_STREAM_DATA: |
| 144 | { |
| 145 | const struct quic_max_stream_data *s = &frm->max_stream_data; |
| 146 | chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu", |
| 147 | (ull)s->id, (ull)s->max_stream_data); |
| 148 | break; |
| 149 | } |
| 150 | case QUIC_FT_MAX_STREAMS_BIDI: |
| 151 | { |
| 152 | const struct quic_max_streams *s = &frm->max_streams_bidi; |
| 153 | chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams); |
| 154 | break; |
| 155 | } |
| 156 | case QUIC_FT_MAX_STREAMS_UNI: |
| 157 | { |
| 158 | const struct quic_max_streams *s = &frm->max_streams_uni; |
| 159 | chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams); |
| 160 | break; |
| 161 | } |
| 162 | case QUIC_FT_DATA_BLOCKED: |
| 163 | { |
| 164 | const struct quic_data_blocked *s = &frm->data_blocked; |
| 165 | chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit); |
| 166 | break; |
| 167 | } |
| 168 | case QUIC_FT_STREAM_DATA_BLOCKED: |
| 169 | { |
| 170 | const struct quic_stream_data_blocked *s = &frm->stream_data_blocked; |
| 171 | chunk_appendf(&trace_buf, " id=%llu limit=%llu", |
| 172 | (ull)s->id, (ull)s->limit); |
| 173 | break; |
| 174 | } |
| 175 | case QUIC_FT_STREAMS_BLOCKED_BIDI: |
| 176 | { |
| 177 | const struct quic_streams_blocked *s = &frm->streams_blocked_bidi; |
| 178 | chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit); |
| 179 | break; |
| 180 | } |
| 181 | case QUIC_FT_STREAMS_BLOCKED_UNI: |
| 182 | { |
| 183 | const struct quic_streams_blocked *s = &frm->streams_blocked_uni; |
| 184 | chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit); |
| 185 | break; |
| 186 | } |
| 187 | case QUIC_FT_RETIRE_CONNECTION_ID: |
| 188 | { |
| 189 | const struct quic_retire_connection_id *rci = &frm->retire_connection_id; |
| 190 | chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num); |
| 191 | break; |
| 192 | } |
| 193 | case QUIC_FT_CONNECTION_CLOSE: |
| 194 | { |
| 195 | const struct quic_connection_close *cc = &frm->connection_close; |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 196 | size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase); |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 197 | chunk_appendf(&trace_buf, |
| 198 | " error_code=%llu frame_type=%llu reason_phrase_len=%llu", |
| 199 | (ull)cc->error_code, (ull)cc->frame_type, |
| 200 | (ull)cc->reason_phrase_len); |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 201 | if (plen) |
| 202 | chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen); |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | case QUIC_FT_CONNECTION_CLOSE_APP: |
| 206 | { |
| 207 | const struct quic_connection_close_app *cc = &frm->connection_close_app; |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 208 | size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase); |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 209 | chunk_appendf(&trace_buf, |
| 210 | " error_code=%llu reason_phrase_len=%llu", |
| 211 | (ull)cc->error_code, (ull)cc->reason_phrase_len); |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 212 | if (plen) |
| 213 | chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen); |
Frédéric Lécaille | 1ede823 | 2021-12-23 14:11:25 +0100 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 219 | /* Encode <frm> PADDING frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 220 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 221 | */ |
| 222 | static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end, |
| 223 | struct quic_frame *frm, struct quic_conn *conn) |
| 224 | { |
| 225 | struct quic_padding *padding = &frm->padding; |
| 226 | |
| 227 | if (end - *buf < padding->len - 1) |
| 228 | return 0; |
| 229 | |
| 230 | memset(*buf, 0, padding->len - 1); |
| 231 | *buf += padding->len - 1; |
| 232 | |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | /* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame. |
| 237 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 238 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 239 | static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 240 | const unsigned char **buf, const unsigned char *end) |
| 241 | { |
| 242 | const unsigned char *beg; |
| 243 | struct quic_padding *padding = &frm->padding; |
| 244 | |
| 245 | beg = *buf; |
| 246 | padding->len = 1; |
| 247 | while (*buf < end && !**buf) |
| 248 | (*buf)++; |
| 249 | padding->len += *buf - beg; |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | /* Encode a ACK frame into <buf> buffer. |
| 255 | * Always succeeds. |
| 256 | */ |
| 257 | static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end, |
| 258 | struct quic_frame *frm, struct quic_conn *conn) |
| 259 | { |
| 260 | /* No field */ |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | /* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame. |
| 265 | * Always succeeds. |
| 266 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 267 | static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 268 | const unsigned char **buf, const unsigned char *end) |
| 269 | { |
| 270 | /* No field */ |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | /* Encode a ACK frame. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 275 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 276 | */ |
| 277 | static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 278 | struct quic_frame *frm, struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 279 | { |
| 280 | struct quic_tx_ack *tx_ack = &frm->tx_ack; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 281 | struct eb64_node *ar, *prev_ar; |
| 282 | struct quic_arng_node *ar_node, *prev_ar_node; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 283 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 284 | ar = eb64_last(&tx_ack->arngs->root); |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 285 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 286 | TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 287 | qc,, &ar_node->last, &ar_node->first.key); |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 288 | if (!quic_enc_int(buf, end, ar_node->last) || |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 289 | !quic_enc_int(buf, end, tx_ack->ack_delay) || |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 290 | !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) || |
| 291 | !quic_enc_int(buf, end, ar_node->last - ar_node->first.key)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 294 | while ((prev_ar = eb64_prev(ar))) { |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 295 | prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 296 | TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,, |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 297 | &prev_ar_node->last, &prev_ar_node->first.key); |
| 298 | if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) || |
| 299 | !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key)) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 300 | return 0; |
| 301 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 302 | ar = prev_ar; |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 303 | ar_node = eb64_entry(ar, struct quic_arng_node, first); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | /* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame. |
| 310 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 311 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 312 | static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 313 | const unsigned char **buf, const unsigned char *end) |
| 314 | { |
| 315 | int ret; |
| 316 | struct quic_ack *ack = &frm->ack; |
| 317 | |
| 318 | ret = quic_dec_int(&ack->largest_ack, buf, end); |
| 319 | if (!ret) |
| 320 | return 0; |
| 321 | |
| 322 | ret = quic_dec_int(&ack->ack_delay, buf, end); |
| 323 | if (!ret) |
| 324 | return 0; |
| 325 | |
| 326 | ret = quic_dec_int(&ack->ack_range_num, buf, end); |
| 327 | if (!ret) |
| 328 | return 0; |
| 329 | |
| 330 | ret = quic_dec_int(&ack->first_ack_range, buf, end); |
| 331 | if (!ret) |
| 332 | return 0; |
| 333 | |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | /* Encode a ACK_ECN frame. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 338 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 339 | */ |
| 340 | static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end, |
| 341 | struct quic_frame *frm, struct quic_conn *conn) |
| 342 | { |
| 343 | struct quic_ack *ack = &frm->ack; |
| 344 | |
| 345 | return quic_enc_int(buf, end, ack->largest_ack) && |
| 346 | quic_enc_int(buf, end, ack->ack_delay) && |
| 347 | quic_enc_int(buf, end, ack->first_ack_range) && |
| 348 | quic_enc_int(buf, end, ack->ack_range_num); |
| 349 | } |
| 350 | |
| 351 | /* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame. |
| 352 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 353 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 354 | static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 355 | const unsigned char **buf, const unsigned char *end) |
| 356 | { |
| 357 | struct quic_ack *ack = &frm->ack; |
| 358 | |
| 359 | return quic_dec_int(&ack->largest_ack, buf, end) && |
| 360 | quic_dec_int(&ack->ack_delay, buf, end) && |
| 361 | quic_dec_int(&ack->first_ack_range, buf, end) && |
| 362 | quic_dec_int(&ack->ack_range_num, buf, end); |
| 363 | } |
| 364 | |
| 365 | /* Encode a RESET_STREAM frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 366 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 367 | */ |
| 368 | static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end, |
| 369 | struct quic_frame *frm, struct quic_conn *conn) |
| 370 | { |
| 371 | struct quic_reset_stream *reset_stream = &frm->reset_stream; |
| 372 | |
| 373 | return quic_enc_int(buf, end, reset_stream->id) && |
| 374 | quic_enc_int(buf, end, reset_stream->app_error_code) && |
| 375 | quic_enc_int(buf, end, reset_stream->final_size); |
| 376 | } |
| 377 | |
| 378 | /* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame. |
| 379 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 380 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 381 | static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 382 | const unsigned char **buf, const unsigned char *end) |
| 383 | { |
| 384 | struct quic_reset_stream *reset_stream = &frm->reset_stream; |
| 385 | |
| 386 | return quic_dec_int(&reset_stream->id, buf, end) && |
| 387 | quic_dec_int(&reset_stream->app_error_code, buf, end) && |
| 388 | quic_dec_int(&reset_stream->final_size, buf, end); |
| 389 | } |
| 390 | |
| 391 | /* Encode a STOP_SENDING frame. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 392 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 393 | */ |
| 394 | static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end, |
| 395 | struct quic_frame *frm, struct quic_conn *conn) |
| 396 | { |
Frédéric Lécaille | 1d2faa2 | 2021-12-10 14:42:02 +0100 | [diff] [blame] | 397 | struct quic_stop_sending *stop_sending = &frm->stop_sending; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 398 | |
Frédéric Lécaille | 1d2faa2 | 2021-12-10 14:42:02 +0100 | [diff] [blame] | 399 | return quic_enc_int(buf, end, stop_sending->id) && |
| 400 | quic_enc_int(buf, end, stop_sending->app_error_code); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame. |
| 404 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 405 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 406 | static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 407 | const unsigned char **buf, const unsigned char *end) |
| 408 | { |
Frédéric Lécaille | 1d2faa2 | 2021-12-10 14:42:02 +0100 | [diff] [blame] | 409 | struct quic_stop_sending *stop_sending = &frm->stop_sending; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 410 | |
Frédéric Lécaille | 1d2faa2 | 2021-12-10 14:42:02 +0100 | [diff] [blame] | 411 | return quic_dec_int(&stop_sending->id, buf, end) && |
| 412 | quic_dec_int(&stop_sending->app_error_code, buf, end); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* Encode a CRYPTO frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 416 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 417 | */ |
| 418 | static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end, |
| 419 | struct quic_frame *frm, struct quic_conn *conn) |
| 420 | { |
| 421 | struct quic_crypto *crypto = &frm->crypto; |
| 422 | const struct quic_enc_level *qel = crypto->qel; |
| 423 | size_t offset, len; |
| 424 | |
| 425 | if (!quic_enc_int(buf, end, crypto->offset) || |
| 426 | !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len) |
| 427 | return 0; |
| 428 | |
| 429 | len = crypto->len; |
| 430 | offset = crypto->offset; |
| 431 | while (len) { |
| 432 | int idx; |
| 433 | size_t to_copy; |
| 434 | const unsigned char *data; |
| 435 | |
| 436 | idx = offset >> QUIC_CRYPTO_BUF_SHIFT; |
| 437 | to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK); |
| 438 | if (to_copy > len) |
| 439 | to_copy = len; |
| 440 | data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK); |
| 441 | memcpy(*buf, data, to_copy); |
| 442 | *buf += to_copy; |
| 443 | offset += to_copy; |
| 444 | len -= to_copy; |
| 445 | } |
| 446 | |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | /* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame. |
| 451 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 452 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 453 | static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 454 | const unsigned char **buf, const unsigned char *end) |
| 455 | { |
| 456 | struct quic_crypto *crypto = &frm->crypto; |
| 457 | |
| 458 | if (!quic_dec_int(&crypto->offset, buf, end) || |
| 459 | !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len) |
| 460 | return 0; |
| 461 | |
| 462 | crypto->data = *buf; |
| 463 | *buf += crypto->len; |
| 464 | |
| 465 | return 1; |
| 466 | } |
| 467 | |
| 468 | /* Encode a NEW_TOKEN frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 469 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 470 | */ |
| 471 | static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end, |
| 472 | struct quic_frame *frm, struct quic_conn *conn) |
| 473 | { |
| 474 | struct quic_new_token *new_token = &frm->new_token; |
| 475 | |
| 476 | if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len) |
| 477 | return 0; |
| 478 | |
| 479 | memcpy(*buf, new_token->data, new_token->len); |
| 480 | |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | /* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame. |
| 485 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 486 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 487 | static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 488 | const unsigned char **buf, const unsigned char *end) |
| 489 | { |
| 490 | struct quic_new_token *new_token = &frm->new_token; |
| 491 | |
| 492 | if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len) |
| 493 | return 0; |
| 494 | |
| 495 | new_token->data = *buf; |
| 496 | *buf += new_token->len; |
| 497 | |
| 498 | return 1; |
| 499 | } |
| 500 | |
| 501 | /* Encode a STREAM frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 502 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 503 | */ |
| 504 | static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end, |
| 505 | struct quic_frame *frm, struct quic_conn *conn) |
| 506 | { |
| 507 | struct quic_stream *stream = &frm->stream; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 508 | const unsigned char *wrap; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 509 | |
Amaury Denoyelle | 1dac018 | 2023-02-02 16:45:07 +0100 | [diff] [blame^] | 510 | /* Caller must set OFF bit if and only if a non-null offset is used. */ |
| 511 | BUG_ON(!!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) != |
| 512 | !!stream->offset.key); |
| 513 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 514 | if (!quic_enc_int(buf, end, stream->id) || |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 515 | ((frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) && !quic_enc_int(buf, end, stream->offset.key)) || |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 516 | ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 517 | (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len))) |
| 518 | return 0; |
| 519 | |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 520 | wrap = (const unsigned char *)b_wrap(stream->buf); |
| 521 | if (stream->data + stream->len > wrap) { |
| 522 | size_t to_copy = wrap - stream->data; |
| 523 | memcpy(*buf, stream->data, to_copy); |
| 524 | *buf += to_copy; |
| 525 | |
| 526 | to_copy = stream->len - to_copy; |
| 527 | memcpy(*buf, b_orig(stream->buf), to_copy); |
| 528 | *buf += to_copy; |
| 529 | } |
| 530 | else { |
| 531 | memcpy(*buf, stream->data, stream->len); |
| 532 | *buf += stream->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 533 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 534 | |
| 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | /* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame. |
| 539 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 540 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 541 | static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 542 | const unsigned char **buf, const unsigned char *end) |
| 543 | { |
| 544 | struct quic_stream *stream = &frm->stream; |
| 545 | |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 546 | if (!quic_dec_int(&stream->id, buf, end)) |
| 547 | return 0; |
| 548 | |
| 549 | /* Offset parsing */ |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 550 | if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 551 | stream->offset.key = 0; |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 552 | } |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 553 | else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end)) |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | |
| 556 | /* Length parsing */ |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 557 | if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) { |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 558 | stream->len = end - *buf; |
| 559 | } |
| 560 | else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | |
| 563 | stream->data = *buf; |
| 564 | *buf += stream->len; |
| 565 | |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* Encode a MAX_DATA frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 570 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 571 | */ |
| 572 | static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end, |
| 573 | struct quic_frame *frm, struct quic_conn *conn) |
| 574 | { |
| 575 | struct quic_max_data *max_data = &frm->max_data; |
| 576 | |
| 577 | return quic_enc_int(buf, end, max_data->max_data); |
| 578 | } |
| 579 | |
| 580 | /* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame. |
| 581 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 582 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 583 | static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 584 | const unsigned char **buf, const unsigned char *end) |
| 585 | { |
| 586 | struct quic_max_data *max_data = &frm->max_data; |
| 587 | |
| 588 | return quic_dec_int(&max_data->max_data, buf, end); |
| 589 | } |
| 590 | |
| 591 | /* Encode a MAX_STREAM_DATA frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 592 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 593 | */ |
| 594 | static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end, |
| 595 | struct quic_frame *frm, struct quic_conn *conn) |
| 596 | { |
| 597 | struct quic_max_stream_data *max_stream_data = &frm->max_stream_data; |
| 598 | |
| 599 | return quic_enc_int(buf, end, max_stream_data->id) && |
| 600 | quic_enc_int(buf, end, max_stream_data->max_stream_data); |
| 601 | } |
| 602 | |
| 603 | /* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame. |
| 604 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 605 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 606 | static int quic_parse_max_stream_data_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 607 | const unsigned char **buf, const unsigned char *end) |
| 608 | { |
| 609 | struct quic_max_stream_data *max_stream_data = &frm->max_stream_data; |
| 610 | |
| 611 | return quic_dec_int(&max_stream_data->id, buf, end) && |
| 612 | quic_dec_int(&max_stream_data->max_stream_data, buf, end); |
| 613 | } |
| 614 | |
| 615 | /* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 616 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 617 | */ |
| 618 | static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end, |
| 619 | struct quic_frame *frm, struct quic_conn *conn) |
| 620 | { |
| 621 | struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi; |
| 622 | |
| 623 | return quic_enc_int(buf, end, max_streams_bidi->max_streams); |
| 624 | } |
| 625 | |
| 626 | /* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end> |
| 627 | * as end into <frm> frame. |
| 628 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 629 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 630 | static int quic_parse_max_streams_bidi_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 631 | const unsigned char **buf, const unsigned char *end) |
| 632 | { |
| 633 | struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi; |
| 634 | |
| 635 | return quic_dec_int(&max_streams_bidi->max_streams, buf, end); |
| 636 | } |
| 637 | |
| 638 | /* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 639 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 640 | */ |
| 641 | static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end, |
| 642 | struct quic_frame *frm, struct quic_conn *conn) |
| 643 | { |
| 644 | struct quic_max_streams *max_streams_uni = &frm->max_streams_uni; |
| 645 | |
| 646 | return quic_enc_int(buf, end, max_streams_uni->max_streams); |
| 647 | } |
| 648 | |
| 649 | /* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end> |
| 650 | * as end into <frm> frame. |
| 651 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 652 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 653 | static int quic_parse_max_streams_uni_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 654 | const unsigned char **buf, const unsigned char *end) |
| 655 | { |
| 656 | struct quic_max_streams *max_streams_uni = &frm->max_streams_uni; |
| 657 | |
| 658 | return quic_dec_int(&max_streams_uni->max_streams, buf, end); |
| 659 | } |
| 660 | |
| 661 | /* Encode a DATA_BLOCKED frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 662 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 663 | */ |
| 664 | static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end, |
| 665 | struct quic_frame *frm, struct quic_conn *conn) |
| 666 | { |
| 667 | struct quic_data_blocked *data_blocked = &frm->data_blocked; |
| 668 | |
| 669 | return quic_enc_int(buf, end, data_blocked->limit); |
| 670 | } |
| 671 | |
| 672 | /* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame. |
| 673 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 674 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 675 | static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 676 | const unsigned char **buf, const unsigned char *end) |
| 677 | { |
| 678 | struct quic_data_blocked *data_blocked = &frm->data_blocked; |
| 679 | |
| 680 | return quic_dec_int(&data_blocked->limit, buf, end); |
| 681 | } |
| 682 | |
| 683 | /* Encode a STREAM_DATA_BLOCKED into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 684 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 685 | */ |
| 686 | static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end, |
| 687 | struct quic_frame *frm, struct quic_conn *conn) |
| 688 | { |
| 689 | struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked; |
| 690 | |
| 691 | return quic_enc_int(buf, end, stream_data_blocked->id) && |
| 692 | quic_enc_int(buf, end, stream_data_blocked->limit); |
| 693 | } |
| 694 | |
| 695 | /* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame. |
| 696 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 697 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 698 | static int quic_parse_stream_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 699 | const unsigned char **buf, const unsigned char *end) |
| 700 | { |
| 701 | struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked; |
| 702 | |
| 703 | return quic_dec_int(&stream_data_blocked->id, buf, end) && |
| 704 | quic_dec_int(&stream_data_blocked->limit, buf, end); |
| 705 | } |
| 706 | |
| 707 | /* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 708 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 709 | */ |
| 710 | static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end, |
| 711 | struct quic_frame *frm, struct quic_conn *conn) |
| 712 | { |
| 713 | struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi; |
| 714 | |
| 715 | return quic_enc_int(buf, end, streams_blocked_bidi->limit); |
| 716 | } |
| 717 | |
| 718 | /* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end> |
| 719 | * as end into <frm> frame. |
| 720 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 721 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 722 | static int quic_parse_streams_blocked_bidi_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 723 | const unsigned char **buf, const unsigned char *end) |
| 724 | { |
| 725 | struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi; |
| 726 | |
| 727 | return quic_dec_int(&streams_blocked_bidi->limit, buf, end); |
| 728 | } |
| 729 | |
| 730 | /* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 731 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 732 | */ |
| 733 | static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end, |
| 734 | struct quic_frame *frm, struct quic_conn *conn) |
| 735 | { |
| 736 | struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni; |
| 737 | |
| 738 | return quic_enc_int(buf, end, streams_blocked_uni->limit); |
| 739 | } |
| 740 | |
| 741 | /* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end> |
| 742 | * as end into <frm> frame. |
| 743 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 744 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 745 | static int quic_parse_streams_blocked_uni_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 746 | const unsigned char **buf, const unsigned char *end) |
| 747 | { |
| 748 | struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni; |
| 749 | |
| 750 | return quic_dec_int(&streams_blocked_uni->limit, buf, end); |
| 751 | } |
| 752 | |
| 753 | /* Encode a NEW_CONNECTION_ID frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 754 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 755 | */ |
| 756 | static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end, |
| 757 | struct quic_frame *frm, struct quic_conn *conn) |
| 758 | { |
| 759 | struct quic_new_connection_id *new_cid = &frm->new_connection_id; |
| 760 | |
| 761 | if (!quic_enc_int(buf, end, new_cid->seq_num) || |
| 762 | !quic_enc_int(buf, end, new_cid->retire_prior_to) || |
| 763 | end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN) |
| 764 | return 0; |
| 765 | |
| 766 | *(*buf)++ = new_cid->cid.len; |
| 767 | |
| 768 | if (new_cid->cid.len) { |
| 769 | memcpy(*buf, new_cid->cid.data, new_cid->cid.len); |
| 770 | *buf += new_cid->cid.len; |
| 771 | } |
| 772 | memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN); |
| 773 | *buf += QUIC_STATELESS_RESET_TOKEN_LEN; |
| 774 | |
| 775 | return 1; |
| 776 | } |
| 777 | |
| 778 | /* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame. |
| 779 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 780 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 781 | static int quic_parse_new_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 782 | const unsigned char **buf, const unsigned char *end) |
| 783 | { |
| 784 | struct quic_new_connection_id *new_cid = &frm->new_connection_id; |
| 785 | |
| 786 | if (!quic_dec_int(&new_cid->seq_num, buf, end) || |
| 787 | !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf) |
| 788 | return 0; |
| 789 | |
| 790 | new_cid->cid.len = *(*buf)++; |
| 791 | if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN) |
| 792 | return 0; |
| 793 | |
| 794 | if (new_cid->cid.len) { |
| 795 | new_cid->cid.data = *buf; |
| 796 | *buf += new_cid->cid.len; |
| 797 | } |
| 798 | new_cid->stateless_reset_token = *buf; |
| 799 | *buf += QUIC_STATELESS_RESET_TOKEN_LEN; |
| 800 | |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | /* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 805 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 806 | */ |
| 807 | static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end, |
| 808 | struct quic_frame *frm, struct quic_conn *conn) |
| 809 | { |
| 810 | struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id; |
| 811 | |
| 812 | return quic_enc_int(buf, end, retire_connection_id->seq_num); |
| 813 | } |
| 814 | |
| 815 | /* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame. |
| 816 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 817 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 818 | static int quic_parse_retire_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 819 | const unsigned char **buf, const unsigned char *end) |
| 820 | { |
| 821 | struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id; |
| 822 | |
| 823 | return quic_dec_int(&retire_connection_id->seq_num, buf, end); |
| 824 | } |
| 825 | |
| 826 | /* Encode a PATH_CHALLENGE frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 827 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 828 | */ |
| 829 | static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end, |
| 830 | struct quic_frame *frm, struct quic_conn *conn) |
| 831 | { |
| 832 | struct quic_path_challenge *path_challenge = &frm->path_challenge; |
| 833 | |
| 834 | if (end - *buf < sizeof path_challenge->data) |
| 835 | return 0; |
| 836 | |
| 837 | memcpy(*buf, path_challenge->data, sizeof path_challenge->data); |
| 838 | *buf += sizeof path_challenge->data; |
| 839 | |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | /* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame. |
| 844 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 845 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 846 | static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 847 | const unsigned char **buf, const unsigned char *end) |
| 848 | { |
| 849 | struct quic_path_challenge *path_challenge = &frm->path_challenge; |
| 850 | |
| 851 | if (end - *buf < sizeof path_challenge->data) |
| 852 | return 0; |
| 853 | |
| 854 | memcpy(path_challenge->data, *buf, sizeof path_challenge->data); |
| 855 | *buf += sizeof path_challenge->data; |
| 856 | |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | |
| 861 | /* Encode a PATH_RESPONSE frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 862 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 863 | */ |
| 864 | static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end, |
| 865 | struct quic_frame *frm, struct quic_conn *conn) |
| 866 | { |
| 867 | struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response; |
| 868 | |
| 869 | if (end - *buf < sizeof path_challenge_response->data) |
| 870 | return 0; |
| 871 | |
| 872 | memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data); |
| 873 | *buf += sizeof path_challenge_response->data; |
| 874 | |
| 875 | return 1; |
| 876 | } |
| 877 | |
| 878 | /* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame. |
| 879 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 880 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 881 | static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 882 | const unsigned char **buf, const unsigned char *end) |
| 883 | { |
| 884 | struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response; |
| 885 | |
| 886 | if (end - *buf < sizeof path_challenge_response->data) |
| 887 | return 0; |
| 888 | |
| 889 | memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data); |
| 890 | *buf += sizeof path_challenge_response->data; |
| 891 | |
| 892 | return 1; |
| 893 | } |
| 894 | |
| 895 | /* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer. |
| 896 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 897 | * and another at QUIC layer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 898 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 899 | */ |
| 900 | static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end, |
| 901 | struct quic_frame *frm, struct quic_conn *conn) |
| 902 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 903 | struct quic_connection_close *cc = &frm->connection_close; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 904 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 905 | if (!quic_enc_int(buf, end, cc->error_code) || |
| 906 | !quic_enc_int(buf, end, cc->frame_type) || |
| 907 | !quic_enc_int(buf, end, cc->reason_phrase_len) || |
| 908 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 909 | return 0; |
| 910 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 911 | memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len); |
| 912 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 913 | |
| 914 | return 1; |
| 915 | } |
| 916 | |
| 917 | /* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 918 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 919 | * and another at QUIC layer. |
| 920 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 921 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 922 | static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 923 | const unsigned char **buf, const unsigned char *end) |
| 924 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 925 | size_t plen; |
| 926 | struct quic_connection_close *cc = &frm->connection_close; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 927 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 928 | if (!quic_dec_int(&cc->error_code, buf, end) || |
| 929 | !quic_dec_int(&cc->frame_type, buf, end) || |
| 930 | !quic_dec_int(&cc->reason_phrase_len, buf, end) || |
| 931 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 932 | return 0; |
| 933 | |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 934 | plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase); |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 935 | memcpy(cc->reason_phrase, *buf, plen); |
| 936 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 937 | |
| 938 | return 1; |
| 939 | } |
| 940 | |
| 941 | /* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer. |
| 942 | * Note there exist two types of CONNECTION_CLOSE frame, one for application layer |
| 943 | * and another at QUIC layer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 944 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 945 | */ |
| 946 | static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end, |
| 947 | struct quic_frame *frm, struct quic_conn *conn) |
| 948 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 949 | struct quic_connection_close_app *cc = &frm->connection_close_app; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 950 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 951 | if (!quic_enc_int(buf, end, cc->error_code) || |
| 952 | !quic_enc_int(buf, end, cc->reason_phrase_len) || |
| 953 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 954 | return 0; |
| 955 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 956 | memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len); |
| 957 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 958 | |
| 959 | return 1; |
| 960 | } |
| 961 | |
| 962 | /* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 963 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 964 | * and another at QUIC layer. |
| 965 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 966 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 967 | static int quic_parse_connection_close_app_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 968 | const unsigned char **buf, const unsigned char *end) |
| 969 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 970 | size_t plen; |
| 971 | struct quic_connection_close_app *cc = &frm->connection_close_app; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 972 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 973 | if (!quic_dec_int(&cc->error_code, buf, end) || |
| 974 | !quic_dec_int(&cc->reason_phrase_len, buf, end) || |
| 975 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 976 | return 0; |
| 977 | |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 978 | plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase); |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 979 | memcpy(cc->reason_phrase, *buf, plen); |
| 980 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 981 | |
| 982 | return 1; |
| 983 | } |
| 984 | |
| 985 | /* Encode a HANDSHAKE_DONE frame into <buf> buffer. |
| 986 | * Always succeeds. |
| 987 | */ |
| 988 | static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end, |
| 989 | struct quic_frame *frm, struct quic_conn *conn) |
| 990 | { |
| 991 | /* No field */ |
| 992 | return 1; |
| 993 | } |
| 994 | |
| 995 | /* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 996 | * Always succeed. |
| 997 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 998 | static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 999 | const unsigned char **buf, const unsigned char *end) |
| 1000 | { |
| 1001 | /* No field */ |
| 1002 | return 1; |
| 1003 | } |
| 1004 | |
| 1005 | struct quic_frame_builder { |
| 1006 | int (*func)(unsigned char **buf, const unsigned char *end, |
| 1007 | struct quic_frame *frm, struct quic_conn *conn); |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1008 | uint32_t mask; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1009 | unsigned char flags; |
| 1010 | }; |
| 1011 | |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1012 | const struct quic_frame_builder quic_frame_builders[] = { |
Frédéric Lécaille | 156a59b | 2021-09-17 16:51:51 +0200 | [diff] [blame] | 1013 | [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1014 | [QUIC_FT_PING] = { .func = quic_build_ping_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1015 | [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1016 | [QUIC_FT_ACK_ECN] = { .func = quic_build_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
Frédéric Lécaille | 156a59b | 2021-09-17 16:51:51 +0200 | [diff] [blame] | 1017 | [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1018 | [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1019 | [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1020 | [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, }, |
| 1021 | [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1022 | [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1023 | [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1024 | [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1025 | [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1026 | [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1027 | [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1028 | [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1029 | [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1030 | [QUIC_FT_MAX_STREAM_DATA] = { .func = quic_build_max_stream_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1031 | [QUIC_FT_MAX_STREAMS_BIDI] = { .func = quic_build_max_streams_bidi_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1032 | [QUIC_FT_MAX_STREAMS_UNI] = { .func = quic_build_max_streams_uni_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1033 | [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1034 | [QUIC_FT_STREAM_DATA_BLOCKED] = { .func = quic_build_stream_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1035 | [QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_build_streams_blocked_bidi_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1036 | [QUIC_FT_STREAMS_BLOCKED_UNI] = { .func = quic_build_streams_blocked_uni_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1037 | [QUIC_FT_NEW_CONNECTION_ID] = { .func = quic_build_new_connection_id_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1038 | [QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_build_retire_connection_id_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1039 | [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1040 | [QUIC_FT_PATH_RESPONSE] = { .func = quic_build_path_response_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1041 | [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1042 | [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_build_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
Frédéric Lécaille | 156a59b | 2021-09-17 16:51:51 +0200 | [diff] [blame] | 1043 | [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écaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1044 | }; |
| 1045 | |
| 1046 | struct quic_frame_parser { |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 1047 | int (*func)(struct quic_frame *frm, struct quic_conn *qc, |
| 1048 | const unsigned char **buf, const unsigned char *end); |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1049 | uint32_t mask; |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1050 | unsigned char flags; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1051 | }; |
| 1052 | |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1053 | const struct quic_frame_parser quic_frame_parsers[] = { |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1054 | [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1055 | [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1056 | [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1057 | [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1058 | [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1059 | [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1060 | [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1061 | [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, }, |
| 1062 | [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1063 | [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1064 | [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1065 | [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1066 | [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1067 | [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1068 | [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1069 | [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1070 | [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1071 | [QUIC_FT_MAX_STREAM_DATA] = { .func = quic_parse_max_stream_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1072 | [QUIC_FT_MAX_STREAMS_BIDI] = { .func = quic_parse_max_streams_bidi_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1073 | [QUIC_FT_MAX_STREAMS_UNI] = { .func = quic_parse_max_streams_uni_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1074 | [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1075 | [QUIC_FT_STREAM_DATA_BLOCKED] = { .func = quic_parse_stream_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1076 | [QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_parse_streams_blocked_bidi_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1077 | [QUIC_FT_STREAMS_BLOCKED_UNI] = { .func = quic_parse_streams_blocked_uni_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1078 | [QUIC_FT_NEW_CONNECTION_ID] = { .func = quic_parse_new_connection_id_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1079 | [QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_parse_retire_connection_id_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1080 | [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1081 | [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1082 | [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1083 | [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1084 | [QUIC_FT_HANDSHAKE_DONE] = { .func = quic_parse_handshake_done_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, }, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1085 | }; |
| 1086 | |
| 1087 | /* Decode a QUIC frame from <buf> buffer into <frm> frame. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1088 | * Returns 1 if succeeded (enough data to parse the frame), 0 if not. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1089 | */ |
| 1090 | int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt, |
| 1091 | const unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1092 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1093 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1094 | int ret = 0; |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1095 | const struct quic_frame_parser *parser; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1096 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1097 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1098 | if (end <= *buf) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1099 | TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1100 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | frm->type = *(*buf)++; |
Frédéric Lécaille | 0c80e69 | 2022-02-15 10:27:34 +0100 | [diff] [blame] | 1104 | if (frm->type >= QUIC_FT_MAX) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1105 | TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1106 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | parser = &quic_frame_parsers[frm->type]; |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1110 | if (!(parser->mask & (1U << pkt->type))) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1111 | TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1112 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1115 | TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm); |
| 1116 | if (!parser->func(frm, qc, buf, end)) { |
| 1117 | TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1118 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1121 | pkt->flags |= parser->flags; |
| 1122 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1123 | ret = 1; |
| 1124 | leave: |
| 1125 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 1126 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /* Encode <frm> QUIC frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1130 | * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not. |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1131 | * The buffer is updated to point to one byte past the end of the built frame |
| 1132 | * only if succeeded. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1133 | */ |
| 1134 | int qc_build_frm(unsigned char **buf, const unsigned char *end, |
| 1135 | struct quic_frame *frm, struct quic_tx_packet *pkt, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1136 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1137 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1138 | int ret = 0; |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1139 | const struct quic_frame_builder *builder; |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1140 | unsigned char *pos = *buf; |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1141 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1142 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1143 | builder = &quic_frame_builders[frm->type]; |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1144 | if (!(builder->mask & (1U << pkt->type))) { |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1145 | /* XXX This it a bug to send an unauthorized frame with such a packet type XXX */ |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1146 | TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm); |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1147 | BUG_ON(!(builder->mask & (1U << pkt->type))); |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1150 | if (end <= pos) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1151 | TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1152 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1155 | TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm); |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1156 | *pos++ = frm->type; |
| 1157 | if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1158 | TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1159 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1160 | } |
| 1161 | |
Frédéric Lécaille | dc2593e | 2021-09-17 16:57:14 +0200 | [diff] [blame] | 1162 | pkt->flags |= builder->flags; |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1163 | *buf = pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1164 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1165 | ret = 1; |
| 1166 | leave: |
| 1167 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 1168 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |