blob: a214a226231c09fdb96a28091f63a2b6e8422176 [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 */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100117
118/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +0200119static uint64_t h3_settings_qpack_max_table_capacity = 0;
120static uint64_t h3_settings_qpack_blocked_streams = 4096;
121static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100122
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200123struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100124 struct qcc *qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +0200125 struct qcs *ctrl_strm; /* Control stream */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100126 enum h3_err err;
127 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200128
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100129 /* Settings */
130 uint64_t qpack_max_table_capacity;
131 uint64_t qpack_blocked_streams;
132 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200133
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200134 uint64_t id_goaway; /* stream ID used for a GOAWAY frame */
135
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100136 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +0200137 /* Stats counters */
138 struct h3_counters *prx_counters;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100139};
140
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200141DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100142
Amaury Denoyelle35550642022-05-24 15:14:53 +0200143#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200144#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100145#define H3_SF_HAVE_CLEN 0x00000004 /* content-length header is present */
Amaury Denoyelle35550642022-05-24 15:14:53 +0200146
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200147struct h3s {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200148 struct h3c *h3c;
149
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200150 enum h3s_t type;
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200151 enum h3s_st_req st_req; /* only used for request streams */
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200152 int demux_frame_len;
153 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200154
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100155 unsigned long long body_len; /* known request body length from content-length header if present */
156 unsigned long long data_len; /* total length of all parsed DATA */
157
Amaury Denoyelle35550642022-05-24 15:14:53 +0200158 int flags;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100159 int err; /* used for stream reset */
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200160};
161
162DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
163
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200164/* Initialize an uni-stream <qcs> by reading its type from <b>.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200165 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200166 * Returns the count of consumed bytes or a negative error code.
Amaury Denoyelle35550642022-05-24 15:14:53 +0200167 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200168static ssize_t h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
169 struct buffer *b)
Amaury Denoyelle35550642022-05-24 15:14:53 +0200170{
171 /* decode unidirectional stream type */
172 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200173 uint64_t type;
174 size_t len = 0, ret;
175
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200176 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
177
Amaury Denoyelle35550642022-05-24 15:14:53 +0200178 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
179 h3s->flags & H3_SF_UNI_INIT);
180
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200181 ret = b_quic_dec_int(&type, b, &len);
Amaury Denoyelle35550642022-05-24 15:14:53 +0200182 if (!ret) {
183 ABORT_NOW();
184 }
185
186 switch (type) {
187 case H3_UNI_S_T_CTRL:
188 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200189 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200190 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200191 }
192 h3c->flags |= H3_CF_UNI_CTRL_SET;
193 h3s->type = H3S_T_CTRL;
194 break;
195
196 case H3_UNI_S_T_PUSH:
197 /* TODO not supported for the moment */
198 h3s->type = H3S_T_PUSH;
199 break;
200
201 case H3_UNI_S_T_QPACK_DEC:
202 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200203 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200204 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200205 }
206 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
207 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200208 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200209 break;
210
211 case H3_UNI_S_T_QPACK_ENC:
212 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
Amaury Denoyelled666d742022-07-13 15:15:58 +0200213 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR, 1);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200214 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200215 }
216 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
217 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200218 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200219 break;
220
221 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200222 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
223 *
224 * Implementations MUST [...] abort reading on unidirectional
225 * streams that have unknown or unsupported types.
226 */
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100227 qcc_abort_stream_read(qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200228 return -1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200229 };
230
231 h3s->flags |= H3_SF_UNI_INIT;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200232
Amaury Denoyelled5581d52022-05-30 15:51:31 +0200233 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200234 return len;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200235}
236
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200237/* Parse a buffer <b> for a <qcs> uni-stream which does not contains H3 frames.
238 * This may be used for QPACK encoder/decoder streams for example. <fin> is set
239 * if this is the last frame of the stream.
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200240 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200241 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200242 */
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200243static ssize_t h3_parse_uni_stream_no_h3(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200244{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200245 struct h3s *h3s = qcs->ctx;
246
247 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
248 !(h3s->flags & H3_SF_UNI_NO_H3));
249
250 switch (h3s->type) {
251 case H3S_T_QPACK_DEC:
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200252 if (qpack_decode_dec(b, fin, qcs))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200253 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200254 break;
255 case H3S_T_QPACK_ENC:
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200256 if (qpack_decode_enc(b, fin, qcs))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200257 return -1;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200258 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200259 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200260 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200261 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
262 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200263 }
264
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200265 /* TODO adjust return code */
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200266 return 0;
267}
268
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200269/* Decode a H3 frame header from <rxbuf> buffer. The frame type is stored in
270 * <ftype> and length in <flen>.
271 *
272 * Returns the size of the H3 frame header. Note that the input buffer is not
273 * consumed.
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100274 */
275static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200276 struct buffer *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100277{
278 size_t hlen;
279
280 hlen = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200281 if (!b_quic_dec_int(ftype, b, &hlen) ||
282 !b_quic_dec_int(flen, b, &hlen)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100283 return 0;
Amaury Denoyelle88d5dd12022-05-31 11:44:52 +0200284 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100285
286 return hlen;
287}
288
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200289/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
290 *
291 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
292 * be reported.
293 */
294static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
295{
296 struct h3s *h3s = qcs->ctx;
297 const uint64_t id = qcs->id;
298
299 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
300
301 switch (ftype) {
302 case H3_FT_DATA:
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200303 return h3s->type != H3S_T_CTRL && (h3s->st_req == H3S_ST_REQ_HEADERS ||
304 h3s->st_req == H3S_ST_REQ_DATA);
305
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200306 case H3_FT_HEADERS:
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200307 return h3s->type != H3S_T_CTRL && h3s->st_req != H3S_ST_REQ_TRAILERS;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200308
309 case H3_FT_CANCEL_PUSH:
310 case H3_FT_GOAWAY:
311 case H3_FT_MAX_PUSH_ID:
312 /* Only allowed for control stream. First frame of control
313 * stream MUST be SETTINGS.
314 */
315 return h3s->type == H3S_T_CTRL &&
316 (h3c->flags & H3_CF_SETTINGS_RECV);
317
318 case H3_FT_SETTINGS:
319 /* draft-ietf-quic-http34 7.2.4. SETTINGS
320 *
321 * If an endpoint receives a second SETTINGS frame on the control
322 * stream, the endpoint MUST respond with a connection error of type
323 * H3_FRAME_UNEXPECTED.
324 */
325 return h3s->type == H3S_T_CTRL &&
326 !(h3c->flags & H3_CF_SETTINGS_RECV);
327
328 case H3_FT_PUSH_PROMISE:
329 return h3s->type != H3S_T_CTRL &&
330 (id & QCS_ID_SRV_INTIATOR_BIT);
331
332 default:
333 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
334 *
335 * Implementations MUST discard frames [...] that have unknown
336 * or unsupported types.
337 */
338 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
339 }
340}
341
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100342/* Check from stream <qcs> that length of all DATA frames does not exceed with
343 * a previously parsed content-length header. <fin> must be set for the last
344 * data of the stream so that length of DATA frames must be equal to the
345 * content-length.
346 *
347 * This must only be called for a stream with H3_SF_HAVE_CLEN flag.
348 *
349 * Return 0 on valid else non-zero.
350 */
351static int h3_check_body_size(struct qcs *qcs, int fin)
352{
353 struct h3s *h3s = qcs->ctx;
354 int ret = 0;
355 TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
356
357 /* Reserved for streams with a previously parsed content-length header. */
358 BUG_ON(!(h3s->flags & H3_SF_HAVE_CLEN));
359
360 /* RFC 9114 4.1.2. Malformed Requests and Responses
361 *
362 * A request or response that is defined as having content when it
363 * contains a Content-Length header field (Section 8.6 of [HTTP]) is
364 * malformed if the value of the Content-Length header field does not
365 * equal the sum of the DATA frame lengths received.
366 *
367 * TODO for backend support
368 * A response that is
369 * defined as never having content, even when a Content-Length is
370 * present, can have a non-zero Content-Length header field even though
371 * no content is included in DATA frames.
372 */
373 if (h3s->data_len > h3s->body_len ||
374 (fin && h3s->data_len < h3s->body_len)) {
375 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 +0100376 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100377 ret = -1;
378 }
379
380 TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
381 return ret;
382}
383
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100384/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200385 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100386 * set if this is the last data to transfer from this stream.
387 *
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100388 * Returns the number of consumed bytes or a negative error code. On error
389 * either the connection should be closed or the stream reset using codes
390 * provided in h3c.err / h3s.err.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100391 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200392static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
393 uint64_t len, char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100394{
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200395 struct h3s *h3s = qcs->ctx;
396 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100397 struct buffer htx_buf = BUF_NULL;
398 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100399 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200400 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200401 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200402 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100403 struct ist meth = IST_NULL, path = IST_NULL;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100404 struct ist scheme = IST_NULL, authority = IST_NULL;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200405 int hdr_idx, ret;
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100406 int cookie = -1, last_cookie = -1, i;
407
408 /* RFC 9114 4.1.2. Malformed Requests and Responses
409 *
410 * A malformed request or response is one that is an otherwise valid
411 * sequence of frames but is invalid due to:
412 * - the presence of prohibited fields or pseudo-header fields,
413 * - the absence of mandatory pseudo-header fields,
414 * - invalid values for pseudo-header fields,
415 * - pseudo-header fields after fields,
416 * - an invalid sequence of HTTP messages,
417 * - the inclusion of uppercase field names, or
418 * - the inclusion of invalid characters in field names or values.
419 *
420 * [...]
421 *
422 * Intermediaries that process HTTP requests or responses (i.e., any
423 * intermediary not acting as a tunnel) MUST NOT forward a malformed
424 * request or response. Malformed requests or responses that are
425 * detected MUST be treated as a stream error of type H3_MESSAGE_ERROR.
426 */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100427
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200428 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
429
Amaury Denoyelle8d818c62022-08-02 11:32:45 +0200430 /* TODO support trailer parsing in this function */
431
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200432 /* TODO support buffer wrapping */
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200433 BUG_ON(b_head(buf) + len >= b_wrap(buf));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200434 ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp,
435 list, sizeof(list) / sizeof(list[0]));
436 if (ret < 0) {
437 TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
438 h3c->err = -ret;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100439 len = -1;
440 goto out;
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200441 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100442
443 qc_get_buf(qcs, &htx_buf);
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100444 BUG_ON(!b_size(&htx_buf)); /* TODO */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100445 htx = htx_from_buf(&htx_buf);
446
447 /* first treat pseudo-header to build the start line */
448 hdr_idx = 0;
449 while (1) {
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100450 /* RFC 9114 4.3. HTTP Control Data
451 *
452 * Endpoints MUST treat a request or response that contains
453 * undefined or invalid pseudo-header fields as malformed.
454 *
455 * All pseudo-header fields MUST appear in the header section before
456 * regular header fields. Any request or response that contains a
457 * pseudo-header field that appears in a header section after a regular
458 * header field MUST be treated as malformed.
459 */
460
461 /* Stop at first non pseudo-header. */
462 if (!istmatch(list[hdr_idx].n, ist(":")))
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100463 break;
464
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100465 /* pseudo-header. Malformed name with uppercase character or
466 * invalid token will be rejected in the else clause.
467 */
468 if (isteq(list[hdr_idx].n, ist(":method"))) {
469 if (isttest(meth)) {
470 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 +0100471 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100472 len = -1;
473 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100474 }
475 meth = list[hdr_idx].v;
476 }
477 else if (isteq(list[hdr_idx].n, ist(":path"))) {
478 if (isttest(path)) {
479 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 +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 path = list[hdr_idx].v;
485 }
486 else if (isteq(list[hdr_idx].n, ist(":scheme"))) {
487 if (isttest(scheme)) {
488 /* duplicated pseudo-header */
489 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 +0100490 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100491 len = -1;
492 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100493 }
494 scheme = list[hdr_idx].v;
495 }
496 else if (isteq(list[hdr_idx].n, ist(":authority"))) {
497 if (isttest(authority)) {
498 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 +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 authority = list[hdr_idx].v;
504 }
505 else {
506 TRACE_ERROR("unknown pseudo-header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100507 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100508 len = -1;
509 goto out;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100510 }
511
512 ++hdr_idx;
513 }
514
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100515 if (!istmatch(meth, ist("CONNECT"))) {
516 /* RFC 9114 4.3.1. Request Pseudo-Header Fields
517 *
518 * All HTTP/3 requests MUST include exactly one value for the :method,
519 * :scheme, and :path pseudo-header fields, unless the request is a
520 * CONNECT request; see Section 4.4.
521 */
522 if (!isttest(meth) || !isttest(scheme) || !isttest(path)) {
523 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 +0100524 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100525 len = -1;
526 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100527 }
528 }
529
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100530 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200531 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100532
533 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200534 if (!sl) {
535 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100536 len = -1;
537 goto out;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200538 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100539
540 if (fin)
541 sl->flags |= HTX_SL_F_BODYLESS;
542
543 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100544
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100545 if (isttest(authority)) {
546 if (!htx_add_header(htx, ist("host"), authority)) {
547 h3c->err = H3_INTERNAL_ERROR;
548 len = -1;
549 goto out;
550 }
551 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100552
553 /* now treat standard headers */
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100554 while (1) {
555 if (isteq(list[hdr_idx].n, ist("")))
556 break;
557
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100558 if (istmatch(list[hdr_idx].n, ist(":"))) {
559 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 +0100560 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100561 len = -1;
562 goto out;
Amaury Denoyelle7b5a6712022-12-07 14:33:26 +0100563 }
564
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100565 for (i = 0; i < list[hdr_idx].n.len; ++i) {
566 const char c = list[hdr_idx].n.ptr[i];
567 if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) {
568 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 +0100569 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100570 len = -1;
571 goto out;
Amaury Denoyelled6fb7a02022-12-07 14:31:42 +0100572 }
573 }
574
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200575 if (isteq(list[hdr_idx].n, ist("cookie"))) {
576 http_cookie_register(list, hdr_idx, &cookie, &last_cookie);
Amaury Denoyelle19942e32022-12-15 09:18:25 +0100577 ++hdr_idx;
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200578 continue;
579 }
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100580 else if (isteq(list[hdr_idx].n, ist("content-length"))) {
581 ret = http_parse_cont_len_header(&list[hdr_idx].v,
582 &h3s->body_len,
583 h3s->flags & H3_SF_HAVE_CLEN);
584 if (ret < 0) {
585 TRACE_ERROR("invalid content-length", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +0100586 h3s->err = H3_MESSAGE_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100587 len = -1;
588 goto out;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100589 }
590 else if (!ret) {
591 /* Skip duplicated value. */
592 ++hdr_idx;
593 continue;
594 }
595
596 h3s->flags |= H3_SF_HAVE_CLEN;
597 /* This will fail if current frame is the last one and
598 * content-length is not null.
599 */
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100600 if (h3_check_body_size(qcs, fin)) {
601 len = -1;
602 goto out;
603 }
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +0100604 }
Amaury Denoyelle8ad26692023-01-17 17:47:06 +0100605 else if (isteq(list[hdr_idx].n, ist("connection")) ||
606 isteq(list[hdr_idx].n, ist("proxy-connection")) ||
607 isteq(list[hdr_idx].n, ist("keep-alive")) ||
608 isteq(list[hdr_idx].n, ist("transfer-encoding"))) {
609 /* RFC 9114 4.2. HTTP Fields
610 *
611 * HTTP/3 does not use the Connection header field to indicate
612 * connection-specific fields; in this protocol, connection-
613 * specific metadata is conveyed by other means. An endpoint
614 * MUST NOT generate an HTTP/3 field section containing
615 * connection-specific fields; any message containing
616 * connection-specific fields MUST be treated as malformed.
617 */
618 TRACE_ERROR("invalid connection header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
619 h3s->err = H3_MESSAGE_ERROR;
620 len = -1;
621 goto out;
622 }
623 else if (isteq(list[hdr_idx].n, ist("te")) &&
624 !isteq(list[hdr_idx].v, ist("trailers"))) {
625 /* RFC 9114 4.2. HTTP Fields
626 *
627 * The only exception to this is the TE header field, which MAY
628 * be present in an HTTP/3 request header; when it is, it MUST
629 * NOT contain any value other than "trailers".
630 */
631 TRACE_ERROR("invalid te header", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
632 h3s->err = H3_MESSAGE_ERROR;
633 len = -1;
634 goto out;
635 }
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200636
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100637 if (!htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v)) {
638 h3c->err = H3_INTERNAL_ERROR;
639 len = -1;
640 goto out;
641 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100642 ++hdr_idx;
643 }
644
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200645 if (cookie >= 0) {
646 if (http_cookie_merge(htx, list, cookie)) {
647 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100648 len = -1;
649 goto out;
Amaury Denoyelle115ccce2022-08-17 18:02:47 +0200650 }
651 }
652
Amaury Denoyellec4913f62022-12-15 10:58:05 +0100653 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
654 h3c->err = H3_INTERNAL_ERROR;
655 len = -1;
656 goto out;
657 }
658
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100659 if (fin)
660 htx->flags |= HTX_FL_EOM;
661
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100662 htx_to_buf(htx, &htx_buf);
663 htx = NULL;
664
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200665 if (!qc_attach_sc(qcs, &htx_buf)) {
666 h3c->err = H3_INTERNAL_ERROR;
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100667 len = -1;
668 goto out;
Amaury Denoyelle2bc47862022-06-30 10:04:42 +0200669 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100670
Amaury Denoyelle114c9c82022-03-28 14:53:45 +0200671 /* RFC 9114 5.2. Connection Shutdown
672 *
673 * The GOAWAY frame contains an identifier that
674 * indicates to the receiver the range of requests or pushes that were
675 * or might be processed in this connection. The server sends a client-
676 * initiated bidirectional stream ID; the client sends a push ID.
677 * Requests or pushes with the indicated identifier or greater are
678 * rejected (Section 4.1.1) by the sender of the GOAWAY. This
679 * identifier MAY be zero if no requests or pushes were processed.
680 */
681 if (qcs->id >= h3c->id_goaway)
682 h3c->id_goaway = qcs->id + 4;
683
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100684 out:
685 /* HTX may be non NULL if error before previous htx_to_buf(). */
686 if (htx)
687 htx_to_buf(htx, &htx_buf);
688
Willy Tarreau4596fe22022-05-17 19:07:51 +0200689 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100690 * except on stream creation error.
691 */
Amaury Denoyelle788fc052022-12-15 10:53:55 +0100692 if (b_size(&htx_buf)) {
693 b_free(&htx_buf);
694 offer_buffers(NULL, 1);
695 }
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100696
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200697 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200698 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100699}
700
Amaury Denoyelleedfcb552023-01-13 16:40:31 +0100701/* Parse from buffer <buf> a H3 HEADERS frame of length <len> used as trailers.
702 * Data are copied in a local HTX buffer and transfer to the stream connector
703 * layer. <fin> must be set if this is the last data to transfer from this
704 * stream.
705 *
706 * Returns the number of consumed bytes or a negative error code. On error
707 * either the connection should be closed or the stream reset using codes
708 * provided in h3c.err / h3s.err.
709 */
710static ssize_t h3_trailers_to_htx(struct qcs *qcs, const struct buffer *buf,
711 uint64_t len, char fin)
712{
713 struct h3s *h3s = qcs->ctx;
714 struct h3c *h3c = h3s->h3c;
715 struct buffer htx_buf = BUF_NULL;
716 struct buffer *tmp = get_trash_chunk();
717 struct htx *htx = NULL;
718 struct htx_sl *sl;
719 struct http_hdr list[global.tune.max_http_hdr];
720 int hdr_idx, ret;
721 int i;
722
723 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
724
725 /* TODO support buffer wrapping */
726 BUG_ON(b_head(buf) + len >= b_wrap(buf));
727 ret = qpack_decode_fs((const unsigned char *)b_head(buf), len, tmp,
728 list, sizeof(list) / sizeof(list[0]));
729 if (ret < 0) {
730 TRACE_ERROR("QPACK decoding error", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
731 h3c->err = -ret;
732 len = -1;
733 goto out;
734 }
735
736 qc_get_buf(qcs, &htx_buf);
737 BUG_ON(!b_size(&htx_buf)); /* TODO */
738 htx = htx_from_buf(&htx_buf);
739
740 if (!h3s->data_len) {
741 /* Notify that no body is present. This can only happens if
742 * there is H3 HEADERS as trailers without or empty H3 DATA
743 * frame. So this is probably not realistice ?
744 *
745 * TODO if sl is NULL because already consumed there is no way
746 * to notify about missing body.
747 */
748 sl = http_get_stline(htx);
749 if (sl)
750 sl->flags |= HTX_SL_F_BODYLESS;
751 else
752 TRACE_ERROR("cannot notify missing body after trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
753 }
754
755 hdr_idx = 0;
756 while (1) {
757 if (isteq(list[hdr_idx].n, ist("")))
758 break;
759
760 /* RFC 9114 4.3. HTTP Control Data
761 *
762 * Pseudo-header
763 * fields MUST NOT appear in trailer sections.
764 */
765 if (istmatch(list[hdr_idx].n, ist(":"))) {
766 TRACE_ERROR("pseudo-header field in trailers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
767 h3s->err = H3_MESSAGE_ERROR;
768 len = -1;
769 goto out;
770 }
771
772 for (i = 0; i < list[hdr_idx].n.len; ++i) {
773 const char c = list[hdr_idx].n.ptr[i];
774 if ((uint8_t)(c - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(c)) {
775 TRACE_ERROR("invalid characters in field name", 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
782 /* forbidden HTTP/3 headers, cf h3_headers_to_htx() */
783 if (isteq(list[hdr_idx].n, ist("host")) ||
784 isteq(list[hdr_idx].n, ist("content-length")) ||
785 isteq(list[hdr_idx].n, ist("connection")) ||
786 isteq(list[hdr_idx].n, ist("proxy-connection")) ||
787 isteq(list[hdr_idx].n, ist("keep-alive")) ||
788 isteq(list[hdr_idx].n, ist("te")) ||
789 isteq(list[hdr_idx].n, ist("transfer-encoding"))) {
790 TRACE_ERROR("forbidden HTTP/3 headers", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
791 h3s->err = H3_MESSAGE_ERROR;
792 len = -1;
793 goto out;
794 }
795
796 if (!htx_add_trailer(htx, list[hdr_idx].n, list[hdr_idx].v)) {
797 TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
798 h3c->err = H3_INTERNAL_ERROR;
799 len = -1;
800 goto out;
801 }
802
803 ++hdr_idx;
804 }
805
806 if (!htx_add_endof(htx, HTX_BLK_EOT)) {
807 TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
808 h3c->err = H3_INTERNAL_ERROR;
809 len = -1;
810 goto out;
811 }
812
813 if (fin)
814 htx->flags |= HTX_FL_EOM;
815
816 htx_to_buf(htx, &htx_buf);
817 htx = NULL;
818
819 out:
820 /* HTX may be non NULL if error before previous htx_to_buf(). */
821 if (htx)
822 htx_to_buf(htx, &htx_buf);
823
824 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
825 return len;
826}
827
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100828/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
829 * HTX buffer. <fin> must be set if this is the last data to transfer from this
830 * stream.
831 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200832 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100833 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200834static ssize_t h3_data_to_htx(struct qcs *qcs, const struct buffer *buf,
835 uint64_t len, char fin)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100836{
837 struct buffer *appbuf;
838 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200839 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100840 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200841 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100842
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200843 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
844
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100845 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
846 BUG_ON(!appbuf);
847 htx = htx_from_buf(appbuf);
848
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200849 if (len > b_data(buf)) {
850 len = b_data(buf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200851 fin = 0;
852 }
853
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200854 head = b_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200855 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100856 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200857 if (!htx_space) {
858 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200859 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200860 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200861
862 if (len > htx_space) {
863 len = htx_space;
864 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100865 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100866
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200867 if (head + len > b_wrap(buf)) {
868 size_t contig = b_wrap(buf) - head;
869 htx_sent = htx_add_data(htx, ist2(b_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200870 if (htx_sent < contig) {
871 qcs->flags |= QC_SF_DEM_FULL;
872 goto out;
873 }
874
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200875 len -= contig;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200876 head = b_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200877 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100878 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100879
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200880 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200881 if (htx_sent < len) {
882 qcs->flags |= QC_SF_DEM_FULL;
883 goto out;
884 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200885
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200886 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100887 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100888
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200889 out:
890 htx_to_buf(htx, appbuf);
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200891
892 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200893 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100894}
895
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200896/* Parse a SETTINGS frame of length <len> of payload <buf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200897 *
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200898 * Returns the number of consumed bytes or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200899 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200900static ssize_t h3_parse_settings_frm(struct h3c *h3c, const struct buffer *buf,
901 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200902{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200903 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200904 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200905 size_t ret = 0;
906 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200907
Amaury Denoyelle494512d2022-05-30 15:50:34 +0200908 TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
909
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200910 /* Work on a copy of <buf>. */
Amaury Denoyelle3a2fcfd2022-06-09 11:54:38 +0200911 b = b_make(b_orig(buf), b_size(buf), b_head_ofs(buf), len);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200912
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200913 while (b_data(&b)) {
914 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
915 h3c->err = H3_FRAME_ERROR;
916 return -1;
917 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200918
919 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
920 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200921
922 /* draft-ietf-quic-http34 7.2.4. SETTINGS
923 *
924 * The same setting identifier MUST NOT occur more than once in the
925 * SETTINGS frame. A receiver MAY treat the presence of duplicate
926 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
927 */
928
929 /* Ignore duplicate check for ID too big used for GREASE. */
930 if (id < sizeof(mask)) {
931 if (ha_bit_test(id, &mask)) {
932 h3c->err = H3_SETTINGS_ERROR;
933 return -1;
934 }
935 ha_bit_set(id, &mask);
936 }
937
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200938 switch (id) {
939 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
940 h3c->qpack_max_table_capacity = value;
941 break;
942 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
943 h3c->max_field_section_size = value;
944 break;
945 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
946 h3c->qpack_blocked_streams = value;
947 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200948
949 case H3_SETTINGS_RESERVED_0:
950 case H3_SETTINGS_RESERVED_2:
951 case H3_SETTINGS_RESERVED_3:
952 case H3_SETTINGS_RESERVED_4:
953 case H3_SETTINGS_RESERVED_5:
954 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
955 *
956 * Setting identifiers which were defined in [HTTP2] where there is no
957 * corresponding HTTP/3 setting have also been reserved
958 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
959 * their receipt MUST be treated as a connection error of type
960 * H3_SETTINGS_ERROR.
961 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200962 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200963 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200964 default:
965 /* MUST be ignored */
966 break;
967 }
968 }
969
Frédéric Lécaillebefcf702022-09-08 16:04:55 +0200970 TRACE_LEAVE(H3_EV_RX_FRAME|H3_EV_RX_SETTINGS, h3c->qcc->conn);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200971 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200972}
973
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100974/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
975 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200976 *
977 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100978 */
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200979static ssize_t h3_decode_qcs(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100980{
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200981 struct h3s *h3s = qcs->ctx;
Amaury Denoyellec0156792022-06-03 15:29:07 +0200982 struct h3c *h3c = h3s->h3c;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200983 ssize_t total = 0, ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100984
Amaury Denoyelle14037bf2023-02-17 15:56:06 +0100985 TRACE_ENTER(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100986
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200987 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +0100988 if ((ret = h3_init_uni_stream(h3c, qcs, b)) < 0) {
989 TRACE_ERROR("cannot initialize uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
990 goto err;
991 }
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200992
993 total += ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200994 }
995
996 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
997 /* For non-h3 STREAM, parse it and return immediately. */
Amaury Denoyelle14037bf2023-02-17 15:56:06 +0100998 if ((ret = h3_parse_uni_stream_no_h3(qcs, b, fin)) < 0) {
999 TRACE_ERROR("error when parsing non-HTTP3 uni stream", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
1000 goto err;
1001 }
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001002
1003 total += ret;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001004 goto done;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001005 }
1006
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001007 /* RFC 9114 6.2.1. Control Streams
1008 *
1009 * The sender MUST NOT close the control stream, and the receiver MUST NOT
1010 * request that the sender close the control stream. If either control
1011 * stream is closed at any point, this MUST be treated as a connection
1012 * error of type H3_CLOSED_CRITICAL_STREAM.
1013 */
1014 if (h3s->type == H3S_T_CTRL && fin) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001015 TRACE_ERROR("control stream closed by remote peer", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001016 qcc_emit_cc_app(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001017 goto err;
Amaury Denoyelle6b02c6b2022-08-16 17:16:47 +02001018 }
1019
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001020 if (!b_data(b) && fin && quic_stream_is_bidi(qcs->id)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001021 TRACE_PROTO("received FIN without data", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001022 qcs_http_handle_standalone_fin(qcs);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001023 goto done;
Amaury Denoyelle381d8132023-02-17 09:51:20 +01001024 }
1025
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001026 while (b_data(b) && !(qcs->flags & QC_SF_DEM_FULL) && !h3c->err && !h3s->err) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001027 uint64_t ftype, flen;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +01001028 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001029
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001030 if (!h3s->demux_frame_len) {
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001031 /* Switch to a new frame. */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001032 size_t hlen = h3_decode_frm_header(&ftype, &flen, b);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001033 if (!hlen) {
1034 TRACE_PROTO("pause parsing on incomplete frame header", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001035 break;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001036 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001037
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001038 h3s->demux_frame_type = ftype;
1039 h3s->demux_frame_len = flen;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001040 total += hlen;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001041 TRACE_PROTO("parsing a new frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001042
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001043 /* Check that content-length is not exceeded on a new DATA frame. */
1044 if (ftype == H3_FT_DATA) {
1045 h3s->data_len += flen;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001046 if (h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, fin))
1047 break;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001048 }
1049
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001050 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001051 TRACE_ERROR("received an invalid frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001052 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001053 goto err;
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001054 }
1055
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001056 if (!b_data(b))
Amaury Denoyelle417c7c02022-05-31 14:18:33 +02001057 break;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001058 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +01001059
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001060 flen = h3s->demux_frame_len;
1061 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001062
1063 /* Do not demux incomplete frames except H3 DATA which can be
1064 * fragmented in multiple HTX blocks.
1065 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001066 if (flen > b_data(b) && ftype != H3_FT_DATA) {
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001067 /* Reject frames bigger than bufsize.
1068 *
1069 * TODO HEADERS should in complement be limited with H3
1070 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
1071 * excessive decompressed size.
1072 */
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001073 if (flen > QC_S_RX_BUF_SZ) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001074 TRACE_ERROR("received a too big frame", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001075 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001076 goto err;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001077 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001078 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +02001079 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001080
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001081 /* Check content-length equality with DATA frames length on the last frame. */
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001082 if (fin && h3s->flags & H3_SF_HAVE_CLEN && h3_check_body_size(qcs, fin))
1083 break;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001084
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001085 last_stream_frame = (fin && flen == b_data(b));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +01001086
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001087 h3_inc_frame_type_cnt(h3c->prx_counters, ftype);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001088 switch (ftype) {
1089 case H3_FT_DATA:
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001090 ret = h3_data_to_htx(qcs, b, flen, last_stream_frame);
Amaury Denoyelle8d818c62022-08-02 11:32:45 +02001091 h3s->st_req = H3S_ST_REQ_DATA;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001092 break;
1093 case H3_FT_HEADERS:
Amaury Denoyelleedfcb552023-01-13 16:40:31 +01001094 if (h3s->st_req == H3S_ST_REQ_BEFORE) {
1095 ret = h3_headers_to_htx(qcs, b, flen, last_stream_frame);
1096 h3s->st_req = H3S_ST_REQ_HEADERS;
1097 }
1098 else {
1099 ret = h3_trailers_to_htx(qcs, b, flen, last_stream_frame);
1100 h3s->st_req = H3S_ST_REQ_TRAILERS;
1101 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001102 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001103 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001104 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001105 case H3_FT_MAX_PUSH_ID:
1106 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001107 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001108 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001109 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001110 case H3_FT_SETTINGS:
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001111 ret = h3_parse_settings_frm(qcs->qcc->ctx, b, flen);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +02001112 if (ret < 0) {
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001113 TRACE_ERROR("error on SETTINGS parsing", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelled666d742022-07-13 15:15:58 +02001114 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001115 goto err;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +02001116 }
1117 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001118 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001119 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +01001120 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +02001121 *
1122 * Implementations MUST discard frames [...] that have unknown
1123 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +01001124 */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +02001125 ret = flen;
1126 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001127 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +02001128
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001129 if (ret > 0) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +02001130 BUG_ON(h3s->demux_frame_len < ret);
1131 h3s->demux_frame_len -= ret;
Amaury Denoyelle62eef852022-06-03 16:40:34 +02001132 b_del(b, ret);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001133 total += ret;
Amaury Denoyelle291ee252022-05-02 10:35:39 +02001134 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001135 }
1136
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001137 /* Reset demux frame type for traces. */
1138 if (!h3s->demux_frame_len)
1139 h3s->demux_frame_type = H3_FT_UNINIT;
1140
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001141 /* Interrupt decoding on stream/connection error detected. */
1142 if (h3s->err) {
1143 qcc_abort_stream_read(qcs);
1144 qcc_reset_stream(qcs, h3s->err);
1145 return b_data(b);
1146 }
1147 else if (h3c->err) {
1148 qcc_emit_cc_app(qcs->qcc, h3c->err, 1);
1149 return b_data(b);
1150 }
1151
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02001152 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
1153 * However, currently, io-cb of MUX does not handle Rx.
1154 */
1155
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001156 done:
1157 TRACE_LEAVE(H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +02001158 return total;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001159
1160 err:
1161 TRACE_DEVEL("leaving on error", H3_EV_RX_FRAME, qcs->qcc->conn, qcs);
1162 return -1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001163}
1164
Amaury Denoyellea5871362021-10-07 16:26:12 +02001165/* Returns buffer for data sending.
1166 * May be NULL if the allocation failed.
1167 */
1168static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001169{
Amaury Denoyellea5871362021-10-07 16:26:12 +02001170 if (!b_size(&qcs->tx.buf))
1171 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001172
Amaury Denoyellea5871362021-10-07 16:26:12 +02001173 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001174}
1175
Amaury Denoyelle6b923942022-05-23 14:25:53 +02001176/* Function used to emit stream data from <qcs> control uni-stream */
1177static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001178{
1179 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001180 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001181 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +02001182 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001183 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001184
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001185 TRACE_ENTER(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
1186
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001187 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001188
1189 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +02001190 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001191
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001192 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
1193 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
1194 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
1195 quic_int_getsize(h3_settings_qpack_blocked_streams);
1196 if (h3_settings_max_field_section_size) {
1197 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
1198 quic_int_getsize(h3_settings_max_field_section_size);
1199 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001200
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001201 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001202 /* Build a SETTINGS frame */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001203 b_quic_enc_int(&pos, H3_FT_SETTINGS, 0);
1204 b_quic_enc_int(&pos, frm_len, 0);
1205 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0);
1206 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity, 0);
1207 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS, 0);
1208 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001209 if (h3_settings_max_field_section_size) {
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001210 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE, 0);
1211 b_quic_enc_int(&pos, h3_settings_max_field_section_size, 0);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001212 }
Amaury Denoyellea5871362021-10-07 16:26:12 +02001213
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001214 res = mux_get_buf(qcs);
1215 if (b_room(res) < b_data(&pos)) {
1216 // TODO the mux should be put in blocked state, with
1217 // the stream in state waiting for settings to be sent
1218 ABORT_NOW();
1219 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001220
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001221 ret = b_force_xfer(res, &pos, b_data(&pos));
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001222 if (ret > 0) {
1223 /* Register qcs for sending before other streams. */
Amaury Denoyellef9b03262023-01-09 10:34:25 +01001224 qcc_send_stream(qcs, 1);
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +02001225 h3c->flags |= H3_CF_SETTINGS_SENT;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001226 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001227
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001228 TRACE_LEAVE(H3_EV_TX_SETTINGS, qcs->qcc->conn, qcs);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001229 return ret;
1230}
1231
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001232static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
1233{
1234 struct buffer outbuf;
1235 struct buffer headers_buf = BUF_NULL;
1236 struct buffer *res;
1237 struct http_hdr list[global.tune.max_http_hdr];
1238 struct htx_sl *sl;
1239 struct htx_blk *blk;
1240 enum htx_blk_type type;
1241 int frame_length_size; /* size in bytes of frame length varint field */
1242 int ret = 0;
1243 int hdr;
1244 int status = 0;
1245
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001246 TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1247
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001248 sl = NULL;
1249 hdr = 0;
1250 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1251 type = htx_get_blk_type(blk);
1252
1253 if (type == HTX_BLK_UNUSED)
1254 continue;
1255
1256 if (type == HTX_BLK_EOH)
1257 break;
1258
1259 if (type == HTX_BLK_RES_SL) {
1260 /* start-line -> HEADERS h3 frame */
1261 BUG_ON(sl);
1262 sl = htx_get_blk_ptr(htx, blk);
1263 /* TODO should be on h3 layer */
1264 status = sl->info.res.status;
1265 }
1266 else if (type == HTX_BLK_HDR) {
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +02001267 if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1))
Amaury Denoyellefa7fadc2022-06-15 15:52:27 +02001268 goto err;
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001269 list[hdr].n = htx_get_blk_name(htx, blk);
1270 list[hdr].v = htx_get_blk_value(htx, blk);
1271 hdr++;
1272 }
1273 else {
1274 ABORT_NOW();
1275 goto err;
1276 }
1277 }
1278
1279 BUG_ON(!sl);
1280
1281 list[hdr].n = ist("");
1282
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001283 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001284
1285 /* At least 5 bytes to store frame type + length as a varint max size */
1286 if (b_room(res) < 5)
1287 ABORT_NOW();
1288
1289 b_reset(&outbuf);
1290 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
1291 /* Start the headers after frame type + length */
1292 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
1293
1294 if (qpack_encode_field_section_line(&headers_buf))
1295 ABORT_NOW();
1296 if (qpack_encode_int_status(&headers_buf, status))
1297 ABORT_NOW();
1298
1299 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
1300 if (isteq(list[hdr].n, ist("")))
1301 break;
1302
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001303 /* RFC 9114 4.2. HTTP Fields
1304 *
1305 * An intermediary transforming an HTTP/1.x message to HTTP/3
1306 * MUST remove connection-specific header fields as discussed in
1307 * Section 7.6.1 of [HTTP], or their messages will be treated by
1308 * other HTTP/3 endpoints as malformed.
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001309 */
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001310 if (isteq(list[hdr].n, ist("connection")) ||
1311 isteq(list[hdr].n, ist("proxy-connection")) ||
1312 isteq(list[hdr].n, ist("keep-alive")) ||
1313 isteq(list[hdr].n, ist("transfer-encoding"))) {
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001314 continue;
Amaury Denoyelle8ad26692023-01-17 17:47:06 +01001315 }
1316 else if (isteq(list[hdr].n, ist("te"))) {
1317 /* "te" may only be sent with "trailers" if this value
1318 * is present, otherwise it must be deleted.
1319 */
1320 const struct ist v = istist(list[hdr].v, ist("trailers"));
1321 if (!isttest(v) || (v.len > 8 && v.ptr[8] != ','))
1322 continue;
1323 list[hdr].v = ist("trailers");
1324 }
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +01001325
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001326 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
1327 ABORT_NOW();
1328 }
1329
1330 /* Now that all headers are encoded, we are certain that res buffer is
1331 * big enough
1332 */
1333 frame_length_size = quic_int_getsize(b_data(&headers_buf));
1334 res->head += 4 - frame_length_size;
1335 b_putchr(res, 0x01); /* h3 HEADERS frame type */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001336 if (!b_quic_enc_int(res, b_data(&headers_buf), 0))
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001337 ABORT_NOW();
1338 b_add(res, b_data(&headers_buf));
1339
1340 ret = 0;
1341 blk = htx_get_head_blk(htx);
1342 while (blk) {
1343 type = htx_get_blk_type(blk);
1344 ret += htx_get_blksz(blk);
1345 blk = htx_remove_blk(htx, blk);
1346 if (type == HTX_BLK_EOH)
1347 break;
1348 }
1349
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001350 TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001351 return ret;
1352
1353 err:
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001354 TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001355 return 0;
1356}
1357
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001358/* Convert a series of HTX trailer blocks from <htx> buffer into <qcs> buffer
1359 * as a H3 HEADERS frame. H3 forbidden trailers are skipped. HTX trailer blocks
1360 * are removed from <htx> until EOT is found and itself removed.
1361 *
1362 * If only a EOT HTX block is present without trailer, no H3 frame is produced.
1363 * Caller is responsible to emit an empty QUIC STREAM frame to signal the end
1364 * of the stream.
1365 *
1366 * Returns the size of HTX blocks removed.
1367 */
1368static int h3_resp_trailers_send(struct qcs *qcs, struct htx *htx)
1369{
1370 struct buffer headers_buf = BUF_NULL;
1371 struct buffer *res;
1372 struct http_hdr list[global.tune.max_http_hdr];
1373 struct htx_blk *blk;
1374 enum htx_blk_type type;
1375 char *tail;
1376 int ret = 0;
1377 int hdr;
1378
1379 TRACE_ENTER(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1380
1381 hdr = 0;
1382 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
1383 type = htx_get_blk_type(blk);
1384
1385 if (type == HTX_BLK_UNUSED)
1386 continue;
1387
1388 if (type == HTX_BLK_EOT)
1389 break;
1390
1391 if (type == HTX_BLK_TLR) {
1392 if (unlikely(hdr >= sizeof(list) / sizeof(list[0]) - 1))
1393 goto err;
1394 list[hdr].n = htx_get_blk_name(htx, blk);
1395 list[hdr].v = htx_get_blk_value(htx, blk);
1396 hdr++;
1397 }
1398 else {
1399 TRACE_ERROR("unexpected HTX block", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1400 goto err;
1401 }
1402 }
1403
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001404 if (!hdr) {
1405 /* No headers encoded here so no need to generate a H3 HEADERS
1406 * frame. Mux will send an empty QUIC STREAM frame with FIN.
1407 */
1408 TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1409 goto end;
1410 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001411 list[hdr].n = ist("");
1412
1413 res = mux_get_buf(qcs);
1414
1415 /* At least 9 bytes to store frame type + length as a varint max size */
1416 if (b_room(res) < 9) {
1417 qcs->flags |= QC_SF_BLK_MROOM;
1418 goto err;
1419 }
1420
1421 /* Force buffer realignment as size required to encode headers is unknown. */
1422 if (b_space_wraps(res))
1423 b_slow_realign(res, trash.area, b_data(res));
1424 /* Start the headers after frame type + length */
1425 headers_buf = b_make(b_peek(res, b_data(res) + 9), b_contig_space(res) - 9, 0, 0);
1426
Amaury Denoyelle224ba5c2023-01-26 17:41:58 +01001427 if (qpack_encode_field_section_line(&headers_buf)) {
1428 qcs->flags |= QC_SF_BLK_MROOM;
1429 goto err;
1430 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001431
1432 tail = b_tail(&headers_buf);
1433 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
1434 if (isteq(list[hdr].n, ist("")))
1435 break;
1436
1437 /* forbidden HTTP/3 headers, cf h3_resp_headers_send() */
1438 if (isteq(list[hdr].n, ist("host")) ||
1439 isteq(list[hdr].n, ist("content-length")) ||
1440 isteq(list[hdr].n, ist("connection")) ||
1441 isteq(list[hdr].n, ist("proxy-connection")) ||
1442 isteq(list[hdr].n, ist("keep-alive")) ||
1443 isteq(list[hdr].n, ist("te")) ||
1444 isteq(list[hdr].n, ist("transfer-encoding"))) {
1445 continue;
1446 }
1447
Amaury Denoyelle224ba5c2023-01-26 17:41:58 +01001448 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v)) {
1449 qcs->flags |= QC_SF_BLK_MROOM;
1450 goto err;
1451 }
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001452 }
1453
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001454 /* Check that at least one header was encoded in buffer. */
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001455 if (b_tail(&headers_buf) == tail) {
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001456 /* No headers encoded here so no need to generate a H3 HEADERS
1457 * frame. Mux will send an empty QUIC STREAM frame with FIN.
1458 */
1459 TRACE_DATA("skipping trailer", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001460 goto end;
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001461 }
1462
Amaury Denoyelle4be54352023-01-26 17:49:21 +01001463 /* Now that all headers are encoded, we are certain that res buffer is
1464 * big enough.
1465 */
1466 b_putchr(res, 0x01); /* h3 HEADERS frame type */
1467 if (!b_quic_enc_int(res, b_data(&headers_buf), 8))
1468 ABORT_NOW();
1469 b_add(res, b_data(&headers_buf));
1470
1471 end:
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001472 ret = 0;
1473 blk = htx_get_head_blk(htx);
1474 while (blk) {
1475 type = htx_get_blk_type(blk);
1476 ret += htx_get_blksz(blk);
1477 blk = htx_remove_blk(htx, blk);
1478 if (type == HTX_BLK_EOT)
1479 break;
1480 }
1481
1482 TRACE_LEAVE(H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1483 return ret;
1484
1485 err:
1486 TRACE_DEVEL("leaving on error", H3_EV_TX_HDR, qcs->qcc->conn, qcs);
1487 return 0;
1488}
1489
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001490/* Returns the total of bytes sent. */
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001491static int h3_resp_data_send(struct qcs *qcs, struct htx *htx, size_t count)
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001492{
1493 struct buffer outbuf;
1494 struct buffer *res;
1495 size_t total = 0;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001496 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001497 struct htx_blk *blk;
1498 enum htx_blk_type type;
1499
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001500 TRACE_ENTER(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
1501
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001502 new_frame:
1503 if (!count || htx_is_empty(htx))
1504 goto end;
1505
1506 blk = htx_get_head_blk(htx);
1507 type = htx_get_blk_type(blk);
1508 fsize = bsize = htx_get_blksz(blk);
1509
1510 if (type != HTX_BLK_DATA)
1511 goto end;
1512
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001513 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001514
1515 if (fsize > count)
1516 fsize = count;
1517
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001518 /* h3 DATA headers : 1-byte frame type + varint frame length */
1519 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001520
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001521 while (1) {
1522 b_reset(&outbuf);
1523 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
1524 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
1525 break;
1526 b_slow_realign(res, trash.area, b_data(res));
1527 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001528
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001529 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001530 * stream. It is expected that the stream connector layer will subscribe
1531 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001532 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001533 if (b_size(&outbuf) <= hsize) {
1534 qcs->flags |= QC_SF_BLK_MROOM;
1535 goto end;
1536 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001537
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001538 if (b_size(&outbuf) < hsize + fsize)
1539 fsize = b_size(&outbuf) - hsize;
1540 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001541
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001542 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001543 b_quic_enc_int(&outbuf, fsize, 0); /* h3 frame length */
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001544
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001545 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001546 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001547 count -= fsize;
1548
1549 if (fsize == bsize)
1550 htx_remove_blk(htx, blk);
1551 else
1552 htx_cut_data_blk(htx, blk, fsize);
1553
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001554 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001555 b_add(res, b_data(&outbuf));
1556 goto new_frame;
1557
1558 end:
Amaury Denoyellea717eb72022-05-30 15:51:01 +02001559 TRACE_LEAVE(H3_EV_TX_DATA, qcs->qcc->conn, qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001560 return total;
1561}
1562
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001563static size_t h3_snd_buf(struct qcs *qcs, struct htx *htx, size_t count)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001564{
1565 size_t total = 0;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001566 enum htx_blk_type btype;
1567 struct htx_blk *blk;
1568 uint32_t bsize;
1569 int32_t idx;
1570 int ret;
1571
Amaury Denoyelled8769d12022-03-25 15:28:33 +01001572 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001573
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +01001574 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001575 idx = htx_get_head(htx);
1576 blk = htx_get_blk(htx, idx);
1577 btype = htx_get_blk_type(blk);
1578 bsize = htx_get_blksz(blk);
1579
1580 /* Not implemented : QUIC on backend side */
1581 BUG_ON(btype == HTX_BLK_REQ_SL);
1582
1583 switch (btype) {
1584 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +02001585 /* start-line -> HEADERS h3 frame */
1586 ret = h3_resp_headers_send(qcs, htx);
1587 if (ret > 0) {
1588 total += ret;
1589 count -= ret;
1590 if (ret < bsize)
1591 goto out;
1592 }
1593 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001594
1595 case HTX_BLK_DATA:
Amaury Denoyelle9534e592022-09-19 17:14:27 +02001596 ret = h3_resp_data_send(qcs, htx, count);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001597 if (ret > 0) {
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +02001598 total += ret;
1599 count -= ret;
1600 if (ret < bsize)
1601 goto out;
1602 }
1603 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001604
1605 case HTX_BLK_TLR:
1606 case HTX_BLK_EOT:
Amaury Denoyelle4e520102023-01-12 14:53:43 +01001607 ret = h3_resp_trailers_send(qcs, htx);
1608 if (ret > 0) {
1609 total += ret;
1610 count -= ret;
1611 if (ret < bsize)
1612 goto out;
1613 }
1614 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001615
1616 default:
1617 htx_remove_blk(htx, blk);
1618 total += bsize;
1619 count -= bsize;
1620 break;
1621 }
1622 }
1623
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001624 out:
1625 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +02001626}
1627
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001628/* Notify about a closure on <qcs> stream requested by the remote peer.
1629 *
1630 * Stream channel <side> is explained relative to our endpoint : WR for
1631 * STOP_SENDING or RD for RESET_STREAM reception. Callback decode_qcs() is used
1632 * instead for closure performed using a STREAM frame with FIN bit.
1633 *
1634 * The main objective of this function is to check if closure is valid
1635 * according to HTTP/3 specification.
1636 *
1637 * Returns 0 on success else non-zero. A CONNECTION_CLOSE is generated on
1638 * error.
1639 */
1640static int h3_close(struct qcs *qcs, enum qcc_app_ops_close_side side)
1641{
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001642 struct h3s *h3s = qcs->ctx;
1643 struct h3c *h3c = h3s->h3c;;
1644
1645 /* RFC 9114 6.2.1. Control Streams
1646 *
1647 * The sender
1648 * MUST NOT close the control stream, and the receiver MUST NOT
1649 * request that the sender close the control stream. If either
1650 * control stream is closed at any point, this MUST be treated
1651 * as a connection error of type H3_CLOSED_CRITICAL_STREAM.
1652 */
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001653 if (qcs == h3c->ctrl_strm || h3s->type == H3S_T_CTRL) {
Amaury Denoyellee31867b2023-01-31 16:01:22 +01001654 TRACE_ERROR("closure detected on control stream", H3_EV_H3S_END, qcs->qcc->conn, qcs);
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001655 qcc_emit_cc_app(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
1656 return 1;
1657 }
1658
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001659 return 0;
1660}
1661
Amaury Denoyellec0156792022-06-03 15:29:07 +02001662static int h3_attach(struct qcs *qcs, void *conn_ctx)
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001663{
1664 struct h3s *h3s;
1665
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001666 TRACE_ENTER(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
1667
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001668 h3s = pool_alloc(pool_head_h3s);
1669 if (!h3s)
1670 return 1;
1671
1672 qcs->ctx = h3s;
Amaury Denoyellec0156792022-06-03 15:29:07 +02001673 h3s->h3c = conn_ctx;
1674
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001675 h3s->demux_frame_len = 0;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001676 h3s->demux_frame_type = H3_FT_UNINIT;
Amaury Denoyelled2c5ee62022-12-08 16:54:42 +01001677 h3s->body_len = 0;
1678 h3s->data_len = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +02001679 h3s->flags = 0;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001680 h3s->err = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +02001681
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001682 if (quic_stream_is_bidi(qcs->id)) {
1683 h3s->type = H3S_T_REQ;
Amaury Denoyelle8d818c62022-08-02 11:32:45 +02001684 h3s->st_req = H3S_ST_REQ_BEFORE;
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001685 qcs_wait_http_req(qcs);
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +02001686 }
1687 else {
1688 /* stream type must be decoded for unidirectional streams */
1689 h3s->type = H3S_T_UNKNOWN;
1690 }
1691
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001692 TRACE_LEAVE(H3_EV_H3S_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001693 return 0;
1694}
1695
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001696static void h3_detach(struct qcs *qcs)
1697{
1698 struct h3s *h3s = qcs->ctx;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001699
1700 TRACE_ENTER(H3_EV_H3S_END, qcs->qcc->conn, qcs);
1701
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001702 pool_free(pool_head_h3s, h3s);
1703 qcs->ctx = NULL;
Amaury Denoyelled5581d52022-05-30 15:51:31 +02001704
1705 TRACE_LEAVE(H3_EV_H3S_END, qcs->qcc->conn, qcs);
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001706}
1707
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001708/* Initialize H3 control stream and prepare SETTINGS emission.
1709 *
1710 * Returns 0 on success else non-zero.
1711 */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001712static int h3_finalize(void *ctx)
1713{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001714 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001715 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001716
Amaury Denoyelleb1437232022-07-08 11:53:22 +02001717 qcs = qcc_init_stream_local(h3c->qcc, 0);
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001718 if (!qcs)
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001719 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001720
Amaury Denoyelle9cc47512022-05-24 16:27:41 +02001721 h3_control_send(qcs, h3c);
Amaury Denoyelled7010392022-07-13 15:17:29 +02001722 h3c->ctrl_strm = qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001723
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001724 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001725}
1726
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001727/* Generate a GOAWAY frame for <h3c> connection on the control stream.
1728 *
1729 * Returns 0 on success else non-zero.
1730 */
1731static int h3_send_goaway(struct h3c *h3c)
1732{
1733 struct qcs *qcs = h3c->ctrl_strm;
1734 struct buffer pos, *res;
1735 unsigned char data[3 * QUIC_VARINT_MAX_SIZE];
1736 size_t frm_len = quic_int_getsize(h3c->id_goaway);
1737
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001738 TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001739
1740 if (!qcs) {
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001741 TRACE_ERROR("control stream not initialized", H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001742 goto err;
1743 }
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001744
1745 pos = b_make((char *)data, sizeof(data), 0, 0);
1746
Amaury Denoyelle7d78eff2023-01-17 15:21:16 +01001747 b_quic_enc_int(&pos, H3_FT_GOAWAY, 0);
1748 b_quic_enc_int(&pos, frm_len, 0);
1749 b_quic_enc_int(&pos, h3c->id_goaway, 0);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001750
1751 res = mux_get_buf(qcs);
1752 if (!res || b_room(res) < b_data(&pos)) {
1753 /* Do not try forcefully to emit GOAWAY if no space left. */
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001754 TRACE_ERROR("cannot send GOAWAY", H3_EV_H3C_END, h3c->qcc->conn, qcs);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001755 goto err;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001756 }
1757
1758 b_force_xfer(res, &pos, b_data(&pos));
Amaury Denoyelle19adeb52023-01-25 10:50:03 +01001759 qcc_send_stream(qcs, 1);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001760
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001761 TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001762 return 0;
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001763
1764 err:
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001765 TRACE_DEVEL("leaving in error", H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001766 return 1;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001767}
1768
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001769/* Initialize the HTTP/3 context for <qcc> mux.
1770 * Return 1 if succeeded, 0 if not.
1771 */
1772static int h3_init(struct qcc *qcc)
1773{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001774 struct h3c *h3c;
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001775 struct quic_conn *qc = qcc->conn->handle.qc;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001776
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001777 h3c = pool_alloc(pool_head_h3c);
1778 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001779 goto fail_no_h3;
1780
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001781 h3c->qcc = qcc;
Amaury Denoyelled7010392022-07-13 15:17:29 +02001782 h3c->ctrl_strm = NULL;
Amaury Denoyelle2fe93ab2022-12-09 15:01:31 +01001783 h3c->err = 0;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001784 h3c->flags = 0;
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001785 h3c->id_goaway = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001786
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001787 qcc->ctx = h3c;
Amaury Denoyelle5c25dc52022-09-30 17:44:15 +02001788 /* TODO cleanup only ref to quic_conn */
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001789 h3c->prx_counters =
1790 EXTRA_COUNTERS_GET(qc->li->bind_conf->frontend->extra_counters_fe,
1791 &h3_stats_module);
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001792 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001793
1794 return 1;
1795
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001796 fail_no_h3:
1797 return 0;
1798}
1799
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001800/* Send a HTTP/3 GOAWAY followed by a CONNECTION_CLOSE_APP. */
1801static void h3_shutdown(void *ctx)
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001802{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001803 struct h3c *h3c = ctx;
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001804
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001805 TRACE_ENTER(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001806
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001807 /* RFC 9114 5.2. Connection Shutdown
1808 *
Amaury Denoyelle114c9c82022-03-28 14:53:45 +02001809 * Even when a connection is not idle, either endpoint can decide to
1810 * stop using the connection and initiate a graceful connection close.
1811 * Endpoints initiate the graceful shutdown of an HTTP/3 connection by
1812 * sending a GOAWAY frame.
1813 */
1814 h3_send_goaway(h3c);
1815
1816 /* RFC 9114 5.2. Connection Shutdown
1817 *
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001818 * An endpoint that completes a
1819 * graceful shutdown SHOULD use the H3_NO_ERROR error code when closing
1820 * the connection.
1821 */
1822 qcc_emit_cc_app(h3c->qcc, H3_NO_ERROR, 0);
Amaury Denoyelle56a86dd2023-01-30 15:36:51 +01001823
Amaury Denoyelle78adb4b2023-01-31 15:50:16 +01001824 TRACE_LEAVE(H3_EV_H3C_END, h3c->qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001825}
Amaury Denoyelle069288b2022-07-15 10:58:25 +02001826
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001827static void h3_release(void *ctx)
1828{
1829 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001830 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001831}
1832
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001833/* Increment the h3 error code counters for <error_code> value */
1834static void h3_stats_inc_err_cnt(void *ctx, int err_code)
1835{
1836 struct h3c *h3c = ctx;
1837
1838 h3_inc_err_cnt(h3c->prx_counters, err_code);
1839}
1840
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001841static inline const char *h3_ft_str(int type)
1842{
1843 switch (type) {
1844 case H3_FT_DATA: return "DATA";
1845 case H3_FT_HEADERS: return "HEADERS";
1846 case H3_FT_SETTINGS: return "SETTINGS";
1847 case H3_FT_PUSH_PROMISE: return "PUSH_PROMISE";
1848 case H3_FT_MAX_PUSH_ID: return "MAX_PUSH_ID";
1849 case H3_FT_CANCEL_PUSH: return "CANCEL_PUSH";
1850 case H3_FT_GOAWAY: return "GOAWAY";
1851 default: return "_UNKNOWN_";
1852 }
1853}
1854
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001855/* h3 trace handler */
1856static void h3_trace(enum trace_level level, uint64_t mask,
1857 const struct trace_source *src,
1858 const struct ist where, const struct ist func,
1859 const void *a1, const void *a2, const void *a3, const void *a4)
1860{
1861 const struct connection *conn = a1;
1862 const struct qcc *qcc = conn ? conn->ctx : NULL;
1863 const struct qcs *qcs = a2;
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001864 const struct h3s *h3s = qcs ? qcs->ctx : NULL;
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001865
Frédéric Lécaille1c725aa2022-09-08 15:49:37 +02001866 if (!qcc)
1867 return;
1868
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001869 if (src->verbosity > H3_VERB_CLEAN) {
1870 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
Frédéric Lécaille2eb5faa2022-09-08 16:03:13 +02001871 if (qcc->conn->handle.qc)
1872 chunk_appendf(&trace_buf, " qc=%p", qcc->conn->handle.qc);
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001873
1874 if (qcs)
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001875 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id);
Amaury Denoyelle14037bf2023-02-17 15:56:06 +01001876
1877 if (h3s && h3s->demux_frame_type != H3_FT_UNINIT) {
1878 chunk_appendf(&trace_buf, " h3s.dem=%s/%d",
1879 h3_ft_str(h3s->demux_frame_type), h3s->demux_frame_len);
1880 }
Amaury Denoyelle016aa932022-05-30 15:49:36 +02001881 }
1882}
1883
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001884/* HTTP/3 application layer operations */
1885const struct qcc_app_ops h3_ops = {
1886 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001887 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001888 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001889 .snd_buf = h3_snd_buf,
Amaury Denoyelle1e340ba2023-01-30 12:12:11 +01001890 .close = h3_close,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001891 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001892 .finalize = h3_finalize,
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001893 .shutdown = h3_shutdown,
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02001894 .inc_err_cnt = h3_stats_inc_err_cnt,
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001895 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001896};