blob: d52566cb7155335a88d272e20a0dc443e9ecc35b [file] [log] [blame]
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001/*
2 * HTTP/3 protocol processing
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.1
7 * exclusively.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020019#include <import/ist.h>
20
21#include <haproxy/api.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010022#include <haproxy/buf.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020023#include <haproxy/chunk.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020024#include <haproxy/connection.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010025#include <haproxy/dynbuf.h>
26#include <haproxy/h3.h>
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +020027#include <haproxy/h3_stats.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020028#include <haproxy/http.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020029#include <haproxy/http-hdr-t.h>
Amaury Denoyelle115ccce2022-08-17 18:02:47 +020030#include <haproxy/http_htx.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020031#include <haproxy/htx.h>
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +020032#include <haproxy/intops.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010033#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020034#include <haproxy/mux_quic.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010035#include <haproxy/pool.h>
Amaury Denoyelle381d8132023-02-17 09:51:20 +010036#include <haproxy/qmux_http.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010037#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020038#include <haproxy/qpack-enc.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020039#include <haproxy/quic_conn-t.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020040#include <haproxy/quic_enc.h>
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +020041#include <haproxy/stats-t.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010042#include <haproxy/tools.h>
Amaury Denoyelle016aa932022-05-30 15:49:36 +020043#include <haproxy/trace.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010044
Amaury Denoyelle016aa932022-05-30 15:49:36 +020045/* trace source and events */
46static void h3_trace(enum trace_level level, uint64_t mask,
47 const struct trace_source *src,
48 const struct ist where, const struct ist func,
49 const void *a1, const void *a2, const void *a3, const void *a4);
50
51static const struct trace_event h3_trace_events[] = {
Amaury Denoyelle494512d2022-05-30 15:50:34 +020052#define H3_EV_RX_FRAME (1ULL << 0)
53 { .mask = H3_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H3 frame" },
54#define H3_EV_RX_DATA (1ULL << 1)
55 { .mask = H3_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H3 DATA frame" },
56#define H3_EV_RX_HDR (1ULL << 2)
57 { .mask = H3_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H3 HEADERS frame" },
58#define H3_EV_RX_SETTINGS (1ULL << 3)
59 { .mask = H3_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H3 SETTINGS frame" },
Amaury Denoyellea717eb72022-05-30 15:51:01 +020060#define H3_EV_TX_DATA (1ULL << 4)
61 { .mask = H3_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H3 DATA frame" },
62#define H3_EV_TX_HDR (1ULL << 5)
63 { .mask = H3_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H3 HEADERS frame" },
64#define H3_EV_TX_SETTINGS (1ULL << 6)
65 { .mask = H3_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H3 SETTINGS frame" },
Amaury Denoyelled5581d52022-05-30 15:51:31 +020066#define H3_EV_H3S_NEW (1ULL << 7)
67 { .mask = H3_EV_H3S_NEW, .name = "h3s_new", .desc = "new H3 stream" },
68#define H3_EV_H3S_END (1ULL << 8)
69 { .mask = H3_EV_H3S_END, .name = "h3s_end", .desc = "H3 stream terminated" },
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +010070#define H3_EV_H3C_END (1ULL << 9)
71 { .mask = H3_EV_H3C_END, .name = "h3c_end", .desc = "H3 connection terminated" },
Amaury Denoyelle016aa932022-05-30 15:49:36 +020072 { }
73};
74
75static const struct name_desc h3_trace_lockon_args[4] = {
76 /* arg1 */ { /* already used by the connection */ },
77 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
78 /* arg3 */ { },
79 /* arg4 */ { }
80};
81
82static const struct name_desc h3_trace_decoding[] = {
83#define H3_VERB_CLEAN 1
84 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
85#define H3_VERB_MINIMAL 2
86 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
87 { /* end */ }
88};
89
90struct trace_source trace_h3 = {
91 .name = IST("h3"),
92 .desc = "HTTP/3 transcoder",
93 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
94 .default_cb = h3_trace,
95 .known_events = h3_trace_events,
96 .lockon_args = h3_trace_lockon_args,
97 .decoding = h3_trace_decoding,
98 .report_events = ~0, /* report everything by default */
99};
100
101#define TRACE_SOURCE &trace_h3
102INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
103
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100104#if defined(DEBUG_H3)
105#define h3_debug_printf fprintf
106#define h3_debug_hexdump debug_hexdump
107#else
108#define h3_debug_printf(...) do { } while (0)
109#define h3_debug_hexdump(...) do { } while (0)
110#endif
111
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200112#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
113#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
114#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
115#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
116#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100117#define H3_CF_GOAWAY_SENT 0x00000020 /* GOAWAY sent on local control stream */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100118
119/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +0200120static uint64_t h3_settings_qpack_max_table_capacity = 0;
121static uint64_t h3_settings_qpack_blocked_streams = 4096;
122static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100123
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200124struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100125 struct qcc *qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +0200126 struct qcs *ctrl_strm; /* Control stream */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100127 enum h3_err err;
128 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200129
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100130 /* Settings */
131 uint64_t qpack_max_table_capacity;
132 uint64_t qpack_blocked_streams;
133 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200134
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200135 uint64_t id_goaway; /* stream ID used for a GOAWAY frame */
136
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100137 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200138 /* Stats counters */
139 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100140};
141
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200142DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100143
Amaury Denoyelle35550642022-05-24 15:14:53 +0200144#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200145#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100146#define H3_SF_HAVE_CLEN 0x00000004 /* content-length header is present */
Amaury Denoyelle35550642022-05-24 15:14:53 +0200147
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200148struct h3s {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200149 struct h3c *h3c;
150
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200151 enum h3s_t type;
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200152 enum h3s_st_req st_req; /* only used for request streams */
Amaury Denoyelle35d90532023-01-26 16:03:45 +0100153 uint64_t demux_frame_len;
154 uint64_t demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200155
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100156 unsigned long long body_len; /* known request body length from content-length header if present */
157 unsigned long long data_len; /* total length of all parsed DATA */
158
Amaury Denoyelle35550642022-05-24 15:14:53 +0200159 int flags;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100160 int err; /* used for stream reset */
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200161};
162
163DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
164
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200165/* Initialize an uni-stream <qcs> by reading its type from <b>.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200166 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200167 * Returns the count of consumed bytes or a negative error code.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200168 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200169static ssize_t h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
170 struct buffer *b)
Amaury Denoyelle35550642022-05-24 15:14:53 +0200171{
172 /* decode unidirectional stream type */
173 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200174 uint64_t type;
175 size_t len = 0, ret;
176
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200177 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
178
Amaury Denoyelle35550642022-05-24 15:14:53 +0200179 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
180 h3s->flags & H3_SF_UNI_INIT);
181
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200182 ret = b_quic_dec_int(&type, b, &len);
Amaury Denoyelle35550642022-05-24 15:14:53 +0200183 if (!ret) {
184 ABORT_NOW();
185 }
186
187 switch (type) {
188 case H3_UNI_S_T_CTRL:
189 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100190 TRACE_ERROR("duplicated control stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +0200191 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100192 goto err;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200193 }
194 h3c->flags |= H3_CF_UNI_CTRL_SET;
195 h3s->type = H3S_T_CTRL;
196 break;
197
198 case H3_UNI_S_T_PUSH:
199 /* TODO not supported for the moment */
200 h3s->type = H3S_T_PUSH;
201 break;
202
203 case H3_UNI_S_T_QPACK_DEC:
204 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100205 TRACE_ERROR("duplicated qpack decoder stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +0200206 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100207 goto err;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200208 }
209 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
210 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200211 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200212 break;
213
214 case H3_UNI_S_T_QPACK_ENC:
215 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100216 TRACE_ERROR("duplicated qpack encoder stream", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +0200217 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100218 goto err;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200219 }
220 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
221 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200222 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200223 break;
224
225 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200226 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
227 *
228 * Implementations MUST [...] abort reading on unidirectional
229 * streams that have unknown or unsupported types.
230 */
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100231 TRACE_STATE("abort reading on unknown uni stream type", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100232 qcc_abort_stream_read(qcs);
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100233 goto err;
234 }
Amaury Denoyelle35550642022-05-24 15:14:53 +0200235
236 h3s->flags |= H3_SF_UNI_INIT;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200237
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200238 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200239 return len;
Amaury Denoyelle815c8ce2023-03-08 10:25:39 +0100240
241 err:
242 TRACE_DEVEL("leaving on error", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
243 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200244}
245
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200246/* Parse a buffer <b> for a <qcs> uni-stream which does not contains H3 frames.
247 * This may be used for QPACK encoder/decoder streams for example. <fin> is set
248 * if this is the last frame of the stream.
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200249 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200250 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200251 */
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200252static ssize_t h3_parse_uni_stream_no_h3(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200253{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200254 struct h3s *h3s = qcs->ctx;
255
256 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
257 !(h3s->flags & H3_SF_UNI_NO_H3));
258
259 switch (h3s->type) {
260 case H3S_T_QPACK_DEC:
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200261 if (qpack_decode_dec(b, fin, qcs))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200262 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200263 break;
264 case H3S_T_QPACK_ENC:
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200265 if (qpack_decode_enc(b, fin, qcs))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200266 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200267 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200268 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200269 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200270 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
271 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200272 }
273
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200274 /* TODO adjust return code */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200275 return 0;
276}
277
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200278/* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in
279 * <ftype> and length in <flen>.
280 *
281 * Returns the size of the H3 frame header. Note that the input buffer is not
282 * consumed.
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100283 */
284static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200285 struct buffer *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100286{
287 size_t hlen;
288
289 hlen = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200290 if (!b_quic_dec_int(ftype, b, &hlen) ||
291 !b_quic_dec_int(flen, b, &hlen)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100292 return 0;
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200293 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100294
295 return hlen;
296}
297
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200298/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
299 *
300 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
301 * be reported.
302 */
303static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
304{
305 struct h3s *h3s = qcs->ctx;
306 const uint64_t id = qcs->id;
307
308 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
309
310 switch (ftype) {
311 case H3_FT_DATA:
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200312 return h3s->type != H3S_T_CTRL && (h3s->st_req == H3S_ST_REQ_HEADERS ||
313 h3s->st_req == H3S_ST_REQ_DATA);
314
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200315 case H3_FT_HEADERS:
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200316 return h3s->type != H3S_T_CTRL && h3s->st_req != H3S_ST_REQ_TRAILERS;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200317
318 case H3_FT_CANCEL_PUSH:
319 case H3_FT_GOAWAY:
320 case H3_FT_MAX_PUSH_ID:
321 /* Only allowed for control stream. First frame of control
322 * stream MUST be SETTINGS.
323 */
324 return h3s->type == H3S_T_CTRL &&
325 (h3c->flags & H3_CF_SETTINGS_RECV);
326
327 case H3_FT_SETTINGS:
328 /* draft-ietf-quic-http34 7.2.4. SETTINGS
329 *
330 * If an endpoint receives a second SETTINGS frame on the control
331 * stream, the endpoint MUST respond with a connection error of type
332 * H3_FRAME_UNEXPECTED.
333 */
334 return h3s->type == H3S_T_CTRL &&
335 !(h3c->flags & H3_CF_SETTINGS_RECV);
336
337 case H3_FT_PUSH_PROMISE:
338 return h3s->type != H3S_T_CTRL &&
339 (id & QCS_ID_SRV_INTIATOR_BIT);
340
341 default:
342 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
343 *
344 * Implementations MUST discard frames [...] that have unknown
345 * or unsupported types.
346 */
347 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
348 }
349}
350
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100351/* Check from stream <qcs> that length of all DATA frames does not exceed with
352 * a previously parsed content-length header. <fin> must be set for the last
353 * data of the stream so that length of DATA frames must be equal to the
354 * content-length.
355 *
356 * This must only be called for a stream with H3_SF_HAVE_CLEN flag.
357 *
358 * Return 0 on valid else non-zero.
359 */
360static int h3_check_body_size(struct qcs *qcs, int fin)
361{
362 struct h3s *h3s = qcs->ctx;
363 int ret = 0;
364 TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
365
366 /* Reserved for streams with a previously parsed content-length header. */
367 BUG_ON(!(h3s->flags & H3_SF_HAVE_CLEN));
368
369 /* RFC 9114 4.1.2. Malformed Requests and Responses
370 *
371 * A request or response that is defined as having content when it
372 * contains a Content-Length header field (Section 8.6 of [HTTP]) is
373 * malformed if the value of the Content-Length header field does not
374 * equal the sum of the DATA frame lengths received.
375 *
376 * TODO for backend support
377 * A response that is
378 * defined as never having content, even when a Content-Length is
379 * present, can have a non-zero Content-Length header field even though
380 * no content is included in DATA frames.
381 */
382 if (h3s->data_len > h3s->body_len ||
383 (fin && h3s->data_len < h3s->body_len)) {
384 TRACE_ERROR("Content-length does not match DATA frame size", H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100385 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100386 ret = -1;
387 }
388
389 TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
390 return ret;
391}
392
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100393/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200394 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100395 * set if this is the last data to transfer from this stream.
396 *
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100397 * Returns the number of consumed bytes or a negative error code. On error
398 * either the connection should be closed or the stream reset using codes
399 * provided in h3c.err / h3s.err.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100400 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200401static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
402 uint64_t len, char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100403{
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200404 struct h3s *h3s = qcs->ctx;
405 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100406 struct buffer htx_buf = BUF_NULL;
407 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100408 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200409 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200410 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200411 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100412 struct ist meth = IST_NULL, path = IST_NULL;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100413 struct ist scheme = IST_NULL, authority = IST_NULL;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200414 int hdr_idx, ret;
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100415 int cookie = -1, last_cookie = -1, i;
416
417 /* RFC 9114 4.1.2. Malformed Requests and Responses
418 *
419 * A malformed request or response is one that is an otherwise valid
420 * sequence of frames but is invalid due to:
421 * - the presence of prohibited fields or pseudo-header fields,
422 * - the absence of mandatory pseudo-header fields,
423 * - invalid values for pseudo-header fields,
424 * - pseudo-header fields after fields,
425 * - an invalid sequence of HTTP messages,
426 * - the inclusion of uppercase field names, or
427 * - the inclusion of invalid characters in field names or values.
428 *
429 * [...]
430 *
431 * Intermediaries that process HTTP requests or responses (i.e., any
432 * intermediary not acting as a tunnel) MUST NOT forward a malformed
433 * request or response. Malformed requests or responses that are
434 * detected MUST be treated as a stream error of type H3_MESSAGE_ERROR.
435 */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100436
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200437 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
438
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200439 /* TODO support trailer parsing in this function */
440
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200441 /* TODO support buffer wrapping */
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200442 BUG_ON(b_head(buf) + len >= b_wrap(buf));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200443 ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp,
444 list, sizeof(list) / sizeof(list[0]));
445 if (ret < 0) {
446 TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
447 h3c->err = -ret;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100448 len = -1;
449 goto out;
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200450 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100451
452 qc_get_buf(qcs, &htx_buf);
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100453 BUG_ON(!b_size(&htx_buf)); /* TODO */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100454 htx = htx_from_buf(&htx_buf);
455
456 /* first treat pseudo-header to build the start line */
457 hdr_idx = 0;
458 while (1) {
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100459 /* RFC 9114 4.3. HTTP Control Data
460 *
461 * Endpoints MUST treat a request or response that contains
462 * undefined or invalid pseudo-header fields as malformed.
463 *
464 * All pseudo-header fields MUST appear in the header section before
465 * regular header fields. Any request or response that contains a
466 * pseudo-header field that appears in a header section after a regular
467 * header field MUST be treated as malformed.
468 */
469
470 /* Stop at first non pseudo-header. */
471 if (!istmatch(list[hdr_idx].n, ist(":")))
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100472 break;
473
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100474 /* pseudo-header. Malformed name with uppercase character or
475 * invalid token will be rejected in the else clause.
476 */
477 if (isteq(list[hdr_idx].n, ist(":method"))) {
478 if (isttest(meth)) {
479 TRACE_ERROR("duplicated method pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100480 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100481 len = -1;
482 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100483 }
484 meth = list[hdr_idx].v;
485 }
486 else if (isteq(list[hdr_idx].n, ist(":path"))) {
487 if (isttest(path)) {
488 TRACE_ERROR("duplicated path pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100489 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100490 len = -1;
491 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100492 }
493 path = list[hdr_idx].v;
494 }
495 else if (isteq(list[hdr_idx].n, ist(":scheme"))) {
496 if (isttest(scheme)) {
497 /* duplicated pseudo-header */
498 TRACE_ERROR("duplicated scheme pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100499 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100500 len = -1;
501 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100502 }
503 scheme = list[hdr_idx].v;
504 }
505 else if (isteq(list[hdr_idx].n, ist(":authority"))) {
506 if (isttest(authority)) {
507 TRACE_ERROR("duplicated authority pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100508 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100509 len = -1;
510 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100511 }
512 authority = list[hdr_idx].v;
513 }
514 else {
515 TRACE_ERROR("unknown pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100516 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100517 len = -1;
518 goto out;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100519 }
520
521 ++hdr_idx;
522 }
523
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100524 if (!istmatch(meth, ist("CONNECT"))) {
525 /* RFC 9114 4.3.1. Request Pseudo-Header Fields
526 *
527 * All HTTP/3 requests MUST include exactly one value for the :method,
528 * :scheme, and :path pseudo-header fields, unless the request is a
529 * CONNECT request; see Section 4.4.
530 */
531 if (!isttest(meth) || !isttest(scheme) || !isttest(path)) {
532 TRACE_ERROR("missing mandatory pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100533 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100534 len = -1;
535 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100536 }
537 }
538
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100539 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200540 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100541
542 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200543 if (!sl) {
544 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100545 len = -1;
546 goto out;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200547 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100548
549 if (fin)
550 sl->flags |= HTX_SL_F_BODYLESS;
551
552 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100553
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100554 if (isttest(authority)) {
555 if (!htx_add_header(htx, ist("host"), authority)) {
556 h3c->err = H3_INTERNAL_ERROR;
557 len = -1;
558 goto out;
559 }
560 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100561
562 /* now treat standard headers */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100563 while (1) {
564 if (isteq(list[hdr_idx].n, ist("")))
565 break;
566
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100567 if (istmatch(list[hdr_idx].n, ist(":"))) {
568 TRACE_ERROR("pseudo-header field after fields", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100569 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100570 len = -1;
571 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100572 }
573
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100574 for (i = 0; i < list[hdr_idx].n.len; ++i) {
575 const char c = list[hdr_idx].n.ptr[i];
576 if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) {
577 TRACE_ERROR("invalid characters in field name", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100578 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100579 len = -1;
580 goto out;
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100581 }
582 }
583
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200584 if (isteq(list[hdr_idx].n, ist("cookie"))) {
585 http_cookie_register(list, hdr_idx, &cookie, &last_cookie);
Amaury Denoyelle19942e32022-12-15 09:18:25 +0100586 ++hdr_idx;
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200587 continue;
588 }
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100589 else if (isteq(list[hdr_idx].n, ist("content-length"))) {
590 ret = http_parse_cont_len_header(&list[hdr_idx].v,
591 &h3s->body_len,
592 h3s->flags & H3_SF_HAVE_CLEN);
593 if (ret < 0) {
594 TRACE_ERROR("invalid content-length", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100595 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100596 len = -1;
597 goto out;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100598 }
599 else if (!ret) {
600 /* Skip duplicated value. */
601 ++hdr_idx;
602 continue;
603 }
604
605 h3s->flags |= H3_SF_HAVE_CLEN;
606 /* This will fail if current frame is the last one and
607 * content-length is not null.
608 */
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100609 if (h3_check_body_size(qcs, fin)) {
610 len = -1;
611 goto out;
612 }
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100613 }
Amaury Denoyelle8ad26692023-01-17 17:47:06 +0100614 else if (isteq(list[hdr_idx].n, ist("connection")) ||
615 isteq(list[hdr_idx].n, ist("proxy-connection")) ||
616 isteq(list[hdr_idx].n, ist("keep-alive")) ||
617 isteq(list[hdr_idx].n, ist("transfer-encoding"))) {
618 /* RFC 9114 4.2. HTTP Fields
619 *
620 * HTTP/3 does not use the Connection header field to indicate
621 * connection-specific fields; in this protocol, connection-
622 * specific metadata is conveyed by other means. An endpoint
623 * MUST NOT generate an HTTP/3 field section containing
624 * connection-specific fields; any message containing
625 * connection-specific fields MUST be treated as malformed.
626 */
627 TRACE_ERROR("invalid connection header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
628 h3s->err = H3_MESSAGE_ERROR;
629 len = -1;
630 goto out;
631 }
632 else if (isteq(list[hdr_idx].n, ist("te")) &&
633 !isteq(list[hdr_idx].v, ist("trailers"))) {
634 /* RFC 9114 4.2. HTTP Fields
635 *
636 * The only exception to this is the TE header field, which MAY
637 * be present in an HTTP/3 request header; when it is, it MUST
638 * NOT contain any value other than "trailers".
639 */
640 TRACE_ERROR("invalid te header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
641 h3s->err = H3_MESSAGE_ERROR;
642 len = -1;
643 goto out;
644 }
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200645
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100646 if (!htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v)) {
647 h3c->err = H3_INTERNAL_ERROR;
648 len = -1;
649 goto out;
650 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100651 ++hdr_idx;
652 }
653
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200654 if (cookie >= 0) {
655 if (http_cookie_merge(htx, list, cookie)) {
656 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100657 len = -1;
658 goto out;
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200659 }
660 }
661
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100662 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
663 h3c->err = H3_INTERNAL_ERROR;
664 len = -1;
665 goto out;
666 }
667
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100668 if (fin)
669 htx->flags |= HTX_FL_EOM;
670
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100671 htx_to_buf(htx, &htx_buf);
672 htx = NULL;
673
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200674 if (!qc_attach_sc(qcs, &htx_buf)) {
675 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100676 len = -1;
677 goto out;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200678 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100679
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200680 /* RFC 9114 5.2. Connection Shutdown
681 *
682 * The GOAWAY frame contains an identifier that
683 * indicates to the receiver the range of requests or pushes that were
684 * or might be processed in this connection. The server sends a client-
685 * initiated bidirectional stream ID; the client sends a push ID.
686 * Requests or pushes with the indicated identifier or greater are
687 * rejected (Section 4.1.1) by the sender of the GOAWAY. This
688 * identifier MAY be zero if no requests or pushes were processed.
689 */
690 if (qcs->id >= h3c->id_goaway)
691 h3c->id_goaway = qcs->id + 4;
692
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100693 out:
694 /* HTX may be non NULL if error before previous htx_to_buf(). */
695 if (htx)
696 htx_to_buf(htx, &htx_buf);
697
Willy Tarreau4596fe22022-05-17 19:07:51 +0200698 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100699 * except on stream creation error.
700 */
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100701 if (b_size(&htx_buf)) {
702 b_free(&htx_buf);
703 offer_buffers(NULL, 1);
704 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100705
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200706 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200707 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100708}
709
Amaury Denoyelleedfcb552023-01-13 16:40:31 +0100710/* Parse from buffer <buf> a H3 HEADERS frame of length <len> used as trailers.
711 * Data are copied in a local HTX buffer and transfer to the stream connector
712 * layer. <fin> must be set if this is the last data to transfer from this
713 * stream.
714 *
715 * Returns the number of consumed bytes or a negative error code. On error
716 * either the connection should be closed or the stream reset using codes
717 * provided in h3c.err / h3s.err.
718 */
719static ssize_t h3_trailers_to_htx(struct qcs *qcs, const struct buffer *buf,
720 uint64_t len, char fin)
721{
722 struct h3s *h3s = qcs->ctx;
723 struct h3c *h3c = h3s->h3c;
724 struct buffer htx_buf = BUF_NULL;
725 struct buffer *tmp = get_trash_chunk();
726 struct htx *htx = NULL;
727 struct htx_sl *sl;
728 struct http_hdr list[global.tune.max_http_hdr];
729 int hdr_idx, ret;
730 int i;
731
732 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
733
734 /* TODO support buffer wrapping */
735 BUG_ON(b_head(buf) + len >= b_wrap(buf));
736 ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp,
737 list, sizeof(list) / sizeof(list[0]));
738 if (ret < 0) {
739 TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
740 h3c->err = -ret;
741 len = -1;
742 goto out;
743 }
744
745 qc_get_buf(qcs, &htx_buf);
746 BUG_ON(!b_size(&htx_buf)); /* TODO */
747 htx = htx_from_buf(&htx_buf);
748
749 if (!h3s->data_len) {
750 /* Notify that no body is present. This can only happens if
751 * there is H3 HEADERS as trailers without or empty H3 DATA
752 * frame. So this is probably not realistice ?
753 *
754 * TODO if sl is NULL because already consumed there is no way
755 * to notify about missing body.
756 */
757 sl = http_get_stline(htx);
758 if (sl)
759 sl->flags |= HTX_SL_F_BODYLESS;
760 else
761 TRACE_ERROR("cannot notify missing body after trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
762 }
763
764 hdr_idx = 0;
765 while (1) {
766 if (isteq(list[hdr_idx].n, ist("")))
767 break;
768
769 /* RFC 9114 4.3. HTTP Control Data
770 *
771 * Pseudo-header
772 * fields MUST NOT appear in trailer sections.
773 */
774 if (istmatch(list[hdr_idx].n, ist(":"))) {
775 TRACE_ERROR("pseudo-header field in trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
776 h3s->err = H3_MESSAGE_ERROR;
777 len = -1;
778 goto out;
779 }
780
781 for (i = 0; i < list[hdr_idx].n.len; ++i) {
782 const char c = list[hdr_idx].n.ptr[i];
783 if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) {
784 TRACE_ERROR("invalid characters in field name", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
785 h3s->err = H3_MESSAGE_ERROR;
786 len = -1;
787 goto out;
788 }
789 }
790
791 /* forbidden HTTP/3 headers, cf h3_headers_to_htx() */
792 if (isteq(list[hdr_idx].n, ist("host")) ||
793 isteq(list[hdr_idx].n, ist("content-length")) ||
794 isteq(list[hdr_idx].n, ist("connection")) ||
795 isteq(list[hdr_idx].n, ist("proxy-connection")) ||
796 isteq(list[hdr_idx].n, ist("keep-alive")) ||
797 isteq(list[hdr_idx].n, ist("te")) ||
798 isteq(list[hdr_idx].n, ist("transfer-encoding"))) {
799 TRACE_ERROR("forbidden HTTP/3 headers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
800 h3s->err = H3_MESSAGE_ERROR;
801 len = -1;
802 goto out;
803 }
804
805 if (!htx_add_trailer(htx, list[hdr_idx].n, list[hdr_idx].v)) {
806 TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
807 h3c->err = H3_INTERNAL_ERROR;
808 len = -1;
809 goto out;
810 }
811
812 ++hdr_idx;
813 }
814
815 if (!htx_add_endof(htx, HTX_BLK_EOT)) {
816 TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
817 h3c->err = H3_INTERNAL_ERROR;
818 len = -1;
819 goto out;
820 }
821
822 if (fin)
823 htx->flags |= HTX_FL_EOM;
824
825 htx_to_buf(htx, &htx_buf);
826 htx = NULL;
827
828 out:
829 /* HTX may be non NULL if error before previous htx_to_buf(). */
830 if (htx)
831 htx_to_buf(htx, &htx_buf);
832
833 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
834 return len;
835}
836
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100837/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
838 * HTX buffer. <fin> must be set if this is the last data to transfer from this
839 * stream.
840 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200841 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100842 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200843static ssize_t h3_data_to_htx(struct qcs *qcs, const struct buffer *buf,
844 uint64_t len, char fin)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100845{
846 struct buffer *appbuf;
847 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200848 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100849 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200850 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100851
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200852 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
853
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100854 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
855 BUG_ON(!appbuf);
856 htx = htx_from_buf(appbuf);
857
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200858 if (len > b_data(buf)) {
859 len = b_data(buf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200860 fin = 0;
861 }
862
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200863 head = b_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200864 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100865 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200866 if (!htx_space) {
867 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200868 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200869 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200870
871 if (len > htx_space) {
872 len = htx_space;
873 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100874 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100875
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200876 if (head + len > b_wrap(buf)) {
877 size_t contig = b_wrap(buf) - head;
878 htx_sent = htx_add_data(htx, ist2(b_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200879 if (htx_sent < contig) {
880 qcs->flags |= QC_SF_DEM_FULL;
881 goto out;
882 }
883
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200884 len -= contig;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200885 head = b_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200886 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100887 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100888
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200889 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200890 if (htx_sent < len) {
891 qcs->flags |= QC_SF_DEM_FULL;
892 goto out;
893 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200894
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200895 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100896 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100897
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200898 out:
899 htx_to_buf(htx, appbuf);
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200900
901 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200902 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100903}
904
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200905/* Parse a SETTINGS frame of length <len> of payload <buf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200906 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200907 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200908 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200909static ssize_t h3_parse_settings_frm(struct h3c *h3c, const struct buffer *buf,
910 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200911{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200912 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200913 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200914 size_t ret = 0;
915 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200916
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200917 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
918
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200919 /* Work on a copy of <buf>. */
Amaury Denoyelle3a2fcfd2022-06-09 11:54:38 +0200920 b = b_make(b_orig(buf), b_size(buf), b_head_ofs(buf), len);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200921
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200922 while (b_data(&b)) {
923 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
924 h3c->err = H3_FRAME_ERROR;
925 return -1;
926 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200927
928 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
929 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200930
931 /* draft-ietf-quic-http34 7.2.4. SETTINGS
932 *
933 * The same setting identifier MUST NOT occur more than once in the
934 * SETTINGS frame. A receiver MAY treat the presence of duplicate
935 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
936 */
937
938 /* Ignore duplicate check for ID too big used for GREASE. */
939 if (id < sizeof(mask)) {
940 if (ha_bit_test(id, &mask)) {
941 h3c->err = H3_SETTINGS_ERROR;
942 return -1;
943 }
944 ha_bit_set(id, &mask);
945 }
946
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200947 switch (id) {
948 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
949 h3c->qpack_max_table_capacity = value;
950 break;
951 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
952 h3c->max_field_section_size = value;
953 break;
954 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
955 h3c->qpack_blocked_streams = value;
956 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200957
958 case H3_SETTINGS_RESERVED_0:
959 case H3_SETTINGS_RESERVED_2:
960 case H3_SETTINGS_RESERVED_3:
961 case H3_SETTINGS_RESERVED_4:
962 case H3_SETTINGS_RESERVED_5:
963 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
964 *
965 * Setting identifiers which were defined in [HTTP2] where there is no
966 * corresponding HTTP/3 setting have also been reserved
967 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
968 * their receipt MUST be treated as a connection error of type
969 * H3_SETTINGS_ERROR.
970 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200971 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200972 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200973 default:
974 /* MUST be ignored */
975 break;
976 }
977 }
978
Frédéric Lécaillebefcf702022-09-08 16:04:55 +0200979 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200980 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200981}
982
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100983/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
984 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200985 *
986 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100987 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200988static ssize_t h3_decode_qcs(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100989{
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200990 struct h3s *h3s = qcs->ctx;
Amaury Denoyellec0156792022-06-03 15:29:07 +0200991 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200992 ssize_t total = 0, ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100993
Amaury Denoyelle14037bf2023-02-17 15:56:06 +0100994 TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100995
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200996 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +0100997 if ((ret = h3_init_uni_stream(h3c, qcs, b)) < 0) {
998 TRACE_ERROR("cannot initialize uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
999 goto err;
1000 }
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001001
1002 total += ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001003 }
1004
1005 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
1006 /* For non-h3 STREAM, parse it and return immediately. */
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001007 if ((ret = h3_parse_uni_stream_no_h3(qcs, b, fin)) < 0) {
1008 TRACE_ERROR("error when parsing non-HTTP3 uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
1009 goto err;
1010 }
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001011
1012 total += ret;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001013 goto done;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001014 }
1015
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001016 /* RFC 9114 6.2.1. Control Streams
1017 *
1018 * The sender MUST NOT close the control stream, and the receiver MUST NOT
1019 * request that the sender close the control stream. If either control
1020 * stream is closed at any point, this MUST be treated as a connection
1021 * error of type H3_CLOSED_CRITICAL_STREAM.
1022 */
1023 if (h3s->type == H3S_T_CTRL && fin) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001024 TRACE_ERROR("control stream closed by remote peer", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001025 qcc_emit_cc_app(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001026 goto err;
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001027 }
1028
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001029 if (!b_data(b) && fin && quic_stream_is_bidi(qcs->id)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001030 TRACE_PROTO("received FIN without data", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001031 qcs_http_handle_standalone_fin(qcs);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001032 goto done;
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001033 }
1034
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001035 while (b_data(b) && !(qcs->flags & QC_SF_DEM_FULL) && !h3c->err && !h3s->err) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001036 uint64_t ftype, flen;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +01001037 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001038
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001039 if (!h3s->demux_frame_len) {
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001040 /* Switch to a new frame. */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001041 size_t hlen = h3_decode_frm_header(&ftype, &flen, b);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001042 if (!hlen) {
1043 TRACE_PROTO("pause parsing on incomplete frame header", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001044 break;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001045 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001046
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001047 h3s->demux_frame_type = ftype;
1048 h3s->demux_frame_len = flen;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001049 total += hlen;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001050 TRACE_PROTO("parsing a new frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001051
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001052 /* Check that content-length is not exceeded on a new DATA frame. */
1053 if (ftype == H3_FT_DATA) {
1054 h3s->data_len += flen;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001055 if (h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, fin))
1056 break;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001057 }
1058
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001059 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001060 TRACE_ERROR("received an invalid frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001061 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001062 goto err;
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001063 }
1064
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001065 if (!b_data(b))
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001066 break;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001067 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +01001068
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001069 flen = h3s->demux_frame_len;
1070 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001071
1072 /* Do not demux incomplete frames except H3 DATA which can be
1073 * fragmented in multiple HTX blocks.
1074 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001075 if (flen > b_data(b) && ftype != H3_FT_DATA) {
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001076 /* Reject frames bigger than bufsize.
1077 *
1078 * TODO HEADERS should in complement be limited with H3
1079 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
1080 * excessive decompressed size.
1081 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001082 if (flen > QC_S_RX_BUF_SZ) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001083 TRACE_ERROR("received a too big frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001084 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001085 goto err;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001086 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001087 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +02001088 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001089
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001090 /* Check content-length equality with DATA frames length on the last frame. */
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001091 if (fin && h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, fin))
1092 break;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001093
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001094 last_stream_frame = (fin && flen == b_data(b));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +01001095
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001096 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001097 switch (ftype) {
1098 case H3_FT_DATA:
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001099 ret = h3_data_to_htx(qcs, b, flen, last_stream_frame);
Amaury Denoyelle8d818c62022-08-02 11:32:45 +02001100 h3s->st_req = H3S_ST_REQ_DATA;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001101 break;
1102 case H3_FT_HEADERS:
Amaury Denoyelleedfcb552023-01-13 16:40:31 +01001103 if (h3s->st_req == H3S_ST_REQ_BEFORE) {
1104 ret = h3_headers_to_htx(qcs, b, flen, last_stream_frame);
1105 h3s->st_req = H3S_ST_REQ_HEADERS;
1106 }
1107 else {
1108 ret = h3_trailers_to_htx(qcs, b, flen, last_stream_frame);
1109 h3s->st_req = H3S_ST_REQ_TRAILERS;
1110 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001111 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001112 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001113 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001114 case H3_FT_MAX_PUSH_ID:
1115 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001116 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001117 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001118 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001119 case H3_FT_SETTINGS:
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001120 ret = h3_parse_settings_frm(qcs->qcc->ctx, b, flen);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +02001121 if (ret < 0) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001122 TRACE_ERROR("error on SETTINGS parsing", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001123 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001124 goto err;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +02001125 }
1126 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001127 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001128 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +01001129 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +02001130 *
1131 * Implementations MUST discard frames [...] that have unknown
1132 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +01001133 */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001134 ret = flen;
1135 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001136 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +02001137
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001138 if (ret > 0) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +02001139 BUG_ON(h3s->demux_frame_len < ret);
1140 h3s->demux_frame_len -= ret;
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001141 b_del(b, ret);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001142 total += ret;
Amaury Denoyelle291ee252022-05-02 10:35:39 +02001143 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001144 }
1145
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001146 /* Reset demux frame type for traces. */
1147 if (!h3s->demux_frame_len)
1148 h3s->demux_frame_type = H3_FT_UNINIT;
1149
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001150 /* Interrupt decoding on stream/connection error detected. */
1151 if (h3s->err) {
1152 qcc_abort_stream_read(qcs);
1153 qcc_reset_stream(qcs, h3s->err);
1154 return b_data(b);
1155 }
1156 else if (h3c->err) {
1157 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
1158 return b_data(b);
1159 }
1160
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02001161 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
1162 * However, currently, io-cb of MUX does not handle Rx.
1163 */
1164
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001165 done:
1166 TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001167 return total;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001168
1169 err:
1170 TRACE_DEVEL("leaving on error", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
1171 return -1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001172}
1173
Amaury Denoyellea5871362021-10-07 16:26:12 +02001174/* Returns buffer for data sending.
1175 * May be NULL if the allocation failed.
1176 */
1177static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001178{
Amaury Denoyellea5871362021-10-07 16:26:12 +02001179 if (!b_size(&qcs->tx.buf))
1180 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001181
Amaury Denoyellea5871362021-10-07 16:26:12 +02001182 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001183}
1184
Amaury Denoyelle6b923942022-05-23 14:25:53 +02001185/* Function used to emit stream data from <qcs> control uni-stream */
1186static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001187{
1188 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001189 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001190 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +02001191 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001192 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001193
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001194 TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
1195
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001196 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001197
1198 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +02001199 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001200
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001201 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
1202 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
1203 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
1204 quic_int_getsize(h3_settings_qpack_blocked_streams);
1205 if (h3_settings_max_field_section_size) {
1206 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
1207 quic_int_getsize(h3_settings_max_field_section_size);
1208 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001209
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001210 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001211 /* Build a SETTINGS frame */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001212 b_quic_enc_int(&pos, H3_FT_SETTINGS, 0);
1213 b_quic_enc_int(&pos, frm_len, 0);
1214 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0);
1215 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity, 0);
1216 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS, 0);
1217 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001218 if (h3_settings_max_field_section_size) {
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001219 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE, 0);
1220 b_quic_enc_int(&pos, h3_settings_max_field_section_size, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001221 }
Amaury Denoyellea5871362021-10-07 16:26:12 +02001222
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001223 res = mux_get_buf(qcs);
1224 if (b_room(res) < b_data(&pos)) {
1225 // TODO the mux should be put in blocked state, with
1226 // the stream in state waiting for settings to be sent
1227 ABORT_NOW();
1228 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001229
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001230 ret = b_force_xfer(res, &pos, b_data(&pos));
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001231 if (ret > 0) {
1232 /* Register qcs for sending before other streams. */
Amaury Denoyellef9b03262023-01-09 10:34:25 +01001233 qcc_send_stream(qcs, 1);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001234 h3c->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001235 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001236
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001237 TRACE_LEAVE(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001238 return ret;
1239}
1240
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001241static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
1242{
1243 struct buffer outbuf;
1244 struct buffer headers_buf = BUF_NULL;
1245 struct buffer *res;
1246 struct http_hdr list[global.tune.max_http_hdr];
1247 struct htx_sl *sl;
1248 struct htx_blk *blk;
1249 enum htx_blk_type type;
1250 int frame_length_size; /* size in bytes of frame length varint field */
1251 int ret = 0;
1252 int hdr;
1253 int status = 0;
1254
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001255 TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1256
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001257 sl = NULL;
1258 hdr = 0;
1259 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1260 type = htx_get_blk_type(blk);
1261
1262 if (type == HTX_BLK_UNUSED)
1263 continue;
1264
1265 if (type == HTX_BLK_EOH)
1266 break;
1267
1268 if (type == HTX_BLK_RES_SL) {
1269 /* start-line -> HEADERS h3 frame */
1270 BUG_ON(sl);
1271 sl = htx_get_blk_ptr(htx, blk);
1272 /* TODO should be on h3 layer */
1273 status = sl->info.res.status;
1274 }
1275 else if (type == HTX_BLK_HDR) {
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +02001276 if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1))
Amaury Denoyellefa7fadc2022-06-15 15:52:27 +02001277 goto err;
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001278 list[hdr].n = htx_get_blk_name(htx, blk);
1279 list[hdr].v = htx_get_blk_value(htx, blk);
1280 hdr++;
1281 }
1282 else {
1283 ABORT_NOW();
1284 goto err;
1285 }
1286 }
1287
1288 BUG_ON(!sl);
1289
1290 list[hdr].n = ist("");
1291
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001292 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001293
1294 /* At least 5 bytes to store frame type + length as a varint max size */
1295 if (b_room(res) < 5)
1296 ABORT_NOW();
1297
1298 b_reset(&outbuf);
1299 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
1300 /* Start the headers after frame type + length */
1301 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
1302
1303 if (qpack_encode_field_section_line(&headers_buf))
1304 ABORT_NOW();
1305 if (qpack_encode_int_status(&headers_buf, status))
1306 ABORT_NOW();
1307
1308 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
1309 if (isteq(list[hdr].n, ist("")))
1310 break;
1311
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001312 /* RFC 9114 4.2. HTTP Fields
1313 *
1314 * An intermediary transforming an HTTP/1.x message to HTTP/3
1315 * MUST remove connection-specific header fields as discussed in
1316 * Section 7.6.1 of [HTTP], or their messages will be treated by
1317 * other HTTP/3 endpoints as malformed.
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001318 */
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001319 if (isteq(list[hdr].n, ist("connection")) ||
1320 isteq(list[hdr].n, ist("proxy-connection")) ||
1321 isteq(list[hdr].n, ist("keep-alive")) ||
1322 isteq(list[hdr].n, ist("transfer-encoding"))) {
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001323 continue;
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001324 }
1325 else if (isteq(list[hdr].n, ist("te"))) {
1326 /* "te" may only be sent with "trailers" if this value
1327 * is present, otherwise it must be deleted.
1328 */
1329 const struct ist v = istist(list[hdr].v, ist("trailers"));
1330 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
1331 continue;
1332 list[hdr].v = ist("trailers");
1333 }
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001334
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001335 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
1336 ABORT_NOW();
1337 }
1338
1339 /* Now that all headers are encoded, we are certain that res buffer is
1340 * big enough
1341 */
1342 frame_length_size = quic_int_getsize(b_data(&headers_buf));
1343 res->head += 4 - frame_length_size;
1344 b_putchr(res, 0x01); /* h3 HEADERS frame type */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001345 if (!b_quic_enc_int(res, b_data(&headers_buf), 0))
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001346 ABORT_NOW();
1347 b_add(res, b_data(&headers_buf));
1348
1349 ret = 0;
1350 blk = htx_get_head_blk(htx);
1351 while (blk) {
1352 type = htx_get_blk_type(blk);
1353 ret += htx_get_blksz(blk);
1354 blk = htx_remove_blk(htx, blk);
1355 if (type == HTX_BLK_EOH)
1356 break;
1357 }
1358
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001359 TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001360 return ret;
1361
1362 err:
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001363 TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001364 return 0;
1365}
1366
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001367/* Convert a series of HTX trailer blocks from <htx> buffer into <qcs> buffer
1368 * as a H3 HEADERS frame. H3 forbidden trailers are skipped. HTX trailer blocks
1369 * are removed from <htx> until EOT is found and itself removed.
1370 *
1371 * If only a EOT HTX block is present without trailer, no H3 frame is produced.
1372 * Caller is responsible to emit an empty QUIC STREAM frame to signal the end
1373 * of the stream.
1374 *
1375 * Returns the size of HTX blocks removed.
1376 */
1377static int h3_resp_trailers_send(struct qcs *qcs, struct htx *htx)
1378{
1379 struct buffer headers_buf = BUF_NULL;
1380 struct buffer *res;
1381 struct http_hdr list[global.tune.max_http_hdr];
1382 struct htx_blk *blk;
1383 enum htx_blk_type type;
1384 char *tail;
1385 int ret = 0;
1386 int hdr;
1387
1388 TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1389
1390 hdr = 0;
1391 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1392 type = htx_get_blk_type(blk);
1393
1394 if (type == HTX_BLK_UNUSED)
1395 continue;
1396
1397 if (type == HTX_BLK_EOT)
1398 break;
1399
1400 if (type == HTX_BLK_TLR) {
1401 if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1))
1402 goto err;
1403 list[hdr].n = htx_get_blk_name(htx, blk);
1404 list[hdr].v = htx_get_blk_value(htx, blk);
1405 hdr++;
1406 }
1407 else {
1408 TRACE_ERROR("unexpected HTX block", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1409 goto err;
1410 }
1411 }
1412
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001413 if (!hdr) {
1414 /* No headers encoded here so no need to generate a H3 HEADERS
1415 * frame. Mux will send an empty QUIC STREAM frame with FIN.
1416 */
1417 TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1418 goto end;
1419 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001420 list[hdr].n = ist("");
1421
1422 res = mux_get_buf(qcs);
1423
1424 /* At least 9 bytes to store frame type + length as a varint max size */
1425 if (b_room(res) < 9) {
1426 qcs->flags |= QC_SF_BLK_MROOM;
1427 goto err;
1428 }
1429
1430 /* Force buffer realignment as size required to encode headers is unknown. */
1431 if (b_space_wraps(res))
1432 b_slow_realign(res, trash.area, b_data(res));
1433 /* Start the headers after frame type + length */
1434 headers_buf = b_make(b_peek(res, b_data(res) + 9), b_contig_space(res) - 9, 0, 0);
1435
Amaury Denoyelle224ba5c2023-01-26 17:41:58 +01001436 if (qpack_encode_field_section_line(&headers_buf)) {
1437 qcs->flags |= QC_SF_BLK_MROOM;
1438 goto err;
1439 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001440
1441 tail = b_tail(&headers_buf);
1442 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
1443 if (isteq(list[hdr].n, ist("")))
1444 break;
1445
1446 /* forbidden HTTP/3 headers, cf h3_resp_headers_send() */
1447 if (isteq(list[hdr].n, ist("host")) ||
1448 isteq(list[hdr].n, ist("content-length")) ||
1449 isteq(list[hdr].n, ist("connection")) ||
1450 isteq(list[hdr].n, ist("proxy-connection")) ||
1451 isteq(list[hdr].n, ist("keep-alive")) ||
1452 isteq(list[hdr].n, ist("te")) ||
1453 isteq(list[hdr].n, ist("transfer-encoding"))) {
1454 continue;
1455 }
1456
Amaury Denoyelle224ba5c2023-01-26 17:41:58 +01001457 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) {
1458 qcs->flags |= QC_SF_BLK_MROOM;
1459 goto err;
1460 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001461 }
1462
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001463 /* Check that at least one header was encoded in buffer. */
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001464 if (b_tail(&headers_buf) == tail) {
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001465 /* No headers encoded here so no need to generate a H3 HEADERS
1466 * frame. Mux will send an empty QUIC STREAM frame with FIN.
1467 */
1468 TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001469 goto end;
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001470 }
1471
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001472 /* Now that all headers are encoded, we are certain that res buffer is
1473 * big enough.
1474 */
1475 b_putchr(res, 0x01); /* h3 HEADERS frame type */
1476 if (!b_quic_enc_int(res, b_data(&headers_buf), 8))
1477 ABORT_NOW();
1478 b_add(res, b_data(&headers_buf));
1479
1480 end:
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001481 ret = 0;
1482 blk = htx_get_head_blk(htx);
1483 while (blk) {
1484 type = htx_get_blk_type(blk);
1485 ret += htx_get_blksz(blk);
1486 blk = htx_remove_blk(htx, blk);
1487 if (type == HTX_BLK_EOT)
1488 break;
1489 }
1490
1491 TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1492 return ret;
1493
1494 err:
1495 TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1496 return 0;
1497}
1498
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001499/* Returns the total of bytes sent. */
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001500static int h3_resp_data_send(struct qcs *qcs, struct htx *htx, size_t count)
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001501{
1502 struct buffer outbuf;
1503 struct buffer *res;
1504 size_t total = 0;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001505 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001506 struct htx_blk *blk;
1507 enum htx_blk_type type;
1508
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001509 TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
1510
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001511 new_frame:
1512 if (!count || htx_is_empty(htx))
1513 goto end;
1514
1515 blk = htx_get_head_blk(htx);
1516 type = htx_get_blk_type(blk);
1517 fsize = bsize = htx_get_blksz(blk);
1518
1519 if (type != HTX_BLK_DATA)
1520 goto end;
1521
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001522 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001523
1524 if (fsize > count)
1525 fsize = count;
1526
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001527 /* h3 DATA headers : 1-byte frame type + varint frame length */
1528 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001529
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001530 while (1) {
1531 b_reset(&outbuf);
1532 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
1533 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
1534 break;
1535 b_slow_realign(res, trash.area, b_data(res));
1536 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001537
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001538 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001539 * stream. It is expected that the stream connector layer will subscribe
1540 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001541 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001542 if (b_size(&outbuf) <= hsize) {
1543 qcs->flags |= QC_SF_BLK_MROOM;
1544 goto end;
1545 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001546
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001547 if (b_size(&outbuf) < hsize + fsize)
1548 fsize = b_size(&outbuf) - hsize;
1549 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001550
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001551 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001552 b_quic_enc_int(&outbuf, fsize, 0); /* h3 frame length */
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001553
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001554 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001555 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001556 count -= fsize;
1557
1558 if (fsize == bsize)
1559 htx_remove_blk(htx, blk);
1560 else
1561 htx_cut_data_blk(htx, blk, fsize);
1562
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001563 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001564 b_add(res, b_data(&outbuf));
1565 goto new_frame;
1566
1567 end:
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001568 TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001569 return total;
1570}
1571
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001572static size_t h3_snd_buf(struct qcs *qcs, struct htx *htx, size_t count)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001573{
1574 size_t total = 0;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001575 enum htx_blk_type btype;
1576 struct htx_blk *blk;
1577 uint32_t bsize;
1578 int32_t idx;
1579 int ret;
1580
Amaury Denoyelled8769d12022-03-25 15:28:33 +01001581 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001582
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001583 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001584 idx = htx_get_head(htx);
1585 blk = htx_get_blk(htx, idx);
1586 btype = htx_get_blk_type(blk);
1587 bsize = htx_get_blksz(blk);
1588
1589 /* Not implemented : QUIC on backend side */
1590 BUG_ON(btype == HTX_BLK_REQ_SL);
1591
1592 switch (btype) {
1593 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001594 /* start-line -> HEADERS h3 frame */
1595 ret = h3_resp_headers_send(qcs, htx);
1596 if (ret > 0) {
1597 total += ret;
1598 count -= ret;
1599 if (ret < bsize)
1600 goto out;
1601 }
1602 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001603
1604 case HTX_BLK_DATA:
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001605 ret = h3_resp_data_send(qcs, htx, count);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001606 if (ret > 0) {
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001607 total += ret;
1608 count -= ret;
1609 if (ret < bsize)
1610 goto out;
1611 }
1612 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001613
1614 case HTX_BLK_TLR:
1615 case HTX_BLK_EOT:
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001616 ret = h3_resp_trailers_send(qcs, htx);
1617 if (ret > 0) {
1618 total += ret;
1619 count -= ret;
1620 if (ret < bsize)
1621 goto out;
1622 }
1623 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001624
1625 default:
1626 htx_remove_blk(htx, blk);
1627 total += bsize;
1628 count -= bsize;
1629 break;
1630 }
1631 }
1632
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001633 out:
1634 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +02001635}
1636
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001637/* Notify about a closure on <qcs> stream requested by the remote peer.
1638 *
1639 * Stream channel <side> is explained relative to our endpoint : WR for
1640 * STOP_SENDING or RD for RESET_STREAM reception. Callback decode_qcs() is used
1641 * instead for closure performed using a STREAM frame with FIN bit.
1642 *
1643 * The main objective of this function is to check if closure is valid
1644 * according to HTTP/3 specification.
1645 *
1646 * Returns 0 on success else non-zero. A CONNECTION_CLOSE is generated on
1647 * error.
1648 */
1649static int h3_close(struct qcs *qcs, enum qcc_app_ops_close_side side)
1650{
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001651 struct h3s *h3s = qcs->ctx;
1652 struct h3c *h3c = h3s->h3c;;
1653
1654 /* RFC 9114 6.2.1. Control Streams
1655 *
1656 * The sender
1657 * MUST NOT close the control stream, and the receiver MUST NOT
1658 * request that the sender close the control stream. If either
1659 * control stream is closed at any point, this MUST be treated
1660 * as a connection error of type H3_CLOSED_CRITICAL_STREAM.
1661 */
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001662 if (qcs == h3c->ctrl_strm || h3s->type == H3S_T_CTRL) {
Amaury Denoyellee31867b2023-01-31 16:01:22 +01001663 TRACE_ERROR("closure detected on control stream", H3_EV_H3S_END, qcs->qcc->conn, qcs);
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001664 qcc_emit_cc_app(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
1665 return 1;
1666 }
1667
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001668 return 0;
1669}
1670
Amaury Denoyellec0156792022-06-03 15:29:07 +02001671static int h3_attach(struct qcs *qcs, void *conn_ctx)
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001672{
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001673 struct h3c *h3c = conn_ctx;
1674 struct h3s *h3s = NULL;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001675
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001676 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1677
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001678 /* RFC 9114 5.2. Connection Shutdown
1679 *
1680 * Upon sending
1681 * a GOAWAY frame, the endpoint SHOULD explicitly cancel (see
1682 * Sections 4.1.1 and 7.2.3) any requests or pushes that have
1683 * identifiers greater than or equal to the one indicated, in
1684 * order to clean up transport state for the affected streams.
1685 * The endpoint SHOULD continue to do so as more requests or
1686 * pushes arrive.
1687 */
1688 if (h3c->flags & H3_CF_GOAWAY_SENT && qcs->id >= h3c->id_goaway &&
1689 quic_stream_is_bidi(qcs->id)) {
1690 /* Reject request and do not allocate a h3s context.
1691 * TODO support push uni-stream rejection.
1692 */
1693 TRACE_STATE("reject stream higher than goaway", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1694 qcc_abort_stream_read(qcs);
1695 qcc_reset_stream(qcs, H3_REQUEST_REJECTED);
1696 goto done;
1697 }
1698
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001699 h3s = pool_alloc(pool_head_h3s);
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001700 if (!h3s) {
1701 TRACE_ERROR("h3s allocation failure", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1702 qcc_emit_cc_app(qcs->qcc, H3_INTERNAL_ERROR, 1);
1703 goto err;
1704 }
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001705
1706 qcs->ctx = h3s;
Amaury Denoyellec0156792022-06-03 15:29:07 +02001707 h3s->h3c = conn_ctx;
1708
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001709 h3s->demux_frame_len = 0;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001710 h3s->demux_frame_type = H3_FT_UNINIT;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001711 h3s->body_len = 0;
1712 h3s->data_len = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +02001713 h3s->flags = 0;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001714 h3s->err = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001715
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001716 if (quic_stream_is_bidi(qcs->id)) {
1717 h3s->type = H3S_T_REQ;
Amaury Denoyelle8d818c62022-08-02 11:32:45 +02001718 h3s->st_req = H3S_ST_REQ_BEFORE;
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001719 qcs_wait_http_req(qcs);
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001720 }
1721 else {
1722 /* stream type must be decoded for unidirectional streams */
1723 h3s->type = H3S_T_UNKNOWN;
1724 }
1725
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001726 done:
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001727 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001728 return 0;
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001729
1730 err:
1731 TRACE_DEVEL("leaving in error", H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1732 return 1;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001733}
1734
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001735static void h3_detach(struct qcs *qcs)
1736{
1737 struct h3s *h3s = qcs->ctx;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001738
1739 TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs);
1740
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001741 pool_free(pool_head_h3s, h3s);
1742 qcs->ctx = NULL;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001743
1744 TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001745}
1746
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001747/* Initialize H3 control stream and prepare SETTINGS emission.
1748 *
1749 * Returns 0 on success else non-zero.
1750 */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001751static int h3_finalize(void *ctx)
1752{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001753 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001754 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001755
Amaury Denoyelleb1437232022-07-08 11:53:22 +02001756 qcs = qcc_init_stream_local(h3c->qcc, 0);
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001757 if (!qcs)
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001758 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001759
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001760 h3_control_send(qcs, h3c);
Amaury Denoyelled7010392022-07-13 15:17:29 +02001761 h3c->ctrl_strm = qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001762
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001763 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001764}
1765
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001766/* Generate a GOAWAY frame for <h3c> connection on the control stream.
1767 *
1768 * Returns 0 on success else non-zero.
1769 */
1770static int h3_send_goaway(struct h3c *h3c)
1771{
1772 struct qcs *qcs = h3c->ctrl_strm;
1773 struct buffer pos, *res;
1774 unsigned char data[3 * QUIC_VARINT_MAX_SIZE];
1775 size_t frm_len = quic_int_getsize(h3c->id_goaway);
1776
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001777 TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001778
1779 if (!qcs) {
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001780 TRACE_ERROR("control stream not initialized", H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001781 goto err;
1782 }
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001783
1784 pos = b_make((char *)data, sizeof(data), 0, 0);
1785
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001786 b_quic_enc_int(&pos, H3_FT_GOAWAY, 0);
1787 b_quic_enc_int(&pos, frm_len, 0);
1788 b_quic_enc_int(&pos, h3c->id_goaway, 0);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001789
1790 res = mux_get_buf(qcs);
1791 if (!res || b_room(res) < b_data(&pos)) {
1792 /* Do not try forcefully to emit GOAWAY if no space left. */
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001793 TRACE_ERROR("cannot send GOAWAY", H3_EV_H3C_END, h3c->qcc->conn, qcs);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001794 goto err;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001795 }
1796
1797 b_force_xfer(res, &pos, b_data(&pos));
Amaury Denoyelle19adeb52023-01-25 10:50:03 +01001798 qcc_send_stream(qcs, 1);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001799
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001800 h3c->flags |= H3_CF_GOAWAY_SENT;
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001801 TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001802 return 0;
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001803
1804 err:
Amaury Denoyelle3d550842023-01-24 17:42:21 +01001805 /* Consider GOAWAY as sent even if not really the case. This will
1806 * block future stream opening using H3_REQUEST_REJECTED reset.
1807 */
1808 h3c->flags |= H3_CF_GOAWAY_SENT;
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001809 TRACE_DEVEL("leaving in error", H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001810 return 1;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001811}
1812
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001813/* Initialize the HTTP/3 context for <qcc> mux.
1814 * Return 1 if succeeded, 0 if not.
1815 */
1816static int h3_init(struct qcc *qcc)
1817{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001818 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001819 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001820
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001821 h3c = pool_alloc(pool_head_h3c);
1822 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001823 goto fail_no_h3;
1824
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001825 h3c->qcc = qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +02001826 h3c->ctrl_strm = NULL;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001827 h3c->err = 0;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001828 h3c->flags = 0;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001829 h3c->id_goaway = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001830
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001831 qcc->ctx = h3c;
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +02001832 /* TODO cleanup only ref to quic_conn */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001833 h3c->prx_counters =
1834 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1835 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001836 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001837
1838 return 1;
1839
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001840 fail_no_h3:
1841 return 0;
1842}
1843
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001844/* Send a HTTP/3 GOAWAY followed by a CONNECTION_CLOSE_APP. */
1845static void h3_shutdown(void *ctx)
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001846{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001847 struct h3c *h3c = ctx;
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001848
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001849 TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001850
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001851 /* RFC 9114 5.2. Connection Shutdown
1852 *
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001853 * Even when a connection is not idle, either endpoint can decide to
1854 * stop using the connection and initiate a graceful connection close.
1855 * Endpoints initiate the graceful shutdown of an HTTP/3 connection by
1856 * sending a GOAWAY frame.
1857 */
1858 h3_send_goaway(h3c);
1859
1860 /* RFC 9114 5.2. Connection Shutdown
1861 *
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001862 * An endpoint that completes a
1863 * graceful shutdown SHOULD use the H3_NO_ERROR error code when closing
1864 * the connection.
1865 */
1866 qcc_emit_cc_app(h3c->qcc, H3_NO_ERROR, 0);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001867
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001868 TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001869}
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001870
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001871static void h3_release(void *ctx)
1872{
1873 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001874 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001875}
1876
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001877/* Increment the h3 error code counters for <error_code> value */
1878static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1879{
1880 struct h3c *h3c = ctx;
1881
1882 h3_inc_err_cnt(h3c->prx_counters, err_code);
1883}
1884
Amaury Denoyelle35d90532023-01-26 16:03:45 +01001885static inline const char *h3_ft_str(uint64_t type)
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001886{
1887 switch (type) {
1888 case H3_FT_DATA: return "DATA";
1889 case H3_FT_HEADERS: return "HEADERS";
1890 case H3_FT_SETTINGS: return "SETTINGS";
1891 case H3_FT_PUSH_PROMISE: return "PUSH_PROMISE";
1892 case H3_FT_MAX_PUSH_ID: return "MAX_PUSH_ID";
1893 case H3_FT_CANCEL_PUSH: return "CANCEL_PUSH";
1894 case H3_FT_GOAWAY: return "GOAWAY";
1895 default: return "_UNKNOWN_";
1896 }
1897}
1898
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001899/* h3 trace handler */
1900static void h3_trace(enum trace_level level, uint64_t mask,
1901 const struct trace_source *src,
1902 const struct ist where, const struct ist func,
1903 const void *a1, const void *a2, const void *a3, const void *a4)
1904{
1905 const struct connection *conn = a1;
1906 const struct qcc *qcc = conn ? conn->ctx : NULL;
1907 const struct qcs *qcs = a2;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001908 const struct h3s *h3s = qcs ? qcs->ctx : NULL;
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001909
Frédéric Lécaille1c725aa2022-09-08 15:49:37 +02001910 if (!qcc)
1911 return;
1912
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001913 if (src->verbosity > H3_VERB_CLEAN) {
1914 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
Frédéric Lécaille2eb5faa2022-09-08 16:03:13 +02001915 if (qcc->conn->handle.qc)
1916 chunk_appendf(&trace_buf, " qc=%p", qcc->conn->handle.qc);
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001917
1918 if (qcs)
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001919 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001920
1921 if (h3s && h3s->demux_frame_type != H3_FT_UNINIT) {
Amaury Denoyelle35d90532023-01-26 16:03:45 +01001922 chunk_appendf(&trace_buf, " h3s.dem=%s/%llu",
1923 h3_ft_str(h3s->demux_frame_type), (ull)h3s->demux_frame_len);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001924 }
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001925 }
1926}
1927
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001928/* HTTP/3 application layer operations */
1929const struct qcc_app_ops h3_ops = {
1930 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001931 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001932 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001933 .snd_buf = h3_snd_buf,
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001934 .close = h3_close,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001935 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001936 .finalize = h3_finalize,
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001937 .shutdown = h3_shutdown,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001938 .inc_err_cnt = h3_stats_inc_err_cnt,
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001939 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001940};