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