blob: 7109c65af223bc62657a6f96ecce44ae10603983 [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 */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_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
57
58
Willy Tarreau5ab6b572017-09-22 08:05:00 +020059/* H2 connection state, in h2c->st0 */
60enum h2_cs {
61 H2_CS_PREFACE, // init done, waiting for connection preface
62 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
63 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
64 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
65 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
66 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
67 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
68 H2_CS_ENTRIES // must be last
69} __attribute__((packed));
70
71/* H2 connection descriptor */
72struct h2c {
73 struct connection *conn;
74
75 enum h2_cs st0; /* mux state */
76 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
77
78 /* 16 bit hole here */
79 uint32_t flags; /* connection flags: H2_CF_* */
80 int32_t max_id; /* highest ID known on this connection, <0 before preface */
81 uint32_t rcvd_c; /* newly received data to ACK for the connection */
82 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
83
84 /* states for the demux direction */
85 struct hpack_dht *ddht; /* demux dynamic header table */
86 struct buffer *dbuf; /* demux buffer */
87
88 int32_t dsi; /* demux stream ID (<0 = idle) */
89 int32_t dfl; /* demux frame length (if dsi >= 0) */
90 int8_t dft; /* demux frame type (if dsi >= 0) */
91 int8_t dff; /* demux frame flags (if dsi >= 0) */
92 /* 16 bit hole here */
93 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
94
95 /* states for the mux direction */
96 struct buffer *mbuf; /* mux buffer */
97 int32_t msi; /* mux stream ID (<0 = idle) */
98 int32_t mfl; /* mux frame length (if dsi >= 0) */
99 int8_t mft; /* mux frame type (if dsi >= 0) */
100 int8_t mff; /* mux frame flags (if dsi >= 0) */
101 /* 16 bit hole here */
102 int32_t miw; /* mux initial window size for all new streams */
103 int32_t mws; /* mux window size. Can be negative. */
104 int32_t mfs; /* mux's max frame size */
105
Willy Tarreauea392822017-10-31 10:02:25 +0100106 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100107 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100108 unsigned int nb_streams; /* number of streams in the tree */
109 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100110 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200111 struct eb_root streams_by_id; /* all active streams by their ID */
112 struct list send_list; /* list of blocked streams requesting to send */
113 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200114 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200115 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200116};
117
Willy Tarreau18312642017-10-11 07:57:07 +0200118/* H2 stream state, in h2s->st */
119enum h2_ss {
120 H2_SS_IDLE = 0, // idle
121 H2_SS_RLOC, // reserved(local)
122 H2_SS_RREM, // reserved(remote)
123 H2_SS_OPEN, // open
124 H2_SS_HREM, // half-closed(remote)
125 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200126 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200127 H2_SS_CLOSED, // closed
128 H2_SS_ENTRIES // must be last
129} __attribute__((packed));
130
131/* HTTP/2 stream flags (32 bit), in h2s->flags */
132#define H2_SF_NONE 0x00000000
133#define H2_SF_ES_RCVD 0x00000001
134#define H2_SF_ES_SENT 0x00000002
135
136#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
137#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
138
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200139/* stream flags indicating the reason the stream is blocked */
140#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
141#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
142#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
143#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
144#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
145
Willy Tarreau454f9052017-10-26 19:40:35 +0200146/* stream flags indicating how data is supposed to be sent */
147#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
148#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
149
150/* step we're currently in when sending chunks. This is needed because we may
151 * have to transfer chunks as large as a full buffer so there's no room left
152 * for size nor crlf around.
153 */
154#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
155#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
156#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
157
158#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
159
Willy Tarreau67434202017-11-06 20:20:51 +0100160#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100161#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100162
Willy Tarreau18312642017-10-11 07:57:07 +0200163/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
164 * it is being processed in the internal HTTP representation (H1 for now).
165 */
166struct h2s {
167 struct conn_stream *cs;
168 struct h2c *h2c;
169 struct h1m req, res; /* request and response parser state for H1 */
170 struct eb32_node by_id; /* place in h2c's streams_by_id */
171 struct list list; /* position in active/blocked lists if blocked>0 */
172 int32_t id; /* stream ID */
173 uint32_t flags; /* H2_SF_* */
174 int mws; /* mux window size for this stream */
175 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
176 enum h2_ss st;
177};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200178
Willy Tarreauc6405142017-09-21 20:23:50 +0200179/* descriptor for an h2 frame header */
180struct h2_fh {
181 uint32_t len; /* length, host order, 24 bits */
182 uint32_t sid; /* stream id, host order, 31 bits */
183 uint8_t ft; /* frame type */
184 uint8_t ff; /* frame flags */
185};
186
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200187/* a few settings from the global section */
188static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200189static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200190static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200191
Willy Tarreau2a856182017-05-16 15:20:39 +0200192/* a dmumy closed stream */
193static const struct h2s *h2_closed_stream = &(const struct h2s){
194 .cs = NULL,
195 .h2c = NULL,
196 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100197 .errcode = H2_ERR_STREAM_CLOSED,
198 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200199 .id = 0,
200};
201
202/* and a dummy idle stream for use with any unannounced stream */
203static const struct h2s *h2_idle_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200208 .id = 0,
209};
210
Willy Tarreauea392822017-10-31 10:02:25 +0100211static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200212
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200213/*****************************************************/
214/* functions below are for dynamic buffer management */
215/*****************************************************/
216
217/* re-enables receiving on mux <target> after a buffer was allocated. It returns
218 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
219 * if it's impossible to wake up and we prefer to be woken up later.
220 */
221static int h2_dbuf_available(void *target)
222{
223 struct h2c *h2c = target;
224
225 /* take the buffer now as we'll get scheduled waiting for ->wake() */
226 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200227 h2c->flags &= ~H2_CF_DEM_DALLOC;
228 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
229 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200230 return 1;
231 }
232 return 0;
233}
234
235static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
236{
237 struct buffer *buf = NULL;
238
239 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
240 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
241 h2c->dbuf_wait.target = h2c->conn;
242 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200244 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200246 __conn_xprt_stop_recv(h2c->conn);
247 }
248 return buf;
249}
250
251static inline void h2_release_dbuf(struct h2c *h2c)
252{
253 if (h2c->dbuf->size) {
254 b_free(&h2c->dbuf);
255 offer_buffers(h2c->dbuf_wait.target,
256 tasks_run_queue + applets_active_queue);
257 }
258}
259
Willy Tarreau14398122017-09-22 14:26:04 +0200260/* re-enables sending on mux <target> after a buffer was allocated. It returns
261 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
262 * if it's impossible to wake up and we prefer to be woken up later.
263 */
264static int h2_mbuf_available(void *target)
265{
266 struct h2c *h2c = target;
267
268 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
269 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200270 if (h2c->flags & H2_CF_MUX_MALLOC) {
271 h2c->flags &= ~H2_CF_MUX_MALLOC;
272 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
273 conn_xprt_want_send(h2c->conn);
274 }
275
276 if (h2c->flags & H2_CF_DEM_MROOM) {
277 h2c->flags &= ~H2_CF_DEM_MROOM;
278 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
279 conn_xprt_want_recv(h2c->conn);
280 }
281
Willy Tarreau14398122017-09-22 14:26:04 +0200282 /* FIXME: we should in fact call something like h2_update_poll()
283 * now to recompte the polling. For now it will be enough like
284 * this.
285 */
Willy Tarreau14398122017-09-22 14:26:04 +0200286 return 1;
287 }
288 return 0;
289}
290
291static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
292{
293 struct buffer *buf = NULL;
294
295 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
296 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
297 h2c->mbuf_wait.target = h2c;
298 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100299 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200300 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100301 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200302
303 /* FIXME: we should in fact only block the direction being
304 * currently used. For now it will be enough like this.
305 */
306 __conn_xprt_stop_send(h2c->conn);
307 __conn_xprt_stop_recv(h2c->conn);
308 }
309 return buf;
310}
311
312static inline void h2_release_mbuf(struct h2c *h2c)
313{
314 if (h2c->mbuf->size) {
315 b_free(&h2c->mbuf);
316 offer_buffers(h2c->mbuf_wait.target,
317 tasks_run_queue + applets_active_queue);
318 }
319}
320
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200321
Willy Tarreau62f52692017-10-08 23:01:42 +0200322/*****************************************************************/
323/* functions below are dedicated to the mux setup and management */
324/*****************************************************************/
325
Willy Tarreau32218eb2017-09-22 08:07:25 +0200326/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
327static int h2c_frt_init(struct connection *conn)
328{
329 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100330 struct task *t = NULL;
331 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200332
Willy Tarreaubafbe012017-11-24 17:34:44 +0100333 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200334 if (!h2c)
335 goto fail;
336
Willy Tarreau3f133572017-10-31 19:21:06 +0100337
Willy Tarreau599391a2017-11-24 10:16:00 +0100338 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
339 if (tick_isset(sess->fe->timeout.clientfin))
340 h2c->shut_timeout = sess->fe->timeout.clientfin;
341
Willy Tarreau33400292017-11-05 11:23:40 +0100342 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100343 if (tick_isset(h2c->timeout)) {
344 t = task_new(tid_bit);
345 if (!t)
346 goto fail;
347
348 h2c->task = t;
349 t->process = h2_timeout_task;
350 t->context = h2c;
351 t->expire = tick_add(now_ms, h2c->timeout);
352 }
Willy Tarreauea392822017-10-31 10:02:25 +0100353
Willy Tarreau32218eb2017-09-22 08:07:25 +0200354 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
355 if (!h2c->ddht)
356 goto fail;
357
358 /* Initialise the context. */
359 h2c->st0 = H2_CS_PREFACE;
360 h2c->conn = conn;
361 h2c->max_id = -1;
362 h2c->errcode = H2_ERR_NO_ERROR;
363 h2c->flags = H2_CF_NONE;
364 h2c->rcvd_c = 0;
365 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100366 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200367
368 h2c->dbuf = &buf_empty;
369 h2c->dsi = -1;
370 h2c->msi = -1;
371 h2c->last_sid = -1;
372
373 h2c->mbuf = &buf_empty;
374 h2c->miw = 65535; /* mux initial window size */
375 h2c->mws = 65535; /* mux window size */
376 h2c->mfs = 16384; /* initial max frame size */
377 h2c->streams_by_id = EB_ROOT_UNIQUE;
378 LIST_INIT(&h2c->send_list);
379 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200380 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200381 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 conn->mux_ctx = h2c;
383
Willy Tarreau3f133572017-10-31 19:21:06 +0100384 if (t)
385 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100387
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 /* mux->wake will be called soon to complete the operation */
389 return 0;
390 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100391 if (t)
392 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100393 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200394 return -1;
395}
396
Willy Tarreau62f52692017-10-08 23:01:42 +0200397/* Initialize the mux once it's attached. For outgoing connections, the context
398 * is already initialized before installing the mux, so we detect incoming
399 * connections from the fact that the context is still NULL. Returns < 0 on
400 * error.
401 */
402static int h2_init(struct connection *conn)
403{
404 if (conn->mux_ctx) {
405 /* we don't support outgoing connections for now */
406 return -1;
407 }
408
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200410}
411
Willy Tarreau2373acc2017-10-12 17:35:14 +0200412/* returns the stream associated with id <id> or NULL if not found */
413static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
414{
415 struct eb32_node *node;
416
Willy Tarreau2a856182017-05-16 15:20:39 +0200417 if (id > h2c->max_id)
418 return (struct h2s *)h2_idle_stream;
419
Willy Tarreau2373acc2017-10-12 17:35:14 +0200420 node = eb32_lookup(&h2c->streams_by_id, id);
421 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200422 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200423
424 return container_of(node, struct h2s, by_id);
425}
426
Willy Tarreau62f52692017-10-08 23:01:42 +0200427/* release function for a connection. This one should be called to free all
428 * resources allocated to the mux.
429 */
430static void h2_release(struct connection *conn)
431{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 struct h2c *h2c = conn->mux_ctx;
433
434 LIST_DEL(&conn->list);
435
436 if (h2c) {
437 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200438 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200440 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200442
443 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100444 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200445 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100446 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200447
Willy Tarreauea392822017-10-31 10:02:25 +0100448 if (h2c->task) {
449 task_delete(h2c->task);
450 task_free(h2c->task);
451 h2c->task = NULL;
452 }
453
Willy Tarreaubafbe012017-11-24 17:34:44 +0100454 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455 }
456
457 conn->mux = NULL;
458 conn->mux_ctx = NULL;
459
460 conn_stop_tracking(conn);
461 conn_full_close(conn);
462 if (conn->destroy_cb)
463 conn->destroy_cb(conn);
464 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200465}
466
467
Willy Tarreau71681172017-10-23 14:39:06 +0200468/******************************************************/
469/* functions below are for the H2 protocol processing */
470/******************************************************/
471
472/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100473static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200474{
475 return h2s ? h2s->id : 0;
476}
477
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200478/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100479static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200480{
481 if (h2c->msi < 0)
482 return 0;
483
484 if (h2c->msi == h2s_id(h2s))
485 return 0;
486
487 return 1;
488}
489
Willy Tarreau741d6df2017-10-17 08:00:59 +0200490/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100491static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200492{
493 h2c->errcode = err;
494 h2c->st0 = H2_CS_ERROR;
495}
496
Willy Tarreau2e43f082017-10-17 08:03:59 +0200497/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100498static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200499{
500 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
501 h2s->errcode = err;
502 h2s->st = H2_SS_ERROR;
503 if (h2s->cs)
504 h2s->cs->flags |= CS_FL_ERROR;
505 }
506}
507
Willy Tarreaue4820742017-07-27 13:37:23 +0200508/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100509static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200510{
511 uint8_t *out = frame;
512
513 *out = len >> 16;
514 write_n16(out + 1, len);
515}
516
Willy Tarreau54c15062017-10-10 17:10:03 +0200517/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
518 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
519 * the caller's responsibility to verify that there are at least <bytes> bytes
520 * available in the buffer's input prior to calling this function.
521 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100522static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200523 const struct buffer *b, int o)
524{
525 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
526}
527
Willy Tarreau1f094672017-11-20 21:27:45 +0100528static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200529{
530 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
531}
532
Willy Tarreau1f094672017-11-20 21:27:45 +0100533static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200534{
535 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
536}
537
Willy Tarreau1f094672017-11-20 21:27:45 +0100538static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200539{
540 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
541}
542
543
Willy Tarreau715d5312017-07-11 15:20:24 +0200544/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
545 * is not obvious. It turns out that H2 headers are neither aligned nor do they
546 * use regular sizes. And to add to the trouble, the buffer may wrap so each
547 * byte read must be checked. The header is formed like this :
548 *
549 * b0 b1 b2 b3 b4 b5..b8
550 * +----------+---------+--------+----+----+----------------------+
551 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
552 * +----------+---------+--------+----+----+----------------------+
553 *
554 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
555 * we get the sid properly aligned and ordered, and 16 bits of len properly
556 * ordered as well. The type and flags can be extracted using bit shifts from
557 * the word, and only one extra read is needed to fetch len[16:23].
558 * Returns zero if some bytes are missing, otherwise non-zero on success.
559 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200561{
562 uint64_t w;
563
564 if (b->i < 9)
565 return 0;
566
567 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
568 h->len = *b->p << 16;
569 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
570 h->ff = w >> 32;
571 h->ft = w >> 40;
572 h->len += w >> 48;
573 return 1;
574}
575
576/* skip the next 9 bytes corresponding to the frame header possibly parsed by
577 * h2_peek_frame_hdr() above.
578 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100579static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200580{
581 bi_del(b, 9);
582}
583
584/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100585static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200586{
587 int ret;
588
589 ret = h2_peek_frame_hdr(b, h);
590 if (ret > 0)
591 h2_skip_frame_hdr(b);
592 return ret;
593}
594
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200595/* creates a new stream <id> on the h2c connection and returns it, or NULL in
596 * case of memory allocation error.
597 */
598static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
599{
600 struct conn_stream *cs;
601 struct h2s *h2s;
602
Willy Tarreaubafbe012017-11-24 17:34:44 +0100603 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200604 if (!h2s)
605 goto out;
606
607 h2s->h2c = h2c;
608 h2s->mws = h2c->miw;
609 h2s->flags = H2_SF_NONE;
610 h2s->errcode = H2_ERR_NO_ERROR;
611 h2s->st = H2_SS_IDLE;
612 h1m_init(&h2s->req);
613 h1m_init(&h2s->res);
614 h2s->by_id.key = h2s->id = id;
615 h2c->max_id = id;
616 LIST_INIT(&h2s->list);
617
618 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100619 h2c->nb_streams++;
620 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
621 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200622
623 cs = cs_new(h2c->conn);
624 if (!cs)
625 goto out_close;
626
627 h2s->cs = cs;
628 cs->ctx = h2s;
629
630 if (stream_create_from_cs(cs) < 0)
631 goto out_free_cs;
632
633 /* OK done, the stream lives its own life now */
634 return h2s;
635
636 out_free_cs:
637 cs_free(cs);
638 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100639 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200640 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100641 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200642 h2s = NULL;
643 out:
644 return h2s;
645}
646
Willy Tarreaube5b7152017-09-25 16:25:39 +0200647/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
648 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
649 * the various settings codes.
650 */
651static int h2c_snd_settings(struct h2c *h2c)
652{
653 struct buffer *res;
654 char buf_data[100]; // enough for 15 settings
655 struct chunk buf;
656 int ret;
657
658 if (h2c_mux_busy(h2c, NULL)) {
659 h2c->flags |= H2_CF_DEM_MBUSY;
660 return 0;
661 }
662
663 res = h2_get_mbuf(h2c);
664 if (!res) {
665 h2c->flags |= H2_CF_MUX_MALLOC;
666 h2c->flags |= H2_CF_DEM_MROOM;
667 return 0;
668 }
669
670 chunk_init(&buf, buf_data, sizeof(buf_data));
671 chunk_memcpy(&buf,
672 "\x00\x00\x00" /* length : 0 for now */
673 "\x04\x00" /* type : 4 (settings), flags : 0 */
674 "\x00\x00\x00\x00", /* stream ID : 0 */
675 9);
676
677 if (h2_settings_header_table_size != 4096) {
678 char str[6] = "\x00\x01"; /* header_table_size */
679
680 write_n32(str + 2, h2_settings_header_table_size);
681 chunk_memcat(&buf, str, 6);
682 }
683
684 if (h2_settings_initial_window_size != 65535) {
685 char str[6] = "\x00\x04"; /* initial_window_size */
686
687 write_n32(str + 2, h2_settings_initial_window_size);
688 chunk_memcat(&buf, str, 6);
689 }
690
691 if (h2_settings_max_concurrent_streams != 0) {
692 char str[6] = "\x00\x03"; /* max_concurrent_streams */
693
694 /* Note: 0 means "unlimited" for haproxy's config but not for
695 * the protocol, so never send this value!
696 */
697 write_n32(str + 2, h2_settings_max_concurrent_streams);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 if (global.tune.bufsize != 16384) {
702 char str[6] = "\x00\x05"; /* max_frame_size */
703
704 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
705 * match bufsize - rewrite size, but at the moment it seems
706 * that clients don't take care of it.
707 */
708 write_n32(str + 2, global.tune.bufsize);
709 chunk_memcat(&buf, str, 6);
710 }
711
712 h2_set_frame_size(buf.str, buf.len - 9);
713 ret = bo_istput(res, ist2(buf.str, buf.len));
714 if (unlikely(ret <= 0)) {
715 if (!ret) {
716 h2c->flags |= H2_CF_MUX_MFULL;
717 h2c->flags |= H2_CF_DEM_MROOM;
718 return 0;
719 }
720 else {
721 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
722 return 0;
723 }
724 }
725 return ret;
726}
727
Willy Tarreau52eed752017-09-22 15:05:09 +0200728/* Try to receive a connection preface, then upon success try to send our
729 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
730 * missing data. It may return an error in h2c.
731 */
732static int h2c_frt_recv_preface(struct h2c *h2c)
733{
734 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200735 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200736
737 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
738
739 if (unlikely(ret1 <= 0)) {
740 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
741 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
742 return 0;
743 }
744
Willy Tarreaube5b7152017-09-25 16:25:39 +0200745 ret2 = h2c_snd_settings(h2c);
746 if (ret2 > 0)
747 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200748
Willy Tarreaube5b7152017-09-25 16:25:39 +0200749 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200750}
751
Willy Tarreau081d4722017-05-16 21:51:05 +0200752/* try to send a GOAWAY frame on the connection to report an error or a graceful
753 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
754 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
755 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
756 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
757 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
758 * on unrecoverable failure. It will not attempt to send one again in this last
759 * case so that it is safe to use h2c_error() to report such errors.
760 */
761static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
762{
763 struct buffer *res;
764 char str[17];
765 int ret;
766
767 if (h2c->flags & H2_CF_GOAWAY_FAILED)
768 return 1; // claim that it worked
769
770 if (h2c_mux_busy(h2c, h2s)) {
771 if (h2s)
772 h2s->flags |= H2_SF_BLK_MBUSY;
773 else
774 h2c->flags |= H2_CF_DEM_MBUSY;
775 return 0;
776 }
777
778 res = h2_get_mbuf(h2c);
779 if (!res) {
780 h2c->flags |= H2_CF_MUX_MALLOC;
781 if (h2s)
782 h2s->flags |= H2_SF_BLK_MROOM;
783 else
784 h2c->flags |= H2_CF_DEM_MROOM;
785 return 0;
786 }
787
788 /* len: 8, type: 7, flags: none, sid: 0 */
789 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
790
791 if (h2c->last_sid < 0)
792 h2c->last_sid = h2c->max_id;
793
794 write_n32(str + 9, h2c->last_sid);
795 write_n32(str + 13, h2c->errcode);
796 ret = bo_istput(res, ist2(str, 17));
797 if (unlikely(ret <= 0)) {
798 if (!ret) {
799 h2c->flags |= H2_CF_MUX_MFULL;
800 if (h2s)
801 h2s->flags |= H2_SF_BLK_MROOM;
802 else
803 h2c->flags |= H2_CF_DEM_MROOM;
804 return 0;
805 }
806 else {
807 /* we cannot report this error using GOAWAY, so we mark
808 * it and claim a success.
809 */
810 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
811 h2c->flags |= H2_CF_GOAWAY_FAILED;
812 return 1;
813 }
814 }
815 h2c->flags |= H2_CF_GOAWAY_SENT;
816 return ret;
817}
818
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100819/* Try to send an RST_STREAM frame on the connection for the indicated stream
820 * during mux operations. This stream must be valid and cannot be closed
821 * already. h2s->id will be used for the stream ID and h2s->errcode will be
822 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
823 * not yet.
824 *
825 * Returns > 0 on success or zero if nothing was done. In case of lack of room
826 * to write the message, it subscribes the stream to future notifications.
827 */
828static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
829{
830 struct buffer *res;
831 char str[13];
832 int ret;
833
834 if (!h2s || h2s->st == H2_SS_CLOSED)
835 return 1;
836
837 if (h2c_mux_busy(h2c, h2s)) {
838 h2s->flags |= H2_SF_BLK_MBUSY;
839 return 0;
840 }
841
842 res = h2_get_mbuf(h2c);
843 if (!res) {
844 h2c->flags |= H2_CF_MUX_MALLOC;
845 h2s->flags |= H2_SF_BLK_MROOM;
846 return 0;
847 }
848
849 /* len: 4, type: 3, flags: none */
850 memcpy(str, "\x00\x00\x04\x03\x00", 5);
851 write_n32(str + 5, h2s->id);
852 write_n32(str + 9, h2s->errcode);
853 ret = bo_istput(res, ist2(str, 13));
854
855 if (unlikely(ret <= 0)) {
856 if (!ret) {
857 h2c->flags |= H2_CF_MUX_MFULL;
858 h2s->flags |= H2_SF_BLK_MROOM;
859 return 0;
860 }
861 else {
862 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
863 return 0;
864 }
865 }
866
867 h2s->flags |= H2_SF_RST_SENT;
868 h2s->st = H2_SS_CLOSED;
869 return ret;
870}
871
872/* Try to send an RST_STREAM frame on the connection for the stream being
873 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
874 * error code unless the stream's state already is IDLE or CLOSED in which
875 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
876 * it was not yet.
877 *
878 * Returns > 0 on success or zero if nothing was done. In case of lack of room
879 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200880 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100881 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200882 */
883static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
884{
885 struct buffer *res;
886 char str[13];
887 int ret;
888
889 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100890 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200891 return 0;
892 }
893
894 res = h2_get_mbuf(h2c);
895 if (!res) {
896 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200898 return 0;
899 }
900
901 /* len: 4, type: 3, flags: none */
902 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100903
Willy Tarreau27a84c92017-10-17 08:10:17 +0200904 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100905 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200906 h2s->errcode : H2_ERR_STREAM_CLOSED);
907 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100908
Willy Tarreau27a84c92017-10-17 08:10:17 +0200909 if (unlikely(ret <= 0)) {
910 if (!ret) {
911 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100912 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200913 return 0;
914 }
915 else {
916 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
917 return 0;
918 }
919 }
920
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100921 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 h2s->st = H2_SS_CLOSED;
924 }
925
Willy Tarreau27a84c92017-10-17 08:10:17 +0200926 return ret;
927}
928
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100929/* try to send an empty DATA frame with the ES flag set to notify about the
930 * end of stream and match a shutdown(write). If an ES was already sent as
931 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
932 * on success or zero if nothing was done. In case of lack of room to write the
933 * message, it subscribes the requesting stream to future notifications.
934 */
935static int h2_send_empty_data_es(struct h2s *h2s)
936{
937 struct h2c *h2c = h2s->h2c;
938 struct buffer *res;
939 char str[9];
940 int ret;
941
Willy Tarreau721c9742017-11-07 11:05:42 +0100942 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100943 return 1;
944
945 if (h2c_mux_busy(h2c, h2s)) {
946 h2s->flags |= H2_SF_BLK_MBUSY;
947 return 0;
948 }
949
950 res = h2_get_mbuf(h2c);
951 if (!res) {
952 h2c->flags |= H2_CF_MUX_MALLOC;
953 h2s->flags |= H2_SF_BLK_MROOM;
954 return 0;
955 }
956
957 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
958 memcpy(str, "\x00\x00\x00\x00\x01", 5);
959 write_n32(str + 5, h2s->id);
960 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100961 if (likely(ret > 0)) {
962 h2s->flags |= H2_SF_ES_SENT;
963 }
964 else if (!ret) {
965 h2c->flags |= H2_CF_MUX_MFULL;
966 h2s->flags |= H2_SF_BLK_MROOM;
967 return 0;
968 }
969 else {
970 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
971 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100972 }
973 return ret;
974}
975
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100976/* wake the streams attached to the connection, whose id is greater than <last>,
977 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
978 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
979 * stream's state is automatically updated accordingly.
980 */
981static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
982{
983 struct eb32_node *node;
984 struct h2s *h2s;
985
986 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
987 flags |= CS_FL_ERROR;
988
989 if (conn_xprt_read0_pending(h2c->conn))
990 flags |= CS_FL_EOS;
991
992 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
993 while (node) {
994 h2s = container_of(node, struct h2s, by_id);
995 if (h2s->id <= last)
996 break;
997 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100998
999 if (!h2s->cs) {
1000 /* this stream was already orphaned */
Willy Tarreau49745612017-12-03 18:56:02 +01001001 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001002 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001003 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001004 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001005 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001006
1007 h2s->cs->flags |= flags;
1008 /* recv is used to force to detect CS_FL_EOS that wake()
1009 * doesn't handle in the stream int code.
1010 */
1011 h2s->cs->data_cb->recv(h2s->cs);
1012 h2s->cs->data_cb->wake(h2s->cs);
1013
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001014 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1015 h2s->st = H2_SS_ERROR;
1016 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1017 h2s->st = H2_SS_HREM;
1018 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1019 h2s->st = H2_SS_CLOSED;
1020 }
1021}
1022
Willy Tarreau3421aba2017-07-27 15:41:03 +02001023/* Increase all streams' outgoing window size by the difference passed in
1024 * argument. This is needed upon receipt of the settings frame if the initial
1025 * window size is different. The difference may be negative and the resulting
1026 * window size as well, for the time it takes to receive some window updates.
1027 */
1028static void h2c_update_all_ws(struct h2c *h2c, int diff)
1029{
1030 struct h2s *h2s;
1031 struct eb32_node *node;
1032
1033 if (!diff)
1034 return;
1035
1036 node = eb32_first(&h2c->streams_by_id);
1037 while (node) {
1038 h2s = container_of(node, struct h2s, by_id);
1039 h2s->mws += diff;
1040 node = eb32_next(node);
1041 }
1042}
1043
1044/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1045 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1046 * return an error in h2c. Described in RFC7540#6.5.
1047 */
1048static int h2c_handle_settings(struct h2c *h2c)
1049{
1050 unsigned int offset;
1051 int error;
1052
1053 if (h2c->dff & H2_F_SETTINGS_ACK) {
1054 if (h2c->dfl) {
1055 error = H2_ERR_FRAME_SIZE_ERROR;
1056 goto fail;
1057 }
1058 return 1;
1059 }
1060
1061 if (h2c->dsi != 0) {
1062 error = H2_ERR_PROTOCOL_ERROR;
1063 goto fail;
1064 }
1065
1066 if (h2c->dfl % 6) {
1067 error = H2_ERR_FRAME_SIZE_ERROR;
1068 goto fail;
1069 }
1070
1071 /* that's the limit we can process */
1072 if (h2c->dfl > global.tune.bufsize) {
1073 error = H2_ERR_FRAME_SIZE_ERROR;
1074 goto fail;
1075 }
1076
1077 /* process full frame only */
1078 if (h2c->dbuf->i < h2c->dfl)
1079 return 0;
1080
1081 /* parse the frame */
1082 for (offset = 0; offset < h2c->dfl; offset += 6) {
1083 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1084 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1085
1086 switch (type) {
1087 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1088 /* we need to update all existing streams with the
1089 * difference from the previous iws.
1090 */
1091 if (arg < 0) { // RFC7540#6.5.2
1092 error = H2_ERR_FLOW_CONTROL_ERROR;
1093 goto fail;
1094 }
1095 h2c_update_all_ws(h2c, arg - h2c->miw);
1096 h2c->miw = arg;
1097 break;
1098 case H2_SETTINGS_MAX_FRAME_SIZE:
1099 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1100 error = H2_ERR_PROTOCOL_ERROR;
1101 goto fail;
1102 }
1103 h2c->mfs = arg;
1104 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001105 case H2_SETTINGS_ENABLE_PUSH:
1106 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1107 error = H2_ERR_PROTOCOL_ERROR;
1108 goto fail;
1109 }
1110 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001111 }
1112 }
1113
1114 /* need to ACK this frame now */
1115 h2c->st0 = H2_CS_FRAME_A;
1116 return 1;
1117 fail:
1118 h2c_error(h2c, error);
1119 return 0;
1120}
1121
1122/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1123 * success or one of the h2_status values.
1124 */
1125static int h2c_ack_settings(struct h2c *h2c)
1126{
1127 struct buffer *res;
1128 char str[9];
1129 int ret = -1;
1130
1131 if (h2c_mux_busy(h2c, NULL)) {
1132 h2c->flags |= H2_CF_DEM_MBUSY;
1133 return 0;
1134 }
1135
1136 res = h2_get_mbuf(h2c);
1137 if (!res) {
1138 h2c->flags |= H2_CF_MUX_MALLOC;
1139 h2c->flags |= H2_CF_DEM_MROOM;
1140 return 0;
1141 }
1142
1143 memcpy(str,
1144 "\x00\x00\x00" /* length : 0 (no data) */
1145 "\x04" "\x01" /* type : 4, flags : ACK */
1146 "\x00\x00\x00\x00" /* stream ID */, 9);
1147
1148 ret = bo_istput(res, ist2(str, 9));
1149 if (unlikely(ret <= 0)) {
1150 if (!ret) {
1151 h2c->flags |= H2_CF_MUX_MFULL;
1152 h2c->flags |= H2_CF_DEM_MROOM;
1153 return 0;
1154 }
1155 else {
1156 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1157 return 0;
1158 }
1159 }
1160 return ret;
1161}
1162
Willy Tarreaucf68c782017-10-10 17:11:41 +02001163/* processes a PING frame and schedules an ACK if needed. The caller must pass
1164 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1165 * missing data. It may return an error in h2c.
1166 */
1167static int h2c_handle_ping(struct h2c *h2c)
1168{
1169 /* frame length must be exactly 8 */
1170 if (h2c->dfl != 8) {
1171 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1172 return 0;
1173 }
1174
1175 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001176 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001177 h2c->st0 = H2_CS_FRAME_A;
1178 return 1;
1179}
1180
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001181/* Try to send a window update for stream id <sid> and value <increment>.
1182 * Returns > 0 on success or zero on missing room or failure. It may return an
1183 * error in h2c.
1184 */
1185static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1186{
1187 struct buffer *res;
1188 char str[13];
1189 int ret = -1;
1190
1191 if (h2c_mux_busy(h2c, NULL)) {
1192 h2c->flags |= H2_CF_DEM_MBUSY;
1193 return 0;
1194 }
1195
1196 res = h2_get_mbuf(h2c);
1197 if (!res) {
1198 h2c->flags |= H2_CF_MUX_MALLOC;
1199 h2c->flags |= H2_CF_DEM_MROOM;
1200 return 0;
1201 }
1202
1203 /* length: 4, type: 8, flags: none */
1204 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1205 write_n32(str + 5, sid);
1206 write_n32(str + 9, increment);
1207
1208 ret = bo_istput(res, ist2(str, 13));
1209
1210 if (unlikely(ret <= 0)) {
1211 if (!ret) {
1212 h2c->flags |= H2_CF_MUX_MFULL;
1213 h2c->flags |= H2_CF_DEM_MROOM;
1214 return 0;
1215 }
1216 else {
1217 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1218 return 0;
1219 }
1220 }
1221 return ret;
1222}
1223
1224/* try to send pending window update for the connection. It's safe to call it
1225 * with no pending updates. Returns > 0 on success or zero on missing room or
1226 * failure. It may return an error in h2c.
1227 */
1228static int h2c_send_conn_wu(struct h2c *h2c)
1229{
1230 int ret = 1;
1231
1232 if (h2c->rcvd_c <= 0)
1233 return 1;
1234
1235 /* send WU for the connection */
1236 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1237 if (ret > 0)
1238 h2c->rcvd_c = 0;
1239
1240 return ret;
1241}
1242
1243/* try to send pending window update for the current dmux stream. It's safe to
1244 * call it with no pending updates. Returns > 0 on success or zero on missing
1245 * room or failure. It may return an error in h2c.
1246 */
1247static int h2c_send_strm_wu(struct h2c *h2c)
1248{
1249 int ret = 1;
1250
1251 if (h2c->rcvd_s <= 0)
1252 return 1;
1253
1254 /* send WU for the stream */
1255 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1256 if (ret > 0)
1257 h2c->rcvd_s = 0;
1258
1259 return ret;
1260}
1261
Willy Tarreaucf68c782017-10-10 17:11:41 +02001262/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1263 * success, 0 on missing data or one of the h2_status values.
1264 */
1265static int h2c_ack_ping(struct h2c *h2c)
1266{
1267 struct buffer *res;
1268 char str[17];
1269 int ret = -1;
1270
1271 if (h2c->dbuf->i < 8)
1272 return 0;
1273
1274 if (h2c_mux_busy(h2c, NULL)) {
1275 h2c->flags |= H2_CF_DEM_MBUSY;
1276 return 0;
1277 }
1278
1279 res = h2_get_mbuf(h2c);
1280 if (!res) {
1281 h2c->flags |= H2_CF_MUX_MALLOC;
1282 h2c->flags |= H2_CF_DEM_MROOM;
1283 return 0;
1284 }
1285
1286 memcpy(str,
1287 "\x00\x00\x08" /* length : 8 (same payload) */
1288 "\x06" "\x01" /* type : 6, flags : ACK */
1289 "\x00\x00\x00\x00" /* stream ID */, 9);
1290
1291 /* copy the original payload */
1292 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1293
1294 ret = bo_istput(res, ist2(str, 17));
1295 if (unlikely(ret <= 0)) {
1296 if (!ret) {
1297 h2c->flags |= H2_CF_MUX_MFULL;
1298 h2c->flags |= H2_CF_DEM_MROOM;
1299 return 0;
1300 }
1301 else {
1302 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1303 return 0;
1304 }
1305 }
1306 return ret;
1307}
1308
Willy Tarreau26f95952017-07-27 17:18:30 +02001309/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1310 * Returns > 0 on success or zero on missing data. It may return an error in
1311 * h2c or h2s. Described in RFC7540#6.9.
1312 */
1313static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1314{
1315 int32_t inc;
1316 int error;
1317
1318 if (h2c->dfl != 4) {
1319 error = H2_ERR_FRAME_SIZE_ERROR;
1320 goto conn_err;
1321 }
1322
1323 /* process full frame only */
1324 if (h2c->dbuf->i < h2c->dfl)
1325 return 0;
1326
1327 inc = h2_get_n32(h2c->dbuf, 0);
1328
1329 if (h2c->dsi != 0) {
1330 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001331
1332 /* it's not an error to receive WU on a closed stream */
1333 if (h2s->st == H2_SS_CLOSED)
1334 return 1;
1335
1336 if (!inc) {
1337 error = H2_ERR_PROTOCOL_ERROR;
1338 goto strm_err;
1339 }
1340
1341 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1342 error = H2_ERR_FLOW_CONTROL_ERROR;
1343 goto strm_err;
1344 }
1345
1346 h2s->mws += inc;
1347 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1348 h2s->flags &= ~H2_SF_BLK_SFCTL;
1349 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1350 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1351 /* This stream wanted to send but could not due to its
1352 * own flow control. We can put it back into the send
1353 * list now, it will be handled upon next send() call.
1354 */
1355 LIST_ADDQ(&h2c->send_list, &h2s->list);
1356 }
1357 }
1358 }
1359 else {
1360 /* connection window update */
1361 if (!inc) {
1362 error = H2_ERR_PROTOCOL_ERROR;
1363 goto conn_err;
1364 }
1365
1366 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1367 error = H2_ERR_FLOW_CONTROL_ERROR;
1368 goto conn_err;
1369 }
1370
1371 h2c->mws += inc;
1372 }
1373
1374 return 1;
1375
1376 conn_err:
1377 h2c_error(h2c, error);
1378 return 0;
1379
1380 strm_err:
1381 if (h2s) {
1382 h2s_error(h2s, error);
1383 h2c->st0 = H2_CS_FRAME_A;
1384 }
1385 else
1386 h2c_error(h2c, error);
1387 return 0;
1388}
1389
Willy Tarreaue96b0922017-10-30 00:28:29 +01001390/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1391 * the last ID. Returns > 0 on success or zero on missing data. It may return
1392 * an error in h2c. Described in RFC7540#6.8.
1393 */
1394static int h2c_handle_goaway(struct h2c *h2c)
1395{
1396 int error;
1397 int last;
1398
1399 if (h2c->dsi != 0) {
1400 error = H2_ERR_PROTOCOL_ERROR;
1401 goto conn_err;
1402 }
1403
1404 if (h2c->dfl < 8) {
1405 error = H2_ERR_FRAME_SIZE_ERROR;
1406 goto conn_err;
1407 }
1408
1409 /* process full frame only */
1410 if (h2c->dbuf->i < h2c->dfl)
1411 return 0;
1412
1413 last = h2_get_n32(h2c->dbuf, 0);
1414 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1415 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001416 if (h2c->last_sid < 0)
1417 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001418 return 1;
1419
1420 conn_err:
1421 h2c_error(h2c, error);
1422 return 0;
1423}
1424
Willy Tarreau92153fc2017-12-03 19:46:19 +01001425/* processes a PRIORITY frame, and either skips it or rejects if it is
1426 * invalid. Returns > 0 on success or zero on missing data. It may return
1427 * an error in h2c. Described in RFC7540#6.3.
1428 */
1429static int h2c_handle_priority(struct h2c *h2c)
1430{
1431 int error;
1432
1433 if (h2c->dsi == 0) {
1434 error = H2_ERR_PROTOCOL_ERROR;
1435 goto conn_err;
1436 }
1437
1438 if (h2c->dfl != 5) {
1439 error = H2_ERR_FRAME_SIZE_ERROR;
1440 goto conn_err;
1441 }
1442
1443 /* process full frame only */
1444 if (h2c->dbuf->i < h2c->dfl)
1445 return 0;
1446
1447 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1448 /* 7540#5.3 : can't depend on itself */
1449 error = H2_ERR_PROTOCOL_ERROR;
1450 goto conn_err;
1451 }
1452 return 1;
1453
1454 conn_err:
1455 h2c_error(h2c, error);
1456 return 0;
1457}
1458
Willy Tarreaucd234e92017-08-18 10:59:39 +02001459/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1460 * Returns > 0 on success or zero on missing data. It may return an error in
1461 * h2c. Described in RFC7540#6.4.
1462 */
1463static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1464{
1465 int error;
1466
1467 if (h2c->dsi == 0) {
1468 error = H2_ERR_PROTOCOL_ERROR;
1469 goto conn_err;
1470 }
1471
Willy Tarreaucd234e92017-08-18 10:59:39 +02001472 if (h2c->dfl != 4) {
1473 error = H2_ERR_FRAME_SIZE_ERROR;
1474 goto conn_err;
1475 }
1476
1477 /* process full frame only */
1478 if (h2c->dbuf->i < h2c->dfl)
1479 return 0;
1480
1481 /* late RST, already handled */
1482 if (h2s->st == H2_SS_CLOSED)
1483 return 1;
1484
1485 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1486 h2s->st = H2_SS_CLOSED;
1487
1488 if (h2s->cs) {
1489 h2s->cs->flags |= CS_FL_EOS;
1490 /* recv is used to force to detect CS_FL_EOS that wake()
1491 * doesn't handle in the stream-int code.
1492 */
1493 h2s->cs->data_cb->recv(h2s->cs);
1494 h2s->cs->data_cb->wake(h2s->cs);
1495 }
1496
1497 h2s->flags |= H2_SF_RST_RCVD;
1498 return 1;
1499
1500 conn_err:
1501 h2c_error(h2c, error);
1502 return 0;
1503}
1504
Willy Tarreau13278b42017-10-13 19:23:14 +02001505/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1506 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1507 * errors here are reported as connection errors since it's impossible to
1508 * recover from such errors after the compression context has been altered.
1509 */
1510static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1511{
1512 int error;
1513
1514 if (!h2c->dfl) {
1515 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1516 goto strm_err;
1517 }
1518
1519 if (!h2c->dbuf->size)
1520 return 0; // empty buffer
1521
1522 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1523 return 0; // incomplete frame
1524
1525 /* now either the frame is complete or the buffer is complete */
1526 if (h2s->st != H2_SS_IDLE) {
1527 /* FIXME: stream already exists, this is only allowed for
1528 * trailers (not supported for now).
1529 */
1530 error = H2_ERR_PROTOCOL_ERROR;
1531 goto conn_err;
1532 }
1533 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1534 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1535 error = H2_ERR_PROTOCOL_ERROR;
1536 goto conn_err;
1537 }
1538
1539 h2s = h2c_stream_new(h2c, h2c->dsi);
1540 if (!h2s) {
1541 error = H2_ERR_INTERNAL_ERROR;
1542 goto conn_err;
1543 }
1544
1545 h2s->st = H2_SS_OPEN;
1546 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1547 h2s->st = H2_SS_HREM;
1548 h2s->flags |= H2_SF_ES_RCVD;
1549 }
1550
1551 /* call the upper layers to process the frame, then let the upper layer
1552 * notify the stream about any change.
1553 */
1554 h2s->cs->data_cb->recv(h2s->cs);
1555
1556 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1557 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1558 error = H2_ERR_INTERNAL_ERROR;
1559 goto conn_err;
1560 }
1561
Willy Tarreau8f650c32017-11-21 19:36:21 +01001562 if (h2c->st0 >= H2_CS_ERROR)
1563 return 0;
1564
Willy Tarreau721c9742017-11-07 11:05:42 +01001565 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001566 /* stream error : send RST_STREAM */
1567 h2c->st0 = H2_CS_FRAME_A;
1568 }
1569 else {
1570 /* update the max stream ID if the request is being processed */
1571 if (h2s->id > h2c->max_id)
1572 h2c->max_id = h2s->id;
1573 }
1574
1575 return 1;
1576
1577 conn_err:
1578 h2c_error(h2c, error);
1579 return 0;
1580
1581 strm_err:
1582 if (h2s) {
1583 h2s_error(h2s, error);
1584 h2c->st0 = H2_CS_FRAME_A;
1585 }
1586 else
1587 h2c_error(h2c, error);
1588 return 0;
1589}
1590
Willy Tarreau454f9052017-10-26 19:40:35 +02001591/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1592 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1593 */
1594static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1595{
1596 int error;
1597
1598 /* note that empty DATA frames are perfectly valid and sometimes used
1599 * to signal an end of stream (with the ES flag).
1600 */
1601
1602 if (!h2c->dbuf->size && h2c->dfl)
1603 return 0; // empty buffer
1604
1605 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1606 return 0; // incomplete frame
1607
1608 /* now either the frame is complete or the buffer is complete */
1609
1610 if (!h2c->dsi) {
1611 /* RFC7540#6.1 */
1612 error = H2_ERR_PROTOCOL_ERROR;
1613 goto conn_err;
1614 }
1615
1616 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1617 /* RFC7540#6.1 */
1618 error = H2_ERR_STREAM_CLOSED;
1619 goto strm_err;
1620 }
1621
1622 /* last frame */
1623 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1624 h2s->st = H2_SS_HREM;
1625 h2s->flags |= H2_SF_ES_RCVD;
1626 }
1627
1628 /* call the upper layers to process the frame, then let the upper layer
1629 * notify the stream about any change.
1630 */
1631 if (!h2s->cs) {
1632 error = H2_ERR_STREAM_CLOSED;
1633 goto strm_err;
1634 }
1635
1636 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001637
Willy Tarreau454f9052017-10-26 19:40:35 +02001638 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1639 /* cs has just been destroyed, we have to kill h2s. */
1640 error = H2_ERR_STREAM_CLOSED;
1641 goto strm_err;
1642 }
1643
Willy Tarreau8f650c32017-11-21 19:36:21 +01001644 if (h2c->st0 >= H2_CS_ERROR)
1645 return 0;
1646
Willy Tarreau721c9742017-11-07 11:05:42 +01001647 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001648 /* stream error : send RST_STREAM */
1649 h2c->st0 = H2_CS_FRAME_A;
1650 }
1651
1652 /* check for completion : the callee will change this to FRAME_A or
1653 * FRAME_H once done.
1654 */
1655 if (h2c->st0 == H2_CS_FRAME_P)
1656 return 0;
1657
1658 return 1;
1659
1660 conn_err:
1661 h2c_error(h2c, error);
1662 return 0;
1663
1664 strm_err:
1665 if (h2s) {
1666 h2s_error(h2s, error);
1667 h2c->st0 = H2_CS_FRAME_A;
1668 }
1669 else
1670 h2c_error(h2c, error);
1671 return 0;
1672}
1673
Willy Tarreaubc933932017-10-09 16:21:43 +02001674/* process Rx frames to be demultiplexed */
1675static void h2_process_demux(struct h2c *h2c)
1676{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001677 struct h2s *h2s;
1678
Willy Tarreau081d4722017-05-16 21:51:05 +02001679 if (h2c->st0 >= H2_CS_ERROR)
1680 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001681
1682 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1683 if (h2c->st0 == H2_CS_PREFACE) {
1684 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1685 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1686 if (h2c->st0 == H2_CS_ERROR)
1687 h2c->st0 = H2_CS_ERROR2;
1688 goto fail;
1689 }
1690
1691 h2c->max_id = 0;
1692 h2c->st0 = H2_CS_SETTINGS1;
1693 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001694
1695 if (h2c->st0 == H2_CS_SETTINGS1) {
1696 struct h2_fh hdr;
1697
1698 /* ensure that what is pending is a valid SETTINGS frame
1699 * without an ACK.
1700 */
1701 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1702 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1703 if (h2c->st0 == H2_CS_ERROR)
1704 h2c->st0 = H2_CS_ERROR2;
1705 goto fail;
1706 }
1707
1708 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1709 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1710 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1711 h2c->st0 = H2_CS_ERROR2;
1712 goto fail;
1713 }
1714
1715 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1716 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1717 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1718 h2c->st0 = H2_CS_ERROR2;
1719 goto fail;
1720 }
1721
1722 /* that's OK, switch to FRAME_P to process it */
1723 h2c->dfl = hdr.len;
1724 h2c->dsi = hdr.sid;
1725 h2c->dft = hdr.ft;
1726 h2c->dff = hdr.ff;
1727 h2c->st0 = H2_CS_FRAME_P;
1728 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001729 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001730
1731 /* process as many incoming frames as possible below */
1732 while (h2c->dbuf->i) {
1733 int ret = 0;
1734
1735 if (h2c->st0 >= H2_CS_ERROR)
1736 break;
1737
1738 if (h2c->st0 == H2_CS_FRAME_H) {
1739 struct h2_fh hdr;
1740
1741 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1742 break;
1743
1744 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1745 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1746 h2c->st0 = H2_CS_ERROR;
1747 break;
1748 }
1749
1750 h2c->dfl = hdr.len;
1751 h2c->dsi = hdr.sid;
1752 h2c->dft = hdr.ft;
1753 h2c->dff = hdr.ff;
1754 h2c->st0 = H2_CS_FRAME_P;
1755 h2_skip_frame_hdr(h2c->dbuf);
1756 }
1757
1758 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001759 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001760
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001761 if (h2s->st == H2_SS_IDLE &&
1762 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1763 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1764 * this state MUST be treated as a connection error
1765 */
1766 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1767 h2c->st0 = H2_CS_ERROR;
1768 break;
1769 }
1770
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001771 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1772 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1773 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1774 * this state MUST be treated as a stream error
1775 */
1776 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1777 goto strm_err;
1778 }
1779
Willy Tarreauc0da1962017-10-30 18:38:00 +01001780#if 0
1781 // problem below: it is not possible to completely ignore such
1782 // streams as we need to maintain the compression state as well
1783 // and for this we need to completely process these frames (eg:
1784 // HEADERS frames) as well as counting DATA frames to emit
1785 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1786 // This is a typical case of layer violation where the
1787 // transported contents are critical to the connection's
1788 // validity and must be ignored at the same time :-(
1789
1790 /* graceful shutdown, ignore streams whose ID is higher than
1791 * the one advertised in GOAWAY. RFC7540#6.8.
1792 */
1793 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1794 ret = MIN(h2c->dbuf->i, h2c->dfl);
1795 bi_del(h2c->dbuf, ret);
1796 h2c->dfl -= ret;
1797 ret = h2c->dfl == 0;
1798 goto strm_err;
1799 }
1800#endif
1801
Willy Tarreau7e98c052017-10-10 15:56:59 +02001802 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001803 case H2_FT_SETTINGS:
1804 if (h2c->st0 == H2_CS_FRAME_P)
1805 ret = h2c_handle_settings(h2c);
1806
1807 if (h2c->st0 == H2_CS_FRAME_A)
1808 ret = h2c_ack_settings(h2c);
1809 break;
1810
Willy Tarreaucf68c782017-10-10 17:11:41 +02001811 case H2_FT_PING:
1812 if (h2c->st0 == H2_CS_FRAME_P)
1813 ret = h2c_handle_ping(h2c);
1814
1815 if (h2c->st0 == H2_CS_FRAME_A)
1816 ret = h2c_ack_ping(h2c);
1817 break;
1818
Willy Tarreau26f95952017-07-27 17:18:30 +02001819 case H2_FT_WINDOW_UPDATE:
1820 if (h2c->st0 == H2_CS_FRAME_P)
1821 ret = h2c_handle_window_update(h2c, h2s);
1822 break;
1823
Willy Tarreau61290ec2017-10-17 08:19:21 +02001824 case H2_FT_CONTINUATION:
1825 /* we currently don't support CONTINUATION frames since
1826 * we have nowhere to store the partial HEADERS frame.
1827 * Let's abort the stream on an INTERNAL_ERROR here.
1828 */
1829 if (h2c->st0 == H2_CS_FRAME_P)
1830 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1831 break;
1832
Willy Tarreau13278b42017-10-13 19:23:14 +02001833 case H2_FT_HEADERS:
1834 if (h2c->st0 == H2_CS_FRAME_P)
1835 ret = h2c_frt_handle_headers(h2c, h2s);
1836 break;
1837
Willy Tarreau454f9052017-10-26 19:40:35 +02001838 case H2_FT_DATA:
1839 if (h2c->st0 == H2_CS_FRAME_P)
1840 ret = h2c_frt_handle_data(h2c, h2s);
1841
1842 if (h2c->st0 == H2_CS_FRAME_A)
1843 ret = h2c_send_strm_wu(h2c);
1844 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001845
Willy Tarreau92153fc2017-12-03 19:46:19 +01001846 case H2_FT_PRIORITY:
1847 if (h2c->st0 == H2_CS_FRAME_P)
1848 ret = h2c_handle_priority(h2c);
1849 break;
1850
Willy Tarreaucd234e92017-08-18 10:59:39 +02001851 case H2_FT_RST_STREAM:
1852 if (h2c->st0 == H2_CS_FRAME_P)
1853 ret = h2c_handle_rst_stream(h2c, h2s);
1854 break;
1855
Willy Tarreaue96b0922017-10-30 00:28:29 +01001856 case H2_FT_GOAWAY:
1857 if (h2c->st0 == H2_CS_FRAME_P)
1858 ret = h2c_handle_goaway(h2c);
1859 break;
1860
Willy Tarreau1c661982017-10-30 13:52:01 +01001861 case H2_FT_PUSH_PROMISE:
1862 /* not permitted here, RFC7540#5.1 */
1863 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001864 break;
1865
1866 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001867 default:
1868 /* drop frames that we ignore. They may be larger than
1869 * the buffer so we drain all of their contents until
1870 * we reach the end.
1871 */
1872 ret = MIN(h2c->dbuf->i, h2c->dfl);
1873 bi_del(h2c->dbuf, ret);
1874 h2c->dfl -= ret;
1875 ret = h2c->dfl == 0;
1876 }
1877
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001878 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001879 /* RST are sent similarly to frame acks */
1880 if (h2s->st == H2_SS_ERROR) {
1881 if (h2c->st0 == H2_CS_FRAME_P)
1882 h2c->st0 = H2_CS_FRAME_A;
1883
1884 if (h2c->st0 == H2_CS_FRAME_A)
1885 ret = h2c_send_rst_stream(h2c, h2s);
1886 }
1887
Willy Tarreau7e98c052017-10-10 15:56:59 +02001888 /* error or missing data condition met above ? */
1889 if (ret <= 0)
1890 break;
1891
1892 if (h2c->st0 != H2_CS_FRAME_H) {
1893 bi_del(h2c->dbuf, h2c->dfl);
1894 h2c->st0 = H2_CS_FRAME_H;
1895 }
1896 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001897
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001898 if (h2c->rcvd_c > 0 &&
1899 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1900 h2c_send_conn_wu(h2c);
1901
Willy Tarreau52eed752017-09-22 15:05:09 +02001902 fail:
1903 /* we can go here on missing data, blocked response or error */
1904 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001905}
1906
1907/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1908 * the end.
1909 */
1910static int h2_process_mux(struct h2c *h2c)
1911{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001912 struct h2s *h2s, *h2s_back;
1913
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001914 /* start by sending possibly pending window updates */
1915 if (h2c->rcvd_c > 0 &&
1916 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1917 h2c_send_conn_wu(h2c) < 0)
1918 goto fail;
1919
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001920 /* First we always process the flow control list because the streams
1921 * waiting there were already elected for immediate emission but were
1922 * blocked just on this.
1923 */
1924
1925 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1926 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1927 h2c->st0 >= H2_CS_ERROR)
1928 break;
1929
1930 /* In theory it's possible that h2s->cs == NULL here :
1931 * - client sends crap that causes a parse error
1932 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1933 * - RST_STREAM cannot be emitted because mux is busy/full
1934 * - stream gets notified, detaches and quits
1935 * - mux buffer gets ready and wakes pending streams up
1936 * - bam!
1937 */
1938 h2s->flags &= ~H2_SF_BLK_ANY;
1939
1940 if (h2s->cs) {
1941 h2s->cs->data_cb->send(h2s->cs);
1942 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001943 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001944 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001945 }
1946
1947 /* depending on callee's blocking reasons, we may queue in send
1948 * list or completely dequeue.
1949 */
1950 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1951 if (h2s->flags & H2_SF_BLK_ANY) {
1952 LIST_DEL(&h2s->list);
1953 LIST_ADDQ(&h2c->send_list, &h2s->list);
1954 }
1955 else {
1956 LIST_DEL(&h2s->list);
1957 LIST_INIT(&h2s->list);
1958 if (h2s->cs)
1959 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001960 else {
1961 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01001962 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001963 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001964 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001965 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001966 }
1967 }
1968 }
1969
1970 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1971 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1972 break;
1973
1974 /* In theory it's possible that h2s->cs == NULL here :
1975 * - client sends crap that causes a parse error
1976 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1977 * - RST_STREAM cannot be emitted because mux is busy/full
1978 * - stream gets notified, detaches and quits
1979 * - mux buffer gets ready and wakes pending streams up
1980 * - bam!
1981 */
1982 h2s->flags &= ~H2_SF_BLK_ANY;
1983
1984 if (h2s->cs) {
1985 h2s->cs->data_cb->send(h2s->cs);
1986 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001987 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001988 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001989 }
1990 /* depending on callee's blocking reasons, we may queue in fctl
1991 * list or completely dequeue.
1992 */
1993 if (h2s->flags & H2_SF_BLK_MFCTL) {
1994 /* stream hit the connection's flow control */
1995 LIST_DEL(&h2s->list);
1996 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1997 }
1998 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1999 LIST_DEL(&h2s->list);
2000 LIST_INIT(&h2s->list);
2001 if (h2s->cs)
2002 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002003 else {
2004 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01002005 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002006 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002007 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002008 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002009 }
2010 }
2011
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002012 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002013 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002014 if (h2c->st0 == H2_CS_ERROR) {
2015 if (h2c->max_id >= 0) {
2016 h2c_send_goaway_error(h2c, NULL);
2017 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2018 return 0;
2019 }
2020
2021 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2022 }
2023 return 1;
2024 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002025 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002026}
2027
Willy Tarreau71681172017-10-23 14:39:06 +02002028
Willy Tarreau62f52692017-10-08 23:01:42 +02002029/*********************************************************/
2030/* functions below are I/O callbacks from the connection */
2031/*********************************************************/
2032
2033/* callback called on recv event by the connection handler */
2034static void h2_recv(struct connection *conn)
2035{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002036 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002037 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002038 int max;
2039
2040 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002041 return;
2042
2043 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
2044 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002045
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002046 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002047 if (!buf) {
2048 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002049 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002050 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002051
Willy Tarreaua2af5122017-10-09 11:56:46 +02002052 /* note: buf->o == 0 */
2053 max = buf->size - buf->i;
2054 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002055 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002056 return;
2057 }
2058
2059 conn->xprt->rcv_buf(conn, buf, max);
2060 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002061 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002062
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002063 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002064 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002065 return;
2066 }
2067
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002068 if (buf->i == buf->size)
2069 h2c->flags |= H2_CF_DEM_DFULL;
2070
Willy Tarreaubc933932017-10-09 16:21:43 +02002071 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002072
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002073 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002074 if (h2c->st0 >= H2_CS_ERROR)
2075 buf->i = 0;
2076
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002077 if (buf->i != buf->size)
2078 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002079 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002080}
2081
2082/* callback called on send event by the connection handler */
2083static void h2_send(struct connection *conn)
2084{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002085 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002086 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002087
2088 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002089 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002090
2091 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2092 /* a handshake was requested */
2093 return;
2094 }
2095
Willy Tarreaubc933932017-10-09 16:21:43 +02002096 /* This loop is quite simple : it tries to fill as much as it can from
2097 * pending streams into the existing buffer until it's reportedly full
2098 * or the end of send requests is reached. Then it tries to send this
2099 * buffer's contents out, marks it not full if at least one byte could
2100 * be sent, and tries again.
2101 *
2102 * The snd_buf() function normally takes a "flags" argument which may
2103 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2104 * data immediately comes and CO_SFL_STREAMER to indicate that the
2105 * connection is streaming lots of data (used to increase TLS record
2106 * size at the expense of latency). The former can be sent any time
2107 * there's a buffer full flag, as it indicates at least one stream
2108 * attempted to send and failed so there are pending data. An
2109 * alternative would be to set it as long as there's an active stream
2110 * but that would be problematic for ACKs until we have an absolute
2111 * guarantee that all waiters have at least one byte to send. The
2112 * latter should possibly not be set for now.
2113 */
2114
2115 done = 0;
2116 while (!done) {
2117 unsigned int flags = 0;
2118
2119 /* fill as much as we can into the current buffer */
2120 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2121 done = h2_process_mux(h2c);
2122
2123 if (conn->flags & CO_FL_ERROR)
2124 break;
2125
2126 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2127 flags |= CO_SFL_MSG_MORE;
2128
Willy Tarreau319994a2017-11-07 11:03:56 +01002129 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002130 break;
2131
2132 /* wrote at least one byte, the buffer is not full anymore */
2133 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2134 }
2135
Willy Tarreaua2af5122017-10-09 11:56:46 +02002136 if (conn->flags & CO_FL_SOCK_WR_SH) {
2137 /* output closed, nothing to send, clear the buffer to release it */
2138 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002139 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002140}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002141
Willy Tarreau62f52692017-10-08 23:01:42 +02002142/* callback called on any event by the connection handler.
2143 * It applies changes and returns zero, or < 0 if it wants immediate
2144 * destruction of the connection (which normally doesn not happen in h2).
2145 */
2146static int h2_wake(struct connection *conn)
2147{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002148 struct h2c *h2c = conn->mux_ctx;
2149
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002150 /*
2151 * If we received early data, try to wake any stream, just in case
2152 * at least one of them was waiting for the handshake
2153 */
2154 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2155 CO_FL_EARLY_DATA) {
2156 h2_wake_some_streams(h2c, 0, 0);
2157 conn->flags &= ~CO_FL_EARLY_DATA;
2158 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002159 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002160 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2161 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2162 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002163 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002164
2165 if (eb_is_empty(&h2c->streams_by_id)) {
2166 /* no more stream, kill the connection now */
2167 h2_release(conn);
2168 return -1;
2169 }
2170 else {
2171 /* some streams still there, we need to signal them all and
2172 * wait for their departure.
2173 */
2174 __conn_xprt_stop_recv(conn);
2175 __conn_xprt_stop_send(conn);
2176 return 0;
2177 }
2178 }
2179
2180 if (!h2c->dbuf->i)
2181 h2_release_dbuf(h2c);
2182
2183 /* stop being notified of incoming data if we can't process them */
2184 if (h2c->st0 >= H2_CS_ERROR ||
2185 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002186 __conn_xprt_stop_recv(conn);
2187 }
2188 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002189 __conn_xprt_want_recv(conn);
2190 }
2191
2192 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002193 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2194 (h2c->st0 == H2_CS_ERROR ||
2195 h2c->mbuf->o ||
2196 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2197 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002198 __conn_xprt_want_send(conn);
2199 }
2200 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002201 h2_release_mbuf(h2c);
2202 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002203 }
2204
Willy Tarreau3f133572017-10-31 19:21:06 +01002205 if (h2c->task) {
2206 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002207 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002208 task_queue(h2c->task);
2209 }
2210 else
2211 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002212 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002213 return 0;
2214}
2215
Willy Tarreauea392822017-10-31 10:02:25 +01002216/* Connection timeout management. The principle is that if there's no receipt
2217 * nor sending for a certain amount of time, the connection is closed. If the
2218 * MUX buffer still has lying data or is not allocatable, the connection is
2219 * immediately killed. If it's allocatable and empty, we attempt to send a
2220 * GOAWAY frame.
2221 */
2222static struct task *h2_timeout_task(struct task *t)
2223{
2224 struct h2c *h2c = t->context;
2225 int expired = tick_is_expired(t->expire, now_ms);
2226
2227 if (!expired)
2228 return t;
2229
2230 h2c_error(h2c, H2_ERR_NO_ERROR);
2231 h2_wake_some_streams(h2c, 0, 0);
2232
2233 if (h2c->mbuf->o) {
2234 /* don't even try to send a GOAWAY, the buffer is stuck */
2235 h2c->flags |= H2_CF_GOAWAY_FAILED;
2236 }
2237
2238 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002239 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002240 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2241 h2c->flags |= H2_CF_GOAWAY_FAILED;
2242
2243 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2244 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2245
2246 if (!eb_is_empty(&h2c->streams_by_id))
2247 goto wait;
2248
2249 h2_release(h2c->conn);
2250 return NULL;
2251
2252 wait:
2253 /* the streams have been notified, we must let them finish and close */
2254 h2c->task = NULL;
2255 task_delete(t);
2256 task_free(t);
2257 return NULL;
2258}
2259
2260
Willy Tarreau62f52692017-10-08 23:01:42 +02002261/*******************************************/
2262/* functions below are used by the streams */
2263/*******************************************/
2264
2265/*
2266 * Attach a new stream to a connection
2267 * (Used for outgoing connections)
2268 */
2269static struct conn_stream *h2_attach(struct connection *conn)
2270{
2271 return NULL;
2272}
2273
2274/* callback used to update the mux's polling flags after changing a cs' status.
2275 * The caller (cs_update_mux_polling) will take care of propagating any changes
2276 * to the transport layer.
2277 */
2278static void h2_update_poll(struct conn_stream *cs)
2279{
Willy Tarreau1d393222017-10-17 10:26:19 +02002280 struct h2s *h2s = cs->ctx;
2281
2282 if (!h2s)
2283 return;
2284
Willy Tarreaud7739c82017-10-30 15:38:23 +01002285 /* we may unblock a blocked read */
2286
2287 if (cs->flags & CS_FL_DATA_RD_ENA &&
2288 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2289 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2290 conn_xprt_want_recv(cs->conn);
2291 }
2292
Willy Tarreau1d393222017-10-17 10:26:19 +02002293 /* Note: the stream and stream-int code doesn't allow us to perform a
2294 * synchronous send() here unfortunately, because this code is called
2295 * as si_update() from the process_stream() context. This means that
2296 * we have to queue the current cs and defer its processing after the
2297 * connection's cs list is processed anyway.
2298 */
2299
2300 if (cs->flags & CS_FL_DATA_WR_ENA) {
2301 if (LIST_ISEMPTY(&h2s->list)) {
2302 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2303 !h2s->h2c->mbuf->o && // not yet subscribed
2304 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2305 conn_xprt_want_send(cs->conn);
2306 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2307 }
2308 }
2309 else if (!LIST_ISEMPTY(&h2s->list)) {
2310 LIST_DEL(&h2s->list);
2311 LIST_INIT(&h2s->list);
2312 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2313 }
2314
2315 /* this can happen from within si_chk_snd() */
2316 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2317 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002318}
2319
2320/*
2321 * Detach the stream from the connection and possibly release the connection.
2322 */
2323static void h2_detach(struct conn_stream *cs)
2324{
Willy Tarreau60935142017-10-16 18:11:19 +02002325 struct h2s *h2s = cs->ctx;
2326 struct h2c *h2c;
2327
2328 cs->ctx = NULL;
2329 if (!h2s)
2330 return;
2331
2332 h2c = h2s->h2c;
2333 h2s->cs = NULL;
2334
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002335 /* this stream may be blocked waiting for some data to leave (possibly
2336 * an ES or RST frame), so orphan it in this case.
2337 */
2338 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2339 return;
2340
Willy Tarreau541dd822017-11-23 18:12:50 +01002341 /* the stream could be in the send list */
2342 LIST_DEL(&h2s->list);
2343
Willy Tarreau45f752e2017-10-30 15:44:59 +01002344 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2345 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2346 /* unblock the connection if it was blocked on this
2347 * stream.
2348 */
2349 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2350 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2351 conn_xprt_want_recv(cs->conn);
2352 conn_xprt_want_send(cs->conn);
2353 }
2354
Willy Tarreau60935142017-10-16 18:11:19 +02002355 if (h2s->by_id.node.leaf_p) {
2356 /* h2s still attached to the h2c */
Willy Tarreau49745612017-12-03 18:56:02 +01002357 h2c->nb_streams--;
Willy Tarreau60935142017-10-16 18:11:19 +02002358 eb32_delete(&h2s->by_id);
2359
2360 /* We don't want to close right now unless we're removing the
2361 * last stream, and either the connection is in error, or it
2362 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002363 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002364 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002365 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2366 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002367 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002368 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2369 (conn_xprt_read0_pending(h2c->conn) ||
2370 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002371 /* no more stream will come, kill it now */
2372 h2_release(h2c->conn);
2373 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002374 else if (h2c->task) {
2375 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002376 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002377 task_queue(h2c->task);
2378 }
2379 else
2380 h2c->task->expire = TICK_ETERNITY;
2381 }
Willy Tarreau60935142017-10-16 18:11:19 +02002382 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002383 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002384}
2385
2386static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2387{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002388 struct h2s *h2s = cs->ctx;
2389
2390 if (!mode)
2391 return;
2392
Willy Tarreau721c9742017-11-07 11:05:42 +01002393 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002394 return;
2395
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002396 /* if no outgoing data was seen on this stream, it means it was
2397 * closed with a "tcp-request content" rule that is normally
2398 * used to kill the connection ASAP (eg: limit abuse). In this
2399 * case we send a goaway to close the connection.
2400 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002401 if (!(h2s->flags & H2_SF_RST_SENT) &&
2402 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2403 return;
2404
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002405 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2406 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2407 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2408 return;
2409
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002410 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2411 conn_xprt_want_send(cs->conn);
2412
2413 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002414}
2415
2416static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2417{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002418 struct h2s *h2s = cs->ctx;
2419
Willy Tarreau721c9742017-11-07 11:05:42 +01002420 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002421 return;
2422
Willy Tarreau67434202017-11-06 20:20:51 +01002423 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002424 /* we can cleanly close using an empty data frame only after headers */
2425
2426 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2427 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002428 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002429
2430 if (h2s->st == H2_SS_HREM)
2431 h2s->st = H2_SS_CLOSED;
2432 else
2433 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002434 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002435 /* if no outgoing data was seen on this stream, it means it was
2436 * closed with a "tcp-request content" rule that is normally
2437 * used to kill the connection ASAP (eg: limit abuse). In this
2438 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002439 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002440 if (!(h2s->flags & H2_SF_RST_SENT) &&
2441 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2442 return;
2443
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002444 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2445 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002446 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2447 return;
2448
Willy Tarreau58e32082017-11-07 14:41:09 +01002449 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002450 }
2451
2452 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2453 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002454}
2455
Willy Tarreau13278b42017-10-13 19:23:14 +02002456/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2457 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2458 * proceed. Stream errors are reported in h2s->errcode and connection errors
2459 * in h2c->errcode. The caller must already have checked the frame header and
2460 * ensured that the frame was complete or the buffer full.
2461 */
2462static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2463{
2464 struct h2c *h2c = h2s->h2c;
2465 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002466 struct chunk *tmp = get_trash_chunk();
2467 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002468 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002469 int flen = h2c->dfl;
2470 int outlen = 0;
2471 int wrap;
2472 int try;
2473
2474 if (!h2c->dfl) {
2475 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2476 return 0;
2477 }
2478
2479 /* if the input buffer wraps, take a temporary copy of it (rare) */
2480 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2481 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002482 copy = alloc_trash_chunk();
2483 if (!copy) {
2484 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2485 goto fail;
2486 }
2487 memcpy(copy->str, h2c->dbuf->p, wrap);
2488 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2489 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002490 }
2491
2492 /* The padlen is the first byte before data, and the padding appears
2493 * after data. padlen+data+padding are included in flen.
2494 */
2495 if (h2c->dff & H2_F_HEADERS_PADDED) {
2496 if (*hdrs >= flen) {
2497 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2498 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002499 return 0;
2500 }
2501 flen -= *hdrs + 1;
2502 hdrs += 1; // skip Pad Length
2503 }
2504
2505 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2506 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002507 if (read_n32(hdrs) == h2s->id) {
2508 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2509 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2510 return 0;//goto fail_stream;
2511 }
2512
Willy Tarreau13278b42017-10-13 19:23:14 +02002513 hdrs += 5; // stream dep = 4, weight = 1
2514 flen -= 5;
2515 }
2516
2517 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2518 * don't support this for now and can't even decompress so we have to
2519 * break the connection.
2520 */
2521 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2522 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002523 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002524 }
2525
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002526 /* we can't retry a failed decompression operation so we must be very
2527 * careful not to take any risks. In practice the output buffer is
2528 * always empty except maybe for trailers, so these operations almost
2529 * never happen.
2530 */
2531 if (unlikely(buf->o)) {
2532 /* need to let the output buffer flush and
2533 * mark the buffer for later wake up.
2534 */
2535 goto fail;
2536 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002537
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002538 if (unlikely(buffer_space_wraps(buf))) {
2539 /* it doesn't fit and the buffer is fragmented,
2540 * so let's defragment it and try again.
2541 */
2542 buffer_slow_realign(buf);
2543 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002544
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002545 /* first check if we have some room after p+i */
2546 try = buf->data + buf->size - (buf->p + buf->i);
2547
2548 /* otherwise continue between data and p-o */
2549 if (try <= 0) {
2550 try = buf->p - (buf->data + buf->o);
2551 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002552 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002553 }
2554 if (try > count)
2555 try = count;
2556
2557 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2558 sizeof(list)/sizeof(list[0]), tmp);
2559 if (outlen < 0) {
2560 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2561 goto fail;
2562 }
2563
2564 /* OK now we have our header list in <list> */
2565 outlen = h2_make_h1_request(list, bi_end(buf), try);
2566
2567 if (outlen < 0) {
2568 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2569 goto fail;
2570 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002571
2572 /* now consume the input data */
2573 bi_del(h2c->dbuf, h2c->dfl);
2574 h2c->st0 = H2_CS_FRAME_H;
2575 buf->i += outlen;
2576
2577 /* don't send it before returning data!
2578 * FIXME: should we instead try to send it much later, after the
2579 * response ? This would require that we keep a copy of it in h2s.
2580 */
2581 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2582 h2s->cs->flags |= CS_FL_EOS;
2583 h2s->flags |= H2_SF_ES_RCVD;
2584 }
2585
Willy Tarreau68dd9852017-07-03 14:44:26 +02002586 leave:
2587 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002588 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002589 fail:
2590 outlen = 0;
2591 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002592}
2593
Willy Tarreau454f9052017-10-26 19:40:35 +02002594/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2595 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2596 * in use, a new chunk is emitted for each frame. This is supposed to fit
2597 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2598 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2599 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2600 * parser state is automatically updated. Returns the number of bytes emitted
2601 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2602 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2603 * have checked the frame header and ensured that the frame was complete or the
2604 * buffer full. It changes the frame state to FRAME_A once done.
2605 */
2606static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2607{
2608 struct h2c *h2c = h2s->h2c;
2609 int block1, block2;
2610 unsigned int flen = h2c->dfl;
2611 unsigned int padlen = 0;
2612 int offset = 0;
2613
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002614 h2s->cs->flags &= ~CS_FL_RCV_MORE;
2615
Willy Tarreau454f9052017-10-26 19:40:35 +02002616 if (h2c->dbuf->i < flen)
2617 return 0;
2618
2619 /* The padlen is the first byte before data, and the padding appears
2620 * after data. padlen+data+padding are included in flen.
2621 */
Willy Tarreau79127812017-12-03 21:06:59 +01002622 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002623 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2624 if (padlen >= flen) {
2625 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2626 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002627 return 0;
2628 }
2629 flen -= padlen + 1;
2630 offset = 1; // skip Pad Length
2631 }
2632
2633 /* does it fit in output buffer or should we wait ? */
2634 if (buf->i + buf->o + flen > buf->size) {
2635 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002636 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau454f9052017-10-26 19:40:35 +02002637 return 0;
2638 }
2639
2640 /* Block1 is the length of the first block before the buffer wraps,
2641 * block2 is the optional second block to reach the end of the frame.
2642 */
2643 block1 = bi_contig_data(h2c->dbuf);
2644 if (block1 > offset + flen)
2645 block1 = offset + flen;
2646 block1 -= offset; // skip Pad Length
2647 block2 = flen - block1;
2648
2649 if (block1)
2650 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2651
2652 if (block2)
2653 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2654
2655 /* now mark the input data as consumed (will be deleted from the buffer
2656 * by the caller when seeing FRAME_A after sending the window update).
2657 */
2658 h2c->rcvd_c += h2c->dfl;
2659 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2660 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2661
2662 /* don't send it before returning data!
2663 * FIXME: should we instead try to send it much later, after the
2664 * response ? This would require that we keep a copy of it in h2s.
2665 */
Willy Tarreau79127812017-12-03 21:06:59 +01002666 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002667 h2s->cs->flags |= CS_FL_EOS;
2668 h2s->flags |= H2_SF_ES_RCVD;
2669 }
2670
2671 return flen;
2672}
2673
Willy Tarreau62f52692017-10-08 23:01:42 +02002674/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002675 * Called from the upper layer to get more data, up to <count> bytes. The
2676 * caller is responsible for never asking for more data than what is available
2677 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002678 */
2679static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2680{
Willy Tarreau13278b42017-10-13 19:23:14 +02002681 struct h2s *h2s = cs->ctx;
2682 struct h2c *h2c = h2s->h2c;
2683 int ret = 0;
2684
2685 if (h2c->st0 != H2_CS_FRAME_P)
2686 return 0; // no pre-parsed frame yet
2687
2688 if (h2c->dsi != h2s->id)
2689 return 0; // not for us
2690
2691 if (!h2c->dbuf->size)
2692 return 0; // empty buffer
2693
2694 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2695 return 0; // incomplete input frame
2696
2697 switch (h2c->dft) {
2698 case H2_FT_HEADERS:
2699 ret = h2_frt_decode_headers(h2s, buf, count);
2700 break;
2701
Willy Tarreau454f9052017-10-26 19:40:35 +02002702 case H2_FT_DATA:
2703 ret = h2_frt_transfer_data(h2s, buf, count);
2704 break;
2705
Willy Tarreau13278b42017-10-13 19:23:14 +02002706 default:
2707 ret = 0;
2708 }
2709 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002710}
2711
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002712/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2713 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2714 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2715 * to the number of buffer bytes consumed.
2716 */
2717static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2718{
2719 struct http_hdr list[MAX_HTTP_HDR];
2720 struct h2c *h2c = h2s->h2c;
2721 struct h1m *h1m = &h2s->res;
2722 struct chunk outbuf;
2723 int es_now = 0;
2724 int ret = 0;
2725 int hdr;
2726
2727 if (h2c_mux_busy(h2c, h2s)) {
2728 h2s->flags |= H2_SF_BLK_MBUSY;
2729 return 0;
2730 }
2731
2732 if (!h2_get_mbuf(h2c)) {
2733 h2c->flags |= H2_CF_MUX_MALLOC;
2734 h2s->flags |= H2_SF_BLK_MROOM;
2735 return 0;
2736 }
2737
2738 /* First, try to parse the H1 response and index it into <list>.
2739 * NOTE! Since it comes from haproxy, we *know* that a response header
2740 * block does not wrap and we can safely read it this way without
2741 * having to realign the buffer.
2742 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002743 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002744 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2745 list, sizeof(list)/sizeof(list[0]), h1m);
2746 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002747 /* incomplete or invalid response, this is abnormal coming from
2748 * haproxy and may only result in a bad errorfile or bad Lua code
2749 * so that won't be fixed, raise an error now.
2750 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002751 * FIXME: we should instead add the ability to only return a
2752 * 502 bad gateway. But in theory this is not supposed to
2753 * happen.
2754 */
2755 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2756 ret = 0;
2757 goto end;
2758 }
2759
2760 chunk_reset(&outbuf);
2761
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002762 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002763 while (1) {
2764 outbuf.str = bo_end(h2c->mbuf);
2765 outbuf.size = bo_contig_space(h2c->mbuf);
2766 outbuf.len = 0;
2767
2768 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2769 break;
2770 realign_again:
2771 buffer_slow_realign(h2c->mbuf);
2772 }
2773
2774 if (outbuf.size < 9) {
2775 h2c->flags |= H2_CF_MUX_MFULL;
2776 h2s->flags |= H2_SF_BLK_MROOM;
2777 ret = 0;
2778 goto end;
2779 }
2780
2781 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2782 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2783 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2784 outbuf.len = 9;
2785
2786 /* encode status, which necessarily is the first one */
2787 if (outbuf.len < outbuf.size && h1m->status == 200)
2788 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2789 else if (outbuf.len < outbuf.size && h1m->status == 304)
2790 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002791 else if (unlikely(list[0].v.len != 3)) {
2792 /* this is an unparsable response */
2793 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2794 ret = 0;
2795 goto end;
2796 }
2797 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002798 /* basic encoding of the status code */
2799 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2800 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2801 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2802 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2803 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2804 }
2805 else {
2806 if (buffer_space_wraps(h2c->mbuf))
2807 goto realign_again;
2808
2809 h2c->flags |= H2_CF_MUX_MFULL;
2810 h2s->flags |= H2_SF_BLK_MROOM;
2811 ret = 0;
2812 goto end;
2813 }
2814
2815 /* encode all headers, stop at empty name */
2816 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002817 /* these ones do not exist in H2 and must be dropped. */
2818 if (isteq(list[hdr].n, ist("connection")) ||
2819 isteq(list[hdr].n, ist("proxy-connection")) ||
2820 isteq(list[hdr].n, ist("keep-alive")) ||
2821 isteq(list[hdr].n, ist("upgrade")) ||
2822 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002823 continue;
2824
2825 if (isteq(list[hdr].n, ist("")))
2826 break; // end
2827
2828 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2829 /* output full */
2830 if (buffer_space_wraps(h2c->mbuf))
2831 goto realign_again;
2832
2833 h2c->flags |= H2_CF_MUX_MFULL;
2834 h2s->flags |= H2_SF_BLK_MROOM;
2835 ret = 0;
2836 goto end;
2837 }
2838 }
2839
2840 /* we may need to add END_STREAM */
2841 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2842 es_now = 1;
2843
2844 /* update the frame's size */
2845 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2846
2847 if (es_now)
2848 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2849
2850 /* consume incoming H1 response */
2851 bo_del(buf, ret);
2852
2853 /* commit the H2 response */
2854 h2c->mbuf->o += outbuf.len;
2855 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002856 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002857
2858 /* for now we don't implemented CONTINUATION, so we wait for a
2859 * body or directly end in TRL2.
2860 */
2861 if (es_now) {
2862 h1m->state = HTTP_MSG_DONE;
2863 h2s->flags |= H2_SF_ES_SENT;
2864 if (h2s->st == H2_SS_OPEN)
2865 h2s->st = H2_SS_HLOC;
2866 else
2867 h2s->st = H2_SS_CLOSED;
2868 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002869 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002870 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002871 h1m->state = HTTP_MSG_RPBEFORE;
2872 h1m->status = 0;
2873 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01002874 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01002875 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002876 else
2877 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2878
2879 end:
2880 //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));
2881 return ret;
2882}
2883
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002884/* Try to send a DATA frame matching HTTP/1 response present in the response
2885 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2886 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2887 * corresponds to the number of buffer bytes consumed.
2888 */
2889static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2890{
2891 struct h2c *h2c = h2s->h2c;
2892 struct h1m *h1m = &h2s->res;
2893 struct chunk outbuf;
2894 int ret = 0;
2895 int total = 0;
2896 int es_now = 0;
2897 int size = 0;
2898 char *blk1, *blk2;
2899 int len1, len2;
2900
2901 if (h2c_mux_busy(h2c, h2s)) {
2902 h2s->flags |= H2_SF_BLK_MBUSY;
2903 goto end;
2904 }
2905
2906 if (!h2_get_mbuf(h2c)) {
2907 h2c->flags |= H2_CF_MUX_MALLOC;
2908 h2s->flags |= H2_SF_BLK_MROOM;
2909 goto end;
2910 }
2911
2912 new_frame:
2913 if (!buf->o)
2914 goto end;
2915
2916 chunk_reset(&outbuf);
2917
2918 while (1) {
2919 outbuf.str = bo_end(h2c->mbuf);
2920 outbuf.size = bo_contig_space(h2c->mbuf);
2921 outbuf.len = 0;
2922
2923 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2924 break;
2925 realign_again:
2926 buffer_slow_realign(h2c->mbuf);
2927 }
2928
2929 if (outbuf.size < 9) {
2930 h2c->flags |= H2_CF_MUX_MFULL;
2931 h2s->flags |= H2_SF_BLK_MROOM;
2932 goto end;
2933 }
2934
2935 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2936 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2937 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2938 outbuf.len = 9;
2939
2940 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2941 case 0: /* no content length, read till SHUTW */
2942 size = buf->o;
2943 break;
2944 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2945 size = buf->o;
2946 if ((long long)size > h1m->curr_len)
2947 size = h1m->curr_len;
2948 break;
2949 default: /* te:chunked : parse chunks */
2950 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2951 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2952 if (!ret)
2953 goto end;
2954
2955 if (ret < 0) {
2956 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2957 h1m->err_pos = ret;
2958 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2959 goto end;
2960 }
2961 bo_del(buf, ret);
2962 total += ret;
2963 h1m->state = HTTP_MSG_CHUNK_SIZE;
2964 }
2965
2966 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2967 unsigned int chunk;
2968
2969 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2970 if (!ret)
2971 goto end;
2972
2973 if (ret < 0) {
2974 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2975 h1m->err_pos = ret;
2976 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2977 goto end;
2978 }
2979
2980 size = chunk;
2981 h1m->curr_len = chunk;
2982 h1m->body_len += chunk;
2983 bo_del(buf, ret);
2984 total += ret;
2985 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2986 if (!size)
2987 goto send_empty;
2988 }
2989
2990 /* in MSG_DATA state, continue below */
2991 size = h1m->curr_len;
2992 break;
2993 }
2994
2995 /* we have in <size> the exact number of bytes we need to copy from
2996 * the H1 buffer. We need to check this against the connection's and
2997 * the stream's send windows, and to ensure that this fits in the max
2998 * frame size and in the buffer's available space minus 9 bytes (for
2999 * the frame header). The connection's flow control is applied last so
3000 * that we can use a separate list of streams which are immediately
3001 * unblocked on window opening. Note: we don't implement padding.
3002 */
3003
3004 if (size > buf->o)
3005 size = buf->o;
3006
3007 if (size > h2s->mws)
3008 size = h2s->mws;
3009
3010 if (size <= 0) {
3011 h2s->flags |= H2_SF_BLK_SFCTL;
3012 goto end;
3013 }
3014
3015 if (h2c->mfs && size > h2c->mfs)
3016 size = h2c->mfs;
3017
3018 if (size + 9 > outbuf.size) {
3019 /* we have an opportunity for enlarging the too small
3020 * available space, let's try.
3021 */
3022 if (buffer_space_wraps(h2c->mbuf))
3023 goto realign_again;
3024 size = outbuf.size - 9;
3025 }
3026
3027 if (size <= 0) {
3028 h2c->flags |= H2_CF_MUX_MFULL;
3029 h2s->flags |= H2_SF_BLK_MROOM;
3030 goto end;
3031 }
3032
3033 if (size > h2c->mws)
3034 size = h2c->mws;
3035
3036 if (size <= 0) {
3037 h2s->flags |= H2_SF_BLK_MFCTL;
3038 goto end;
3039 }
3040
3041 /* copy whatever we can */
3042 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3043 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3044 if (ret == 1)
3045 len2 = 0;
3046
3047 if (!ret || len1 + len2 < size) {
3048 /* FIXME: must normally never happen */
3049 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3050 goto end;
3051 }
3052
3053 /* limit len1/len2 to size */
3054 if (len1 + len2 > size) {
3055 int sub = len1 + len2 - size;
3056
3057 if (len2 > sub)
3058 len2 -= sub;
3059 else {
3060 sub -= len2;
3061 len2 = 0;
3062 len1 -= sub;
3063 }
3064 }
3065
3066 /* now let's copy this this into the output buffer */
3067 memcpy(outbuf.str + 9, blk1, len1);
3068 if (len2)
3069 memcpy(outbuf.str + 9 + len1, blk2, len2);
3070
3071 send_empty:
3072 /* we may need to add END_STREAM */
3073 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3074 * could rely on the MSG_MORE flag as a hint for this ?
3075 */
3076 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3077 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3078 es_now = 1;
3079
3080 /* update the frame's size */
3081 h2_set_frame_size(outbuf.str, size);
3082
3083 if (es_now)
3084 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3085
3086 /* commit the H2 response */
3087 h2c->mbuf->o += size + 9;
3088 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3089
3090 /* consume incoming H1 response */
3091 if (size > 0) {
3092 bo_del(buf, size);
3093 total += size;
3094 h1m->curr_len -= size;
3095 h2s->mws -= size;
3096 h2c->mws -= size;
3097
3098 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3099 h1m->state = HTTP_MSG_CHUNK_CRLF;
3100 goto new_frame;
3101 }
3102 }
3103
3104 if (es_now) {
3105 if (h2s->st == H2_SS_OPEN)
3106 h2s->st = H2_SS_HLOC;
3107 else
3108 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003109
3110 if (!(h1m->flags & H1_MF_CHNK))
3111 h1m->state = HTTP_MSG_DONE;
3112
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003113 h2s->flags |= H2_SF_ES_SENT;
3114 }
3115
3116 end:
3117 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);
3118 return total;
3119}
3120
Willy Tarreau62f52692017-10-08 23:01:42 +02003121/* Called from the upper layer, to send data */
3122static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3123{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003124 struct h2s *h2s = cs->ctx;
3125 int total = 0;
3126
Willy Tarreauc4312d32017-11-07 12:01:53 +01003127 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3128 h2s->flags |= H2_SF_OUTGOING_DATA;
3129
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3131 if (h2s->res.state < HTTP_MSG_BODY) {
3132 total += h2s_frt_make_resp_headers(h2s, buf);
3133
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003134 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003135 break;
3136
3137 if (h2s->flags & H2_SF_BLK_ANY)
3138 break;
3139 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003140 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3141 total += h2s_frt_make_resp_data(h2s, buf);
3142
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003143 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003144 break;
3145
3146 if (h2s->flags & H2_SF_BLK_ANY)
3147 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003148 }
3149 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3150 /* consume the trailers if any (we don't forward them for now) */
3151 int count = h1_measure_trailers(buf);
3152
3153 if (unlikely(count <= 0)) {
3154 if (count < 0)
3155 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3156 break;
3157 }
3158 total += count;
3159 bo_del(buf, count);
3160 h2s->res.state = HTTP_MSG_DONE;
3161 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003162 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003163 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003164 cs->flags |= CS_FL_ERROR;
3165 break;
3166 }
3167 }
3168
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003169 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003170 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003171 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003172 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003173 h2s->st = H2_SS_CLOSED;
3174 }
3175
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003176 if (h2s->flags & H2_SF_BLK_SFCTL) {
3177 /* stream flow control, quit the list */
3178 LIST_DEL(&h2s->list);
3179 LIST_INIT(&h2s->list);
3180 }
3181
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003182 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003183}
3184
3185
3186/*******************************************************/
3187/* functions below are dedicated to the config parsers */
3188/*******************************************************/
3189
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003190/* config parser for global "tune.h2.header-table-size" */
3191static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3192 struct proxy *defpx, const char *file, int line,
3193 char **err)
3194{
3195 if (too_many_args(1, args, err, NULL))
3196 return -1;
3197
3198 h2_settings_header_table_size = atoi(args[1]);
3199 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3200 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3201 return -1;
3202 }
3203 return 0;
3204}
Willy Tarreau62f52692017-10-08 23:01:42 +02003205
Willy Tarreaue6baec02017-07-27 11:45:11 +02003206/* config parser for global "tune.h2.initial-window-size" */
3207static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3208 struct proxy *defpx, const char *file, int line,
3209 char **err)
3210{
3211 if (too_many_args(1, args, err, NULL))
3212 return -1;
3213
3214 h2_settings_initial_window_size = atoi(args[1]);
3215 if (h2_settings_initial_window_size < 0) {
3216 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3217 return -1;
3218 }
3219 return 0;
3220}
3221
Willy Tarreau5242ef82017-07-27 11:47:28 +02003222/* config parser for global "tune.h2.max-concurrent-streams" */
3223static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3224 struct proxy *defpx, const char *file, int line,
3225 char **err)
3226{
3227 if (too_many_args(1, args, err, NULL))
3228 return -1;
3229
3230 h2_settings_max_concurrent_streams = atoi(args[1]);
3231 if (h2_settings_max_concurrent_streams < 0) {
3232 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3233 return -1;
3234 }
3235 return 0;
3236}
3237
Willy Tarreau62f52692017-10-08 23:01:42 +02003238
3239/****************************************/
3240/* MUX initialization and instanciation */
3241/***************************************/
3242
3243/* The mux operations */
3244const struct mux_ops h2_ops = {
3245 .init = h2_init,
3246 .recv = h2_recv,
3247 .send = h2_send,
3248 .wake = h2_wake,
3249 .update_poll = h2_update_poll,
3250 .rcv_buf = h2_rcv_buf,
3251 .snd_buf = h2_snd_buf,
3252 .attach = h2_attach,
3253 .detach = h2_detach,
3254 .shutr = h2_shutr,
3255 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003256 .name = "H2",
3257};
3258
3259/* ALPN selection : this mux registers ALPN tolen "h2" */
3260static struct alpn_mux_list alpn_mux_h2 =
3261 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3262
3263/* config keyword parsers */
3264static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003265 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003266 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003267 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003268 { 0, NULL, NULL }
3269}};
3270
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003271static void __h2_deinit(void)
3272{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003273 pool_destroy(pool_head_h2s);
3274 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003275}
3276
Willy Tarreau62f52692017-10-08 23:01:42 +02003277__attribute__((constructor))
3278static void __h2_init(void)
3279{
3280 alpn_register_mux(&alpn_mux_h2);
3281 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003282 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003283 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3284 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003285}