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