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