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