blob: f30d478ecbd9f12ed5ac2e3fb276283849f9f030 [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
19#include <haproxy/buf.h>
Amaury Denoyelle99043552021-08-24 15:36:02 +020020#include <haproxy/connection.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010021#include <haproxy/dynbuf.h>
22#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020023#include <haproxy/http.h>
24#include <haproxy/htx.h>
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +020025#include <haproxy/intops.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010026#include <haproxy/istbuf.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +020027#include <haproxy/mux_quic.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020028#include <haproxy/ncbuf.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010029#include <haproxy/pool.h>
30#include <haproxy/qpack-dec.h>
Amaury Denoyelle15b09612021-08-24 16:20:27 +020031#include <haproxy/qpack-enc.h>
32#include <haproxy/quic_enc.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020033#include <haproxy/stconn.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010034#include <haproxy/tools.h>
35#include <haproxy/xprt_quic.h>
36
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010037#if defined(DEBUG_H3)
38#define h3_debug_printf fprintf
39#define h3_debug_hexdump debug_hexdump
40#else
41#define h3_debug_printf(...) do { } while (0)
42#define h3_debug_hexdump(...) do { } while (0)
43#endif
44
Amaury Denoyelle302ecd42022-05-24 15:24:32 +020045#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
46#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
47#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
48#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
49#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010050
51/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020052static uint64_t h3_settings_qpack_max_table_capacity = 0;
53static uint64_t h3_settings_qpack_blocked_streams = 4096;
54static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010055
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020056struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010057 struct qcc *qcc;
58 enum h3_err err;
59 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020060
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010061 /* Settings */
62 uint64_t qpack_max_table_capacity;
63 uint64_t qpack_blocked_streams;
64 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020065
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010066 struct buffer_wait buf_wait; /* wait list for buffer allocations */
67};
68
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020069DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010070
Amaury Denoyelle35550642022-05-24 15:14:53 +020071#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +020072#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +020073
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020074struct h3s {
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +020075 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020076 int demux_frame_len;
77 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +020078
79 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020080};
81
82DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
83
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010084/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +020085static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010086{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020087 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010088}
89
Amaury Denoyelle35550642022-05-24 15:14:53 +020090/* Initialize an uni-stream <qcs> by reading its type from <rxbuf>.
91 *
92 * Returns 0 on success else non-zero.
93 */
94static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
95 struct ncbuf *rxbuf)
96{
97 /* decode unidirectional stream type */
98 struct h3s *h3s = qcs->ctx;
99 struct buffer b;
100 uint64_t type;
101 size_t len = 0, ret;
102
103 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
104 h3s->flags & H3_SF_UNI_INIT);
105
106 b = h3_b_dup(rxbuf);
107 ret = b_quic_dec_int(&type, &b, &len);
108 if (!ret) {
109 ABORT_NOW();
110 }
111
112 switch (type) {
113 case H3_UNI_S_T_CTRL:
114 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
115 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
116 return 1;
117 }
118 h3c->flags |= H3_CF_UNI_CTRL_SET;
119 h3s->type = H3S_T_CTRL;
120 break;
121
122 case H3_UNI_S_T_PUSH:
123 /* TODO not supported for the moment */
124 h3s->type = H3S_T_PUSH;
125 break;
126
127 case H3_UNI_S_T_QPACK_DEC:
128 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
129 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
130 return 1;
131 }
132 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
133 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200134 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200135 break;
136
137 case H3_UNI_S_T_QPACK_ENC:
138 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
139 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
140 return 1;
141 }
142 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
143 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200144 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200145 break;
146
147 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200148 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
149 *
150 * Implementations MUST [...] abort reading on unidirectional
151 * streams that have unknown or unsupported types.
152 */
153 qcs->flags |= QC_SF_READ_ABORTED;
154 return 1;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200155 };
156
157 h3s->flags |= H3_SF_UNI_INIT;
158 qcs_consume(qcs, len);
159
160 return 0;
161}
162
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200163/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
164 * This may be used for QPACK encoder/decoder streams for example.
165 *
166 * Returns 0 on success else non-zero.
167 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200168static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200169{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200170 struct h3s *h3s = qcs->ctx;
171
172 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
173 !(h3s->flags & H3_SF_UNI_NO_H3));
174
175 switch (h3s->type) {
176 case H3S_T_QPACK_DEC:
177 if (!qpack_decode_dec(qcs, NULL))
178 return 1;
179 break;
180 case H3S_T_QPACK_ENC:
181 if (!qpack_decode_enc(qcs, NULL))
182 return 1;
183 break;
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200184 case H3S_T_UNKNOWN:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200185 default:
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200186 /* Unknown stream should be flagged with QC_SF_READ_ABORTED. */
187 ABORT_NOW();
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200188 }
189
190 return 0;
191}
192
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100193/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
194 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
195 * Note that this function update <b> buffer to reflect the number of bytes consumed
196 * to decode the h3 frame header.
197 */
198static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
199 struct buffer *b)
200{
201 size_t hlen;
202
203 hlen = 0;
204 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
205 return 0;
206
207 return hlen;
208}
209
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200210/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
211 *
212 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
213 * be reported.
214 */
215static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
216{
217 struct h3s *h3s = qcs->ctx;
218 const uint64_t id = qcs->id;
219
220 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
221
222 switch (ftype) {
223 case H3_FT_DATA:
224 case H3_FT_HEADERS:
225 return h3s->type != H3S_T_CTRL;
226
227 case H3_FT_CANCEL_PUSH:
228 case H3_FT_GOAWAY:
229 case H3_FT_MAX_PUSH_ID:
230 /* Only allowed for control stream. First frame of control
231 * stream MUST be SETTINGS.
232 */
233 return h3s->type == H3S_T_CTRL &&
234 (h3c->flags & H3_CF_SETTINGS_RECV);
235
236 case H3_FT_SETTINGS:
237 /* draft-ietf-quic-http34 7.2.4. SETTINGS
238 *
239 * If an endpoint receives a second SETTINGS frame on the control
240 * stream, the endpoint MUST respond with a connection error of type
241 * H3_FRAME_UNEXPECTED.
242 */
243 return h3s->type == H3S_T_CTRL &&
244 !(h3c->flags & H3_CF_SETTINGS_RECV);
245
246 case H3_FT_PUSH_PROMISE:
247 return h3s->type != H3S_T_CTRL &&
248 (id & QCS_ID_SRV_INTIATOR_BIT);
249
250 default:
251 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
252 *
253 * Implementations MUST discard frames [...] that have unknown
254 * or unsupported types.
255 */
256 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
257 }
258}
259
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100260/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
Willy Tarreau4596fe22022-05-17 19:07:51 +0200261 * in a local HTX buffer and transfer to the stream connector layer. <fin> must be
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100262 * set if this is the last data to transfer from this stream.
263 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200264 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100265 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200266static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100267 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100268{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100269 struct buffer htx_buf = BUF_NULL;
270 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100271 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200272 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200273 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200274 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100275 struct ist meth = IST_NULL, path = IST_NULL;
276 //struct ist scheme = IST_NULL, authority = IST_NULL;
277 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200278 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100279
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200280 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200281 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
282 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200283 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100284
285 qc_get_buf(qcs, &htx_buf);
286 BUG_ON(!b_size(&htx_buf));
287 htx = htx_from_buf(&htx_buf);
288
289 /* first treat pseudo-header to build the start line */
290 hdr_idx = 0;
291 while (1) {
292 if (isteq(list[hdr_idx].n, ist("")))
293 break;
294
295 if (istmatch(list[hdr_idx].n, ist(":"))) {
296 /* pseudo-header */
297 if (isteq(list[hdr_idx].n, ist(":method")))
298 meth = list[hdr_idx].v;
299 else if (isteq(list[hdr_idx].n, ist(":path")))
300 path = list[hdr_idx].v;
301 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
302 // scheme = list[hdr_idx].v;
303 else if (isteq(list[hdr_idx].n, ist(":authority")))
304 authority = list[hdr_idx].v;
305 }
306
307 ++hdr_idx;
308 }
309
310 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200311 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100312
313 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
314 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200315 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100316
317 if (fin)
318 sl->flags |= HTX_SL_F_BODYLESS;
319
320 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
321 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
322
323 if (isttest(authority))
324 htx_add_header(htx, ist("host"), authority);
325
326 /* now treat standard headers */
327 hdr_idx = 0;
328 while (1) {
329 if (isteq(list[hdr_idx].n, ist("")))
330 break;
331
332 if (!istmatch(list[hdr_idx].n, ist(":")))
333 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
334
335 ++hdr_idx;
336 }
337
338 htx_add_endof(htx, HTX_BLK_EOH);
339 htx_to_buf(htx, &htx_buf);
340
341 if (fin)
342 htx->flags |= HTX_FL_EOM;
343
Willy Tarreau3215e732022-05-27 10:09:11 +0200344 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200345 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100346
Willy Tarreau4596fe22022-05-17 19:07:51 +0200347 /* buffer is transferred to the stream connector and set to NULL
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100348 * except on stream creation error.
349 */
350 b_free(&htx_buf);
351 offer_buffers(NULL, 1);
352
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200353 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100354}
355
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100356/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
357 * HTX buffer. <fin> must be set if this is the last data to transfer from this
358 * stream.
359 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200360 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100361 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200362static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100363 char fin)
364{
365 struct buffer *appbuf;
366 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200367 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100368 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200369 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100370
371 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
372 BUG_ON(!appbuf);
373 htx = htx_from_buf(appbuf);
374
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200375 if (len > ncb_data(buf, 0)) {
376 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200377 fin = 0;
378 }
379
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200380 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200381 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100382 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200383 if (!htx_space) {
384 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200385 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200386 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200387
388 if (len > htx_space) {
389 len = htx_space;
390 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100391 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100392
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200393 if (head + len > ncb_wrap(buf)) {
394 size_t contig = ncb_wrap(buf) - head;
395 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200396 if (htx_sent < contig) {
397 qcs->flags |= QC_SF_DEM_FULL;
398 goto out;
399 }
400
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200401 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200402 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200403 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100404 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100405
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200406 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200407 if (htx_sent < len) {
408 qcs->flags |= QC_SF_DEM_FULL;
409 goto out;
410 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200411
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200412 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100413 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100414
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200415 out:
416 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200417 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100418}
419
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200420/* Parse a SETTINGS frame of length <len> of payload <rxbuf>.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200421 *
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200422 * Returns the number of bytes handled or a negative error code.
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200423 */
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200424static size_t h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf,
425 size_t len)
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200426{
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200427 struct buffer b;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200428 uint64_t id, value;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200429 size_t ret = 0;
430 long mask = 0; /* used to detect duplicated settings identifier */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200431
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200432 b = h3_b_dup(rxbuf);
433 b_set_data(&b, len);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200434
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200435 while (b_data(&b)) {
436 if (!b_quic_dec_int(&id, &b, &ret) || !b_quic_dec_int(&value, &b, &ret)) {
437 h3c->err = H3_FRAME_ERROR;
438 return -1;
439 }
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200440
441 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
442 __func__, (unsigned long long)id, (unsigned long long)value);
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200443
444 /* draft-ietf-quic-http34 7.2.4. SETTINGS
445 *
446 * The same setting identifier MUST NOT occur more than once in the
447 * SETTINGS frame. A receiver MAY treat the presence of duplicate
448 * setting identifiers as a connection error of type H3_SETTINGS_ERROR.
449 */
450
451 /* Ignore duplicate check for ID too big used for GREASE. */
452 if (id < sizeof(mask)) {
453 if (ha_bit_test(id, &mask)) {
454 h3c->err = H3_SETTINGS_ERROR;
455 return -1;
456 }
457 ha_bit_set(id, &mask);
458 }
459
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200460 switch (id) {
461 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
462 h3c->qpack_max_table_capacity = value;
463 break;
464 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
465 h3c->max_field_section_size = value;
466 break;
467 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
468 h3c->qpack_blocked_streams = value;
469 break;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200470
471 case H3_SETTINGS_RESERVED_0:
472 case H3_SETTINGS_RESERVED_2:
473 case H3_SETTINGS_RESERVED_3:
474 case H3_SETTINGS_RESERVED_4:
475 case H3_SETTINGS_RESERVED_5:
476 /* draft-ietf-quic-http34 7.2.4.1. Defined SETTINGS Parameters
477 *
478 * Setting identifiers which were defined in [HTTP2] where there is no
479 * corresponding HTTP/3 setting have also been reserved
480 * (Section 11.2.2). These reserved settings MUST NOT be sent, and
481 * their receipt MUST be treated as a connection error of type
482 * H3_SETTINGS_ERROR.
483 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200484 h3c->err = H3_SETTINGS_ERROR;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200485 return -1;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200486 default:
487 /* MUST be ignored */
488 break;
489 }
490 }
491
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200492 return ret;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200493}
494
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100495/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
496 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200497 *
498 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100499 */
500static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
501{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200502 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200503 struct h3c *h3c = ctx;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200504 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200505 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100506
Amaury Denoyellebb970422022-04-12 16:40:52 +0200507 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200508 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100509 return 0;
510
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200511 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
512 if (h3_init_uni_stream(h3c, qcs, rxbuf))
513 return 1;
514 }
515
516 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
517 /* For non-h3 STREAM, parse it and return immediately. */
518 if (h3_parse_uni_stream_no_h3(qcs, rxbuf))
519 return 1;
520 return 0;
521 }
522
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200523 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100524 uint64_t ftype, flen;
525 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100526 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100527
528 /* Work on a copy of <rxbuf> */
529 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200530 if (!h3s->demux_frame_len) {
531 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
532 if (!hlen)
533 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100534
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200535 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
536 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100537
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200538 h3s->demux_frame_type = ftype;
539 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200540 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200541 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100542
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200543 flen = h3s->demux_frame_len;
544 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200545
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200546 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
547 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED);
548 return 1;
549 }
550
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200551 /* Do not demux incomplete frames except H3 DATA which can be
552 * fragmented in multiple HTX blocks.
553 */
554 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
555 /* Reject frames bigger than bufsize.
556 *
557 * TODO HEADERS should in complement be limited with H3
558 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
559 * excessive decompressed size.
560 */
561 if (flen > ncb_size(rxbuf)) {
562 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
563 return 1;
564 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200565 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200566 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200567
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200568 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100569
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100570 switch (ftype) {
571 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100572 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
573 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200574 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100575 break;
576 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100577 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
578 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200579 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100580 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200581 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100582 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200583 case H3_FT_MAX_PUSH_ID:
584 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100585 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200586 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100587 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200588 case H3_FT_SETTINGS:
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200589 ret = h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen);
590 if (ret < 0) {
591 qcc_emit_cc_app(qcs->qcc, h3c->err);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200592 return 1;
Amaury Denoyelle8c6176b2022-05-24 18:16:49 +0200593 }
594 h3c->flags |= H3_CF_SETTINGS_RECV;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200595 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100596 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100597 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200598 *
599 * Implementations MUST discard frames [...] that have unknown
600 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100601 */
602 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200603 ret = flen;
604 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100605 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200606
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200607 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200608 BUG_ON(h3s->demux_frame_len < ret);
609 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200610 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200611 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100612 }
613
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200614 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
615 * However, currently, io-cb of MUX does not handle Rx.
616 */
617
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100618 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100619}
620
Amaury Denoyellea5871362021-10-07 16:26:12 +0200621/* Returns buffer for data sending.
622 * May be NULL if the allocation failed.
623 */
624static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100625{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200626 if (!b_size(&qcs->tx.buf))
627 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100628
Amaury Denoyellea5871362021-10-07 16:26:12 +0200629 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100630}
631
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200632/* Function used to emit stream data from <qcs> control uni-stream */
633static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100634{
635 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200636 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100637 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200638 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200639 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200640
641 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100642
643 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200644 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100645
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200646 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
647 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
648 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
649 quic_int_getsize(h3_settings_qpack_blocked_streams);
650 if (h3_settings_max_field_section_size) {
651 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
652 quic_int_getsize(h3_settings_max_field_section_size);
653 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100654
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200655 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
656 /* Build a SETTINGS frame */
657 b_quic_enc_int(&pos, H3_FT_SETTINGS);
658 b_quic_enc_int(&pos, frm_len);
659 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
660 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
661 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
662 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
663 if (h3_settings_max_field_section_size) {
664 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
665 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
666 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200667
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200668 res = mux_get_buf(qcs);
669 if (b_room(res) < b_data(&pos)) {
670 // TODO the mux should be put in blocked state, with
671 // the stream in state waiting for settings to be sent
672 ABORT_NOW();
673 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100674
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200675 ret = b_force_xfer(res, &pos, b_data(&pos));
676 if (ret > 0) {
677 h3c->flags |= H3_CF_SETTINGS_SENT;
678 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
679 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100680 }
681
682 return ret;
683}
684
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200685static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
686{
687 struct buffer outbuf;
688 struct buffer headers_buf = BUF_NULL;
689 struct buffer *res;
690 struct http_hdr list[global.tune.max_http_hdr];
691 struct htx_sl *sl;
692 struct htx_blk *blk;
693 enum htx_blk_type type;
694 int frame_length_size; /* size in bytes of frame length varint field */
695 int ret = 0;
696 int hdr;
697 int status = 0;
698
699 sl = NULL;
700 hdr = 0;
701 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
702 type = htx_get_blk_type(blk);
703
704 if (type == HTX_BLK_UNUSED)
705 continue;
706
707 if (type == HTX_BLK_EOH)
708 break;
709
710 if (type == HTX_BLK_RES_SL) {
711 /* start-line -> HEADERS h3 frame */
712 BUG_ON(sl);
713 sl = htx_get_blk_ptr(htx, blk);
714 /* TODO should be on h3 layer */
715 status = sl->info.res.status;
716 }
717 else if (type == HTX_BLK_HDR) {
718 list[hdr].n = htx_get_blk_name(htx, blk);
719 list[hdr].v = htx_get_blk_value(htx, blk);
720 hdr++;
721 }
722 else {
723 ABORT_NOW();
724 goto err;
725 }
726 }
727
728 BUG_ON(!sl);
729
730 list[hdr].n = ist("");
731
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200732 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200733
734 /* At least 5 bytes to store frame type + length as a varint max size */
735 if (b_room(res) < 5)
736 ABORT_NOW();
737
738 b_reset(&outbuf);
739 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
740 /* Start the headers after frame type + length */
741 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
742
743 if (qpack_encode_field_section_line(&headers_buf))
744 ABORT_NOW();
745 if (qpack_encode_int_status(&headers_buf, status))
746 ABORT_NOW();
747
748 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
749 if (isteq(list[hdr].n, ist("")))
750 break;
751
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100752 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
753 * Transfer codings (see Section 6.1 of [HTTP11]) are not
754 * defined for HTTP/3; the Transfer-Encoding header field MUST
755 * NOT be used.
756 */
757 if (isteq(list[hdr].n, ist("transfer-encoding")))
758 continue;
759
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200760 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
761 ABORT_NOW();
762 }
763
764 /* Now that all headers are encoded, we are certain that res buffer is
765 * big enough
766 */
767 frame_length_size = quic_int_getsize(b_data(&headers_buf));
768 res->head += 4 - frame_length_size;
769 b_putchr(res, 0x01); /* h3 HEADERS frame type */
770 if (!b_quic_enc_int(res, b_data(&headers_buf)))
771 ABORT_NOW();
772 b_add(res, b_data(&headers_buf));
773
774 ret = 0;
775 blk = htx_get_head_blk(htx);
776 while (blk) {
777 type = htx_get_blk_type(blk);
778 ret += htx_get_blksz(blk);
779 blk = htx_remove_blk(htx, blk);
780 if (type == HTX_BLK_EOH)
781 break;
782 }
783
784 return ret;
785
786 err:
787 return 0;
788}
789
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200790/* Returns the total of bytes sent. */
791static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
792{
793 struct buffer outbuf;
794 struct buffer *res;
795 size_t total = 0;
796 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200797 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200798 struct htx_blk *blk;
799 enum htx_blk_type type;
800
801 htx = htx_from_buf(buf);
802
803 new_frame:
804 if (!count || htx_is_empty(htx))
805 goto end;
806
807 blk = htx_get_head_blk(htx);
808 type = htx_get_blk_type(blk);
809 fsize = bsize = htx_get_blksz(blk);
810
811 if (type != HTX_BLK_DATA)
812 goto end;
813
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200814 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200815
816 if (fsize > count)
817 fsize = count;
818
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200819 /* h3 DATA headers : 1-byte frame type + varint frame length */
820 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200821
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200822 while (1) {
823 b_reset(&outbuf);
824 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
825 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
826 break;
827 b_slow_realign(res, trash.area, b_data(res));
828 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200829
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100830 /* Not enough room for headers and at least one data byte, block the
Willy Tarreau4596fe22022-05-17 19:07:51 +0200831 * stream. It is expected that the stream connector layer will subscribe
832 * on SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200833 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100834 if (b_size(&outbuf) <= hsize) {
835 qcs->flags |= QC_SF_BLK_MROOM;
836 goto end;
837 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200838
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200839 if (b_size(&outbuf) < hsize + fsize)
840 fsize = b_size(&outbuf) - hsize;
841 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200842
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200843 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
844 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
845
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200846 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200847 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200848 count -= fsize;
849
850 if (fsize == bsize)
851 htx_remove_blk(htx, blk);
852 else
853 htx_cut_data_blk(htx, blk, fsize);
854
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200855 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200856 b_add(res, b_data(&outbuf));
857 goto new_frame;
858
859 end:
860 return total;
861}
862
Willy Tarreau3215e732022-05-27 10:09:11 +0200863size_t h3_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags)
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200864{
865 size_t total = 0;
Willy Tarreau3215e732022-05-27 10:09:11 +0200866 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200867 struct htx *htx;
868 enum htx_blk_type btype;
869 struct htx_blk *blk;
870 uint32_t bsize;
871 int32_t idx;
872 int ret;
873
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100874 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100875
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200876 htx = htx_from_buf(buf);
877
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100878 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200879 idx = htx_get_head(htx);
880 blk = htx_get_blk(htx, idx);
881 btype = htx_get_blk_type(blk);
882 bsize = htx_get_blksz(blk);
883
884 /* Not implemented : QUIC on backend side */
885 BUG_ON(btype == HTX_BLK_REQ_SL);
886
887 switch (btype) {
888 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200889 /* start-line -> HEADERS h3 frame */
890 ret = h3_resp_headers_send(qcs, htx);
891 if (ret > 0) {
892 total += ret;
893 count -= ret;
894 if (ret < bsize)
895 goto out;
896 }
897 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200898
899 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200900 ret = h3_resp_data_send(qcs, buf, count);
901 if (ret > 0) {
902 htx = htx_from_buf(buf);
903 total += ret;
904 count -= ret;
905 if (ret < bsize)
906 goto out;
907 }
908 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200909
910 case HTX_BLK_TLR:
911 case HTX_BLK_EOT:
912 /* TODO trailers */
913
914 default:
915 htx_remove_blk(htx, blk);
916 total += bsize;
917 count -= bsize;
918 break;
919 }
920 }
921
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100922 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
923 qcs->flags |= QC_SF_FIN_STREAM;
924
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200925 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200926 if (total) {
927 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
928 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
929 }
930
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200931 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200932}
933
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200934static int h3_attach(struct qcs *qcs)
935{
936 struct h3s *h3s;
937
938 h3s = pool_alloc(pool_head_h3s);
939 if (!h3s)
940 return 1;
941
942 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200943 h3s->demux_frame_len = 0;
944 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200945 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200946
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200947 if (quic_stream_is_bidi(qcs->id)) {
948 h3s->type = H3S_T_REQ;
949 }
950 else {
951 /* stream type must be decoded for unidirectional streams */
952 h3s->type = H3S_T_UNKNOWN;
953 }
954
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200955 return 0;
956}
957
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200958static void h3_detach(struct qcs *qcs)
959{
960 struct h3s *h3s = qcs->ctx;
961 pool_free(pool_head_h3s, h3s);
962 qcs->ctx = NULL;
963}
964
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100965static int h3_finalize(void *ctx)
966{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200967 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200968 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100969
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200970 qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
971 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100972 return 0;
973
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200974 h3_control_send(qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100975
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100976 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100977}
978
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100979/* Initialize the HTTP/3 context for <qcc> mux.
980 * Return 1 if succeeded, 0 if not.
981 */
982static int h3_init(struct qcc *qcc)
983{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200984 struct h3c *h3c;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100985
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200986 h3c = pool_alloc(pool_head_h3c);
987 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100988 goto fail_no_h3;
989
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200990 h3c->qcc = qcc;
991 h3c->err = H3_NO_ERROR;
992 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100993
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200994 qcc->ctx = h3c;
995 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100996
997 return 1;
998
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100999 fail_no_h3:
1000 return 0;
1001}
1002
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001003static void h3_release(void *ctx)
1004{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001005 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +02001006 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001007}
1008
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001009/* Check if the H3 connection can still be considered as active.
1010 *
1011 * Return true if active else false.
1012 */
1013static int h3_is_active(const struct qcc *qcc, void *ctx)
1014{
1015 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
1016 return 1;
1017
1018 return 0;
1019}
1020
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001021/* HTTP/3 application layer operations */
1022const struct qcc_app_ops h3_ops = {
1023 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001024 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001025 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001026 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +02001027 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001028 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001029 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +02001030 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +01001031};