blob: ed4027c5f1fd9cae0793f637d15c669a87c6a1b0 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020020#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
33static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
35static struct pool_head *pool2_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
45/* Flags indicating why writing to the demux is blocked. */
46#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
47#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
48#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
49#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
50#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
51#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
52#define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above
53
Willy Tarreau081d4722017-05-16 21:51:05 +020054/* other flags */
55#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
56#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020057#define H2_CF_HEADERS_SENT 0x00000400 // a HEADERS frame was sent
Willy Tarreau081d4722017-05-16 21:51:05 +020058
59
Willy Tarreau5ab6b572017-09-22 08:05:00 +020060/* H2 connection state, in h2c->st0 */
61enum h2_cs {
62 H2_CS_PREFACE, // init done, waiting for connection preface
63 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
64 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
65 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
66 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
67 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
68 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
69 H2_CS_ENTRIES // must be last
70} __attribute__((packed));
71
72/* H2 connection descriptor */
73struct h2c {
74 struct connection *conn;
75
76 enum h2_cs st0; /* mux state */
77 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
78
79 /* 16 bit hole here */
80 uint32_t flags; /* connection flags: H2_CF_* */
81 int32_t max_id; /* highest ID known on this connection, <0 before preface */
82 uint32_t rcvd_c; /* newly received data to ACK for the connection */
83 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
84
85 /* states for the demux direction */
86 struct hpack_dht *ddht; /* demux dynamic header table */
87 struct buffer *dbuf; /* demux buffer */
88
89 int32_t dsi; /* demux stream ID (<0 = idle) */
90 int32_t dfl; /* demux frame length (if dsi >= 0) */
91 int8_t dft; /* demux frame type (if dsi >= 0) */
92 int8_t dff; /* demux frame flags (if dsi >= 0) */
93 /* 16 bit hole here */
94 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
95
96 /* states for the mux direction */
97 struct buffer *mbuf; /* mux buffer */
98 int32_t msi; /* mux stream ID (<0 = idle) */
99 int32_t mfl; /* mux frame length (if dsi >= 0) */
100 int8_t mft; /* mux frame type (if dsi >= 0) */
101 int8_t mff; /* mux frame flags (if dsi >= 0) */
102 /* 16 bit hole here */
103 int32_t miw; /* mux initial window size for all new streams */
104 int32_t mws; /* mux window size. Can be negative. */
105 int32_t mfs; /* mux's max frame size */
106
Willy Tarreauea392822017-10-31 10:02:25 +0100107 int timeout; /* idle timeout duration in ticks */
108 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 struct eb_root streams_by_id; /* all active streams by their ID */
110 struct list send_list; /* list of blocked streams requesting to send */
111 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200112 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200113 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200114};
115
Willy Tarreau18312642017-10-11 07:57:07 +0200116/* H2 stream state, in h2s->st */
117enum h2_ss {
118 H2_SS_IDLE = 0, // idle
119 H2_SS_RLOC, // reserved(local)
120 H2_SS_RREM, // reserved(remote)
121 H2_SS_OPEN, // open
122 H2_SS_HREM, // half-closed(remote)
123 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200124 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
125 H2_SS_RESET, // closed after sending RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200126 H2_SS_CLOSED, // closed
127 H2_SS_ENTRIES // must be last
128} __attribute__((packed));
129
130/* HTTP/2 stream flags (32 bit), in h2s->flags */
131#define H2_SF_NONE 0x00000000
132#define H2_SF_ES_RCVD 0x00000001
133#define H2_SF_ES_SENT 0x00000002
134
135#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
136#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
137
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200138/* stream flags indicating the reason the stream is blocked */
139#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
140#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
141#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
142#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
143#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
144
Willy Tarreau454f9052017-10-26 19:40:35 +0200145/* stream flags indicating how data is supposed to be sent */
146#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
147#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
148
149/* step we're currently in when sending chunks. This is needed because we may
150 * have to transfer chunks as large as a full buffer so there's no room left
151 * for size nor crlf around.
152 */
153#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
154#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
155#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
156
157#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
158
Willy Tarreau18312642017-10-11 07:57:07 +0200159/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
160 * it is being processed in the internal HTTP representation (H1 for now).
161 */
162struct h2s {
163 struct conn_stream *cs;
164 struct h2c *h2c;
165 struct h1m req, res; /* request and response parser state for H1 */
166 struct eb32_node by_id; /* place in h2c's streams_by_id */
167 struct list list; /* position in active/blocked lists if blocked>0 */
168 int32_t id; /* stream ID */
169 uint32_t flags; /* H2_SF_* */
170 int mws; /* mux window size for this stream */
171 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
172 enum h2_ss st;
173};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200174
Willy Tarreauc6405142017-09-21 20:23:50 +0200175/* descriptor for an h2 frame header */
176struct h2_fh {
177 uint32_t len; /* length, host order, 24 bits */
178 uint32_t sid; /* stream id, host order, 31 bits */
179 uint8_t ft; /* frame type */
180 uint8_t ff; /* frame flags */
181};
182
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200183/* a few settings from the global section */
184static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200185static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200186static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200187
Willy Tarreau2a856182017-05-16 15:20:39 +0200188/* a dmumy closed stream */
189static const struct h2s *h2_closed_stream = &(const struct h2s){
190 .cs = NULL,
191 .h2c = NULL,
192 .st = H2_SS_CLOSED,
193 .id = 0,
194};
195
196/* and a dummy idle stream for use with any unannounced stream */
197static const struct h2s *h2_idle_stream = &(const struct h2s){
198 .cs = NULL,
199 .h2c = NULL,
200 .st = H2_SS_IDLE,
201 .id = 0,
202};
203
Willy Tarreauea392822017-10-31 10:02:25 +0100204static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200205
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200206/*****************************************************/
207/* functions below are for dynamic buffer management */
208/*****************************************************/
209
210/* re-enables receiving on mux <target> after a buffer was allocated. It returns
211 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
212 * if it's impossible to wake up and we prefer to be woken up later.
213 */
214static int h2_dbuf_available(void *target)
215{
216 struct h2c *h2c = target;
217
218 /* take the buffer now as we'll get scheduled waiting for ->wake() */
219 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200220 h2c->flags &= ~H2_CF_DEM_DALLOC;
221 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
222 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200223 return 1;
224 }
225 return 0;
226}
227
228static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
229{
230 struct buffer *buf = NULL;
231
232 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
233 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
234 h2c->dbuf_wait.target = h2c->conn;
235 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
236 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
237 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
238 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
239 __conn_xprt_stop_recv(h2c->conn);
240 }
241 return buf;
242}
243
244static inline void h2_release_dbuf(struct h2c *h2c)
245{
246 if (h2c->dbuf->size) {
247 b_free(&h2c->dbuf);
248 offer_buffers(h2c->dbuf_wait.target,
249 tasks_run_queue + applets_active_queue);
250 }
251}
252
Willy Tarreau14398122017-09-22 14:26:04 +0200253/* re-enables sending on mux <target> after a buffer was allocated. It returns
254 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
255 * if it's impossible to wake up and we prefer to be woken up later.
256 */
257static int h2_mbuf_available(void *target)
258{
259 struct h2c *h2c = target;
260
261 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
262 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200263 if (h2c->flags & H2_CF_MUX_MALLOC) {
264 h2c->flags &= ~H2_CF_MUX_MALLOC;
265 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
266 conn_xprt_want_send(h2c->conn);
267 }
268
269 if (h2c->flags & H2_CF_DEM_MROOM) {
270 h2c->flags &= ~H2_CF_DEM_MROOM;
271 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
272 conn_xprt_want_recv(h2c->conn);
273 }
274
Willy Tarreau14398122017-09-22 14:26:04 +0200275 /* FIXME: we should in fact call something like h2_update_poll()
276 * now to recompte the polling. For now it will be enough like
277 * this.
278 */
Willy Tarreau14398122017-09-22 14:26:04 +0200279 return 1;
280 }
281 return 0;
282}
283
284static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
285{
286 struct buffer *buf = NULL;
287
288 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
289 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
290 h2c->mbuf_wait.target = h2c;
291 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
292 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
293 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
294 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
295
296 /* FIXME: we should in fact only block the direction being
297 * currently used. For now it will be enough like this.
298 */
299 __conn_xprt_stop_send(h2c->conn);
300 __conn_xprt_stop_recv(h2c->conn);
301 }
302 return buf;
303}
304
305static inline void h2_release_mbuf(struct h2c *h2c)
306{
307 if (h2c->mbuf->size) {
308 b_free(&h2c->mbuf);
309 offer_buffers(h2c->mbuf_wait.target,
310 tasks_run_queue + applets_active_queue);
311 }
312}
313
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200314
Willy Tarreau62f52692017-10-08 23:01:42 +0200315/*****************************************************************/
316/* functions below are dedicated to the mux setup and management */
317/*****************************************************************/
318
Willy Tarreau32218eb2017-09-22 08:07:25 +0200319/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
320static int h2c_frt_init(struct connection *conn)
321{
322 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100323 struct task *t = NULL;
324 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200325
326 h2c = pool_alloc2(pool2_h2c);
327 if (!h2c)
328 goto fail;
329
Willy Tarreau3f133572017-10-31 19:21:06 +0100330
331 h2c->timeout = sess->fe->timeout.client;
Willy Tarreau33400292017-11-05 11:23:40 +0100332 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100333 if (tick_isset(h2c->timeout)) {
334 t = task_new(tid_bit);
335 if (!t)
336 goto fail;
337
338 h2c->task = t;
339 t->process = h2_timeout_task;
340 t->context = h2c;
341 t->expire = tick_add(now_ms, h2c->timeout);
342 }
Willy Tarreauea392822017-10-31 10:02:25 +0100343
Willy Tarreau32218eb2017-09-22 08:07:25 +0200344 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
345 if (!h2c->ddht)
346 goto fail;
347
348 /* Initialise the context. */
349 h2c->st0 = H2_CS_PREFACE;
350 h2c->conn = conn;
351 h2c->max_id = -1;
352 h2c->errcode = H2_ERR_NO_ERROR;
353 h2c->flags = H2_CF_NONE;
354 h2c->rcvd_c = 0;
355 h2c->rcvd_s = 0;
356
357 h2c->dbuf = &buf_empty;
358 h2c->dsi = -1;
359 h2c->msi = -1;
360 h2c->last_sid = -1;
361
362 h2c->mbuf = &buf_empty;
363 h2c->miw = 65535; /* mux initial window size */
364 h2c->mws = 65535; /* mux window size */
365 h2c->mfs = 16384; /* initial max frame size */
366 h2c->streams_by_id = EB_ROOT_UNIQUE;
367 LIST_INIT(&h2c->send_list);
368 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200369 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200370 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371 conn->mux_ctx = h2c;
372
Willy Tarreau3f133572017-10-31 19:21:06 +0100373 if (t)
374 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100376
Willy Tarreau32218eb2017-09-22 08:07:25 +0200377 /* mux->wake will be called soon to complete the operation */
378 return 0;
379 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100380 if (t)
381 task_free(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 pool_free2(pool2_h2c, h2c);
383 return -1;
384}
385
Willy Tarreau62f52692017-10-08 23:01:42 +0200386/* Initialize the mux once it's attached. For outgoing connections, the context
387 * is already initialized before installing the mux, so we detect incoming
388 * connections from the fact that the context is still NULL. Returns < 0 on
389 * error.
390 */
391static int h2_init(struct connection *conn)
392{
393 if (conn->mux_ctx) {
394 /* we don't support outgoing connections for now */
395 return -1;
396 }
397
Willy Tarreau32218eb2017-09-22 08:07:25 +0200398 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200399}
400
Willy Tarreau2373acc2017-10-12 17:35:14 +0200401/* returns the stream associated with id <id> or NULL if not found */
402static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
403{
404 struct eb32_node *node;
405
Willy Tarreau2a856182017-05-16 15:20:39 +0200406 if (id > h2c->max_id)
407 return (struct h2s *)h2_idle_stream;
408
Willy Tarreau2373acc2017-10-12 17:35:14 +0200409 node = eb32_lookup(&h2c->streams_by_id, id);
410 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200411 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200412
413 return container_of(node, struct h2s, by_id);
414}
415
Willy Tarreau62f52692017-10-08 23:01:42 +0200416/* release function for a connection. This one should be called to free all
417 * resources allocated to the mux.
418 */
419static void h2_release(struct connection *conn)
420{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 struct h2c *h2c = conn->mux_ctx;
422
423 LIST_DEL(&conn->list);
424
425 if (h2c) {
426 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200427 h2_release_dbuf(h2c);
428 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
429 LIST_DEL(&h2c->dbuf_wait.list);
430 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200431
432 h2_release_mbuf(h2c);
433 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
434 LIST_DEL(&h2c->mbuf_wait.list);
435 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
436
Willy Tarreauea392822017-10-31 10:02:25 +0100437 if (h2c->task) {
438 task_delete(h2c->task);
439 task_free(h2c->task);
440 h2c->task = NULL;
441 }
442
Willy Tarreau32218eb2017-09-22 08:07:25 +0200443 pool_free2(pool2_h2c, h2c);
444 }
445
446 conn->mux = NULL;
447 conn->mux_ctx = NULL;
448
449 conn_stop_tracking(conn);
450 conn_full_close(conn);
451 if (conn->destroy_cb)
452 conn->destroy_cb(conn);
453 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200454}
455
456
Willy Tarreau71681172017-10-23 14:39:06 +0200457/******************************************************/
458/* functions below are for the H2 protocol processing */
459/******************************************************/
460
461/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
462static inline int h2s_id(const struct h2s *h2s)
463{
464 return h2s ? h2s->id : 0;
465}
466
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200467/* returns true of the mux is currently busy as seen from stream <h2s> */
468static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
469{
470 if (h2c->msi < 0)
471 return 0;
472
473 if (h2c->msi == h2s_id(h2s))
474 return 0;
475
476 return 1;
477}
478
Willy Tarreau741d6df2017-10-17 08:00:59 +0200479/* marks an error on the connection */
480static inline void h2c_error(struct h2c *h2c, enum h2_err err)
481{
482 h2c->errcode = err;
483 h2c->st0 = H2_CS_ERROR;
484}
485
Willy Tarreau2e43f082017-10-17 08:03:59 +0200486/* marks an error on the stream */
487static inline void h2s_error(struct h2s *h2s, enum h2_err err)
488{
489 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
490 h2s->errcode = err;
491 h2s->st = H2_SS_ERROR;
492 if (h2s->cs)
493 h2s->cs->flags |= CS_FL_ERROR;
494 }
495}
496
Willy Tarreaue4820742017-07-27 13:37:23 +0200497/* writes the 24-bit frame size <len> at address <frame> */
498static inline void h2_set_frame_size(void *frame, uint32_t len)
499{
500 uint8_t *out = frame;
501
502 *out = len >> 16;
503 write_n16(out + 1, len);
504}
505
Willy Tarreau54c15062017-10-10 17:10:03 +0200506/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
507 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
508 * the caller's responsibility to verify that there are at least <bytes> bytes
509 * available in the buffer's input prior to calling this function.
510 */
511static inline void h2_get_buf_bytes(void *dst, size_t bytes,
512 const struct buffer *b, int o)
513{
514 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
515}
516
517static inline uint16_t h2_get_n16(const struct buffer *b, int o)
518{
519 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
520}
521
522static inline uint32_t h2_get_n32(const struct buffer *b, int o)
523{
524 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
525}
526
527static inline uint64_t h2_get_n64(const struct buffer *b, int o)
528{
529 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
530}
531
532
Willy Tarreau715d5312017-07-11 15:20:24 +0200533/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
534 * is not obvious. It turns out that H2 headers are neither aligned nor do they
535 * use regular sizes. And to add to the trouble, the buffer may wrap so each
536 * byte read must be checked. The header is formed like this :
537 *
538 * b0 b1 b2 b3 b4 b5..b8
539 * +----------+---------+--------+----+----+----------------------+
540 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
541 * +----------+---------+--------+----+----+----------------------+
542 *
543 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
544 * we get the sid properly aligned and ordered, and 16 bits of len properly
545 * ordered as well. The type and flags can be extracted using bit shifts from
546 * the word, and only one extra read is needed to fetch len[16:23].
547 * Returns zero if some bytes are missing, otherwise non-zero on success.
548 */
549static int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
550{
551 uint64_t w;
552
553 if (b->i < 9)
554 return 0;
555
556 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
557 h->len = *b->p << 16;
558 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
559 h->ff = w >> 32;
560 h->ft = w >> 40;
561 h->len += w >> 48;
562 return 1;
563}
564
565/* skip the next 9 bytes corresponding to the frame header possibly parsed by
566 * h2_peek_frame_hdr() above.
567 */
568static inline void h2_skip_frame_hdr(struct buffer *b)
569{
570 bi_del(b, 9);
571}
572
573/* same as above, automatically advances the buffer on success */
574static inline int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
575{
576 int ret;
577
578 ret = h2_peek_frame_hdr(b, h);
579 if (ret > 0)
580 h2_skip_frame_hdr(b);
581 return ret;
582}
583
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200584/* creates a new stream <id> on the h2c connection and returns it, or NULL in
585 * case of memory allocation error.
586 */
587static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
588{
589 struct conn_stream *cs;
590 struct h2s *h2s;
591
592 h2s = pool_alloc2(pool2_h2s);
593 if (!h2s)
594 goto out;
595
596 h2s->h2c = h2c;
597 h2s->mws = h2c->miw;
598 h2s->flags = H2_SF_NONE;
599 h2s->errcode = H2_ERR_NO_ERROR;
600 h2s->st = H2_SS_IDLE;
601 h1m_init(&h2s->req);
602 h1m_init(&h2s->res);
603 h2s->by_id.key = h2s->id = id;
604 h2c->max_id = id;
605 LIST_INIT(&h2s->list);
606
607 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
608
609 cs = cs_new(h2c->conn);
610 if (!cs)
611 goto out_close;
612
613 h2s->cs = cs;
614 cs->ctx = h2s;
615
616 if (stream_create_from_cs(cs) < 0)
617 goto out_free_cs;
618
619 /* OK done, the stream lives its own life now */
620 return h2s;
621
622 out_free_cs:
623 cs_free(cs);
624 out_close:
625 eb32_delete(&h2s->by_id);
626 pool_free2(pool2_h2s, h2s);
627 h2s = NULL;
628 out:
629 return h2s;
630}
631
Willy Tarreaube5b7152017-09-25 16:25:39 +0200632/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
633 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
634 * the various settings codes.
635 */
636static int h2c_snd_settings(struct h2c *h2c)
637{
638 struct buffer *res;
639 char buf_data[100]; // enough for 15 settings
640 struct chunk buf;
641 int ret;
642
643 if (h2c_mux_busy(h2c, NULL)) {
644 h2c->flags |= H2_CF_DEM_MBUSY;
645 return 0;
646 }
647
648 res = h2_get_mbuf(h2c);
649 if (!res) {
650 h2c->flags |= H2_CF_MUX_MALLOC;
651 h2c->flags |= H2_CF_DEM_MROOM;
652 return 0;
653 }
654
655 chunk_init(&buf, buf_data, sizeof(buf_data));
656 chunk_memcpy(&buf,
657 "\x00\x00\x00" /* length : 0 for now */
658 "\x04\x00" /* type : 4 (settings), flags : 0 */
659 "\x00\x00\x00\x00", /* stream ID : 0 */
660 9);
661
662 if (h2_settings_header_table_size != 4096) {
663 char str[6] = "\x00\x01"; /* header_table_size */
664
665 write_n32(str + 2, h2_settings_header_table_size);
666 chunk_memcat(&buf, str, 6);
667 }
668
669 if (h2_settings_initial_window_size != 65535) {
670 char str[6] = "\x00\x04"; /* initial_window_size */
671
672 write_n32(str + 2, h2_settings_initial_window_size);
673 chunk_memcat(&buf, str, 6);
674 }
675
676 if (h2_settings_max_concurrent_streams != 0) {
677 char str[6] = "\x00\x03"; /* max_concurrent_streams */
678
679 /* Note: 0 means "unlimited" for haproxy's config but not for
680 * the protocol, so never send this value!
681 */
682 write_n32(str + 2, h2_settings_max_concurrent_streams);
683 chunk_memcat(&buf, str, 6);
684 }
685
686 if (global.tune.bufsize != 16384) {
687 char str[6] = "\x00\x05"; /* max_frame_size */
688
689 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
690 * match bufsize - rewrite size, but at the moment it seems
691 * that clients don't take care of it.
692 */
693 write_n32(str + 2, global.tune.bufsize);
694 chunk_memcat(&buf, str, 6);
695 }
696
697 h2_set_frame_size(buf.str, buf.len - 9);
698 ret = bo_istput(res, ist2(buf.str, buf.len));
699 if (unlikely(ret <= 0)) {
700 if (!ret) {
701 h2c->flags |= H2_CF_MUX_MFULL;
702 h2c->flags |= H2_CF_DEM_MROOM;
703 return 0;
704 }
705 else {
706 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
707 return 0;
708 }
709 }
710 return ret;
711}
712
Willy Tarreau52eed752017-09-22 15:05:09 +0200713/* Try to receive a connection preface, then upon success try to send our
714 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
715 * missing data. It may return an error in h2c.
716 */
717static int h2c_frt_recv_preface(struct h2c *h2c)
718{
719 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200720 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200721
722 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
723
724 if (unlikely(ret1 <= 0)) {
725 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
726 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
727 return 0;
728 }
729
Willy Tarreaube5b7152017-09-25 16:25:39 +0200730 ret2 = h2c_snd_settings(h2c);
731 if (ret2 > 0)
732 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200733
Willy Tarreaube5b7152017-09-25 16:25:39 +0200734 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200735}
736
Willy Tarreau081d4722017-05-16 21:51:05 +0200737/* try to send a GOAWAY frame on the connection to report an error or a graceful
738 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
739 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
740 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
741 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
742 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
743 * on unrecoverable failure. It will not attempt to send one again in this last
744 * case so that it is safe to use h2c_error() to report such errors.
745 */
746static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
747{
748 struct buffer *res;
749 char str[17];
750 int ret;
751
752 if (h2c->flags & H2_CF_GOAWAY_FAILED)
753 return 1; // claim that it worked
754
755 if (h2c_mux_busy(h2c, h2s)) {
756 if (h2s)
757 h2s->flags |= H2_SF_BLK_MBUSY;
758 else
759 h2c->flags |= H2_CF_DEM_MBUSY;
760 return 0;
761 }
762
763 res = h2_get_mbuf(h2c);
764 if (!res) {
765 h2c->flags |= H2_CF_MUX_MALLOC;
766 if (h2s)
767 h2s->flags |= H2_SF_BLK_MROOM;
768 else
769 h2c->flags |= H2_CF_DEM_MROOM;
770 return 0;
771 }
772
773 /* len: 8, type: 7, flags: none, sid: 0 */
774 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
775
776 if (h2c->last_sid < 0)
777 h2c->last_sid = h2c->max_id;
778
779 write_n32(str + 9, h2c->last_sid);
780 write_n32(str + 13, h2c->errcode);
781 ret = bo_istput(res, ist2(str, 17));
782 if (unlikely(ret <= 0)) {
783 if (!ret) {
784 h2c->flags |= H2_CF_MUX_MFULL;
785 if (h2s)
786 h2s->flags |= H2_SF_BLK_MROOM;
787 else
788 h2c->flags |= H2_CF_DEM_MROOM;
789 return 0;
790 }
791 else {
792 /* we cannot report this error using GOAWAY, so we mark
793 * it and claim a success.
794 */
795 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
796 h2c->flags |= H2_CF_GOAWAY_FAILED;
797 return 1;
798 }
799 }
800 h2c->flags |= H2_CF_GOAWAY_SENT;
801 return ret;
802}
803
Willy Tarreau27a84c92017-10-17 08:10:17 +0200804/* try to send an RST_STREAM frame on the connection for the current demuxed
805 * stream to report an error, with h2s->errcode as the error code. Returns > 0
806 * on success or zero if nothing was done. It uses h2c->dsi as the stream ID
807 * and h2s->errcode for the error code. In case of lack of room to write the
808 * message, it subscribes the requester (either <h2s> or <h2c>) to future
809 * notifications. It's worth mentionning that an RST may even be sent for a
810 * closed stream with error 0 in this case.
811 */
812static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
813{
814 struct buffer *res;
815 char str[13];
816 int ret;
817
818 if (h2c_mux_busy(h2c, h2s)) {
819 if (h2s)
820 h2s->flags |= H2_SF_BLK_MBUSY;
821 else
822 h2c->flags |= H2_CF_DEM_MBUSY;
823 return 0;
824 }
825
826 res = h2_get_mbuf(h2c);
827 if (!res) {
828 h2c->flags |= H2_CF_MUX_MALLOC;
829 if (h2s)
830 h2s->flags |= H2_SF_BLK_MROOM;
831 else
832 h2c->flags |= H2_CF_DEM_MROOM;
833 return 0;
834 }
835
836 /* len: 4, type: 3, flags: none */
837 memcpy(str, "\x00\x00\x04\x03\x00", 5);
838 write_n32(str + 5, h2c->dsi);
839 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_RESET) ?
840 h2s->errcode : H2_ERR_STREAM_CLOSED);
841 ret = bo_istput(res, ist2(str, 13));
842 if (unlikely(ret <= 0)) {
843 if (!ret) {
844 h2c->flags |= H2_CF_MUX_MFULL;
845 if (h2s)
846 h2s->flags |= H2_SF_BLK_MROOM;
847 else
848 h2c->flags |= H2_CF_DEM_MROOM;
849 return 0;
850 }
851 else {
852 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
853 return 0;
854 }
855 }
856
857 if (h2s)
858 h2s->flags |= H2_SF_RST_SENT;
859 return ret;
860}
861
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100862/* try to send an empty DATA frame with the ES flag set to notify about the
863 * end of stream and match a shutdown(write). If an ES was already sent as
864 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
865 * on success or zero if nothing was done. In case of lack of room to write the
866 * message, it subscribes the requesting stream to future notifications.
867 */
868static int h2_send_empty_data_es(struct h2s *h2s)
869{
870 struct h2c *h2c = h2s->h2c;
871 struct buffer *res;
872 char str[9];
873 int ret;
874
875 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
876 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
877 return 1;
878
879 if (h2c_mux_busy(h2c, h2s)) {
880 h2s->flags |= H2_SF_BLK_MBUSY;
881 return 0;
882 }
883
884 res = h2_get_mbuf(h2c);
885 if (!res) {
886 h2c->flags |= H2_CF_MUX_MALLOC;
887 h2s->flags |= H2_SF_BLK_MROOM;
888 return 0;
889 }
890
891 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
892 memcpy(str, "\x00\x00\x00\x00\x01", 5);
893 write_n32(str + 5, h2s->id);
894 ret = bo_istput(res, ist2(str, 9));
895 if (unlikely(ret <= 0)) {
896 if (!ret) {
897 h2c->flags |= H2_CF_MUX_MFULL;
898 h2s->flags |= H2_SF_BLK_MROOM;
899 return 0;
900 }
901 else {
902 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
903 return 0;
904 }
905 }
906 return ret;
907}
908
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100909/* wake the streams attached to the connection, whose id is greater than <last>,
910 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
911 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
912 * stream's state is automatically updated accordingly.
913 */
914static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
915{
916 struct eb32_node *node;
917 struct h2s *h2s;
918
919 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
920 flags |= CS_FL_ERROR;
921
922 if (conn_xprt_read0_pending(h2c->conn))
923 flags |= CS_FL_EOS;
924
925 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
926 while (node) {
927 h2s = container_of(node, struct h2s, by_id);
928 if (h2s->id <= last)
929 break;
930 node = eb32_next(node);
931 if (h2s->cs) {
932 h2s->cs->flags |= flags;
933 /* recv is used to force to detect CS_FL_EOS that wake()
934 * doesn't handle in the stream int code.
935 */
936 h2s->cs->data_cb->recv(h2s->cs);
937 h2s->cs->data_cb->wake(h2s->cs);
938 }
939 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
940 h2s->st = H2_SS_ERROR;
941 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
942 h2s->st = H2_SS_HREM;
943 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
944 h2s->st = H2_SS_CLOSED;
945 }
946}
947
Willy Tarreau3421aba2017-07-27 15:41:03 +0200948/* Increase all streams' outgoing window size by the difference passed in
949 * argument. This is needed upon receipt of the settings frame if the initial
950 * window size is different. The difference may be negative and the resulting
951 * window size as well, for the time it takes to receive some window updates.
952 */
953static void h2c_update_all_ws(struct h2c *h2c, int diff)
954{
955 struct h2s *h2s;
956 struct eb32_node *node;
957
958 if (!diff)
959 return;
960
961 node = eb32_first(&h2c->streams_by_id);
962 while (node) {
963 h2s = container_of(node, struct h2s, by_id);
964 h2s->mws += diff;
965 node = eb32_next(node);
966 }
967}
968
969/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
970 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
971 * return an error in h2c. Described in RFC7540#6.5.
972 */
973static int h2c_handle_settings(struct h2c *h2c)
974{
975 unsigned int offset;
976 int error;
977
978 if (h2c->dff & H2_F_SETTINGS_ACK) {
979 if (h2c->dfl) {
980 error = H2_ERR_FRAME_SIZE_ERROR;
981 goto fail;
982 }
983 return 1;
984 }
985
986 if (h2c->dsi != 0) {
987 error = H2_ERR_PROTOCOL_ERROR;
988 goto fail;
989 }
990
991 if (h2c->dfl % 6) {
992 error = H2_ERR_FRAME_SIZE_ERROR;
993 goto fail;
994 }
995
996 /* that's the limit we can process */
997 if (h2c->dfl > global.tune.bufsize) {
998 error = H2_ERR_FRAME_SIZE_ERROR;
999 goto fail;
1000 }
1001
1002 /* process full frame only */
1003 if (h2c->dbuf->i < h2c->dfl)
1004 return 0;
1005
1006 /* parse the frame */
1007 for (offset = 0; offset < h2c->dfl; offset += 6) {
1008 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1009 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1010
1011 switch (type) {
1012 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1013 /* we need to update all existing streams with the
1014 * difference from the previous iws.
1015 */
1016 if (arg < 0) { // RFC7540#6.5.2
1017 error = H2_ERR_FLOW_CONTROL_ERROR;
1018 goto fail;
1019 }
1020 h2c_update_all_ws(h2c, arg - h2c->miw);
1021 h2c->miw = arg;
1022 break;
1023 case H2_SETTINGS_MAX_FRAME_SIZE:
1024 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1025 error = H2_ERR_PROTOCOL_ERROR;
1026 goto fail;
1027 }
1028 h2c->mfs = arg;
1029 break;
1030 }
1031 }
1032
1033 /* need to ACK this frame now */
1034 h2c->st0 = H2_CS_FRAME_A;
1035 return 1;
1036 fail:
1037 h2c_error(h2c, error);
1038 return 0;
1039}
1040
1041/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1042 * success or one of the h2_status values.
1043 */
1044static int h2c_ack_settings(struct h2c *h2c)
1045{
1046 struct buffer *res;
1047 char str[9];
1048 int ret = -1;
1049
1050 if (h2c_mux_busy(h2c, NULL)) {
1051 h2c->flags |= H2_CF_DEM_MBUSY;
1052 return 0;
1053 }
1054
1055 res = h2_get_mbuf(h2c);
1056 if (!res) {
1057 h2c->flags |= H2_CF_MUX_MALLOC;
1058 h2c->flags |= H2_CF_DEM_MROOM;
1059 return 0;
1060 }
1061
1062 memcpy(str,
1063 "\x00\x00\x00" /* length : 0 (no data) */
1064 "\x04" "\x01" /* type : 4, flags : ACK */
1065 "\x00\x00\x00\x00" /* stream ID */, 9);
1066
1067 ret = bo_istput(res, ist2(str, 9));
1068 if (unlikely(ret <= 0)) {
1069 if (!ret) {
1070 h2c->flags |= H2_CF_MUX_MFULL;
1071 h2c->flags |= H2_CF_DEM_MROOM;
1072 return 0;
1073 }
1074 else {
1075 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1076 return 0;
1077 }
1078 }
1079 return ret;
1080}
1081
Willy Tarreaucf68c782017-10-10 17:11:41 +02001082/* processes a PING frame and schedules an ACK if needed. The caller must pass
1083 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1084 * missing data. It may return an error in h2c.
1085 */
1086static int h2c_handle_ping(struct h2c *h2c)
1087{
1088 /* frame length must be exactly 8 */
1089 if (h2c->dfl != 8) {
1090 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1091 return 0;
1092 }
1093
1094 /* schedule a response */
1095 if (!(h2c->dft & H2_F_PING_ACK))
1096 h2c->st0 = H2_CS_FRAME_A;
1097 return 1;
1098}
1099
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001100/* Try to send a window update for stream id <sid> and value <increment>.
1101 * Returns > 0 on success or zero on missing room or failure. It may return an
1102 * error in h2c.
1103 */
1104static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1105{
1106 struct buffer *res;
1107 char str[13];
1108 int ret = -1;
1109
1110 if (h2c_mux_busy(h2c, NULL)) {
1111 h2c->flags |= H2_CF_DEM_MBUSY;
1112 return 0;
1113 }
1114
1115 res = h2_get_mbuf(h2c);
1116 if (!res) {
1117 h2c->flags |= H2_CF_MUX_MALLOC;
1118 h2c->flags |= H2_CF_DEM_MROOM;
1119 return 0;
1120 }
1121
1122 /* length: 4, type: 8, flags: none */
1123 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1124 write_n32(str + 5, sid);
1125 write_n32(str + 9, increment);
1126
1127 ret = bo_istput(res, ist2(str, 13));
1128
1129 if (unlikely(ret <= 0)) {
1130 if (!ret) {
1131 h2c->flags |= H2_CF_MUX_MFULL;
1132 h2c->flags |= H2_CF_DEM_MROOM;
1133 return 0;
1134 }
1135 else {
1136 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1137 return 0;
1138 }
1139 }
1140 return ret;
1141}
1142
1143/* try to send pending window update for the connection. It's safe to call it
1144 * with no pending updates. Returns > 0 on success or zero on missing room or
1145 * failure. It may return an error in h2c.
1146 */
1147static int h2c_send_conn_wu(struct h2c *h2c)
1148{
1149 int ret = 1;
1150
1151 if (h2c->rcvd_c <= 0)
1152 return 1;
1153
1154 /* send WU for the connection */
1155 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1156 if (ret > 0)
1157 h2c->rcvd_c = 0;
1158
1159 return ret;
1160}
1161
1162/* try to send pending window update for the current dmux stream. It's safe to
1163 * call it with no pending updates. Returns > 0 on success or zero on missing
1164 * room or failure. It may return an error in h2c.
1165 */
1166static int h2c_send_strm_wu(struct h2c *h2c)
1167{
1168 int ret = 1;
1169
1170 if (h2c->rcvd_s <= 0)
1171 return 1;
1172
1173 /* send WU for the stream */
1174 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1175 if (ret > 0)
1176 h2c->rcvd_s = 0;
1177
1178 return ret;
1179}
1180
Willy Tarreaucf68c782017-10-10 17:11:41 +02001181/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1182 * success, 0 on missing data or one of the h2_status values.
1183 */
1184static int h2c_ack_ping(struct h2c *h2c)
1185{
1186 struct buffer *res;
1187 char str[17];
1188 int ret = -1;
1189
1190 if (h2c->dbuf->i < 8)
1191 return 0;
1192
1193 if (h2c_mux_busy(h2c, NULL)) {
1194 h2c->flags |= H2_CF_DEM_MBUSY;
1195 return 0;
1196 }
1197
1198 res = h2_get_mbuf(h2c);
1199 if (!res) {
1200 h2c->flags |= H2_CF_MUX_MALLOC;
1201 h2c->flags |= H2_CF_DEM_MROOM;
1202 return 0;
1203 }
1204
1205 memcpy(str,
1206 "\x00\x00\x08" /* length : 8 (same payload) */
1207 "\x06" "\x01" /* type : 6, flags : ACK */
1208 "\x00\x00\x00\x00" /* stream ID */, 9);
1209
1210 /* copy the original payload */
1211 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1212
1213 ret = bo_istput(res, ist2(str, 17));
1214 if (unlikely(ret <= 0)) {
1215 if (!ret) {
1216 h2c->flags |= H2_CF_MUX_MFULL;
1217 h2c->flags |= H2_CF_DEM_MROOM;
1218 return 0;
1219 }
1220 else {
1221 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1222 return 0;
1223 }
1224 }
1225 return ret;
1226}
1227
Willy Tarreau26f95952017-07-27 17:18:30 +02001228/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1229 * Returns > 0 on success or zero on missing data. It may return an error in
1230 * h2c or h2s. Described in RFC7540#6.9.
1231 */
1232static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1233{
1234 int32_t inc;
1235 int error;
1236
1237 if (h2c->dfl != 4) {
1238 error = H2_ERR_FRAME_SIZE_ERROR;
1239 goto conn_err;
1240 }
1241
1242 /* process full frame only */
1243 if (h2c->dbuf->i < h2c->dfl)
1244 return 0;
1245
1246 inc = h2_get_n32(h2c->dbuf, 0);
1247
1248 if (h2c->dsi != 0) {
1249 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001250
1251 /* it's not an error to receive WU on a closed stream */
1252 if (h2s->st == H2_SS_CLOSED)
1253 return 1;
1254
1255 if (!inc) {
1256 error = H2_ERR_PROTOCOL_ERROR;
1257 goto strm_err;
1258 }
1259
1260 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1261 error = H2_ERR_FLOW_CONTROL_ERROR;
1262 goto strm_err;
1263 }
1264
1265 h2s->mws += inc;
1266 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1267 h2s->flags &= ~H2_SF_BLK_SFCTL;
1268 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1269 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1270 /* This stream wanted to send but could not due to its
1271 * own flow control. We can put it back into the send
1272 * list now, it will be handled upon next send() call.
1273 */
1274 LIST_ADDQ(&h2c->send_list, &h2s->list);
1275 }
1276 }
1277 }
1278 else {
1279 /* connection window update */
1280 if (!inc) {
1281 error = H2_ERR_PROTOCOL_ERROR;
1282 goto conn_err;
1283 }
1284
1285 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1286 error = H2_ERR_FLOW_CONTROL_ERROR;
1287 goto conn_err;
1288 }
1289
1290 h2c->mws += inc;
1291 }
1292
1293 return 1;
1294
1295 conn_err:
1296 h2c_error(h2c, error);
1297 return 0;
1298
1299 strm_err:
1300 if (h2s) {
1301 h2s_error(h2s, error);
1302 h2c->st0 = H2_CS_FRAME_A;
1303 }
1304 else
1305 h2c_error(h2c, error);
1306 return 0;
1307}
1308
Willy Tarreaue96b0922017-10-30 00:28:29 +01001309/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1310 * the last ID. Returns > 0 on success or zero on missing data. It may return
1311 * an error in h2c. Described in RFC7540#6.8.
1312 */
1313static int h2c_handle_goaway(struct h2c *h2c)
1314{
1315 int error;
1316 int last;
1317
1318 if (h2c->dsi != 0) {
1319 error = H2_ERR_PROTOCOL_ERROR;
1320 goto conn_err;
1321 }
1322
1323 if (h2c->dfl < 8) {
1324 error = H2_ERR_FRAME_SIZE_ERROR;
1325 goto conn_err;
1326 }
1327
1328 /* process full frame only */
1329 if (h2c->dbuf->i < h2c->dfl)
1330 return 0;
1331
1332 last = h2_get_n32(h2c->dbuf, 0);
1333 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1334 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
1335 return 1;
1336
1337 conn_err:
1338 h2c_error(h2c, error);
1339 return 0;
1340}
1341
Willy Tarreaucd234e92017-08-18 10:59:39 +02001342/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1343 * Returns > 0 on success or zero on missing data. It may return an error in
1344 * h2c. Described in RFC7540#6.4.
1345 */
1346static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1347{
1348 int error;
1349
1350 if (h2c->dsi == 0) {
1351 error = H2_ERR_PROTOCOL_ERROR;
1352 goto conn_err;
1353 }
1354
Willy Tarreaucd234e92017-08-18 10:59:39 +02001355 if (h2c->dfl != 4) {
1356 error = H2_ERR_FRAME_SIZE_ERROR;
1357 goto conn_err;
1358 }
1359
1360 /* process full frame only */
1361 if (h2c->dbuf->i < h2c->dfl)
1362 return 0;
1363
1364 /* late RST, already handled */
1365 if (h2s->st == H2_SS_CLOSED)
1366 return 1;
1367
1368 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1369 h2s->st = H2_SS_CLOSED;
1370
1371 if (h2s->cs) {
1372 h2s->cs->flags |= CS_FL_EOS;
1373 /* recv is used to force to detect CS_FL_EOS that wake()
1374 * doesn't handle in the stream-int code.
1375 */
1376 h2s->cs->data_cb->recv(h2s->cs);
1377 h2s->cs->data_cb->wake(h2s->cs);
1378 }
1379
1380 h2s->flags |= H2_SF_RST_RCVD;
1381 return 1;
1382
1383 conn_err:
1384 h2c_error(h2c, error);
1385 return 0;
1386}
1387
Willy Tarreau13278b42017-10-13 19:23:14 +02001388/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1389 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1390 * errors here are reported as connection errors since it's impossible to
1391 * recover from such errors after the compression context has been altered.
1392 */
1393static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1394{
1395 int error;
1396
1397 if (!h2c->dfl) {
1398 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1399 goto strm_err;
1400 }
1401
1402 if (!h2c->dbuf->size)
1403 return 0; // empty buffer
1404
1405 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1406 return 0; // incomplete frame
1407
1408 /* now either the frame is complete or the buffer is complete */
1409 if (h2s->st != H2_SS_IDLE) {
1410 /* FIXME: stream already exists, this is only allowed for
1411 * trailers (not supported for now).
1412 */
1413 error = H2_ERR_PROTOCOL_ERROR;
1414 goto conn_err;
1415 }
1416 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1417 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1418 error = H2_ERR_PROTOCOL_ERROR;
1419 goto conn_err;
1420 }
1421
1422 h2s = h2c_stream_new(h2c, h2c->dsi);
1423 if (!h2s) {
1424 error = H2_ERR_INTERNAL_ERROR;
1425 goto conn_err;
1426 }
1427
1428 h2s->st = H2_SS_OPEN;
1429 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1430 h2s->st = H2_SS_HREM;
1431 h2s->flags |= H2_SF_ES_RCVD;
1432 }
1433
1434 /* call the upper layers to process the frame, then let the upper layer
1435 * notify the stream about any change.
1436 */
1437 h2s->cs->data_cb->recv(h2s->cs);
1438
1439 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1440 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1441 error = H2_ERR_INTERNAL_ERROR;
1442 goto conn_err;
1443 }
1444
1445 if (h2s->st >= H2_SS_RESET) {
1446 /* stream error : send RST_STREAM */
1447 h2c->st0 = H2_CS_FRAME_A;
1448 }
1449 else {
1450 /* update the max stream ID if the request is being processed */
1451 if (h2s->id > h2c->max_id)
1452 h2c->max_id = h2s->id;
1453 }
1454
1455 return 1;
1456
1457 conn_err:
1458 h2c_error(h2c, error);
1459 return 0;
1460
1461 strm_err:
1462 if (h2s) {
1463 h2s_error(h2s, error);
1464 h2c->st0 = H2_CS_FRAME_A;
1465 }
1466 else
1467 h2c_error(h2c, error);
1468 return 0;
1469}
1470
Willy Tarreau454f9052017-10-26 19:40:35 +02001471/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1472 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1473 */
1474static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1475{
1476 int error;
1477
1478 /* note that empty DATA frames are perfectly valid and sometimes used
1479 * to signal an end of stream (with the ES flag).
1480 */
1481
1482 if (!h2c->dbuf->size && h2c->dfl)
1483 return 0; // empty buffer
1484
1485 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1486 return 0; // incomplete frame
1487
1488 /* now either the frame is complete or the buffer is complete */
1489
1490 if (!h2c->dsi) {
1491 /* RFC7540#6.1 */
1492 error = H2_ERR_PROTOCOL_ERROR;
1493 goto conn_err;
1494 }
1495
1496 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1497 /* RFC7540#6.1 */
1498 error = H2_ERR_STREAM_CLOSED;
1499 goto strm_err;
1500 }
1501
1502 /* last frame */
1503 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1504 h2s->st = H2_SS_HREM;
1505 h2s->flags |= H2_SF_ES_RCVD;
1506 }
1507
1508 /* call the upper layers to process the frame, then let the upper layer
1509 * notify the stream about any change.
1510 */
1511 if (!h2s->cs) {
1512 error = H2_ERR_STREAM_CLOSED;
1513 goto strm_err;
1514 }
1515
1516 h2s->cs->data_cb->recv(h2s->cs);
1517 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1518 /* cs has just been destroyed, we have to kill h2s. */
1519 error = H2_ERR_STREAM_CLOSED;
1520 goto strm_err;
1521 }
1522
1523 if (h2s->st >= H2_SS_RESET) {
1524 /* stream error : send RST_STREAM */
1525 h2c->st0 = H2_CS_FRAME_A;
1526 }
1527
1528 /* check for completion : the callee will change this to FRAME_A or
1529 * FRAME_H once done.
1530 */
1531 if (h2c->st0 == H2_CS_FRAME_P)
1532 return 0;
1533
1534 return 1;
1535
1536 conn_err:
1537 h2c_error(h2c, error);
1538 return 0;
1539
1540 strm_err:
1541 if (h2s) {
1542 h2s_error(h2s, error);
1543 h2c->st0 = H2_CS_FRAME_A;
1544 }
1545 else
1546 h2c_error(h2c, error);
1547 return 0;
1548}
1549
Willy Tarreaubc933932017-10-09 16:21:43 +02001550/* process Rx frames to be demultiplexed */
1551static void h2_process_demux(struct h2c *h2c)
1552{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001553 struct h2s *h2s;
1554
Willy Tarreau081d4722017-05-16 21:51:05 +02001555 if (h2c->st0 >= H2_CS_ERROR)
1556 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001557
1558 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1559 if (h2c->st0 == H2_CS_PREFACE) {
1560 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1561 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1562 if (h2c->st0 == H2_CS_ERROR)
1563 h2c->st0 = H2_CS_ERROR2;
1564 goto fail;
1565 }
1566
1567 h2c->max_id = 0;
1568 h2c->st0 = H2_CS_SETTINGS1;
1569 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001570
1571 if (h2c->st0 == H2_CS_SETTINGS1) {
1572 struct h2_fh hdr;
1573
1574 /* ensure that what is pending is a valid SETTINGS frame
1575 * without an ACK.
1576 */
1577 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1578 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1579 if (h2c->st0 == H2_CS_ERROR)
1580 h2c->st0 = H2_CS_ERROR2;
1581 goto fail;
1582 }
1583
1584 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1585 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1586 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1587 h2c->st0 = H2_CS_ERROR2;
1588 goto fail;
1589 }
1590
1591 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1592 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1593 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1594 h2c->st0 = H2_CS_ERROR2;
1595 goto fail;
1596 }
1597
1598 /* that's OK, switch to FRAME_P to process it */
1599 h2c->dfl = hdr.len;
1600 h2c->dsi = hdr.sid;
1601 h2c->dft = hdr.ft;
1602 h2c->dff = hdr.ff;
1603 h2c->st0 = H2_CS_FRAME_P;
1604 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001605 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001606
1607 /* process as many incoming frames as possible below */
1608 while (h2c->dbuf->i) {
1609 int ret = 0;
1610
1611 if (h2c->st0 >= H2_CS_ERROR)
1612 break;
1613
1614 if (h2c->st0 == H2_CS_FRAME_H) {
1615 struct h2_fh hdr;
1616
1617 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1618 break;
1619
1620 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1621 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1622 h2c->st0 = H2_CS_ERROR;
1623 break;
1624 }
1625
1626 h2c->dfl = hdr.len;
1627 h2c->dsi = hdr.sid;
1628 h2c->dft = hdr.ft;
1629 h2c->dff = hdr.ff;
1630 h2c->st0 = H2_CS_FRAME_P;
1631 h2_skip_frame_hdr(h2c->dbuf);
1632 }
1633
1634 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001635 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001636
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001637 if (h2s->st == H2_SS_IDLE &&
1638 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1639 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1640 * this state MUST be treated as a connection error
1641 */
1642 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1643 h2c->st0 = H2_CS_ERROR;
1644 break;
1645 }
1646
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001647 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1648 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1649 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1650 * this state MUST be treated as a stream error
1651 */
1652 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1653 goto strm_err;
1654 }
1655
Willy Tarreauc0da1962017-10-30 18:38:00 +01001656#if 0
1657 // problem below: it is not possible to completely ignore such
1658 // streams as we need to maintain the compression state as well
1659 // and for this we need to completely process these frames (eg:
1660 // HEADERS frames) as well as counting DATA frames to emit
1661 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1662 // This is a typical case of layer violation where the
1663 // transported contents are critical to the connection's
1664 // validity and must be ignored at the same time :-(
1665
1666 /* graceful shutdown, ignore streams whose ID is higher than
1667 * the one advertised in GOAWAY. RFC7540#6.8.
1668 */
1669 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1670 ret = MIN(h2c->dbuf->i, h2c->dfl);
1671 bi_del(h2c->dbuf, ret);
1672 h2c->dfl -= ret;
1673 ret = h2c->dfl == 0;
1674 goto strm_err;
1675 }
1676#endif
1677
Willy Tarreau7e98c052017-10-10 15:56:59 +02001678 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001679 case H2_FT_SETTINGS:
1680 if (h2c->st0 == H2_CS_FRAME_P)
1681 ret = h2c_handle_settings(h2c);
1682
1683 if (h2c->st0 == H2_CS_FRAME_A)
1684 ret = h2c_ack_settings(h2c);
1685 break;
1686
Willy Tarreaucf68c782017-10-10 17:11:41 +02001687 case H2_FT_PING:
1688 if (h2c->st0 == H2_CS_FRAME_P)
1689 ret = h2c_handle_ping(h2c);
1690
1691 if (h2c->st0 == H2_CS_FRAME_A)
1692 ret = h2c_ack_ping(h2c);
1693 break;
1694
Willy Tarreau26f95952017-07-27 17:18:30 +02001695 case H2_FT_WINDOW_UPDATE:
1696 if (h2c->st0 == H2_CS_FRAME_P)
1697 ret = h2c_handle_window_update(h2c, h2s);
1698 break;
1699
Willy Tarreau61290ec2017-10-17 08:19:21 +02001700 case H2_FT_CONTINUATION:
1701 /* we currently don't support CONTINUATION frames since
1702 * we have nowhere to store the partial HEADERS frame.
1703 * Let's abort the stream on an INTERNAL_ERROR here.
1704 */
1705 if (h2c->st0 == H2_CS_FRAME_P)
1706 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1707 break;
1708
Willy Tarreau13278b42017-10-13 19:23:14 +02001709 case H2_FT_HEADERS:
1710 if (h2c->st0 == H2_CS_FRAME_P)
1711 ret = h2c_frt_handle_headers(h2c, h2s);
1712 break;
1713
Willy Tarreau454f9052017-10-26 19:40:35 +02001714 case H2_FT_DATA:
1715 if (h2c->st0 == H2_CS_FRAME_P)
1716 ret = h2c_frt_handle_data(h2c, h2s);
1717
1718 if (h2c->st0 == H2_CS_FRAME_A)
1719 ret = h2c_send_strm_wu(h2c);
1720 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001721
1722 case H2_FT_RST_STREAM:
1723 if (h2c->st0 == H2_CS_FRAME_P)
1724 ret = h2c_handle_rst_stream(h2c, h2s);
1725 break;
1726
Willy Tarreaue96b0922017-10-30 00:28:29 +01001727 case H2_FT_GOAWAY:
1728 if (h2c->st0 == H2_CS_FRAME_P)
1729 ret = h2c_handle_goaway(h2c);
1730 break;
1731
Willy Tarreau1c661982017-10-30 13:52:01 +01001732 case H2_FT_PUSH_PROMISE:
1733 /* not permitted here, RFC7540#5.1 */
1734 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1735 h2c->st0 = H2_SS_ERROR;
1736 break;
1737
1738 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001739 default:
1740 /* drop frames that we ignore. They may be larger than
1741 * the buffer so we drain all of their contents until
1742 * we reach the end.
1743 */
1744 ret = MIN(h2c->dbuf->i, h2c->dfl);
1745 bi_del(h2c->dbuf, ret);
1746 h2c->dfl -= ret;
1747 ret = h2c->dfl == 0;
1748 }
1749
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001750 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001751 /* RST are sent similarly to frame acks */
1752 if (h2s->st == H2_SS_ERROR) {
1753 if (h2c->st0 == H2_CS_FRAME_P)
1754 h2c->st0 = H2_CS_FRAME_A;
1755
1756 if (h2c->st0 == H2_CS_FRAME_A)
1757 ret = h2c_send_rst_stream(h2c, h2s);
1758 }
1759
Willy Tarreau7e98c052017-10-10 15:56:59 +02001760 /* error or missing data condition met above ? */
1761 if (ret <= 0)
1762 break;
1763
1764 if (h2c->st0 != H2_CS_FRAME_H) {
1765 bi_del(h2c->dbuf, h2c->dfl);
1766 h2c->st0 = H2_CS_FRAME_H;
1767 }
1768 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001769
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001770 if (h2c->rcvd_c > 0 &&
1771 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1772 h2c_send_conn_wu(h2c);
1773
Willy Tarreau52eed752017-09-22 15:05:09 +02001774 fail:
1775 /* we can go here on missing data, blocked response or error */
1776 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001777}
1778
1779/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1780 * the end.
1781 */
1782static int h2_process_mux(struct h2c *h2c)
1783{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001784 struct h2s *h2s, *h2s_back;
1785
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001786 /* start by sending possibly pending window updates */
1787 if (h2c->rcvd_c > 0 &&
1788 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1789 h2c_send_conn_wu(h2c) < 0)
1790 goto fail;
1791
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001792 /* First we always process the flow control list because the streams
1793 * waiting there were already elected for immediate emission but were
1794 * blocked just on this.
1795 */
1796
1797 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1798 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1799 h2c->st0 >= H2_CS_ERROR)
1800 break;
1801
1802 /* In theory it's possible that h2s->cs == NULL here :
1803 * - client sends crap that causes a parse error
1804 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1805 * - RST_STREAM cannot be emitted because mux is busy/full
1806 * - stream gets notified, detaches and quits
1807 * - mux buffer gets ready and wakes pending streams up
1808 * - bam!
1809 */
1810 h2s->flags &= ~H2_SF_BLK_ANY;
1811
1812 if (h2s->cs) {
1813 h2s->cs->data_cb->send(h2s->cs);
1814 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001815 } else {
1816 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001817 }
1818
1819 /* depending on callee's blocking reasons, we may queue in send
1820 * list or completely dequeue.
1821 */
1822 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1823 if (h2s->flags & H2_SF_BLK_ANY) {
1824 LIST_DEL(&h2s->list);
1825 LIST_ADDQ(&h2c->send_list, &h2s->list);
1826 }
1827 else {
1828 LIST_DEL(&h2s->list);
1829 LIST_INIT(&h2s->list);
1830 if (h2s->cs)
1831 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1832 }
1833 }
1834 }
1835
1836 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1837 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1838 break;
1839
1840 /* In theory it's possible that h2s->cs == NULL here :
1841 * - client sends crap that causes a parse error
1842 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1843 * - RST_STREAM cannot be emitted because mux is busy/full
1844 * - stream gets notified, detaches and quits
1845 * - mux buffer gets ready and wakes pending streams up
1846 * - bam!
1847 */
1848 h2s->flags &= ~H2_SF_BLK_ANY;
1849
1850 if (h2s->cs) {
1851 h2s->cs->data_cb->send(h2s->cs);
1852 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001853 } else {
1854 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001855 }
1856 /* depending on callee's blocking reasons, we may queue in fctl
1857 * list or completely dequeue.
1858 */
1859 if (h2s->flags & H2_SF_BLK_MFCTL) {
1860 /* stream hit the connection's flow control */
1861 LIST_DEL(&h2s->list);
1862 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1863 }
1864 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1865 LIST_DEL(&h2s->list);
1866 LIST_INIT(&h2s->list);
1867 if (h2s->cs)
1868 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1869 }
1870 }
1871
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001872 fail:
Willy Tarreau081d4722017-05-16 21:51:05 +02001873 if (unlikely(h2c->st0 > H2_CS_ERROR)) {
1874 if (h2c->st0 == H2_CS_ERROR) {
1875 if (h2c->max_id >= 0) {
1876 h2c_send_goaway_error(h2c, NULL);
1877 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1878 return 0;
1879 }
1880
1881 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1882 }
1883 return 1;
1884 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001885 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001886}
1887
Willy Tarreau71681172017-10-23 14:39:06 +02001888
Willy Tarreau62f52692017-10-08 23:01:42 +02001889/*********************************************************/
1890/* functions below are I/O callbacks from the connection */
1891/*********************************************************/
1892
1893/* callback called on recv event by the connection handler */
1894static void h2_recv(struct connection *conn)
1895{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001896 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001897 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001898 int max;
1899
1900 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001901 return;
1902
1903 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1904 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001905
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001906 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001907 if (!buf) {
1908 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001909 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001910 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001911
Willy Tarreaua2af5122017-10-09 11:56:46 +02001912 /* note: buf->o == 0 */
1913 max = buf->size - buf->i;
1914 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001915 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001916 return;
1917 }
1918
1919 conn->xprt->rcv_buf(conn, buf, max);
1920 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001921 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001922
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001923 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001924 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001925 return;
1926 }
1927
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001928 if (buf->i == buf->size)
1929 h2c->flags |= H2_CF_DEM_DFULL;
1930
Willy Tarreaubc933932017-10-09 16:21:43 +02001931 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001932
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001933 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02001934 if (h2c->st0 >= H2_CS_ERROR)
1935 buf->i = 0;
1936
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001937 if (buf->i != buf->size)
1938 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001939 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02001940}
1941
1942/* callback called on send event by the connection handler */
1943static void h2_send(struct connection *conn)
1944{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001945 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02001946 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001947
1948 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001949 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001950
1951 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
1952 /* a handshake was requested */
1953 return;
1954 }
1955
Willy Tarreaubc933932017-10-09 16:21:43 +02001956 /* This loop is quite simple : it tries to fill as much as it can from
1957 * pending streams into the existing buffer until it's reportedly full
1958 * or the end of send requests is reached. Then it tries to send this
1959 * buffer's contents out, marks it not full if at least one byte could
1960 * be sent, and tries again.
1961 *
1962 * The snd_buf() function normally takes a "flags" argument which may
1963 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
1964 * data immediately comes and CO_SFL_STREAMER to indicate that the
1965 * connection is streaming lots of data (used to increase TLS record
1966 * size at the expense of latency). The former can be sent any time
1967 * there's a buffer full flag, as it indicates at least one stream
1968 * attempted to send and failed so there are pending data. An
1969 * alternative would be to set it as long as there's an active stream
1970 * but that would be problematic for ACKs until we have an absolute
1971 * guarantee that all waiters have at least one byte to send. The
1972 * latter should possibly not be set for now.
1973 */
1974
1975 done = 0;
1976 while (!done) {
1977 unsigned int flags = 0;
1978
1979 /* fill as much as we can into the current buffer */
1980 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
1981 done = h2_process_mux(h2c);
1982
1983 if (conn->flags & CO_FL_ERROR)
1984 break;
1985
1986 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
1987 flags |= CO_SFL_MSG_MORE;
1988
1989 if (conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
1990 break;
1991
1992 /* wrote at least one byte, the buffer is not full anymore */
1993 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
1994 }
1995
Willy Tarreaua2af5122017-10-09 11:56:46 +02001996 if (conn->flags & CO_FL_SOCK_WR_SH) {
1997 /* output closed, nothing to send, clear the buffer to release it */
1998 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001999 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002000}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002001
Willy Tarreau62f52692017-10-08 23:01:42 +02002002/* callback called on any event by the connection handler.
2003 * It applies changes and returns zero, or < 0 if it wants immediate
2004 * destruction of the connection (which normally doesn not happen in h2).
2005 */
2006static int h2_wake(struct connection *conn)
2007{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002008 struct h2c *h2c = conn->mux_ctx;
2009
Willy Tarreau26bd7612017-10-09 16:47:04 +02002010 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002011 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2012 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2013 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002014 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002015
2016 if (eb_is_empty(&h2c->streams_by_id)) {
2017 /* no more stream, kill the connection now */
2018 h2_release(conn);
2019 return -1;
2020 }
2021 else {
2022 /* some streams still there, we need to signal them all and
2023 * wait for their departure.
2024 */
2025 __conn_xprt_stop_recv(conn);
2026 __conn_xprt_stop_send(conn);
2027 return 0;
2028 }
2029 }
2030
2031 if (!h2c->dbuf->i)
2032 h2_release_dbuf(h2c);
2033
2034 /* stop being notified of incoming data if we can't process them */
2035 if (h2c->st0 >= H2_CS_ERROR ||
2036 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002037 __conn_xprt_stop_recv(conn);
2038 }
2039 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002040 __conn_xprt_want_recv(conn);
2041 }
2042
2043 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002044 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2045 (h2c->st0 == H2_CS_ERROR ||
2046 h2c->mbuf->o ||
2047 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2048 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002049 __conn_xprt_want_send(conn);
2050 }
2051 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002052 h2_release_mbuf(h2c);
2053 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002054 }
2055
Willy Tarreau3f133572017-10-31 19:21:06 +01002056 if (h2c->task) {
2057 if (eb_is_empty(&h2c->streams_by_id)) {
2058 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2059 task_queue(h2c->task);
2060 }
2061 else
2062 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002063 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002064 return 0;
2065}
2066
Willy Tarreauea392822017-10-31 10:02:25 +01002067/* Connection timeout management. The principle is that if there's no receipt
2068 * nor sending for a certain amount of time, the connection is closed. If the
2069 * MUX buffer still has lying data or is not allocatable, the connection is
2070 * immediately killed. If it's allocatable and empty, we attempt to send a
2071 * GOAWAY frame.
2072 */
2073static struct task *h2_timeout_task(struct task *t)
2074{
2075 struct h2c *h2c = t->context;
2076 int expired = tick_is_expired(t->expire, now_ms);
2077
2078 if (!expired)
2079 return t;
2080
2081 h2c_error(h2c, H2_ERR_NO_ERROR);
2082 h2_wake_some_streams(h2c, 0, 0);
2083
2084 if (h2c->mbuf->o) {
2085 /* don't even try to send a GOAWAY, the buffer is stuck */
2086 h2c->flags |= H2_CF_GOAWAY_FAILED;
2087 }
2088
2089 /* try to send but no need to insist */
2090 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2091 h2c->flags |= H2_CF_GOAWAY_FAILED;
2092
2093 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2094 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2095
2096 if (!eb_is_empty(&h2c->streams_by_id))
2097 goto wait;
2098
2099 h2_release(h2c->conn);
2100 return NULL;
2101
2102 wait:
2103 /* the streams have been notified, we must let them finish and close */
2104 h2c->task = NULL;
2105 task_delete(t);
2106 task_free(t);
2107 return NULL;
2108}
2109
2110
Willy Tarreau62f52692017-10-08 23:01:42 +02002111/*******************************************/
2112/* functions below are used by the streams */
2113/*******************************************/
2114
2115/*
2116 * Attach a new stream to a connection
2117 * (Used for outgoing connections)
2118 */
2119static struct conn_stream *h2_attach(struct connection *conn)
2120{
2121 return NULL;
2122}
2123
2124/* callback used to update the mux's polling flags after changing a cs' status.
2125 * The caller (cs_update_mux_polling) will take care of propagating any changes
2126 * to the transport layer.
2127 */
2128static void h2_update_poll(struct conn_stream *cs)
2129{
Willy Tarreau1d393222017-10-17 10:26:19 +02002130 struct h2s *h2s = cs->ctx;
2131
2132 if (!h2s)
2133 return;
2134
Willy Tarreaud7739c82017-10-30 15:38:23 +01002135 /* we may unblock a blocked read */
2136
2137 if (cs->flags & CS_FL_DATA_RD_ENA &&
2138 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2139 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2140 conn_xprt_want_recv(cs->conn);
2141 }
2142
Willy Tarreau1d393222017-10-17 10:26:19 +02002143 /* Note: the stream and stream-int code doesn't allow us to perform a
2144 * synchronous send() here unfortunately, because this code is called
2145 * as si_update() from the process_stream() context. This means that
2146 * we have to queue the current cs and defer its processing after the
2147 * connection's cs list is processed anyway.
2148 */
2149
2150 if (cs->flags & CS_FL_DATA_WR_ENA) {
2151 if (LIST_ISEMPTY(&h2s->list)) {
2152 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2153 !h2s->h2c->mbuf->o && // not yet subscribed
2154 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2155 conn_xprt_want_send(cs->conn);
2156 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2157 }
2158 }
2159 else if (!LIST_ISEMPTY(&h2s->list)) {
2160 LIST_DEL(&h2s->list);
2161 LIST_INIT(&h2s->list);
2162 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2163 }
2164
2165 /* this can happen from within si_chk_snd() */
2166 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2167 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002168}
2169
2170/*
2171 * Detach the stream from the connection and possibly release the connection.
2172 */
2173static void h2_detach(struct conn_stream *cs)
2174{
Willy Tarreau60935142017-10-16 18:11:19 +02002175 struct h2s *h2s = cs->ctx;
2176 struct h2c *h2c;
2177
2178 cs->ctx = NULL;
2179 if (!h2s)
2180 return;
2181
2182 h2c = h2s->h2c;
2183 h2s->cs = NULL;
2184
Willy Tarreau45f752e2017-10-30 15:44:59 +01002185 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2186 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2187 /* unblock the connection if it was blocked on this
2188 * stream.
2189 */
2190 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2191 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2192 conn_xprt_want_recv(cs->conn);
2193 conn_xprt_want_send(cs->conn);
2194 }
2195
Willy Tarreau60935142017-10-16 18:11:19 +02002196 if (h2s->by_id.node.leaf_p) {
2197 /* h2s still attached to the h2c */
2198 eb32_delete(&h2s->by_id);
2199
Willy Tarreau3f133572017-10-31 19:21:06 +01002200 if (h2c->task) {
2201 if (eb_is_empty(&h2c->streams_by_id)) {
2202 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2203 task_queue(h2c->task);
2204 }
2205 else
2206 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002207 }
Willy Tarreauea392822017-10-31 10:02:25 +01002208
Willy Tarreau60935142017-10-16 18:11:19 +02002209 /* We don't want to close right now unless we're removing the
2210 * last stream, and either the connection is in error, or it
2211 * reached the ID already specified in a GOAWAY frame received
2212 * or sent (as seen by last_sid >= 0). A timer should be armed
2213 * to kill the connection after some idle time though.
2214 */
2215 if (eb_is_empty(&h2c->streams_by_id) &&
2216 (conn_xprt_read0_pending(h2c->conn) ||
2217 (h2c->conn->flags & CO_FL_ERROR) ||
2218 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2219 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))) {
2220 /* no more stream will come, kill it now */
2221 h2_release(h2c->conn);
2222 }
2223 }
2224 pool_free2(pool2_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002225}
2226
2227static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2228{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002229 struct h2s *h2s = cs->ctx;
2230
2231 if (!mode)
2232 return;
2233
2234 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
2235 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
2236 return;
2237
2238 if (h2c_send_rst_stream(h2s->h2c, h2s) <= 0)
2239 return;
2240
2241 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2242 conn_xprt_want_send(cs->conn);
2243
2244 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002245}
2246
2247static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2248{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002249 struct h2s *h2s = cs->ctx;
2250
2251 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
2252 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
2253 return;
2254
2255 if (h2s->h2c->flags & H2_CF_HEADERS_SENT) {
2256 if (h2_send_empty_data_es(h2s) <= 0)
2257 return;
2258 } else {
Willy Tarreaua1349f02017-10-31 07:41:55 +01002259 /* let's signal a wish to close the connection if no headers
2260 * were seen as this usually means it's a tcp-request rule which
2261 * has aborted the response.
2262 */
2263 if (!(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2264 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2265 return;
2266
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002267 if (h2c_send_rst_stream(h2s->h2c, h2s) <= 0)
2268 return;
2269 }
2270
2271 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2272 conn_xprt_want_send(cs->conn);
2273
2274 if (h2s->st == H2_SS_OPEN && !(h2s->flags & H2_SF_RST_SENT))
2275 h2s->st = H2_SS_HLOC;
2276 else
2277 h2s->st = H2_SS_CLOSED;
2278
Willy Tarreau62f52692017-10-08 23:01:42 +02002279}
2280
Willy Tarreau13278b42017-10-13 19:23:14 +02002281/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2282 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2283 * proceed. Stream errors are reported in h2s->errcode and connection errors
2284 * in h2c->errcode. The caller must already have checked the frame header and
2285 * ensured that the frame was complete or the buffer full.
2286 */
2287static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2288{
2289 struct h2c *h2c = h2s->h2c;
2290 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002291 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002292 int flen = h2c->dfl;
2293 int outlen = 0;
2294 int wrap;
2295 int try;
2296
2297 if (!h2c->dfl) {
2298 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2299 return 0;
2300 }
2301
2302 /* if the input buffer wraps, take a temporary copy of it (rare) */
2303 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2304 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002305 copy = alloc_trash_chunk();
2306 if (!copy) {
2307 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2308 goto fail;
2309 }
2310 memcpy(copy->str, h2c->dbuf->p, wrap);
2311 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2312 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002313 }
2314
2315 /* The padlen is the first byte before data, and the padding appears
2316 * after data. padlen+data+padding are included in flen.
2317 */
2318 if (h2c->dff & H2_F_HEADERS_PADDED) {
2319 if (*hdrs >= flen) {
2320 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2321 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2322 h2c->st0 = H2_SS_ERROR;
2323 return 0;
2324 }
2325 flen -= *hdrs + 1;
2326 hdrs += 1; // skip Pad Length
2327 }
2328
2329 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2330 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2331 hdrs += 5; // stream dep = 4, weight = 1
2332 flen -= 5;
2333 }
2334
2335 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2336 * don't support this for now and can't even decompress so we have to
2337 * break the connection.
2338 */
2339 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2340 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002341 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002342 }
2343
2344 do {
2345 /* first check if we have some room after p+i */
2346 try = buf->data + buf->size - (buf->p + buf->i);
2347
2348 /* otherwise continue between data and p-o */
2349 if (try <= 0) {
2350 try = buf->p - (buf->data + buf->o);
2351 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002352 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002353 }
2354 if (try > count)
2355 try = count;
2356
2357 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, bi_end(buf), try);
2358 if (outlen == -HPACK_ERR_TOO_LARGE) {
2359 if (buffer_space_wraps(buf)) {
2360 /* it doesn't fit and the buffer is fragmented,
2361 * so let's defragment it and try again.
2362 */
2363 buffer_slow_realign(buf);
2364 }
2365 else if (buf->o) {
2366 /* need to let the output buffer flush and
2367 * mark the buffer for later wake up.
2368 */
Willy Tarreau68dd9852017-07-03 14:44:26 +02002369 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002370 }
2371 else {
2372 /* no other way around */
2373 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002374 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002375 }
2376 }
2377 else if (outlen < 0) {
2378 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002379 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002380 }
2381 } while (outlen < 0);
2382
2383 /* now consume the input data */
2384 bi_del(h2c->dbuf, h2c->dfl);
2385 h2c->st0 = H2_CS_FRAME_H;
2386 buf->i += outlen;
2387
2388 /* don't send it before returning data!
2389 * FIXME: should we instead try to send it much later, after the
2390 * response ? This would require that we keep a copy of it in h2s.
2391 */
2392 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2393 h2s->cs->flags |= CS_FL_EOS;
2394 h2s->flags |= H2_SF_ES_RCVD;
2395 }
2396
Willy Tarreau68dd9852017-07-03 14:44:26 +02002397 leave:
2398 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002399 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002400 fail:
2401 outlen = 0;
2402 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002403}
2404
Willy Tarreau454f9052017-10-26 19:40:35 +02002405/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2406 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2407 * in use, a new chunk is emitted for each frame. This is supposed to fit
2408 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2409 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2410 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2411 * parser state is automatically updated. Returns the number of bytes emitted
2412 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2413 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2414 * have checked the frame header and ensured that the frame was complete or the
2415 * buffer full. It changes the frame state to FRAME_A once done.
2416 */
2417static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2418{
2419 struct h2c *h2c = h2s->h2c;
2420 int block1, block2;
2421 unsigned int flen = h2c->dfl;
2422 unsigned int padlen = 0;
2423 int offset = 0;
2424
2425 if (h2c->dbuf->i < flen)
2426 return 0;
2427
2428 /* The padlen is the first byte before data, and the padding appears
2429 * after data. padlen+data+padding are included in flen.
2430 */
2431 if (h2c->dff & H2_F_HEADERS_PADDED) {
2432 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2433 if (padlen >= flen) {
2434 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2435 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2436 h2c->st0 = H2_SS_ERROR;
2437 return 0;
2438 }
2439 flen -= padlen + 1;
2440 offset = 1; // skip Pad Length
2441 }
2442
2443 /* does it fit in output buffer or should we wait ? */
2444 if (buf->i + buf->o + flen > buf->size) {
2445 h2c->flags |= H2_CF_DEM_SFULL;
2446 return 0;
2447 }
2448
2449 /* Block1 is the length of the first block before the buffer wraps,
2450 * block2 is the optional second block to reach the end of the frame.
2451 */
2452 block1 = bi_contig_data(h2c->dbuf);
2453 if (block1 > offset + flen)
2454 block1 = offset + flen;
2455 block1 -= offset; // skip Pad Length
2456 block2 = flen - block1;
2457
2458 if (block1)
2459 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2460
2461 if (block2)
2462 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2463
2464 /* now mark the input data as consumed (will be deleted from the buffer
2465 * by the caller when seeing FRAME_A after sending the window update).
2466 */
2467 h2c->rcvd_c += h2c->dfl;
2468 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2469 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2470
2471 /* don't send it before returning data!
2472 * FIXME: should we instead try to send it much later, after the
2473 * response ? This would require that we keep a copy of it in h2s.
2474 */
2475 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2476 h2s->cs->flags |= CS_FL_EOS;
2477 h2s->flags |= H2_SF_ES_RCVD;
2478 }
2479
2480 return flen;
2481}
2482
Willy Tarreau62f52692017-10-08 23:01:42 +02002483/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002484 * Called from the upper layer to get more data, up to <count> bytes. The
2485 * caller is responsible for never asking for more data than what is available
2486 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002487 */
2488static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2489{
Willy Tarreau13278b42017-10-13 19:23:14 +02002490 struct h2s *h2s = cs->ctx;
2491 struct h2c *h2c = h2s->h2c;
2492 int ret = 0;
2493
2494 if (h2c->st0 != H2_CS_FRAME_P)
2495 return 0; // no pre-parsed frame yet
2496
2497 if (h2c->dsi != h2s->id)
2498 return 0; // not for us
2499
2500 if (!h2c->dbuf->size)
2501 return 0; // empty buffer
2502
2503 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2504 return 0; // incomplete input frame
2505
2506 switch (h2c->dft) {
2507 case H2_FT_HEADERS:
2508 ret = h2_frt_decode_headers(h2s, buf, count);
2509 break;
2510
Willy Tarreau454f9052017-10-26 19:40:35 +02002511 case H2_FT_DATA:
2512 ret = h2_frt_transfer_data(h2s, buf, count);
2513 break;
2514
Willy Tarreau13278b42017-10-13 19:23:14 +02002515 default:
2516 ret = 0;
2517 }
2518 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002519}
2520
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002521/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2522 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2523 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2524 * to the number of buffer bytes consumed.
2525 */
2526static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2527{
2528 struct http_hdr list[MAX_HTTP_HDR];
2529 struct h2c *h2c = h2s->h2c;
2530 struct h1m *h1m = &h2s->res;
2531 struct chunk outbuf;
2532 int es_now = 0;
2533 int ret = 0;
2534 int hdr;
2535
2536 if (h2c_mux_busy(h2c, h2s)) {
2537 h2s->flags |= H2_SF_BLK_MBUSY;
2538 return 0;
2539 }
2540
2541 if (!h2_get_mbuf(h2c)) {
2542 h2c->flags |= H2_CF_MUX_MALLOC;
2543 h2s->flags |= H2_SF_BLK_MROOM;
2544 return 0;
2545 }
2546
2547 /* First, try to parse the H1 response and index it into <list>.
2548 * NOTE! Since it comes from haproxy, we *know* that a response header
2549 * block does not wrap and we can safely read it this way without
2550 * having to realign the buffer.
2551 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002552 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002553 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2554 list, sizeof(list)/sizeof(list[0]), h1m);
2555 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002556 /* incomplete or invalid response, this is abnormal coming from
2557 * haproxy and may only result in a bad errorfile or bad Lua code
2558 * so that won't be fixed, raise an error now.
2559 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002560 * FIXME: we should instead add the ability to only return a
2561 * 502 bad gateway. But in theory this is not supposed to
2562 * happen.
2563 */
2564 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2565 ret = 0;
2566 goto end;
2567 }
2568
2569 chunk_reset(&outbuf);
2570
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002571 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002572 while (1) {
2573 outbuf.str = bo_end(h2c->mbuf);
2574 outbuf.size = bo_contig_space(h2c->mbuf);
2575 outbuf.len = 0;
2576
2577 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2578 break;
2579 realign_again:
2580 buffer_slow_realign(h2c->mbuf);
2581 }
2582
2583 if (outbuf.size < 9) {
2584 h2c->flags |= H2_CF_MUX_MFULL;
2585 h2s->flags |= H2_SF_BLK_MROOM;
2586 ret = 0;
2587 goto end;
2588 }
2589
2590 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2591 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2592 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2593 outbuf.len = 9;
2594
2595 /* encode status, which necessarily is the first one */
2596 if (outbuf.len < outbuf.size && h1m->status == 200)
2597 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2598 else if (outbuf.len < outbuf.size && h1m->status == 304)
2599 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
2600 else if (list[0].v.len == 3 && outbuf.len + 2 + 3 <= outbuf.size) {
2601 /* basic encoding of the status code */
2602 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2603 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2604 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2605 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2606 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2607 }
2608 else {
2609 if (buffer_space_wraps(h2c->mbuf))
2610 goto realign_again;
2611
2612 h2c->flags |= H2_CF_MUX_MFULL;
2613 h2s->flags |= H2_SF_BLK_MROOM;
2614 ret = 0;
2615 goto end;
2616 }
2617
2618 /* encode all headers, stop at empty name */
2619 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002620 /* these ones do not exist in H2 and must be dropped. But if we
2621 * see "connection: close", we also perform a graceful shutdown
2622 * on the connection. Note that the match is not perfect but it
2623 * is sufficient for dealing with some deny rules.
2624 */
2625 if (isteq(list[hdr].n, ist("connection"))) {
2626 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2627 word_match(list[hdr].v.ptr, list[hdr].v.len, "close", 5)) {
2628 if (h2c->last_sid < 0)
2629 h2c->last_sid = (1U << 31) - 1;
2630 if (h2c_send_goaway_error(h2c, h2s) <= 0) {
2631 ret = 0;
2632 goto end;
2633 }
2634 /* OK sent, but this changed the output buffer's
2635 * contents hence the write position.
2636 */
2637 goto try_again;
2638 }
2639 continue;
2640 }
2641 else if (isteq(list[hdr].n, ist("proxy-connection")) ||
2642 isteq(list[hdr].n, ist("keep-alive")) ||
2643 isteq(list[hdr].n, ist("upgrade")) ||
2644 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002645 continue;
2646
2647 if (isteq(list[hdr].n, ist("")))
2648 break; // end
2649
2650 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2651 /* output full */
2652 if (buffer_space_wraps(h2c->mbuf))
2653 goto realign_again;
2654
2655 h2c->flags |= H2_CF_MUX_MFULL;
2656 h2s->flags |= H2_SF_BLK_MROOM;
2657 ret = 0;
2658 goto end;
2659 }
2660 }
2661
2662 /* we may need to add END_STREAM */
2663 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2664 es_now = 1;
2665
2666 /* update the frame's size */
2667 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2668
2669 if (es_now)
2670 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2671
2672 /* consume incoming H1 response */
2673 bo_del(buf, ret);
2674
2675 /* commit the H2 response */
2676 h2c->mbuf->o += outbuf.len;
2677 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
2678 h2c->flags |= H2_CF_HEADERS_SENT;
2679
2680 /* for now we don't implemented CONTINUATION, so we wait for a
2681 * body or directly end in TRL2.
2682 */
2683 if (es_now) {
2684 h1m->state = HTTP_MSG_DONE;
2685 h2s->flags |= H2_SF_ES_SENT;
2686 if (h2s->st == H2_SS_OPEN)
2687 h2s->st = H2_SS_HLOC;
2688 else
2689 h2s->st = H2_SS_CLOSED;
2690 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002691 else if (h1m->status >= 100 && h1m->status < 200) {
2692 h1m->state = HTTP_MSG_RPBEFORE;
2693 h1m->status = 0;
2694 h1m->flags = 0;
2695 goto next_header_block;
2696 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002697 else
2698 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2699
2700 end:
2701 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1_msg_state_str(h1m->err_state));
2702 return ret;
2703}
2704
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002705/* Try to send a DATA frame matching HTTP/1 response present in the response
2706 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2707 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2708 * corresponds to the number of buffer bytes consumed.
2709 */
2710static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2711{
2712 struct h2c *h2c = h2s->h2c;
2713 struct h1m *h1m = &h2s->res;
2714 struct chunk outbuf;
2715 int ret = 0;
2716 int total = 0;
2717 int es_now = 0;
2718 int size = 0;
2719 char *blk1, *blk2;
2720 int len1, len2;
2721
2722 if (h2c_mux_busy(h2c, h2s)) {
2723 h2s->flags |= H2_SF_BLK_MBUSY;
2724 goto end;
2725 }
2726
2727 if (!h2_get_mbuf(h2c)) {
2728 h2c->flags |= H2_CF_MUX_MALLOC;
2729 h2s->flags |= H2_SF_BLK_MROOM;
2730 goto end;
2731 }
2732
2733 new_frame:
2734 if (!buf->o)
2735 goto end;
2736
2737 chunk_reset(&outbuf);
2738
2739 while (1) {
2740 outbuf.str = bo_end(h2c->mbuf);
2741 outbuf.size = bo_contig_space(h2c->mbuf);
2742 outbuf.len = 0;
2743
2744 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2745 break;
2746 realign_again:
2747 buffer_slow_realign(h2c->mbuf);
2748 }
2749
2750 if (outbuf.size < 9) {
2751 h2c->flags |= H2_CF_MUX_MFULL;
2752 h2s->flags |= H2_SF_BLK_MROOM;
2753 goto end;
2754 }
2755
2756 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2757 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2758 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2759 outbuf.len = 9;
2760
2761 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2762 case 0: /* no content length, read till SHUTW */
2763 size = buf->o;
2764 break;
2765 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2766 size = buf->o;
2767 if ((long long)size > h1m->curr_len)
2768 size = h1m->curr_len;
2769 break;
2770 default: /* te:chunked : parse chunks */
2771 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2772 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2773 if (!ret)
2774 goto end;
2775
2776 if (ret < 0) {
2777 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2778 h1m->err_pos = ret;
2779 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2780 goto end;
2781 }
2782 bo_del(buf, ret);
2783 total += ret;
2784 h1m->state = HTTP_MSG_CHUNK_SIZE;
2785 }
2786
2787 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2788 unsigned int chunk;
2789
2790 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2791 if (!ret)
2792 goto end;
2793
2794 if (ret < 0) {
2795 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2796 h1m->err_pos = ret;
2797 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2798 goto end;
2799 }
2800
2801 size = chunk;
2802 h1m->curr_len = chunk;
2803 h1m->body_len += chunk;
2804 bo_del(buf, ret);
2805 total += ret;
2806 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2807 if (!size)
2808 goto send_empty;
2809 }
2810
2811 /* in MSG_DATA state, continue below */
2812 size = h1m->curr_len;
2813 break;
2814 }
2815
2816 /* we have in <size> the exact number of bytes we need to copy from
2817 * the H1 buffer. We need to check this against the connection's and
2818 * the stream's send windows, and to ensure that this fits in the max
2819 * frame size and in the buffer's available space minus 9 bytes (for
2820 * the frame header). The connection's flow control is applied last so
2821 * that we can use a separate list of streams which are immediately
2822 * unblocked on window opening. Note: we don't implement padding.
2823 */
2824
2825 if (size > buf->o)
2826 size = buf->o;
2827
2828 if (size > h2s->mws)
2829 size = h2s->mws;
2830
2831 if (size <= 0) {
2832 h2s->flags |= H2_SF_BLK_SFCTL;
2833 goto end;
2834 }
2835
2836 if (h2c->mfs && size > h2c->mfs)
2837 size = h2c->mfs;
2838
2839 if (size + 9 > outbuf.size) {
2840 /* we have an opportunity for enlarging the too small
2841 * available space, let's try.
2842 */
2843 if (buffer_space_wraps(h2c->mbuf))
2844 goto realign_again;
2845 size = outbuf.size - 9;
2846 }
2847
2848 if (size <= 0) {
2849 h2c->flags |= H2_CF_MUX_MFULL;
2850 h2s->flags |= H2_SF_BLK_MROOM;
2851 goto end;
2852 }
2853
2854 if (size > h2c->mws)
2855 size = h2c->mws;
2856
2857 if (size <= 0) {
2858 h2s->flags |= H2_SF_BLK_MFCTL;
2859 goto end;
2860 }
2861
2862 /* copy whatever we can */
2863 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2864 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2865 if (ret == 1)
2866 len2 = 0;
2867
2868 if (!ret || len1 + len2 < size) {
2869 /* FIXME: must normally never happen */
2870 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2871 goto end;
2872 }
2873
2874 /* limit len1/len2 to size */
2875 if (len1 + len2 > size) {
2876 int sub = len1 + len2 - size;
2877
2878 if (len2 > sub)
2879 len2 -= sub;
2880 else {
2881 sub -= len2;
2882 len2 = 0;
2883 len1 -= sub;
2884 }
2885 }
2886
2887 /* now let's copy this this into the output buffer */
2888 memcpy(outbuf.str + 9, blk1, len1);
2889 if (len2)
2890 memcpy(outbuf.str + 9 + len1, blk2, len2);
2891
2892 send_empty:
2893 /* we may need to add END_STREAM */
2894 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
2895 * could rely on the MSG_MORE flag as a hint for this ?
2896 */
2897 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
2898 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
2899 es_now = 1;
2900
2901 /* update the frame's size */
2902 h2_set_frame_size(outbuf.str, size);
2903
2904 if (es_now)
2905 outbuf.str[4] |= H2_F_DATA_END_STREAM;
2906
2907 /* commit the H2 response */
2908 h2c->mbuf->o += size + 9;
2909 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
2910
2911 /* consume incoming H1 response */
2912 if (size > 0) {
2913 bo_del(buf, size);
2914 total += size;
2915 h1m->curr_len -= size;
2916 h2s->mws -= size;
2917 h2c->mws -= size;
2918
2919 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
2920 h1m->state = HTTP_MSG_CHUNK_CRLF;
2921 goto new_frame;
2922 }
2923 }
2924
2925 if (es_now) {
2926 if (h2s->st == H2_SS_OPEN)
2927 h2s->st = H2_SS_HLOC;
2928 else
2929 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002930
2931 if (!(h1m->flags & H1_MF_CHNK))
2932 h1m->state = HTTP_MSG_DONE;
2933
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002934 h2s->flags |= H2_SF_ES_SENT;
2935 }
2936
2937 end:
2938 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o);
2939 return total;
2940}
2941
Willy Tarreau62f52692017-10-08 23:01:42 +02002942/* Called from the upper layer, to send data */
2943static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
2944{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002945 struct h2s *h2s = cs->ctx;
2946 int total = 0;
2947
2948 //fprintf(stderr, "cs=%p h2s=%p rqst=%d rsst=%d\n", cs, h2s, h2s->req.state, h2s->res.state);
2949 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
2950 if (h2s->res.state < HTTP_MSG_BODY) {
2951 total += h2s_frt_make_resp_headers(h2s, buf);
2952
2953 if (h2s->st == H2_SS_ERROR)
2954 break;
2955
2956 if (h2s->flags & H2_SF_BLK_ANY)
2957 break;
2958 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002959 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
2960 total += h2s_frt_make_resp_data(h2s, buf);
2961
2962 if (h2s->st == H2_SS_ERROR)
2963 break;
2964
2965 if (h2s->flags & H2_SF_BLK_ANY)
2966 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002967 }
2968 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
2969 /* consume the trailers if any (we don't forward them for now) */
2970 int count = h1_measure_trailers(buf);
2971
2972 if (unlikely(count <= 0)) {
2973 if (count < 0)
2974 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2975 break;
2976 }
2977 total += count;
2978 bo_del(buf, count);
2979 h2s->res.state = HTTP_MSG_DONE;
2980 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002981 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002982 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002983 cs->flags |= CS_FL_ERROR;
2984 break;
2985 }
2986 }
2987
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002988 if (h2s->flags & H2_SF_BLK_SFCTL) {
2989 /* stream flow control, quit the list */
2990 LIST_DEL(&h2s->list);
2991 LIST_INIT(&h2s->list);
2992 }
2993
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002994 if (h2s->st == H2_SS_ERROR)
2995 cs->flags |= CS_FL_ERROR;
2996
2997 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02002998}
2999
3000
3001/*******************************************************/
3002/* functions below are dedicated to the config parsers */
3003/*******************************************************/
3004
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003005/* config parser for global "tune.h2.header-table-size" */
3006static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3007 struct proxy *defpx, const char *file, int line,
3008 char **err)
3009{
3010 if (too_many_args(1, args, err, NULL))
3011 return -1;
3012
3013 h2_settings_header_table_size = atoi(args[1]);
3014 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3015 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3016 return -1;
3017 }
3018 return 0;
3019}
Willy Tarreau62f52692017-10-08 23:01:42 +02003020
Willy Tarreaue6baec02017-07-27 11:45:11 +02003021/* config parser for global "tune.h2.initial-window-size" */
3022static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3023 struct proxy *defpx, const char *file, int line,
3024 char **err)
3025{
3026 if (too_many_args(1, args, err, NULL))
3027 return -1;
3028
3029 h2_settings_initial_window_size = atoi(args[1]);
3030 if (h2_settings_initial_window_size < 0) {
3031 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3032 return -1;
3033 }
3034 return 0;
3035}
3036
Willy Tarreau5242ef82017-07-27 11:47:28 +02003037/* config parser for global "tune.h2.max-concurrent-streams" */
3038static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3039 struct proxy *defpx, const char *file, int line,
3040 char **err)
3041{
3042 if (too_many_args(1, args, err, NULL))
3043 return -1;
3044
3045 h2_settings_max_concurrent_streams = atoi(args[1]);
3046 if (h2_settings_max_concurrent_streams < 0) {
3047 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3048 return -1;
3049 }
3050 return 0;
3051}
3052
Willy Tarreau62f52692017-10-08 23:01:42 +02003053
3054/****************************************/
3055/* MUX initialization and instanciation */
3056/***************************************/
3057
3058/* The mux operations */
3059const struct mux_ops h2_ops = {
3060 .init = h2_init,
3061 .recv = h2_recv,
3062 .send = h2_send,
3063 .wake = h2_wake,
3064 .update_poll = h2_update_poll,
3065 .rcv_buf = h2_rcv_buf,
3066 .snd_buf = h2_snd_buf,
3067 .attach = h2_attach,
3068 .detach = h2_detach,
3069 .shutr = h2_shutr,
3070 .shutw = h2_shutw,
3071 .release = h2_release,
3072 .name = "H2",
3073};
3074
3075/* ALPN selection : this mux registers ALPN tolen "h2" */
3076static struct alpn_mux_list alpn_mux_h2 =
3077 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3078
3079/* config keyword parsers */
3080static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003081 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003082 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003083 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003084 { 0, NULL, NULL }
3085}};
3086
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003087static void __h2_deinit(void)
3088{
Willy Tarreau18312642017-10-11 07:57:07 +02003089 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003090 pool_destroy2(pool2_h2c);
3091}
3092
Willy Tarreau62f52692017-10-08 23:01:42 +02003093__attribute__((constructor))
3094static void __h2_init(void)
3095{
3096 alpn_register_mux(&alpn_mux_h2);
3097 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003098 hap_register_post_deinit(__h2_deinit);
3099 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +02003100 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003101}