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 | |
| 19 | #include <haproxy/buf.h> |
Amaury Denoyelle | 9904355 | 2021-08-24 15:36:02 +0200 | [diff] [blame] | 20 | #include <haproxy/connection.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 21 | #include <haproxy/dynbuf.h> |
| 22 | #include <haproxy/h3.h> |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 23 | #include <haproxy/h3_stats.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame^] | 25 | #include <haproxy/http_htx.h> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 26 | #include <haproxy/htx.h> |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 27 | #include <haproxy/intops.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 28 | #include <haproxy/istbuf.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 29 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 30 | #include <haproxy/pool.h> |
| 31 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 32 | #include <haproxy/qpack-enc.h> |
| 33 | #include <haproxy/quic_enc.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 34 | #include <haproxy/stconn.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 36 | #include <haproxy/trace.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 37 | #include <haproxy/xprt_quic.h> |
| 38 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 39 | /* trace source and events */ |
| 40 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 41 | const struct trace_source *src, |
| 42 | const struct ist where, const struct ist func, |
| 43 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 44 | |
| 45 | static const struct trace_event h3_trace_events[] = { |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 46 | #define H3_EV_RX_FRAME (1ULL << 0) |
| 47 | { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" }, |
| 48 | #define H3_EV_RX_DATA (1ULL << 1) |
| 49 | { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" }, |
| 50 | #define H3_EV_RX_HDR (1ULL << 2) |
| 51 | { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" }, |
| 52 | #define H3_EV_RX_SETTINGS (1ULL << 3) |
| 53 | { .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] | 54 | #define H3_EV_TX_DATA (1ULL << 4) |
| 55 | { .mask = H3_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H3 DATA frame" }, |
| 56 | #define H3_EV_TX_HDR (1ULL << 5) |
| 57 | { .mask = H3_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H3 HEADERS frame" }, |
| 58 | #define H3_EV_TX_SETTINGS (1ULL << 6) |
| 59 | { .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] | 60 | #define H3_EV_H3S_NEW (1ULL << 7) |
| 61 | { .mask = H3_EV_H3S_NEW, .name = "h3s_new", .desc = "new H3 stream" }, |
| 62 | #define H3_EV_H3S_END (1ULL << 8) |
| 63 | { .mask = H3_EV_H3S_END, .name = "h3s_end", .desc = "H3 stream terminated" }, |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 64 | { } |
| 65 | }; |
| 66 | |
| 67 | static const struct name_desc h3_trace_lockon_args[4] = { |
| 68 | /* arg1 */ { /* already used by the connection */ }, |
| 69 | /* arg2 */ { .name="qcs", .desc="QUIC stream" }, |
| 70 | /* arg3 */ { }, |
| 71 | /* arg4 */ { } |
| 72 | }; |
| 73 | |
| 74 | static const struct name_desc h3_trace_decoding[] = { |
| 75 | #define H3_VERB_CLEAN 1 |
| 76 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 77 | #define H3_VERB_MINIMAL 2 |
| 78 | { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" }, |
| 79 | { /* end */ } |
| 80 | }; |
| 81 | |
| 82 | struct trace_source trace_h3 = { |
| 83 | .name = IST("h3"), |
| 84 | .desc = "HTTP/3 transcoder", |
| 85 | .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */ |
| 86 | .default_cb = h3_trace, |
| 87 | .known_events = h3_trace_events, |
| 88 | .lockon_args = h3_trace_lockon_args, |
| 89 | .decoding = h3_trace_decoding, |
| 90 | .report_events = ~0, /* report everything by default */ |
| 91 | }; |
| 92 | |
| 93 | #define TRACE_SOURCE &trace_h3 |
| 94 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 95 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 96 | #if defined(DEBUG_H3) |
| 97 | #define h3_debug_printf fprintf |
| 98 | #define h3_debug_hexdump debug_hexdump |
| 99 | #else |
| 100 | #define h3_debug_printf(...) do { } while (0) |
| 101 | #define h3_debug_hexdump(...) do { } while (0) |
| 102 | #endif |
| 103 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 104 | #define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */ |
| 105 | #define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */ |
| 106 | #define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */ |
| 107 | #define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */ |
| 108 | #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] | 109 | |
| 110 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 111 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 112 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 113 | 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] | 114 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 115 | struct h3c { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 116 | struct qcc *qcc; |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 117 | struct qcs *ctrl_strm; /* Control stream */ |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 118 | enum h3_err err; |
| 119 | uint32_t flags; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 120 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 121 | /* Settings */ |
| 122 | uint64_t qpack_max_table_capacity; |
| 123 | uint64_t qpack_blocked_streams; |
| 124 | uint64_t max_field_section_size; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 125 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 126 | uint64_t id_goaway; /* stream ID used for a GOAWAY frame */ |
| 127 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 128 | 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] | 129 | /* Stats counters */ |
| 130 | struct h3_counters *prx_counters; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 133 | 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] | 134 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 135 | #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] | 136 | #define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */ |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 137 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 138 | struct h3s { |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 139 | struct h3c *h3c; |
| 140 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 141 | enum h3s_t type; |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 142 | enum h3s_st_req st_req; /* only used for request streams */ |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 143 | int demux_frame_len; |
| 144 | int demux_frame_type; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 145 | |
| 146 | int flags; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s)); |
| 150 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 151 | /* Initialize an uni-stream <qcs> by reading its type from <b>. |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 152 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 153 | * Returns the count of consumed bytes or a negative error code. |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 154 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 155 | static ssize_t h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs, |
| 156 | struct buffer *b) |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 157 | { |
| 158 | /* decode unidirectional stream type */ |
| 159 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 160 | uint64_t type; |
| 161 | size_t len = 0, ret; |
| 162 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 163 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 164 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 165 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 166 | h3s->flags & H3_SF_UNI_INIT); |
| 167 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 168 | ret = b_quic_dec_int(&type, b, &len); |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 169 | if (!ret) { |
| 170 | ABORT_NOW(); |
| 171 | } |
| 172 | |
| 173 | switch (type) { |
| 174 | case H3_UNI_S_T_CTRL: |
| 175 | if (h3c->flags & H3_CF_UNI_CTRL_SET) { |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 176 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 177 | return -1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 178 | } |
| 179 | h3c->flags |= H3_CF_UNI_CTRL_SET; |
| 180 | h3s->type = H3S_T_CTRL; |
| 181 | break; |
| 182 | |
| 183 | case H3_UNI_S_T_PUSH: |
| 184 | /* TODO not supported for the moment */ |
| 185 | h3s->type = H3S_T_PUSH; |
| 186 | break; |
| 187 | |
| 188 | case H3_UNI_S_T_QPACK_DEC: |
| 189 | if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) { |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 190 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 191 | return -1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 192 | } |
| 193 | h3c->flags |= H3_CF_UNI_QPACK_DEC_SET; |
| 194 | h3s->type = H3S_T_QPACK_DEC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 195 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case H3_UNI_S_T_QPACK_ENC: |
| 199 | if (h3c->flags & H3_CF_UNI_QPACK_ENC_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_ENC_SET; |
| 204 | h3s->type = H3S_T_QPACK_ENC; |
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 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 209 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 210 | * |
| 211 | * Implementations MUST [...] abort reading on unidirectional |
| 212 | * streams that have unknown or unsupported types. |
| 213 | */ |
| 214 | qcs->flags |= QC_SF_READ_ABORTED; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 215 | return -1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | h3s->flags |= H3_SF_UNI_INIT; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 219 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 220 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 221 | return len; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 224 | /* Parse a buffer <b> for a <qcs> uni-stream which does not contains H3 frames. |
| 225 | * This may be used for QPACK encoder/decoder streams for example. <fin> is set |
| 226 | * if this is the last frame of the stream. |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 227 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 228 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 229 | */ |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 230 | 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] | 231 | { |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 232 | struct h3s *h3s = qcs->ctx; |
| 233 | |
| 234 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 235 | !(h3s->flags & H3_SF_UNI_NO_H3)); |
| 236 | |
| 237 | switch (h3s->type) { |
| 238 | case H3S_T_QPACK_DEC: |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 239 | if (qpack_decode_dec(b, fin, qcs)) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 240 | return -1; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 241 | break; |
| 242 | case H3S_T_QPACK_ENC: |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 243 | if (qpack_decode_enc(b, fin, qcs)) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 244 | return -1; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 245 | break; |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 246 | case H3S_T_UNKNOWN: |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 247 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 248 | /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */ |
| 249 | ABORT_NOW(); |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 252 | /* TODO adjust return code */ |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 256 | /* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in |
| 257 | * <ftype> and length in <flen>. |
| 258 | * |
| 259 | * Returns the size of the H3 frame header. Note that the input buffer is not |
| 260 | * consumed. |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 261 | */ |
| 262 | 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] | 263 | struct buffer *b) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 264 | { |
| 265 | size_t hlen; |
| 266 | |
| 267 | hlen = 0; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 268 | if (!b_quic_dec_int(ftype, b, &hlen) || |
| 269 | !b_quic_dec_int(flen, b, &hlen)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 270 | return 0; |
Amaury Denoyelle | 88d5dd1 | 2022-05-31 11:44:52 +0200 | [diff] [blame] | 271 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 272 | |
| 273 | return hlen; |
| 274 | } |
| 275 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 276 | /* Check if H3 frame of type <ftype> is valid when received on stream <qcs>. |
| 277 | * |
| 278 | * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should |
| 279 | * be reported. |
| 280 | */ |
| 281 | static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype) |
| 282 | { |
| 283 | struct h3s *h3s = qcs->ctx; |
| 284 | const uint64_t id = qcs->id; |
| 285 | |
| 286 | BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN); |
| 287 | |
| 288 | switch (ftype) { |
| 289 | case H3_FT_DATA: |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 290 | return h3s->type != H3S_T_CTRL && (h3s->st_req == H3S_ST_REQ_HEADERS || |
| 291 | h3s->st_req == H3S_ST_REQ_DATA); |
| 292 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 293 | case H3_FT_HEADERS: |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 294 | 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] | 295 | |
| 296 | case H3_FT_CANCEL_PUSH: |
| 297 | case H3_FT_GOAWAY: |
| 298 | case H3_FT_MAX_PUSH_ID: |
| 299 | /* Only allowed for control stream. First frame of control |
| 300 | * stream MUST be SETTINGS. |
| 301 | */ |
| 302 | return h3s->type == H3S_T_CTRL && |
| 303 | (h3c->flags & H3_CF_SETTINGS_RECV); |
| 304 | |
| 305 | case H3_FT_SETTINGS: |
| 306 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 307 | * |
| 308 | * If an endpoint receives a second SETTINGS frame on the control |
| 309 | * stream, the endpoint MUST respond with a connection error of type |
| 310 | * H3_FRAME_UNEXPECTED. |
| 311 | */ |
| 312 | return h3s->type == H3S_T_CTRL && |
| 313 | !(h3c->flags & H3_CF_SETTINGS_RECV); |
| 314 | |
| 315 | case H3_FT_PUSH_PROMISE: |
| 316 | return h3s->type != H3S_T_CTRL && |
| 317 | (id & QCS_ID_SRV_INTIATOR_BIT); |
| 318 | |
| 319 | default: |
| 320 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 321 | * |
| 322 | * Implementations MUST discard frames [...] that have unknown |
| 323 | * or unsupported types. |
| 324 | */ |
| 325 | return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV); |
| 326 | } |
| 327 | } |
| 328 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 329 | /* 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] | 330 | * 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] | 331 | * set if this is the last data to transfer from this stream. |
| 332 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 333 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 334 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 335 | static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf, |
| 336 | uint64_t len, char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 337 | { |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 338 | struct h3s *h3s = qcs->ctx; |
| 339 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 340 | struct buffer htx_buf = BUF_NULL; |
| 341 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 342 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 343 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 344 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 345 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 346 | struct ist meth = IST_NULL, path = IST_NULL; |
| 347 | //struct ist scheme = IST_NULL, authority = IST_NULL; |
| 348 | struct ist authority = IST_NULL; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 349 | int hdr_idx, ret; |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame^] | 350 | int cookie = -1, last_cookie = -1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 351 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 352 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 353 | |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 354 | /* TODO support trailer parsing in this function */ |
| 355 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 356 | /* TODO support buffer wrapping */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 357 | BUG_ON(b_head(buf) + len >= b_wrap(buf)); |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 358 | ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp, |
| 359 | list, sizeof(list) / sizeof(list[0])); |
| 360 | if (ret < 0) { |
| 361 | TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); |
| 362 | h3c->err = -ret; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 363 | return -1; |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 364 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 365 | |
| 366 | qc_get_buf(qcs, &htx_buf); |
| 367 | BUG_ON(!b_size(&htx_buf)); |
| 368 | htx = htx_from_buf(&htx_buf); |
| 369 | |
| 370 | /* first treat pseudo-header to build the start line */ |
| 371 | hdr_idx = 0; |
| 372 | while (1) { |
| 373 | if (isteq(list[hdr_idx].n, ist(""))) |
| 374 | break; |
| 375 | |
| 376 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 377 | /* pseudo-header */ |
| 378 | if (isteq(list[hdr_idx].n, ist(":method"))) |
| 379 | meth = list[hdr_idx].v; |
| 380 | else if (isteq(list[hdr_idx].n, ist(":path"))) |
| 381 | path = list[hdr_idx].v; |
| 382 | //else if (isteq(list[hdr_idx].n, ist(":scheme"))) |
| 383 | // scheme = list[hdr_idx].v; |
| 384 | else if (isteq(list[hdr_idx].n, ist(":authority"))) |
| 385 | authority = list[hdr_idx].v; |
| 386 | } |
| 387 | |
| 388 | ++hdr_idx; |
| 389 | } |
| 390 | |
| 391 | flags |= HTX_SL_F_VER_11; |
Amaury Denoyelle | 0fa14a6 | 2022-04-26 16:24:39 +0200 | [diff] [blame] | 392 | flags |= HTX_SL_F_XFER_LEN; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 393 | |
| 394 | 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] | 395 | if (!sl) { |
| 396 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 397 | return -1; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 398 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 399 | |
| 400 | if (fin) |
| 401 | sl->flags |= HTX_SL_F_BODYLESS; |
| 402 | |
| 403 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 404 | |
| 405 | if (isttest(authority)) |
| 406 | htx_add_header(htx, ist("host"), authority); |
| 407 | |
| 408 | /* now treat standard headers */ |
| 409 | hdr_idx = 0; |
| 410 | while (1) { |
| 411 | if (isteq(list[hdr_idx].n, ist(""))) |
| 412 | break; |
| 413 | |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame^] | 414 | if (isteq(list[hdr_idx].n, ist("cookie"))) { |
| 415 | http_cookie_register(list, hdr_idx, &cookie, &last_cookie); |
| 416 | continue; |
| 417 | } |
| 418 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 419 | if (!istmatch(list[hdr_idx].n, ist(":"))) |
| 420 | htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v); |
| 421 | |
| 422 | ++hdr_idx; |
| 423 | } |
| 424 | |
Amaury Denoyelle | 115ccce | 2022-08-17 18:02:47 +0200 | [diff] [blame^] | 425 | if (cookie >= 0) { |
| 426 | if (http_cookie_merge(htx, list, cookie)) { |
| 427 | h3c->err = H3_INTERNAL_ERROR; |
| 428 | return -1; |
| 429 | } |
| 430 | } |
| 431 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 432 | htx_add_endof(htx, HTX_BLK_EOH); |
| 433 | htx_to_buf(htx, &htx_buf); |
| 434 | |
| 435 | if (fin) |
| 436 | htx->flags |= HTX_FL_EOM; |
| 437 | |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 438 | if (!qc_attach_sc(qcs, &htx_buf)) { |
| 439 | h3c->err = H3_INTERNAL_ERROR; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 440 | return -1; |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 441 | } |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 442 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 443 | /* RFC 9114 5.2. Connection Shutdown |
| 444 | * |
| 445 | * The GOAWAY frame contains an identifier that |
| 446 | * indicates to the receiver the range of requests or pushes that were |
| 447 | * or might be processed in this connection. The server sends a client- |
| 448 | * initiated bidirectional stream ID; the client sends a push ID. |
| 449 | * Requests or pushes with the indicated identifier or greater are |
| 450 | * rejected (Section 4.1.1) by the sender of the GOAWAY. This |
| 451 | * identifier MAY be zero if no requests or pushes were processed. |
| 452 | */ |
| 453 | if (qcs->id >= h3c->id_goaway) |
| 454 | h3c->id_goaway = qcs->id + 4; |
| 455 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 456 | /* buffer is transferred to the stream connector and set to NULL |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 457 | * except on stream creation error. |
| 458 | */ |
| 459 | b_free(&htx_buf); |
| 460 | offer_buffers(NULL, 1); |
| 461 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 462 | 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] | 463 | return len; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 466 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 467 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 468 | * stream. |
| 469 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 470 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 471 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 472 | static ssize_t h3_data_to_htx(struct qcs *qcs, const struct buffer *buf, |
| 473 | uint64_t len, char fin) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 474 | { |
| 475 | struct buffer *appbuf; |
| 476 | struct htx *htx = NULL; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 477 | size_t htx_sent = 0; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 478 | int htx_space; |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 479 | char *head; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 480 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 481 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs); |
| 482 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 483 | appbuf = qc_get_buf(qcs, &qcs->rx.app_buf); |
| 484 | BUG_ON(!appbuf); |
| 485 | htx = htx_from_buf(appbuf); |
| 486 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 487 | if (len > b_data(buf)) { |
| 488 | len = b_data(buf); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 489 | fin = 0; |
| 490 | } |
| 491 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 492 | head = b_head(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 493 | retry: |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 494 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 495 | if (!htx_space) { |
| 496 | qcs->flags |= QC_SF_DEM_FULL; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 497 | goto out; |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 498 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 499 | |
| 500 | if (len > htx_space) { |
| 501 | len = htx_space; |
| 502 | fin = 0; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 503 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 504 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 505 | if (head + len > b_wrap(buf)) { |
| 506 | size_t contig = b_wrap(buf) - head; |
| 507 | htx_sent = htx_add_data(htx, ist2(b_head(buf), contig)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 508 | if (htx_sent < contig) { |
| 509 | qcs->flags |= QC_SF_DEM_FULL; |
| 510 | goto out; |
| 511 | } |
| 512 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 513 | len -= contig; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 514 | head = b_orig(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 515 | goto retry; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 516 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 517 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 518 | htx_sent += htx_add_data(htx, ist2(head, len)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 519 | if (htx_sent < len) { |
| 520 | qcs->flags |= QC_SF_DEM_FULL; |
| 521 | goto out; |
| 522 | } |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 523 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 524 | if (fin && len == htx_sent) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 525 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 526 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 527 | out: |
| 528 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 529 | |
| 530 | 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] | 531 | return htx_sent; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 534 | /* Parse a SETTINGS frame of length <len> of payload <buf>. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 535 | * |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 536 | * Returns the number of consumed bytes or a negative error code. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 537 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 538 | static ssize_t h3_parse_settings_frm(struct h3c *h3c, const struct buffer *buf, |
| 539 | size_t len) |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 540 | { |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 541 | struct buffer b; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 542 | uint64_t id, value; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 543 | size_t ret = 0; |
| 544 | long mask = 0; /* used to detect duplicated settings identifier */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 545 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 546 | TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn); |
| 547 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 548 | /* Work on a copy of <buf>. */ |
Amaury Denoyelle | 3a2fcfd | 2022-06-09 11:54:38 +0200 | [diff] [blame] | 549 | 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] | 550 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 551 | while (b_data(&b)) { |
| 552 | if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) { |
| 553 | h3c->err = H3_FRAME_ERROR; |
| 554 | return -1; |
| 555 | } |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 556 | |
| 557 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 558 | __func__, (unsigned long long)id, (unsigned long long)value); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 559 | |
| 560 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 561 | * |
| 562 | * The same setting identifier MUST NOT occur more than once in the |
| 563 | * SETTINGS frame. A receiver MAY treat the presence of duplicate |
| 564 | * setting identifiers as a connection error of type H3_SETTINGS_ERROR. |
| 565 | */ |
| 566 | |
| 567 | /* Ignore duplicate check for ID too big used for GREASE. */ |
| 568 | if (id < sizeof(mask)) { |
| 569 | if (ha_bit_test(id, &mask)) { |
| 570 | h3c->err = H3_SETTINGS_ERROR; |
| 571 | return -1; |
| 572 | } |
| 573 | ha_bit_set(id, &mask); |
| 574 | } |
| 575 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 576 | switch (id) { |
| 577 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 578 | h3c->qpack_max_table_capacity = value; |
| 579 | break; |
| 580 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
| 581 | h3c->max_field_section_size = value; |
| 582 | break; |
| 583 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
| 584 | h3c->qpack_blocked_streams = value; |
| 585 | break; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 586 | |
| 587 | case H3_SETTINGS_RESERVED_0: |
| 588 | case H3_SETTINGS_RESERVED_2: |
| 589 | case H3_SETTINGS_RESERVED_3: |
| 590 | case H3_SETTINGS_RESERVED_4: |
| 591 | case H3_SETTINGS_RESERVED_5: |
| 592 | /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters |
| 593 | * |
| 594 | * Setting identifiers which were defined in [HTTP2] where there is no |
| 595 | * corresponding HTTP/3 setting have also been reserved |
| 596 | * (Section 11.2.2). These reserved settings MUST NOT be sent, and |
| 597 | * their receipt MUST be treated as a connection error of type |
| 598 | * H3_SETTINGS_ERROR. |
| 599 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 600 | h3c->err = H3_SETTINGS_ERROR; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 601 | return -1; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 602 | default: |
| 603 | /* MUST be ignored */ |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
Amaury Denoyelle | 494512d | 2022-05-30 15:50:34 +0200 | [diff] [blame] | 608 | TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 609 | return ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 612 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 613 | * that we received the last data of the stream. |
Amaury Denoyelle | 0ffd6e7 | 2022-05-24 11:07:28 +0200 | [diff] [blame] | 614 | * |
| 615 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 616 | */ |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 617 | 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] | 618 | { |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 619 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 620 | struct h3c *h3c = h3s->h3c; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 621 | ssize_t total = 0, ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 622 | |
Amaury Denoyelle | bb97042 | 2022-04-12 16:40:52 +0200 | [diff] [blame] | 623 | h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 624 | if (!b_data(b)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 625 | return 0; |
| 626 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 627 | 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] | 628 | if ((ret = h3_init_uni_stream(h3c, qcs, b)) < 0) |
| 629 | return -1; |
| 630 | |
| 631 | total += ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) { |
| 635 | /* For non-h3 STREAM, parse it and return immediately. */ |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 636 | if ((ret = h3_parse_uni_stream_no_h3(qcs, b, fin)) < 0) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 637 | return -1; |
| 638 | |
| 639 | total += ret; |
| 640 | return total; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Amaury Denoyelle | 6b02c6b | 2022-08-16 17:16:47 +0200 | [diff] [blame] | 643 | /* RFC 9114 6.2.1. Control Streams |
| 644 | * |
| 645 | * The sender MUST NOT close the control stream, and the receiver MUST NOT |
| 646 | * request that the sender close the control stream. If either control |
| 647 | * stream is closed at any point, this MUST be treated as a connection |
| 648 | * error of type H3_CLOSED_CRITICAL_STREAM. |
| 649 | */ |
| 650 | if (h3s->type == H3S_T_CTRL && fin) { |
| 651 | qcc_emit_cc_app(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1); |
| 652 | return -1; |
| 653 | } |
| 654 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 655 | while (b_data(b) && !(qcs->flags & QC_SF_DEM_FULL)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 656 | uint64_t ftype, flen; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 657 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 658 | |
| 659 | /* Work on a copy of <rxbuf> */ |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 660 | if (!h3s->demux_frame_len) { |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 661 | size_t hlen = h3_decode_frm_header(&ftype, &flen, b); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 662 | if (!hlen) |
| 663 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 664 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 665 | h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n", |
| 666 | __func__, ftype, flen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 667 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 668 | h3s->demux_frame_type = ftype; |
| 669 | h3s->demux_frame_len = flen; |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 670 | total += hlen; |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 671 | |
| 672 | if (!h3_is_frame_valid(h3c, qcs, ftype)) { |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 673 | qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED, 1); |
Amaury Denoyelle | dca4c53 | 2022-06-07 18:24:34 +0200 | [diff] [blame] | 674 | return -1; |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 677 | if (!b_data(b)) |
Amaury Denoyelle | 417c7c0 | 2022-05-31 14:18:33 +0200 | [diff] [blame] | 678 | break; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 679 | } |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 680 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 681 | flen = h3s->demux_frame_len; |
| 682 | ftype = h3s->demux_frame_type; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 683 | |
| 684 | /* Do not demux incomplete frames except H3 DATA which can be |
| 685 | * fragmented in multiple HTX blocks. |
| 686 | */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 687 | if (flen > b_data(b) && ftype != H3_FT_DATA) { |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 688 | /* Reject frames bigger than bufsize. |
| 689 | * |
| 690 | * TODO HEADERS should in complement be limited with H3 |
| 691 | * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent |
| 692 | * excessive decompressed size. |
| 693 | */ |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 694 | if (flen > QC_S_RX_BUF_SZ) { |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 695 | qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD, 1); |
Amaury Denoyelle | dca4c53 | 2022-06-07 18:24:34 +0200 | [diff] [blame] | 696 | return -1; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 697 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 698 | break; |
Amaury Denoyelle | b5454d4 | 2022-05-12 16:56:16 +0200 | [diff] [blame] | 699 | } |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 700 | |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 701 | last_stream_frame = (fin && flen == b_data(b)); |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 702 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 703 | h3_inc_frame_type_cnt(h3c->prx_counters, ftype); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 704 | switch (ftype) { |
| 705 | case H3_FT_DATA: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 706 | ret = h3_data_to_htx(qcs, b, flen, last_stream_frame); |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 707 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 708 | if (ret < 0) { ABORT_NOW(); } |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 709 | h3s->st_req = H3S_ST_REQ_DATA; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 710 | break; |
| 711 | case H3_FT_HEADERS: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 712 | ret = h3_headers_to_htx(qcs, b, flen, last_stream_frame); |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 713 | if (ret < 0) { |
| 714 | /* TODO for some error, it may be preferable to |
| 715 | * only close the stream once RESET_STREAM is |
| 716 | * supported. |
| 717 | */ |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 718 | qcc_emit_cc_app(qcs->qcc, h3c->err, 1); |
Amaury Denoyelle | 2bc4786 | 2022-06-30 10:04:42 +0200 | [diff] [blame] | 719 | return -1; |
| 720 | } |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 721 | h3s->st_req = (h3s->st_req == H3S_ST_REQ_BEFORE) ? |
| 722 | H3S_ST_REQ_HEADERS : H3S_ST_REQ_TRAILERS; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 723 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 724 | case H3_FT_CANCEL_PUSH: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 725 | case H3_FT_PUSH_PROMISE: |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 726 | case H3_FT_MAX_PUSH_ID: |
| 727 | case H3_FT_GOAWAY: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 728 | /* Not supported */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 729 | ret = flen; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 730 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 731 | case H3_FT_SETTINGS: |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 732 | ret = h3_parse_settings_frm(qcs->qcc->ctx, b, flen); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 733 | if (ret < 0) { |
Amaury Denoyelle | d666d74 | 2022-07-13 15:15:58 +0200 | [diff] [blame] | 734 | qcc_emit_cc_app(qcs->qcc, h3c->err, 1); |
Amaury Denoyelle | dca4c53 | 2022-06-07 18:24:34 +0200 | [diff] [blame] | 735 | return -1; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 736 | } |
| 737 | h3c->flags |= H3_CF_SETTINGS_RECV; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 738 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 739 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 740 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 741 | * |
| 742 | * Implementations MUST discard frames [...] that have unknown |
| 743 | * or unsupported types. |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 744 | */ |
| 745 | h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype); |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 746 | ret = flen; |
| 747 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 748 | } |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 749 | |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 750 | if (ret) { |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 751 | BUG_ON(h3s->demux_frame_len < ret); |
| 752 | h3s->demux_frame_len -= ret; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 753 | b_del(b, ret); |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 754 | total += ret; |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 755 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 758 | /* TODO may be useful to wakeup the MUX if blocked due to full buffer. |
| 759 | * However, currently, io-cb of MUX does not handle Rx. |
| 760 | */ |
| 761 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 762 | return total; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 765 | /* Returns buffer for data sending. |
| 766 | * May be NULL if the allocation failed. |
| 767 | */ |
| 768 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 769 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 770 | if (!b_size(&qcs->tx.buf)) |
| 771 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 772 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 773 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 774 | } |
| 775 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 776 | /* Function used to emit stream data from <qcs> control uni-stream */ |
| 777 | 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] | 778 | { |
| 779 | int ret; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 780 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 781 | 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] | 782 | struct buffer pos, *res; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 783 | size_t frm_len; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 784 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 785 | TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs); |
| 786 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 787 | BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 788 | |
| 789 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 790 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 791 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 792 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 793 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 794 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 795 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 796 | if (h3_settings_max_field_section_size) { |
| 797 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 798 | quic_int_getsize(h3_settings_max_field_section_size); |
| 799 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 800 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 801 | b_quic_enc_int(&pos, H3_UNI_S_T_CTRL); |
| 802 | /* Build a SETTINGS frame */ |
| 803 | b_quic_enc_int(&pos, H3_FT_SETTINGS); |
| 804 | b_quic_enc_int(&pos, frm_len); |
| 805 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 806 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity); |
| 807 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS); |
| 808 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams); |
| 809 | if (h3_settings_max_field_section_size) { |
| 810 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE); |
| 811 | b_quic_enc_int(&pos, h3_settings_max_field_section_size); |
| 812 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 813 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 814 | res = mux_get_buf(qcs); |
| 815 | if (b_room(res) < b_data(&pos)) { |
| 816 | // TODO the mux should be put in blocked state, with |
| 817 | // the stream in state waiting for settings to be sent |
| 818 | ABORT_NOW(); |
| 819 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 820 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 821 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
| 822 | if (ret > 0) { |
| 823 | h3c->flags |= H3_CF_SETTINGS_SENT; |
| 824 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 825 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 828 | 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] | 829 | return ret; |
| 830 | } |
| 831 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 832 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 833 | { |
| 834 | struct buffer outbuf; |
| 835 | struct buffer headers_buf = BUF_NULL; |
| 836 | struct buffer *res; |
| 837 | struct http_hdr list[global.tune.max_http_hdr]; |
| 838 | struct htx_sl *sl; |
| 839 | struct htx_blk *blk; |
| 840 | enum htx_blk_type type; |
| 841 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 842 | int ret = 0; |
| 843 | int hdr; |
| 844 | int status = 0; |
| 845 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 846 | TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
| 847 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 848 | sl = NULL; |
| 849 | hdr = 0; |
| 850 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 851 | type = htx_get_blk_type(blk); |
| 852 | |
| 853 | if (type == HTX_BLK_UNUSED) |
| 854 | continue; |
| 855 | |
| 856 | if (type == HTX_BLK_EOH) |
| 857 | break; |
| 858 | |
| 859 | if (type == HTX_BLK_RES_SL) { |
| 860 | /* start-line -> HEADERS h3 frame */ |
| 861 | BUG_ON(sl); |
| 862 | sl = htx_get_blk_ptr(htx, blk); |
| 863 | /* TODO should be on h3 layer */ |
| 864 | status = sl->info.res.status; |
| 865 | } |
| 866 | else if (type == HTX_BLK_HDR) { |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 867 | if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1)) |
Amaury Denoyelle | fa7fadc | 2022-06-15 15:52:27 +0200 | [diff] [blame] | 868 | goto err; |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 869 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 870 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 871 | hdr++; |
| 872 | } |
| 873 | else { |
| 874 | ABORT_NOW(); |
| 875 | goto err; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | BUG_ON(!sl); |
| 880 | |
| 881 | list[hdr].n = ist(""); |
| 882 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 883 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 884 | |
| 885 | /* At least 5 bytes to store frame type + length as a varint max size */ |
| 886 | if (b_room(res) < 5) |
| 887 | ABORT_NOW(); |
| 888 | |
| 889 | b_reset(&outbuf); |
| 890 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 891 | /* Start the headers after frame type + length */ |
| 892 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 893 | |
| 894 | if (qpack_encode_field_section_line(&headers_buf)) |
| 895 | ABORT_NOW(); |
| 896 | if (qpack_encode_int_status(&headers_buf, status)) |
| 897 | ABORT_NOW(); |
| 898 | |
| 899 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 900 | if (isteq(list[hdr].n, ist(""))) |
| 901 | break; |
| 902 | |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 903 | /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges |
| 904 | * Transfer codings (see Section 6.1 of [HTTP11]) are not |
| 905 | * defined for HTTP/3; the Transfer-Encoding header field MUST |
| 906 | * NOT be used. |
| 907 | */ |
| 908 | if (isteq(list[hdr].n, ist("transfer-encoding"))) |
| 909 | continue; |
| 910 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 911 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) |
| 912 | ABORT_NOW(); |
| 913 | } |
| 914 | |
| 915 | /* Now that all headers are encoded, we are certain that res buffer is |
| 916 | * big enough |
| 917 | */ |
| 918 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 919 | res->head += 4 - frame_length_size; |
| 920 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
| 921 | if (!b_quic_enc_int(res, b_data(&headers_buf))) |
| 922 | ABORT_NOW(); |
| 923 | b_add(res, b_data(&headers_buf)); |
| 924 | |
| 925 | ret = 0; |
| 926 | blk = htx_get_head_blk(htx); |
| 927 | while (blk) { |
| 928 | type = htx_get_blk_type(blk); |
| 929 | ret += htx_get_blksz(blk); |
| 930 | blk = htx_remove_blk(htx, blk); |
| 931 | if (type == HTX_BLK_EOH) |
| 932 | break; |
| 933 | } |
| 934 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 935 | TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 936 | return ret; |
| 937 | |
| 938 | err: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 939 | 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] | 940 | return 0; |
| 941 | } |
| 942 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 943 | /* Returns the total of bytes sent. */ |
| 944 | static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count) |
| 945 | { |
| 946 | struct buffer outbuf; |
| 947 | struct buffer *res; |
| 948 | size_t total = 0; |
| 949 | struct htx *htx; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 950 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 951 | struct htx_blk *blk; |
| 952 | enum htx_blk_type type; |
| 953 | |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 954 | TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
| 955 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 956 | htx = htx_from_buf(buf); |
| 957 | |
| 958 | new_frame: |
| 959 | if (!count || htx_is_empty(htx)) |
| 960 | goto end; |
| 961 | |
| 962 | blk = htx_get_head_blk(htx); |
| 963 | type = htx_get_blk_type(blk); |
| 964 | fsize = bsize = htx_get_blksz(blk); |
| 965 | |
| 966 | if (type != HTX_BLK_DATA) |
| 967 | goto end; |
| 968 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 969 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 970 | |
| 971 | if (fsize > count) |
| 972 | fsize = count; |
| 973 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 974 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 975 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 976 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 977 | while (1) { |
| 978 | b_reset(&outbuf); |
| 979 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 980 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 981 | break; |
| 982 | b_slow_realign(res, trash.area, b_data(res)); |
| 983 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 984 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 985 | /* 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] | 986 | * stream. It is expected that the stream connector layer will subscribe |
| 987 | * on SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 988 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 989 | if (b_size(&outbuf) <= hsize) { |
| 990 | qcs->flags |= QC_SF_BLK_MROOM; |
| 991 | goto end; |
| 992 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 993 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 994 | if (b_size(&outbuf) < hsize + fsize) |
| 995 | fsize = b_size(&outbuf) - hsize; |
| 996 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 997 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 998 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
| 999 | b_quic_enc_int(&outbuf, fsize); /* h3 frame length */ |
| 1000 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1001 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1002 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1003 | count -= fsize; |
| 1004 | |
| 1005 | if (fsize == bsize) |
| 1006 | htx_remove_blk(htx, blk); |
| 1007 | else |
| 1008 | htx_cut_data_blk(htx, blk, fsize); |
| 1009 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1010 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1011 | b_add(res, b_data(&outbuf)); |
| 1012 | goto new_frame; |
| 1013 | |
| 1014 | end: |
Amaury Denoyelle | a717eb7 | 2022-05-30 15:51:01 +0200 | [diff] [blame] | 1015 | TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1016 | return total; |
| 1017 | } |
| 1018 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 1019 | size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1020 | { |
| 1021 | size_t total = 0; |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 1022 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1023 | struct htx *htx; |
| 1024 | enum htx_blk_type btype; |
| 1025 | struct htx_blk *blk; |
| 1026 | uint32_t bsize; |
| 1027 | int32_t idx; |
| 1028 | int ret; |
| 1029 | |
Amaury Denoyelle | d8769d1 | 2022-03-25 15:28:33 +0100 | [diff] [blame] | 1030 | h3_debug_printf(stderr, "%s\n", __func__); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1031 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1032 | htx = htx_from_buf(buf); |
| 1033 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 1034 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1035 | idx = htx_get_head(htx); |
| 1036 | blk = htx_get_blk(htx, idx); |
| 1037 | btype = htx_get_blk_type(blk); |
| 1038 | bsize = htx_get_blksz(blk); |
| 1039 | |
| 1040 | /* Not implemented : QUIC on backend side */ |
| 1041 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 1042 | |
| 1043 | switch (btype) { |
| 1044 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 1045 | /* start-line -> HEADERS h3 frame */ |
| 1046 | ret = h3_resp_headers_send(qcs, htx); |
| 1047 | if (ret > 0) { |
| 1048 | total += ret; |
| 1049 | count -= ret; |
| 1050 | if (ret < bsize) |
| 1051 | goto out; |
| 1052 | } |
| 1053 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1054 | |
| 1055 | case HTX_BLK_DATA: |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 1056 | ret = h3_resp_data_send(qcs, buf, count); |
| 1057 | if (ret > 0) { |
| 1058 | htx = htx_from_buf(buf); |
| 1059 | total += ret; |
| 1060 | count -= ret; |
| 1061 | if (ret < bsize) |
| 1062 | goto out; |
| 1063 | } |
| 1064 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1065 | |
| 1066 | case HTX_BLK_TLR: |
| 1067 | case HTX_BLK_EOT: |
| 1068 | /* TODO trailers */ |
| 1069 | |
| 1070 | default: |
| 1071 | htx_remove_blk(htx, blk); |
| 1072 | total += bsize; |
| 1073 | count -= bsize; |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 1078 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 1079 | qcs->flags |= QC_SF_FIN_STREAM; |
| 1080 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1081 | out: |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 1082 | if (total) { |
| 1083 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 1084 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 1085 | } |
| 1086 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 1087 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 1090 | static int h3_attach(struct qcs *qcs, void *conn_ctx) |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1091 | { |
| 1092 | struct h3s *h3s; |
| 1093 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1094 | TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
| 1095 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1096 | h3s = pool_alloc(pool_head_h3s); |
| 1097 | if (!h3s) |
| 1098 | return 1; |
| 1099 | |
| 1100 | qcs->ctx = h3s; |
Amaury Denoyelle | c015679 | 2022-06-03 15:29:07 +0200 | [diff] [blame] | 1101 | h3s->h3c = conn_ctx; |
| 1102 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1103 | h3s->demux_frame_len = 0; |
| 1104 | h3s->demux_frame_type = 0; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 1105 | h3s->flags = 0; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 1106 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 1107 | if (quic_stream_is_bidi(qcs->id)) { |
| 1108 | h3s->type = H3S_T_REQ; |
Amaury Denoyelle | 8d818c6 | 2022-08-02 11:32:45 +0200 | [diff] [blame] | 1109 | h3s->st_req = H3S_ST_REQ_BEFORE; |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 1110 | qcs_wait_http_req(qcs); |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 1111 | } |
| 1112 | else { |
| 1113 | /* stream type must be decoded for unidirectional streams */ |
| 1114 | h3s->type = H3S_T_UNKNOWN; |
| 1115 | } |
| 1116 | |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1117 | TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1121 | static void h3_detach(struct qcs *qcs) |
| 1122 | { |
| 1123 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1124 | |
| 1125 | TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
| 1126 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1127 | pool_free(pool_head_h3s, h3s); |
| 1128 | qcs->ctx = NULL; |
Amaury Denoyelle | d5581d5 | 2022-05-30 15:51:31 +0200 | [diff] [blame] | 1129 | |
| 1130 | TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs); |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1133 | static int h3_finalize(void *ctx) |
| 1134 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1135 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1136 | struct qcs *qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1137 | |
Amaury Denoyelle | b143723 | 2022-07-08 11:53:22 +0200 | [diff] [blame] | 1138 | qcs = qcc_init_stream_local(h3c->qcc, 0); |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1139 | if (!qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1140 | return 0; |
| 1141 | |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 1142 | h3_control_send(qcs, h3c); |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 1143 | h3c->ctrl_strm = qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1144 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1145 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 1148 | /* Generate a GOAWAY frame for <h3c> connection on the control stream. |
| 1149 | * |
| 1150 | * Returns 0 on success else non-zero. |
| 1151 | */ |
| 1152 | static int h3_send_goaway(struct h3c *h3c) |
| 1153 | { |
| 1154 | struct qcs *qcs = h3c->ctrl_strm; |
| 1155 | struct buffer pos, *res; |
| 1156 | unsigned char data[3 * QUIC_VARINT_MAX_SIZE]; |
| 1157 | size_t frm_len = quic_int_getsize(h3c->id_goaway); |
| 1158 | |
| 1159 | if (!qcs) |
| 1160 | return 1; |
| 1161 | |
| 1162 | pos = b_make((char *)data, sizeof(data), 0, 0); |
| 1163 | |
| 1164 | b_quic_enc_int(&pos, H3_FT_GOAWAY); |
| 1165 | b_quic_enc_int(&pos, frm_len); |
| 1166 | b_quic_enc_int(&pos, h3c->id_goaway); |
| 1167 | |
| 1168 | res = mux_get_buf(qcs); |
| 1169 | if (!res || b_room(res) < b_data(&pos)) { |
| 1170 | /* Do not try forcefully to emit GOAWAY if no space left. */ |
| 1171 | return 1; |
| 1172 | } |
| 1173 | |
| 1174 | b_force_xfer(res, &pos, b_data(&pos)); |
| 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1179 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 1180 | * Return 1 if succeeded, 0 if not. |
| 1181 | */ |
| 1182 | static int h3_init(struct qcc *qcc) |
| 1183 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1184 | struct h3c *h3c; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1185 | struct quic_conn *qc = qcc->conn->handle.qc; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1186 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1187 | h3c = pool_alloc(pool_head_h3c); |
| 1188 | if (!h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1189 | goto fail_no_h3; |
| 1190 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1191 | h3c->qcc = qcc; |
Amaury Denoyelle | d701039 | 2022-07-13 15:17:29 +0200 | [diff] [blame] | 1192 | h3c->ctrl_strm = NULL; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1193 | h3c->err = H3_NO_ERROR; |
| 1194 | h3c->flags = 0; |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 1195 | h3c->id_goaway = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1196 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1197 | qcc->ctx = h3c; |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1198 | h3c->prx_counters = |
| 1199 | EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe, |
| 1200 | &h3_stats_module); |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1201 | LIST_INIT(&h3c->buf_wait.list); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1202 | |
| 1203 | return 1; |
| 1204 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1205 | fail_no_h3: |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1209 | static void h3_release(void *ctx) |
| 1210 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1211 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* RFC 9114 5.2. Connection Shutdown |
| 1214 | * |
Amaury Denoyelle | 114c9c8 | 2022-03-28 14:53:45 +0200 | [diff] [blame] | 1215 | * Even when a connection is not idle, either endpoint can decide to |
| 1216 | * stop using the connection and initiate a graceful connection close. |
| 1217 | * Endpoints initiate the graceful shutdown of an HTTP/3 connection by |
| 1218 | * sending a GOAWAY frame. |
| 1219 | */ |
| 1220 | h3_send_goaway(h3c); |
| 1221 | |
| 1222 | /* RFC 9114 5.2. Connection Shutdown |
| 1223 | * |
Amaury Denoyelle | 069288b | 2022-07-15 10:58:25 +0200 | [diff] [blame] | 1224 | * An endpoint that completes a |
| 1225 | * graceful shutdown SHOULD use the H3_NO_ERROR error code when closing |
| 1226 | * the connection. |
| 1227 | */ |
| 1228 | qcc_emit_cc_app(h3c->qcc, H3_NO_ERROR, 0); |
| 1229 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1230 | pool_free(pool_head_h3c, h3c); |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1233 | /* Increment the h3 error code counters for <error_code> value */ |
| 1234 | static void h3_stats_inc_err_cnt(void *ctx, int err_code) |
| 1235 | { |
| 1236 | struct h3c *h3c = ctx; |
| 1237 | |
| 1238 | h3_inc_err_cnt(h3c->prx_counters, err_code); |
| 1239 | } |
| 1240 | |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 1241 | /* h3 trace handler */ |
| 1242 | static void h3_trace(enum trace_level level, uint64_t mask, |
| 1243 | const struct trace_source *src, |
| 1244 | const struct ist where, const struct ist func, |
| 1245 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 1246 | { |
| 1247 | const struct connection *conn = a1; |
| 1248 | const struct qcc *qcc = conn ? conn->ctx : NULL; |
| 1249 | const struct qcs *qcs = a2; |
| 1250 | |
| 1251 | if (src->verbosity > H3_VERB_CLEAN) { |
| 1252 | chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc); |
| 1253 | |
| 1254 | if (qcs) |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 1255 | chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id); |
Amaury Denoyelle | 016aa93 | 2022-05-30 15:49:36 +0200 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1259 | /* HTTP/3 application layer operations */ |
| 1260 | const struct qcc_app_ops h3_ops = { |
| 1261 | .init = h3_init, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1262 | .attach = h3_attach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1263 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 1264 | .snd_buf = h3_snd_buf, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1265 | .detach = h3_detach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1266 | .finalize = h3_finalize, |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1267 | .release = h3_release, |
Frédéric Lécaille | 6f7607e | 2022-05-25 22:25:37 +0200 | [diff] [blame] | 1268 | .inc_err_cnt = h3_stats_inc_err_cnt, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1269 | }; |