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> |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 23 | #include <haproxy/http.h> |
| 24 | #include <haproxy/htx.h> |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 25 | #include <haproxy/intops.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 26 | #include <haproxy/istbuf.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 27 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 28 | #include <haproxy/ncbuf.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 29 | #include <haproxy/pool.h> |
| 30 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 31 | #include <haproxy/qpack-enc.h> |
| 32 | #include <haproxy/quic_enc.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 33 | #include <haproxy/stconn.h> |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 34 | #include <haproxy/tools.h> |
| 35 | #include <haproxy/xprt_quic.h> |
| 36 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 37 | #if defined(DEBUG_H3) |
| 38 | #define h3_debug_printf fprintf |
| 39 | #define h3_debug_hexdump debug_hexdump |
| 40 | #else |
| 41 | #define h3_debug_printf(...) do { } while (0) |
| 42 | #define h3_debug_hexdump(...) do { } while (0) |
| 43 | #endif |
| 44 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 45 | #define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */ |
| 46 | #define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */ |
| 47 | #define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */ |
| 48 | #define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */ |
| 49 | #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] | 50 | |
| 51 | /* Default settings */ |
Amaury Denoyelle | 3394939 | 2021-08-24 15:16:58 +0200 | [diff] [blame] | 52 | static uint64_t h3_settings_qpack_max_table_capacity = 0; |
| 53 | static uint64_t h3_settings_qpack_blocked_streams = 4096; |
| 54 | 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] | 55 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 56 | struct h3c { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 57 | struct qcc *qcc; |
| 58 | enum h3_err err; |
| 59 | uint32_t flags; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 60 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 61 | /* Settings */ |
| 62 | uint64_t qpack_max_table_capacity; |
| 63 | uint64_t qpack_blocked_streams; |
| 64 | uint64_t max_field_section_size; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 65 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 66 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
| 67 | }; |
| 68 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 69 | 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] | 70 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 71 | #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] | 72 | #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] | 73 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 74 | struct h3s { |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 75 | enum h3s_t type; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 76 | int demux_frame_len; |
| 77 | int demux_frame_type; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 78 | |
| 79 | int flags; |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s)); |
| 83 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 84 | /* Simple function to duplicate a buffer */ |
Amaury Denoyelle | c7dd9d6 | 2022-05-24 18:14:28 +0200 | [diff] [blame] | 85 | static inline struct buffer h3_b_dup(const struct ncbuf *b) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 86 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 87 | return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0)); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 90 | /* Initialize an uni-stream <qcs> by reading its type from <rxbuf>. |
| 91 | * |
| 92 | * Returns 0 on success else non-zero. |
| 93 | */ |
| 94 | static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs, |
| 95 | struct ncbuf *rxbuf) |
| 96 | { |
| 97 | /* decode unidirectional stream type */ |
| 98 | struct h3s *h3s = qcs->ctx; |
| 99 | struct buffer b; |
| 100 | uint64_t type; |
| 101 | size_t len = 0, ret; |
| 102 | |
| 103 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 104 | h3s->flags & H3_SF_UNI_INIT); |
| 105 | |
| 106 | b = h3_b_dup(rxbuf); |
| 107 | ret = b_quic_dec_int(&type, &b, &len); |
| 108 | if (!ret) { |
| 109 | ABORT_NOW(); |
| 110 | } |
| 111 | |
| 112 | switch (type) { |
| 113 | case H3_UNI_S_T_CTRL: |
| 114 | if (h3c->flags & H3_CF_UNI_CTRL_SET) { |
| 115 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 116 | return 1; |
| 117 | } |
| 118 | h3c->flags |= H3_CF_UNI_CTRL_SET; |
| 119 | h3s->type = H3S_T_CTRL; |
| 120 | break; |
| 121 | |
| 122 | case H3_UNI_S_T_PUSH: |
| 123 | /* TODO not supported for the moment */ |
| 124 | h3s->type = H3S_T_PUSH; |
| 125 | break; |
| 126 | |
| 127 | case H3_UNI_S_T_QPACK_DEC: |
| 128 | if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) { |
| 129 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 130 | return 1; |
| 131 | } |
| 132 | h3c->flags |= H3_CF_UNI_QPACK_DEC_SET; |
| 133 | h3s->type = H3S_T_QPACK_DEC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 134 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case H3_UNI_S_T_QPACK_ENC: |
| 138 | if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) { |
| 139 | qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR); |
| 140 | return 1; |
| 141 | } |
| 142 | h3c->flags |= H3_CF_UNI_QPACK_ENC_SET; |
| 143 | h3s->type = H3S_T_QPACK_ENC; |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 144 | h3s->flags |= H3_SF_UNI_NO_H3; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 148 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 149 | * |
| 150 | * Implementations MUST [...] abort reading on unidirectional |
| 151 | * streams that have unknown or unsupported types. |
| 152 | */ |
| 153 | qcs->flags |= QC_SF_READ_ABORTED; |
| 154 | return 1; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | h3s->flags |= H3_SF_UNI_INIT; |
| 158 | qcs_consume(qcs, len); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 163 | /* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames. |
| 164 | * This may be used for QPACK encoder/decoder streams for example. |
| 165 | * |
| 166 | * Returns 0 on success else non-zero. |
| 167 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 168 | static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf) |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 169 | { |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 170 | struct h3s *h3s = qcs->ctx; |
| 171 | |
| 172 | BUG_ON_HOT(!quic_stream_is_uni(qcs->id) || |
| 173 | !(h3s->flags & H3_SF_UNI_NO_H3)); |
| 174 | |
| 175 | switch (h3s->type) { |
| 176 | case H3S_T_QPACK_DEC: |
| 177 | if (!qpack_decode_dec(qcs, NULL)) |
| 178 | return 1; |
| 179 | break; |
| 180 | case H3S_T_QPACK_ENC: |
| 181 | if (!qpack_decode_enc(qcs, NULL)) |
| 182 | return 1; |
| 183 | break; |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 184 | case H3S_T_UNKNOWN: |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 185 | default: |
Amaury Denoyelle | 849b24f | 2022-05-24 17:22:07 +0200 | [diff] [blame] | 186 | /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */ |
| 187 | ABORT_NOW(); |
Amaury Denoyelle | fc99a69 | 2022-05-24 15:25:19 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 193 | /* Decode a h3 frame header made of two QUIC varints from <b> buffer. |
| 194 | * Returns the number of bytes consumed if there was enough data in <b>, 0 if not. |
| 195 | * Note that this function update <b> buffer to reflect the number of bytes consumed |
| 196 | * to decode the h3 frame header. |
| 197 | */ |
| 198 | static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen, |
| 199 | struct buffer *b) |
| 200 | { |
| 201 | size_t hlen; |
| 202 | |
| 203 | hlen = 0; |
| 204 | if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen)) |
| 205 | return 0; |
| 206 | |
| 207 | return hlen; |
| 208 | } |
| 209 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 210 | /* Check if H3 frame of type <ftype> is valid when received on stream <qcs>. |
| 211 | * |
| 212 | * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should |
| 213 | * be reported. |
| 214 | */ |
| 215 | static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype) |
| 216 | { |
| 217 | struct h3s *h3s = qcs->ctx; |
| 218 | const uint64_t id = qcs->id; |
| 219 | |
| 220 | BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN); |
| 221 | |
| 222 | switch (ftype) { |
| 223 | case H3_FT_DATA: |
| 224 | case H3_FT_HEADERS: |
| 225 | return h3s->type != H3S_T_CTRL; |
| 226 | |
| 227 | case H3_FT_CANCEL_PUSH: |
| 228 | case H3_FT_GOAWAY: |
| 229 | case H3_FT_MAX_PUSH_ID: |
| 230 | /* Only allowed for control stream. First frame of control |
| 231 | * stream MUST be SETTINGS. |
| 232 | */ |
| 233 | return h3s->type == H3S_T_CTRL && |
| 234 | (h3c->flags & H3_CF_SETTINGS_RECV); |
| 235 | |
| 236 | case H3_FT_SETTINGS: |
| 237 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 238 | * |
| 239 | * If an endpoint receives a second SETTINGS frame on the control |
| 240 | * stream, the endpoint MUST respond with a connection error of type |
| 241 | * H3_FRAME_UNEXPECTED. |
| 242 | */ |
| 243 | return h3s->type == H3S_T_CTRL && |
| 244 | !(h3c->flags & H3_CF_SETTINGS_RECV); |
| 245 | |
| 246 | case H3_FT_PUSH_PROMISE: |
| 247 | return h3s->type != H3S_T_CTRL && |
| 248 | (id & QCS_ID_SRV_INTIATOR_BIT); |
| 249 | |
| 250 | default: |
| 251 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
| 252 | * |
| 253 | * Implementations MUST discard frames [...] that have unknown |
| 254 | * or unsupported types. |
| 255 | */ |
| 256 | return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV); |
| 257 | } |
| 258 | } |
| 259 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 260 | /* 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] | 261 | * 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] | 262 | * set if this is the last data to transfer from this stream. |
| 263 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 264 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 265 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 266 | static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len, |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 267 | char fin) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 268 | { |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 269 | struct buffer htx_buf = BUF_NULL; |
| 270 | struct buffer *tmp = get_trash_chunk(); |
Amaury Denoyelle | 7059ebc | 2021-12-08 15:51:04 +0100 | [diff] [blame] | 271 | struct htx *htx = NULL; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 272 | struct htx_sl *sl; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 273 | struct http_hdr list[global.tune.max_http_hdr]; |
Amaury Denoyelle | b49fa1a | 2021-08-24 15:30:12 +0200 | [diff] [blame] | 274 | unsigned int flags = HTX_SL_F_NONE; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 275 | struct ist meth = IST_NULL, path = IST_NULL; |
| 276 | //struct ist scheme = IST_NULL, authority = IST_NULL; |
| 277 | struct ist authority = IST_NULL; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 278 | int hdr_idx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 279 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 280 | /* TODO support buffer wrapping */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 281 | BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf)); |
| 282 | if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 283 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 284 | |
| 285 | qc_get_buf(qcs, &htx_buf); |
| 286 | BUG_ON(!b_size(&htx_buf)); |
| 287 | htx = htx_from_buf(&htx_buf); |
| 288 | |
| 289 | /* first treat pseudo-header to build the start line */ |
| 290 | hdr_idx = 0; |
| 291 | while (1) { |
| 292 | if (isteq(list[hdr_idx].n, ist(""))) |
| 293 | break; |
| 294 | |
| 295 | if (istmatch(list[hdr_idx].n, ist(":"))) { |
| 296 | /* pseudo-header */ |
| 297 | if (isteq(list[hdr_idx].n, ist(":method"))) |
| 298 | meth = list[hdr_idx].v; |
| 299 | else if (isteq(list[hdr_idx].n, ist(":path"))) |
| 300 | path = list[hdr_idx].v; |
| 301 | //else if (isteq(list[hdr_idx].n, ist(":scheme"))) |
| 302 | // scheme = list[hdr_idx].v; |
| 303 | else if (isteq(list[hdr_idx].n, ist(":authority"))) |
| 304 | authority = list[hdr_idx].v; |
| 305 | } |
| 306 | |
| 307 | ++hdr_idx; |
| 308 | } |
| 309 | |
| 310 | flags |= HTX_SL_F_VER_11; |
Amaury Denoyelle | 0fa14a6 | 2022-04-26 16:24:39 +0200 | [diff] [blame] | 311 | flags |= HTX_SL_F_XFER_LEN; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 312 | |
| 313 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0")); |
| 314 | if (!sl) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 315 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 316 | |
| 317 | if (fin) |
| 318 | sl->flags |= HTX_SL_F_BODYLESS; |
| 319 | |
| 320 | sl->info.req.meth = find_http_meth(meth.ptr, meth.len); |
| 321 | BUG_ON(sl->info.req.meth == HTTP_METH_OTHER); |
| 322 | |
| 323 | if (isttest(authority)) |
| 324 | htx_add_header(htx, ist("host"), authority); |
| 325 | |
| 326 | /* now treat standard headers */ |
| 327 | hdr_idx = 0; |
| 328 | while (1) { |
| 329 | if (isteq(list[hdr_idx].n, ist(""))) |
| 330 | break; |
| 331 | |
| 332 | if (!istmatch(list[hdr_idx].n, ist(":"))) |
| 333 | htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v); |
| 334 | |
| 335 | ++hdr_idx; |
| 336 | } |
| 337 | |
| 338 | htx_add_endof(htx, HTX_BLK_EOH); |
| 339 | htx_to_buf(htx, &htx_buf); |
| 340 | |
| 341 | if (fin) |
| 342 | htx->flags |= HTX_FL_EOM; |
| 343 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 344 | if (!qc_attach_sc(qcs, &htx_buf)) |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 345 | return -1; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 346 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 347 | /* buffer is transferred to the stream connector and set to NULL |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 348 | * except on stream creation error. |
| 349 | */ |
| 350 | b_free(&htx_buf); |
| 351 | offer_buffers(NULL, 1); |
| 352 | |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 353 | return len; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 356 | /* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs> |
| 357 | * HTX buffer. <fin> must be set if this is the last data to transfer from this |
| 358 | * stream. |
| 359 | * |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 360 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 361 | */ |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 362 | static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len, |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 363 | char fin) |
| 364 | { |
| 365 | struct buffer *appbuf; |
| 366 | struct htx *htx = NULL; |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 367 | size_t htx_sent = 0; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 368 | int htx_space; |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 369 | char *head; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 370 | |
| 371 | appbuf = qc_get_buf(qcs, &qcs->rx.app_buf); |
| 372 | BUG_ON(!appbuf); |
| 373 | htx = htx_from_buf(appbuf); |
| 374 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 375 | if (len > ncb_data(buf, 0)) { |
| 376 | len = ncb_data(buf, 0); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 377 | fin = 0; |
| 378 | } |
| 379 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 380 | head = ncb_head(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 381 | retry: |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 382 | htx_space = htx_free_data_space(htx); |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 383 | if (!htx_space) { |
| 384 | qcs->flags |= QC_SF_DEM_FULL; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 385 | goto out; |
Amaury Denoyelle | f1fc0b3 | 2022-05-02 11:07:06 +0200 | [diff] [blame] | 386 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 387 | |
| 388 | if (len > htx_space) { |
| 389 | len = htx_space; |
| 390 | fin = 0; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 391 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 392 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 393 | if (head + len > ncb_wrap(buf)) { |
| 394 | size_t contig = ncb_wrap(buf) - head; |
| 395 | htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 396 | if (htx_sent < contig) { |
| 397 | qcs->flags |= QC_SF_DEM_FULL; |
| 398 | goto out; |
| 399 | } |
| 400 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 401 | len -= contig; |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 402 | head = ncb_orig(buf); |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 403 | goto retry; |
Amaury Denoyelle | ff191de | 2022-02-21 18:38:29 +0100 | [diff] [blame] | 404 | } |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 405 | |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 406 | htx_sent += htx_add_data(htx, ist2(head, len)); |
Amaury Denoyelle | 73d6ffe | 2022-05-16 13:54:31 +0200 | [diff] [blame] | 407 | if (htx_sent < len) { |
| 408 | qcs->flags |= QC_SF_DEM_FULL; |
| 409 | goto out; |
| 410 | } |
Amaury Denoyelle | 30f23f5 | 2022-04-27 14:41:53 +0200 | [diff] [blame] | 411 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 412 | if (fin && len == htx_sent) |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 413 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 414 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 415 | out: |
| 416 | htx_to_buf(htx, appbuf); |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 417 | return htx_sent; |
Amaury Denoyelle | 91379f7 | 2022-02-14 17:14:59 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 420 | /* Parse a SETTINGS frame of length <len> of payload <rxbuf>. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 421 | * |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 422 | * Returns the number of bytes handled or a negative error code. |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 423 | */ |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 424 | static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf, |
| 425 | size_t len) |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 426 | { |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 427 | struct buffer b; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 428 | uint64_t id, value; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 429 | size_t ret = 0; |
| 430 | long mask = 0; /* used to detect duplicated settings identifier */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 431 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 432 | b = h3_b_dup(rxbuf); |
| 433 | b_set_data(&b, len); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 434 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 435 | while (b_data(&b)) { |
| 436 | if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) { |
| 437 | h3c->err = H3_FRAME_ERROR; |
| 438 | return -1; |
| 439 | } |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 440 | |
| 441 | h3_debug_printf(stderr, "%s id: %llu value: %llu\n", |
| 442 | __func__, (unsigned long long)id, (unsigned long long)value); |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 443 | |
| 444 | /* draft-ietf-quic-http34 7.2.4. SETTINGS |
| 445 | * |
| 446 | * The same setting identifier MUST NOT occur more than once in the |
| 447 | * SETTINGS frame. A receiver MAY treat the presence of duplicate |
| 448 | * setting identifiers as a connection error of type H3_SETTINGS_ERROR. |
| 449 | */ |
| 450 | |
| 451 | /* Ignore duplicate check for ID too big used for GREASE. */ |
| 452 | if (id < sizeof(mask)) { |
| 453 | if (ha_bit_test(id, &mask)) { |
| 454 | h3c->err = H3_SETTINGS_ERROR; |
| 455 | return -1; |
| 456 | } |
| 457 | ha_bit_set(id, &mask); |
| 458 | } |
| 459 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 460 | switch (id) { |
| 461 | case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 462 | h3c->qpack_max_table_capacity = value; |
| 463 | break; |
| 464 | case H3_SETTINGS_MAX_FIELD_SECTION_SIZE: |
| 465 | h3c->max_field_section_size = value; |
| 466 | break; |
| 467 | case H3_SETTINGS_QPACK_BLOCKED_STREAMS: |
| 468 | h3c->qpack_blocked_streams = value; |
| 469 | break; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 470 | |
| 471 | case H3_SETTINGS_RESERVED_0: |
| 472 | case H3_SETTINGS_RESERVED_2: |
| 473 | case H3_SETTINGS_RESERVED_3: |
| 474 | case H3_SETTINGS_RESERVED_4: |
| 475 | case H3_SETTINGS_RESERVED_5: |
| 476 | /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters |
| 477 | * |
| 478 | * Setting identifiers which were defined in [HTTP2] where there is no |
| 479 | * corresponding HTTP/3 setting have also been reserved |
| 480 | * (Section 11.2.2). These reserved settings MUST NOT be sent, and |
| 481 | * their receipt MUST be treated as a connection error of type |
| 482 | * H3_SETTINGS_ERROR. |
| 483 | */ |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 484 | h3c->err = H3_SETTINGS_ERROR; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 485 | return -1; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 486 | default: |
| 487 | /* MUST be ignored */ |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 492 | return ret; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 495 | /* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate |
| 496 | * that we received the last data of the stream. |
Amaury Denoyelle | 0ffd6e7 | 2022-05-24 11:07:28 +0200 | [diff] [blame] | 497 | * |
| 498 | * Returns 0 on success else non-zero. |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 499 | */ |
| 500 | static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx) |
| 501 | { |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 502 | struct ncbuf *rxbuf = &qcs->rx.ncbuf; |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 503 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 504 | struct h3s *h3s = qcs->ctx; |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 505 | ssize_t ret; |
Amaury Denoyelle | 7b0f122 | 2022-02-14 17:13:55 +0100 | [diff] [blame] | 506 | |
Amaury Denoyelle | bb97042 | 2022-04-12 16:40:52 +0200 | [diff] [blame] | 507 | h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id); |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 508 | if (!ncb_data(rxbuf, 0)) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 509 | return 0; |
| 510 | |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 511 | if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) { |
| 512 | if (h3_init_uni_stream(h3c, qcs, rxbuf)) |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) { |
| 517 | /* For non-h3 STREAM, parse it and return immediately. */ |
| 518 | if (h3_parse_uni_stream_no_h3(qcs, rxbuf)) |
| 519 | return 1; |
| 520 | return 0; |
| 521 | } |
| 522 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 523 | while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) { |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 524 | uint64_t ftype, flen; |
| 525 | struct buffer b; |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 526 | char last_stream_frame = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 527 | |
| 528 | /* Work on a copy of <rxbuf> */ |
| 529 | b = h3_b_dup(rxbuf); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 530 | if (!h3s->demux_frame_len) { |
| 531 | size_t hlen = h3_decode_frm_header(&ftype, &flen, &b); |
| 532 | if (!hlen) |
| 533 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 534 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 535 | h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n", |
| 536 | __func__, ftype, flen); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 537 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 538 | h3s->demux_frame_type = ftype; |
| 539 | h3s->demux_frame_len = flen; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 540 | qcs_consume(qcs, hlen); |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 541 | } |
Amaury Denoyelle | 0484f92 | 2022-02-15 16:59:39 +0100 | [diff] [blame] | 542 | |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 543 | flen = h3s->demux_frame_len; |
| 544 | ftype = h3s->demux_frame_type; |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 545 | |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 546 | if (!h3_is_frame_valid(h3c, qcs, ftype)) { |
| 547 | qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED); |
| 548 | return 1; |
| 549 | } |
| 550 | |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 551 | /* Do not demux incomplete frames except H3 DATA which can be |
| 552 | * fragmented in multiple HTX blocks. |
| 553 | */ |
| 554 | if (flen > b_data(&b) && ftype != H3_FT_DATA) { |
| 555 | /* Reject frames bigger than bufsize. |
| 556 | * |
| 557 | * TODO HEADERS should in complement be limited with H3 |
| 558 | * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent |
| 559 | * excessive decompressed size. |
| 560 | */ |
| 561 | if (flen > ncb_size(rxbuf)) { |
| 562 | qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD); |
| 563 | return 1; |
| 564 | } |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 565 | break; |
Amaury Denoyelle | b5454d4 | 2022-05-12 16:56:16 +0200 | [diff] [blame] | 566 | } |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 567 | |
Amaury Denoyelle | 1290f1e | 2022-05-13 14:49:05 +0200 | [diff] [blame] | 568 | last_stream_frame = (fin && flen == ncb_total_data(rxbuf)); |
Amaury Denoyelle | 95b93a3 | 2022-02-14 15:49:53 +0100 | [diff] [blame] | 569 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 570 | switch (ftype) { |
| 571 | case H3_FT_DATA: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 572 | ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 573 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 574 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 575 | break; |
| 576 | case H3_FT_HEADERS: |
Amaury Denoyelle | 31e4f6e | 2022-02-15 17:30:27 +0100 | [diff] [blame] | 577 | ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame); |
| 578 | /* TODO handle error reporting. Stream closure required. */ |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 579 | if (ret < 0) { ABORT_NOW(); } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 580 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 581 | case H3_FT_CANCEL_PUSH: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 582 | case H3_FT_PUSH_PROMISE: |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 583 | case H3_FT_MAX_PUSH_ID: |
| 584 | case H3_FT_GOAWAY: |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 585 | /* Not supported */ |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 586 | ret = flen; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 587 | break; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 588 | case H3_FT_SETTINGS: |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 589 | ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen); |
| 590 | if (ret < 0) { |
| 591 | qcc_emit_cc_app(qcs->qcc, h3c->err); |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 592 | return 1; |
Amaury Denoyelle | 8c6176b | 2022-05-24 18:16:49 +0200 | [diff] [blame] | 593 | } |
| 594 | h3c->flags |= H3_CF_SETTINGS_RECV; |
Amaury Denoyelle | f8db5aa | 2022-05-24 15:26:07 +0200 | [diff] [blame] | 595 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 596 | default: |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 597 | /* draft-ietf-quic-http34 9. Extensions to HTTP/3 |
Amaury Denoyelle | 302ecd4 | 2022-05-24 15:24:32 +0200 | [diff] [blame] | 598 | * |
| 599 | * Implementations MUST discard frames [...] that have unknown |
| 600 | * or unsupported types. |
Amaury Denoyelle | d1acaf9 | 2021-11-15 15:52:55 +0100 | [diff] [blame] | 601 | */ |
| 602 | h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype); |
Amaury Denoyelle | 80097cc | 2022-05-24 11:13:46 +0200 | [diff] [blame] | 603 | ret = flen; |
| 604 | break; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 605 | } |
Amaury Denoyelle | 314578a | 2022-04-27 14:52:52 +0200 | [diff] [blame] | 606 | |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 607 | if (ret) { |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 608 | BUG_ON(h3s->demux_frame_len < ret); |
| 609 | h3s->demux_frame_len -= ret; |
Amaury Denoyelle | a977355 | 2022-05-16 14:38:25 +0200 | [diff] [blame] | 610 | qcs_consume(qcs, ret); |
Amaury Denoyelle | 291ee25 | 2022-05-02 10:35:39 +0200 | [diff] [blame] | 611 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Amaury Denoyelle | 03cc62c | 2022-04-27 16:53:16 +0200 | [diff] [blame] | 614 | /* TODO may be useful to wakeup the MUX if blocked due to full buffer. |
| 615 | * However, currently, io-cb of MUX does not handle Rx. |
| 616 | */ |
| 617 | |
Amaury Denoyelle | b9ce14e | 2021-11-08 09:13:42 +0100 | [diff] [blame] | 618 | return 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 621 | /* Returns buffer for data sending. |
| 622 | * May be NULL if the allocation failed. |
| 623 | */ |
| 624 | static struct buffer *mux_get_buf(struct qcs *qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 625 | { |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 626 | if (!b_size(&qcs->tx.buf)) |
| 627 | b_alloc(&qcs->tx.buf); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 628 | |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 629 | return &qcs->tx.buf; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Amaury Denoyelle | 6b92394 | 2022-05-23 14:25:53 +0200 | [diff] [blame] | 632 | /* Function used to emit stream data from <qcs> control uni-stream */ |
| 633 | 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] | 634 | { |
| 635 | int ret; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 636 | struct h3c *h3c = ctx; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 637 | 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] | 638 | struct buffer pos, *res; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 639 | size_t frm_len; |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 640 | |
| 641 | BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 642 | |
| 643 | ret = 0; |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 644 | pos = b_make((char *)data, sizeof(data), 0, 0); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 645 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 646 | frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) + |
| 647 | quic_int_getsize(h3_settings_qpack_max_table_capacity) + |
| 648 | quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) + |
| 649 | quic_int_getsize(h3_settings_qpack_blocked_streams); |
| 650 | if (h3_settings_max_field_section_size) { |
| 651 | frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) + |
| 652 | quic_int_getsize(h3_settings_max_field_section_size); |
| 653 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 654 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 655 | b_quic_enc_int(&pos, H3_UNI_S_T_CTRL); |
| 656 | /* Build a SETTINGS frame */ |
| 657 | b_quic_enc_int(&pos, H3_FT_SETTINGS); |
| 658 | b_quic_enc_int(&pos, frm_len); |
| 659 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY); |
| 660 | b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity); |
| 661 | b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS); |
| 662 | b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams); |
| 663 | if (h3_settings_max_field_section_size) { |
| 664 | b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE); |
| 665 | b_quic_enc_int(&pos, h3_settings_max_field_section_size); |
| 666 | } |
Amaury Denoyelle | a587136 | 2021-10-07 16:26:12 +0200 | [diff] [blame] | 667 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 668 | res = mux_get_buf(qcs); |
| 669 | if (b_room(res) < b_data(&pos)) { |
| 670 | // TODO the mux should be put in blocked state, with |
| 671 | // the stream in state waiting for settings to be sent |
| 672 | ABORT_NOW(); |
| 673 | } |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 674 | |
Amaury Denoyelle | 65df3ad | 2022-05-24 15:06:10 +0200 | [diff] [blame] | 675 | ret = b_force_xfer(res, &pos, b_data(&pos)); |
| 676 | if (ret > 0) { |
| 677 | h3c->flags |= H3_CF_SETTINGS_SENT; |
| 678 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 679 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | return ret; |
| 683 | } |
| 684 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 685 | static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx) |
| 686 | { |
| 687 | struct buffer outbuf; |
| 688 | struct buffer headers_buf = BUF_NULL; |
| 689 | struct buffer *res; |
| 690 | struct http_hdr list[global.tune.max_http_hdr]; |
| 691 | struct htx_sl *sl; |
| 692 | struct htx_blk *blk; |
| 693 | enum htx_blk_type type; |
| 694 | int frame_length_size; /* size in bytes of frame length varint field */ |
| 695 | int ret = 0; |
| 696 | int hdr; |
| 697 | int status = 0; |
| 698 | |
| 699 | sl = NULL; |
| 700 | hdr = 0; |
| 701 | for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 702 | type = htx_get_blk_type(blk); |
| 703 | |
| 704 | if (type == HTX_BLK_UNUSED) |
| 705 | continue; |
| 706 | |
| 707 | if (type == HTX_BLK_EOH) |
| 708 | break; |
| 709 | |
| 710 | if (type == HTX_BLK_RES_SL) { |
| 711 | /* start-line -> HEADERS h3 frame */ |
| 712 | BUG_ON(sl); |
| 713 | sl = htx_get_blk_ptr(htx, blk); |
| 714 | /* TODO should be on h3 layer */ |
| 715 | status = sl->info.res.status; |
| 716 | } |
| 717 | else if (type == HTX_BLK_HDR) { |
| 718 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 719 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 720 | hdr++; |
| 721 | } |
| 722 | else { |
| 723 | ABORT_NOW(); |
| 724 | goto err; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | BUG_ON(!sl); |
| 729 | |
| 730 | list[hdr].n = ist(""); |
| 731 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 732 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 733 | |
| 734 | /* At least 5 bytes to store frame type + length as a varint max size */ |
| 735 | if (b_room(res) < 5) |
| 736 | ABORT_NOW(); |
| 737 | |
| 738 | b_reset(&outbuf); |
| 739 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 740 | /* Start the headers after frame type + length */ |
| 741 | headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0); |
| 742 | |
| 743 | if (qpack_encode_field_section_line(&headers_buf)) |
| 744 | ABORT_NOW(); |
| 745 | if (qpack_encode_int_status(&headers_buf, status)) |
| 746 | ABORT_NOW(); |
| 747 | |
| 748 | for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) { |
| 749 | if (isteq(list[hdr].n, ist(""))) |
| 750 | break; |
| 751 | |
Amaury Denoyelle | ffafb3d | 2022-02-15 16:10:42 +0100 | [diff] [blame] | 752 | /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges |
| 753 | * Transfer codings (see Section 6.1 of [HTTP11]) are not |
| 754 | * defined for HTTP/3; the Transfer-Encoding header field MUST |
| 755 | * NOT be used. |
| 756 | */ |
| 757 | if (isteq(list[hdr].n, ist("transfer-encoding"))) |
| 758 | continue; |
| 759 | |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 760 | if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) |
| 761 | ABORT_NOW(); |
| 762 | } |
| 763 | |
| 764 | /* Now that all headers are encoded, we are certain that res buffer is |
| 765 | * big enough |
| 766 | */ |
| 767 | frame_length_size = quic_int_getsize(b_data(&headers_buf)); |
| 768 | res->head += 4 - frame_length_size; |
| 769 | b_putchr(res, 0x01); /* h3 HEADERS frame type */ |
| 770 | if (!b_quic_enc_int(res, b_data(&headers_buf))) |
| 771 | ABORT_NOW(); |
| 772 | b_add(res, b_data(&headers_buf)); |
| 773 | |
| 774 | ret = 0; |
| 775 | blk = htx_get_head_blk(htx); |
| 776 | while (blk) { |
| 777 | type = htx_get_blk_type(blk); |
| 778 | ret += htx_get_blksz(blk); |
| 779 | blk = htx_remove_blk(htx, blk); |
| 780 | if (type == HTX_BLK_EOH) |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | return ret; |
| 785 | |
| 786 | err: |
| 787 | return 0; |
| 788 | } |
| 789 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 790 | /* Returns the total of bytes sent. */ |
| 791 | static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count) |
| 792 | { |
| 793 | struct buffer outbuf; |
| 794 | struct buffer *res; |
| 795 | size_t total = 0; |
| 796 | struct htx *htx; |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 797 | int bsize, fsize, hsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 798 | struct htx_blk *blk; |
| 799 | enum htx_blk_type type; |
| 800 | |
| 801 | htx = htx_from_buf(buf); |
| 802 | |
| 803 | new_frame: |
| 804 | if (!count || htx_is_empty(htx)) |
| 805 | goto end; |
| 806 | |
| 807 | blk = htx_get_head_blk(htx); |
| 808 | type = htx_get_blk_type(blk); |
| 809 | fsize = bsize = htx_get_blksz(blk); |
| 810 | |
| 811 | if (type != HTX_BLK_DATA) |
| 812 | goto end; |
| 813 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 814 | res = mux_get_buf(qcs); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 815 | |
| 816 | if (fsize > count) |
| 817 | fsize = count; |
| 818 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 819 | /* h3 DATA headers : 1-byte frame type + varint frame length */ |
| 820 | hsize = 1 + QUIC_VARINT_MAX_SIZE; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 821 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 822 | while (1) { |
| 823 | b_reset(&outbuf); |
| 824 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 825 | if (b_size(&outbuf) > hsize || !b_space_wraps(res)) |
| 826 | break; |
| 827 | b_slow_realign(res, trash.area, b_data(res)); |
| 828 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 829 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 830 | /* 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] | 831 | * stream. It is expected that the stream connector layer will subscribe |
| 832 | * on SEND. |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 833 | */ |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 834 | if (b_size(&outbuf) <= hsize) { |
| 835 | qcs->flags |= QC_SF_BLK_MROOM; |
| 836 | goto end; |
| 837 | } |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 838 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 839 | if (b_size(&outbuf) < hsize + fsize) |
| 840 | fsize = b_size(&outbuf) - hsize; |
| 841 | BUG_ON(fsize <= 0); |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 842 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 843 | b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */ |
| 844 | b_quic_enc_int(&outbuf, fsize); /* h3 frame length */ |
| 845 | |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 846 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 847 | total += fsize; |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 848 | count -= fsize; |
| 849 | |
| 850 | if (fsize == bsize) |
| 851 | htx_remove_blk(htx, blk); |
| 852 | else |
| 853 | htx_cut_data_blk(htx, blk, fsize); |
| 854 | |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 855 | /* commit the buffer */ |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 856 | b_add(res, b_data(&outbuf)); |
| 857 | goto new_frame; |
| 858 | |
| 859 | end: |
| 860 | return total; |
| 861 | } |
| 862 | |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 863 | 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] | 864 | { |
| 865 | size_t total = 0; |
Willy Tarreau | 3215e73 | 2022-05-27 10:09:11 +0200 | [diff] [blame] | 866 | struct qcs *qcs = __sc_mux_strm(sc); |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 867 | struct htx *htx; |
| 868 | enum htx_blk_type btype; |
| 869 | struct htx_blk *blk; |
| 870 | uint32_t bsize; |
| 871 | int32_t idx; |
| 872 | int ret; |
| 873 | |
Amaury Denoyelle | d8769d1 | 2022-03-25 15:28:33 +0100 | [diff] [blame] | 874 | h3_debug_printf(stderr, "%s\n", __func__); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 875 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 876 | htx = htx_from_buf(buf); |
| 877 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 878 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 879 | idx = htx_get_head(htx); |
| 880 | blk = htx_get_blk(htx, idx); |
| 881 | btype = htx_get_blk_type(blk); |
| 882 | bsize = htx_get_blksz(blk); |
| 883 | |
| 884 | /* Not implemented : QUIC on backend side */ |
| 885 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 886 | |
| 887 | switch (btype) { |
| 888 | case HTX_BLK_RES_SL: |
Amaury Denoyelle | 15b0961 | 2021-08-24 16:20:27 +0200 | [diff] [blame] | 889 | /* start-line -> HEADERS h3 frame */ |
| 890 | ret = h3_resp_headers_send(qcs, htx); |
| 891 | if (ret > 0) { |
| 892 | total += ret; |
| 893 | count -= ret; |
| 894 | if (ret < bsize) |
| 895 | goto out; |
| 896 | } |
| 897 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 898 | |
| 899 | case HTX_BLK_DATA: |
Amaury Denoyelle | 8e2a998 | 2021-08-24 16:24:37 +0200 | [diff] [blame] | 900 | ret = h3_resp_data_send(qcs, buf, count); |
| 901 | if (ret > 0) { |
| 902 | htx = htx_from_buf(buf); |
| 903 | total += ret; |
| 904 | count -= ret; |
| 905 | if (ret < bsize) |
| 906 | goto out; |
| 907 | } |
| 908 | break; |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 909 | |
| 910 | case HTX_BLK_TLR: |
| 911 | case HTX_BLK_EOT: |
| 912 | /* TODO trailers */ |
| 913 | |
| 914 | default: |
| 915 | htx_remove_blk(htx, blk); |
| 916 | total += bsize; |
| 917 | count -= bsize; |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 922 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 923 | qcs->flags |= QC_SF_FIN_STREAM; |
| 924 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 925 | out: |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 926 | if (total) { |
| 927 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 928 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 929 | } |
| 930 | |
Amaury Denoyelle | 26dfd90 | 2021-08-24 16:33:53 +0200 | [diff] [blame] | 931 | return total; |
Amaury Denoyelle | f52151d | 2021-08-24 16:11:18 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 934 | static int h3_attach(struct qcs *qcs) |
| 935 | { |
| 936 | struct h3s *h3s; |
| 937 | |
| 938 | h3s = pool_alloc(pool_head_h3s); |
| 939 | if (!h3s) |
| 940 | return 1; |
| 941 | |
| 942 | qcs->ctx = h3s; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 943 | h3s->demux_frame_len = 0; |
| 944 | h3s->demux_frame_type = 0; |
Amaury Denoyelle | 3555064 | 2022-05-24 15:14:53 +0200 | [diff] [blame] | 945 | h3s->flags = 0; |
Amaury Denoyelle | 48f01bd | 2022-04-27 15:37:20 +0200 | [diff] [blame] | 946 | |
Amaury Denoyelle | 3236a8e | 2022-05-24 15:24:03 +0200 | [diff] [blame] | 947 | if (quic_stream_is_bidi(qcs->id)) { |
| 948 | h3s->type = H3S_T_REQ; |
| 949 | } |
| 950 | else { |
| 951 | /* stream type must be decoded for unidirectional streams */ |
| 952 | h3s->type = H3S_T_UNKNOWN; |
| 953 | } |
| 954 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 958 | static void h3_detach(struct qcs *qcs) |
| 959 | { |
| 960 | struct h3s *h3s = qcs->ctx; |
| 961 | pool_free(pool_head_h3s, h3s); |
| 962 | qcs->ctx = NULL; |
| 963 | } |
| 964 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 965 | static int h3_finalize(void *ctx) |
| 966 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 967 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 968 | struct qcs *qcs; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 969 | |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 970 | qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI); |
| 971 | if (!qcs) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 972 | return 0; |
| 973 | |
Amaury Denoyelle | 9cc4751 | 2022-05-24 16:27:41 +0200 | [diff] [blame] | 974 | h3_control_send(qcs, h3c); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 975 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 976 | return 1; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 979 | /* Initialize the HTTP/3 context for <qcc> mux. |
| 980 | * Return 1 if succeeded, 0 if not. |
| 981 | */ |
| 982 | static int h3_init(struct qcc *qcc) |
| 983 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 984 | struct h3c *h3c; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 985 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 986 | h3c = pool_alloc(pool_head_h3c); |
| 987 | if (!h3c) |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 988 | goto fail_no_h3; |
| 989 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 990 | h3c->qcc = qcc; |
| 991 | h3c->err = H3_NO_ERROR; |
| 992 | h3c->flags = 0; |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 993 | |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 994 | qcc->ctx = h3c; |
| 995 | LIST_INIT(&h3c->buf_wait.list); |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 996 | |
| 997 | return 1; |
| 998 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 999 | fail_no_h3: |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1003 | static void h3_release(void *ctx) |
| 1004 | { |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1005 | struct h3c *h3c = ctx; |
Amaury Denoyelle | 8d1ecac | 2022-05-24 14:55:43 +0200 | [diff] [blame] | 1006 | pool_free(pool_head_h3c, h3c); |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1009 | /* Check if the H3 connection can still be considered as active. |
| 1010 | * |
| 1011 | * Return true if active else false. |
| 1012 | */ |
| 1013 | static int h3_is_active(const struct qcc *qcc, void *ctx) |
| 1014 | { |
| 1015 | if (qcc->strms[QCS_CLT_BIDI].nb_streams) |
| 1016 | return 1; |
| 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1021 | /* HTTP/3 application layer operations */ |
| 1022 | const struct qcc_app_ops h3_ops = { |
| 1023 | .init = h3_init, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1024 | .attach = h3_attach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1025 | .decode_qcs = h3_decode_qcs, |
Amaury Denoyelle | abbe91e | 2021-11-12 16:09:29 +0100 | [diff] [blame] | 1026 | .snd_buf = h3_snd_buf, |
Amaury Denoyelle | 67e92d3 | 2022-04-27 18:04:01 +0200 | [diff] [blame] | 1027 | .detach = h3_detach, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1028 | .finalize = h3_finalize, |
Amaury Denoyelle | 198d35f | 2022-04-01 17:56:58 +0200 | [diff] [blame] | 1029 | .is_active = h3_is_active, |
Amaury Denoyelle | 8347f27 | 2022-03-29 14:46:55 +0200 | [diff] [blame] | 1030 | .release = h3_release, |
Frédéric Lécaille | ccac11f | 2021-03-03 16:09:02 +0100 | [diff] [blame] | 1031 | }; |