blob: a792851abb6e4ccc84e3ee69bf87c65af1a2d0c9 [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>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021#include <haproxy/conn_stream.h>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010022#include <haproxy/dynbuf.h>
23#include <haproxy/h3.h>
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +020024#include <haproxy/http.h>
25#include <haproxy/htx.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>
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010033#include <haproxy/tools.h>
34#include <haproxy/xprt_quic.h>
35
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010036#if defined(DEBUG_H3)
37#define h3_debug_printf fprintf
38#define h3_debug_hexdump debug_hexdump
39#else
40#define h3_debug_printf(...) do { } while (0)
41#define h3_debug_hexdump(...) do { } while (0)
42#endif
43
Amaury Denoyelle302ecd42022-05-24 15:24:32 +020044#define H3_CF_SETTINGS_SENT 0x00000001 /* SETTINGS frame already sent on local control stream */
45#define H3_CF_SETTINGS_RECV 0x00000002 /* SETTINGS frame already received on remote control stream */
46#define H3_CF_UNI_CTRL_SET 0x00000004 /* Remote H3 Control stream opened */
47#define H3_CF_UNI_QPACK_DEC_SET 0x00000008 /* Remote QPACK decoder stream opened */
48#define H3_CF_UNI_QPACK_ENC_SET 0x00000010 /* Remote QPACK encoder stream opened */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010049
50/* Default settings */
Amaury Denoyelle33949392021-08-24 15:16:58 +020051static uint64_t h3_settings_qpack_max_table_capacity = 0;
52static uint64_t h3_settings_qpack_blocked_streams = 4096;
53static uint64_t h3_settings_max_field_section_size = QUIC_VARINT_8_BYTE_MAX; /* Unlimited */
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010054
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020055struct h3c {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010056 struct qcc *qcc;
57 enum h3_err err;
58 uint32_t flags;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020059
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010060 /* Settings */
61 uint64_t qpack_max_table_capacity;
62 uint64_t qpack_blocked_streams;
63 uint64_t max_field_section_size;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +020064
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010065 struct buffer_wait buf_wait; /* wait list for buffer allocations */
66};
67
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +020068DECLARE_STATIC_POOL(pool_head_h3c, "h3c", sizeof(struct h3c));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010069
Amaury Denoyelle35550642022-05-24 15:14:53 +020070#define H3_SF_UNI_INIT 0x00000001 /* stream type not parsed for unidirectional stream */
Amaury Denoyellefc99a692022-05-24 15:25:19 +020071#define H3_SF_UNI_NO_H3 0x00000002 /* unidirectional stream does not carry H3 frames */
Amaury Denoyelle35550642022-05-24 15:14:53 +020072
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020073struct h3s {
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +020074 enum h3s_t type;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +020075 int demux_frame_len;
76 int demux_frame_type;
Amaury Denoyelle35550642022-05-24 15:14:53 +020077
78 int flags;
Amaury Denoyelle67e92d32022-04-27 18:04:01 +020079};
80
81DECLARE_STATIC_POOL(pool_head_h3s, "h3s", sizeof(struct h3s));
82
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010083/* Simple function to duplicate a buffer */
Amaury Denoyellec7dd9d62022-05-24 18:14:28 +020084static inline struct buffer h3_b_dup(const struct ncbuf *b)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010085{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020086 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
Frédéric Lécailleccac11f2021-03-03 16:09:02 +010087}
88
Amaury Denoyelle35550642022-05-24 15:14:53 +020089/* Initialize an uni-stream <qcs> by reading its type from <rxbuf>.
90 *
91 * Returns 0 on success else non-zero.
92 */
93static int h3_init_uni_stream(struct h3c *h3c, struct qcs *qcs,
94 struct ncbuf *rxbuf)
95{
96 /* decode unidirectional stream type */
97 struct h3s *h3s = qcs->ctx;
98 struct buffer b;
99 uint64_t type;
100 size_t len = 0, ret;
101
102 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
103 h3s->flags & H3_SF_UNI_INIT);
104
105 b = h3_b_dup(rxbuf);
106 ret = b_quic_dec_int(&type, &b, &len);
107 if (!ret) {
108 ABORT_NOW();
109 }
110
111 switch (type) {
112 case H3_UNI_S_T_CTRL:
113 if (h3c->flags & H3_CF_UNI_CTRL_SET) {
114 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
115 return 1;
116 }
117 h3c->flags |= H3_CF_UNI_CTRL_SET;
118 h3s->type = H3S_T_CTRL;
119 break;
120
121 case H3_UNI_S_T_PUSH:
122 /* TODO not supported for the moment */
123 h3s->type = H3S_T_PUSH;
124 break;
125
126 case H3_UNI_S_T_QPACK_DEC:
127 if (h3c->flags & H3_CF_UNI_QPACK_DEC_SET) {
128 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
129 return 1;
130 }
131 h3c->flags |= H3_CF_UNI_QPACK_DEC_SET;
132 h3s->type = H3S_T_QPACK_DEC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200133 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200134 break;
135
136 case H3_UNI_S_T_QPACK_ENC:
137 if (h3c->flags & H3_CF_UNI_QPACK_ENC_SET) {
138 qcc_emit_cc_app(qcs->qcc, H3_STREAM_CREATION_ERROR);
139 return 1;
140 }
141 h3c->flags |= H3_CF_UNI_QPACK_ENC_SET;
142 h3s->type = H3S_T_QPACK_ENC;
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200143 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200144 break;
145
146 default:
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200147 h3s->flags |= H3_SF_UNI_NO_H3;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200148 break;
149 };
150
151 h3s->flags |= H3_SF_UNI_INIT;
152 qcs_consume(qcs, len);
153
154 return 0;
155}
156
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200157/* Parse an uni-stream <qcs> from <rxbuf> which does not contains H3 frames.
158 * This may be used for QPACK encoder/decoder streams for example.
159 *
160 * Returns 0 on success else non-zero.
161 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200162static int h3_parse_uni_stream_no_h3(struct qcs *qcs, struct ncbuf *rxbuf)
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200163{
Amaury Denoyellefc99a692022-05-24 15:25:19 +0200164 struct h3s *h3s = qcs->ctx;
165
166 BUG_ON_HOT(!quic_stream_is_uni(qcs->id) ||
167 !(h3s->flags & H3_SF_UNI_NO_H3));
168
169 switch (h3s->type) {
170 case H3S_T_QPACK_DEC:
171 if (!qpack_decode_dec(qcs, NULL))
172 return 1;
173 break;
174 case H3S_T_QPACK_ENC:
175 if (!qpack_decode_enc(qcs, NULL))
176 return 1;
177 break;
178 default:
179 /* unknown uni stream : just consume it. */
180 qcs_consume(qcs, ncb_data(rxbuf, 0));
181 break;
182 }
183
184 return 0;
185}
186
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100187/* Decode a h3 frame header made of two QUIC varints from <b> buffer.
188 * Returns the number of bytes consumed if there was enough data in <b>, 0 if not.
189 * Note that this function update <b> buffer to reflect the number of bytes consumed
190 * to decode the h3 frame header.
191 */
192static inline size_t h3_decode_frm_header(uint64_t *ftype, uint64_t *flen,
193 struct buffer *b)
194{
195 size_t hlen;
196
197 hlen = 0;
198 if (!b_quic_dec_int(ftype, b, &hlen) || !b_quic_dec_int(flen, b, &hlen))
199 return 0;
200
201 return hlen;
202}
203
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200204/* Check if H3 frame of type <ftype> is valid when received on stream <qcs>.
205 *
206 * Returns a boolean. If false, a connection error H3_FRAME_UNEXPECTED should
207 * be reported.
208 */
209static int h3_is_frame_valid(struct h3c *h3c, struct qcs *qcs, uint64_t ftype)
210{
211 struct h3s *h3s = qcs->ctx;
212 const uint64_t id = qcs->id;
213
214 BUG_ON_HOT(h3s->type == H3S_T_UNKNOWN);
215
216 switch (ftype) {
217 case H3_FT_DATA:
218 case H3_FT_HEADERS:
219 return h3s->type != H3S_T_CTRL;
220
221 case H3_FT_CANCEL_PUSH:
222 case H3_FT_GOAWAY:
223 case H3_FT_MAX_PUSH_ID:
224 /* Only allowed for control stream. First frame of control
225 * stream MUST be SETTINGS.
226 */
227 return h3s->type == H3S_T_CTRL &&
228 (h3c->flags & H3_CF_SETTINGS_RECV);
229
230 case H3_FT_SETTINGS:
231 /* draft-ietf-quic-http34 7.2.4. SETTINGS
232 *
233 * If an endpoint receives a second SETTINGS frame on the control
234 * stream, the endpoint MUST respond with a connection error of type
235 * H3_FRAME_UNEXPECTED.
236 */
237 return h3s->type == H3S_T_CTRL &&
238 !(h3c->flags & H3_CF_SETTINGS_RECV);
239
240 case H3_FT_PUSH_PROMISE:
241 return h3s->type != H3S_T_CTRL &&
242 (id & QCS_ID_SRV_INTIATOR_BIT);
243
244 default:
245 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
246 *
247 * Implementations MUST discard frames [...] that have unknown
248 * or unsupported types.
249 */
250 return h3s->type != H3S_T_CTRL || (h3c->flags & H3_CF_SETTINGS_RECV);
251 }
252}
253
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100254/* Parse from buffer <buf> a H3 HEADERS frame of length <len>. Data are copied
255 * in a local HTX buffer and transfer to the conn-stream layer. <fin> must be
256 * set if this is the last data to transfer from this stream.
257 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200258 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100259 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200260static int h3_headers_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100261 char fin)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100262{
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100263 struct buffer htx_buf = BUF_NULL;
264 struct buffer *tmp = get_trash_chunk();
Amaury Denoyelle7059ebc2021-12-08 15:51:04 +0100265 struct htx *htx = NULL;
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200266 struct htx_sl *sl;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200267 struct http_hdr list[global.tune.max_http_hdr];
Amaury Denoyelleb49fa1a2021-08-24 15:30:12 +0200268 unsigned int flags = HTX_SL_F_NONE;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100269 struct ist meth = IST_NULL, path = IST_NULL;
270 //struct ist scheme = IST_NULL, authority = IST_NULL;
271 struct ist authority = IST_NULL;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200272 int hdr_idx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100273
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200274 /* TODO support buffer wrapping */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200275 BUG_ON(ncb_head(buf) + len >= ncb_wrap(buf));
276 if (qpack_decode_fs((const unsigned char *)ncb_head(buf), len, tmp, list) < 0)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200277 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100278
279 qc_get_buf(qcs, &htx_buf);
280 BUG_ON(!b_size(&htx_buf));
281 htx = htx_from_buf(&htx_buf);
282
283 /* first treat pseudo-header to build the start line */
284 hdr_idx = 0;
285 while (1) {
286 if (isteq(list[hdr_idx].n, ist("")))
287 break;
288
289 if (istmatch(list[hdr_idx].n, ist(":"))) {
290 /* pseudo-header */
291 if (isteq(list[hdr_idx].n, ist(":method")))
292 meth = list[hdr_idx].v;
293 else if (isteq(list[hdr_idx].n, ist(":path")))
294 path = list[hdr_idx].v;
295 //else if (isteq(list[hdr_idx].n, ist(":scheme")))
296 // scheme = list[hdr_idx].v;
297 else if (isteq(list[hdr_idx].n, ist(":authority")))
298 authority = list[hdr_idx].v;
299 }
300
301 ++hdr_idx;
302 }
303
304 flags |= HTX_SL_F_VER_11;
Amaury Denoyelle0fa14a62022-04-26 16:24:39 +0200305 flags |= HTX_SL_F_XFER_LEN;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100306
307 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, path, ist("HTTP/3.0"));
308 if (!sl)
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200309 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100310
311 if (fin)
312 sl->flags |= HTX_SL_F_BODYLESS;
313
314 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
315 BUG_ON(sl->info.req.meth == HTTP_METH_OTHER);
316
317 if (isttest(authority))
318 htx_add_header(htx, ist("host"), authority);
319
320 /* now treat standard headers */
321 hdr_idx = 0;
322 while (1) {
323 if (isteq(list[hdr_idx].n, ist("")))
324 break;
325
326 if (!istmatch(list[hdr_idx].n, ist(":")))
327 htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v);
328
329 ++hdr_idx;
330 }
331
332 htx_add_endof(htx, HTX_BLK_EOH);
333 htx_to_buf(htx, &htx_buf);
334
335 if (fin)
336 htx->flags |= HTX_FL_EOM;
337
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200338 if (!qc_attach_cs(qcs, &htx_buf))
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200339 return -1;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100340
341 /* buffer is transferred to conn_stream and set to NULL
342 * except on stream creation error.
343 */
344 b_free(&htx_buf);
345 offer_buffers(NULL, 1);
346
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200347 return len;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100348}
349
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100350/* Copy from buffer <buf> a H3 DATA frame of length <len> in QUIC stream <qcs>
351 * HTX buffer. <fin> must be set if this is the last data to transfer from this
352 * stream.
353 *
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200354 * Returns the number of bytes handled or a negative error code.
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100355 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200356static int h3_data_to_htx(struct qcs *qcs, struct ncbuf *buf, uint64_t len,
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100357 char fin)
358{
359 struct buffer *appbuf;
360 struct htx *htx = NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200361 size_t htx_sent = 0;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100362 int htx_space;
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200363 char *head;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100364
365 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
366 BUG_ON(!appbuf);
367 htx = htx_from_buf(appbuf);
368
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200369 if (len > ncb_data(buf, 0)) {
370 len = ncb_data(buf, 0);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200371 fin = 0;
372 }
373
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200374 head = ncb_head(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200375 retry:
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100376 htx_space = htx_free_data_space(htx);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200377 if (!htx_space) {
378 qcs->flags |= QC_SF_DEM_FULL;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200379 goto out;
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +0200380 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200381
382 if (len > htx_space) {
383 len = htx_space;
384 fin = 0;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100385 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100386
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200387 if (head + len > ncb_wrap(buf)) {
388 size_t contig = ncb_wrap(buf) - head;
389 htx_sent = htx_add_data(htx, ist2(ncb_head(buf), contig));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200390 if (htx_sent < contig) {
391 qcs->flags |= QC_SF_DEM_FULL;
392 goto out;
393 }
394
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200395 len -= contig;
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200396 head = ncb_orig(buf);
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200397 goto retry;
Amaury Denoyelleff191de2022-02-21 18:38:29 +0100398 }
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100399
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200400 htx_sent += htx_add_data(htx, ist2(head, len));
Amaury Denoyelle73d6ffe2022-05-16 13:54:31 +0200401 if (htx_sent < len) {
402 qcs->flags |= QC_SF_DEM_FULL;
403 goto out;
404 }
Amaury Denoyelle30f23f52022-04-27 14:41:53 +0200405
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200406 if (fin && len == htx_sent)
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100407 htx->flags |= HTX_FL_EOM;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100408
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200409 out:
410 htx_to_buf(htx, appbuf);
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200411 return htx_sent;
Amaury Denoyelle91379f72022-02-14 17:14:59 +0100412}
413
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200414/* Parse a SETTINGS frame which must not be truncated with <flen> as length from
415 * <rxbuf> buffer. This function does not update this buffer.
416 *
417 * Returns 0 on success else non-zero.
418 */
419static int h3_parse_settings_frm(struct h3c *h3c, const struct ncbuf *rxbuf, size_t flen)
420{
421 uint64_t id, value;
422 const unsigned char *buf, *end;
423
424 buf = (const unsigned char *)ncb_head(rxbuf);
425 end = buf + flen;
426
427 while (buf < end) {
428 if (!quic_dec_int(&id, &buf, end) || !quic_dec_int(&value, &buf, end))
429 return 1;
430
431 h3_debug_printf(stderr, "%s id: %llu value: %llu\n",
432 __func__, (unsigned long long)id, (unsigned long long)value);
433 switch (id) {
434 case H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY:
435 h3c->qpack_max_table_capacity = value;
436 break;
437 case H3_SETTINGS_MAX_FIELD_SECTION_SIZE:
438 h3c->max_field_section_size = value;
439 break;
440 case H3_SETTINGS_QPACK_BLOCKED_STREAMS:
441 h3c->qpack_blocked_streams = value;
442 break;
443 case H3_SETTINGS_RESERVED_2 ... H3_SETTINGS_RESERVED_5:
444 h3c->err = H3_SETTINGS_ERROR;
445 return 1;
446 default:
447 /* MUST be ignored */
448 break;
449 }
450 }
451
452 return 0;
453}
454
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100455/* Decode <qcs> remotely initiated bidi-stream. <fin> must be set to indicate
456 * that we received the last data of the stream.
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200457 *
458 * Returns 0 on success else non-zero.
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100459 */
460static int h3_decode_qcs(struct qcs *qcs, int fin, void *ctx)
461{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200462 struct ncbuf *rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200463 struct h3c *h3c = ctx;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200464 struct h3s *h3s = qcs->ctx;
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200465 ssize_t ret;
Amaury Denoyelle7b0f1222022-02-14 17:13:55 +0100466
Amaury Denoyellebb970422022-04-12 16:40:52 +0200467 h3_debug_printf(stderr, "%s: STREAM ID: %lu\n", __func__, qcs->id);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200468 if (!ncb_data(rxbuf, 0))
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100469 return 0;
470
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200471 if (quic_stream_is_uni(qcs->id) && !(h3s->flags & H3_SF_UNI_INIT)) {
472 if (h3_init_uni_stream(h3c, qcs, rxbuf))
473 return 1;
474 }
475
476 if (quic_stream_is_uni(qcs->id) && (h3s->flags & H3_SF_UNI_NO_H3)) {
477 /* For non-h3 STREAM, parse it and return immediately. */
478 if (h3_parse_uni_stream_no_h3(qcs, rxbuf))
479 return 1;
480 return 0;
481 }
482
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200483 while (ncb_data(rxbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100484 uint64_t ftype, flen;
485 struct buffer b;
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100486 char last_stream_frame = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100487
488 /* Work on a copy of <rxbuf> */
489 b = h3_b_dup(rxbuf);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200490 if (!h3s->demux_frame_len) {
491 size_t hlen = h3_decode_frm_header(&ftype, &flen, &b);
492 if (!hlen)
493 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100494
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200495 h3_debug_printf(stderr, "%s: ftype: %lu, flen: %lu\n",
496 __func__, ftype, flen);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100497
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200498 h3s->demux_frame_type = ftype;
499 h3s->demux_frame_len = flen;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200500 qcs_consume(qcs, hlen);
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200501 }
Amaury Denoyelle0484f922022-02-15 16:59:39 +0100502
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200503 flen = h3s->demux_frame_len;
504 ftype = h3s->demux_frame_type;
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200505
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200506 if (!h3_is_frame_valid(h3c, qcs, ftype)) {
507 qcc_emit_cc_app(qcs->qcc, H3_FRAME_UNEXPECTED);
508 return 1;
509 }
510
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200511 /* Do not demux incomplete frames except H3 DATA which can be
512 * fragmented in multiple HTX blocks.
513 */
514 if (flen > b_data(&b) && ftype != H3_FT_DATA) {
515 /* Reject frames bigger than bufsize.
516 *
517 * TODO HEADERS should in complement be limited with H3
518 * SETTINGS_MAX_FIELD_SECTION_SIZE parameter to prevent
519 * excessive decompressed size.
520 */
521 if (flen > ncb_size(rxbuf)) {
522 qcc_emit_cc_app(qcs->qcc, H3_EXCESSIVE_LOAD);
523 return 1;
524 }
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200525 break;
Amaury Denoyelleb5454d42022-05-12 16:56:16 +0200526 }
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200527
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200528 last_stream_frame = (fin && flen == ncb_total_data(rxbuf));
Amaury Denoyelle95b93a32022-02-14 15:49:53 +0100529
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100530 switch (ftype) {
531 case H3_FT_DATA:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100532 ret = h3_data_to_htx(qcs, rxbuf, flen, last_stream_frame);
533 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200534 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100535 break;
536 case H3_FT_HEADERS:
Amaury Denoyelle31e4f6e2022-02-15 17:30:27 +0100537 ret = h3_headers_to_htx(qcs, rxbuf, flen, last_stream_frame);
538 /* TODO handle error reporting. Stream closure required. */
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200539 if (ret < 0) { ABORT_NOW(); }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100540 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200541 case H3_FT_CANCEL_PUSH:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100542 case H3_FT_PUSH_PROMISE:
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200543 case H3_FT_MAX_PUSH_ID:
544 case H3_FT_GOAWAY:
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100545 /* Not supported */
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200546 ret = flen;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100547 break;
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +0200548 case H3_FT_SETTINGS:
549 if (h3_parse_settings_frm(qcs->qcc->ctx, rxbuf, flen))
550 return 1;
551 ret = flen;
552 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100553 default:
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100554 /* draft-ietf-quic-http34 9. Extensions to HTTP/3
Amaury Denoyelle302ecd42022-05-24 15:24:32 +0200555 *
556 * Implementations MUST discard frames [...] that have unknown
557 * or unsupported types.
Amaury Denoyelled1acaf92021-11-15 15:52:55 +0100558 */
559 h3_debug_printf(stderr, "ignore unknown frame type 0x%lx\n", ftype);
Amaury Denoyelle80097cc2022-05-24 11:13:46 +0200560 ret = flen;
561 break;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100562 }
Amaury Denoyelle314578a2022-04-27 14:52:52 +0200563
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200564 if (ret) {
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200565 BUG_ON(h3s->demux_frame_len < ret);
566 h3s->demux_frame_len -= ret;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200567 qcs_consume(qcs, ret);
Amaury Denoyelle291ee252022-05-02 10:35:39 +0200568 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100569 }
570
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +0200571 /* TODO may be useful to wakeup the MUX if blocked due to full buffer.
572 * However, currently, io-cb of MUX does not handle Rx.
573 */
574
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +0100575 return 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100576}
577
Amaury Denoyellea5871362021-10-07 16:26:12 +0200578/* Returns buffer for data sending.
579 * May be NULL if the allocation failed.
580 */
581static struct buffer *mux_get_buf(struct qcs *qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100582{
Amaury Denoyellea5871362021-10-07 16:26:12 +0200583 if (!b_size(&qcs->tx.buf))
584 b_alloc(&qcs->tx.buf);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100585
Amaury Denoyellea5871362021-10-07 16:26:12 +0200586 return &qcs->tx.buf;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100587}
588
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200589/* Function used to emit stream data from <qcs> control uni-stream */
590static int h3_control_send(struct qcs *qcs, void *ctx)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100591{
592 int ret;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200593 struct h3c *h3c = ctx;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100594 unsigned char data[(2 + 3) * 2 * QUIC_VARINT_MAX_SIZE]; /* enough for 3 settings */
Amaury Denoyellea5871362021-10-07 16:26:12 +0200595 struct buffer pos, *res;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200596 size_t frm_len;
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200597
598 BUG_ON_HOT(h3c->flags & H3_CF_SETTINGS_SENT);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100599
600 ret = 0;
Amaury Denoyellea5871362021-10-07 16:26:12 +0200601 pos = b_make((char *)data, sizeof(data), 0, 0);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100602
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200603 frm_len = quic_int_getsize(H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY) +
604 quic_int_getsize(h3_settings_qpack_max_table_capacity) +
605 quic_int_getsize(H3_SETTINGS_QPACK_BLOCKED_STREAMS) +
606 quic_int_getsize(h3_settings_qpack_blocked_streams);
607 if (h3_settings_max_field_section_size) {
608 frm_len += quic_int_getsize(H3_SETTINGS_MAX_FIELD_SECTION_SIZE) +
609 quic_int_getsize(h3_settings_max_field_section_size);
610 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100611
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200612 b_quic_enc_int(&pos, H3_UNI_S_T_CTRL);
613 /* Build a SETTINGS frame */
614 b_quic_enc_int(&pos, H3_FT_SETTINGS);
615 b_quic_enc_int(&pos, frm_len);
616 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_MAX_TABLE_CAPACITY);
617 b_quic_enc_int(&pos, h3_settings_qpack_max_table_capacity);
618 b_quic_enc_int(&pos, H3_SETTINGS_QPACK_BLOCKED_STREAMS);
619 b_quic_enc_int(&pos, h3_settings_qpack_blocked_streams);
620 if (h3_settings_max_field_section_size) {
621 b_quic_enc_int(&pos, H3_SETTINGS_MAX_FIELD_SECTION_SIZE);
622 b_quic_enc_int(&pos, h3_settings_max_field_section_size);
623 }
Amaury Denoyellea5871362021-10-07 16:26:12 +0200624
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200625 res = mux_get_buf(qcs);
626 if (b_room(res) < b_data(&pos)) {
627 // TODO the mux should be put in blocked state, with
628 // the stream in state waiting for settings to be sent
629 ABORT_NOW();
630 }
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100631
Amaury Denoyelle65df3ad2022-05-24 15:06:10 +0200632 ret = b_force_xfer(res, &pos, b_data(&pos));
633 if (ret > 0) {
634 h3c->flags |= H3_CF_SETTINGS_SENT;
635 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
636 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100637 }
638
639 return ret;
640}
641
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200642static int h3_resp_headers_send(struct qcs *qcs, struct htx *htx)
643{
644 struct buffer outbuf;
645 struct buffer headers_buf = BUF_NULL;
646 struct buffer *res;
647 struct http_hdr list[global.tune.max_http_hdr];
648 struct htx_sl *sl;
649 struct htx_blk *blk;
650 enum htx_blk_type type;
651 int frame_length_size; /* size in bytes of frame length varint field */
652 int ret = 0;
653 int hdr;
654 int status = 0;
655
656 sl = NULL;
657 hdr = 0;
658 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
659 type = htx_get_blk_type(blk);
660
661 if (type == HTX_BLK_UNUSED)
662 continue;
663
664 if (type == HTX_BLK_EOH)
665 break;
666
667 if (type == HTX_BLK_RES_SL) {
668 /* start-line -> HEADERS h3 frame */
669 BUG_ON(sl);
670 sl = htx_get_blk_ptr(htx, blk);
671 /* TODO should be on h3 layer */
672 status = sl->info.res.status;
673 }
674 else if (type == HTX_BLK_HDR) {
675 list[hdr].n = htx_get_blk_name(htx, blk);
676 list[hdr].v = htx_get_blk_value(htx, blk);
677 hdr++;
678 }
679 else {
680 ABORT_NOW();
681 goto err;
682 }
683 }
684
685 BUG_ON(!sl);
686
687 list[hdr].n = ist("");
688
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200689 res = mux_get_buf(qcs);
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200690
691 /* At least 5 bytes to store frame type + length as a varint max size */
692 if (b_room(res) < 5)
693 ABORT_NOW();
694
695 b_reset(&outbuf);
696 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
697 /* Start the headers after frame type + length */
698 headers_buf = b_make(b_head(res) + 5, b_size(res) - 5, 0, 0);
699
700 if (qpack_encode_field_section_line(&headers_buf))
701 ABORT_NOW();
702 if (qpack_encode_int_status(&headers_buf, status))
703 ABORT_NOW();
704
705 for (hdr = 0; hdr < sizeof(list) / sizeof(list[0]); ++hdr) {
706 if (isteq(list[hdr].n, ist("")))
707 break;
708
Amaury Denoyelleffafb3d2022-02-15 16:10:42 +0100709 /* draft-ietf-quic-http34 4.1. HTTP Message Exchanges
710 * Transfer codings (see Section 6.1 of [HTTP11]) are not
711 * defined for HTTP/3; the Transfer-Encoding header field MUST
712 * NOT be used.
713 */
714 if (isteq(list[hdr].n, ist("transfer-encoding")))
715 continue;
716
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200717 if (qpack_encode_header(&headers_buf, list[hdr].n, list[hdr].v))
718 ABORT_NOW();
719 }
720
721 /* Now that all headers are encoded, we are certain that res buffer is
722 * big enough
723 */
724 frame_length_size = quic_int_getsize(b_data(&headers_buf));
725 res->head += 4 - frame_length_size;
726 b_putchr(res, 0x01); /* h3 HEADERS frame type */
727 if (!b_quic_enc_int(res, b_data(&headers_buf)))
728 ABORT_NOW();
729 b_add(res, b_data(&headers_buf));
730
731 ret = 0;
732 blk = htx_get_head_blk(htx);
733 while (blk) {
734 type = htx_get_blk_type(blk);
735 ret += htx_get_blksz(blk);
736 blk = htx_remove_blk(htx, blk);
737 if (type == HTX_BLK_EOH)
738 break;
739 }
740
741 return ret;
742
743 err:
744 return 0;
745}
746
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200747/* Returns the total of bytes sent. */
748static int h3_resp_data_send(struct qcs *qcs, struct buffer *buf, size_t count)
749{
750 struct buffer outbuf;
751 struct buffer *res;
752 size_t total = 0;
753 struct htx *htx;
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200754 int bsize, fsize, hsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200755 struct htx_blk *blk;
756 enum htx_blk_type type;
757
758 htx = htx_from_buf(buf);
759
760 new_frame:
761 if (!count || htx_is_empty(htx))
762 goto end;
763
764 blk = htx_get_head_blk(htx);
765 type = htx_get_blk_type(blk);
766 fsize = bsize = htx_get_blksz(blk);
767
768 if (type != HTX_BLK_DATA)
769 goto end;
770
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200771 res = mux_get_buf(qcs);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200772
773 if (fsize > count)
774 fsize = count;
775
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200776 /* h3 DATA headers : 1-byte frame type + varint frame length */
777 hsize = 1 + QUIC_VARINT_MAX_SIZE;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200778
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200779 while (1) {
780 b_reset(&outbuf);
781 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
782 if (b_size(&outbuf) > hsize || !b_space_wraps(res))
783 break;
784 b_slow_realign(res, trash.area, b_data(res));
785 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200786
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100787 /* Not enough room for headers and at least one data byte, block the
788 * stream. It is expected that the conn-stream layer will subscribe on
789 * SEND.
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200790 */
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100791 if (b_size(&outbuf) <= hsize) {
792 qcs->flags |= QC_SF_BLK_MROOM;
793 goto end;
794 }
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200795
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200796 if (b_size(&outbuf) < hsize + fsize)
797 fsize = b_size(&outbuf) - hsize;
798 BUG_ON(fsize <= 0);
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200799
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200800 b_putchr(&outbuf, 0x00); /* h3 frame type = DATA */
801 b_quic_enc_int(&outbuf, fsize); /* h3 frame length */
802
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200803 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200804 total += fsize;
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200805 count -= fsize;
806
807 if (fsize == bsize)
808 htx_remove_blk(htx, blk);
809 else
810 htx_cut_data_blk(htx, blk, fsize);
811
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200812 /* commit the buffer */
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200813 b_add(res, b_data(&outbuf));
814 goto new_frame;
815
816 end:
817 return total;
818}
819
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200820size_t h3_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
821{
822 size_t total = 0;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100823 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200824 struct htx *htx;
825 enum htx_blk_type btype;
826 struct htx_blk *blk;
827 uint32_t bsize;
828 int32_t idx;
829 int ret;
830
Amaury Denoyelled8769d12022-03-25 15:28:33 +0100831 h3_debug_printf(stderr, "%s\n", __func__);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100832
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200833 htx = htx_from_buf(buf);
834
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100835 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200836 idx = htx_get_head(htx);
837 blk = htx_get_blk(htx, idx);
838 btype = htx_get_blk_type(blk);
839 bsize = htx_get_blksz(blk);
840
841 /* Not implemented : QUIC on backend side */
842 BUG_ON(btype == HTX_BLK_REQ_SL);
843
844 switch (btype) {
845 case HTX_BLK_RES_SL:
Amaury Denoyelle15b09612021-08-24 16:20:27 +0200846 /* start-line -> HEADERS h3 frame */
847 ret = h3_resp_headers_send(qcs, htx);
848 if (ret > 0) {
849 total += ret;
850 count -= ret;
851 if (ret < bsize)
852 goto out;
853 }
854 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200855
856 case HTX_BLK_DATA:
Amaury Denoyelle8e2a9982021-08-24 16:24:37 +0200857 ret = h3_resp_data_send(qcs, buf, count);
858 if (ret > 0) {
859 htx = htx_from_buf(buf);
860 total += ret;
861 count -= ret;
862 if (ret < bsize)
863 goto out;
864 }
865 break;
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200866
867 case HTX_BLK_TLR:
868 case HTX_BLK_EOT:
869 /* TODO trailers */
870
871 default:
872 htx_remove_blk(htx, blk);
873 total += bsize;
874 count -= bsize;
875 break;
876 }
877 }
878
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100879 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
880 qcs->flags |= QC_SF_FIN_STREAM;
881
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200882 out:
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200883 if (total) {
884 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
885 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
886 }
887
Amaury Denoyelle26dfd902021-08-24 16:33:53 +0200888 return total;
Amaury Denoyellef52151d2021-08-24 16:11:18 +0200889}
890
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200891static int h3_attach(struct qcs *qcs)
892{
893 struct h3s *h3s;
894
895 h3s = pool_alloc(pool_head_h3s);
896 if (!h3s)
897 return 1;
898
899 qcs->ctx = h3s;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200900 h3s->demux_frame_len = 0;
901 h3s->demux_frame_type = 0;
Amaury Denoyelle35550642022-05-24 15:14:53 +0200902 h3s->flags = 0;
Amaury Denoyelle48f01bd2022-04-27 15:37:20 +0200903
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200904 if (quic_stream_is_bidi(qcs->id)) {
905 h3s->type = H3S_T_REQ;
906 }
907 else {
908 /* stream type must be decoded for unidirectional streams */
909 h3s->type = H3S_T_UNKNOWN;
910 }
911
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200912 return 0;
913}
914
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200915static void h3_detach(struct qcs *qcs)
916{
917 struct h3s *h3s = qcs->ctx;
918 pool_free(pool_head_h3s, h3s);
919 qcs->ctx = NULL;
920}
921
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100922static int h3_finalize(void *ctx)
923{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200924 struct h3c *h3c = ctx;
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200925 struct qcs *qcs;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100926
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200927 qcs = qcs_new(h3c->qcc, 0x3, QCS_SRV_UNI);
928 if (!qcs)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100929 return 0;
930
Amaury Denoyelle9cc47512022-05-24 16:27:41 +0200931 h3_control_send(qcs, h3c);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100932
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100933 return 1;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100934}
935
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100936/* Initialize the HTTP/3 context for <qcc> mux.
937 * Return 1 if succeeded, 0 if not.
938 */
939static int h3_init(struct qcc *qcc)
940{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200941 struct h3c *h3c;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100942
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200943 h3c = pool_alloc(pool_head_h3c);
944 if (!h3c)
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100945 goto fail_no_h3;
946
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200947 h3c->qcc = qcc;
948 h3c->err = H3_NO_ERROR;
949 h3c->flags = 0;
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100950
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200951 qcc->ctx = h3c;
952 LIST_INIT(&h3c->buf_wait.list);
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100953
954 return 1;
955
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100956 fail_no_h3:
957 return 0;
958}
959
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200960static void h3_release(void *ctx)
961{
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200962 struct h3c *h3c = ctx;
Amaury Denoyelle8d1ecac2022-05-24 14:55:43 +0200963 pool_free(pool_head_h3c, h3c);
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200964}
965
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200966/* Check if the H3 connection can still be considered as active.
967 *
968 * Return true if active else false.
969 */
970static int h3_is_active(const struct qcc *qcc, void *ctx)
971{
972 if (qcc->strms[QCS_CLT_BIDI].nb_streams)
973 return 1;
974
975 return 0;
976}
977
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100978/* HTTP/3 application layer operations */
979const struct qcc_app_ops h3_ops = {
980 .init = h3_init,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200981 .attach = h3_attach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100982 .decode_qcs = h3_decode_qcs,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +0100983 .snd_buf = h3_snd_buf,
Amaury Denoyelle67e92d32022-04-27 18:04:01 +0200984 .detach = h3_detach,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100985 .finalize = h3_finalize,
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200986 .is_active = h3_is_active,
Amaury Denoyelle8347f272022-03-29 14:46:55 +0200987 .release = h3_release,
Frédéric Lécailleccac11f2021-03-03 16:09:02 +0100988};