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