blob: fc655db95cc6bd540e881b169d9aef4801b22955 [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
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010057#define H2_CF_DEM_BLOCK_ANY 0x000000F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
60#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
62
63
Willy Tarreau5ab6b572017-09-22 08:05:00 +020064/* H2 connection state, in h2c->st0 */
65enum h2_cs {
66 H2_CS_PREFACE, // init done, waiting for connection preface
67 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
68 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
69 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
70 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
71 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
91 struct buffer *dbuf; /* demux buffer */
92
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
97 /* 16 bit hole here */
98 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
99
100 /* states for the mux direction */
101 struct buffer *mbuf; /* mux buffer */
102 int32_t msi; /* mux stream ID (<0 = idle) */
103 int32_t mfl; /* mux frame length (if dsi >= 0) */
104 int8_t mft; /* mux frame type (if dsi >= 0) */
105 int8_t mff; /* mux frame flags (if dsi >= 0) */
106 /* 16 bit hole here */
107 int32_t miw; /* mux initial window size for all new streams */
108 int32_t mws; /* mux window size. Can be negative. */
109 int32_t mfs; /* mux's max frame size */
110
Willy Tarreauea392822017-10-31 10:02:25 +0100111 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100112 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100113 unsigned int nb_streams; /* number of streams in the tree */
114 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100115 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200116 struct eb_root streams_by_id; /* all active streams by their ID */
117 struct list send_list; /* list of blocked streams requesting to send */
118 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200119 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200120 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121};
122
Willy Tarreau18312642017-10-11 07:57:07 +0200123/* H2 stream state, in h2s->st */
124enum h2_ss {
125 H2_SS_IDLE = 0, // idle
126 H2_SS_RLOC, // reserved(local)
127 H2_SS_RREM, // reserved(remote)
128 H2_SS_OPEN, // open
129 H2_SS_HREM, // half-closed(remote)
130 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200131 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200132 H2_SS_CLOSED, // closed
133 H2_SS_ENTRIES // must be last
134} __attribute__((packed));
135
136/* HTTP/2 stream flags (32 bit), in h2s->flags */
137#define H2_SF_NONE 0x00000000
138#define H2_SF_ES_RCVD 0x00000001
139#define H2_SF_ES_SENT 0x00000002
140
141#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
142#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
143
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200144/* stream flags indicating the reason the stream is blocked */
145#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
146#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
147#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
148#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
149#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
150
Willy Tarreau454f9052017-10-26 19:40:35 +0200151/* stream flags indicating how data is supposed to be sent */
152#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
153#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
154
155/* step we're currently in when sending chunks. This is needed because we may
156 * have to transfer chunks as large as a full buffer so there's no room left
157 * for size nor crlf around.
158 */
159#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
160#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
161#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
162
163#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
164
Willy Tarreau67434202017-11-06 20:20:51 +0100165#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100166#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100167
Willy Tarreau18312642017-10-11 07:57:07 +0200168/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
169 * it is being processed in the internal HTTP representation (H1 for now).
170 */
171struct h2s {
172 struct conn_stream *cs;
173 struct h2c *h2c;
174 struct h1m req, res; /* request and response parser state for H1 */
175 struct eb32_node by_id; /* place in h2c's streams_by_id */
176 struct list list; /* position in active/blocked lists if blocked>0 */
177 int32_t id; /* stream ID */
178 uint32_t flags; /* H2_SF_* */
179 int mws; /* mux window size for this stream */
180 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
181 enum h2_ss st;
182};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200183
Willy Tarreauc6405142017-09-21 20:23:50 +0200184/* descriptor for an h2 frame header */
185struct h2_fh {
186 uint32_t len; /* length, host order, 24 bits */
187 uint32_t sid; /* stream id, host order, 31 bits */
188 uint8_t ft; /* frame type */
189 uint8_t ff; /* frame flags */
190};
191
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200192/* a few settings from the global section */
193static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200194static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200195static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200196
Willy Tarreau2a856182017-05-16 15:20:39 +0200197/* a dmumy closed stream */
198static const struct h2s *h2_closed_stream = &(const struct h2s){
199 .cs = NULL,
200 .h2c = NULL,
201 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100202 .errcode = H2_ERR_STREAM_CLOSED,
203 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200204 .id = 0,
205};
206
207/* and a dummy idle stream for use with any unannounced stream */
208static const struct h2s *h2_idle_stream = &(const struct h2s){
209 .cs = NULL,
210 .h2c = NULL,
211 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100212 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200213 .id = 0,
214};
215
Willy Tarreauea392822017-10-31 10:02:25 +0100216static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200217
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200218/*****************************************************/
219/* functions below are for dynamic buffer management */
220/*****************************************************/
221
Willy Tarreau315d8072017-12-10 22:17:57 +0100222/* indicates whether or not the we may call the h2_recv() function to attempt
223 * to receive data into the buffer and/or demux pending data. The condition is
224 * a bit complex due to some API limits for now. The rules are the following :
225 * - if an error or a shutdown was detected on the connection and the buffer
226 * is empty, we must not attempt to receive
227 * - if the demux buf failed to be allocated, we must not try to receive and
228 * we know there is nothing pending
229 * - if the buffer is not full, we may attempt to receive
230 * - if no flag indicates a blocking condition, we may attempt to receive
231 * - otherwise must may not attempt
232 */
233static inline int h2_recv_allowed(const struct h2c *h2c)
234{
235 if (h2c->dbuf->i == 0 &&
236 (h2c->st0 >= H2_CS_ERROR ||
237 h2c->conn->flags & CO_FL_ERROR ||
238 conn_xprt_read0_pending(h2c->conn)))
239 return 0;
240
241 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
242 (!(h2c->flags & H2_CF_DEM_DFULL) ||
243 !(h2c->flags & H2_CF_DEM_BLOCK_ANY)))
244 return 1;
245
246 return 0;
247}
248
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200249/* re-enables receiving on mux <target> after a buffer was allocated. It returns
250 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
251 * if it's impossible to wake up and we prefer to be woken up later.
252 */
253static int h2_dbuf_available(void *target)
254{
255 struct h2c *h2c = target;
256
257 /* take the buffer now as we'll get scheduled waiting for ->wake() */
258 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200259 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100260 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200261 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200262 return 1;
263 }
264 return 0;
265}
266
267static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
268{
269 struct buffer *buf = NULL;
270
271 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
272 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
273 h2c->dbuf_wait.target = h2c->conn;
274 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100275 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100277 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200278 __conn_xprt_stop_recv(h2c->conn);
279 }
280 return buf;
281}
282
283static inline void h2_release_dbuf(struct h2c *h2c)
284{
285 if (h2c->dbuf->size) {
286 b_free(&h2c->dbuf);
287 offer_buffers(h2c->dbuf_wait.target,
288 tasks_run_queue + applets_active_queue);
289 }
290}
291
Willy Tarreau14398122017-09-22 14:26:04 +0200292/* re-enables sending on mux <target> after a buffer was allocated. It returns
293 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
294 * if it's impossible to wake up and we prefer to be woken up later.
295 */
296static int h2_mbuf_available(void *target)
297{
298 struct h2c *h2c = target;
299
300 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
301 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200302 if (h2c->flags & H2_CF_MUX_MALLOC) {
303 h2c->flags &= ~H2_CF_MUX_MALLOC;
304 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
305 conn_xprt_want_send(h2c->conn);
306 }
307
308 if (h2c->flags & H2_CF_DEM_MROOM) {
309 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100310 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200311 conn_xprt_want_recv(h2c->conn);
312 }
313
Willy Tarreau14398122017-09-22 14:26:04 +0200314 /* FIXME: we should in fact call something like h2_update_poll()
315 * now to recompte the polling. For now it will be enough like
316 * this.
317 */
Willy Tarreau14398122017-09-22 14:26:04 +0200318 return 1;
319 }
320 return 0;
321}
322
323static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
324{
325 struct buffer *buf = NULL;
326
327 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
328 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
329 h2c->mbuf_wait.target = h2c;
330 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200332 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200334
335 /* FIXME: we should in fact only block the direction being
336 * currently used. For now it will be enough like this.
337 */
338 __conn_xprt_stop_send(h2c->conn);
339 __conn_xprt_stop_recv(h2c->conn);
340 }
341 return buf;
342}
343
344static inline void h2_release_mbuf(struct h2c *h2c)
345{
346 if (h2c->mbuf->size) {
347 b_free(&h2c->mbuf);
348 offer_buffers(h2c->mbuf_wait.target,
349 tasks_run_queue + applets_active_queue);
350 }
351}
352
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200353
Willy Tarreau62f52692017-10-08 23:01:42 +0200354/*****************************************************************/
355/* functions below are dedicated to the mux setup and management */
356/*****************************************************************/
357
Willy Tarreau32218eb2017-09-22 08:07:25 +0200358/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
359static int h2c_frt_init(struct connection *conn)
360{
361 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100362 struct task *t = NULL;
363 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364
Willy Tarreaubafbe012017-11-24 17:34:44 +0100365 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200366 if (!h2c)
367 goto fail;
368
Willy Tarreau3f133572017-10-31 19:21:06 +0100369
Willy Tarreau599391a2017-11-24 10:16:00 +0100370 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
371 if (tick_isset(sess->fe->timeout.clientfin))
372 h2c->shut_timeout = sess->fe->timeout.clientfin;
373
Willy Tarreau33400292017-11-05 11:23:40 +0100374 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100375 if (tick_isset(h2c->timeout)) {
376 t = task_new(tid_bit);
377 if (!t)
378 goto fail;
379
380 h2c->task = t;
381 t->process = h2_timeout_task;
382 t->context = h2c;
383 t->expire = tick_add(now_ms, h2c->timeout);
384 }
Willy Tarreauea392822017-10-31 10:02:25 +0100385
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
387 if (!h2c->ddht)
388 goto fail;
389
390 /* Initialise the context. */
391 h2c->st0 = H2_CS_PREFACE;
392 h2c->conn = conn;
393 h2c->max_id = -1;
394 h2c->errcode = H2_ERR_NO_ERROR;
395 h2c->flags = H2_CF_NONE;
396 h2c->rcvd_c = 0;
397 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100398 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200399
400 h2c->dbuf = &buf_empty;
401 h2c->dsi = -1;
402 h2c->msi = -1;
403 h2c->last_sid = -1;
404
405 h2c->mbuf = &buf_empty;
406 h2c->miw = 65535; /* mux initial window size */
407 h2c->mws = 65535; /* mux window size */
408 h2c->mfs = 16384; /* initial max frame size */
409 h2c->streams_by_id = EB_ROOT_UNIQUE;
410 LIST_INIT(&h2c->send_list);
411 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200412 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200413 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200414 conn->mux_ctx = h2c;
415
Willy Tarreau3f133572017-10-31 19:21:06 +0100416 if (t)
417 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100419
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 /* mux->wake will be called soon to complete the operation */
421 return 0;
422 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100423 if (t)
424 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100425 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 return -1;
427}
428
Willy Tarreau62f52692017-10-08 23:01:42 +0200429/* Initialize the mux once it's attached. For outgoing connections, the context
430 * is already initialized before installing the mux, so we detect incoming
431 * connections from the fact that the context is still NULL. Returns < 0 on
432 * error.
433 */
434static int h2_init(struct connection *conn)
435{
436 if (conn->mux_ctx) {
437 /* we don't support outgoing connections for now */
438 return -1;
439 }
440
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200442}
443
Willy Tarreau2373acc2017-10-12 17:35:14 +0200444/* returns the stream associated with id <id> or NULL if not found */
445static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
446{
447 struct eb32_node *node;
448
Willy Tarreau2a856182017-05-16 15:20:39 +0200449 if (id > h2c->max_id)
450 return (struct h2s *)h2_idle_stream;
451
Willy Tarreau2373acc2017-10-12 17:35:14 +0200452 node = eb32_lookup(&h2c->streams_by_id, id);
453 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200454 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200455
456 return container_of(node, struct h2s, by_id);
457}
458
Willy Tarreau62f52692017-10-08 23:01:42 +0200459/* release function for a connection. This one should be called to free all
460 * resources allocated to the mux.
461 */
462static void h2_release(struct connection *conn)
463{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200464 struct h2c *h2c = conn->mux_ctx;
465
466 LIST_DEL(&conn->list);
467
468 if (h2c) {
469 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200470 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100471 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200472 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200474
475 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100476 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200477 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200479
Willy Tarreauea392822017-10-31 10:02:25 +0100480 if (h2c->task) {
481 task_delete(h2c->task);
482 task_free(h2c->task);
483 h2c->task = NULL;
484 }
485
Willy Tarreaubafbe012017-11-24 17:34:44 +0100486 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200487 }
488
489 conn->mux = NULL;
490 conn->mux_ctx = NULL;
491
492 conn_stop_tracking(conn);
493 conn_full_close(conn);
494 if (conn->destroy_cb)
495 conn->destroy_cb(conn);
496 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200497}
498
499
Willy Tarreau71681172017-10-23 14:39:06 +0200500/******************************************************/
501/* functions below are for the H2 protocol processing */
502/******************************************************/
503
504/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100505static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200506{
507 return h2s ? h2s->id : 0;
508}
509
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200510/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100511static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200512{
513 if (h2c->msi < 0)
514 return 0;
515
516 if (h2c->msi == h2s_id(h2s))
517 return 0;
518
519 return 1;
520}
521
Willy Tarreau741d6df2017-10-17 08:00:59 +0200522/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100523static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200524{
525 h2c->errcode = err;
526 h2c->st0 = H2_CS_ERROR;
527}
528
Willy Tarreau2e43f082017-10-17 08:03:59 +0200529/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100530static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200531{
532 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
533 h2s->errcode = err;
534 h2s->st = H2_SS_ERROR;
535 if (h2s->cs)
536 h2s->cs->flags |= CS_FL_ERROR;
537 }
538}
539
Willy Tarreaue4820742017-07-27 13:37:23 +0200540/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100541static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200542{
543 uint8_t *out = frame;
544
545 *out = len >> 16;
546 write_n16(out + 1, len);
547}
548
Willy Tarreau54c15062017-10-10 17:10:03 +0200549/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
550 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
551 * the caller's responsibility to verify that there are at least <bytes> bytes
552 * available in the buffer's input prior to calling this function.
553 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100554static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200555 const struct buffer *b, int o)
556{
557 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
558}
559
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200561{
562 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
563}
564
Willy Tarreau1f094672017-11-20 21:27:45 +0100565static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200566{
567 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
568}
569
Willy Tarreau1f094672017-11-20 21:27:45 +0100570static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200571{
572 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
573}
574
575
Willy Tarreau715d5312017-07-11 15:20:24 +0200576/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
577 * is not obvious. It turns out that H2 headers are neither aligned nor do they
578 * use regular sizes. And to add to the trouble, the buffer may wrap so each
579 * byte read must be checked. The header is formed like this :
580 *
581 * b0 b1 b2 b3 b4 b5..b8
582 * +----------+---------+--------+----+----+----------------------+
583 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
584 * +----------+---------+--------+----+----+----------------------+
585 *
586 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
587 * we get the sid properly aligned and ordered, and 16 bits of len properly
588 * ordered as well. The type and flags can be extracted using bit shifts from
589 * the word, and only one extra read is needed to fetch len[16:23].
590 * Returns zero if some bytes are missing, otherwise non-zero on success.
591 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100592static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200593{
594 uint64_t w;
595
596 if (b->i < 9)
597 return 0;
598
599 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
600 h->len = *b->p << 16;
601 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
602 h->ff = w >> 32;
603 h->ft = w >> 40;
604 h->len += w >> 48;
605 return 1;
606}
607
608/* skip the next 9 bytes corresponding to the frame header possibly parsed by
609 * h2_peek_frame_hdr() above.
610 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100611static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200612{
613 bi_del(b, 9);
614}
615
616/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100617static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200618{
619 int ret;
620
621 ret = h2_peek_frame_hdr(b, h);
622 if (ret > 0)
623 h2_skip_frame_hdr(b);
624 return ret;
625}
626
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200627/* creates a new stream <id> on the h2c connection and returns it, or NULL in
628 * case of memory allocation error.
629 */
630static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
631{
632 struct conn_stream *cs;
633 struct h2s *h2s;
634
Willy Tarreaubafbe012017-11-24 17:34:44 +0100635 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200636 if (!h2s)
637 goto out;
638
639 h2s->h2c = h2c;
640 h2s->mws = h2c->miw;
641 h2s->flags = H2_SF_NONE;
642 h2s->errcode = H2_ERR_NO_ERROR;
643 h2s->st = H2_SS_IDLE;
644 h1m_init(&h2s->req);
645 h1m_init(&h2s->res);
646 h2s->by_id.key = h2s->id = id;
647 h2c->max_id = id;
648 LIST_INIT(&h2s->list);
649
650 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100651 h2c->nb_streams++;
652 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
653 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200654
655 cs = cs_new(h2c->conn);
656 if (!cs)
657 goto out_close;
658
659 h2s->cs = cs;
660 cs->ctx = h2s;
661
662 if (stream_create_from_cs(cs) < 0)
663 goto out_free_cs;
664
665 /* OK done, the stream lives its own life now */
666 return h2s;
667
668 out_free_cs:
669 cs_free(cs);
670 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100671 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200672 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100673 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674 h2s = NULL;
675 out:
676 return h2s;
677}
678
Willy Tarreaube5b7152017-09-25 16:25:39 +0200679/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
680 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
681 * the various settings codes.
682 */
683static int h2c_snd_settings(struct h2c *h2c)
684{
685 struct buffer *res;
686 char buf_data[100]; // enough for 15 settings
687 struct chunk buf;
688 int ret;
689
690 if (h2c_mux_busy(h2c, NULL)) {
691 h2c->flags |= H2_CF_DEM_MBUSY;
692 return 0;
693 }
694
695 res = h2_get_mbuf(h2c);
696 if (!res) {
697 h2c->flags |= H2_CF_MUX_MALLOC;
698 h2c->flags |= H2_CF_DEM_MROOM;
699 return 0;
700 }
701
702 chunk_init(&buf, buf_data, sizeof(buf_data));
703 chunk_memcpy(&buf,
704 "\x00\x00\x00" /* length : 0 for now */
705 "\x04\x00" /* type : 4 (settings), flags : 0 */
706 "\x00\x00\x00\x00", /* stream ID : 0 */
707 9);
708
709 if (h2_settings_header_table_size != 4096) {
710 char str[6] = "\x00\x01"; /* header_table_size */
711
712 write_n32(str + 2, h2_settings_header_table_size);
713 chunk_memcat(&buf, str, 6);
714 }
715
716 if (h2_settings_initial_window_size != 65535) {
717 char str[6] = "\x00\x04"; /* initial_window_size */
718
719 write_n32(str + 2, h2_settings_initial_window_size);
720 chunk_memcat(&buf, str, 6);
721 }
722
723 if (h2_settings_max_concurrent_streams != 0) {
724 char str[6] = "\x00\x03"; /* max_concurrent_streams */
725
726 /* Note: 0 means "unlimited" for haproxy's config but not for
727 * the protocol, so never send this value!
728 */
729 write_n32(str + 2, h2_settings_max_concurrent_streams);
730 chunk_memcat(&buf, str, 6);
731 }
732
733 if (global.tune.bufsize != 16384) {
734 char str[6] = "\x00\x05"; /* max_frame_size */
735
736 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
737 * match bufsize - rewrite size, but at the moment it seems
738 * that clients don't take care of it.
739 */
740 write_n32(str + 2, global.tune.bufsize);
741 chunk_memcat(&buf, str, 6);
742 }
743
744 h2_set_frame_size(buf.str, buf.len - 9);
745 ret = bo_istput(res, ist2(buf.str, buf.len));
746 if (unlikely(ret <= 0)) {
747 if (!ret) {
748 h2c->flags |= H2_CF_MUX_MFULL;
749 h2c->flags |= H2_CF_DEM_MROOM;
750 return 0;
751 }
752 else {
753 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
754 return 0;
755 }
756 }
757 return ret;
758}
759
Willy Tarreau52eed752017-09-22 15:05:09 +0200760/* Try to receive a connection preface, then upon success try to send our
761 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
762 * missing data. It may return an error in h2c.
763 */
764static int h2c_frt_recv_preface(struct h2c *h2c)
765{
766 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200768
769 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
770
771 if (unlikely(ret1 <= 0)) {
772 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
773 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
774 return 0;
775 }
776
Willy Tarreaube5b7152017-09-25 16:25:39 +0200777 ret2 = h2c_snd_settings(h2c);
778 if (ret2 > 0)
779 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200780
Willy Tarreaube5b7152017-09-25 16:25:39 +0200781 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200782}
783
Willy Tarreau081d4722017-05-16 21:51:05 +0200784/* try to send a GOAWAY frame on the connection to report an error or a graceful
785 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
786 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
787 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
788 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
789 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
790 * on unrecoverable failure. It will not attempt to send one again in this last
791 * case so that it is safe to use h2c_error() to report such errors.
792 */
793static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
794{
795 struct buffer *res;
796 char str[17];
797 int ret;
798
799 if (h2c->flags & H2_CF_GOAWAY_FAILED)
800 return 1; // claim that it worked
801
802 if (h2c_mux_busy(h2c, h2s)) {
803 if (h2s)
804 h2s->flags |= H2_SF_BLK_MBUSY;
805 else
806 h2c->flags |= H2_CF_DEM_MBUSY;
807 return 0;
808 }
809
810 res = h2_get_mbuf(h2c);
811 if (!res) {
812 h2c->flags |= H2_CF_MUX_MALLOC;
813 if (h2s)
814 h2s->flags |= H2_SF_BLK_MROOM;
815 else
816 h2c->flags |= H2_CF_DEM_MROOM;
817 return 0;
818 }
819
820 /* len: 8, type: 7, flags: none, sid: 0 */
821 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
822
823 if (h2c->last_sid < 0)
824 h2c->last_sid = h2c->max_id;
825
826 write_n32(str + 9, h2c->last_sid);
827 write_n32(str + 13, h2c->errcode);
828 ret = bo_istput(res, ist2(str, 17));
829 if (unlikely(ret <= 0)) {
830 if (!ret) {
831 h2c->flags |= H2_CF_MUX_MFULL;
832 if (h2s)
833 h2s->flags |= H2_SF_BLK_MROOM;
834 else
835 h2c->flags |= H2_CF_DEM_MROOM;
836 return 0;
837 }
838 else {
839 /* we cannot report this error using GOAWAY, so we mark
840 * it and claim a success.
841 */
842 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
843 h2c->flags |= H2_CF_GOAWAY_FAILED;
844 return 1;
845 }
846 }
847 h2c->flags |= H2_CF_GOAWAY_SENT;
848 return ret;
849}
850
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100851/* Try to send an RST_STREAM frame on the connection for the indicated stream
852 * during mux operations. This stream must be valid and cannot be closed
853 * already. h2s->id will be used for the stream ID and h2s->errcode will be
854 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
855 * not yet.
856 *
857 * Returns > 0 on success or zero if nothing was done. In case of lack of room
858 * to write the message, it subscribes the stream to future notifications.
859 */
860static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
861{
862 struct buffer *res;
863 char str[13];
864 int ret;
865
866 if (!h2s || h2s->st == H2_SS_CLOSED)
867 return 1;
868
869 if (h2c_mux_busy(h2c, h2s)) {
870 h2s->flags |= H2_SF_BLK_MBUSY;
871 return 0;
872 }
873
874 res = h2_get_mbuf(h2c);
875 if (!res) {
876 h2c->flags |= H2_CF_MUX_MALLOC;
877 h2s->flags |= H2_SF_BLK_MROOM;
878 return 0;
879 }
880
881 /* len: 4, type: 3, flags: none */
882 memcpy(str, "\x00\x00\x04\x03\x00", 5);
883 write_n32(str + 5, h2s->id);
884 write_n32(str + 9, h2s->errcode);
885 ret = bo_istput(res, ist2(str, 13));
886
887 if (unlikely(ret <= 0)) {
888 if (!ret) {
889 h2c->flags |= H2_CF_MUX_MFULL;
890 h2s->flags |= H2_SF_BLK_MROOM;
891 return 0;
892 }
893 else {
894 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
895 return 0;
896 }
897 }
898
899 h2s->flags |= H2_SF_RST_SENT;
900 h2s->st = H2_SS_CLOSED;
901 return ret;
902}
903
904/* Try to send an RST_STREAM frame on the connection for the stream being
905 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
906 * error code unless the stream's state already is IDLE or CLOSED in which
907 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
908 * it was not yet.
909 *
910 * Returns > 0 on success or zero if nothing was done. In case of lack of room
911 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200912 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100913 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200914 */
915static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
916{
917 struct buffer *res;
918 char str[13];
919 int ret;
920
921 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100922 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200923 return 0;
924 }
925
926 res = h2_get_mbuf(h2c);
927 if (!res) {
928 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100929 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 return 0;
931 }
932
933 /* len: 4, type: 3, flags: none */
934 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100935
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100937 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200938 h2s->errcode : H2_ERR_STREAM_CLOSED);
939 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 if (unlikely(ret <= 0)) {
942 if (!ret) {
943 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100944 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200945 return 0;
946 }
947 else {
948 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
949 return 0;
950 }
951 }
952
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100953 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200954 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100955 h2s->st = H2_SS_CLOSED;
956 }
957
Willy Tarreau27a84c92017-10-17 08:10:17 +0200958 return ret;
959}
960
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100961/* try to send an empty DATA frame with the ES flag set to notify about the
962 * end of stream and match a shutdown(write). If an ES was already sent as
963 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
964 * on success or zero if nothing was done. In case of lack of room to write the
965 * message, it subscribes the requesting stream to future notifications.
966 */
967static int h2_send_empty_data_es(struct h2s *h2s)
968{
969 struct h2c *h2c = h2s->h2c;
970 struct buffer *res;
971 char str[9];
972 int ret;
973
Willy Tarreau721c9742017-11-07 11:05:42 +0100974 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100975 return 1;
976
977 if (h2c_mux_busy(h2c, h2s)) {
978 h2s->flags |= H2_SF_BLK_MBUSY;
979 return 0;
980 }
981
982 res = h2_get_mbuf(h2c);
983 if (!res) {
984 h2c->flags |= H2_CF_MUX_MALLOC;
985 h2s->flags |= H2_SF_BLK_MROOM;
986 return 0;
987 }
988
989 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
990 memcpy(str, "\x00\x00\x00\x00\x01", 5);
991 write_n32(str + 5, h2s->id);
992 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100993 if (likely(ret > 0)) {
994 h2s->flags |= H2_SF_ES_SENT;
995 }
996 else if (!ret) {
997 h2c->flags |= H2_CF_MUX_MFULL;
998 h2s->flags |= H2_SF_BLK_MROOM;
999 return 0;
1000 }
1001 else {
1002 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1003 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001004 }
1005 return ret;
1006}
1007
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001008/* wake the streams attached to the connection, whose id is greater than <last>,
1009 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1010 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1011 * stream's state is automatically updated accordingly.
1012 */
1013static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1014{
1015 struct eb32_node *node;
1016 struct h2s *h2s;
1017
1018 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1019 flags |= CS_FL_ERROR;
1020
1021 if (conn_xprt_read0_pending(h2c->conn))
1022 flags |= CS_FL_EOS;
1023
1024 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1025 while (node) {
1026 h2s = container_of(node, struct h2s, by_id);
1027 if (h2s->id <= last)
1028 break;
1029 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001030
1031 if (!h2s->cs) {
1032 /* this stream was already orphaned */
Willy Tarreau49745612017-12-03 18:56:02 +01001033 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001034 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001035 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001036 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001037 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001038
1039 h2s->cs->flags |= flags;
1040 /* recv is used to force to detect CS_FL_EOS that wake()
1041 * doesn't handle in the stream int code.
1042 */
1043 h2s->cs->data_cb->recv(h2s->cs);
1044 h2s->cs->data_cb->wake(h2s->cs);
1045
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001046 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1047 h2s->st = H2_SS_ERROR;
1048 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1049 h2s->st = H2_SS_HREM;
1050 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1051 h2s->st = H2_SS_CLOSED;
1052 }
1053}
1054
Willy Tarreau3421aba2017-07-27 15:41:03 +02001055/* Increase all streams' outgoing window size by the difference passed in
1056 * argument. This is needed upon receipt of the settings frame if the initial
1057 * window size is different. The difference may be negative and the resulting
1058 * window size as well, for the time it takes to receive some window updates.
1059 */
1060static void h2c_update_all_ws(struct h2c *h2c, int diff)
1061{
1062 struct h2s *h2s;
1063 struct eb32_node *node;
1064
1065 if (!diff)
1066 return;
1067
1068 node = eb32_first(&h2c->streams_by_id);
1069 while (node) {
1070 h2s = container_of(node, struct h2s, by_id);
1071 h2s->mws += diff;
1072 node = eb32_next(node);
1073 }
1074}
1075
1076/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1077 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1078 * return an error in h2c. Described in RFC7540#6.5.
1079 */
1080static int h2c_handle_settings(struct h2c *h2c)
1081{
1082 unsigned int offset;
1083 int error;
1084
1085 if (h2c->dff & H2_F_SETTINGS_ACK) {
1086 if (h2c->dfl) {
1087 error = H2_ERR_FRAME_SIZE_ERROR;
1088 goto fail;
1089 }
1090 return 1;
1091 }
1092
1093 if (h2c->dsi != 0) {
1094 error = H2_ERR_PROTOCOL_ERROR;
1095 goto fail;
1096 }
1097
1098 if (h2c->dfl % 6) {
1099 error = H2_ERR_FRAME_SIZE_ERROR;
1100 goto fail;
1101 }
1102
1103 /* that's the limit we can process */
1104 if (h2c->dfl > global.tune.bufsize) {
1105 error = H2_ERR_FRAME_SIZE_ERROR;
1106 goto fail;
1107 }
1108
1109 /* process full frame only */
1110 if (h2c->dbuf->i < h2c->dfl)
1111 return 0;
1112
1113 /* parse the frame */
1114 for (offset = 0; offset < h2c->dfl; offset += 6) {
1115 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1116 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1117
1118 switch (type) {
1119 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1120 /* we need to update all existing streams with the
1121 * difference from the previous iws.
1122 */
1123 if (arg < 0) { // RFC7540#6.5.2
1124 error = H2_ERR_FLOW_CONTROL_ERROR;
1125 goto fail;
1126 }
1127 h2c_update_all_ws(h2c, arg - h2c->miw);
1128 h2c->miw = arg;
1129 break;
1130 case H2_SETTINGS_MAX_FRAME_SIZE:
1131 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1132 error = H2_ERR_PROTOCOL_ERROR;
1133 goto fail;
1134 }
1135 h2c->mfs = arg;
1136 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001137 case H2_SETTINGS_ENABLE_PUSH:
1138 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1139 error = H2_ERR_PROTOCOL_ERROR;
1140 goto fail;
1141 }
1142 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001143 }
1144 }
1145
1146 /* need to ACK this frame now */
1147 h2c->st0 = H2_CS_FRAME_A;
1148 return 1;
1149 fail:
1150 h2c_error(h2c, error);
1151 return 0;
1152}
1153
1154/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1155 * success or one of the h2_status values.
1156 */
1157static int h2c_ack_settings(struct h2c *h2c)
1158{
1159 struct buffer *res;
1160 char str[9];
1161 int ret = -1;
1162
1163 if (h2c_mux_busy(h2c, NULL)) {
1164 h2c->flags |= H2_CF_DEM_MBUSY;
1165 return 0;
1166 }
1167
1168 res = h2_get_mbuf(h2c);
1169 if (!res) {
1170 h2c->flags |= H2_CF_MUX_MALLOC;
1171 h2c->flags |= H2_CF_DEM_MROOM;
1172 return 0;
1173 }
1174
1175 memcpy(str,
1176 "\x00\x00\x00" /* length : 0 (no data) */
1177 "\x04" "\x01" /* type : 4, flags : ACK */
1178 "\x00\x00\x00\x00" /* stream ID */, 9);
1179
1180 ret = bo_istput(res, ist2(str, 9));
1181 if (unlikely(ret <= 0)) {
1182 if (!ret) {
1183 h2c->flags |= H2_CF_MUX_MFULL;
1184 h2c->flags |= H2_CF_DEM_MROOM;
1185 return 0;
1186 }
1187 else {
1188 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1189 return 0;
1190 }
1191 }
1192 return ret;
1193}
1194
Willy Tarreaucf68c782017-10-10 17:11:41 +02001195/* processes a PING frame and schedules an ACK if needed. The caller must pass
1196 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1197 * missing data. It may return an error in h2c.
1198 */
1199static int h2c_handle_ping(struct h2c *h2c)
1200{
1201 /* frame length must be exactly 8 */
1202 if (h2c->dfl != 8) {
1203 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1204 return 0;
1205 }
1206
1207 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001208 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001209 h2c->st0 = H2_CS_FRAME_A;
1210 return 1;
1211}
1212
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001213/* Try to send a window update for stream id <sid> and value <increment>.
1214 * Returns > 0 on success or zero on missing room or failure. It may return an
1215 * error in h2c.
1216 */
1217static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1218{
1219 struct buffer *res;
1220 char str[13];
1221 int ret = -1;
1222
1223 if (h2c_mux_busy(h2c, NULL)) {
1224 h2c->flags |= H2_CF_DEM_MBUSY;
1225 return 0;
1226 }
1227
1228 res = h2_get_mbuf(h2c);
1229 if (!res) {
1230 h2c->flags |= H2_CF_MUX_MALLOC;
1231 h2c->flags |= H2_CF_DEM_MROOM;
1232 return 0;
1233 }
1234
1235 /* length: 4, type: 8, flags: none */
1236 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1237 write_n32(str + 5, sid);
1238 write_n32(str + 9, increment);
1239
1240 ret = bo_istput(res, ist2(str, 13));
1241
1242 if (unlikely(ret <= 0)) {
1243 if (!ret) {
1244 h2c->flags |= H2_CF_MUX_MFULL;
1245 h2c->flags |= H2_CF_DEM_MROOM;
1246 return 0;
1247 }
1248 else {
1249 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1250 return 0;
1251 }
1252 }
1253 return ret;
1254}
1255
1256/* try to send pending window update for the connection. It's safe to call it
1257 * with no pending updates. Returns > 0 on success or zero on missing room or
1258 * failure. It may return an error in h2c.
1259 */
1260static int h2c_send_conn_wu(struct h2c *h2c)
1261{
1262 int ret = 1;
1263
1264 if (h2c->rcvd_c <= 0)
1265 return 1;
1266
1267 /* send WU for the connection */
1268 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1269 if (ret > 0)
1270 h2c->rcvd_c = 0;
1271
1272 return ret;
1273}
1274
1275/* try to send pending window update for the current dmux stream. It's safe to
1276 * call it with no pending updates. Returns > 0 on success or zero on missing
1277 * room or failure. It may return an error in h2c.
1278 */
1279static int h2c_send_strm_wu(struct h2c *h2c)
1280{
1281 int ret = 1;
1282
1283 if (h2c->rcvd_s <= 0)
1284 return 1;
1285
1286 /* send WU for the stream */
1287 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1288 if (ret > 0)
1289 h2c->rcvd_s = 0;
1290
1291 return ret;
1292}
1293
Willy Tarreaucf68c782017-10-10 17:11:41 +02001294/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1295 * success, 0 on missing data or one of the h2_status values.
1296 */
1297static int h2c_ack_ping(struct h2c *h2c)
1298{
1299 struct buffer *res;
1300 char str[17];
1301 int ret = -1;
1302
1303 if (h2c->dbuf->i < 8)
1304 return 0;
1305
1306 if (h2c_mux_busy(h2c, NULL)) {
1307 h2c->flags |= H2_CF_DEM_MBUSY;
1308 return 0;
1309 }
1310
1311 res = h2_get_mbuf(h2c);
1312 if (!res) {
1313 h2c->flags |= H2_CF_MUX_MALLOC;
1314 h2c->flags |= H2_CF_DEM_MROOM;
1315 return 0;
1316 }
1317
1318 memcpy(str,
1319 "\x00\x00\x08" /* length : 8 (same payload) */
1320 "\x06" "\x01" /* type : 6, flags : ACK */
1321 "\x00\x00\x00\x00" /* stream ID */, 9);
1322
1323 /* copy the original payload */
1324 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1325
1326 ret = bo_istput(res, ist2(str, 17));
1327 if (unlikely(ret <= 0)) {
1328 if (!ret) {
1329 h2c->flags |= H2_CF_MUX_MFULL;
1330 h2c->flags |= H2_CF_DEM_MROOM;
1331 return 0;
1332 }
1333 else {
1334 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1335 return 0;
1336 }
1337 }
1338 return ret;
1339}
1340
Willy Tarreau26f95952017-07-27 17:18:30 +02001341/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1342 * Returns > 0 on success or zero on missing data. It may return an error in
1343 * h2c or h2s. Described in RFC7540#6.9.
1344 */
1345static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1346{
1347 int32_t inc;
1348 int error;
1349
1350 if (h2c->dfl != 4) {
1351 error = H2_ERR_FRAME_SIZE_ERROR;
1352 goto conn_err;
1353 }
1354
1355 /* process full frame only */
1356 if (h2c->dbuf->i < h2c->dfl)
1357 return 0;
1358
1359 inc = h2_get_n32(h2c->dbuf, 0);
1360
1361 if (h2c->dsi != 0) {
1362 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001363
1364 /* it's not an error to receive WU on a closed stream */
1365 if (h2s->st == H2_SS_CLOSED)
1366 return 1;
1367
1368 if (!inc) {
1369 error = H2_ERR_PROTOCOL_ERROR;
1370 goto strm_err;
1371 }
1372
1373 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1374 error = H2_ERR_FLOW_CONTROL_ERROR;
1375 goto strm_err;
1376 }
1377
1378 h2s->mws += inc;
1379 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1380 h2s->flags &= ~H2_SF_BLK_SFCTL;
1381 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1382 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1383 /* This stream wanted to send but could not due to its
1384 * own flow control. We can put it back into the send
1385 * list now, it will be handled upon next send() call.
1386 */
1387 LIST_ADDQ(&h2c->send_list, &h2s->list);
1388 }
1389 }
1390 }
1391 else {
1392 /* connection window update */
1393 if (!inc) {
1394 error = H2_ERR_PROTOCOL_ERROR;
1395 goto conn_err;
1396 }
1397
1398 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1399 error = H2_ERR_FLOW_CONTROL_ERROR;
1400 goto conn_err;
1401 }
1402
1403 h2c->mws += inc;
1404 }
1405
1406 return 1;
1407
1408 conn_err:
1409 h2c_error(h2c, error);
1410 return 0;
1411
1412 strm_err:
1413 if (h2s) {
1414 h2s_error(h2s, error);
1415 h2c->st0 = H2_CS_FRAME_A;
1416 }
1417 else
1418 h2c_error(h2c, error);
1419 return 0;
1420}
1421
Willy Tarreaue96b0922017-10-30 00:28:29 +01001422/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1423 * the last ID. Returns > 0 on success or zero on missing data. It may return
1424 * an error in h2c. Described in RFC7540#6.8.
1425 */
1426static int h2c_handle_goaway(struct h2c *h2c)
1427{
1428 int error;
1429 int last;
1430
1431 if (h2c->dsi != 0) {
1432 error = H2_ERR_PROTOCOL_ERROR;
1433 goto conn_err;
1434 }
1435
1436 if (h2c->dfl < 8) {
1437 error = H2_ERR_FRAME_SIZE_ERROR;
1438 goto conn_err;
1439 }
1440
1441 /* process full frame only */
1442 if (h2c->dbuf->i < h2c->dfl)
1443 return 0;
1444
1445 last = h2_get_n32(h2c->dbuf, 0);
1446 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1447 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001448 if (h2c->last_sid < 0)
1449 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001450 return 1;
1451
1452 conn_err:
1453 h2c_error(h2c, error);
1454 return 0;
1455}
1456
Willy Tarreau92153fc2017-12-03 19:46:19 +01001457/* processes a PRIORITY frame, and either skips it or rejects if it is
1458 * invalid. Returns > 0 on success or zero on missing data. It may return
1459 * an error in h2c. Described in RFC7540#6.3.
1460 */
1461static int h2c_handle_priority(struct h2c *h2c)
1462{
1463 int error;
1464
1465 if (h2c->dsi == 0) {
1466 error = H2_ERR_PROTOCOL_ERROR;
1467 goto conn_err;
1468 }
1469
1470 if (h2c->dfl != 5) {
1471 error = H2_ERR_FRAME_SIZE_ERROR;
1472 goto conn_err;
1473 }
1474
1475 /* process full frame only */
1476 if (h2c->dbuf->i < h2c->dfl)
1477 return 0;
1478
1479 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1480 /* 7540#5.3 : can't depend on itself */
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto conn_err;
1483 }
1484 return 1;
1485
1486 conn_err:
1487 h2c_error(h2c, error);
1488 return 0;
1489}
1490
Willy Tarreaucd234e92017-08-18 10:59:39 +02001491/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1492 * Returns > 0 on success or zero on missing data. It may return an error in
1493 * h2c. Described in RFC7540#6.4.
1494 */
1495static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1496{
1497 int error;
1498
1499 if (h2c->dsi == 0) {
1500 error = H2_ERR_PROTOCOL_ERROR;
1501 goto conn_err;
1502 }
1503
Willy Tarreaucd234e92017-08-18 10:59:39 +02001504 if (h2c->dfl != 4) {
1505 error = H2_ERR_FRAME_SIZE_ERROR;
1506 goto conn_err;
1507 }
1508
1509 /* process full frame only */
1510 if (h2c->dbuf->i < h2c->dfl)
1511 return 0;
1512
1513 /* late RST, already handled */
1514 if (h2s->st == H2_SS_CLOSED)
1515 return 1;
1516
1517 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1518 h2s->st = H2_SS_CLOSED;
1519
1520 if (h2s->cs) {
1521 h2s->cs->flags |= CS_FL_EOS;
1522 /* recv is used to force to detect CS_FL_EOS that wake()
1523 * doesn't handle in the stream-int code.
1524 */
1525 h2s->cs->data_cb->recv(h2s->cs);
1526 h2s->cs->data_cb->wake(h2s->cs);
1527 }
1528
1529 h2s->flags |= H2_SF_RST_RCVD;
1530 return 1;
1531
1532 conn_err:
1533 h2c_error(h2c, error);
1534 return 0;
1535}
1536
Willy Tarreau13278b42017-10-13 19:23:14 +02001537/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1538 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1539 * errors here are reported as connection errors since it's impossible to
1540 * recover from such errors after the compression context has been altered.
1541 */
1542static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1543{
1544 int error;
1545
1546 if (!h2c->dfl) {
1547 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1548 goto strm_err;
1549 }
1550
1551 if (!h2c->dbuf->size)
1552 return 0; // empty buffer
1553
1554 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1555 return 0; // incomplete frame
1556
1557 /* now either the frame is complete or the buffer is complete */
1558 if (h2s->st != H2_SS_IDLE) {
1559 /* FIXME: stream already exists, this is only allowed for
1560 * trailers (not supported for now).
1561 */
1562 error = H2_ERR_PROTOCOL_ERROR;
1563 goto conn_err;
1564 }
1565 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1566 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1567 error = H2_ERR_PROTOCOL_ERROR;
1568 goto conn_err;
1569 }
1570
1571 h2s = h2c_stream_new(h2c, h2c->dsi);
1572 if (!h2s) {
1573 error = H2_ERR_INTERNAL_ERROR;
1574 goto conn_err;
1575 }
1576
1577 h2s->st = H2_SS_OPEN;
1578 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1579 h2s->st = H2_SS_HREM;
1580 h2s->flags |= H2_SF_ES_RCVD;
1581 }
1582
1583 /* call the upper layers to process the frame, then let the upper layer
1584 * notify the stream about any change.
1585 */
1586 h2s->cs->data_cb->recv(h2s->cs);
1587
1588 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1589 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1590 error = H2_ERR_INTERNAL_ERROR;
1591 goto conn_err;
1592 }
1593
Willy Tarreau8f650c32017-11-21 19:36:21 +01001594 if (h2c->st0 >= H2_CS_ERROR)
1595 return 0;
1596
Willy Tarreau721c9742017-11-07 11:05:42 +01001597 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001598 /* stream error : send RST_STREAM */
1599 h2c->st0 = H2_CS_FRAME_A;
1600 }
1601 else {
1602 /* update the max stream ID if the request is being processed */
1603 if (h2s->id > h2c->max_id)
1604 h2c->max_id = h2s->id;
1605 }
1606
1607 return 1;
1608
1609 conn_err:
1610 h2c_error(h2c, error);
1611 return 0;
1612
1613 strm_err:
1614 if (h2s) {
1615 h2s_error(h2s, error);
1616 h2c->st0 = H2_CS_FRAME_A;
1617 }
1618 else
1619 h2c_error(h2c, error);
1620 return 0;
1621}
1622
Willy Tarreau454f9052017-10-26 19:40:35 +02001623/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1624 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1625 */
1626static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1627{
1628 int error;
1629
1630 /* note that empty DATA frames are perfectly valid and sometimes used
1631 * to signal an end of stream (with the ES flag).
1632 */
1633
1634 if (!h2c->dbuf->size && h2c->dfl)
1635 return 0; // empty buffer
1636
1637 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1638 return 0; // incomplete frame
1639
1640 /* now either the frame is complete or the buffer is complete */
1641
1642 if (!h2c->dsi) {
1643 /* RFC7540#6.1 */
1644 error = H2_ERR_PROTOCOL_ERROR;
1645 goto conn_err;
1646 }
1647
1648 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1649 /* RFC7540#6.1 */
1650 error = H2_ERR_STREAM_CLOSED;
1651 goto strm_err;
1652 }
1653
1654 /* last frame */
1655 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1656 h2s->st = H2_SS_HREM;
1657 h2s->flags |= H2_SF_ES_RCVD;
1658 }
1659
1660 /* call the upper layers to process the frame, then let the upper layer
1661 * notify the stream about any change.
1662 */
1663 if (!h2s->cs) {
1664 error = H2_ERR_STREAM_CLOSED;
1665 goto strm_err;
1666 }
1667
1668 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001669
Willy Tarreau454f9052017-10-26 19:40:35 +02001670 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1671 /* cs has just been destroyed, we have to kill h2s. */
1672 error = H2_ERR_STREAM_CLOSED;
1673 goto strm_err;
1674 }
1675
Willy Tarreau8f650c32017-11-21 19:36:21 +01001676 if (h2c->st0 >= H2_CS_ERROR)
1677 return 0;
1678
Willy Tarreau721c9742017-11-07 11:05:42 +01001679 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001680 /* stream error : send RST_STREAM */
1681 h2c->st0 = H2_CS_FRAME_A;
1682 }
1683
1684 /* check for completion : the callee will change this to FRAME_A or
1685 * FRAME_H once done.
1686 */
1687 if (h2c->st0 == H2_CS_FRAME_P)
1688 return 0;
1689
1690 return 1;
1691
1692 conn_err:
1693 h2c_error(h2c, error);
1694 return 0;
1695
1696 strm_err:
1697 if (h2s) {
1698 h2s_error(h2s, error);
1699 h2c->st0 = H2_CS_FRAME_A;
1700 }
1701 else
1702 h2c_error(h2c, error);
1703 return 0;
1704}
1705
Willy Tarreaubc933932017-10-09 16:21:43 +02001706/* process Rx frames to be demultiplexed */
1707static void h2_process_demux(struct h2c *h2c)
1708{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001709 struct h2s *h2s;
1710
Willy Tarreau081d4722017-05-16 21:51:05 +02001711 if (h2c->st0 >= H2_CS_ERROR)
1712 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001713
1714 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1715 if (h2c->st0 == H2_CS_PREFACE) {
1716 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1717 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1718 if (h2c->st0 == H2_CS_ERROR)
1719 h2c->st0 = H2_CS_ERROR2;
1720 goto fail;
1721 }
1722
1723 h2c->max_id = 0;
1724 h2c->st0 = H2_CS_SETTINGS1;
1725 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001726
1727 if (h2c->st0 == H2_CS_SETTINGS1) {
1728 struct h2_fh hdr;
1729
1730 /* ensure that what is pending is a valid SETTINGS frame
1731 * without an ACK.
1732 */
1733 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1734 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1735 if (h2c->st0 == H2_CS_ERROR)
1736 h2c->st0 = H2_CS_ERROR2;
1737 goto fail;
1738 }
1739
1740 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1741 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1742 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1743 h2c->st0 = H2_CS_ERROR2;
1744 goto fail;
1745 }
1746
1747 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1748 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1749 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1750 h2c->st0 = H2_CS_ERROR2;
1751 goto fail;
1752 }
1753
1754 /* that's OK, switch to FRAME_P to process it */
1755 h2c->dfl = hdr.len;
1756 h2c->dsi = hdr.sid;
1757 h2c->dft = hdr.ft;
1758 h2c->dff = hdr.ff;
1759 h2c->st0 = H2_CS_FRAME_P;
1760 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001761 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001762
1763 /* process as many incoming frames as possible below */
1764 while (h2c->dbuf->i) {
1765 int ret = 0;
1766
1767 if (h2c->st0 >= H2_CS_ERROR)
1768 break;
1769
1770 if (h2c->st0 == H2_CS_FRAME_H) {
1771 struct h2_fh hdr;
1772
1773 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1774 break;
1775
1776 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1777 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1778 h2c->st0 = H2_CS_ERROR;
1779 break;
1780 }
1781
1782 h2c->dfl = hdr.len;
1783 h2c->dsi = hdr.sid;
1784 h2c->dft = hdr.ft;
1785 h2c->dff = hdr.ff;
1786 h2c->st0 = H2_CS_FRAME_P;
1787 h2_skip_frame_hdr(h2c->dbuf);
1788 }
1789
1790 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001791 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001792
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001793 if (h2s->st == H2_SS_IDLE &&
1794 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1795 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1796 * this state MUST be treated as a connection error
1797 */
1798 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1799 h2c->st0 = H2_CS_ERROR;
1800 break;
1801 }
1802
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001803 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1804 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1805 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1806 * this state MUST be treated as a stream error
1807 */
1808 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1809 goto strm_err;
1810 }
1811
Willy Tarreauc0da1962017-10-30 18:38:00 +01001812#if 0
1813 // problem below: it is not possible to completely ignore such
1814 // streams as we need to maintain the compression state as well
1815 // and for this we need to completely process these frames (eg:
1816 // HEADERS frames) as well as counting DATA frames to emit
1817 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1818 // This is a typical case of layer violation where the
1819 // transported contents are critical to the connection's
1820 // validity and must be ignored at the same time :-(
1821
1822 /* graceful shutdown, ignore streams whose ID is higher than
1823 * the one advertised in GOAWAY. RFC7540#6.8.
1824 */
1825 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1826 ret = MIN(h2c->dbuf->i, h2c->dfl);
1827 bi_del(h2c->dbuf, ret);
1828 h2c->dfl -= ret;
1829 ret = h2c->dfl == 0;
1830 goto strm_err;
1831 }
1832#endif
1833
Willy Tarreau7e98c052017-10-10 15:56:59 +02001834 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001835 case H2_FT_SETTINGS:
1836 if (h2c->st0 == H2_CS_FRAME_P)
1837 ret = h2c_handle_settings(h2c);
1838
1839 if (h2c->st0 == H2_CS_FRAME_A)
1840 ret = h2c_ack_settings(h2c);
1841 break;
1842
Willy Tarreaucf68c782017-10-10 17:11:41 +02001843 case H2_FT_PING:
1844 if (h2c->st0 == H2_CS_FRAME_P)
1845 ret = h2c_handle_ping(h2c);
1846
1847 if (h2c->st0 == H2_CS_FRAME_A)
1848 ret = h2c_ack_ping(h2c);
1849 break;
1850
Willy Tarreau26f95952017-07-27 17:18:30 +02001851 case H2_FT_WINDOW_UPDATE:
1852 if (h2c->st0 == H2_CS_FRAME_P)
1853 ret = h2c_handle_window_update(h2c, h2s);
1854 break;
1855
Willy Tarreau61290ec2017-10-17 08:19:21 +02001856 case H2_FT_CONTINUATION:
1857 /* we currently don't support CONTINUATION frames since
1858 * we have nowhere to store the partial HEADERS frame.
1859 * Let's abort the stream on an INTERNAL_ERROR here.
1860 */
1861 if (h2c->st0 == H2_CS_FRAME_P)
1862 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1863 break;
1864
Willy Tarreau13278b42017-10-13 19:23:14 +02001865 case H2_FT_HEADERS:
1866 if (h2c->st0 == H2_CS_FRAME_P)
1867 ret = h2c_frt_handle_headers(h2c, h2s);
1868 break;
1869
Willy Tarreau454f9052017-10-26 19:40:35 +02001870 case H2_FT_DATA:
1871 if (h2c->st0 == H2_CS_FRAME_P)
1872 ret = h2c_frt_handle_data(h2c, h2s);
1873
1874 if (h2c->st0 == H2_CS_FRAME_A)
1875 ret = h2c_send_strm_wu(h2c);
1876 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001877
Willy Tarreau92153fc2017-12-03 19:46:19 +01001878 case H2_FT_PRIORITY:
1879 if (h2c->st0 == H2_CS_FRAME_P)
1880 ret = h2c_handle_priority(h2c);
1881 break;
1882
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883 case H2_FT_RST_STREAM:
1884 if (h2c->st0 == H2_CS_FRAME_P)
1885 ret = h2c_handle_rst_stream(h2c, h2s);
1886 break;
1887
Willy Tarreaue96b0922017-10-30 00:28:29 +01001888 case H2_FT_GOAWAY:
1889 if (h2c->st0 == H2_CS_FRAME_P)
1890 ret = h2c_handle_goaway(h2c);
1891 break;
1892
Willy Tarreau1c661982017-10-30 13:52:01 +01001893 case H2_FT_PUSH_PROMISE:
1894 /* not permitted here, RFC7540#5.1 */
1895 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001896 break;
1897
1898 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001899 default:
1900 /* drop frames that we ignore. They may be larger than
1901 * the buffer so we drain all of their contents until
1902 * we reach the end.
1903 */
1904 ret = MIN(h2c->dbuf->i, h2c->dfl);
1905 bi_del(h2c->dbuf, ret);
1906 h2c->dfl -= ret;
1907 ret = h2c->dfl == 0;
1908 }
1909
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001910 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001911 /* RST are sent similarly to frame acks */
1912 if (h2s->st == H2_SS_ERROR) {
1913 if (h2c->st0 == H2_CS_FRAME_P)
1914 h2c->st0 = H2_CS_FRAME_A;
1915
1916 if (h2c->st0 == H2_CS_FRAME_A)
1917 ret = h2c_send_rst_stream(h2c, h2s);
1918 }
1919
Willy Tarreau7e98c052017-10-10 15:56:59 +02001920 /* error or missing data condition met above ? */
1921 if (ret <= 0)
1922 break;
1923
1924 if (h2c->st0 != H2_CS_FRAME_H) {
1925 bi_del(h2c->dbuf, h2c->dfl);
1926 h2c->st0 = H2_CS_FRAME_H;
1927 }
1928 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001929
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001930 if (h2c->rcvd_c > 0 &&
1931 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1932 h2c_send_conn_wu(h2c);
1933
Willy Tarreau52eed752017-09-22 15:05:09 +02001934 fail:
1935 /* we can go here on missing data, blocked response or error */
1936 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001937}
1938
1939/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1940 * the end.
1941 */
1942static int h2_process_mux(struct h2c *h2c)
1943{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001944 struct h2s *h2s, *h2s_back;
1945
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001946 /* start by sending possibly pending window updates */
1947 if (h2c->rcvd_c > 0 &&
1948 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1949 h2c_send_conn_wu(h2c) < 0)
1950 goto fail;
1951
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001952 /* First we always process the flow control list because the streams
1953 * waiting there were already elected for immediate emission but were
1954 * blocked just on this.
1955 */
1956
1957 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1958 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1959 h2c->st0 >= H2_CS_ERROR)
1960 break;
1961
1962 /* In theory it's possible that h2s->cs == NULL here :
1963 * - client sends crap that causes a parse error
1964 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1965 * - RST_STREAM cannot be emitted because mux is busy/full
1966 * - stream gets notified, detaches and quits
1967 * - mux buffer gets ready and wakes pending streams up
1968 * - bam!
1969 */
1970 h2s->flags &= ~H2_SF_BLK_ANY;
1971
1972 if (h2s->cs) {
1973 h2s->cs->data_cb->send(h2s->cs);
1974 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001975 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001976 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001977 }
1978
1979 /* depending on callee's blocking reasons, we may queue in send
1980 * list or completely dequeue.
1981 */
1982 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1983 if (h2s->flags & H2_SF_BLK_ANY) {
1984 LIST_DEL(&h2s->list);
1985 LIST_ADDQ(&h2c->send_list, &h2s->list);
1986 }
1987 else {
1988 LIST_DEL(&h2s->list);
1989 LIST_INIT(&h2s->list);
1990 if (h2s->cs)
1991 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001992 else {
1993 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01001994 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001995 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001996 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001997 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001998 }
1999 }
2000 }
2001
2002 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2003 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2004 break;
2005
2006 /* In theory it's possible that h2s->cs == NULL here :
2007 * - client sends crap that causes a parse error
2008 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2009 * - RST_STREAM cannot be emitted because mux is busy/full
2010 * - stream gets notified, detaches and quits
2011 * - mux buffer gets ready and wakes pending streams up
2012 * - bam!
2013 */
2014 h2s->flags &= ~H2_SF_BLK_ANY;
2015
2016 if (h2s->cs) {
2017 h2s->cs->data_cb->send(h2s->cs);
2018 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002019 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002020 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002021 }
2022 /* depending on callee's blocking reasons, we may queue in fctl
2023 * list or completely dequeue.
2024 */
2025 if (h2s->flags & H2_SF_BLK_MFCTL) {
2026 /* stream hit the connection's flow control */
2027 LIST_DEL(&h2s->list);
2028 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2029 }
2030 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2031 LIST_DEL(&h2s->list);
2032 LIST_INIT(&h2s->list);
2033 if (h2s->cs)
2034 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002035 else {
2036 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01002037 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002038 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002039 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002040 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002041 }
2042 }
2043
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002044 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002045 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002046 if (h2c->st0 == H2_CS_ERROR) {
2047 if (h2c->max_id >= 0) {
2048 h2c_send_goaway_error(h2c, NULL);
2049 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2050 return 0;
2051 }
2052
2053 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2054 }
2055 return 1;
2056 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002057 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002058}
2059
Willy Tarreau71681172017-10-23 14:39:06 +02002060
Willy Tarreau62f52692017-10-08 23:01:42 +02002061/*********************************************************/
2062/* functions below are I/O callbacks from the connection */
2063/*********************************************************/
2064
2065/* callback called on recv event by the connection handler */
2066static void h2_recv(struct connection *conn)
2067{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002068 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002069 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002070 int max;
2071
Willy Tarreau315d8072017-12-10 22:17:57 +01002072 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002073 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002074
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002075 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002076 if (!buf) {
2077 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002078 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002079 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002080
Willy Tarreaua2af5122017-10-09 11:56:46 +02002081 /* note: buf->o == 0 */
2082 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002083 if (max)
2084 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002085
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002086 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002087 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002088 return;
2089 }
2090
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002091 if (buf->i == buf->size)
2092 h2c->flags |= H2_CF_DEM_DFULL;
2093
Willy Tarreaubc933932017-10-09 16:21:43 +02002094 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002095
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002096 /* after streams have been processed, we should have made some room */
Willy Tarreau315d8072017-12-10 22:17:57 +01002097 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreau081d4722017-05-16 21:51:05 +02002098 buf->i = 0;
2099
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002100 if (buf->i != buf->size)
2101 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002102 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002103}
2104
2105/* callback called on send event by the connection handler */
2106static void h2_send(struct connection *conn)
2107{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002108 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002109 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002110
2111 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002112 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002113
2114 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2115 /* a handshake was requested */
2116 return;
2117 }
2118
Willy Tarreaubc933932017-10-09 16:21:43 +02002119 /* This loop is quite simple : it tries to fill as much as it can from
2120 * pending streams into the existing buffer until it's reportedly full
2121 * or the end of send requests is reached. Then it tries to send this
2122 * buffer's contents out, marks it not full if at least one byte could
2123 * be sent, and tries again.
2124 *
2125 * The snd_buf() function normally takes a "flags" argument which may
2126 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2127 * data immediately comes and CO_SFL_STREAMER to indicate that the
2128 * connection is streaming lots of data (used to increase TLS record
2129 * size at the expense of latency). The former can be sent any time
2130 * there's a buffer full flag, as it indicates at least one stream
2131 * attempted to send and failed so there are pending data. An
2132 * alternative would be to set it as long as there's an active stream
2133 * but that would be problematic for ACKs until we have an absolute
2134 * guarantee that all waiters have at least one byte to send. The
2135 * latter should possibly not be set for now.
2136 */
2137
2138 done = 0;
2139 while (!done) {
2140 unsigned int flags = 0;
2141
2142 /* fill as much as we can into the current buffer */
2143 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2144 done = h2_process_mux(h2c);
2145
2146 if (conn->flags & CO_FL_ERROR)
2147 break;
2148
2149 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2150 flags |= CO_SFL_MSG_MORE;
2151
Willy Tarreau319994a2017-11-07 11:03:56 +01002152 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002153 break;
2154
2155 /* wrote at least one byte, the buffer is not full anymore */
2156 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2157 }
2158
Willy Tarreaua2af5122017-10-09 11:56:46 +02002159 if (conn->flags & CO_FL_SOCK_WR_SH) {
2160 /* output closed, nothing to send, clear the buffer to release it */
2161 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002162 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002163}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002164
Willy Tarreau62f52692017-10-08 23:01:42 +02002165/* callback called on any event by the connection handler.
2166 * It applies changes and returns zero, or < 0 if it wants immediate
2167 * destruction of the connection (which normally doesn not happen in h2).
2168 */
2169static int h2_wake(struct connection *conn)
2170{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002171 struct h2c *h2c = conn->mux_ctx;
2172
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002173 /*
2174 * If we received early data, try to wake any stream, just in case
2175 * at least one of them was waiting for the handshake
2176 */
2177 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2178 CO_FL_EARLY_DATA) {
2179 h2_wake_some_streams(h2c, 0, 0);
2180 conn->flags &= ~CO_FL_EARLY_DATA;
2181 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002182 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002183 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2184 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2185 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002186 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002187
2188 if (eb_is_empty(&h2c->streams_by_id)) {
2189 /* no more stream, kill the connection now */
2190 h2_release(conn);
2191 return -1;
2192 }
2193 else {
2194 /* some streams still there, we need to signal them all and
2195 * wait for their departure.
2196 */
2197 __conn_xprt_stop_recv(conn);
2198 __conn_xprt_stop_send(conn);
2199 return 0;
2200 }
2201 }
2202
2203 if (!h2c->dbuf->i)
2204 h2_release_dbuf(h2c);
2205
2206 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002207 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002208 __conn_xprt_stop_recv(conn);
2209 }
2210 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002211 __conn_xprt_want_recv(conn);
2212 }
2213
2214 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002215 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2216 (h2c->st0 == H2_CS_ERROR ||
2217 h2c->mbuf->o ||
2218 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2219 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002220 __conn_xprt_want_send(conn);
2221 }
2222 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002223 h2_release_mbuf(h2c);
2224 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002225 }
2226
Willy Tarreau3f133572017-10-31 19:21:06 +01002227 if (h2c->task) {
2228 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002229 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002230 task_queue(h2c->task);
2231 }
2232 else
2233 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002234 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002235 return 0;
2236}
2237
Willy Tarreauea392822017-10-31 10:02:25 +01002238/* Connection timeout management. The principle is that if there's no receipt
2239 * nor sending for a certain amount of time, the connection is closed. If the
2240 * MUX buffer still has lying data or is not allocatable, the connection is
2241 * immediately killed. If it's allocatable and empty, we attempt to send a
2242 * GOAWAY frame.
2243 */
2244static struct task *h2_timeout_task(struct task *t)
2245{
2246 struct h2c *h2c = t->context;
2247 int expired = tick_is_expired(t->expire, now_ms);
2248
2249 if (!expired)
2250 return t;
2251
2252 h2c_error(h2c, H2_ERR_NO_ERROR);
2253 h2_wake_some_streams(h2c, 0, 0);
2254
2255 if (h2c->mbuf->o) {
2256 /* don't even try to send a GOAWAY, the buffer is stuck */
2257 h2c->flags |= H2_CF_GOAWAY_FAILED;
2258 }
2259
2260 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002261 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002262 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2263 h2c->flags |= H2_CF_GOAWAY_FAILED;
2264
2265 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2266 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2267
2268 if (!eb_is_empty(&h2c->streams_by_id))
2269 goto wait;
2270
2271 h2_release(h2c->conn);
2272 return NULL;
2273
2274 wait:
2275 /* the streams have been notified, we must let them finish and close */
2276 h2c->task = NULL;
2277 task_delete(t);
2278 task_free(t);
2279 return NULL;
2280}
2281
2282
Willy Tarreau62f52692017-10-08 23:01:42 +02002283/*******************************************/
2284/* functions below are used by the streams */
2285/*******************************************/
2286
2287/*
2288 * Attach a new stream to a connection
2289 * (Used for outgoing connections)
2290 */
2291static struct conn_stream *h2_attach(struct connection *conn)
2292{
2293 return NULL;
2294}
2295
2296/* callback used to update the mux's polling flags after changing a cs' status.
2297 * The caller (cs_update_mux_polling) will take care of propagating any changes
2298 * to the transport layer.
2299 */
2300static void h2_update_poll(struct conn_stream *cs)
2301{
Willy Tarreau1d393222017-10-17 10:26:19 +02002302 struct h2s *h2s = cs->ctx;
2303
2304 if (!h2s)
2305 return;
2306
Willy Tarreaud7739c82017-10-30 15:38:23 +01002307 /* we may unblock a blocked read */
2308
Willy Tarreau315d8072017-12-10 22:17:57 +01002309 if (cs->flags & CS_FL_DATA_RD_ENA) {
2310 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002311 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau315d8072017-12-10 22:17:57 +01002312 if (h2s->h2c->dsi == h2s->id)
2313 conn_xprt_want_recv(cs->conn);
Willy Tarreaud7739c82017-10-30 15:38:23 +01002314 }
2315
Willy Tarreau1d393222017-10-17 10:26:19 +02002316 /* Note: the stream and stream-int code doesn't allow us to perform a
2317 * synchronous send() here unfortunately, because this code is called
2318 * as si_update() from the process_stream() context. This means that
2319 * we have to queue the current cs and defer its processing after the
2320 * connection's cs list is processed anyway.
2321 */
2322
2323 if (cs->flags & CS_FL_DATA_WR_ENA) {
2324 if (LIST_ISEMPTY(&h2s->list)) {
2325 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2326 !h2s->h2c->mbuf->o && // not yet subscribed
2327 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2328 conn_xprt_want_send(cs->conn);
2329 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2330 }
2331 }
2332 else if (!LIST_ISEMPTY(&h2s->list)) {
2333 LIST_DEL(&h2s->list);
2334 LIST_INIT(&h2s->list);
2335 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2336 }
2337
2338 /* this can happen from within si_chk_snd() */
2339 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2340 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002341}
2342
2343/*
2344 * Detach the stream from the connection and possibly release the connection.
2345 */
2346static void h2_detach(struct conn_stream *cs)
2347{
Willy Tarreau60935142017-10-16 18:11:19 +02002348 struct h2s *h2s = cs->ctx;
2349 struct h2c *h2c;
2350
2351 cs->ctx = NULL;
2352 if (!h2s)
2353 return;
2354
2355 h2c = h2s->h2c;
2356 h2s->cs = NULL;
2357
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002358 /* this stream may be blocked waiting for some data to leave (possibly
2359 * an ES or RST frame), so orphan it in this case.
2360 */
2361 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2362 return;
2363
Willy Tarreau541dd822017-11-23 18:12:50 +01002364 /* the stream could be in the send list */
2365 LIST_DEL(&h2s->list);
2366
Willy Tarreau45f752e2017-10-30 15:44:59 +01002367 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2368 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2369 /* unblock the connection if it was blocked on this
2370 * stream.
2371 */
2372 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2373 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2374 conn_xprt_want_recv(cs->conn);
2375 conn_xprt_want_send(cs->conn);
2376 }
2377
Willy Tarreau60935142017-10-16 18:11:19 +02002378 if (h2s->by_id.node.leaf_p) {
2379 /* h2s still attached to the h2c */
Willy Tarreau49745612017-12-03 18:56:02 +01002380 h2c->nb_streams--;
Willy Tarreau60935142017-10-16 18:11:19 +02002381 eb32_delete(&h2s->by_id);
2382
2383 /* We don't want to close right now unless we're removing the
2384 * last stream, and either the connection is in error, or it
2385 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002386 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002387 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002388 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2389 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002390 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002391 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2392 (conn_xprt_read0_pending(h2c->conn) ||
2393 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002394 /* no more stream will come, kill it now */
2395 h2_release(h2c->conn);
2396 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002397 else if (h2c->task) {
2398 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002399 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002400 task_queue(h2c->task);
2401 }
2402 else
2403 h2c->task->expire = TICK_ETERNITY;
2404 }
Willy Tarreau60935142017-10-16 18:11:19 +02002405 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002406 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002407}
2408
2409static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2410{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002411 struct h2s *h2s = cs->ctx;
2412
2413 if (!mode)
2414 return;
2415
Willy Tarreau721c9742017-11-07 11:05:42 +01002416 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002417 return;
2418
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002419 /* if no outgoing data was seen on this stream, it means it was
2420 * closed with a "tcp-request content" rule that is normally
2421 * used to kill the connection ASAP (eg: limit abuse). In this
2422 * case we send a goaway to close the connection.
2423 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002424 if (!(h2s->flags & H2_SF_RST_SENT) &&
2425 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2426 return;
2427
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002428 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2429 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2430 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2431 return;
2432
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002433 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2434 conn_xprt_want_send(cs->conn);
2435
2436 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002437}
2438
2439static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2440{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002441 struct h2s *h2s = cs->ctx;
2442
Willy Tarreau721c9742017-11-07 11:05:42 +01002443 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002444 return;
2445
Willy Tarreau67434202017-11-06 20:20:51 +01002446 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002447 /* we can cleanly close using an empty data frame only after headers */
2448
2449 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2450 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002451 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002452
2453 if (h2s->st == H2_SS_HREM)
2454 h2s->st = H2_SS_CLOSED;
2455 else
2456 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002457 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002458 /* if no outgoing data was seen on this stream, it means it was
2459 * closed with a "tcp-request content" rule that is normally
2460 * used to kill the connection ASAP (eg: limit abuse). In this
2461 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002462 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002463 if (!(h2s->flags & H2_SF_RST_SENT) &&
2464 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2465 return;
2466
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002467 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2468 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002469 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2470 return;
2471
Willy Tarreau58e32082017-11-07 14:41:09 +01002472 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002473 }
2474
2475 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2476 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002477}
2478
Willy Tarreau13278b42017-10-13 19:23:14 +02002479/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2480 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2481 * proceed. Stream errors are reported in h2s->errcode and connection errors
2482 * in h2c->errcode. The caller must already have checked the frame header and
2483 * ensured that the frame was complete or the buffer full.
2484 */
2485static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2486{
2487 struct h2c *h2c = h2s->h2c;
2488 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002489 struct chunk *tmp = get_trash_chunk();
2490 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002491 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002492 int flen = h2c->dfl;
2493 int outlen = 0;
2494 int wrap;
2495 int try;
2496
2497 if (!h2c->dfl) {
2498 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2499 return 0;
2500 }
2501
2502 /* if the input buffer wraps, take a temporary copy of it (rare) */
2503 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2504 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002505 copy = alloc_trash_chunk();
2506 if (!copy) {
2507 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2508 goto fail;
2509 }
2510 memcpy(copy->str, h2c->dbuf->p, wrap);
2511 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2512 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002513 }
2514
2515 /* The padlen is the first byte before data, and the padding appears
2516 * after data. padlen+data+padding are included in flen.
2517 */
2518 if (h2c->dff & H2_F_HEADERS_PADDED) {
2519 if (*hdrs >= flen) {
2520 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2521 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002522 return 0;
2523 }
2524 flen -= *hdrs + 1;
2525 hdrs += 1; // skip Pad Length
2526 }
2527
2528 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2529 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002530 if (read_n32(hdrs) == h2s->id) {
2531 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2532 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2533 return 0;//goto fail_stream;
2534 }
2535
Willy Tarreau13278b42017-10-13 19:23:14 +02002536 hdrs += 5; // stream dep = 4, weight = 1
2537 flen -= 5;
2538 }
2539
2540 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2541 * don't support this for now and can't even decompress so we have to
2542 * break the connection.
2543 */
2544 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2545 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002546 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002547 }
2548
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002549 /* we can't retry a failed decompression operation so we must be very
2550 * careful not to take any risks. In practice the output buffer is
2551 * always empty except maybe for trailers, so these operations almost
2552 * never happen.
2553 */
2554 if (unlikely(buf->o)) {
2555 /* need to let the output buffer flush and
2556 * mark the buffer for later wake up.
2557 */
2558 goto fail;
2559 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002560
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002561 if (unlikely(buffer_space_wraps(buf))) {
2562 /* it doesn't fit and the buffer is fragmented,
2563 * so let's defragment it and try again.
2564 */
2565 buffer_slow_realign(buf);
2566 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002567
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002568 /* first check if we have some room after p+i */
2569 try = buf->data + buf->size - (buf->p + buf->i);
2570
2571 /* otherwise continue between data and p-o */
2572 if (try <= 0) {
2573 try = buf->p - (buf->data + buf->o);
2574 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002575 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002576 }
2577 if (try > count)
2578 try = count;
2579
2580 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2581 sizeof(list)/sizeof(list[0]), tmp);
2582 if (outlen < 0) {
2583 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2584 goto fail;
2585 }
2586
2587 /* OK now we have our header list in <list> */
2588 outlen = h2_make_h1_request(list, bi_end(buf), try);
2589
2590 if (outlen < 0) {
2591 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2592 goto fail;
2593 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002594
2595 /* now consume the input data */
2596 bi_del(h2c->dbuf, h2c->dfl);
2597 h2c->st0 = H2_CS_FRAME_H;
2598 buf->i += outlen;
2599
2600 /* don't send it before returning data!
2601 * FIXME: should we instead try to send it much later, after the
2602 * response ? This would require that we keep a copy of it in h2s.
2603 */
2604 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2605 h2s->cs->flags |= CS_FL_EOS;
2606 h2s->flags |= H2_SF_ES_RCVD;
2607 }
2608
Willy Tarreau68dd9852017-07-03 14:44:26 +02002609 leave:
2610 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002611 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002612 fail:
2613 outlen = 0;
2614 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002615}
2616
Willy Tarreau454f9052017-10-26 19:40:35 +02002617/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2618 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2619 * in use, a new chunk is emitted for each frame. This is supposed to fit
2620 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2621 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2622 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2623 * parser state is automatically updated. Returns the number of bytes emitted
2624 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2625 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2626 * have checked the frame header and ensured that the frame was complete or the
2627 * buffer full. It changes the frame state to FRAME_A once done.
2628 */
2629static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2630{
2631 struct h2c *h2c = h2s->h2c;
2632 int block1, block2;
2633 unsigned int flen = h2c->dfl;
2634 unsigned int padlen = 0;
2635 int offset = 0;
2636
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002637 h2s->cs->flags &= ~CS_FL_RCV_MORE;
2638
Willy Tarreau454f9052017-10-26 19:40:35 +02002639 if (h2c->dbuf->i < flen)
2640 return 0;
2641
2642 /* The padlen is the first byte before data, and the padding appears
2643 * after data. padlen+data+padding are included in flen.
2644 */
Willy Tarreau79127812017-12-03 21:06:59 +01002645 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002646 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2647 if (padlen >= flen) {
2648 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2649 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002650 return 0;
2651 }
2652 flen -= padlen + 1;
2653 offset = 1; // skip Pad Length
2654 }
2655
2656 /* does it fit in output buffer or should we wait ? */
2657 if (buf->i + buf->o + flen > buf->size) {
2658 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002659 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau454f9052017-10-26 19:40:35 +02002660 return 0;
2661 }
2662
2663 /* Block1 is the length of the first block before the buffer wraps,
2664 * block2 is the optional second block to reach the end of the frame.
2665 */
2666 block1 = bi_contig_data(h2c->dbuf);
2667 if (block1 > offset + flen)
2668 block1 = offset + flen;
2669 block1 -= offset; // skip Pad Length
2670 block2 = flen - block1;
2671
2672 if (block1)
2673 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2674
2675 if (block2)
2676 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2677
2678 /* now mark the input data as consumed (will be deleted from the buffer
2679 * by the caller when seeing FRAME_A after sending the window update).
2680 */
2681 h2c->rcvd_c += h2c->dfl;
2682 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2683 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2684
2685 /* don't send it before returning data!
2686 * FIXME: should we instead try to send it much later, after the
2687 * response ? This would require that we keep a copy of it in h2s.
2688 */
Willy Tarreau79127812017-12-03 21:06:59 +01002689 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002690 h2s->cs->flags |= CS_FL_EOS;
2691 h2s->flags |= H2_SF_ES_RCVD;
2692 }
2693
2694 return flen;
2695}
2696
Willy Tarreau62f52692017-10-08 23:01:42 +02002697/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002698 * Called from the upper layer to get more data, up to <count> bytes. The
2699 * caller is responsible for never asking for more data than what is available
2700 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002701 */
2702static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2703{
Willy Tarreau13278b42017-10-13 19:23:14 +02002704 struct h2s *h2s = cs->ctx;
2705 struct h2c *h2c = h2s->h2c;
2706 int ret = 0;
2707
2708 if (h2c->st0 != H2_CS_FRAME_P)
2709 return 0; // no pre-parsed frame yet
2710
2711 if (h2c->dsi != h2s->id)
2712 return 0; // not for us
2713
2714 if (!h2c->dbuf->size)
2715 return 0; // empty buffer
2716
2717 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2718 return 0; // incomplete input frame
2719
2720 switch (h2c->dft) {
2721 case H2_FT_HEADERS:
2722 ret = h2_frt_decode_headers(h2s, buf, count);
2723 break;
2724
Willy Tarreau454f9052017-10-26 19:40:35 +02002725 case H2_FT_DATA:
2726 ret = h2_frt_transfer_data(h2s, buf, count);
2727 break;
2728
Willy Tarreau13278b42017-10-13 19:23:14 +02002729 default:
2730 ret = 0;
2731 }
2732 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002733}
2734
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002735/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2736 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2737 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2738 * to the number of buffer bytes consumed.
2739 */
2740static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2741{
2742 struct http_hdr list[MAX_HTTP_HDR];
2743 struct h2c *h2c = h2s->h2c;
2744 struct h1m *h1m = &h2s->res;
2745 struct chunk outbuf;
2746 int es_now = 0;
2747 int ret = 0;
2748 int hdr;
2749
2750 if (h2c_mux_busy(h2c, h2s)) {
2751 h2s->flags |= H2_SF_BLK_MBUSY;
2752 return 0;
2753 }
2754
2755 if (!h2_get_mbuf(h2c)) {
2756 h2c->flags |= H2_CF_MUX_MALLOC;
2757 h2s->flags |= H2_SF_BLK_MROOM;
2758 return 0;
2759 }
2760
2761 /* First, try to parse the H1 response and index it into <list>.
2762 * NOTE! Since it comes from haproxy, we *know* that a response header
2763 * block does not wrap and we can safely read it this way without
2764 * having to realign the buffer.
2765 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002766 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002767 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2768 list, sizeof(list)/sizeof(list[0]), h1m);
2769 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002770 /* incomplete or invalid response, this is abnormal coming from
2771 * haproxy and may only result in a bad errorfile or bad Lua code
2772 * so that won't be fixed, raise an error now.
2773 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002774 * FIXME: we should instead add the ability to only return a
2775 * 502 bad gateway. But in theory this is not supposed to
2776 * happen.
2777 */
2778 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2779 ret = 0;
2780 goto end;
2781 }
2782
2783 chunk_reset(&outbuf);
2784
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002785 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002786 while (1) {
2787 outbuf.str = bo_end(h2c->mbuf);
2788 outbuf.size = bo_contig_space(h2c->mbuf);
2789 outbuf.len = 0;
2790
2791 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2792 break;
2793 realign_again:
2794 buffer_slow_realign(h2c->mbuf);
2795 }
2796
2797 if (outbuf.size < 9) {
2798 h2c->flags |= H2_CF_MUX_MFULL;
2799 h2s->flags |= H2_SF_BLK_MROOM;
2800 ret = 0;
2801 goto end;
2802 }
2803
2804 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2805 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2806 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2807 outbuf.len = 9;
2808
2809 /* encode status, which necessarily is the first one */
2810 if (outbuf.len < outbuf.size && h1m->status == 200)
2811 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2812 else if (outbuf.len < outbuf.size && h1m->status == 304)
2813 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002814 else if (unlikely(list[0].v.len != 3)) {
2815 /* this is an unparsable response */
2816 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2817 ret = 0;
2818 goto end;
2819 }
2820 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002821 /* basic encoding of the status code */
2822 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2823 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2824 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2825 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2826 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2827 }
2828 else {
2829 if (buffer_space_wraps(h2c->mbuf))
2830 goto realign_again;
2831
2832 h2c->flags |= H2_CF_MUX_MFULL;
2833 h2s->flags |= H2_SF_BLK_MROOM;
2834 ret = 0;
2835 goto end;
2836 }
2837
2838 /* encode all headers, stop at empty name */
2839 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002840 /* these ones do not exist in H2 and must be dropped. */
2841 if (isteq(list[hdr].n, ist("connection")) ||
2842 isteq(list[hdr].n, ist("proxy-connection")) ||
2843 isteq(list[hdr].n, ist("keep-alive")) ||
2844 isteq(list[hdr].n, ist("upgrade")) ||
2845 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002846 continue;
2847
2848 if (isteq(list[hdr].n, ist("")))
2849 break; // end
2850
2851 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2852 /* output full */
2853 if (buffer_space_wraps(h2c->mbuf))
2854 goto realign_again;
2855
2856 h2c->flags |= H2_CF_MUX_MFULL;
2857 h2s->flags |= H2_SF_BLK_MROOM;
2858 ret = 0;
2859 goto end;
2860 }
2861 }
2862
2863 /* we may need to add END_STREAM */
2864 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2865 es_now = 1;
2866
2867 /* update the frame's size */
2868 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2869
2870 if (es_now)
2871 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2872
2873 /* consume incoming H1 response */
2874 bo_del(buf, ret);
2875
2876 /* commit the H2 response */
2877 h2c->mbuf->o += outbuf.len;
2878 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002879 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002880
2881 /* for now we don't implemented CONTINUATION, so we wait for a
2882 * body or directly end in TRL2.
2883 */
2884 if (es_now) {
2885 h1m->state = HTTP_MSG_DONE;
2886 h2s->flags |= H2_SF_ES_SENT;
2887 if (h2s->st == H2_SS_OPEN)
2888 h2s->st = H2_SS_HLOC;
2889 else
2890 h2s->st = H2_SS_CLOSED;
2891 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002892 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002893 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002894 h1m->state = HTTP_MSG_RPBEFORE;
2895 h1m->status = 0;
2896 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01002897 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01002898 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002899 else
2900 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2901
2902 end:
2903 //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));
2904 return ret;
2905}
2906
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002907/* Try to send a DATA frame matching HTTP/1 response present in the response
2908 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2909 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2910 * corresponds to the number of buffer bytes consumed.
2911 */
2912static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2913{
2914 struct h2c *h2c = h2s->h2c;
2915 struct h1m *h1m = &h2s->res;
2916 struct chunk outbuf;
2917 int ret = 0;
2918 int total = 0;
2919 int es_now = 0;
2920 int size = 0;
2921 char *blk1, *blk2;
2922 int len1, len2;
2923
2924 if (h2c_mux_busy(h2c, h2s)) {
2925 h2s->flags |= H2_SF_BLK_MBUSY;
2926 goto end;
2927 }
2928
2929 if (!h2_get_mbuf(h2c)) {
2930 h2c->flags |= H2_CF_MUX_MALLOC;
2931 h2s->flags |= H2_SF_BLK_MROOM;
2932 goto end;
2933 }
2934
2935 new_frame:
2936 if (!buf->o)
2937 goto end;
2938
2939 chunk_reset(&outbuf);
2940
2941 while (1) {
2942 outbuf.str = bo_end(h2c->mbuf);
2943 outbuf.size = bo_contig_space(h2c->mbuf);
2944 outbuf.len = 0;
2945
2946 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2947 break;
2948 realign_again:
2949 buffer_slow_realign(h2c->mbuf);
2950 }
2951
2952 if (outbuf.size < 9) {
2953 h2c->flags |= H2_CF_MUX_MFULL;
2954 h2s->flags |= H2_SF_BLK_MROOM;
2955 goto end;
2956 }
2957
2958 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2959 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2960 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2961 outbuf.len = 9;
2962
2963 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2964 case 0: /* no content length, read till SHUTW */
2965 size = buf->o;
2966 break;
2967 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2968 size = buf->o;
2969 if ((long long)size > h1m->curr_len)
2970 size = h1m->curr_len;
2971 break;
2972 default: /* te:chunked : parse chunks */
2973 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2974 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2975 if (!ret)
2976 goto end;
2977
2978 if (ret < 0) {
2979 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2980 h1m->err_pos = ret;
2981 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2982 goto end;
2983 }
2984 bo_del(buf, ret);
2985 total += ret;
2986 h1m->state = HTTP_MSG_CHUNK_SIZE;
2987 }
2988
2989 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2990 unsigned int chunk;
2991
2992 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2993 if (!ret)
2994 goto end;
2995
2996 if (ret < 0) {
2997 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2998 h1m->err_pos = ret;
2999 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3000 goto end;
3001 }
3002
3003 size = chunk;
3004 h1m->curr_len = chunk;
3005 h1m->body_len += chunk;
3006 bo_del(buf, ret);
3007 total += ret;
3008 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3009 if (!size)
3010 goto send_empty;
3011 }
3012
3013 /* in MSG_DATA state, continue below */
3014 size = h1m->curr_len;
3015 break;
3016 }
3017
3018 /* we have in <size> the exact number of bytes we need to copy from
3019 * the H1 buffer. We need to check this against the connection's and
3020 * the stream's send windows, and to ensure that this fits in the max
3021 * frame size and in the buffer's available space minus 9 bytes (for
3022 * the frame header). The connection's flow control is applied last so
3023 * that we can use a separate list of streams which are immediately
3024 * unblocked on window opening. Note: we don't implement padding.
3025 */
3026
3027 if (size > buf->o)
3028 size = buf->o;
3029
3030 if (size > h2s->mws)
3031 size = h2s->mws;
3032
3033 if (size <= 0) {
3034 h2s->flags |= H2_SF_BLK_SFCTL;
3035 goto end;
3036 }
3037
3038 if (h2c->mfs && size > h2c->mfs)
3039 size = h2c->mfs;
3040
3041 if (size + 9 > outbuf.size) {
3042 /* we have an opportunity for enlarging the too small
3043 * available space, let's try.
3044 */
3045 if (buffer_space_wraps(h2c->mbuf))
3046 goto realign_again;
3047 size = outbuf.size - 9;
3048 }
3049
3050 if (size <= 0) {
3051 h2c->flags |= H2_CF_MUX_MFULL;
3052 h2s->flags |= H2_SF_BLK_MROOM;
3053 goto end;
3054 }
3055
3056 if (size > h2c->mws)
3057 size = h2c->mws;
3058
3059 if (size <= 0) {
3060 h2s->flags |= H2_SF_BLK_MFCTL;
3061 goto end;
3062 }
3063
3064 /* copy whatever we can */
3065 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3066 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3067 if (ret == 1)
3068 len2 = 0;
3069
3070 if (!ret || len1 + len2 < size) {
3071 /* FIXME: must normally never happen */
3072 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3073 goto end;
3074 }
3075
3076 /* limit len1/len2 to size */
3077 if (len1 + len2 > size) {
3078 int sub = len1 + len2 - size;
3079
3080 if (len2 > sub)
3081 len2 -= sub;
3082 else {
3083 sub -= len2;
3084 len2 = 0;
3085 len1 -= sub;
3086 }
3087 }
3088
3089 /* now let's copy this this into the output buffer */
3090 memcpy(outbuf.str + 9, blk1, len1);
3091 if (len2)
3092 memcpy(outbuf.str + 9 + len1, blk2, len2);
3093
3094 send_empty:
3095 /* we may need to add END_STREAM */
3096 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3097 * could rely on the MSG_MORE flag as a hint for this ?
3098 */
3099 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3100 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3101 es_now = 1;
3102
3103 /* update the frame's size */
3104 h2_set_frame_size(outbuf.str, size);
3105
3106 if (es_now)
3107 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3108
3109 /* commit the H2 response */
3110 h2c->mbuf->o += size + 9;
3111 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3112
3113 /* consume incoming H1 response */
3114 if (size > 0) {
3115 bo_del(buf, size);
3116 total += size;
3117 h1m->curr_len -= size;
3118 h2s->mws -= size;
3119 h2c->mws -= size;
3120
3121 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3122 h1m->state = HTTP_MSG_CHUNK_CRLF;
3123 goto new_frame;
3124 }
3125 }
3126
3127 if (es_now) {
3128 if (h2s->st == H2_SS_OPEN)
3129 h2s->st = H2_SS_HLOC;
3130 else
3131 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003132
3133 if (!(h1m->flags & H1_MF_CHNK))
3134 h1m->state = HTTP_MSG_DONE;
3135
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003136 h2s->flags |= H2_SF_ES_SENT;
3137 }
3138
3139 end:
3140 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);
3141 return total;
3142}
3143
Willy Tarreau62f52692017-10-08 23:01:42 +02003144/* Called from the upper layer, to send data */
3145static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3146{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003147 struct h2s *h2s = cs->ctx;
3148 int total = 0;
3149
Willy Tarreauc4312d32017-11-07 12:01:53 +01003150 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3151 h2s->flags |= H2_SF_OUTGOING_DATA;
3152
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003153 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3154 if (h2s->res.state < HTTP_MSG_BODY) {
3155 total += h2s_frt_make_resp_headers(h2s, buf);
3156
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003157 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003158 break;
3159
3160 if (h2s->flags & H2_SF_BLK_ANY)
3161 break;
3162 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003163 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3164 total += h2s_frt_make_resp_data(h2s, buf);
3165
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003166 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003167 break;
3168
3169 if (h2s->flags & H2_SF_BLK_ANY)
3170 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003171 }
3172 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3173 /* consume the trailers if any (we don't forward them for now) */
3174 int count = h1_measure_trailers(buf);
3175
3176 if (unlikely(count <= 0)) {
3177 if (count < 0)
3178 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3179 break;
3180 }
3181 total += count;
3182 bo_del(buf, count);
3183 h2s->res.state = HTTP_MSG_DONE;
3184 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003185 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003186 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003187 cs->flags |= CS_FL_ERROR;
3188 break;
3189 }
3190 }
3191
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003192 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003193 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003194 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003195 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003196 h2s->st = H2_SS_CLOSED;
3197 }
3198
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003199 if (h2s->flags & H2_SF_BLK_SFCTL) {
3200 /* stream flow control, quit the list */
3201 LIST_DEL(&h2s->list);
3202 LIST_INIT(&h2s->list);
3203 }
3204
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003205 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003206}
3207
3208
3209/*******************************************************/
3210/* functions below are dedicated to the config parsers */
3211/*******************************************************/
3212
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003213/* config parser for global "tune.h2.header-table-size" */
3214static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3215 struct proxy *defpx, const char *file, int line,
3216 char **err)
3217{
3218 if (too_many_args(1, args, err, NULL))
3219 return -1;
3220
3221 h2_settings_header_table_size = atoi(args[1]);
3222 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3223 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3224 return -1;
3225 }
3226 return 0;
3227}
Willy Tarreau62f52692017-10-08 23:01:42 +02003228
Willy Tarreaue6baec02017-07-27 11:45:11 +02003229/* config parser for global "tune.h2.initial-window-size" */
3230static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3231 struct proxy *defpx, const char *file, int line,
3232 char **err)
3233{
3234 if (too_many_args(1, args, err, NULL))
3235 return -1;
3236
3237 h2_settings_initial_window_size = atoi(args[1]);
3238 if (h2_settings_initial_window_size < 0) {
3239 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3240 return -1;
3241 }
3242 return 0;
3243}
3244
Willy Tarreau5242ef82017-07-27 11:47:28 +02003245/* config parser for global "tune.h2.max-concurrent-streams" */
3246static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3247 struct proxy *defpx, const char *file, int line,
3248 char **err)
3249{
3250 if (too_many_args(1, args, err, NULL))
3251 return -1;
3252
3253 h2_settings_max_concurrent_streams = atoi(args[1]);
3254 if (h2_settings_max_concurrent_streams < 0) {
3255 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3256 return -1;
3257 }
3258 return 0;
3259}
3260
Willy Tarreau62f52692017-10-08 23:01:42 +02003261
3262/****************************************/
3263/* MUX initialization and instanciation */
3264/***************************************/
3265
3266/* The mux operations */
3267const struct mux_ops h2_ops = {
3268 .init = h2_init,
3269 .recv = h2_recv,
3270 .send = h2_send,
3271 .wake = h2_wake,
3272 .update_poll = h2_update_poll,
3273 .rcv_buf = h2_rcv_buf,
3274 .snd_buf = h2_snd_buf,
3275 .attach = h2_attach,
3276 .detach = h2_detach,
3277 .shutr = h2_shutr,
3278 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003279 .name = "H2",
3280};
3281
3282/* ALPN selection : this mux registers ALPN tolen "h2" */
3283static struct alpn_mux_list alpn_mux_h2 =
3284 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3285
3286/* config keyword parsers */
3287static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003288 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003289 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003290 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003291 { 0, NULL, NULL }
3292}};
3293
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003294static void __h2_deinit(void)
3295{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003296 pool_destroy(pool_head_h2s);
3297 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003298}
3299
Willy Tarreau62f52692017-10-08 23:01:42 +02003300__attribute__((constructor))
3301static void __h2_init(void)
3302{
3303 alpn_register_mux(&alpn_mux_h2);
3304 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003305 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003306 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3307 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003308}