Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/3 protocol processing |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation, version 2.1 |
| 7 | * exclusively. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 19 | #include <import/ist.h> |
| 20 | |
| 21 | #include <haproxy/api.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 22 | #include <haproxy/buf.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 23 | #include <haproxy/chunk.h> |
Amaury Denoyelle | 9904355 | 2021-08-24 15:36:02 +0200 | [diff] [blame] | 24 | #include <haproxy/connection.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 25 | #include <haproxy/dynbuf.h> |
| 26 | #include <haproxy/h3.h> |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 27 | #include <haproxy/h3_stats.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 28 | #include <haproxy/http.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 29 | #include <haproxy/http-hdr-t.h> |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 30 | #include <haproxy/http_htx.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 31 | #include <haproxy/htx.h> |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 32 | #include <haproxy/intops.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 33 | #include <haproxy/istbuf.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 34 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 35 | #include <haproxy/pool.h> |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 36 | #include <haproxy/qmux_http.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 37 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 38 | #include <haproxy/qpack-enc.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 39 | #include <haproxy/quic_conn-t.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 40 | #include <haproxy/quic_enc.h> |
Amaury Denoyelle | 51f116d | 2023-05-04 15:49:02 +0200 | [diff] [blame] | 41 | #include <haproxy/quic_frame.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 42 | #include <haproxy/stats-t.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 43 | #include <haproxy/tools.h> |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 44 | #include <haproxy/trace.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 45 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 46 | /* trace source and events */ |
| 47 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 48 | const struct trace_source *src, |
| 49 | const struct ist where, const struct ist func, |
| 50 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 51 | |
| 52 | static const struct trace_event h3_trace_events[] = { |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 53 | #define H3_EV_RX_FRAME (1ULL << 0) |
| 54 | { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" }, |
| 55 | #define H3_EV_RX_DATA (1ULL << 1) |
| 56 | { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" }, |
| 57 | #define H3_EV_RX_HDR (1ULL << 2) |
| 58 | { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" }, |
| 59 | #define H3_EV_RX_SETTINGS (1ULL << 3) |
| 60 | { .mask = H3_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H3 SETTINGS frame" }, |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 61 | #define H3_EV_TX_DATA (1ULL << 4) |
| 62 | { .mask = H3_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H3 DATA frame" }, |
| 63 | #define H3_EV_TX_HDR (1ULL << 5) |
| 64 | { .mask = H3_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H3 HEADERS frame" }, |
| 65 | #define H3_EV_TX_SETTINGS (1ULL << 6) |
| 66 | { .mask = H3_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H3 SETTINGS frame" }, |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 67 | #define H3_EV_H3S_NEW (1ULL << 7) |
| 68 | { .mask = H3_EV_H3S_NEW, .name = "h3s_new", .desc = "new H3 stream" }, |
| 69 | #define H3_EV_H3S_END (1ULL << 8) |
| 70 | { .mask = H3_EV_H3S_END, .name = "h3s_end", .desc = "H3 stream terminated" }, |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 71 | #define H3_EV_H3C_END (1ULL << 9) |
| 72 | { .mask = H3_EV_H3C_END, .name = "h3c_end", .desc = "H3 connection terminated" }, |
Amaury Denoyelle | 44083ac | 2024-01-29 15:15:27 +0100 | [diff] [blame] | 73 | #define H3_EV_STRM_SEND (1ULL << 12) |
| 74 | { .mask = H3_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 75 | { } |
| 76 | }; |
| 77 | |
| 78 | static const struct name_desc h3_trace_lockon_args[4] = { |
| 79 | /* arg1 */ { /* already used by the connection */ }, |
| 80 | /* arg2 */ { .name="qcs", .desc="QUIC stream" }, |
| 81 | /* arg3 */ { }, |
| 82 | /* arg4 */ { } |
| 83 | }; |
| 84 | |
| 85 | static const struct name_desc h3_trace_decoding[] = { |
| 86 | #define H3_VERB_CLEAN 1 |
| 87 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 88 | #define H3_VERB_MINIMAL 2 |
| 89 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 90 | { /* end */ } |
| 91 | }; |
| 92 | |
| 93 | struct trace_source trace_h3 = { |
| 94 | .name = IST("h3"), |
| 95 | .desc = "HTTP/3 transcoder", |
| 96 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 97 | .default_cb = h3_trace, |
| 98 | .known_events = h3_trace_events, |
| 99 | .lockon_args = h3_trace_lockon_args, |
| 100 | .decoding = h3_trace_decoding, |
| 101 | .report_events = ~0, /* report everything by default */ |
| 102 | }; |
| 103 | |
| 104 | #define TRACE_SOURCE &trace_h3 |
| 105 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 106 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 107 | #if defined(DEBUG_H3) |
| 108 | #define h3_debug_printf fprintf |
| 109 | #define h3_debug_hexdump debug_hexdump |
| 110 | #else |
| 111 | #define h3_debug_printf(...) do { } while (0) |
| 112 | #define h3_debug_hexdump(...) do { } while (0) |
| 113 | #endif |
| 114 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 115 | #define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */ |
| 116 | #define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */ |
| 117 | #define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */ |
| 118 | #define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */ |
| 119 | #define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */ |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 120 | #define H3_CF_GOAWAY_SENT 0x00000020 /* GOAWAY sent on local control stream */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 121 | |
| 122 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 123 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 124 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 125 | static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 126 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 127 | struct h3c { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 128 | struct qcc *qcc; |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 129 | struct qcs *ctrl_strm; /* Control stream */ |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 130 | int err; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 131 | uint32_t flags; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 132 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 133 | /* Settings */ |
| 134 | uint64_t qpack_max_table_capacity; |
| 135 | uint64_t qpack_blocked_streams; |
| 136 | uint64_t max_field_section_size; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 137 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 138 | uint64_t id_goaway; /* stream ID used for a GOAWAY frame */ |
| 139 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 140 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 141 | /* Stats counters */ |
| 142 | struct h3_counters *prx_counters; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 143 | }; |
| 144 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 145 | DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c)); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 146 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 147 | #define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */ |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 148 | #define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */ |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 149 | #define H3_SF_HAVE_CLEN 0x00000004 /* content-length header is present */ |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 150 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 151 | struct h3s { |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 152 | struct h3c *h3c; |
| 153 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 154 | enum h3s_t type; |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 155 | enum h3s_st_req st_req; /* only used for request streams */ |
Amaury Denoyelle | 35d9053 | 2023-01-26 16:03:45 +0100 | [diff] [blame] | 156 | uint64_t demux_frame_len; |
| 157 | uint64_t demux_frame_type; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 158 | |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 159 | unsigned long long body_len; /* known request body length from content-length header if present */ |
| 160 | unsigned long long data_len; /* total length of all parsed DATA */ |
| 161 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 162 | int flags; |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 163 | int err; /* used for stream reset */ |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s)); |
| 167 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 168 | /* Initialize an uni-stream <qcs> by reading its type from <b>. |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 169 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 170 | * Returns the count of consumed bytes or a negative error code. |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 171 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 172 | static ssize_t h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs, |
| 173 | struct buffer *b) |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 174 | { |
| 175 | /* decode unidirectional stream type */ |
| 176 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 177 | uint64_t type; |
| 178 | size_t len = 0, ret; |
| 179 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 180 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 181 | |
Amaury Denoyelle | 8d6d246 | 2023-05-11 16:55:30 +0200 | [diff] [blame] | 182 | /* Function reserved to uni streams. Must be called only once per stream instance. */ |
| 183 | BUG_ON(!quic_stream_is_uni(qcs->id) || h3s->flags & H3_SF_UNI_INIT); |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 184 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 185 | ret = b_quic_dec_int(&type, b, &len); |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 186 | if (!ret) { |
Amaury Denoyelle | 5aa21c1 | 2023-03-09 11:12:32 +0100 | [diff] [blame] | 187 | /* not enough data to decode uni stream type, retry later */ |
| 188 | TRACE_DATA("cannot decode uni stream type due to incomplete data", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 189 | goto out; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | switch (type) { |
| 193 | case H3_UNI_S_T_CTRL: |
| 194 | if (h3c->flags & H3_CF_UNI_CTRL_SET) { |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 195 | TRACE_ERROR("duplicated control stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 196 | qcc_set_error(qcs->qcc, H3_STREAM_CREATION_ERROR, 1); |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 197 | goto err; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 198 | } |
| 199 | h3c->flags |= H3_CF_UNI_CTRL_SET; |
| 200 | h3s->type = H3S_T_CTRL; |
| 201 | break; |
| 202 | |
| 203 | case H3_UNI_S_T_PUSH: |
| 204 | /* TODO not supported for the moment */ |
| 205 | h3s->type = H3S_T_PUSH; |
| 206 | break; |
| 207 | |
| 208 | case H3_UNI_S_T_QPACK_DEC: |
| 209 | if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) { |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 210 | TRACE_ERROR("duplicated qpack decoder stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 211 | qcc_set_error(qcs->qcc, H3_STREAM_CREATION_ERROR, 1); |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 212 | goto err; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 213 | } |
| 214 | h3c->flags |= H3_CF_UNI_QPACK_DEC_SET; |
| 215 | h3s->type = H3S_T_QPACK_DEC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 216 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 217 | break; |
| 218 | |
| 219 | case H3_UNI_S_T_QPACK_ENC: |
| 220 | if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) { |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 221 | TRACE_ERROR("duplicated qpack encoder stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 222 | qcc_set_error(qcs->qcc, H3_STREAM_CREATION_ERROR, 1); |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 223 | goto err; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 224 | } |
| 225 | h3c->flags |= H3_CF_UNI_QPACK_ENC_SET; |
| 226 | h3s->type = H3S_T_QPACK_ENC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 227 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 228 | break; |
| 229 | |
| 230 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 231 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 232 | * |
| 233 | * Implementations MUST [...] abort reading on unidirectional |
| 234 | * streams that have unknown or unsupported types. |
| 235 | */ |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 236 | TRACE_STATE("abort reading on unknown uni stream type", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 663e872 | 2022-12-09 14:58:28 +0100 | [diff] [blame] | 237 | qcc_abort_stream_read(qcs); |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 238 | goto err; |
| 239 | } |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 240 | |
| 241 | h3s->flags |= H3_SF_UNI_INIT; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 242 | |
Amaury Denoyelle | 5aa21c1 | 2023-03-09 11:12:32 +0100 | [diff] [blame] | 243 | out: |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 244 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 245 | return len; |
Amaury Denoyelle | 815c8ce | 2023-03-08 10:25:39 +0100 | [diff] [blame] | 246 | |
| 247 | err: |
| 248 | TRACE_DEVEL("leaving on error", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 249 | return -1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 252 | /* Parse a buffer <b> for a <qcs> uni-stream which does not contains H3 frames. |
| 253 | * This may be used for QPACK encoder/decoder streams for example. <fin> is set |
| 254 | * if this is the last frame of the stream. |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 255 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 256 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 257 | */ |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 258 | static ssize_t h3_parse_uni_stream_no_h3(struct qcs *qcs, struct buffer *b, int fin) |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 259 | { |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 260 | struct h3s *h3s = qcs->ctx; |
| 261 | |
Amaury Denoyelle | 8d6d246 | 2023-05-11 16:55:30 +0200 | [diff] [blame] | 262 | /* Function reserved to non-HTTP/3 unidirectional streams. */ |
| 263 | BUG_ON(!quic_stream_is_uni(qcs->id) || !(h3s->flags & H3_SF_UNI_NO_H3)); |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 264 | |
| 265 | switch (h3s->type) { |
| 266 | case H3S_T_QPACK_DEC: |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 267 | if (qpack_decode_dec(b, fin, qcs)) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 268 | return -1; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 269 | break; |
| 270 | case H3S_T_QPACK_ENC: |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 271 | if (qpack_decode_enc(b, fin, qcs)) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 272 | return -1; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 273 | break; |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 274 | case H3S_T_UNKNOWN: |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 275 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 276 | /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */ |
| 277 | ABORT_NOW(); |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 280 | /* TODO adjust return code */ |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 284 | /* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in |
| 285 | * <ftype> and length in <flen>. |
| 286 | * |
| 287 | * Returns the size of the H3 frame header. Note that the input buffer is not |
| 288 | * consumed. |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 289 | */ |
| 290 | static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen, |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 291 | struct buffer *b) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 292 | { |
| 293 | size_t hlen; |
| 294 | |
| 295 | hlen = 0; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 296 | if (!b_quic_dec_int(ftype, b, &hlen) || |
| 297 | !b_quic_dec_int(flen, b, &hlen)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 298 | return 0; |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 299 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 300 | |
| 301 | return hlen; |
| 302 | } |
| 303 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 304 | /* Check if H3 frame of type <ftype> is valid when received on stream <qcs>. |
| 305 | * |
| 306 | * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should |
| 307 | * be reported. |
| 308 | */ |
| 309 | static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype) |
| 310 | { |
| 311 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 312 | |
Amaury Denoyelle | 8d6d246 | 2023-05-11 16:55:30 +0200 | [diff] [blame] | 313 | /* Stream type must be known to ensure frame is valid for this stream. */ |
| 314 | BUG_ON(h3s->type == H3S_T_UNKNOWN); |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 315 | |
| 316 | switch (ftype) { |
| 317 | case H3_FT_DATA: |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 318 | return h3s->type != H3S_T_CTRL && (h3s->st_req == H3S_ST_REQ_HEADERS || |
| 319 | h3s->st_req == H3S_ST_REQ_DATA); |
| 320 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 321 | case H3_FT_HEADERS: |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 322 | return h3s->type != H3S_T_CTRL && h3s->st_req != H3S_ST_REQ_TRAILERS; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 323 | |
| 324 | case H3_FT_CANCEL_PUSH: |
| 325 | case H3_FT_GOAWAY: |
| 326 | case H3_FT_MAX_PUSH_ID: |
| 327 | /* Only allowed for control stream. First frame of control |
| 328 | * stream MUST be SETTINGS. |
| 329 | */ |
| 330 | return h3s->type == H3S_T_CTRL && |
| 331 | (h3c->flags & H3_CF_SETTINGS_RECV); |
| 332 | |
| 333 | case H3_FT_SETTINGS: |
| 334 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 335 | * |
| 336 | * If an endpoint receives a second SETTINGS frame on the control |
| 337 | * stream, the endpoint MUST respond with a connection error of type |
| 338 | * H3_FRAME_UNEXPECTED. |
| 339 | */ |
| 340 | return h3s->type == H3S_T_CTRL && |
| 341 | !(h3c->flags & H3_CF_SETTINGS_RECV); |
| 342 | |
| 343 | case H3_FT_PUSH_PROMISE: |
Amaury Denoyelle | 68bf6c8 | 2023-11-28 12:00:40 +0100 | [diff] [blame] | 344 | /* RFC 9114 7.2.5. PUSH_PROMISE |
| 345 | * A client MUST NOT send a PUSH_PROMISE frame. A server MUST treat the |
| 346 | * receipt of a PUSH_PROMISE frame as a connection error of type |
| 347 | * H3_FRAME_UNEXPECTED. |
| 348 | */ |
| 349 | |
| 350 | /* TODO server-side only. */ |
| 351 | return 0; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 352 | |
| 353 | default: |
| 354 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 355 | * |
| 356 | * Implementations MUST discard frames [...] that have unknown |
| 357 | * or unsupported types. |
| 358 | */ |
| 359 | return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV); |
| 360 | } |
| 361 | } |
| 362 | |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 363 | /* Check from stream <qcs> that length of all DATA frames does not exceed with |
| 364 | * a previously parsed content-length header. <fin> must be set for the last |
| 365 | * data of the stream so that length of DATA frames must be equal to the |
| 366 | * content-length. |
| 367 | * |
| 368 | * This must only be called for a stream with H3_SF_HAVE_CLEN flag. |
| 369 | * |
| 370 | * Return 0 on valid else non-zero. |
| 371 | */ |
| 372 | static int h3_check_body_size(struct qcs *qcs, int fin) |
| 373 | { |
| 374 | struct h3s *h3s = qcs->ctx; |
| 375 | int ret = 0; |
| 376 | TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 377 | |
| 378 | /* Reserved for streams with a previously parsed content-length header. */ |
| 379 | BUG_ON(!(h3s->flags & H3_SF_HAVE_CLEN)); |
| 380 | |
| 381 | /* RFC 9114 4.1.2. Malformed Requests and Responses |
| 382 | * |
| 383 | * A request or response that is defined as having content when it |
| 384 | * contains a Content-Length header field (Section 8.6 of [HTTP]) is |
| 385 | * malformed if the value of the Content-Length header field does not |
| 386 | * equal the sum of the DATA frame lengths received. |
| 387 | * |
| 388 | * TODO for backend support |
| 389 | * A response that is |
| 390 | * defined as never having content, even when a Content-Length is |
| 391 | * present, can have a non-zero Content-Length header field even though |
| 392 | * no content is included in DATA frames. |
| 393 | */ |
| 394 | if (h3s->data_len > h3s->body_len || |
| 395 | (fin && h3s->data_len < h3s->body_len)) { |
| 396 | TRACE_ERROR("Content-length does not match DATA frame size", H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 397 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 398 | ret = -1; |
| 399 | } |
| 400 | |
| 401 | TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 402 | return ret; |
| 403 | } |
| 404 | |
Amaury Denoyelle | a514a11 | 2023-10-09 16:14:44 +0200 | [diff] [blame] | 405 | /* Set <auth> authority header to the new value <value> for <qcs> stream. This |
| 406 | * ensures that value is conformant to the specification. If <auth> is a |
| 407 | * non-null length string, it ensures that <value> is identical to it. |
| 408 | * |
| 409 | * Returns 0 on success else non-zero. |
| 410 | */ |
| 411 | static int h3_set_authority(struct qcs *qcs, struct ist *auth, const struct ist value) |
| 412 | { |
| 413 | /* RFC 9114 4.3.1. Request Pseudo-Header Fields |
| 414 | * |
| 415 | * If the :scheme pseudo-header field identifies a scheme that has a |
| 416 | * mandatory authority component (including "http" and "https"), the |
| 417 | * request MUST contain either an :authority pseudo-header field or a |
| 418 | * Host header field. If these fields are present, they MUST NOT be |
| 419 | * empty. If both fields are present, they MUST contain the same value. |
| 420 | */ |
| 421 | |
| 422 | /* Check that if a previous value is set the new value is identical. */ |
| 423 | if (isttest(*auth) && !isteq(*auth, value)) { |
| 424 | TRACE_ERROR("difference between :authority and host headers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | /* Check that value is not empty. */ |
| 429 | if (!istlen(value)) { |
| 430 | TRACE_ERROR("empty :authority/host header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | *auth = value; |
| 435 | return 0; |
| 436 | } |
| 437 | |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 438 | /* Return <value> as is or H3_INTERNAL_ERROR if negative. Useful to prepare a standard error code. */ |
| 439 | static int h3_err(const int value) |
| 440 | { |
| 441 | return value >= 0 ? value : H3_INTERNAL_ERROR; |
| 442 | } |
| 443 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 444 | /* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 445 | * in a local HTX buffer and transfer to the stream connector layer. <fin> must be |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 446 | * set if this is the last data to transfer from this stream. |
| 447 | * |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 448 | * Returns the number of consumed bytes or a negative error code. On error |
| 449 | * either the connection should be closed or the stream reset using codes |
| 450 | * provided in h3c.err / h3s.err. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 451 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 452 | static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf, |
| 453 | uint64_t len, char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 454 | { |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 455 | struct h3s *h3s = qcs->ctx; |
| 456 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 457 | struct buffer htx_buf = BUF_NULL; |
| 458 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 459 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 460 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 461 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 462 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 463 | struct ist meth = IST_NULL, path = IST_NULL; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 464 | struct ist scheme = IST_NULL, authority = IST_NULL; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 465 | int hdr_idx, ret; |
Amaury Denoyelle | d6fb7a0 | 2022-12-07 14:31:42 +0100 | [diff] [blame] | 466 | int cookie = -1, last_cookie = -1, i; |
Willy Tarreau | 0404bf1 | 2023-08-08 17:18:27 +0200 | [diff] [blame] | 467 | const char *ctl; |
Willy Tarreau | 96dfea8 | 2023-08-08 17:54:26 +0200 | [diff] [blame] | 468 | int relaxed = !!(h3c->qcc->proxy->options2 & PR_O2_REQBUG_OK); |
Amaury Denoyelle | d6fb7a0 | 2022-12-07 14:31:42 +0100 | [diff] [blame] | 469 | |
| 470 | /* RFC 9114 4.1.2. Malformed Requests and Responses |
| 471 | * |
| 472 | * A malformed request or response is one that is an otherwise valid |
| 473 | * sequence of frames but is invalid due to: |
| 474 | * - the presence of prohibited fields or pseudo-header fields, |
| 475 | * - the absence of mandatory pseudo-header fields, |
| 476 | * - invalid values for pseudo-header fields, |
| 477 | * - pseudo-header fields after fields, |
| 478 | * - an invalid sequence of HTTP messages, |
| 479 | * - the inclusion of uppercase field names, or |
| 480 | * - the inclusion of invalid characters in field names or values. |
| 481 | * |
| 482 | * [...] |
| 483 | * |
| 484 | * Intermediaries that process HTTP requests or responses (i.e., any |
| 485 | * intermediary not acting as a tunnel) MUST NOT forward a malformed |
| 486 | * request or response. Malformed requests or responses that are |
| 487 | * detected MUST be treated as a stream error of type H3_MESSAGE_ERROR. |
| 488 | */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 489 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 490 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 491 | |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 492 | /* TODO support trailer parsing in this function */ |
| 493 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 494 | /* TODO support buffer wrapping */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 495 | BUG_ON(b_head(buf) + len >= b_wrap(buf)); |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 496 | ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, |
| 497 | list, sizeof(list) / sizeof(list[0])); |
| 498 | if (ret < 0) { |
| 499 | TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 500 | h3c->err = h3_err(qpack_err_decode(ret)); |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 501 | len = -1; |
| 502 | goto out; |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 503 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 504 | |
Amaury Denoyelle | d68f8b5 | 2023-05-30 15:04:46 +0200 | [diff] [blame] | 505 | if (!qcs_get_buf(qcs, &htx_buf)) { |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 506 | TRACE_ERROR("HTX buffer alloc failure", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 507 | h3c->err = H3_INTERNAL_ERROR; |
| 508 | len = -1; |
| 509 | goto out; |
| 510 | } |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 511 | BUG_ON(!b_size(&htx_buf)); /* TODO */ |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 512 | htx = htx_from_buf(&htx_buf); |
| 513 | |
| 514 | /* first treat pseudo-header to build the start line */ |
| 515 | hdr_idx = 0; |
| 516 | while (1) { |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 517 | /* RFC 9114 4.3. HTTP Control Data |
| 518 | * |
| 519 | * Endpoints MUST treat a request or response that contains |
| 520 | * undefined or invalid pseudo-header fields as malformed. |
| 521 | * |
| 522 | * All pseudo-header fields MUST appear in the header section before |
| 523 | * regular header fields. Any request or response that contains a |
| 524 | * pseudo-header field that appears in a header section after a regular |
| 525 | * header field MUST be treated as malformed. |
| 526 | */ |
| 527 | |
| 528 | /* Stop at first non pseudo-header. */ |
| 529 | if (!istmatch(list[hdr_idx].n, ist(":"))) |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 530 | break; |
| 531 | |
Willy Tarreau | 0404bf1 | 2023-08-08 17:18:27 +0200 | [diff] [blame] | 532 | /* RFC 9114 10.3 Intermediary-Encapsulation Attacks |
| 533 | * |
| 534 | * While most values that can be encoded will not alter field |
| 535 | * parsing, carriage return (ASCII 0x0d), line feed (ASCII 0x0a), |
| 536 | * and the null character (ASCII 0x00) might be exploited by an |
| 537 | * attacker if they are translated verbatim. Any request or |
| 538 | * response that contains a character not permitted in a field |
| 539 | * value MUST be treated as malformed |
| 540 | */ |
| 541 | |
| 542 | /* look for forbidden control characters in the pseudo-header value */ |
| 543 | ctl = ist_find_ctl(list[hdr_idx].v); |
| 544 | if (unlikely(ctl) && http_header_has_forbidden_char(list[hdr_idx].v, ctl)) { |
| 545 | TRACE_ERROR("control character present in pseudo-header value", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 546 | h3s->err = H3_MESSAGE_ERROR; |
| 547 | len = -1; |
| 548 | goto out; |
| 549 | } |
| 550 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 551 | /* pseudo-header. Malformed name with uppercase character or |
| 552 | * invalid token will be rejected in the else clause. |
| 553 | */ |
| 554 | if (isteq(list[hdr_idx].n, ist(":method"))) { |
| 555 | if (isttest(meth)) { |
| 556 | TRACE_ERROR("duplicated method pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 557 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 558 | len = -1; |
| 559 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 560 | } |
Amaury Denoyelle | 94d305e | 2024-06-28 10:43:19 +0200 | [diff] [blame] | 561 | |
| 562 | if (!istlen(list[hdr_idx].v) || http_method_has_forbidden_char(list[hdr_idx].v)) { |
| 563 | TRACE_ERROR("invalid method pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 564 | h3s->err = H3_MESSAGE_ERROR; |
| 565 | len = -1; |
| 566 | goto out; |
| 567 | } |
| 568 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 569 | meth = list[hdr_idx].v; |
| 570 | } |
| 571 | else if (isteq(list[hdr_idx].n, ist(":path"))) { |
| 572 | if (isttest(path)) { |
| 573 | TRACE_ERROR("duplicated path pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 574 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 575 | len = -1; |
| 576 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 577 | } |
Willy Tarreau | 96dfea8 | 2023-08-08 17:54:26 +0200 | [diff] [blame] | 578 | |
| 579 | if (!relaxed) { |
| 580 | /* we need to reject any control chars or '#' from the path, |
| 581 | * unless option accept-invalid-http-request is set. |
| 582 | */ |
| 583 | ctl = ist_find_range(list[hdr_idx].v, 0, '#'); |
| 584 | if (unlikely(ctl) && http_path_has_forbidden_char(list[hdr_idx].v, ctl)) { |
| 585 | TRACE_ERROR("forbidden character in ':path' pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 586 | h3s->err = H3_MESSAGE_ERROR; |
| 587 | len = -1; |
| 588 | goto out; |
| 589 | } |
| 590 | } |
| 591 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 592 | path = list[hdr_idx].v; |
| 593 | } |
| 594 | else if (isteq(list[hdr_idx].n, ist(":scheme"))) { |
| 595 | if (isttest(scheme)) { |
| 596 | /* duplicated pseudo-header */ |
| 597 | TRACE_ERROR("duplicated scheme pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 598 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 599 | len = -1; |
| 600 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 601 | } |
Amaury Denoyelle | 56ab17d | 2024-06-28 10:50:19 +0200 | [diff] [blame] | 602 | |
| 603 | if (!http_validate_scheme(list[hdr_idx].v)) { |
| 604 | TRACE_ERROR("invalid scheme pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 605 | h3s->err = H3_MESSAGE_ERROR; |
| 606 | len = -1; |
| 607 | goto out; |
| 608 | } |
| 609 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 610 | scheme = list[hdr_idx].v; |
| 611 | } |
| 612 | else if (isteq(list[hdr_idx].n, ist(":authority"))) { |
| 613 | if (isttest(authority)) { |
| 614 | TRACE_ERROR("duplicated authority pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 615 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 616 | len = -1; |
| 617 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 618 | } |
Amaury Denoyelle | a514a11 | 2023-10-09 16:14:44 +0200 | [diff] [blame] | 619 | |
| 620 | if (h3_set_authority(qcs, &authority, list[hdr_idx].v)) { |
| 621 | h3s->err = H3_MESSAGE_ERROR; |
| 622 | len = -1; |
| 623 | goto out; |
| 624 | } |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 625 | } |
| 626 | else { |
| 627 | TRACE_ERROR("unknown pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 628 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 629 | len = -1; |
| 630 | goto out; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | ++hdr_idx; |
| 634 | } |
| 635 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 636 | if (!istmatch(meth, ist("CONNECT"))) { |
| 637 | /* RFC 9114 4.3.1. Request Pseudo-Header Fields |
| 638 | * |
| 639 | * All HTTP/3 requests MUST include exactly one value for the :method, |
| 640 | * :scheme, and :path pseudo-header fields, unless the request is a |
| 641 | * CONNECT request; see Section 4.4. |
| 642 | */ |
| 643 | if (!isttest(meth) || !isttest(scheme) || !isttest(path)) { |
| 644 | TRACE_ERROR("missing mandatory pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 645 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 646 | len = -1; |
| 647 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 651 | flags |= HTX_SL_F_VER_11; |
Amaury Denoyelle | 0fa14a6 | 2022-04-26 16:24:39 +0200 | [diff] [blame] | 652 | flags |= HTX_SL_F_XFER_LEN; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 653 | |
| 654 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0")); |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 655 | if (!sl) { |
| 656 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 657 | len = -1; |
| 658 | goto out; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 659 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 660 | |
| 661 | if (fin) |
| 662 | sl->flags |= HTX_SL_F_BODYLESS; |
| 663 | |
| 664 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 665 | |
Amaury Denoyelle | c4913f6 | 2022-12-15 10:58:05 +0100 | [diff] [blame] | 666 | if (isttest(authority)) { |
| 667 | if (!htx_add_header(htx, ist("host"), authority)) { |
| 668 | h3c->err = H3_INTERNAL_ERROR; |
| 669 | len = -1; |
| 670 | goto out; |
| 671 | } |
| 672 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 673 | |
| 674 | /* now treat standard headers */ |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 675 | while (1) { |
| 676 | if (isteq(list[hdr_idx].n, ist(""))) |
| 677 | break; |
| 678 | |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 679 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 680 | TRACE_ERROR("pseudo-header field after fields", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 681 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 682 | len = -1; |
| 683 | goto out; |
Amaury Denoyelle | 7b5a671 | 2022-12-07 14:33:26 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Amaury Denoyelle | d6fb7a0 | 2022-12-07 14:31:42 +0100 | [diff] [blame] | 686 | for (i = 0; i < list[hdr_idx].n.len; ++i) { |
| 687 | const char c = list[hdr_idx].n.ptr[i]; |
| 688 | if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) { |
| 689 | TRACE_ERROR("invalid characters in field name", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 690 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 691 | len = -1; |
| 692 | goto out; |
Amaury Denoyelle | d6fb7a0 | 2022-12-07 14:31:42 +0100 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Willy Tarreau | 0404bf1 | 2023-08-08 17:18:27 +0200 | [diff] [blame] | 696 | |
| 697 | /* RFC 9114 10.3 Intermediary-Encapsulation Attacks |
| 698 | * |
| 699 | * While most values that can be encoded will not alter field |
| 700 | * parsing, carriage return (ASCII 0x0d), line feed (ASCII 0x0a), |
| 701 | * and the null character (ASCII 0x00) might be exploited by an |
| 702 | * attacker if they are translated verbatim. Any request or |
| 703 | * response that contains a character not permitted in a field |
| 704 | * value MUST be treated as malformed |
| 705 | */ |
| 706 | |
| 707 | /* look for forbidden control characters in the header value */ |
| 708 | ctl = ist_find_ctl(list[hdr_idx].v); |
| 709 | if (unlikely(ctl) && http_header_has_forbidden_char(list[hdr_idx].v, ctl)) { |
| 710 | TRACE_ERROR("control character present in header value", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 711 | h3s->err = H3_MESSAGE_ERROR; |
| 712 | len = -1; |
| 713 | goto out; |
| 714 | } |
| 715 | |
Amaury Denoyelle | a514a11 | 2023-10-09 16:14:44 +0200 | [diff] [blame] | 716 | if (isteq(list[hdr_idx].n, ist("host"))) { |
| 717 | if (h3_set_authority(qcs, &authority, list[hdr_idx].v)) { |
| 718 | h3s->err = H3_MESSAGE_ERROR; |
| 719 | len = -1; |
| 720 | goto out; |
| 721 | } |
| 722 | } |
| 723 | else if (isteq(list[hdr_idx].n, ist("cookie"))) { |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 724 | http_cookie_register(list, hdr_idx, &cookie, &last_cookie); |
Amaury Denoyelle | 19942e3 | 2022-12-15 09:18:25 +0100 | [diff] [blame] | 725 | ++hdr_idx; |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 726 | continue; |
| 727 | } |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 728 | else if (isteq(list[hdr_idx].n, ist("content-length"))) { |
| 729 | ret = http_parse_cont_len_header(&list[hdr_idx].v, |
| 730 | &h3s->body_len, |
| 731 | h3s->flags & H3_SF_HAVE_CLEN); |
| 732 | if (ret < 0) { |
| 733 | TRACE_ERROR("invalid content-length", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 734 | h3s->err = H3_MESSAGE_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 735 | len = -1; |
| 736 | goto out; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 737 | } |
| 738 | else if (!ret) { |
| 739 | /* Skip duplicated value. */ |
| 740 | ++hdr_idx; |
| 741 | continue; |
| 742 | } |
| 743 | |
| 744 | h3s->flags |= H3_SF_HAVE_CLEN; |
Christopher Faulet | 87230d3 | 2023-07-24 11:37:10 +0200 | [diff] [blame] | 745 | sl->flags |= HTX_SL_F_CLEN; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 746 | /* This will fail if current frame is the last one and |
| 747 | * content-length is not null. |
| 748 | */ |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 749 | if (h3_check_body_size(qcs, fin)) { |
| 750 | len = -1; |
| 751 | goto out; |
| 752 | } |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 753 | } |
Amaury Denoyelle | 8ad2669 | 2023-01-17 17:47:06 +0100 | [diff] [blame] | 754 | else if (isteq(list[hdr_idx].n, ist("connection")) || |
| 755 | isteq(list[hdr_idx].n, ist("proxy-connection")) || |
| 756 | isteq(list[hdr_idx].n, ist("keep-alive")) || |
| 757 | isteq(list[hdr_idx].n, ist("transfer-encoding"))) { |
| 758 | /* RFC 9114 4.2. HTTP Fields |
| 759 | * |
| 760 | * HTTP/3 does not use the Connection header field to indicate |
| 761 | * connection-specific fields; in this protocol, connection- |
| 762 | * specific metadata is conveyed by other means. An endpoint |
| 763 | * MUST NOT generate an HTTP/3 field section containing |
| 764 | * connection-specific fields; any message containing |
| 765 | * connection-specific fields MUST be treated as malformed. |
| 766 | */ |
| 767 | TRACE_ERROR("invalid connection header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 768 | h3s->err = H3_MESSAGE_ERROR; |
| 769 | len = -1; |
| 770 | goto out; |
| 771 | } |
| 772 | else if (isteq(list[hdr_idx].n, ist("te")) && |
| 773 | !isteq(list[hdr_idx].v, ist("trailers"))) { |
| 774 | /* RFC 9114 4.2. HTTP Fields |
| 775 | * |
| 776 | * The only exception to this is the TE header field, which MAY |
| 777 | * be present in an HTTP/3 request header; when it is, it MUST |
| 778 | * NOT contain any value other than "trailers". |
| 779 | */ |
| 780 | TRACE_ERROR("invalid te header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 781 | h3s->err = H3_MESSAGE_ERROR; |
| 782 | len = -1; |
| 783 | goto out; |
| 784 | } |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 785 | |
Amaury Denoyelle | c4913f6 | 2022-12-15 10:58:05 +0100 | [diff] [blame] | 786 | if (!htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v)) { |
| 787 | h3c->err = H3_INTERNAL_ERROR; |
| 788 | len = -1; |
| 789 | goto out; |
| 790 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 791 | ++hdr_idx; |
| 792 | } |
| 793 | |
Amaury Denoyelle | a514a11 | 2023-10-09 16:14:44 +0200 | [diff] [blame] | 794 | /* RFC 9114 4.3.1. Request Pseudo-Header Fields |
| 795 | * |
| 796 | * If the :scheme pseudo-header field identifies a scheme that has a |
| 797 | * mandatory authority component (including "http" and "https"), the |
| 798 | * request MUST contain either an :authority pseudo-header field or a |
| 799 | * Host header field. |
| 800 | */ |
| 801 | if (!isttest(authority)) { |
| 802 | TRACE_ERROR("missing mandatory pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 803 | h3s->err = H3_MESSAGE_ERROR; |
| 804 | len = -1; |
| 805 | goto out; |
| 806 | } |
| 807 | |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 808 | if (cookie >= 0) { |
| 809 | if (http_cookie_merge(htx, list, cookie)) { |
| 810 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 811 | len = -1; |
| 812 | goto out; |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Amaury Denoyelle | c4913f6 | 2022-12-15 10:58:05 +0100 | [diff] [blame] | 816 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 817 | h3c->err = H3_INTERNAL_ERROR; |
| 818 | len = -1; |
| 819 | goto out; |
| 820 | } |
| 821 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 822 | if (fin) |
| 823 | htx->flags |= HTX_FL_EOM; |
| 824 | |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 825 | htx_to_buf(htx, &htx_buf); |
| 826 | htx = NULL; |
| 827 | |
Amaury Denoyelle | d68f8b5 | 2023-05-30 15:04:46 +0200 | [diff] [blame] | 828 | if (!qcs_attach_sc(qcs, &htx_buf, fin)) { |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 829 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 830 | len = -1; |
| 831 | goto out; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 832 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 833 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 834 | /* RFC 9114 5.2. Connection Shutdown |
| 835 | * |
| 836 | * The GOAWAY frame contains an identifier that |
| 837 | * indicates to the receiver the range of requests or pushes that were |
| 838 | * or might be processed in this connection. The server sends a client- |
| 839 | * initiated bidirectional stream ID; the client sends a push ID. |
| 840 | * Requests or pushes with the indicated identifier or greater are |
| 841 | * rejected (Section 4.1.1) by the sender of the GOAWAY. This |
| 842 | * identifier MAY be zero if no requests or pushes were processed. |
| 843 | */ |
| 844 | if (qcs->id >= h3c->id_goaway) |
| 845 | h3c->id_goaway = qcs->id + 4; |
| 846 | |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 847 | out: |
| 848 | /* HTX may be non NULL if error before previous htx_to_buf(). */ |
| 849 | if (htx) |
| 850 | htx_to_buf(htx, &htx_buf); |
| 851 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 852 | /* buffer is transferred to the stream connector and set to NULL |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 853 | * except on stream creation error. |
| 854 | */ |
Amaury Denoyelle | 788fc05 | 2022-12-15 10:53:55 +0100 | [diff] [blame] | 855 | if (b_size(&htx_buf)) { |
| 856 | b_free(&htx_buf); |
| 857 | offer_buffers(NULL, 1); |
| 858 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 859 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 860 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 861 | return len; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 864 | /* Parse from buffer <buf> a H3 HEADERS frame of length <len> used as trailers. |
| 865 | * Data are copied in a local HTX buffer and transfer to the stream connector |
| 866 | * layer. <fin> must be set if this is the last data to transfer from this |
| 867 | * stream. |
| 868 | * |
| 869 | * Returns the number of consumed bytes or a negative error code. On error |
| 870 | * either the connection should be closed or the stream reset using codes |
| 871 | * provided in h3c.err / h3s.err. |
| 872 | */ |
| 873 | static ssize_t h3_trailers_to_htx(struct qcs *qcs, const struct buffer *buf, |
| 874 | uint64_t len, char fin) |
| 875 | { |
| 876 | struct h3s *h3s = qcs->ctx; |
| 877 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 878 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 29058f7 | 2023-11-28 15:59:38 +0100 | [diff] [blame] | 879 | struct buffer *appbuf = NULL; |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 880 | struct htx *htx = NULL; |
| 881 | struct htx_sl *sl; |
| 882 | struct http_hdr list[global.tune.max_http_hdr]; |
| 883 | int hdr_idx, ret; |
Willy Tarreau | 0404bf1 | 2023-08-08 17:18:27 +0200 | [diff] [blame] | 884 | const char *ctl; |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 885 | int i; |
| 886 | |
| 887 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 888 | |
| 889 | /* TODO support buffer wrapping */ |
| 890 | BUG_ON(b_head(buf) + len >= b_wrap(buf)); |
| 891 | ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, |
| 892 | list, sizeof(list) / sizeof(list[0])); |
| 893 | if (ret < 0) { |
| 894 | TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 895 | h3c->err = h3_err(qpack_err_decode(ret)); |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 896 | len = -1; |
| 897 | goto out; |
| 898 | } |
| 899 | |
Amaury Denoyelle | 29058f7 | 2023-11-28 15:59:38 +0100 | [diff] [blame] | 900 | if (!(appbuf = qcs_get_buf(qcs, &qcs->rx.app_buf))) { |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 901 | TRACE_ERROR("HTX buffer alloc failure", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 902 | h3c->err = H3_INTERNAL_ERROR; |
| 903 | len = -1; |
| 904 | goto out; |
| 905 | } |
Amaury Denoyelle | 29058f7 | 2023-11-28 15:59:38 +0100 | [diff] [blame] | 906 | BUG_ON(!b_size(appbuf)); /* TODO */ |
| 907 | htx = htx_from_buf(appbuf); |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 908 | |
| 909 | if (!h3s->data_len) { |
| 910 | /* Notify that no body is present. This can only happens if |
| 911 | * there is H3 HEADERS as trailers without or empty H3 DATA |
| 912 | * frame. So this is probably not realistice ? |
| 913 | * |
| 914 | * TODO if sl is NULL because already consumed there is no way |
| 915 | * to notify about missing body. |
| 916 | */ |
| 917 | sl = http_get_stline(htx); |
| 918 | if (sl) |
| 919 | sl->flags |= HTX_SL_F_BODYLESS; |
| 920 | else |
| 921 | TRACE_ERROR("cannot notify missing body after trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 922 | } |
| 923 | |
| 924 | hdr_idx = 0; |
| 925 | while (1) { |
| 926 | if (isteq(list[hdr_idx].n, ist(""))) |
| 927 | break; |
| 928 | |
| 929 | /* RFC 9114 4.3. HTTP Control Data |
| 930 | * |
| 931 | * Pseudo-header |
| 932 | * fields MUST NOT appear in trailer sections. |
| 933 | */ |
| 934 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 935 | TRACE_ERROR("pseudo-header field in trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 936 | h3s->err = H3_MESSAGE_ERROR; |
| 937 | len = -1; |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | for (i = 0; i < list[hdr_idx].n.len; ++i) { |
| 942 | const char c = list[hdr_idx].n.ptr[i]; |
| 943 | if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) { |
| 944 | TRACE_ERROR("invalid characters in field name", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 945 | h3s->err = H3_MESSAGE_ERROR; |
| 946 | len = -1; |
| 947 | goto out; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /* forbidden HTTP/3 headers, cf h3_headers_to_htx() */ |
| 952 | if (isteq(list[hdr_idx].n, ist("host")) || |
| 953 | isteq(list[hdr_idx].n, ist("content-length")) || |
| 954 | isteq(list[hdr_idx].n, ist("connection")) || |
| 955 | isteq(list[hdr_idx].n, ist("proxy-connection")) || |
| 956 | isteq(list[hdr_idx].n, ist("keep-alive")) || |
| 957 | isteq(list[hdr_idx].n, ist("te")) || |
| 958 | isteq(list[hdr_idx].n, ist("transfer-encoding"))) { |
| 959 | TRACE_ERROR("forbidden HTTP/3 headers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 960 | h3s->err = H3_MESSAGE_ERROR; |
| 961 | len = -1; |
| 962 | goto out; |
| 963 | } |
| 964 | |
Willy Tarreau | 0404bf1 | 2023-08-08 17:18:27 +0200 | [diff] [blame] | 965 | /* RFC 9114 10.3 Intermediary-Encapsulation Attacks |
| 966 | * |
| 967 | * While most values that can be encoded will not alter field |
| 968 | * parsing, carriage return (ASCII 0x0d), line feed (ASCII 0x0a), |
| 969 | * and the null character (ASCII 0x00) might be exploited by an |
| 970 | * attacker if they are translated verbatim. Any request or |
| 971 | * response that contains a character not permitted in a field |
| 972 | * value MUST be treated as malformed |
| 973 | */ |
| 974 | |
| 975 | /* look for forbidden control characters in the trailer value */ |
| 976 | ctl = ist_find_ctl(list[hdr_idx].v); |
| 977 | if (unlikely(ctl) && http_header_has_forbidden_char(list[hdr_idx].v, ctl)) { |
| 978 | TRACE_ERROR("control character present in trailer value", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 979 | h3s->err = H3_MESSAGE_ERROR; |
| 980 | len = -1; |
| 981 | goto out; |
| 982 | } |
| 983 | |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 984 | if (!htx_add_trailer(htx, list[hdr_idx].n, list[hdr_idx].v)) { |
| 985 | TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 986 | h3c->err = H3_INTERNAL_ERROR; |
| 987 | len = -1; |
| 988 | goto out; |
| 989 | } |
| 990 | |
| 991 | ++hdr_idx; |
| 992 | } |
| 993 | |
| 994 | if (!htx_add_endof(htx, HTX_BLK_EOT)) { |
| 995 | TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 996 | h3c->err = H3_INTERNAL_ERROR; |
| 997 | len = -1; |
| 998 | goto out; |
| 999 | } |
| 1000 | |
| 1001 | if (fin) |
| 1002 | htx->flags |= HTX_FL_EOM; |
| 1003 | |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 1004 | out: |
| 1005 | /* HTX may be non NULL if error before previous htx_to_buf(). */ |
Amaury Denoyelle | 29058f7 | 2023-11-28 15:59:38 +0100 | [diff] [blame] | 1006 | if (appbuf) |
| 1007 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 1008 | |
| 1009 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 1010 | return len; |
| 1011 | } |
| 1012 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1013 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 1014 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 1015 | * stream. |
| 1016 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1017 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1018 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1019 | static ssize_t h3_data_to_htx(struct qcs *qcs, const struct buffer *buf, |
| 1020 | uint64_t len, char fin) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1021 | { |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 1022 | struct h3s *h3s = qcs->ctx; |
| 1023 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1024 | struct buffer *appbuf; |
| 1025 | struct htx *htx = NULL; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 1026 | size_t htx_sent = 0; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1027 | int htx_space; |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1028 | char *head; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1029 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 1030 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
| 1031 | |
Amaury Denoyelle | d68f8b5 | 2023-05-30 15:04:46 +0200 | [diff] [blame] | 1032 | if (!(appbuf = qcs_get_buf(qcs, &qcs->rx.app_buf))) { |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 1033 | TRACE_ERROR("data buffer alloc failure", H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
| 1034 | h3c->err = H3_INTERNAL_ERROR; |
| 1035 | len = -1; |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1039 | htx = htx_from_buf(appbuf); |
| 1040 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1041 | if (len > b_data(buf)) { |
| 1042 | len = b_data(buf); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1043 | fin = 0; |
| 1044 | } |
| 1045 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1046 | head = b_head(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1047 | retry: |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1048 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1049 | if (!htx_space) { |
| 1050 | qcs->flags |= QC_SF_DEM_FULL; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1051 | goto out; |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 1052 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1053 | |
| 1054 | if (len > htx_space) { |
| 1055 | len = htx_space; |
| 1056 | fin = 0; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 1057 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1058 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1059 | if (head + len > b_wrap(buf)) { |
| 1060 | size_t contig = b_wrap(buf) - head; |
| 1061 | htx_sent = htx_add_data(htx, ist2(b_head(buf), contig)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 1062 | if (htx_sent < contig) { |
| 1063 | qcs->flags |= QC_SF_DEM_FULL; |
| 1064 | goto out; |
| 1065 | } |
| 1066 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1067 | len -= contig; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1068 | head = b_orig(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1069 | goto retry; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 1070 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1071 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1072 | htx_sent += htx_add_data(htx, ist2(head, len)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 1073 | if (htx_sent < len) { |
| 1074 | qcs->flags |= QC_SF_DEM_FULL; |
| 1075 | goto out; |
| 1076 | } |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 1077 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1078 | if (fin && len == htx_sent) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1079 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1080 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1081 | out: |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 1082 | if (appbuf) |
| 1083 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 1084 | |
| 1085 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 1086 | return htx_sent; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1089 | /* Parse a SETTINGS frame of length <len> of payload <buf>. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1090 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1091 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1092 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1093 | static ssize_t h3_parse_settings_frm(struct h3c *h3c, const struct buffer *buf, |
| 1094 | size_t len) |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1095 | { |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1096 | struct buffer b; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1097 | uint64_t id, value; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1098 | size_t ret = 0; |
| 1099 | long mask = 0; /* used to detect duplicated settings identifier */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1100 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 1101 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn); |
| 1102 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1103 | /* Work on a copy of <buf>. */ |
Amaury Denoyelle | 3a2fcfd | 2022-06-09 11:54:38 +0200 | [diff] [blame] | 1104 | b = b_make(b_orig(buf), b_size(buf), b_head_ofs(buf), len); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1105 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1106 | while (b_data(&b)) { |
| 1107 | if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) { |
| 1108 | h3c->err = H3_FRAME_ERROR; |
| 1109 | return -1; |
| 1110 | } |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1111 | |
| 1112 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 1113 | __func__, (unsigned long long)id, (unsigned long long)value); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1114 | |
| 1115 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 1116 | * |
| 1117 | * The same setting identifier MUST NOT occur more than once in the |
| 1118 | * SETTINGS frame. A receiver MAY treat the presence of duplicate |
| 1119 | * setting identifiers as a connection error of type H3_SETTINGS_ERROR. |
| 1120 | */ |
| 1121 | |
| 1122 | /* Ignore duplicate check for ID too big used for GREASE. */ |
| 1123 | if (id < sizeof(mask)) { |
| 1124 | if (ha_bit_test(id, &mask)) { |
| 1125 | h3c->err = H3_SETTINGS_ERROR; |
| 1126 | return -1; |
| 1127 | } |
| 1128 | ha_bit_set(id, &mask); |
| 1129 | } |
| 1130 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1131 | switch (id) { |
| 1132 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 1133 | h3c->qpack_max_table_capacity = value; |
| 1134 | break; |
| 1135 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
| 1136 | h3c->max_field_section_size = value; |
| 1137 | break; |
| 1138 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
| 1139 | h3c->qpack_blocked_streams = value; |
| 1140 | break; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1141 | |
| 1142 | case H3_SETTINGS_RESERVED_0: |
| 1143 | case H3_SETTINGS_RESERVED_2: |
| 1144 | case H3_SETTINGS_RESERVED_3: |
| 1145 | case H3_SETTINGS_RESERVED_4: |
| 1146 | case H3_SETTINGS_RESERVED_5: |
| 1147 | /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters |
| 1148 | * |
| 1149 | * Setting identifiers which were defined in [HTTP2] where there is no |
| 1150 | * corresponding HTTP/3 setting have also been reserved |
| 1151 | * (Section 11.2.2). These reserved settings MUST NOT be sent, and |
| 1152 | * their receipt MUST be treated as a connection error of type |
| 1153 | * H3_SETTINGS_ERROR. |
| 1154 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1155 | h3c->err = H3_SETTINGS_ERROR; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1156 | return -1; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1157 | default: |
| 1158 | /* MUST be ignored */ |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Frédéric Lécaille | befcf70 | 2022-09-08 16:04:55 +0200 | [diff] [blame] | 1163 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1164 | return ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 1167 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 1168 | * that we received the last data of the stream. |
Amaury Denoyelle | 0ffd6e7 | 2022-05-24 11:07:28 +0200 | [diff] [blame] | 1169 | * |
| 1170 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 1171 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1172 | static ssize_t h3_decode_qcs(struct qcs *qcs, struct buffer *b, int fin) |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 1173 | { |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1174 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 1175 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1176 | ssize_t total = 0, ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 1177 | |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1178 | TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1179 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1180 | if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) { |
Amaury Denoyelle | 5aa21c1 | 2023-03-09 11:12:32 +0100 | [diff] [blame] | 1181 | ret = h3_init_uni_stream(h3c, qcs, b); |
| 1182 | if (ret < 0) { |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1183 | TRACE_ERROR("cannot initialize uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 1184 | goto err; |
| 1185 | } |
Amaury Denoyelle | 5aa21c1 | 2023-03-09 11:12:32 +0100 | [diff] [blame] | 1186 | else if (!ret) { |
| 1187 | /* not enough data to initialize uni stream, retry later */ |
| 1188 | goto done; |
| 1189 | } |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1190 | |
| 1191 | total += ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) { |
| 1195 | /* For non-h3 STREAM, parse it and return immediately. */ |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1196 | if ((ret = h3_parse_uni_stream_no_h3(qcs, b, fin)) < 0) { |
| 1197 | TRACE_ERROR("error when parsing non-HTTP3 uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 1198 | goto err; |
| 1199 | } |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1200 | |
| 1201 | total += ret; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1202 | goto done; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Amaury Denoyelle | 6b02c6b | 2022-08-16 17:16:47 +0200 | [diff] [blame] | 1205 | /* RFC 9114 6.2.1. Control Streams |
| 1206 | * |
| 1207 | * The sender MUST NOT close the control stream, and the receiver MUST NOT |
| 1208 | * request that the sender close the control stream. If either control |
| 1209 | * stream is closed at any point, this MUST be treated as a connection |
| 1210 | * error of type H3_CLOSED_CRITICAL_STREAM. |
| 1211 | */ |
| 1212 | if (h3s->type == H3S_T_CTRL && fin) { |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1213 | TRACE_ERROR("control stream closed by remote peer", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1214 | qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1215 | goto err; |
Amaury Denoyelle | 6b02c6b | 2022-08-16 17:16:47 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 1218 | if (!b_data(b) && fin && quic_stream_is_bidi(qcs->id)) { |
Amaury Denoyelle | 93dd23c | 2023-05-11 16:49:28 +0200 | [diff] [blame] | 1219 | struct buffer *appbuf; |
| 1220 | struct htx *htx; |
| 1221 | |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1222 | TRACE_PROTO("received FIN without data", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | d68f8b5 | 2023-05-30 15:04:46 +0200 | [diff] [blame] | 1223 | if (!(appbuf = qcs_get_buf(qcs, &qcs->rx.app_buf))) { |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 1224 | TRACE_ERROR("data buffer alloc failure", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 1225 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 6133aba | 2023-05-15 09:35:59 +0200 | [diff] [blame] | 1226 | goto err; |
Amaury Denoyelle | 0abde9d | 2023-05-11 16:52:17 +0200 | [diff] [blame] | 1227 | } |
Amaury Denoyelle | 93dd23c | 2023-05-11 16:49:28 +0200 | [diff] [blame] | 1228 | |
| 1229 | htx = htx_from_buf(appbuf); |
| 1230 | if (!htx_set_eom(htx)) { |
| 1231 | TRACE_ERROR("cannot set EOM", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 1232 | h3c->err = H3_INTERNAL_ERROR; |
| 1233 | } |
| 1234 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1235 | goto done; |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1238 | while (b_data(b) && !(qcs->flags & QC_SF_DEM_FULL) && !h3c->err && !h3s->err) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1239 | uint64_t ftype, flen; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 1240 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1241 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1242 | if (!h3s->demux_frame_len) { |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1243 | /* Switch to a new frame. */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1244 | size_t hlen = h3_decode_frm_header(&ftype, &flen, b); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1245 | if (!hlen) { |
| 1246 | TRACE_PROTO("pause parsing on incomplete frame header", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1247 | break; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1248 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1249 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1250 | h3s->demux_frame_type = ftype; |
| 1251 | h3s->demux_frame_len = flen; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1252 | total += hlen; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1253 | TRACE_PROTO("parsing a new frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 1254 | |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1255 | /* Check that content-length is not exceeded on a new DATA frame. */ |
| 1256 | if (ftype == H3_FT_DATA) { |
| 1257 | h3s->data_len += flen; |
Christopher Faulet | 3809fe9 | 2023-07-28 09:33:29 +0200 | [diff] [blame] | 1258 | if (h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, (fin && flen == b_data(b)))) |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1259 | break; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 1262 | if (!h3_is_frame_valid(h3c, qcs, ftype)) { |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1263 | TRACE_ERROR("received an invalid frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1264 | qcc_set_error(qcs->qcc, H3_FRAME_UNEXPECTED, 1); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1265 | goto err; |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1268 | if (!b_data(b)) |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 1269 | break; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1270 | } |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 1271 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1272 | flen = h3s->demux_frame_len; |
| 1273 | ftype = h3s->demux_frame_type; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1274 | |
| 1275 | /* Do not demux incomplete frames except H3 DATA which can be |
| 1276 | * fragmented in multiple HTX blocks. |
| 1277 | */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1278 | if (flen > b_data(b) && ftype != H3_FT_DATA) { |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1279 | /* Reject frames bigger than bufsize. |
| 1280 | * |
| 1281 | * TODO HEADERS should in complement be limited with H3 |
| 1282 | * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent |
| 1283 | * excessive decompressed size. |
| 1284 | */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1285 | if (flen > QC_S_RX_BUF_SZ) { |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1286 | TRACE_ERROR("received a too big frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1287 | qcc_set_error(qcs->qcc, H3_EXCESSIVE_LOAD, 1); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1288 | goto err; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1289 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1290 | break; |
Amaury Denoyelle | b5454d4 | 2022-05-12 16:56:16 +0200 | [diff] [blame] | 1291 | } |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1292 | |
Christopher Faulet | 3809fe9 | 2023-07-28 09:33:29 +0200 | [diff] [blame] | 1293 | last_stream_frame = (fin && flen == b_data(b)); |
| 1294 | |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1295 | /* Check content-length equality with DATA frames length on the last frame. */ |
Christopher Faulet | 3809fe9 | 2023-07-28 09:33:29 +0200 | [diff] [blame] | 1296 | if (last_stream_frame && h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, last_stream_frame)) |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1297 | break; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1298 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1299 | h3_inc_frame_type_cnt(h3c->prx_counters, ftype); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1300 | switch (ftype) { |
| 1301 | case H3_FT_DATA: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1302 | ret = h3_data_to_htx(qcs, b, flen, last_stream_frame); |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 1303 | h3s->st_req = H3S_ST_REQ_DATA; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1304 | break; |
| 1305 | case H3_FT_HEADERS: |
Amaury Denoyelle | edfcb55 | 2023-01-13 16:40:31 +0100 | [diff] [blame] | 1306 | if (h3s->st_req == H3S_ST_REQ_BEFORE) { |
| 1307 | ret = h3_headers_to_htx(qcs, b, flen, last_stream_frame); |
| 1308 | h3s->st_req = H3S_ST_REQ_HEADERS; |
| 1309 | } |
| 1310 | else { |
| 1311 | ret = h3_trailers_to_htx(qcs, b, flen, last_stream_frame); |
| 1312 | h3s->st_req = H3S_ST_REQ_TRAILERS; |
| 1313 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1314 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1315 | case H3_FT_CANCEL_PUSH: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1316 | case H3_FT_PUSH_PROMISE: |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1317 | case H3_FT_MAX_PUSH_ID: |
| 1318 | case H3_FT_GOAWAY: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1319 | /* Not supported */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1320 | ret = flen; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1321 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1322 | case H3_FT_SETTINGS: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1323 | ret = h3_parse_settings_frm(qcs->qcc->ctx, b, flen); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1324 | if (ret < 0) { |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1325 | TRACE_ERROR("error on SETTINGS parsing", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1326 | qcc_set_error(qcs->qcc, h3c->err, 1); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1327 | goto err; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 1328 | } |
| 1329 | h3c->flags |= H3_CF_SETTINGS_RECV; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 1330 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1331 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 1332 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 1333 | * |
| 1334 | * Implementations MUST discard frames [...] that have unknown |
| 1335 | * or unsupported types. |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 1336 | */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 1337 | ret = flen; |
| 1338 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1339 | } |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 1340 | |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1341 | if (ret > 0) { |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 1342 | BUG_ON(h3s->demux_frame_len < ret); |
| 1343 | h3s->demux_frame_len -= ret; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 1344 | b_del(b, ret); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1345 | total += ret; |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 1346 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1347 | } |
| 1348 | |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1349 | /* Reset demux frame type for traces. */ |
| 1350 | if (!h3s->demux_frame_len) |
| 1351 | h3s->demux_frame_type = H3_FT_UNINIT; |
| 1352 | |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1353 | /* Interrupt decoding on stream/connection error detected. */ |
| 1354 | if (h3s->err) { |
| 1355 | qcc_abort_stream_read(qcs); |
| 1356 | qcc_reset_stream(qcs, h3s->err); |
| 1357 | return b_data(b); |
| 1358 | } |
| 1359 | else if (h3c->err) { |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1360 | qcc_set_error(qcs->qcc, h3c->err, 1); |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1361 | return b_data(b); |
| 1362 | } |
| 1363 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 1364 | /* TODO may be useful to wakeup the MUX if blocked due to full buffer. |
| 1365 | * However, currently, io-cb of MUX does not handle Rx. |
| 1366 | */ |
| 1367 | |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1368 | done: |
| 1369 | TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 1370 | return total; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1371 | |
| 1372 | err: |
| 1373 | TRACE_DEVEL("leaving on error", H3_EV_RX_FRAME, qcs->qcc->conn, qcs); |
| 1374 | return -1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1377 | /* Returns buffer for data sending. |
| 1378 | * May be NULL if the allocation failed. |
| 1379 | */ |
| 1380 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1381 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1382 | if (!b_size(&qcs->tx.buf)) |
| 1383 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1384 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1385 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 1388 | /* Function used to emit stream data from <qcs> control uni-stream. |
| 1389 | * |
| 1390 | * On success return the number of sent bytes. A negative code is used on |
| 1391 | * error. |
| 1392 | */ |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 1393 | static int h3_control_send(struct qcs *qcs, void *ctx) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1394 | { |
| 1395 | int ret; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1396 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1397 | unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */ |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1398 | struct buffer pos, *res; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1399 | size_t frm_len; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1400 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1401 | TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
| 1402 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1403 | BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1404 | |
| 1405 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1406 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1407 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1408 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 1409 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 1410 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 1411 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 1412 | if (h3_settings_max_field_section_size) { |
| 1413 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 1414 | quic_int_getsize(h3_settings_max_field_section_size); |
| 1415 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1416 | |
Amaury Denoyelle | 7d78eff | 2023-01-17 15:21:16 +0100 | [diff] [blame] | 1417 | b_quic_enc_int(&pos, H3_UNI_S_T_CTRL, 0); |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1418 | /* Build a SETTINGS frame */ |
Amaury Denoyelle | 7d78eff | 2023-01-17 15:21:16 +0100 | [diff] [blame] | 1419 | b_quic_enc_int(&pos, H3_FT_SETTINGS, 0); |
| 1420 | b_quic_enc_int(&pos, frm_len, 0); |
| 1421 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0); |
| 1422 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity, 0); |
| 1423 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS, 0); |
| 1424 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams, 0); |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1425 | if (h3_settings_max_field_section_size) { |
Amaury Denoyelle | 7d78eff | 2023-01-17 15:21:16 +0100 | [diff] [blame] | 1426 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE, 0); |
| 1427 | b_quic_enc_int(&pos, h3_settings_max_field_section_size, 0); |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1428 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 1429 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1430 | res = mux_get_buf(qcs); |
Amaury Denoyelle | d70f133 | 2024-01-29 14:39:19 +0100 | [diff] [blame] | 1431 | if (b_is_null(res)) { |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 1432 | TRACE_ERROR("cannot allocate Tx buffer", H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
| 1433 | goto err; |
| 1434 | } |
| 1435 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1436 | if (b_room(res) < b_data(&pos)) { |
| 1437 | // TODO the mux should be put in blocked state, with |
| 1438 | // the stream in state waiting for settings to be sent |
| 1439 | ABORT_NOW(); |
| 1440 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1441 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1442 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1443 | if (ret > 0) { |
| 1444 | /* Register qcs for sending before other streams. */ |
Amaury Denoyelle | f9b0326 | 2023-01-09 10:34:25 +0100 | [diff] [blame] | 1445 | qcc_send_stream(qcs, 1); |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 1446 | h3c->flags |= H3_CF_SETTINGS_SENT; |
Amaury Denoyelle | 20f2a42 | 2023-01-03 14:39:24 +0100 | [diff] [blame] | 1447 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1448 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1449 | TRACE_LEAVE(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1450 | return ret; |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 1451 | |
| 1452 | err: |
| 1453 | TRACE_DEVEL("leaving on error", H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
| 1454 | return -1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1457 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 1458 | { |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1459 | struct h3s *h3s = qcs->ctx; |
| 1460 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1461 | struct buffer outbuf; |
| 1462 | struct buffer headers_buf = BUF_NULL; |
| 1463 | struct buffer *res; |
| 1464 | struct http_hdr list[global.tune.max_http_hdr]; |
| 1465 | struct htx_sl *sl; |
| 1466 | struct htx_blk *blk; |
| 1467 | enum htx_blk_type type; |
| 1468 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 1469 | int ret = 0; |
| 1470 | int hdr; |
| 1471 | int status = 0; |
| 1472 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1473 | TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1474 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1475 | sl = NULL; |
| 1476 | hdr = 0; |
| 1477 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 1478 | type = htx_get_blk_type(blk); |
| 1479 | |
| 1480 | if (type == HTX_BLK_UNUSED) |
| 1481 | continue; |
| 1482 | |
| 1483 | if (type == HTX_BLK_EOH) |
| 1484 | break; |
| 1485 | |
| 1486 | if (type == HTX_BLK_RES_SL) { |
| 1487 | /* start-line -> HEADERS h3 frame */ |
| 1488 | BUG_ON(sl); |
| 1489 | sl = htx_get_blk_ptr(htx, blk); |
| 1490 | /* TODO should be on h3 layer */ |
| 1491 | status = sl->info.res.status; |
| 1492 | } |
| 1493 | else if (type == HTX_BLK_HDR) { |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1494 | if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1)) { |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 1495 | TRACE_ERROR("too many headers", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1496 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | fa7fadc | 2022-06-15 15:52:27 +0200 | [diff] [blame] | 1497 | goto err; |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1498 | } |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1499 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 1500 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 1501 | hdr++; |
| 1502 | } |
| 1503 | else { |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1504 | /* Unhandled HTX block type. */ |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1505 | ABORT_NOW(); |
| 1506 | goto err; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | BUG_ON(!sl); |
| 1511 | |
| 1512 | list[hdr].n = ist(""); |
| 1513 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 1514 | res = mux_get_buf(qcs); |
Amaury Denoyelle | d70f133 | 2024-01-29 14:39:19 +0100 | [diff] [blame] | 1515 | if (b_is_null(res)) { |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1516 | TRACE_ERROR("cannot allocate Tx buffer", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1517 | h3c->err = H3_INTERNAL_ERROR; |
| 1518 | goto err; |
| 1519 | } |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1520 | |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1521 | /* Buffer allocated just now : must be enough for frame type + length as a max varint size */ |
| 1522 | BUG_ON(b_room(res) < 5); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1523 | |
| 1524 | b_reset(&outbuf); |
| 1525 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 1526 | /* Start the headers after frame type + length */ |
| 1527 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 1528 | |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1529 | if (qpack_encode_field_section_line(&headers_buf)) { |
| 1530 | h3c->err = H3_INTERNAL_ERROR; |
| 1531 | goto err; |
| 1532 | } |
Amaury Denoyelle | ed96865 | 2024-01-29 13:47:44 +0100 | [diff] [blame] | 1533 | if (qpack_encode_int_status(&headers_buf, status)) { |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1534 | /* TODO handle invalid status code VS no buf space left */ |
| 1535 | TRACE_ERROR("error during status code encoding", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | ed96865 | 2024-01-29 13:47:44 +0100 | [diff] [blame] | 1536 | h3c->err = H3_INTERNAL_ERROR; |
| 1537 | goto err; |
| 1538 | } |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1539 | |
| 1540 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 1541 | if (isteq(list[hdr].n, ist(""))) |
| 1542 | break; |
| 1543 | |
Amaury Denoyelle | 8ad2669 | 2023-01-17 17:47:06 +0100 | [diff] [blame] | 1544 | /* RFC 9114 4.2. HTTP Fields |
| 1545 | * |
| 1546 | * An intermediary transforming an HTTP/1.x message to HTTP/3 |
| 1547 | * MUST remove connection-specific header fields as discussed in |
| 1548 | * Section 7.6.1 of [HTTP], or their messages will be treated by |
| 1549 | * other HTTP/3 endpoints as malformed. |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 1550 | */ |
Amaury Denoyelle | 8ad2669 | 2023-01-17 17:47:06 +0100 | [diff] [blame] | 1551 | if (isteq(list[hdr].n, ist("connection")) || |
| 1552 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 1553 | isteq(list[hdr].n, ist("keep-alive")) || |
| 1554 | isteq(list[hdr].n, ist("transfer-encoding"))) { |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 1555 | continue; |
Amaury Denoyelle | 8ad2669 | 2023-01-17 17:47:06 +0100 | [diff] [blame] | 1556 | } |
| 1557 | else if (isteq(list[hdr].n, ist("te"))) { |
| 1558 | /* "te" may only be sent with "trailers" if this value |
| 1559 | * is present, otherwise it must be deleted. |
| 1560 | */ |
| 1561 | const struct ist v = istist(list[hdr].v, ist("trailers")); |
| 1562 | if (!isttest(v) || (v.len > 8 && v.ptr[8] != ',')) |
| 1563 | continue; |
| 1564 | list[hdr].v = ist("trailers"); |
| 1565 | } |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 1566 | |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1567 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) { |
| 1568 | h3c->err = H3_INTERNAL_ERROR; |
| 1569 | goto err; |
| 1570 | } |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* Now that all headers are encoded, we are certain that res buffer is |
| 1574 | * big enough |
| 1575 | */ |
| 1576 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 1577 | res->head += 4 - frame_length_size; |
| 1578 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1579 | b_quic_enc_int(res, b_data(&headers_buf), 0); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1580 | b_add(res, b_data(&headers_buf)); |
| 1581 | |
| 1582 | ret = 0; |
| 1583 | blk = htx_get_head_blk(htx); |
| 1584 | while (blk) { |
| 1585 | type = htx_get_blk_type(blk); |
| 1586 | ret += htx_get_blksz(blk); |
| 1587 | blk = htx_remove_blk(htx, blk); |
| 1588 | if (type == HTX_BLK_EOH) |
| 1589 | break; |
| 1590 | } |
| 1591 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1592 | TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1593 | return ret; |
| 1594 | |
| 1595 | err: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1596 | TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1597 | return -1; |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1600 | /* Convert a series of HTX trailer blocks from <htx> buffer into <qcs> buffer |
| 1601 | * as a H3 HEADERS frame. H3 forbidden trailers are skipped. HTX trailer blocks |
| 1602 | * are removed from <htx> until EOT is found and itself removed. |
| 1603 | * |
| 1604 | * If only a EOT HTX block is present without trailer, no H3 frame is produced. |
| 1605 | * Caller is responsible to emit an empty QUIC STREAM frame to signal the end |
| 1606 | * of the stream. |
| 1607 | * |
| 1608 | * Returns the size of HTX blocks removed. |
| 1609 | */ |
| 1610 | static int h3_resp_trailers_send(struct qcs *qcs, struct htx *htx) |
| 1611 | { |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1612 | struct h3s *h3s = qcs->ctx; |
| 1613 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1614 | struct buffer headers_buf = BUF_NULL; |
| 1615 | struct buffer *res; |
| 1616 | struct http_hdr list[global.tune.max_http_hdr]; |
| 1617 | struct htx_blk *blk; |
| 1618 | enum htx_blk_type type; |
| 1619 | char *tail; |
| 1620 | int ret = 0; |
| 1621 | int hdr; |
| 1622 | |
| 1623 | TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1624 | |
| 1625 | hdr = 0; |
| 1626 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 1627 | type = htx_get_blk_type(blk); |
| 1628 | |
| 1629 | if (type == HTX_BLK_UNUSED) |
| 1630 | continue; |
| 1631 | |
| 1632 | if (type == HTX_BLK_EOT) |
| 1633 | break; |
| 1634 | |
| 1635 | if (type == HTX_BLK_TLR) { |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1636 | if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1)) { |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 1637 | TRACE_ERROR("too many headers", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1638 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1639 | goto err; |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1640 | } |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1641 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 1642 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 1643 | hdr++; |
| 1644 | } |
| 1645 | else { |
| 1646 | TRACE_ERROR("unexpected HTX block", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | a755439 | 2023-12-21 17:42:43 +0100 | [diff] [blame] | 1647 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1648 | goto err; |
| 1649 | } |
| 1650 | } |
| 1651 | |
Amaury Denoyelle | 4be5435 | 2023-01-26 17:49:21 +0100 | [diff] [blame] | 1652 | if (!hdr) { |
| 1653 | /* No headers encoded here so no need to generate a H3 HEADERS |
| 1654 | * frame. Mux will send an empty QUIC STREAM frame with FIN. |
| 1655 | */ |
| 1656 | TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1657 | goto end; |
| 1658 | } |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1659 | list[hdr].n = ist(""); |
| 1660 | |
| 1661 | res = mux_get_buf(qcs); |
Amaury Denoyelle | d70f133 | 2024-01-29 14:39:19 +0100 | [diff] [blame] | 1662 | if (b_is_null(res)) { |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1663 | TRACE_ERROR("cannot allocate Tx buffer", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1664 | h3c->err = H3_INTERNAL_ERROR; |
| 1665 | goto err; |
| 1666 | } |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1667 | |
| 1668 | /* At least 9 bytes to store frame type + length as a varint max size */ |
| 1669 | if (b_room(res) < 9) { |
| 1670 | qcs->flags |= QC_SF_BLK_MROOM; |
| 1671 | goto err; |
| 1672 | } |
| 1673 | |
| 1674 | /* Force buffer realignment as size required to encode headers is unknown. */ |
| 1675 | if (b_space_wraps(res)) |
| 1676 | b_slow_realign(res, trash.area, b_data(res)); |
| 1677 | /* Start the headers after frame type + length */ |
| 1678 | headers_buf = b_make(b_peek(res, b_data(res) + 9), b_contig_space(res) - 9, 0, 0); |
| 1679 | |
Amaury Denoyelle | 224ba5c | 2023-01-26 17:41:58 +0100 | [diff] [blame] | 1680 | if (qpack_encode_field_section_line(&headers_buf)) { |
| 1681 | qcs->flags |= QC_SF_BLK_MROOM; |
| 1682 | goto err; |
| 1683 | } |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1684 | |
| 1685 | tail = b_tail(&headers_buf); |
| 1686 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 1687 | if (isteq(list[hdr].n, ist(""))) |
| 1688 | break; |
| 1689 | |
| 1690 | /* forbidden HTTP/3 headers, cf h3_resp_headers_send() */ |
| 1691 | if (isteq(list[hdr].n, ist("host")) || |
| 1692 | isteq(list[hdr].n, ist("content-length")) || |
| 1693 | isteq(list[hdr].n, ist("connection")) || |
| 1694 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 1695 | isteq(list[hdr].n, ist("keep-alive")) || |
| 1696 | isteq(list[hdr].n, ist("te")) || |
| 1697 | isteq(list[hdr].n, ist("transfer-encoding"))) { |
| 1698 | continue; |
| 1699 | } |
| 1700 | |
Amaury Denoyelle | 224ba5c | 2023-01-26 17:41:58 +0100 | [diff] [blame] | 1701 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) { |
| 1702 | qcs->flags |= QC_SF_BLK_MROOM; |
| 1703 | goto err; |
| 1704 | } |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1707 | /* Check that at least one header was encoded in buffer. */ |
Amaury Denoyelle | 4be5435 | 2023-01-26 17:49:21 +0100 | [diff] [blame] | 1708 | if (b_tail(&headers_buf) == tail) { |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1709 | /* No headers encoded here so no need to generate a H3 HEADERS |
| 1710 | * frame. Mux will send an empty QUIC STREAM frame with FIN. |
| 1711 | */ |
| 1712 | TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 4be5435 | 2023-01-26 17:49:21 +0100 | [diff] [blame] | 1713 | goto end; |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Amaury Denoyelle | 4be5435 | 2023-01-26 17:49:21 +0100 | [diff] [blame] | 1716 | /* Now that all headers are encoded, we are certain that res buffer is |
| 1717 | * big enough. |
| 1718 | */ |
| 1719 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
Amaury Denoyelle | ffb6f1a | 2024-08-01 18:20:08 +0200 | [diff] [blame] | 1720 | b_quic_enc_int(res, b_data(&headers_buf), 8); |
Amaury Denoyelle | 4be5435 | 2023-01-26 17:49:21 +0100 | [diff] [blame] | 1721 | b_add(res, b_data(&headers_buf)); |
| 1722 | |
| 1723 | end: |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1724 | ret = 0; |
| 1725 | blk = htx_get_head_blk(htx); |
| 1726 | while (blk) { |
| 1727 | type = htx_get_blk_type(blk); |
| 1728 | ret += htx_get_blksz(blk); |
| 1729 | blk = htx_remove_blk(htx, blk); |
| 1730 | if (type == HTX_BLK_EOT) |
| 1731 | break; |
| 1732 | } |
| 1733 | |
| 1734 | TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 1735 | return ret; |
| 1736 | |
| 1737 | err: |
| 1738 | TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1739 | return -1; |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1740 | } |
| 1741 | |
Amaury Denoyelle | 3d5b533 | 2024-01-04 11:33:33 +0100 | [diff] [blame] | 1742 | /* Returns the total of bytes sent. This corresponds to the |
| 1743 | * total bytes of HTX block removed. A negative error code is returned in case |
| 1744 | * of a fatal error which should caused a connection closure. |
| 1745 | */ |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 1746 | static int h3_resp_data_send(struct qcs *qcs, struct htx *htx, size_t count) |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1747 | { |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1748 | struct h3s *h3s = qcs->ctx; |
| 1749 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1750 | struct buffer outbuf; |
| 1751 | struct buffer *res; |
| 1752 | size_t total = 0; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1753 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1754 | struct htx_blk *blk; |
| 1755 | enum htx_blk_type type; |
| 1756 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1757 | TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
| 1758 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1759 | new_frame: |
| 1760 | if (!count || htx_is_empty(htx)) |
| 1761 | goto end; |
| 1762 | |
| 1763 | blk = htx_get_head_blk(htx); |
| 1764 | type = htx_get_blk_type(blk); |
| 1765 | fsize = bsize = htx_get_blksz(blk); |
| 1766 | |
| 1767 | if (type != HTX_BLK_DATA) |
| 1768 | goto end; |
| 1769 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 1770 | res = mux_get_buf(qcs); |
Amaury Denoyelle | d70f133 | 2024-01-29 14:39:19 +0100 | [diff] [blame] | 1771 | if (b_is_null(res)) { |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1772 | TRACE_ERROR("cannot allocate Tx buffer", H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
| 1773 | h3c->err = H3_INTERNAL_ERROR; |
| 1774 | goto err; |
| 1775 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1776 | |
| 1777 | if (fsize > count) |
| 1778 | fsize = count; |
| 1779 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1780 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 1781 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1782 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1783 | while (1) { |
| 1784 | b_reset(&outbuf); |
| 1785 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 1786 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 1787 | break; |
| 1788 | b_slow_realign(res, trash.area, b_data(res)); |
| 1789 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1790 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 1791 | /* Not enough room for headers and at least one data byte, block the |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1792 | * stream. It is expected that the stream connector layer will subscribe |
| 1793 | * on SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1794 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 1795 | if (b_size(&outbuf) <= hsize) { |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1796 | TRACE_STATE("not enough room for data frame", H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 1797 | qcs->flags |= QC_SF_BLK_MROOM; |
Amaury Denoyelle | 3d5b533 | 2024-01-04 11:33:33 +0100 | [diff] [blame] | 1798 | goto end; |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 1799 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1800 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1801 | if (b_size(&outbuf) < hsize + fsize) |
| 1802 | fsize = b_size(&outbuf) - hsize; |
| 1803 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1804 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1805 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
Amaury Denoyelle | 7d78eff | 2023-01-17 15:21:16 +0100 | [diff] [blame] | 1806 | b_quic_enc_int(&outbuf, fsize, 0); /* h3 frame length */ |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1807 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1808 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1809 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1810 | count -= fsize; |
| 1811 | |
| 1812 | if (fsize == bsize) |
| 1813 | htx_remove_blk(htx, blk); |
| 1814 | else |
| 1815 | htx_cut_data_blk(htx, blk, fsize); |
| 1816 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1817 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1818 | b_add(res, b_data(&outbuf)); |
| 1819 | goto new_frame; |
| 1820 | |
| 1821 | end: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1822 | TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1823 | return total; |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1824 | |
| 1825 | err: |
Amaury Denoyelle | 3d5b533 | 2024-01-04 11:33:33 +0100 | [diff] [blame] | 1826 | BUG_ON(total); /* Must return HTX removed size if at least on frame encoded. */ |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1827 | TRACE_DEVEL("leaving on error", H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
| 1828 | return -1; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1829 | } |
| 1830 | |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 1831 | static size_t h3_snd_buf(struct qcs *qcs, struct htx *htx, size_t count) |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1832 | { |
Amaury Denoyelle | df0ea3f | 2023-12-22 11:45:54 +0100 | [diff] [blame] | 1833 | struct h3s *h3s = qcs->ctx; |
| 1834 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1835 | size_t total = 0; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1836 | enum htx_blk_type btype; |
| 1837 | struct htx_blk *blk; |
| 1838 | uint32_t bsize; |
| 1839 | int32_t idx; |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1840 | int ret = 0; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1841 | |
Amaury Denoyelle | 44083ac | 2024-01-29 15:15:27 +0100 | [diff] [blame] | 1842 | TRACE_ENTER(H3_EV_STRM_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1843 | |
Amaury Denoyelle | df0ea3f | 2023-12-22 11:45:54 +0100 | [diff] [blame] | 1844 | while (count && !htx_is_empty(htx) && |
| 1845 | !(qcs->flags & QC_SF_BLK_MROOM) && !h3c->err) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1846 | idx = htx_get_head(htx); |
| 1847 | blk = htx_get_blk(htx, idx); |
| 1848 | btype = htx_get_blk_type(blk); |
| 1849 | bsize = htx_get_blksz(blk); |
| 1850 | |
| 1851 | /* Not implemented : QUIC on backend side */ |
| 1852 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 1853 | |
| 1854 | switch (btype) { |
| 1855 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1856 | /* start-line -> HEADERS h3 frame */ |
| 1857 | ret = h3_resp_headers_send(qcs, htx); |
| 1858 | if (ret > 0) { |
| 1859 | total += ret; |
| 1860 | count -= ret; |
| 1861 | if (ret < bsize) |
| 1862 | goto out; |
| 1863 | } |
| 1864 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1865 | |
| 1866 | case HTX_BLK_DATA: |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 1867 | ret = h3_resp_data_send(qcs, htx, count); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1868 | if (ret > 0) { |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1869 | total += ret; |
| 1870 | count -= ret; |
| 1871 | if (ret < bsize) |
| 1872 | goto out; |
| 1873 | } |
| 1874 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1875 | |
| 1876 | case HTX_BLK_TLR: |
| 1877 | case HTX_BLK_EOT: |
Amaury Denoyelle | 4e52010 | 2023-01-12 14:53:43 +0100 | [diff] [blame] | 1878 | ret = h3_resp_trailers_send(qcs, htx); |
| 1879 | if (ret > 0) { |
| 1880 | total += ret; |
| 1881 | count -= ret; |
| 1882 | if (ret < bsize) |
| 1883 | goto out; |
| 1884 | } |
| 1885 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1886 | |
| 1887 | default: |
| 1888 | htx_remove_blk(htx, blk); |
| 1889 | total += bsize; |
| 1890 | count -= bsize; |
| 1891 | break; |
| 1892 | } |
Amaury Denoyelle | 40ca072 | 2023-12-22 09:00:13 +0100 | [diff] [blame] | 1893 | |
| 1894 | /* If an error occured, either buffer space or connection error |
| 1895 | * must be set to break current loop. |
| 1896 | */ |
| 1897 | BUG_ON(ret < 0 && !(qcs->flags & QC_SF_BLK_MROOM) && !h3c->err); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Amaury Denoyelle | df0ea3f | 2023-12-22 11:45:54 +0100 | [diff] [blame] | 1900 | /* Interrupt sending on connection error. */ |
| 1901 | if (unlikely(h3c->err)) { |
| 1902 | qcc_set_error(qcs->qcc, h3c->err, 1); |
| 1903 | goto out; |
| 1904 | } |
| 1905 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1906 | out: |
Amaury Denoyelle | 44083ac | 2024-01-29 15:15:27 +0100 | [diff] [blame] | 1907 | TRACE_LEAVE(H3_EV_STRM_SEND, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1908 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Amaury Denoyelle | 1e340ba | 2023-01-30 12:12:11 +0100 | [diff] [blame] | 1911 | /* Notify about a closure on <qcs> stream requested by the remote peer. |
| 1912 | * |
| 1913 | * Stream channel <side> is explained relative to our endpoint : WR for |
| 1914 | * STOP_SENDING or RD for RESET_STREAM reception. Callback decode_qcs() is used |
| 1915 | * instead for closure performed using a STREAM frame with FIN bit. |
| 1916 | * |
| 1917 | * The main objective of this function is to check if closure is valid |
| 1918 | * according to HTTP/3 specification. |
| 1919 | * |
| 1920 | * Returns 0 on success else non-zero. A CONNECTION_CLOSE is generated on |
| 1921 | * error. |
| 1922 | */ |
| 1923 | static int h3_close(struct qcs *qcs, enum qcc_app_ops_close_side side) |
| 1924 | { |
Amaury Denoyelle | 87f8766 | 2023-01-30 12:12:43 +0100 | [diff] [blame] | 1925 | struct h3s *h3s = qcs->ctx; |
| 1926 | struct h3c *h3c = h3s->h3c;; |
| 1927 | |
| 1928 | /* RFC 9114 6.2.1. Control Streams |
| 1929 | * |
| 1930 | * The sender |
| 1931 | * MUST NOT close the control stream, and the receiver MUST NOT |
| 1932 | * request that the sender close the control stream. If either |
| 1933 | * control stream is closed at any point, this MUST be treated |
| 1934 | * as a connection error of type H3_CLOSED_CRITICAL_STREAM. |
| 1935 | */ |
Amaury Denoyelle | e269aeb | 2023-01-30 12:13:22 +0100 | [diff] [blame] | 1936 | if (qcs == h3c->ctrl_strm || h3s->type == H3S_T_CTRL) { |
Amaury Denoyelle | e31867b | 2023-01-31 16:01:22 +0100 | [diff] [blame] | 1937 | TRACE_ERROR("closure detected on control stream", H3_EV_H3S_END, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 1938 | qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1); |
Amaury Denoyelle | 87f8766 | 2023-01-30 12:12:43 +0100 | [diff] [blame] | 1939 | return 1; |
| 1940 | } |
| 1941 | |
Amaury Denoyelle | 1e340ba | 2023-01-30 12:12:11 +0100 | [diff] [blame] | 1942 | return 0; |
| 1943 | } |
| 1944 | |
Amaury Denoyelle | 583895a | 2024-06-21 14:45:04 +0200 | [diff] [blame] | 1945 | /* Allocates HTTP/3 stream context relative to <qcs>. If the operation cannot |
| 1946 | * be performed, an error is returned and <qcs> context is unchanged. |
| 1947 | * |
| 1948 | * Returns 0 on success else non-zero. |
| 1949 | */ |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 1950 | static int h3_attach(struct qcs *qcs, void *conn_ctx) |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1951 | { |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 1952 | struct h3c *h3c = conn_ctx; |
| 1953 | struct h3s *h3s = NULL; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1954 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1955 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 1956 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1957 | h3s = pool_alloc(pool_head_h3s); |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 1958 | if (!h3s) { |
| 1959 | TRACE_ERROR("h3s allocation failure", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 1960 | goto err; |
| 1961 | } |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1962 | |
| 1963 | qcs->ctx = h3s; |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 1964 | h3s->h3c = conn_ctx; |
| 1965 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1966 | h3s->demux_frame_len = 0; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 1967 | h3s->demux_frame_type = H3_FT_UNINIT; |
Amaury Denoyelle | d2c5ee6 | 2022-12-08 16:54:42 +0100 | [diff] [blame] | 1968 | h3s->body_len = 0; |
| 1969 | h3s->data_len = 0; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 1970 | h3s->flags = 0; |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 1971 | h3s->err = 0; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1972 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 1973 | if (quic_stream_is_bidi(qcs->id)) { |
| 1974 | h3s->type = H3S_T_REQ; |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 1975 | h3s->st_req = H3S_ST_REQ_BEFORE; |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1976 | qcs_wait_http_req(qcs); |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 1977 | } |
| 1978 | else { |
| 1979 | /* stream type must be decoded for unidirectional streams */ |
| 1980 | h3s->type = H3S_T_UNKNOWN; |
| 1981 | } |
| 1982 | |
Amaury Denoyelle | 583895a | 2024-06-21 14:45:04 +0200 | [diff] [blame] | 1983 | /* RFC 9114 5.2. Connection Shutdown |
| 1984 | * |
| 1985 | * Upon sending |
| 1986 | * a GOAWAY frame, the endpoint SHOULD explicitly cancel (see |
| 1987 | * Sections 4.1.1 and 7.2.3) any requests or pushes that have |
| 1988 | * identifiers greater than or equal to the one indicated, in |
| 1989 | * order to clean up transport state for the affected streams. |
| 1990 | * The endpoint SHOULD continue to do so as more requests or |
| 1991 | * pushes arrive. |
| 1992 | */ |
| 1993 | if (h3c->flags & H3_CF_GOAWAY_SENT && qcs->id >= h3c->id_goaway && |
| 1994 | quic_stream_is_bidi(qcs->id)) { |
| 1995 | TRACE_STATE("close stream outside of goaway range", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 1996 | qcc_abort_stream_read(qcs); |
| 1997 | qcc_reset_stream(qcs, H3_REQUEST_REJECTED); |
| 1998 | } |
| 1999 | |
| 2000 | /* TODO support push uni-stream rejection. */ |
| 2001 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 2002 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2003 | return 0; |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 2004 | |
| 2005 | err: |
| 2006 | TRACE_DEVEL("leaving in error", H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 2007 | return 1; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2008 | } |
| 2009 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2010 | static void h3_detach(struct qcs *qcs) |
| 2011 | { |
| 2012 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 2013 | |
| 2014 | TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
| 2015 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2016 | pool_free(pool_head_h3s, h3s); |
| 2017 | qcs->ctx = NULL; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 2018 | |
| 2019 | TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Amaury Denoyelle | 71fd036 | 2023-01-24 17:35:37 +0100 | [diff] [blame] | 2022 | /* Initialize H3 control stream and prepare SETTINGS emission. |
| 2023 | * |
| 2024 | * Returns 0 on success else non-zero. |
| 2025 | */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2026 | static int h3_finalize(void *ctx) |
| 2027 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2028 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 2029 | struct qcs *qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2030 | |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 2031 | qcs = qcc_init_stream_local(h3c->qcc, 0); |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 2032 | if (!qcs) |
Amaury Denoyelle | 71fd036 | 2023-01-24 17:35:37 +0100 | [diff] [blame] | 2033 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2034 | |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 2035 | h3c->ctrl_strm = qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2036 | |
Amaury Denoyelle | ddc1ef1 | 2023-12-15 17:32:06 +0100 | [diff] [blame] | 2037 | if (h3_control_send(qcs, h3c) < 0) |
| 2038 | return 1; |
| 2039 | |
Amaury Denoyelle | 71fd036 | 2023-01-24 17:35:37 +0100 | [diff] [blame] | 2040 | return 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2043 | /* Generate a GOAWAY frame for <h3c> connection on the control stream. |
| 2044 | * |
| 2045 | * Returns 0 on success else non-zero. |
| 2046 | */ |
| 2047 | static int h3_send_goaway(struct h3c *h3c) |
| 2048 | { |
| 2049 | struct qcs *qcs = h3c->ctrl_strm; |
| 2050 | struct buffer pos, *res; |
| 2051 | unsigned char data[3 * QUIC_VARINT_MAX_SIZE]; |
| 2052 | size_t frm_len = quic_int_getsize(h3c->id_goaway); |
| 2053 | |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2054 | TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2055 | |
| 2056 | if (!qcs) { |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2057 | TRACE_ERROR("control stream not initialized", H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2058 | goto err; |
| 2059 | } |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2060 | |
| 2061 | pos = b_make((char *)data, sizeof(data), 0, 0); |
| 2062 | |
Amaury Denoyelle | 7d78eff | 2023-01-17 15:21:16 +0100 | [diff] [blame] | 2063 | b_quic_enc_int(&pos, H3_FT_GOAWAY, 0); |
| 2064 | b_quic_enc_int(&pos, frm_len, 0); |
| 2065 | b_quic_enc_int(&pos, h3c->id_goaway, 0); |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2066 | |
| 2067 | res = mux_get_buf(qcs); |
Amaury Denoyelle | d70f133 | 2024-01-29 14:39:19 +0100 | [diff] [blame] | 2068 | if (b_is_null(res) || b_room(res) < b_data(&pos)) { |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2069 | /* Do not try forcefully to emit GOAWAY if no space left. */ |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2070 | TRACE_ERROR("cannot send GOAWAY", H3_EV_H3C_END, h3c->qcc->conn, qcs); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2071 | goto err; |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | b_force_xfer(res, &pos, b_data(&pos)); |
Amaury Denoyelle | 19adeb5 | 2023-01-25 10:50:03 +0100 | [diff] [blame] | 2075 | qcc_send_stream(qcs, 1); |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2076 | |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 2077 | h3c->flags |= H3_CF_GOAWAY_SENT; |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2078 | TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2079 | return 0; |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2080 | |
| 2081 | err: |
Amaury Denoyelle | 3d55084 | 2023-01-24 17:42:21 +0100 | [diff] [blame] | 2082 | /* Consider GOAWAY as sent even if not really the case. This will |
| 2083 | * block future stream opening using H3_REQUEST_REJECTED reset. |
| 2084 | */ |
| 2085 | h3c->flags |= H3_CF_GOAWAY_SENT; |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2086 | TRACE_DEVEL("leaving in error", H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2087 | return 1; |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2090 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 2091 | * Return 1 if succeeded, 0 if not. |
| 2092 | */ |
| 2093 | static int h3_init(struct qcc *qcc) |
| 2094 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2095 | struct h3c *h3c; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2096 | struct quic_conn *qc = qcc->conn->handle.qc; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2097 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2098 | h3c = pool_alloc(pool_head_h3c); |
| 2099 | if (!h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2100 | goto fail_no_h3; |
| 2101 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2102 | h3c->qcc = qcc; |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 2103 | h3c->ctrl_strm = NULL; |
Amaury Denoyelle | 2fe93ab | 2022-12-09 15:01:31 +0100 | [diff] [blame] | 2104 | h3c->err = 0; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2105 | h3c->flags = 0; |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2106 | h3c->id_goaway = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2107 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2108 | qcc->ctx = h3c; |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 2109 | /* TODO cleanup only ref to quic_conn */ |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2110 | h3c->prx_counters = |
| 2111 | EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe, |
| 2112 | &h3_stats_module); |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2113 | LIST_INIT(&h3c->buf_wait.list); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2114 | |
| 2115 | return 1; |
| 2116 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2117 | fail_no_h3: |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 2121 | /* Send a HTTP/3 GOAWAY followed by a CONNECTION_CLOSE_APP. */ |
| 2122 | static void h3_shutdown(void *ctx) |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 2123 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2124 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 2125 | |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2126 | TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2127 | |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 2128 | /* RFC 9114 5.2. Connection Shutdown |
| 2129 | * |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 2130 | * Even when a connection is not idle, either endpoint can decide to |
| 2131 | * stop using the connection and initiate a graceful connection close. |
| 2132 | * Endpoints initiate the graceful shutdown of an HTTP/3 connection by |
| 2133 | * sending a GOAWAY frame. |
| 2134 | */ |
| 2135 | h3_send_goaway(h3c); |
| 2136 | |
| 2137 | /* RFC 9114 5.2. Connection Shutdown |
| 2138 | * |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 2139 | * An endpoint that completes a |
| 2140 | * graceful shutdown SHOULD use the H3_NO_ERROR error code when closing |
| 2141 | * the connection. |
| 2142 | */ |
Amaury Denoyelle | 51f116d | 2023-05-04 15:49:02 +0200 | [diff] [blame] | 2143 | h3c->qcc->err = quic_err_app(H3_NO_ERROR); |
Amaury Denoyelle | 56a86dd | 2023-01-30 15:36:51 +0100 | [diff] [blame] | 2144 | |
Amaury Denoyelle | 78adb4b | 2023-01-31 15:50:16 +0100 | [diff] [blame] | 2145 | TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn); |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 2146 | } |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 2147 | |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 2148 | static void h3_release(void *ctx) |
| 2149 | { |
| 2150 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 2151 | pool_free(pool_head_h3c, h3c); |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2154 | /* Increment the h3 error code counters for <error_code> value */ |
| 2155 | static void h3_stats_inc_err_cnt(void *ctx, int err_code) |
| 2156 | { |
| 2157 | struct h3c *h3c = ctx; |
| 2158 | |
| 2159 | h3_inc_err_cnt(h3c->prx_counters, err_code); |
| 2160 | } |
| 2161 | |
Amaury Denoyelle | 35d9053 | 2023-01-26 16:03:45 +0100 | [diff] [blame] | 2162 | static inline const char *h3_ft_str(uint64_t type) |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 2163 | { |
| 2164 | switch (type) { |
| 2165 | case H3_FT_DATA: return "DATA"; |
| 2166 | case H3_FT_HEADERS: return "HEADERS"; |
| 2167 | case H3_FT_SETTINGS: return "SETTINGS"; |
| 2168 | case H3_FT_PUSH_PROMISE: return "PUSH_PROMISE"; |
| 2169 | case H3_FT_MAX_PUSH_ID: return "MAX_PUSH_ID"; |
| 2170 | case H3_FT_CANCEL_PUSH: return "CANCEL_PUSH"; |
| 2171 | case H3_FT_GOAWAY: return "GOAWAY"; |
| 2172 | default: return "_UNKNOWN_"; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 2176 | /* h3 trace handler */ |
| 2177 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 2178 | const struct trace_source *src, |
| 2179 | const struct ist where, const struct ist func, |
| 2180 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 2181 | { |
| 2182 | const struct connection *conn = a1; |
| 2183 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 2184 | const struct qcs *qcs = a2; |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 2185 | const struct h3s *h3s = qcs ? qcs->ctx : NULL; |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 2186 | |
Frédéric Lécaille | 1c725aa | 2022-09-08 15:49:37 +0200 | [diff] [blame] | 2187 | if (!qcc) |
| 2188 | return; |
| 2189 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 2190 | if (src->verbosity > H3_VERB_CLEAN) { |
| 2191 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
Frédéric Lécaille | 2eb5faa | 2022-09-08 16:03:13 +0200 | [diff] [blame] | 2192 | if (qcc->conn->handle.qc) |
| 2193 | chunk_appendf(&trace_buf, " qc=%p", qcc->conn->handle.qc); |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 2194 | |
| 2195 | if (qcs) |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 2196 | chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 2197 | |
| 2198 | if (h3s && h3s->demux_frame_type != H3_FT_UNINIT) { |
Amaury Denoyelle | 35d9053 | 2023-01-26 16:03:45 +0100 | [diff] [blame] | 2199 | chunk_appendf(&trace_buf, " h3s.dem=%s/%llu", |
| 2200 | h3_ft_str(h3s->demux_frame_type), (ull)h3s->demux_frame_len); |
Amaury Denoyelle | 14037bf | 2023-02-17 15:56:06 +0100 | [diff] [blame] | 2201 | } |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2205 | /* HTTP/3 application layer operations */ |
| 2206 | const struct qcc_app_ops h3_ops = { |
| 2207 | .init = h3_init, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2208 | .attach = h3_attach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2209 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 2210 | .snd_buf = h3_snd_buf, |
Amaury Denoyelle | 1e340ba | 2023-01-30 12:12:11 +0100 | [diff] [blame] | 2211 | .close = h3_close, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 2212 | .detach = h3_detach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2213 | .finalize = h3_finalize, |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 2214 | .shutdown = h3_shutdown, |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 2215 | .inc_err_cnt = h3_stats_inc_err_cnt, |
Amaury Denoyelle | f8aaf8b | 2022-09-14 16:23:47 +0200 | [diff] [blame] | 2216 | .release = h3_release, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 2217 | }; |