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 | |
| 510 | if (!quic_enc_int(buf, end, stream->id) || |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 511 | ((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] | 512 | ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) && |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 513 | (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len))) |
| 514 | return 0; |
| 515 | |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 516 | wrap = (const unsigned char *)b_wrap(stream->buf); |
| 517 | if (stream->data + stream->len > wrap) { |
| 518 | size_t to_copy = wrap - stream->data; |
| 519 | memcpy(*buf, stream->data, to_copy); |
| 520 | *buf += to_copy; |
| 521 | |
| 522 | to_copy = stream->len - to_copy; |
| 523 | memcpy(*buf, b_orig(stream->buf), to_copy); |
| 524 | *buf += to_copy; |
| 525 | } |
| 526 | else { |
| 527 | memcpy(*buf, stream->data, stream->len); |
| 528 | *buf += stream->len; |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 529 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 530 | |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | /* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame. |
| 535 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 536 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 537 | 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] | 538 | const unsigned char **buf, const unsigned char *end) |
| 539 | { |
| 540 | struct quic_stream *stream = &frm->stream; |
| 541 | |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 542 | if (!quic_dec_int(&stream->id, buf, end)) |
| 543 | return 0; |
| 544 | |
| 545 | /* Offset parsing */ |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 546 | if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) { |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 547 | stream->offset.key = 0; |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 548 | } |
Frédéric Lécaille | 785d3bd | 2021-09-10 09:13:39 +0200 | [diff] [blame] | 549 | 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] | 550 | return 0; |
| 551 | |
| 552 | /* Length parsing */ |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 553 | if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) { |
Frédéric Lécaille | 129a351 | 2020-12-31 10:57:04 +0100 | [diff] [blame] | 554 | stream->len = end - *buf; |
| 555 | } |
| 556 | 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] | 557 | return 0; |
| 558 | |
Amaury Denoyelle | db44338 | 2021-11-30 11:23:29 +0100 | [diff] [blame] | 559 | stream->fin = (frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT); |
| 560 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 561 | stream->data = *buf; |
| 562 | *buf += stream->len; |
| 563 | |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | /* Encode a MAX_DATA frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 568 | * 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] | 569 | */ |
| 570 | static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end, |
| 571 | struct quic_frame *frm, struct quic_conn *conn) |
| 572 | { |
| 573 | struct quic_max_data *max_data = &frm->max_data; |
| 574 | |
| 575 | return quic_enc_int(buf, end, max_data->max_data); |
| 576 | } |
| 577 | |
| 578 | /* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame. |
| 579 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 580 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 581 | 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] | 582 | const unsigned char **buf, const unsigned char *end) |
| 583 | { |
| 584 | struct quic_max_data *max_data = &frm->max_data; |
| 585 | |
| 586 | return quic_dec_int(&max_data->max_data, buf, end); |
| 587 | } |
| 588 | |
| 589 | /* Encode a MAX_STREAM_DATA frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 590 | * 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] | 591 | */ |
| 592 | static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end, |
| 593 | struct quic_frame *frm, struct quic_conn *conn) |
| 594 | { |
| 595 | struct quic_max_stream_data *max_stream_data = &frm->max_stream_data; |
| 596 | |
| 597 | return quic_enc_int(buf, end, max_stream_data->id) && |
| 598 | quic_enc_int(buf, end, max_stream_data->max_stream_data); |
| 599 | } |
| 600 | |
| 601 | /* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame. |
| 602 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 603 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 604 | 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] | 605 | const unsigned char **buf, const unsigned char *end) |
| 606 | { |
| 607 | struct quic_max_stream_data *max_stream_data = &frm->max_stream_data; |
| 608 | |
| 609 | return quic_dec_int(&max_stream_data->id, buf, end) && |
| 610 | quic_dec_int(&max_stream_data->max_stream_data, buf, end); |
| 611 | } |
| 612 | |
| 613 | /* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 614 | * 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] | 615 | */ |
| 616 | static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end, |
| 617 | struct quic_frame *frm, struct quic_conn *conn) |
| 618 | { |
| 619 | struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi; |
| 620 | |
| 621 | return quic_enc_int(buf, end, max_streams_bidi->max_streams); |
| 622 | } |
| 623 | |
| 624 | /* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end> |
| 625 | * as end into <frm> frame. |
| 626 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 627 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 628 | 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] | 629 | const unsigned char **buf, const unsigned char *end) |
| 630 | { |
| 631 | struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi; |
| 632 | |
| 633 | return quic_dec_int(&max_streams_bidi->max_streams, buf, end); |
| 634 | } |
| 635 | |
| 636 | /* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 637 | * 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] | 638 | */ |
| 639 | static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end, |
| 640 | struct quic_frame *frm, struct quic_conn *conn) |
| 641 | { |
| 642 | struct quic_max_streams *max_streams_uni = &frm->max_streams_uni; |
| 643 | |
| 644 | return quic_enc_int(buf, end, max_streams_uni->max_streams); |
| 645 | } |
| 646 | |
| 647 | /* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end> |
| 648 | * as end into <frm> frame. |
| 649 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 650 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 651 | 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] | 652 | const unsigned char **buf, const unsigned char *end) |
| 653 | { |
| 654 | struct quic_max_streams *max_streams_uni = &frm->max_streams_uni; |
| 655 | |
| 656 | return quic_dec_int(&max_streams_uni->max_streams, buf, end); |
| 657 | } |
| 658 | |
| 659 | /* Encode a DATA_BLOCKED frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 660 | * 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] | 661 | */ |
| 662 | static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end, |
| 663 | struct quic_frame *frm, struct quic_conn *conn) |
| 664 | { |
| 665 | struct quic_data_blocked *data_blocked = &frm->data_blocked; |
| 666 | |
| 667 | return quic_enc_int(buf, end, data_blocked->limit); |
| 668 | } |
| 669 | |
| 670 | /* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame. |
| 671 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 672 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 673 | 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] | 674 | const unsigned char **buf, const unsigned char *end) |
| 675 | { |
| 676 | struct quic_data_blocked *data_blocked = &frm->data_blocked; |
| 677 | |
| 678 | return quic_dec_int(&data_blocked->limit, buf, end); |
| 679 | } |
| 680 | |
| 681 | /* Encode a STREAM_DATA_BLOCKED into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 682 | * 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] | 683 | */ |
| 684 | static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end, |
| 685 | struct quic_frame *frm, struct quic_conn *conn) |
| 686 | { |
| 687 | struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked; |
| 688 | |
| 689 | return quic_enc_int(buf, end, stream_data_blocked->id) && |
| 690 | quic_enc_int(buf, end, stream_data_blocked->limit); |
| 691 | } |
| 692 | |
| 693 | /* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame. |
| 694 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 695 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 696 | 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] | 697 | const unsigned char **buf, const unsigned char *end) |
| 698 | { |
| 699 | struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked; |
| 700 | |
| 701 | return quic_dec_int(&stream_data_blocked->id, buf, end) && |
| 702 | quic_dec_int(&stream_data_blocked->limit, buf, end); |
| 703 | } |
| 704 | |
| 705 | /* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 706 | * 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] | 707 | */ |
| 708 | static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end, |
| 709 | struct quic_frame *frm, struct quic_conn *conn) |
| 710 | { |
| 711 | struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi; |
| 712 | |
| 713 | return quic_enc_int(buf, end, streams_blocked_bidi->limit); |
| 714 | } |
| 715 | |
| 716 | /* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end> |
| 717 | * as end into <frm> frame. |
| 718 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 719 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 720 | 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] | 721 | const unsigned char **buf, const unsigned char *end) |
| 722 | { |
| 723 | struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi; |
| 724 | |
| 725 | return quic_dec_int(&streams_blocked_bidi->limit, buf, end); |
| 726 | } |
| 727 | |
| 728 | /* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 729 | * 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] | 730 | */ |
| 731 | static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end, |
| 732 | struct quic_frame *frm, struct quic_conn *conn) |
| 733 | { |
| 734 | struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni; |
| 735 | |
| 736 | return quic_enc_int(buf, end, streams_blocked_uni->limit); |
| 737 | } |
| 738 | |
| 739 | /* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end> |
| 740 | * as end into <frm> frame. |
| 741 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 742 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 743 | 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] | 744 | const unsigned char **buf, const unsigned char *end) |
| 745 | { |
| 746 | struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni; |
| 747 | |
| 748 | return quic_dec_int(&streams_blocked_uni->limit, buf, end); |
| 749 | } |
| 750 | |
| 751 | /* Encode a NEW_CONNECTION_ID frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 752 | * 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] | 753 | */ |
| 754 | static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end, |
| 755 | struct quic_frame *frm, struct quic_conn *conn) |
| 756 | { |
| 757 | struct quic_new_connection_id *new_cid = &frm->new_connection_id; |
| 758 | |
| 759 | if (!quic_enc_int(buf, end, new_cid->seq_num) || |
| 760 | !quic_enc_int(buf, end, new_cid->retire_prior_to) || |
| 761 | end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN) |
| 762 | return 0; |
| 763 | |
| 764 | *(*buf)++ = new_cid->cid.len; |
| 765 | |
| 766 | if (new_cid->cid.len) { |
| 767 | memcpy(*buf, new_cid->cid.data, new_cid->cid.len); |
| 768 | *buf += new_cid->cid.len; |
| 769 | } |
| 770 | memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN); |
| 771 | *buf += QUIC_STATELESS_RESET_TOKEN_LEN; |
| 772 | |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | /* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame. |
| 777 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 778 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 779 | 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] | 780 | const unsigned char **buf, const unsigned char *end) |
| 781 | { |
| 782 | struct quic_new_connection_id *new_cid = &frm->new_connection_id; |
| 783 | |
| 784 | if (!quic_dec_int(&new_cid->seq_num, buf, end) || |
| 785 | !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf) |
| 786 | return 0; |
| 787 | |
| 788 | new_cid->cid.len = *(*buf)++; |
| 789 | if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN) |
| 790 | return 0; |
| 791 | |
| 792 | if (new_cid->cid.len) { |
| 793 | new_cid->cid.data = *buf; |
| 794 | *buf += new_cid->cid.len; |
| 795 | } |
| 796 | new_cid->stateless_reset_token = *buf; |
| 797 | *buf += QUIC_STATELESS_RESET_TOKEN_LEN; |
| 798 | |
| 799 | return 1; |
| 800 | } |
| 801 | |
| 802 | /* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 803 | * 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] | 804 | */ |
| 805 | static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end, |
| 806 | struct quic_frame *frm, struct quic_conn *conn) |
| 807 | { |
| 808 | struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id; |
| 809 | |
| 810 | return quic_enc_int(buf, end, retire_connection_id->seq_num); |
| 811 | } |
| 812 | |
| 813 | /* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame. |
| 814 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 815 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 816 | 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] | 817 | const unsigned char **buf, const unsigned char *end) |
| 818 | { |
| 819 | struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id; |
| 820 | |
| 821 | return quic_dec_int(&retire_connection_id->seq_num, buf, end); |
| 822 | } |
| 823 | |
| 824 | /* Encode a PATH_CHALLENGE frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 825 | * 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] | 826 | */ |
| 827 | static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end, |
| 828 | struct quic_frame *frm, struct quic_conn *conn) |
| 829 | { |
| 830 | struct quic_path_challenge *path_challenge = &frm->path_challenge; |
| 831 | |
| 832 | if (end - *buf < sizeof path_challenge->data) |
| 833 | return 0; |
| 834 | |
| 835 | memcpy(*buf, path_challenge->data, sizeof path_challenge->data); |
| 836 | *buf += sizeof path_challenge->data; |
| 837 | |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | /* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame. |
| 842 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 843 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 844 | 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] | 845 | const unsigned char **buf, const unsigned char *end) |
| 846 | { |
| 847 | struct quic_path_challenge *path_challenge = &frm->path_challenge; |
| 848 | |
| 849 | if (end - *buf < sizeof path_challenge->data) |
| 850 | return 0; |
| 851 | |
| 852 | memcpy(path_challenge->data, *buf, sizeof path_challenge->data); |
| 853 | *buf += sizeof path_challenge->data; |
| 854 | |
| 855 | return 1; |
| 856 | } |
| 857 | |
| 858 | |
| 859 | /* Encode a PATH_RESPONSE frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 860 | * 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] | 861 | */ |
| 862 | static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end, |
| 863 | struct quic_frame *frm, struct quic_conn *conn) |
| 864 | { |
| 865 | struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response; |
| 866 | |
| 867 | if (end - *buf < sizeof path_challenge_response->data) |
| 868 | return 0; |
| 869 | |
| 870 | memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data); |
| 871 | *buf += sizeof path_challenge_response->data; |
| 872 | |
| 873 | return 1; |
| 874 | } |
| 875 | |
| 876 | /* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame. |
| 877 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 878 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 879 | 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] | 880 | const unsigned char **buf, const unsigned char *end) |
| 881 | { |
| 882 | struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response; |
| 883 | |
| 884 | if (end - *buf < sizeof path_challenge_response->data) |
| 885 | return 0; |
| 886 | |
| 887 | memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data); |
| 888 | *buf += sizeof path_challenge_response->data; |
| 889 | |
| 890 | return 1; |
| 891 | } |
| 892 | |
| 893 | /* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer. |
| 894 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 895 | * and another at QUIC layer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 896 | * 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] | 897 | */ |
| 898 | static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end, |
| 899 | struct quic_frame *frm, struct quic_conn *conn) |
| 900 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 901 | struct quic_connection_close *cc = &frm->connection_close; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 902 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 903 | if (!quic_enc_int(buf, end, cc->error_code) || |
| 904 | !quic_enc_int(buf, end, cc->frame_type) || |
| 905 | !quic_enc_int(buf, end, cc->reason_phrase_len) || |
| 906 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 907 | return 0; |
| 908 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 909 | memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len); |
| 910 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 911 | |
| 912 | return 1; |
| 913 | } |
| 914 | |
| 915 | /* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 916 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 917 | * and another at QUIC layer. |
| 918 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 919 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 920 | 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] | 921 | const unsigned char **buf, const unsigned char *end) |
| 922 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 923 | size_t plen; |
| 924 | struct quic_connection_close *cc = &frm->connection_close; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 925 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 926 | if (!quic_dec_int(&cc->error_code, buf, end) || |
| 927 | !quic_dec_int(&cc->frame_type, buf, end) || |
| 928 | !quic_dec_int(&cc->reason_phrase_len, buf, end) || |
| 929 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 930 | return 0; |
| 931 | |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 932 | 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] | 933 | memcpy(cc->reason_phrase, *buf, plen); |
| 934 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 935 | |
| 936 | return 1; |
| 937 | } |
| 938 | |
| 939 | /* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer. |
| 940 | * Note there exist two types of CONNECTION_CLOSE frame, one for application layer |
| 941 | * and another at QUIC layer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 942 | * 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] | 943 | */ |
| 944 | static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end, |
| 945 | struct quic_frame *frm, struct quic_conn *conn) |
| 946 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 947 | 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] | 948 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 949 | if (!quic_enc_int(buf, end, cc->error_code) || |
| 950 | !quic_enc_int(buf, end, cc->reason_phrase_len) || |
| 951 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 952 | return 0; |
| 953 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 954 | memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len); |
| 955 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 956 | |
| 957 | return 1; |
| 958 | } |
| 959 | |
| 960 | /* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 961 | * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer |
| 962 | * and another at QUIC layer. |
| 963 | * Return 1 if succeeded (enough room to parse this frame), 0 if not. |
| 964 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 965 | 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] | 966 | const unsigned char **buf, const unsigned char *end) |
| 967 | { |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 968 | size_t plen; |
| 969 | 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] | 970 | |
Frédéric Lécaille | 010e532 | 2021-12-23 15:19:15 +0100 | [diff] [blame] | 971 | if (!quic_dec_int(&cc->error_code, buf, end) || |
| 972 | !quic_dec_int(&cc->reason_phrase_len, buf, end) || |
| 973 | end - *buf < cc->reason_phrase_len) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 974 | return 0; |
| 975 | |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 976 | 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] | 977 | memcpy(cc->reason_phrase, *buf, plen); |
| 978 | *buf += cc->reason_phrase_len; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 979 | |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | /* Encode a HANDSHAKE_DONE frame into <buf> buffer. |
| 984 | * Always succeeds. |
| 985 | */ |
| 986 | static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end, |
| 987 | struct quic_frame *frm, struct quic_conn *conn) |
| 988 | { |
| 989 | /* No field */ |
| 990 | return 1; |
| 991 | } |
| 992 | |
| 993 | /* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame. |
| 994 | * Always succeed. |
| 995 | */ |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 996 | 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] | 997 | const unsigned char **buf, const unsigned char *end) |
| 998 | { |
| 999 | /* No field */ |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | struct quic_frame_builder { |
| 1004 | int (*func)(unsigned char **buf, const unsigned char *end, |
| 1005 | struct quic_frame *frm, struct quic_conn *conn); |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1006 | uint32_t mask; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1007 | unsigned char flags; |
| 1008 | }; |
| 1009 | |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1010 | const struct quic_frame_builder quic_frame_builders[] = { |
Frédéric Lécaille | 156a59b | 2021-09-17 16:51:51 +0200 | [diff] [blame] | 1011 | [QUIC_FT_PADDING] = { .func = quic_build_padding_frame, .flags = QUIC_FL_TX_PACKET_PADDING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1012 | [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] | 1013 | [QUIC_FT_ACK] = { .func = quic_build_ack_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1014 | [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] | 1015 | [QUIC_FT_RESET_STREAM] = { .func = quic_build_reset_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1016 | [QUIC_FT_STOP_SENDING] = { .func = quic_build_stop_sending_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1017 | [QUIC_FT_CRYPTO] = { .func = quic_build_crypto_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1018 | [QUIC_FT_NEW_TOKEN] = { .func = quic_build_new_token_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, }, |
| 1019 | [QUIC_FT_STREAM_8] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1020 | [QUIC_FT_STREAM_9] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1021 | [QUIC_FT_STREAM_A] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1022 | [QUIC_FT_STREAM_B] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1023 | [QUIC_FT_STREAM_C] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1024 | [QUIC_FT_STREAM_D] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1025 | [QUIC_FT_STREAM_E] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1026 | [QUIC_FT_STREAM_F] = { .func = quic_build_stream_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1027 | [QUIC_FT_MAX_DATA] = { .func = quic_build_max_data_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1028 | [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, }, |
| 1029 | [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, }, |
| 1030 | [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, }, |
| 1031 | [QUIC_FT_DATA_BLOCKED] = { .func = quic_build_data_blocked_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1032 | [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, }, |
| 1033 | [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, }, |
| 1034 | [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, }, |
| 1035 | [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, }, |
| 1036 | [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, }, |
| 1037 | [QUIC_FT_PATH_CHALLENGE] = { .func = quic_build_path_challenge_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1038 | [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] | 1039 | [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_build_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1040 | [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] | 1041 | [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] | 1042 | }; |
| 1043 | |
| 1044 | struct quic_frame_parser { |
Frédéric Lécaille | 50044ad | 2020-12-29 11:42:08 +0100 | [diff] [blame] | 1045 | int (*func)(struct quic_frame *frm, struct quic_conn *qc, |
| 1046 | const unsigned char **buf, const unsigned char *end); |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1047 | uint32_t mask; |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1048 | unsigned char flags; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1049 | }; |
| 1050 | |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1051 | const struct quic_frame_parser quic_frame_parsers[] = { |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1052 | [QUIC_FT_PADDING] = { .func = quic_parse_padding_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1053 | [QUIC_FT_PING] = { .func = quic_parse_ping_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1054 | [QUIC_FT_ACK] = { .func = quic_parse_ack_frame_header, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1055 | [QUIC_FT_ACK_ECN] = { .func = quic_parse_ack_ecn_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1056 | [QUIC_FT_RESET_STREAM] = { .func = quic_parse_reset_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1057 | [QUIC_FT_STOP_SENDING] = { .func = quic_parse_stop_sending_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1058 | [QUIC_FT_CRYPTO] = { .func = quic_parse_crypto_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, }, |
| 1059 | [QUIC_FT_NEW_TOKEN] = { .func = quic_parse_new_token_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, }, |
| 1060 | [QUIC_FT_STREAM_8] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1061 | [QUIC_FT_STREAM_9] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1062 | [QUIC_FT_STREAM_A] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1063 | [QUIC_FT_STREAM_B] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1064 | [QUIC_FT_STREAM_C] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1065 | [QUIC_FT_STREAM_D] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1066 | [QUIC_FT_STREAM_E] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1067 | [QUIC_FT_STREAM_F] = { .func = quic_parse_stream_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1068 | [QUIC_FT_MAX_DATA] = { .func = quic_parse_max_data_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1069 | [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, }, |
| 1070 | [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, }, |
| 1071 | [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, }, |
| 1072 | [QUIC_FT_DATA_BLOCKED] = { .func = quic_parse_data_blocked_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1073 | [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, }, |
| 1074 | [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, }, |
| 1075 | [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, }, |
| 1076 | [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, }, |
| 1077 | [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, }, |
| 1078 | [QUIC_FT_PATH_CHALLENGE] = { .func = quic_parse_path_challenge_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1079 | [QUIC_FT_PATH_RESPONSE] = { .func = quic_parse_path_response_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1080 | [QUIC_FT_CONNECTION_CLOSE] = { .func = quic_parse_connection_close_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, }, |
| 1081 | [QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, }, |
| 1082 | [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] | 1083 | }; |
| 1084 | |
| 1085 | /* Decode a QUIC frame from <buf> buffer into <frm> frame. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1086 | * 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] | 1087 | */ |
| 1088 | int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt, |
| 1089 | const unsigned char **buf, const unsigned char *end, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1090 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1091 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1092 | int ret = 0; |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1093 | const struct quic_frame_parser *parser; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1094 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1095 | TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1096 | if (end <= *buf) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1097 | TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc); |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1098 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | frm->type = *(*buf)++; |
Frédéric Lécaille | 0c80e69 | 2022-02-15 10:27:34 +0100 | [diff] [blame] | 1102 | if (frm->type >= QUIC_FT_MAX) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1103 | 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] | 1104 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | parser = &quic_frame_parsers[frm->type]; |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1108 | if (!(parser->mask & (1U << pkt->type))) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1109 | 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] | 1110 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1113 | TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm); |
| 1114 | if (!parser->func(frm, qc, buf, end)) { |
| 1115 | 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] | 1116 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1119 | pkt->flags |= parser->flags; |
| 1120 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1121 | ret = 1; |
| 1122 | leave: |
| 1123 | TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc); |
| 1124 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /* Encode <frm> QUIC frame into <buf> buffer. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 1128 | * 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] | 1129 | * The buffer is updated to point to one byte past the end of the built frame |
| 1130 | * only if succeeded. |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1131 | */ |
| 1132 | int qc_build_frm(unsigned char **buf, const unsigned char *end, |
| 1133 | struct quic_frame *frm, struct quic_tx_packet *pkt, |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1134 | struct quic_conn *qc) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1135 | { |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1136 | int ret = 0; |
Frédéric Lécaille | 2cb130c | 2021-09-17 17:05:44 +0200 | [diff] [blame] | 1137 | const struct quic_frame_builder *builder; |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1138 | unsigned char *pos = *buf; |
Frédéric Lécaille | f7fe965 | 2020-12-09 14:56:18 +0100 | [diff] [blame] | 1139 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1140 | TRACE_ENTER(QUIC_EV_CONN_BFRM, qc); |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1141 | builder = &quic_frame_builders[frm->type]; |
Frédéric Lécaille | ce2ecc9 | 2022-02-02 14:37:37 +0100 | [diff] [blame] | 1142 | if (!(builder->mask & (1U << pkt->type))) { |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1143 | /* 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] | 1144 | 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] | 1145 | BUG_ON(!(builder->mask & (1U << pkt->type))); |
Frédéric Lécaille | f5821dc | 2021-07-30 14:42:33 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1148 | if (end <= pos) { |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1149 | 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] | 1150 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 1153 | TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm); |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1154 | *pos++ = frm->type; |
| 1155 | 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] | 1156 | 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] | 1157 | goto leave; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1158 | } |
| 1159 | |
Frédéric Lécaille | dc2593e | 2021-09-17 16:57:14 +0200 | [diff] [blame] | 1160 | pkt->flags |= builder->flags; |
Frédéric Lécaille | b8047de | 2022-08-23 17:40:09 +0200 | [diff] [blame] | 1161 | *buf = pos; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1162 | |
Frédéric Lécaille | a8b2f84 | 2022-08-10 17:56:45 +0200 | [diff] [blame] | 1163 | ret = 1; |
| 1164 | leave: |
| 1165 | TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc); |
| 1166 | return ret; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |