blob: 57b172250879c5d3440c26317901854a75f7ca4e [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 Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010056#define H2_CF_DEM_BLOCK_ANY 0x000000F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020057
Willy Tarreau081d4722017-05-16 21:51:05 +020058/* other flags */
59#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
60#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
Olivier Houchard6fa63d92017-11-27 18:41:32 +010061#define H2_CF_WAIT_FOR_HS 0x00000400 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020062
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
Willy Tarreaua20a5192017-12-27 11:02:06 +010070 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
71 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020072 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
73 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
74 H2_CS_ENTRIES // must be last
75} __attribute__((packed));
76
77/* H2 connection descriptor */
78struct h2c {
79 struct connection *conn;
80
81 enum h2_cs st0; /* mux state */
82 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
83
84 /* 16 bit hole here */
85 uint32_t flags; /* connection flags: H2_CF_* */
86 int32_t max_id; /* highest ID known on this connection, <0 before preface */
87 uint32_t rcvd_c; /* newly received data to ACK for the connection */
88 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
89
90 /* states for the demux direction */
91 struct hpack_dht *ddht; /* demux dynamic header table */
92 struct buffer *dbuf; /* demux buffer */
93
94 int32_t dsi; /* demux stream ID (<0 = idle) */
95 int32_t dfl; /* demux frame length (if dsi >= 0) */
96 int8_t dft; /* demux frame type (if dsi >= 0) */
97 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010098 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
99 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200100 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
101
102 /* states for the mux direction */
103 struct buffer *mbuf; /* mux buffer */
104 int32_t msi; /* mux stream ID (<0 = idle) */
105 int32_t mfl; /* mux frame length (if dsi >= 0) */
106 int8_t mft; /* mux frame type (if dsi >= 0) */
107 int8_t mff; /* mux frame flags (if dsi >= 0) */
108 /* 16 bit hole here */
109 int32_t miw; /* mux initial window size for all new streams */
110 int32_t mws; /* mux window size. Can be negative. */
111 int32_t mfs; /* mux's max frame size */
112
Willy Tarreauea392822017-10-31 10:02:25 +0100113 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100114 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100115 unsigned int nb_streams; /* number of streams in the tree */
116 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100121 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200122};
123
Willy Tarreau18312642017-10-11 07:57:07 +0200124/* H2 stream state, in h2s->st */
125enum h2_ss {
126 H2_SS_IDLE = 0, // idle
127 H2_SS_RLOC, // reserved(local)
128 H2_SS_RREM, // reserved(remote)
129 H2_SS_OPEN, // open
130 H2_SS_HREM, // half-closed(remote)
131 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200132 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200133 H2_SS_CLOSED, // closed
134 H2_SS_ENTRIES // must be last
135} __attribute__((packed));
136
137/* HTTP/2 stream flags (32 bit), in h2s->flags */
138#define H2_SF_NONE 0x00000000
139#define H2_SF_ES_RCVD 0x00000001
140#define H2_SF_ES_SENT 0x00000002
141
142#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
143#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
144
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200145/* stream flags indicating the reason the stream is blocked */
146#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
147#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
148#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
149#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
150#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
151
Willy Tarreau454f9052017-10-26 19:40:35 +0200152/* stream flags indicating how data is supposed to be sent */
153#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
154#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
155
156/* step we're currently in when sending chunks. This is needed because we may
157 * have to transfer chunks as large as a full buffer so there's no room left
158 * for size nor crlf around.
159 */
160#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
161#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
162#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
163
164#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
165
Willy Tarreau67434202017-11-06 20:20:51 +0100166#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100167#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100168
Willy Tarreau18312642017-10-11 07:57:07 +0200169/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
170 * it is being processed in the internal HTTP representation (H1 for now).
171 */
172struct h2s {
173 struct conn_stream *cs;
174 struct h2c *h2c;
175 struct h1m req, res; /* request and response parser state for H1 */
176 struct eb32_node by_id; /* place in h2c's streams_by_id */
177 struct list list; /* position in active/blocked lists if blocked>0 */
178 int32_t id; /* stream ID */
179 uint32_t flags; /* H2_SF_* */
180 int mws; /* mux window size for this stream */
181 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
182 enum h2_ss st;
183};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200184
Willy Tarreauc6405142017-09-21 20:23:50 +0200185/* descriptor for an h2 frame header */
186struct h2_fh {
187 uint32_t len; /* length, host order, 24 bits */
188 uint32_t sid; /* stream id, host order, 31 bits */
189 uint8_t ft; /* frame type */
190 uint8_t ff; /* frame flags */
191};
192
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200193/* a few settings from the global section */
194static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200195static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200196static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197
Willy Tarreau2a856182017-05-16 15:20:39 +0200198/* a dmumy closed stream */
199static const struct h2s *h2_closed_stream = &(const struct h2s){
200 .cs = NULL,
201 .h2c = NULL,
202 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100203 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100204 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200205 .id = 0,
206};
207
208/* and a dummy idle stream for use with any unannounced stream */
209static const struct h2s *h2_idle_stream = &(const struct h2s){
210 .cs = NULL,
211 .h2c = NULL,
212 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100213 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200214 .id = 0,
215};
216
Olivier Houchard9f6af332018-05-25 14:04:04 +0200217static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200218
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200219/*****************************************************/
220/* functions below are for dynamic buffer management */
221/*****************************************************/
222
Willy Tarreau315d8072017-12-10 22:17:57 +0100223/* indicates whether or not the we may call the h2_recv() function to attempt
224 * to receive data into the buffer and/or demux pending data. The condition is
225 * a bit complex due to some API limits for now. The rules are the following :
226 * - if an error or a shutdown was detected on the connection and the buffer
227 * is empty, we must not attempt to receive
228 * - if the demux buf failed to be allocated, we must not try to receive and
229 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100230 * - if no flag indicates a blocking condition, we may attempt to receive,
231 * regardless of whether the demux buffer is full or not, so that only
232 * de demux part decides whether or not to block. This is needed because
233 * the connection API indeed prevents us from re-enabling receipt that is
234 * already enabled in a polled state, so we must always immediately stop
235 * as soon as the demux can't proceed so as never to hit an end of read
236 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100237 * - otherwise must may not attempt
238 */
239static inline int h2_recv_allowed(const struct h2c *h2c)
240{
241 if (h2c->dbuf->i == 0 &&
242 (h2c->st0 >= H2_CS_ERROR ||
243 h2c->conn->flags & CO_FL_ERROR ||
244 conn_xprt_read0_pending(h2c->conn)))
245 return 0;
246
247 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100248 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100249 return 1;
250
251 return 0;
252}
253
Willy Tarreau44e973f2018-03-01 17:49:30 +0100254/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
255 * flags are used to figure what buffer was requested. It returns 1 if the
256 * allocation succeeds, in which case the connection is woken up, or 0 if it's
257 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200258 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100259static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200260{
261 struct h2c *h2c = target;
262
Willy Tarreau44e973f2018-03-01 17:49:30 +0100263 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200264 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100265 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200266 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200267 return 1;
268 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200269
Willy Tarreau44e973f2018-03-01 17:49:30 +0100270 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
271 h2c->flags &= ~H2_CF_MUX_MALLOC;
272 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
273 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200274
275 if (h2c->flags & H2_CF_DEM_MROOM) {
276 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100277 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200278 conn_xprt_want_recv(h2c->conn);
279 }
Willy Tarreau14398122017-09-22 14:26:04 +0200280 return 1;
281 }
282 return 0;
283}
284
Willy Tarreau44e973f2018-03-01 17:49:30 +0100285static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200286{
287 struct buffer *buf = NULL;
288
Willy Tarreau44e973f2018-03-01 17:49:30 +0100289 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
290 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
291 h2c->buf_wait.target = h2c;
292 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100293 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100294 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100295 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200296 __conn_xprt_stop_recv(h2c->conn);
297 }
298 return buf;
299}
300
Willy Tarreau44e973f2018-03-01 17:49:30 +0100301static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200302{
Willy Tarreau44e973f2018-03-01 17:49:30 +0100303 if ((*bptr)->size) {
304 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200305 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200306 }
307}
308
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200309
Willy Tarreau62f52692017-10-08 23:01:42 +0200310/*****************************************************************/
311/* functions below are dedicated to the mux setup and management */
312/*****************************************************************/
313
Willy Tarreau32218eb2017-09-22 08:07:25 +0200314/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
315static int h2c_frt_init(struct connection *conn)
316{
317 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100318 struct task *t = NULL;
319 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200320
Willy Tarreaubafbe012017-11-24 17:34:44 +0100321 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200322 if (!h2c)
323 goto fail;
324
Willy Tarreau3f133572017-10-31 19:21:06 +0100325
Willy Tarreau599391a2017-11-24 10:16:00 +0100326 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
327 if (tick_isset(sess->fe->timeout.clientfin))
328 h2c->shut_timeout = sess->fe->timeout.clientfin;
329
Willy Tarreau33400292017-11-05 11:23:40 +0100330 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100331 if (tick_isset(h2c->timeout)) {
332 t = task_new(tid_bit);
333 if (!t)
334 goto fail;
335
336 h2c->task = t;
337 t->process = h2_timeout_task;
338 t->context = h2c;
339 t->expire = tick_add(now_ms, h2c->timeout);
340 }
Willy Tarreauea392822017-10-31 10:02:25 +0100341
Willy Tarreau32218eb2017-09-22 08:07:25 +0200342 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
343 if (!h2c->ddht)
344 goto fail;
345
346 /* Initialise the context. */
347 h2c->st0 = H2_CS_PREFACE;
348 h2c->conn = conn;
349 h2c->max_id = -1;
350 h2c->errcode = H2_ERR_NO_ERROR;
351 h2c->flags = H2_CF_NONE;
352 h2c->rcvd_c = 0;
353 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100354 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200355
356 h2c->dbuf = &buf_empty;
357 h2c->dsi = -1;
358 h2c->msi = -1;
359 h2c->last_sid = -1;
360
361 h2c->mbuf = &buf_empty;
362 h2c->miw = 65535; /* mux initial window size */
363 h2c->mws = 65535; /* mux window size */
364 h2c->mfs = 16384; /* initial max frame size */
365 h2c->streams_by_id = EB_ROOT_UNIQUE;
366 LIST_INIT(&h2c->send_list);
367 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200369 conn->mux_ctx = h2c;
370
Willy Tarreau3f133572017-10-31 19:21:06 +0100371 if (t)
372 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200373 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100374
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 /* mux->wake will be called soon to complete the operation */
376 return 0;
377 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100378 if (t)
379 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100380 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381 return -1;
382}
383
Willy Tarreau62f52692017-10-08 23:01:42 +0200384/* Initialize the mux once it's attached. For outgoing connections, the context
385 * is already initialized before installing the mux, so we detect incoming
386 * connections from the fact that the context is still NULL. Returns < 0 on
387 * error.
388 */
389static int h2_init(struct connection *conn)
390{
391 if (conn->mux_ctx) {
392 /* we don't support outgoing connections for now */
393 return -1;
394 }
395
Willy Tarreau32218eb2017-09-22 08:07:25 +0200396 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200397}
398
Willy Tarreau2373acc2017-10-12 17:35:14 +0200399/* returns the stream associated with id <id> or NULL if not found */
400static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
401{
402 struct eb32_node *node;
403
Willy Tarreau2a856182017-05-16 15:20:39 +0200404 if (id > h2c->max_id)
405 return (struct h2s *)h2_idle_stream;
406
Willy Tarreau2373acc2017-10-12 17:35:14 +0200407 node = eb32_lookup(&h2c->streams_by_id, id);
408 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200409 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200410
411 return container_of(node, struct h2s, by_id);
412}
413
Willy Tarreau62f52692017-10-08 23:01:42 +0200414/* release function for a connection. This one should be called to free all
415 * resources allocated to the mux.
416 */
417static void h2_release(struct connection *conn)
418{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 struct h2c *h2c = conn->mux_ctx;
420
421 LIST_DEL(&conn->list);
422
423 if (h2c) {
424 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200425
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100426 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100427 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200429
Willy Tarreau44e973f2018-03-01 17:49:30 +0100430 h2_release_buf(h2c, &h2c->dbuf);
431 h2_release_buf(h2c, &h2c->mbuf);
432
Willy Tarreauea392822017-10-31 10:02:25 +0100433 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200434 h2c->task->context = NULL;
435 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100436 h2c->task = NULL;
437 }
438
Willy Tarreaubafbe012017-11-24 17:34:44 +0100439 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200440 }
441
442 conn->mux = NULL;
443 conn->mux_ctx = NULL;
444
445 conn_stop_tracking(conn);
446 conn_full_close(conn);
447 if (conn->destroy_cb)
448 conn->destroy_cb(conn);
449 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200450}
451
452
Willy Tarreau71681172017-10-23 14:39:06 +0200453/******************************************************/
454/* functions below are for the H2 protocol processing */
455/******************************************************/
456
457/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100458static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200459{
460 return h2s ? h2s->id : 0;
461}
462
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200463/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100464static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200465{
466 if (h2c->msi < 0)
467 return 0;
468
469 if (h2c->msi == h2s_id(h2s))
470 return 0;
471
472 return 1;
473}
474
Willy Tarreau741d6df2017-10-17 08:00:59 +0200475/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100476static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200477{
478 h2c->errcode = err;
479 h2c->st0 = H2_CS_ERROR;
480}
481
Willy Tarreau2e43f082017-10-17 08:03:59 +0200482/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100483static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200484{
485 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
486 h2s->errcode = err;
487 h2s->st = H2_SS_ERROR;
488 if (h2s->cs)
489 h2s->cs->flags |= CS_FL_ERROR;
490 }
491}
492
Willy Tarreaue4820742017-07-27 13:37:23 +0200493/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100494static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200495{
496 uint8_t *out = frame;
497
498 *out = len >> 16;
499 write_n16(out + 1, len);
500}
501
Willy Tarreau54c15062017-10-10 17:10:03 +0200502/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
503 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
504 * the caller's responsibility to verify that there are at least <bytes> bytes
505 * available in the buffer's input prior to calling this function.
506 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100507static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200508 const struct buffer *b, int o)
509{
510 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
511}
512
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200514{
515 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
516}
517
Willy Tarreau1f094672017-11-20 21:27:45 +0100518static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200519{
520 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
521}
522
Willy Tarreau1f094672017-11-20 21:27:45 +0100523static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200524{
525 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
526}
527
528
Willy Tarreau715d5312017-07-11 15:20:24 +0200529/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
530 * is not obvious. It turns out that H2 headers are neither aligned nor do they
531 * use regular sizes. And to add to the trouble, the buffer may wrap so each
532 * byte read must be checked. The header is formed like this :
533 *
534 * b0 b1 b2 b3 b4 b5..b8
535 * +----------+---------+--------+----+----+----------------------+
536 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
537 * +----------+---------+--------+----+----+----------------------+
538 *
539 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
540 * we get the sid properly aligned and ordered, and 16 bits of len properly
541 * ordered as well. The type and flags can be extracted using bit shifts from
542 * the word, and only one extra read is needed to fetch len[16:23].
543 * Returns zero if some bytes are missing, otherwise non-zero on success.
544 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100545static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200546{
547 uint64_t w;
548
549 if (b->i < 9)
550 return 0;
551
552 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
553 h->len = *b->p << 16;
554 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
555 h->ff = w >> 32;
556 h->ft = w >> 40;
557 h->len += w >> 48;
558 return 1;
559}
560
561/* skip the next 9 bytes corresponding to the frame header possibly parsed by
562 * h2_peek_frame_hdr() above.
563 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100564static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200565{
566 bi_del(b, 9);
567}
568
569/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100570static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200571{
572 int ret;
573
574 ret = h2_peek_frame_hdr(b, h);
575 if (ret > 0)
576 h2_skip_frame_hdr(b);
577 return ret;
578}
579
Willy Tarreau00dd0782018-03-01 16:31:34 +0100580/* marks stream <h2s> as CLOSED and decrement the number of active streams for
581 * its connection if the stream was not yet closed. Please use this exclusively
582 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100583 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100584static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100585{
586 if (h2s->st != H2_SS_CLOSED)
587 h2s->h2c->nb_streams--;
588 h2s->st = H2_SS_CLOSED;
589}
590
Willy Tarreau71049cc2018-03-28 13:56:39 +0200591/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
592static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100593{
594 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200595 LIST_DEL(&h2s->list);
596 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100597 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100598 pool_free(pool_head_h2s, h2s);
599}
600
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200601/* creates a new stream <id> on the h2c connection and returns it, or NULL in
602 * case of memory allocation error.
603 */
604static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
605{
606 struct conn_stream *cs;
607 struct h2s *h2s;
608
Willy Tarreaubafbe012017-11-24 17:34:44 +0100609 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200610 if (!h2s)
611 goto out;
612
613 h2s->h2c = h2c;
614 h2s->mws = h2c->miw;
615 h2s->flags = H2_SF_NONE;
616 h2s->errcode = H2_ERR_NO_ERROR;
617 h2s->st = H2_SS_IDLE;
618 h1m_init(&h2s->req);
619 h1m_init(&h2s->res);
620 h2s->by_id.key = h2s->id = id;
621 h2c->max_id = id;
622 LIST_INIT(&h2s->list);
623
624 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100625 h2c->nb_streams++;
626 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
627 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200628
629 cs = cs_new(h2c->conn);
630 if (!cs)
631 goto out_close;
632
633 h2s->cs = cs;
634 cs->ctx = h2s;
635
636 if (stream_create_from_cs(cs) < 0)
637 goto out_free_cs;
638
639 /* OK done, the stream lives its own life now */
640 return h2s;
641
642 out_free_cs:
643 cs_free(cs);
644 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200645 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646 h2s = NULL;
647 out:
648 return h2s;
649}
650
Willy Tarreaube5b7152017-09-25 16:25:39 +0200651/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
652 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
653 * the various settings codes.
654 */
655static int h2c_snd_settings(struct h2c *h2c)
656{
657 struct buffer *res;
658 char buf_data[100]; // enough for 15 settings
659 struct chunk buf;
660 int ret;
661
662 if (h2c_mux_busy(h2c, NULL)) {
663 h2c->flags |= H2_CF_DEM_MBUSY;
664 return 0;
665 }
666
Willy Tarreau44e973f2018-03-01 17:49:30 +0100667 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200668 if (!res) {
669 h2c->flags |= H2_CF_MUX_MALLOC;
670 h2c->flags |= H2_CF_DEM_MROOM;
671 return 0;
672 }
673
674 chunk_init(&buf, buf_data, sizeof(buf_data));
675 chunk_memcpy(&buf,
676 "\x00\x00\x00" /* length : 0 for now */
677 "\x04\x00" /* type : 4 (settings), flags : 0 */
678 "\x00\x00\x00\x00", /* stream ID : 0 */
679 9);
680
681 if (h2_settings_header_table_size != 4096) {
682 char str[6] = "\x00\x01"; /* header_table_size */
683
684 write_n32(str + 2, h2_settings_header_table_size);
685 chunk_memcat(&buf, str, 6);
686 }
687
688 if (h2_settings_initial_window_size != 65535) {
689 char str[6] = "\x00\x04"; /* initial_window_size */
690
691 write_n32(str + 2, h2_settings_initial_window_size);
692 chunk_memcat(&buf, str, 6);
693 }
694
695 if (h2_settings_max_concurrent_streams != 0) {
696 char str[6] = "\x00\x03"; /* max_concurrent_streams */
697
698 /* Note: 0 means "unlimited" for haproxy's config but not for
699 * the protocol, so never send this value!
700 */
701 write_n32(str + 2, h2_settings_max_concurrent_streams);
702 chunk_memcat(&buf, str, 6);
703 }
704
705 if (global.tune.bufsize != 16384) {
706 char str[6] = "\x00\x05"; /* max_frame_size */
707
708 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
709 * match bufsize - rewrite size, but at the moment it seems
710 * that clients don't take care of it.
711 */
712 write_n32(str + 2, global.tune.bufsize);
713 chunk_memcat(&buf, str, 6);
714 }
715
716 h2_set_frame_size(buf.str, buf.len - 9);
717 ret = bo_istput(res, ist2(buf.str, buf.len));
718 if (unlikely(ret <= 0)) {
719 if (!ret) {
720 h2c->flags |= H2_CF_MUX_MFULL;
721 h2c->flags |= H2_CF_DEM_MROOM;
722 return 0;
723 }
724 else {
725 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
726 return 0;
727 }
728 }
729 return ret;
730}
731
Willy Tarreau52eed752017-09-22 15:05:09 +0200732/* Try to receive a connection preface, then upon success try to send our
733 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
734 * missing data. It may return an error in h2c.
735 */
736static int h2c_frt_recv_preface(struct h2c *h2c)
737{
738 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200739 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200740
741 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
742
743 if (unlikely(ret1 <= 0)) {
744 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
745 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
746 return 0;
747 }
748
Willy Tarreaube5b7152017-09-25 16:25:39 +0200749 ret2 = h2c_snd_settings(h2c);
750 if (ret2 > 0)
751 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200752
Willy Tarreaube5b7152017-09-25 16:25:39 +0200753 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200754}
755
Willy Tarreau081d4722017-05-16 21:51:05 +0200756/* try to send a GOAWAY frame on the connection to report an error or a graceful
757 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
758 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
759 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
760 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
761 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
762 * on unrecoverable failure. It will not attempt to send one again in this last
763 * case so that it is safe to use h2c_error() to report such errors.
764 */
765static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
766{
767 struct buffer *res;
768 char str[17];
769 int ret;
770
771 if (h2c->flags & H2_CF_GOAWAY_FAILED)
772 return 1; // claim that it worked
773
774 if (h2c_mux_busy(h2c, h2s)) {
775 if (h2s)
776 h2s->flags |= H2_SF_BLK_MBUSY;
777 else
778 h2c->flags |= H2_CF_DEM_MBUSY;
779 return 0;
780 }
781
Willy Tarreau44e973f2018-03-01 17:49:30 +0100782 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200783 if (!res) {
784 h2c->flags |= H2_CF_MUX_MALLOC;
785 if (h2s)
786 h2s->flags |= H2_SF_BLK_MROOM;
787 else
788 h2c->flags |= H2_CF_DEM_MROOM;
789 return 0;
790 }
791
792 /* len: 8, type: 7, flags: none, sid: 0 */
793 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
794
795 if (h2c->last_sid < 0)
796 h2c->last_sid = h2c->max_id;
797
798 write_n32(str + 9, h2c->last_sid);
799 write_n32(str + 13, h2c->errcode);
800 ret = bo_istput(res, ist2(str, 17));
801 if (unlikely(ret <= 0)) {
802 if (!ret) {
803 h2c->flags |= H2_CF_MUX_MFULL;
804 if (h2s)
805 h2s->flags |= H2_SF_BLK_MROOM;
806 else
807 h2c->flags |= H2_CF_DEM_MROOM;
808 return 0;
809 }
810 else {
811 /* we cannot report this error using GOAWAY, so we mark
812 * it and claim a success.
813 */
814 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
815 h2c->flags |= H2_CF_GOAWAY_FAILED;
816 return 1;
817 }
818 }
819 h2c->flags |= H2_CF_GOAWAY_SENT;
820 return ret;
821}
822
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100823/* Try to send an RST_STREAM frame on the connection for the indicated stream
824 * during mux operations. This stream must be valid and cannot be closed
825 * already. h2s->id will be used for the stream ID and h2s->errcode will be
826 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
827 * not yet.
828 *
829 * Returns > 0 on success or zero if nothing was done. In case of lack of room
830 * to write the message, it subscribes the stream to future notifications.
831 */
832static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
833{
834 struct buffer *res;
835 char str[13];
836 int ret;
837
838 if (!h2s || h2s->st == H2_SS_CLOSED)
839 return 1;
840
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100841 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
842 * RST_STREAM in response to a RST_STREAM frame.
843 */
844 if (h2c->dft == H2_FT_RST_STREAM) {
845 ret = 1;
846 goto ignore;
847 }
848
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100849 if (h2c_mux_busy(h2c, h2s)) {
850 h2s->flags |= H2_SF_BLK_MBUSY;
851 return 0;
852 }
853
Willy Tarreau44e973f2018-03-01 17:49:30 +0100854 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100855 if (!res) {
856 h2c->flags |= H2_CF_MUX_MALLOC;
857 h2s->flags |= H2_SF_BLK_MROOM;
858 return 0;
859 }
860
861 /* len: 4, type: 3, flags: none */
862 memcpy(str, "\x00\x00\x04\x03\x00", 5);
863 write_n32(str + 5, h2s->id);
864 write_n32(str + 9, h2s->errcode);
865 ret = bo_istput(res, ist2(str, 13));
866
867 if (unlikely(ret <= 0)) {
868 if (!ret) {
869 h2c->flags |= H2_CF_MUX_MFULL;
870 h2s->flags |= H2_SF_BLK_MROOM;
871 return 0;
872 }
873 else {
874 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
875 return 0;
876 }
877 }
878
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100879 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100880 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100881 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100882 return ret;
883}
884
885/* Try to send an RST_STREAM frame on the connection for the stream being
886 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
887 * error code unless the stream's state already is IDLE or CLOSED in which
888 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
889 * it was not yet.
890 *
891 * Returns > 0 on success or zero if nothing was done. In case of lack of room
892 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200893 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100894 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200895 */
896static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
897{
898 struct buffer *res;
899 char str[13];
900 int ret;
901
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100902 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
903 * RST_STREAM in response to a RST_STREAM frame.
904 */
905 if (h2c->dft == H2_FT_RST_STREAM) {
906 ret = 1;
907 goto ignore;
908 }
909
Willy Tarreau27a84c92017-10-17 08:10:17 +0200910 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100911 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200912 return 0;
913 }
914
Willy Tarreau44e973f2018-03-01 17:49:30 +0100915 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200916 if (!res) {
917 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100918 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200919 return 0;
920 }
921
922 /* len: 4, type: 3, flags: none */
923 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100924
Willy Tarreau27a84c92017-10-17 08:10:17 +0200925 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100926 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200927 h2s->errcode : H2_ERR_STREAM_CLOSED);
928 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100929
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 if (unlikely(ret <= 0)) {
931 if (!ret) {
932 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100933 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200934 return 0;
935 }
936 else {
937 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
938 return 0;
939 }
940 }
941
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100942 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200944 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100945 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100946 }
947
Willy Tarreau27a84c92017-10-17 08:10:17 +0200948 return ret;
949}
950
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100951/* try to send an empty DATA frame with the ES flag set to notify about the
952 * end of stream and match a shutdown(write). If an ES was already sent as
953 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
954 * on success or zero if nothing was done. In case of lack of room to write the
955 * message, it subscribes the requesting stream to future notifications.
956 */
957static int h2_send_empty_data_es(struct h2s *h2s)
958{
959 struct h2c *h2c = h2s->h2c;
960 struct buffer *res;
961 char str[9];
962 int ret;
963
Willy Tarreau721c9742017-11-07 11:05:42 +0100964 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100965 return 1;
966
967 if (h2c_mux_busy(h2c, h2s)) {
968 h2s->flags |= H2_SF_BLK_MBUSY;
969 return 0;
970 }
971
Willy Tarreau44e973f2018-03-01 17:49:30 +0100972 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100973 if (!res) {
974 h2c->flags |= H2_CF_MUX_MALLOC;
975 h2s->flags |= H2_SF_BLK_MROOM;
976 return 0;
977 }
978
979 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
980 memcpy(str, "\x00\x00\x00\x00\x01", 5);
981 write_n32(str + 5, h2s->id);
982 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100983 if (likely(ret > 0)) {
984 h2s->flags |= H2_SF_ES_SENT;
985 }
986 else if (!ret) {
987 h2c->flags |= H2_CF_MUX_MFULL;
988 h2s->flags |= H2_SF_BLK_MROOM;
989 return 0;
990 }
991 else {
992 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
993 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100994 }
995 return ret;
996}
997
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100998/* wake the streams attached to the connection, whose id is greater than <last>,
999 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1000 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1001 * stream's state is automatically updated accordingly.
1002 */
1003static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1004{
1005 struct eb32_node *node;
1006 struct h2s *h2s;
1007
1008 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1009 flags |= CS_FL_ERROR;
1010
1011 if (conn_xprt_read0_pending(h2c->conn))
1012 flags |= CS_FL_EOS;
1013
1014 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1015 while (node) {
1016 h2s = container_of(node, struct h2s, by_id);
1017 if (h2s->id <= last)
1018 break;
1019 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001020
1021 if (!h2s->cs) {
1022 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001023 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001024 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001025 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001026
1027 h2s->cs->flags |= flags;
1028 /* recv is used to force to detect CS_FL_EOS that wake()
1029 * doesn't handle in the stream int code.
1030 */
1031 h2s->cs->data_cb->recv(h2s->cs);
1032 h2s->cs->data_cb->wake(h2s->cs);
1033
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001034 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1035 h2s->st = H2_SS_ERROR;
1036 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1037 h2s->st = H2_SS_HREM;
1038 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001039 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001040 }
1041}
1042
Willy Tarreau3421aba2017-07-27 15:41:03 +02001043/* Increase all streams' outgoing window size by the difference passed in
1044 * argument. This is needed upon receipt of the settings frame if the initial
1045 * window size is different. The difference may be negative and the resulting
1046 * window size as well, for the time it takes to receive some window updates.
1047 */
1048static void h2c_update_all_ws(struct h2c *h2c, int diff)
1049{
1050 struct h2s *h2s;
1051 struct eb32_node *node;
1052
1053 if (!diff)
1054 return;
1055
1056 node = eb32_first(&h2c->streams_by_id);
1057 while (node) {
1058 h2s = container_of(node, struct h2s, by_id);
1059 h2s->mws += diff;
1060 node = eb32_next(node);
1061 }
1062}
1063
1064/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1065 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1066 * return an error in h2c. Described in RFC7540#6.5.
1067 */
1068static int h2c_handle_settings(struct h2c *h2c)
1069{
1070 unsigned int offset;
1071 int error;
1072
1073 if (h2c->dff & H2_F_SETTINGS_ACK) {
1074 if (h2c->dfl) {
1075 error = H2_ERR_FRAME_SIZE_ERROR;
1076 goto fail;
1077 }
1078 return 1;
1079 }
1080
1081 if (h2c->dsi != 0) {
1082 error = H2_ERR_PROTOCOL_ERROR;
1083 goto fail;
1084 }
1085
1086 if (h2c->dfl % 6) {
1087 error = H2_ERR_FRAME_SIZE_ERROR;
1088 goto fail;
1089 }
1090
1091 /* that's the limit we can process */
1092 if (h2c->dfl > global.tune.bufsize) {
1093 error = H2_ERR_FRAME_SIZE_ERROR;
1094 goto fail;
1095 }
1096
1097 /* process full frame only */
1098 if (h2c->dbuf->i < h2c->dfl)
1099 return 0;
1100
1101 /* parse the frame */
1102 for (offset = 0; offset < h2c->dfl; offset += 6) {
1103 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1104 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1105
1106 switch (type) {
1107 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1108 /* we need to update all existing streams with the
1109 * difference from the previous iws.
1110 */
1111 if (arg < 0) { // RFC7540#6.5.2
1112 error = H2_ERR_FLOW_CONTROL_ERROR;
1113 goto fail;
1114 }
1115 h2c_update_all_ws(h2c, arg - h2c->miw);
1116 h2c->miw = arg;
1117 break;
1118 case H2_SETTINGS_MAX_FRAME_SIZE:
1119 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1120 error = H2_ERR_PROTOCOL_ERROR;
1121 goto fail;
1122 }
1123 h2c->mfs = arg;
1124 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001125 case H2_SETTINGS_ENABLE_PUSH:
1126 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1127 error = H2_ERR_PROTOCOL_ERROR;
1128 goto fail;
1129 }
1130 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001131 }
1132 }
1133
1134 /* need to ACK this frame now */
1135 h2c->st0 = H2_CS_FRAME_A;
1136 return 1;
1137 fail:
1138 h2c_error(h2c, error);
1139 return 0;
1140}
1141
1142/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1143 * success or one of the h2_status values.
1144 */
1145static int h2c_ack_settings(struct h2c *h2c)
1146{
1147 struct buffer *res;
1148 char str[9];
1149 int ret = -1;
1150
1151 if (h2c_mux_busy(h2c, NULL)) {
1152 h2c->flags |= H2_CF_DEM_MBUSY;
1153 return 0;
1154 }
1155
Willy Tarreau44e973f2018-03-01 17:49:30 +01001156 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001157 if (!res) {
1158 h2c->flags |= H2_CF_MUX_MALLOC;
1159 h2c->flags |= H2_CF_DEM_MROOM;
1160 return 0;
1161 }
1162
1163 memcpy(str,
1164 "\x00\x00\x00" /* length : 0 (no data) */
1165 "\x04" "\x01" /* type : 4, flags : ACK */
1166 "\x00\x00\x00\x00" /* stream ID */, 9);
1167
1168 ret = bo_istput(res, ist2(str, 9));
1169 if (unlikely(ret <= 0)) {
1170 if (!ret) {
1171 h2c->flags |= H2_CF_MUX_MFULL;
1172 h2c->flags |= H2_CF_DEM_MROOM;
1173 return 0;
1174 }
1175 else {
1176 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1177 return 0;
1178 }
1179 }
1180 return ret;
1181}
1182
Willy Tarreaucf68c782017-10-10 17:11:41 +02001183/* processes a PING frame and schedules an ACK if needed. The caller must pass
1184 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1185 * missing data. It may return an error in h2c.
1186 */
1187static int h2c_handle_ping(struct h2c *h2c)
1188{
1189 /* frame length must be exactly 8 */
1190 if (h2c->dfl != 8) {
1191 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1192 return 0;
1193 }
1194
1195 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001196 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001197 h2c->st0 = H2_CS_FRAME_A;
1198 return 1;
1199}
1200
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001201/* Try to send a window update for stream id <sid> and value <increment>.
1202 * Returns > 0 on success or zero on missing room or failure. It may return an
1203 * error in h2c.
1204 */
1205static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1206{
1207 struct buffer *res;
1208 char str[13];
1209 int ret = -1;
1210
1211 if (h2c_mux_busy(h2c, NULL)) {
1212 h2c->flags |= H2_CF_DEM_MBUSY;
1213 return 0;
1214 }
1215
Willy Tarreau44e973f2018-03-01 17:49:30 +01001216 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001217 if (!res) {
1218 h2c->flags |= H2_CF_MUX_MALLOC;
1219 h2c->flags |= H2_CF_DEM_MROOM;
1220 return 0;
1221 }
1222
1223 /* length: 4, type: 8, flags: none */
1224 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1225 write_n32(str + 5, sid);
1226 write_n32(str + 9, increment);
1227
1228 ret = bo_istput(res, ist2(str, 13));
1229
1230 if (unlikely(ret <= 0)) {
1231 if (!ret) {
1232 h2c->flags |= H2_CF_MUX_MFULL;
1233 h2c->flags |= H2_CF_DEM_MROOM;
1234 return 0;
1235 }
1236 else {
1237 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1238 return 0;
1239 }
1240 }
1241 return ret;
1242}
1243
1244/* try to send pending window update for the connection. It's safe to call it
1245 * with no pending updates. Returns > 0 on success or zero on missing room or
1246 * failure. It may return an error in h2c.
1247 */
1248static int h2c_send_conn_wu(struct h2c *h2c)
1249{
1250 int ret = 1;
1251
1252 if (h2c->rcvd_c <= 0)
1253 return 1;
1254
1255 /* send WU for the connection */
1256 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1257 if (ret > 0)
1258 h2c->rcvd_c = 0;
1259
1260 return ret;
1261}
1262
1263/* try to send pending window update for the current dmux stream. It's safe to
1264 * call it with no pending updates. Returns > 0 on success or zero on missing
1265 * room or failure. It may return an error in h2c.
1266 */
1267static int h2c_send_strm_wu(struct h2c *h2c)
1268{
1269 int ret = 1;
1270
1271 if (h2c->rcvd_s <= 0)
1272 return 1;
1273
1274 /* send WU for the stream */
1275 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1276 if (ret > 0)
1277 h2c->rcvd_s = 0;
1278
1279 return ret;
1280}
1281
Willy Tarreaucf68c782017-10-10 17:11:41 +02001282/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1283 * success, 0 on missing data or one of the h2_status values.
1284 */
1285static int h2c_ack_ping(struct h2c *h2c)
1286{
1287 struct buffer *res;
1288 char str[17];
1289 int ret = -1;
1290
1291 if (h2c->dbuf->i < 8)
1292 return 0;
1293
1294 if (h2c_mux_busy(h2c, NULL)) {
1295 h2c->flags |= H2_CF_DEM_MBUSY;
1296 return 0;
1297 }
1298
Willy Tarreau44e973f2018-03-01 17:49:30 +01001299 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001300 if (!res) {
1301 h2c->flags |= H2_CF_MUX_MALLOC;
1302 h2c->flags |= H2_CF_DEM_MROOM;
1303 return 0;
1304 }
1305
1306 memcpy(str,
1307 "\x00\x00\x08" /* length : 8 (same payload) */
1308 "\x06" "\x01" /* type : 6, flags : ACK */
1309 "\x00\x00\x00\x00" /* stream ID */, 9);
1310
1311 /* copy the original payload */
1312 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1313
1314 ret = bo_istput(res, ist2(str, 17));
1315 if (unlikely(ret <= 0)) {
1316 if (!ret) {
1317 h2c->flags |= H2_CF_MUX_MFULL;
1318 h2c->flags |= H2_CF_DEM_MROOM;
1319 return 0;
1320 }
1321 else {
1322 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1323 return 0;
1324 }
1325 }
1326 return ret;
1327}
1328
Willy Tarreau26f95952017-07-27 17:18:30 +02001329/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1330 * Returns > 0 on success or zero on missing data. It may return an error in
1331 * h2c or h2s. Described in RFC7540#6.9.
1332 */
1333static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1334{
1335 int32_t inc;
1336 int error;
1337
1338 if (h2c->dfl != 4) {
1339 error = H2_ERR_FRAME_SIZE_ERROR;
1340 goto conn_err;
1341 }
1342
1343 /* process full frame only */
1344 if (h2c->dbuf->i < h2c->dfl)
1345 return 0;
1346
1347 inc = h2_get_n32(h2c->dbuf, 0);
1348
1349 if (h2c->dsi != 0) {
1350 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001351
1352 /* it's not an error to receive WU on a closed stream */
1353 if (h2s->st == H2_SS_CLOSED)
1354 return 1;
1355
1356 if (!inc) {
1357 error = H2_ERR_PROTOCOL_ERROR;
1358 goto strm_err;
1359 }
1360
1361 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1362 error = H2_ERR_FLOW_CONTROL_ERROR;
1363 goto strm_err;
1364 }
1365
1366 h2s->mws += inc;
1367 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1368 h2s->flags &= ~H2_SF_BLK_SFCTL;
1369 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1370 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1371 /* This stream wanted to send but could not due to its
1372 * own flow control. We can put it back into the send
1373 * list now, it will be handled upon next send() call.
1374 */
1375 LIST_ADDQ(&h2c->send_list, &h2s->list);
1376 }
1377 }
1378 }
1379 else {
1380 /* connection window update */
1381 if (!inc) {
1382 error = H2_ERR_PROTOCOL_ERROR;
1383 goto conn_err;
1384 }
1385
1386 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1387 error = H2_ERR_FLOW_CONTROL_ERROR;
1388 goto conn_err;
1389 }
1390
1391 h2c->mws += inc;
1392 }
1393
1394 return 1;
1395
1396 conn_err:
1397 h2c_error(h2c, error);
1398 return 0;
1399
1400 strm_err:
1401 if (h2s) {
1402 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001403 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001404 }
1405 else
1406 h2c_error(h2c, error);
1407 return 0;
1408}
1409
Willy Tarreaue96b0922017-10-30 00:28:29 +01001410/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1411 * the last ID. Returns > 0 on success or zero on missing data. It may return
1412 * an error in h2c. Described in RFC7540#6.8.
1413 */
1414static int h2c_handle_goaway(struct h2c *h2c)
1415{
1416 int error;
1417 int last;
1418
1419 if (h2c->dsi != 0) {
1420 error = H2_ERR_PROTOCOL_ERROR;
1421 goto conn_err;
1422 }
1423
1424 if (h2c->dfl < 8) {
1425 error = H2_ERR_FRAME_SIZE_ERROR;
1426 goto conn_err;
1427 }
1428
1429 /* process full frame only */
1430 if (h2c->dbuf->i < h2c->dfl)
1431 return 0;
1432
1433 last = h2_get_n32(h2c->dbuf, 0);
1434 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1435 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001436 if (h2c->last_sid < 0)
1437 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001438 return 1;
1439
1440 conn_err:
1441 h2c_error(h2c, error);
1442 return 0;
1443}
1444
Willy Tarreau92153fc2017-12-03 19:46:19 +01001445/* processes a PRIORITY frame, and either skips it or rejects if it is
1446 * invalid. Returns > 0 on success or zero on missing data. It may return
1447 * an error in h2c. Described in RFC7540#6.3.
1448 */
1449static int h2c_handle_priority(struct h2c *h2c)
1450{
1451 int error;
1452
1453 if (h2c->dsi == 0) {
1454 error = H2_ERR_PROTOCOL_ERROR;
1455 goto conn_err;
1456 }
1457
1458 if (h2c->dfl != 5) {
1459 error = H2_ERR_FRAME_SIZE_ERROR;
1460 goto conn_err;
1461 }
1462
1463 /* process full frame only */
1464 if (h2c->dbuf->i < h2c->dfl)
1465 return 0;
1466
1467 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1468 /* 7540#5.3 : can't depend on itself */
1469 error = H2_ERR_PROTOCOL_ERROR;
1470 goto conn_err;
1471 }
1472 return 1;
1473
1474 conn_err:
1475 h2c_error(h2c, error);
1476 return 0;
1477}
1478
Willy Tarreaucd234e92017-08-18 10:59:39 +02001479/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1480 * Returns > 0 on success or zero on missing data. It may return an error in
1481 * h2c. Described in RFC7540#6.4.
1482 */
1483static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1484{
1485 int error;
1486
1487 if (h2c->dsi == 0) {
1488 error = H2_ERR_PROTOCOL_ERROR;
1489 goto conn_err;
1490 }
1491
Willy Tarreaucd234e92017-08-18 10:59:39 +02001492 if (h2c->dfl != 4) {
1493 error = H2_ERR_FRAME_SIZE_ERROR;
1494 goto conn_err;
1495 }
1496
1497 /* process full frame only */
1498 if (h2c->dbuf->i < h2c->dfl)
1499 return 0;
1500
1501 /* late RST, already handled */
1502 if (h2s->st == H2_SS_CLOSED)
1503 return 1;
1504
1505 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001506 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001507
1508 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001509 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001510 /* recv is used to force to detect CS_FL_EOS that wake()
1511 * doesn't handle in the stream-int code.
1512 */
1513 h2s->cs->data_cb->recv(h2s->cs);
1514 h2s->cs->data_cb->wake(h2s->cs);
1515 }
1516
1517 h2s->flags |= H2_SF_RST_RCVD;
1518 return 1;
1519
1520 conn_err:
1521 h2c_error(h2c, error);
1522 return 0;
1523}
1524
Willy Tarreau13278b42017-10-13 19:23:14 +02001525/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1526 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1527 * errors here are reported as connection errors since it's impossible to
1528 * recover from such errors after the compression context has been altered.
1529 */
1530static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1531{
1532 int error;
1533
1534 if (!h2c->dfl) {
1535 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1536 goto strm_err;
1537 }
1538
1539 if (!h2c->dbuf->size)
1540 return 0; // empty buffer
1541
1542 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1543 return 0; // incomplete frame
1544
1545 /* now either the frame is complete or the buffer is complete */
1546 if (h2s->st != H2_SS_IDLE) {
1547 /* FIXME: stream already exists, this is only allowed for
1548 * trailers (not supported for now).
1549 */
1550 error = H2_ERR_PROTOCOL_ERROR;
1551 goto conn_err;
1552 }
1553 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1554 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1555 error = H2_ERR_PROTOCOL_ERROR;
1556 goto conn_err;
1557 }
1558
1559 h2s = h2c_stream_new(h2c, h2c->dsi);
1560 if (!h2s) {
1561 error = H2_ERR_INTERNAL_ERROR;
1562 goto conn_err;
1563 }
1564
1565 h2s->st = H2_SS_OPEN;
1566 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1567 h2s->st = H2_SS_HREM;
1568 h2s->flags |= H2_SF_ES_RCVD;
1569 }
1570
1571 /* call the upper layers to process the frame, then let the upper layer
1572 * notify the stream about any change.
1573 */
1574 h2s->cs->data_cb->recv(h2s->cs);
1575
1576 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1577 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1578 error = H2_ERR_INTERNAL_ERROR;
1579 goto conn_err;
1580 }
1581
Willy Tarreau8f650c32017-11-21 19:36:21 +01001582 if (h2c->st0 >= H2_CS_ERROR)
1583 return 0;
1584
Willy Tarreau721c9742017-11-07 11:05:42 +01001585 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001586 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001587 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001588 }
1589 else {
1590 /* update the max stream ID if the request is being processed */
1591 if (h2s->id > h2c->max_id)
1592 h2c->max_id = h2s->id;
1593 }
1594
1595 return 1;
1596
1597 conn_err:
1598 h2c_error(h2c, error);
1599 return 0;
1600
1601 strm_err:
1602 if (h2s) {
1603 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001604 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001605 }
1606 else
1607 h2c_error(h2c, error);
1608 return 0;
1609}
1610
Willy Tarreau454f9052017-10-26 19:40:35 +02001611/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1612 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1613 */
1614static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1615{
1616 int error;
1617
1618 /* note that empty DATA frames are perfectly valid and sometimes used
1619 * to signal an end of stream (with the ES flag).
1620 */
1621
1622 if (!h2c->dbuf->size && h2c->dfl)
1623 return 0; // empty buffer
1624
1625 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1626 return 0; // incomplete frame
1627
1628 /* now either the frame is complete or the buffer is complete */
1629
1630 if (!h2c->dsi) {
1631 /* RFC7540#6.1 */
1632 error = H2_ERR_PROTOCOL_ERROR;
1633 goto conn_err;
1634 }
1635
1636 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1637 /* RFC7540#6.1 */
1638 error = H2_ERR_STREAM_CLOSED;
1639 goto strm_err;
1640 }
1641
Willy Tarreau454f9052017-10-26 19:40:35 +02001642 /* call the upper layers to process the frame, then let the upper layer
1643 * notify the stream about any change.
1644 */
1645 if (!h2s->cs) {
1646 error = H2_ERR_STREAM_CLOSED;
1647 goto strm_err;
1648 }
1649
1650 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001651
Willy Tarreau454f9052017-10-26 19:40:35 +02001652 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1653 /* cs has just been destroyed, we have to kill h2s. */
1654 error = H2_ERR_STREAM_CLOSED;
1655 goto strm_err;
1656 }
1657
Willy Tarreau8f650c32017-11-21 19:36:21 +01001658 if (h2c->st0 >= H2_CS_ERROR)
1659 return 0;
1660
Willy Tarreau721c9742017-11-07 11:05:42 +01001661 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001662 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001663 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001664 }
1665
1666 /* check for completion : the callee will change this to FRAME_A or
1667 * FRAME_H once done.
1668 */
1669 if (h2c->st0 == H2_CS_FRAME_P)
1670 return 0;
1671
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001672
1673 /* last frame */
1674 if (h2c->dff & H2_F_DATA_END_STREAM) {
1675 h2s->st = H2_SS_HREM;
1676 h2s->flags |= H2_SF_ES_RCVD;
1677 }
1678
Willy Tarreau454f9052017-10-26 19:40:35 +02001679 return 1;
1680
1681 conn_err:
1682 h2c_error(h2c, error);
1683 return 0;
1684
1685 strm_err:
1686 if (h2s) {
1687 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001688 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001689 }
1690 else
1691 h2c_error(h2c, error);
1692 return 0;
1693}
1694
Willy Tarreaubc933932017-10-09 16:21:43 +02001695/* process Rx frames to be demultiplexed */
1696static void h2_process_demux(struct h2c *h2c)
1697{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001698 struct h2s *h2s;
1699
Willy Tarreau081d4722017-05-16 21:51:05 +02001700 if (h2c->st0 >= H2_CS_ERROR)
1701 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001702
1703 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1704 if (h2c->st0 == H2_CS_PREFACE) {
1705 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1706 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1707 if (h2c->st0 == H2_CS_ERROR)
1708 h2c->st0 = H2_CS_ERROR2;
1709 goto fail;
1710 }
1711
1712 h2c->max_id = 0;
1713 h2c->st0 = H2_CS_SETTINGS1;
1714 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001715
1716 if (h2c->st0 == H2_CS_SETTINGS1) {
1717 struct h2_fh hdr;
1718
1719 /* ensure that what is pending is a valid SETTINGS frame
1720 * without an ACK.
1721 */
1722 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1723 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1724 if (h2c->st0 == H2_CS_ERROR)
1725 h2c->st0 = H2_CS_ERROR2;
1726 goto fail;
1727 }
1728
1729 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1730 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1731 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1732 h2c->st0 = H2_CS_ERROR2;
1733 goto fail;
1734 }
1735
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001736 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001737 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1738 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1739 h2c->st0 = H2_CS_ERROR2;
1740 goto fail;
1741 }
1742
1743 /* that's OK, switch to FRAME_P to process it */
1744 h2c->dfl = hdr.len;
1745 h2c->dsi = hdr.sid;
1746 h2c->dft = hdr.ft;
1747 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001748 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001749 h2c->st0 = H2_CS_FRAME_P;
1750 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001751 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001752
1753 /* process as many incoming frames as possible below */
1754 while (h2c->dbuf->i) {
1755 int ret = 0;
1756
1757 if (h2c->st0 >= H2_CS_ERROR)
1758 break;
1759
1760 if (h2c->st0 == H2_CS_FRAME_H) {
1761 struct h2_fh hdr;
1762
1763 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1764 break;
1765
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001766 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001767 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1768 h2c->st0 = H2_CS_ERROR;
1769 break;
1770 }
1771
1772 h2c->dfl = hdr.len;
1773 h2c->dsi = hdr.sid;
1774 h2c->dft = hdr.ft;
1775 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001776 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001777 h2c->st0 = H2_CS_FRAME_P;
1778 h2_skip_frame_hdr(h2c->dbuf);
1779 }
1780
1781 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001782 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001783
Willy Tarreaud7901432017-12-29 11:34:40 +01001784 if (h2c->st0 == H2_CS_FRAME_E)
1785 goto strm_err;
1786
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001787 if (h2s->st == H2_SS_IDLE &&
1788 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1789 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1790 * this state MUST be treated as a connection error
1791 */
1792 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1793 h2c->st0 = H2_CS_ERROR;
1794 break;
1795 }
1796
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001797 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1798 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1799 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1800 * this state MUST be treated as a stream error
1801 */
1802 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001803 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001804 goto strm_err;
1805 }
1806
Willy Tarreauab837502017-12-27 15:07:30 +01001807 /* Below the management of frames received in closed state is a
1808 * bit hackish because the spec makes strong differences between
1809 * streams closed by receiving RST, sending RST, and seeing ES
1810 * in both directions. In addition to this, the creation of a
1811 * new stream reusing the identifier of a closed one will be
1812 * detected here. Given that we cannot keep track of all closed
1813 * streams forever, we consider that unknown closed streams were
1814 * closed on RST received, which allows us to respond with an
1815 * RST without breaking the connection (eg: to abort a transfer).
1816 * Some frames have to be silently ignored as well.
1817 */
1818 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1819 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1820 /* #5.1.1: The identifier of a newly
1821 * established stream MUST be numerically
1822 * greater than all streams that the initiating
1823 * endpoint has opened or reserved. This
1824 * governs streams that are opened using a
1825 * HEADERS frame and streams that are reserved
1826 * using PUSH_PROMISE. An endpoint that
1827 * receives an unexpected stream identifier
1828 * MUST respond with a connection error.
1829 */
1830 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1831 goto strm_err;
1832 }
1833
1834 if (h2s->flags & H2_SF_RST_RCVD) {
1835 /* RFC7540#5.1:closed: an endpoint that
1836 * receives any frame other than PRIORITY after
1837 * receiving a RST_STREAM MUST treat that as a
1838 * stream error of type STREAM_CLOSED.
1839 *
1840 * Note that old streams fall into this category
1841 * and will lead to an RST being sent.
1842 */
1843 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1844 h2c->st0 = H2_CS_FRAME_E;
1845 goto strm_err;
1846 }
1847
1848 /* RFC7540#5.1:closed: if this state is reached as a
1849 * result of sending a RST_STREAM frame, the peer that
1850 * receives the RST_STREAM might have already sent
1851 * frames on the stream that cannot be withdrawn. An
1852 * endpoint MUST ignore frames that it receives on
1853 * closed streams after it has sent a RST_STREAM
1854 * frame. An endpoint MAY choose to limit the period
1855 * over which it ignores frames and treat frames that
1856 * arrive after this time as being in error.
1857 */
1858 if (!(h2s->flags & H2_SF_RST_SENT)) {
1859 /* RFC7540#5.1:closed: any frame other than
1860 * PRIO/WU/RST in this state MUST be treated as
1861 * a connection error
1862 */
1863 if (h2c->dft != H2_FT_RST_STREAM &&
1864 h2c->dft != H2_FT_PRIORITY &&
1865 h2c->dft != H2_FT_WINDOW_UPDATE) {
1866 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1867 goto strm_err;
1868 }
1869 }
1870 }
1871
Willy Tarreauc0da1962017-10-30 18:38:00 +01001872#if 0
1873 // problem below: it is not possible to completely ignore such
1874 // streams as we need to maintain the compression state as well
1875 // and for this we need to completely process these frames (eg:
1876 // HEADERS frames) as well as counting DATA frames to emit
1877 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1878 // This is a typical case of layer violation where the
1879 // transported contents are critical to the connection's
1880 // validity and must be ignored at the same time :-(
1881
1882 /* graceful shutdown, ignore streams whose ID is higher than
1883 * the one advertised in GOAWAY. RFC7540#6.8.
1884 */
1885 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1886 ret = MIN(h2c->dbuf->i, h2c->dfl);
1887 bi_del(h2c->dbuf, ret);
1888 h2c->dfl -= ret;
1889 ret = h2c->dfl == 0;
1890 goto strm_err;
1891 }
1892#endif
1893
Willy Tarreau7e98c052017-10-10 15:56:59 +02001894 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001895 case H2_FT_SETTINGS:
1896 if (h2c->st0 == H2_CS_FRAME_P)
1897 ret = h2c_handle_settings(h2c);
1898
1899 if (h2c->st0 == H2_CS_FRAME_A)
1900 ret = h2c_ack_settings(h2c);
1901 break;
1902
Willy Tarreaucf68c782017-10-10 17:11:41 +02001903 case H2_FT_PING:
1904 if (h2c->st0 == H2_CS_FRAME_P)
1905 ret = h2c_handle_ping(h2c);
1906
1907 if (h2c->st0 == H2_CS_FRAME_A)
1908 ret = h2c_ack_ping(h2c);
1909 break;
1910
Willy Tarreau26f95952017-07-27 17:18:30 +02001911 case H2_FT_WINDOW_UPDATE:
1912 if (h2c->st0 == H2_CS_FRAME_P)
1913 ret = h2c_handle_window_update(h2c, h2s);
1914 break;
1915
Willy Tarreau61290ec2017-10-17 08:19:21 +02001916 case H2_FT_CONTINUATION:
1917 /* we currently don't support CONTINUATION frames since
1918 * we have nowhere to store the partial HEADERS frame.
1919 * Let's abort the stream on an INTERNAL_ERROR here.
1920 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001921 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001922 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001923 h2c->st0 = H2_CS_FRAME_E;
1924 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001925 break;
1926
Willy Tarreau13278b42017-10-13 19:23:14 +02001927 case H2_FT_HEADERS:
1928 if (h2c->st0 == H2_CS_FRAME_P)
1929 ret = h2c_frt_handle_headers(h2c, h2s);
1930 break;
1931
Willy Tarreau454f9052017-10-26 19:40:35 +02001932 case H2_FT_DATA:
1933 if (h2c->st0 == H2_CS_FRAME_P)
1934 ret = h2c_frt_handle_data(h2c, h2s);
1935
1936 if (h2c->st0 == H2_CS_FRAME_A)
1937 ret = h2c_send_strm_wu(h2c);
1938 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001939
Willy Tarreau92153fc2017-12-03 19:46:19 +01001940 case H2_FT_PRIORITY:
1941 if (h2c->st0 == H2_CS_FRAME_P)
1942 ret = h2c_handle_priority(h2c);
1943 break;
1944
Willy Tarreaucd234e92017-08-18 10:59:39 +02001945 case H2_FT_RST_STREAM:
1946 if (h2c->st0 == H2_CS_FRAME_P)
1947 ret = h2c_handle_rst_stream(h2c, h2s);
1948 break;
1949
Willy Tarreaue96b0922017-10-30 00:28:29 +01001950 case H2_FT_GOAWAY:
1951 if (h2c->st0 == H2_CS_FRAME_P)
1952 ret = h2c_handle_goaway(h2c);
1953 break;
1954
Willy Tarreau1c661982017-10-30 13:52:01 +01001955 case H2_FT_PUSH_PROMISE:
1956 /* not permitted here, RFC7540#5.1 */
1957 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001958 break;
1959
1960 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001961 default:
1962 /* drop frames that we ignore. They may be larger than
1963 * the buffer so we drain all of their contents until
1964 * we reach the end.
1965 */
1966 ret = MIN(h2c->dbuf->i, h2c->dfl);
1967 bi_del(h2c->dbuf, ret);
1968 h2c->dfl -= ret;
1969 ret = h2c->dfl == 0;
1970 }
1971
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001972 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001973 /* We may have to send an RST if not done yet */
1974 if (h2s->st == H2_SS_ERROR)
1975 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001976
Willy Tarreaua20a5192017-12-27 11:02:06 +01001977 if (h2c->st0 == H2_CS_FRAME_E)
1978 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001979
Willy Tarreau7e98c052017-10-10 15:56:59 +02001980 /* error or missing data condition met above ? */
1981 if (ret <= 0)
1982 break;
1983
1984 if (h2c->st0 != H2_CS_FRAME_H) {
1985 bi_del(h2c->dbuf, h2c->dfl);
1986 h2c->st0 = H2_CS_FRAME_H;
1987 }
1988 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001989
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001990 if (h2c->rcvd_c > 0 &&
1991 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1992 h2c_send_conn_wu(h2c);
1993
Willy Tarreau52eed752017-09-22 15:05:09 +02001994 fail:
1995 /* we can go here on missing data, blocked response or error */
1996 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001997}
1998
1999/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2000 * the end.
2001 */
2002static int h2_process_mux(struct h2c *h2c)
2003{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002004 struct h2s *h2s, *h2s_back;
2005
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002006 /* start by sending possibly pending window updates */
2007 if (h2c->rcvd_c > 0 &&
2008 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2009 h2c_send_conn_wu(h2c) < 0)
2010 goto fail;
2011
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002012 /* First we always process the flow control list because the streams
2013 * waiting there were already elected for immediate emission but were
2014 * blocked just on this.
2015 */
2016
2017 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2018 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2019 h2c->st0 >= H2_CS_ERROR)
2020 break;
2021
2022 /* In theory it's possible that h2s->cs == NULL here :
2023 * - client sends crap that causes a parse error
2024 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2025 * - RST_STREAM cannot be emitted because mux is busy/full
2026 * - stream gets notified, detaches and quits
2027 * - mux buffer gets ready and wakes pending streams up
2028 * - bam!
2029 */
2030 h2s->flags &= ~H2_SF_BLK_ANY;
2031
2032 if (h2s->cs) {
2033 h2s->cs->data_cb->send(h2s->cs);
2034 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002035 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002036 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002037 }
2038
2039 /* depending on callee's blocking reasons, we may queue in send
2040 * list or completely dequeue.
2041 */
2042 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2043 if (h2s->flags & H2_SF_BLK_ANY) {
2044 LIST_DEL(&h2s->list);
2045 LIST_ADDQ(&h2c->send_list, &h2s->list);
2046 }
2047 else {
2048 LIST_DEL(&h2s->list);
2049 LIST_INIT(&h2s->list);
2050 if (h2s->cs)
2051 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002052 else {
2053 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002054 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002055 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002056 }
2057 }
2058 }
2059
2060 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2061 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2062 break;
2063
2064 /* In theory it's possible that h2s->cs == NULL here :
2065 * - client sends crap that causes a parse error
2066 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2067 * - RST_STREAM cannot be emitted because mux is busy/full
2068 * - stream gets notified, detaches and quits
2069 * - mux buffer gets ready and wakes pending streams up
2070 * - bam!
2071 */
2072 h2s->flags &= ~H2_SF_BLK_ANY;
2073
2074 if (h2s->cs) {
2075 h2s->cs->data_cb->send(h2s->cs);
2076 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002077 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002078 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002079 }
2080 /* depending on callee's blocking reasons, we may queue in fctl
2081 * list or completely dequeue.
2082 */
2083 if (h2s->flags & H2_SF_BLK_MFCTL) {
2084 /* stream hit the connection's flow control */
2085 LIST_DEL(&h2s->list);
2086 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2087 }
2088 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2089 LIST_DEL(&h2s->list);
2090 LIST_INIT(&h2s->list);
2091 if (h2s->cs)
2092 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002093 else {
2094 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002095 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002096 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002097 }
2098 }
2099
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002100 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002101 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002102 if (h2c->st0 == H2_CS_ERROR) {
2103 if (h2c->max_id >= 0) {
2104 h2c_send_goaway_error(h2c, NULL);
2105 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2106 return 0;
2107 }
2108
2109 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2110 }
2111 return 1;
2112 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002113 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002114}
2115
Willy Tarreau71681172017-10-23 14:39:06 +02002116
Willy Tarreau62f52692017-10-08 23:01:42 +02002117/*********************************************************/
2118/* functions below are I/O callbacks from the connection */
2119/*********************************************************/
2120
2121/* callback called on recv event by the connection handler */
2122static void h2_recv(struct connection *conn)
2123{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002124 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002125 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002126 int max;
2127
Willy Tarreau315d8072017-12-10 22:17:57 +01002128 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002129 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002130
Willy Tarreau44e973f2018-03-01 17:49:30 +01002131 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002132 if (!buf) {
2133 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002134 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002135 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002136
Willy Tarreaua2af5122017-10-09 11:56:46 +02002137 /* note: buf->o == 0 */
2138 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002139 if (max)
2140 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002141
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002142 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002143 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002144 return;
2145 }
2146
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002147 if (buf->i == buf->size)
2148 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002149 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002150}
2151
2152/* callback called on send event by the connection handler */
2153static void h2_send(struct connection *conn)
2154{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002155 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002156 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002157
2158 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002159 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002160
2161 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2162 /* a handshake was requested */
2163 return;
2164 }
2165
Willy Tarreaubc933932017-10-09 16:21:43 +02002166 /* This loop is quite simple : it tries to fill as much as it can from
2167 * pending streams into the existing buffer until it's reportedly full
2168 * or the end of send requests is reached. Then it tries to send this
2169 * buffer's contents out, marks it not full if at least one byte could
2170 * be sent, and tries again.
2171 *
2172 * The snd_buf() function normally takes a "flags" argument which may
2173 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2174 * data immediately comes and CO_SFL_STREAMER to indicate that the
2175 * connection is streaming lots of data (used to increase TLS record
2176 * size at the expense of latency). The former can be sent any time
2177 * there's a buffer full flag, as it indicates at least one stream
2178 * attempted to send and failed so there are pending data. An
2179 * alternative would be to set it as long as there's an active stream
2180 * but that would be problematic for ACKs until we have an absolute
2181 * guarantee that all waiters have at least one byte to send. The
2182 * latter should possibly not be set for now.
2183 */
2184
2185 done = 0;
2186 while (!done) {
2187 unsigned int flags = 0;
2188
2189 /* fill as much as we can into the current buffer */
2190 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2191 done = h2_process_mux(h2c);
2192
2193 if (conn->flags & CO_FL_ERROR)
2194 break;
2195
2196 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2197 flags |= CO_SFL_MSG_MORE;
2198
Willy Tarreau319994a2017-11-07 11:03:56 +01002199 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002200 break;
2201
2202 /* wrote at least one byte, the buffer is not full anymore */
2203 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2204 }
2205
Willy Tarreaua2af5122017-10-09 11:56:46 +02002206 if (conn->flags & CO_FL_SOCK_WR_SH) {
2207 /* output closed, nothing to send, clear the buffer to release it */
2208 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002209 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002210}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002211
Willy Tarreau62f52692017-10-08 23:01:42 +02002212/* callback called on any event by the connection handler.
2213 * It applies changes and returns zero, or < 0 if it wants immediate
2214 * destruction of the connection (which normally doesn not happen in h2).
2215 */
2216static int h2_wake(struct connection *conn)
2217{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002218 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002219 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002220
Willy Tarreaud13bf272017-12-14 10:34:52 +01002221 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2222 h2_process_demux(h2c);
2223
2224 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2225 h2c->dbuf->i = 0;
2226
2227 if (h2c->dbuf->i != h2c->dbuf->size)
2228 h2c->flags &= ~H2_CF_DEM_DFULL;
2229 }
2230
Willy Tarreau8ec14062017-12-30 18:08:13 +01002231 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2232 /* frontend is stopping, reload likely in progress, let's try
2233 * to announce a graceful shutdown if not yet done. We don't
2234 * care if it fails, it will be tried again later.
2235 */
2236 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2237 if (h2c->last_sid < 0)
2238 h2c->last_sid = (1U << 31) - 1;
2239 h2c_send_goaway_error(h2c, NULL);
2240 }
2241 }
2242
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002243 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002244 * If we received early data, and the handshake is done, wake
2245 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002246 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002247 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2248 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2249 struct eb32_node *node;
2250 struct h2s *h2s;
2251
2252 h2c->flags |= H2_CF_WAIT_FOR_HS;
2253 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2254
2255 while (node) {
2256 h2s = container_of(node, struct h2s, by_id);
2257 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2258 h2s->cs->data_cb->wake(h2s->cs);
2259 node = eb32_next(node);
2260 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002261 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002262
Willy Tarreau26bd7612017-10-09 16:47:04 +02002263 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002264 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2265 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2266 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002267 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002268
2269 if (eb_is_empty(&h2c->streams_by_id)) {
2270 /* no more stream, kill the connection now */
2271 h2_release(conn);
2272 return -1;
2273 }
2274 else {
2275 /* some streams still there, we need to signal them all and
2276 * wait for their departure.
2277 */
2278 __conn_xprt_stop_recv(conn);
2279 __conn_xprt_stop_send(conn);
2280 return 0;
2281 }
2282 }
2283
2284 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002285 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002286
2287 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002288 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002289 __conn_xprt_stop_recv(conn);
2290 }
2291 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002292 __conn_xprt_want_recv(conn);
2293 }
2294
2295 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002296 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2297 (h2c->st0 == H2_CS_ERROR ||
2298 h2c->mbuf->o ||
2299 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2300 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002301 __conn_xprt_want_send(conn);
2302 }
2303 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002304 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002305 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002306 }
2307
Willy Tarreau3f133572017-10-31 19:21:06 +01002308 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002309 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002310 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002311 task_queue(h2c->task);
2312 }
2313 else
2314 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002315 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002316 return 0;
2317}
2318
Willy Tarreauea392822017-10-31 10:02:25 +01002319/* Connection timeout management. The principle is that if there's no receipt
2320 * nor sending for a certain amount of time, the connection is closed. If the
2321 * MUX buffer still has lying data or is not allocatable, the connection is
2322 * immediately killed. If it's allocatable and empty, we attempt to send a
2323 * GOAWAY frame.
2324 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002325static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002326{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002327 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002328 int expired = tick_is_expired(t->expire, now_ms);
2329
Willy Tarreau0975f112018-03-29 15:22:59 +02002330 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002331 return t;
2332
Willy Tarreau0975f112018-03-29 15:22:59 +02002333 task_delete(t);
2334 task_free(t);
2335
2336 if (!h2c) {
2337 /* resources were already deleted */
2338 return NULL;
2339 }
2340
2341 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002342 h2c_error(h2c, H2_ERR_NO_ERROR);
2343 h2_wake_some_streams(h2c, 0, 0);
2344
2345 if (h2c->mbuf->o) {
2346 /* don't even try to send a GOAWAY, the buffer is stuck */
2347 h2c->flags |= H2_CF_GOAWAY_FAILED;
2348 }
2349
2350 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002351 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002352 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2353 h2c->flags |= H2_CF_GOAWAY_FAILED;
2354
2355 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2356 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2357
Willy Tarreau0975f112018-03-29 15:22:59 +02002358 /* either we can release everything now or it will be done later once
2359 * the last stream closes.
2360 */
2361 if (eb_is_empty(&h2c->streams_by_id))
2362 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002363
Willy Tarreauea392822017-10-31 10:02:25 +01002364 return NULL;
2365}
2366
2367
Willy Tarreau62f52692017-10-08 23:01:42 +02002368/*******************************************/
2369/* functions below are used by the streams */
2370/*******************************************/
2371
2372/*
2373 * Attach a new stream to a connection
2374 * (Used for outgoing connections)
2375 */
2376static struct conn_stream *h2_attach(struct connection *conn)
2377{
2378 return NULL;
2379}
2380
2381/* callback used to update the mux's polling flags after changing a cs' status.
2382 * The caller (cs_update_mux_polling) will take care of propagating any changes
2383 * to the transport layer.
2384 */
2385static void h2_update_poll(struct conn_stream *cs)
2386{
Willy Tarreau1d393222017-10-17 10:26:19 +02002387 struct h2s *h2s = cs->ctx;
2388
2389 if (!h2s)
2390 return;
2391
Willy Tarreaud7739c82017-10-30 15:38:23 +01002392 /* we may unblock a blocked read */
2393
Willy Tarreau315d8072017-12-10 22:17:57 +01002394 if (cs->flags & CS_FL_DATA_RD_ENA) {
2395 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002396 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002397 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002398 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002399 conn_xprt_want_send(cs->conn);
2400 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002401 }
2402
Willy Tarreau1d393222017-10-17 10:26:19 +02002403 /* Note: the stream and stream-int code doesn't allow us to perform a
2404 * synchronous send() here unfortunately, because this code is called
2405 * as si_update() from the process_stream() context. This means that
2406 * we have to queue the current cs and defer its processing after the
2407 * connection's cs list is processed anyway.
2408 */
2409
2410 if (cs->flags & CS_FL_DATA_WR_ENA) {
2411 if (LIST_ISEMPTY(&h2s->list)) {
2412 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2413 !h2s->h2c->mbuf->o && // not yet subscribed
2414 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2415 conn_xprt_want_send(cs->conn);
2416 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2417 }
2418 }
2419 else if (!LIST_ISEMPTY(&h2s->list)) {
2420 LIST_DEL(&h2s->list);
2421 LIST_INIT(&h2s->list);
2422 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2423 }
2424
2425 /* this can happen from within si_chk_snd() */
2426 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2427 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002428}
2429
2430/*
2431 * Detach the stream from the connection and possibly release the connection.
2432 */
2433static void h2_detach(struct conn_stream *cs)
2434{
Willy Tarreau60935142017-10-16 18:11:19 +02002435 struct h2s *h2s = cs->ctx;
2436 struct h2c *h2c;
2437
2438 cs->ctx = NULL;
2439 if (!h2s)
2440 return;
2441
2442 h2c = h2s->h2c;
2443 h2s->cs = NULL;
2444
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002445 /* this stream may be blocked waiting for some data to leave (possibly
2446 * an ES or RST frame), so orphan it in this case.
2447 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002448 if (!(cs->conn->flags & CO_FL_ERROR) &&
2449 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002450 return;
2451
Willy Tarreau45f752e2017-10-30 15:44:59 +01002452 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2453 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2454 /* unblock the connection if it was blocked on this
2455 * stream.
2456 */
2457 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2458 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2459 conn_xprt_want_recv(cs->conn);
2460 conn_xprt_want_send(cs->conn);
2461 }
2462
Willy Tarreau71049cc2018-03-28 13:56:39 +02002463 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002464
Willy Tarreaue323f342018-03-28 13:51:45 +02002465 /* We don't want to close right now unless we're removing the
2466 * last stream, and either the connection is in error, or it
2467 * reached the ID already specified in a GOAWAY frame received
2468 * or sent (as seen by last_sid >= 0).
2469 */
2470 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2471 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
2472 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2473 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2474 (conn_xprt_read0_pending(h2c->conn) ||
2475 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2476 /* no more stream will come, kill it now */
2477 h2_release(h2c->conn);
2478 }
2479 else if (h2c->task) {
2480 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2481 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2482 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002483 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002484 else
2485 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002486 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002487}
2488
2489static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2490{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002491 struct h2s *h2s = cs->ctx;
2492
2493 if (!mode)
2494 return;
2495
Willy Tarreau721c9742017-11-07 11:05:42 +01002496 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002497 return;
2498
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002499 /* if no outgoing data was seen on this stream, it means it was
2500 * closed with a "tcp-request content" rule that is normally
2501 * used to kill the connection ASAP (eg: limit abuse). In this
2502 * case we send a goaway to close the connection.
2503 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002504 if (!(h2s->flags & H2_SF_RST_SENT) &&
2505 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002506 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002507
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002508 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2509 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2510 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002511 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002512
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002513 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2514 conn_xprt_want_send(cs->conn);
2515
Willy Tarreau00dd0782018-03-01 16:31:34 +01002516 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002517
2518 add_to_list:
2519 if (LIST_ISEMPTY(&h2s->list)) {
2520 if (h2s->flags & H2_SF_BLK_MFCTL)
2521 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2522 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2523 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2524 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002525}
2526
2527static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2528{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002529 struct h2s *h2s = cs->ctx;
2530
Willy Tarreau721c9742017-11-07 11:05:42 +01002531 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002532 return;
2533
Willy Tarreau67434202017-11-06 20:20:51 +01002534 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002535 /* we can cleanly close using an empty data frame only after headers */
2536
2537 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2538 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002539 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002540
2541 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002542 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002543 else
2544 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002545 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002546 /* if no outgoing data was seen on this stream, it means it was
2547 * closed with a "tcp-request content" rule that is normally
2548 * used to kill the connection ASAP (eg: limit abuse). In this
2549 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002550 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002551 if (!(h2s->flags & H2_SF_RST_SENT) &&
2552 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002553 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002554
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002555 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2556 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002557 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002558 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002559
Willy Tarreau00dd0782018-03-01 16:31:34 +01002560 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002561 }
2562
2563 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2564 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002565
2566 add_to_list:
2567 if (LIST_ISEMPTY(&h2s->list)) {
2568 if (h2s->flags & H2_SF_BLK_MFCTL)
2569 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2570 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2571 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2572 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002573}
2574
Willy Tarreau13278b42017-10-13 19:23:14 +02002575/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2576 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2577 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002578 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002579 */
2580static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2581{
2582 struct h2c *h2c = h2s->h2c;
2583 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002584 struct chunk *tmp = get_trash_chunk();
2585 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002586 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002587 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002588 int flen = h2c->dfl;
2589 int outlen = 0;
2590 int wrap;
2591 int try;
2592
2593 if (!h2c->dfl) {
2594 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002595 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002596 return 0;
2597 }
2598
Willy Tarreau68472622017-12-11 18:36:37 +01002599 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2600 return 0; // incomplete input frame
2601
Willy Tarreau13278b42017-10-13 19:23:14 +02002602 /* if the input buffer wraps, take a temporary copy of it (rare) */
2603 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2604 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002605 copy = alloc_trash_chunk();
2606 if (!copy) {
2607 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2608 goto fail;
2609 }
2610 memcpy(copy->str, h2c->dbuf->p, wrap);
2611 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2612 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002613 }
2614
2615 /* The padlen is the first byte before data, and the padding appears
2616 * after data. padlen+data+padding are included in flen.
2617 */
2618 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002619 h2c->dpl = *hdrs;
2620 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002621 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2622 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002623 return 0;
2624 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002625 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002626 hdrs += 1; // skip Pad Length
2627 }
2628
2629 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2630 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002631 if (read_n32(hdrs) == h2s->id) {
2632 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2633 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2634 return 0;//goto fail_stream;
2635 }
2636
Willy Tarreau13278b42017-10-13 19:23:14 +02002637 hdrs += 5; // stream dep = 4, weight = 1
2638 flen -= 5;
2639 }
2640
2641 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2642 * don't support this for now and can't even decompress so we have to
2643 * break the connection.
2644 */
2645 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2646 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002647 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002648 }
2649
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002650 /* we can't retry a failed decompression operation so we must be very
2651 * careful not to take any risks. In practice the output buffer is
2652 * always empty except maybe for trailers, so these operations almost
2653 * never happen.
2654 */
2655 if (unlikely(buf->o)) {
2656 /* need to let the output buffer flush and
2657 * mark the buffer for later wake up.
2658 */
2659 goto fail;
2660 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002661
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002662 if (unlikely(buffer_space_wraps(buf))) {
2663 /* it doesn't fit and the buffer is fragmented,
2664 * so let's defragment it and try again.
2665 */
2666 buffer_slow_realign(buf);
2667 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002668
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002669 /* first check if we have some room after p+i */
2670 try = buf->data + buf->size - (buf->p + buf->i);
2671
2672 /* otherwise continue between data and p-o */
2673 if (try <= 0) {
2674 try = buf->p - (buf->data + buf->o);
2675 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002676 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002677 }
2678 if (try > count)
2679 try = count;
2680
2681 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2682 sizeof(list)/sizeof(list[0]), tmp);
2683 if (outlen < 0) {
2684 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2685 goto fail;
2686 }
2687
2688 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002689 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
2690 outlen = h2_make_h1_request(list, bi_end(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002691
2692 if (outlen < 0) {
2693 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2694 goto fail;
2695 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002696
Willy Tarreau174b06a2018-04-25 18:13:58 +02002697 if (msgf & H2_MSGF_BODY) {
2698 /* a payload is present */
2699 if (msgf & H2_MSGF_BODY_CL)
2700 h2s->flags |= H2_SF_DATA_CLEN;
2701 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2702 h2s->flags |= H2_SF_DATA_CHNK;
2703 }
2704
Willy Tarreau13278b42017-10-13 19:23:14 +02002705 /* now consume the input data */
2706 bi_del(h2c->dbuf, h2c->dfl);
2707 h2c->st0 = H2_CS_FRAME_H;
2708 buf->i += outlen;
2709
2710 /* don't send it before returning data!
2711 * FIXME: should we instead try to send it much later, after the
2712 * response ? This would require that we keep a copy of it in h2s.
2713 */
2714 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2715 h2s->cs->flags |= CS_FL_EOS;
2716 h2s->flags |= H2_SF_ES_RCVD;
2717 }
2718
Willy Tarreau68dd9852017-07-03 14:44:26 +02002719 leave:
2720 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002721 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002722 fail:
2723 outlen = 0;
2724 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002725}
2726
Willy Tarreau454f9052017-10-26 19:40:35 +02002727/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2728 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2729 * in use, a new chunk is emitted for each frame. This is supposed to fit
2730 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2731 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2732 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2733 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002734 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2735 * checked to know if some data remain pending (an empty DATA frame can return
2736 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2737 * connection errors in h2c->errcode. The caller must already have checked the
2738 * frame header and ensured that the frame was complete or the buffer full. It
2739 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002740 */
2741static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2742{
2743 struct h2c *h2c = h2s->h2c;
2744 int block1, block2;
2745 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002746 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002747
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002748 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002749 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002750
2751 /* The padlen is the first byte before data, and the padding appears
2752 * after data. padlen+data+padding are included in flen.
2753 */
Willy Tarreau79127812017-12-03 21:06:59 +01002754 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002755 if (h2c->dbuf->i < 1)
2756 return 0;
2757
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002758 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2759 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002760 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2761 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002762 return 0;
2763 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002764
2765 /* skip the padlen byte */
2766 bi_del(h2c->dbuf, 1);
2767 h2c->dfl--;
2768 h2c->rcvd_c++; h2c->rcvd_s++;
2769 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002770 }
2771
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002772 flen = h2c->dfl - h2c->dpl;
2773 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002774 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002775
2776 if (flen > h2c->dbuf->i) {
2777 flen = h2c->dbuf->i;
2778 if (!flen)
2779 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002780 }
2781
Willy Tarreaueba10f22018-04-25 20:44:22 +02002782 /* chunked-encoding requires more room */
2783 if (h2s->flags & H2_SF_DATA_CHNK) {
2784 chklen = MIN(flen, count);
2785 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2786 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2787 (chklen < 1048576) ? 4 : 8;
2788 chklen += 4; // CRLF, CRLF
2789 }
2790
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002791 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002792 if (flen + chklen > count) {
2793 if (chklen >= count)
2794 goto full;
2795 flen = count - chklen;
2796 }
2797
2798 if (h2s->flags & H2_SF_DATA_CHNK) {
2799 /* emit the chunk size */
2800 unsigned int chksz = flen;
2801 char str[10];
2802 char *beg;
2803
2804 beg = str + sizeof(str);
2805 *--beg = '\n';
2806 *--beg = '\r';
2807 do {
2808 *--beg = hextab[chksz & 0xF];
2809 } while (chksz >>= 4);
2810 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002811 }
2812
Willy Tarreau454f9052017-10-26 19:40:35 +02002813 /* Block1 is the length of the first block before the buffer wraps,
2814 * block2 is the optional second block to reach the end of the frame.
2815 */
2816 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002817 if (block1 > flen)
2818 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002819 block2 = flen - block1;
2820
2821 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002822 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002823
2824 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002825 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002826
Willy Tarreaueba10f22018-04-25 20:44:22 +02002827 if (h2s->flags & H2_SF_DATA_CHNK) {
2828 /* emit the CRLF */
2829 bi_putblk(buf, "\r\n", 2);
2830 }
2831
Willy Tarreau454f9052017-10-26 19:40:35 +02002832 /* now mark the input data as consumed (will be deleted from the buffer
2833 * by the caller when seeing FRAME_A after sending the window update).
2834 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002835 bi_del(h2c->dbuf, flen);
2836 h2c->dfl -= flen;
2837 h2c->rcvd_c += flen;
2838 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2839
2840 if (h2c->dfl > h2c->dpl) {
2841 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002842 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002843 }
2844
Willy Tarreau4a28da12018-01-04 14:41:00 +01002845 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002846 /* here we're done with the frame, all the payload (except padding) was
2847 * transferred.
2848 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002849
2850 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2851 /* emit the trailing 0 CRLF CRLF */
2852 if (count < 5)
2853 goto more;
2854 chklen += 5;
2855 bi_putblk(buf, "0\r\n\r\n", 5);
2856 }
2857
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002858 h2c->rcvd_c += h2c->dpl;
2859 h2c->rcvd_s += h2c->dpl;
2860 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002861 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2862
2863 /* don't send it before returning data!
2864 * FIXME: should we instead try to send it much later, after the
2865 * response ? This would require that we keep a copy of it in h2s.
2866 */
Willy Tarreau79127812017-12-03 21:06:59 +01002867 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002868 h2s->cs->flags |= CS_FL_EOS;
2869 h2s->flags |= H2_SF_ES_RCVD;
2870 }
2871
Willy Tarreaueba10f22018-04-25 20:44:22 +02002872 return flen + chklen;
2873 full:
2874 flen = chklen = 0;
2875 more:
2876 h2c->flags |= H2_CF_DEM_SFULL;
2877 h2s->cs->flags |= CS_FL_RCV_MORE;
2878 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002879}
2880
Willy Tarreau62f52692017-10-08 23:01:42 +02002881/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002882 * Called from the upper layer to get more data, up to <count> bytes. The
2883 * caller is responsible for never asking for more data than what is available
2884 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002885 */
2886static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2887{
Willy Tarreau13278b42017-10-13 19:23:14 +02002888 struct h2s *h2s = cs->ctx;
2889 struct h2c *h2c = h2s->h2c;
2890 int ret = 0;
2891
2892 if (h2c->st0 != H2_CS_FRAME_P)
2893 return 0; // no pre-parsed frame yet
2894
2895 if (h2c->dsi != h2s->id)
2896 return 0; // not for us
2897
2898 if (!h2c->dbuf->size)
2899 return 0; // empty buffer
2900
Willy Tarreau13278b42017-10-13 19:23:14 +02002901 switch (h2c->dft) {
2902 case H2_FT_HEADERS:
2903 ret = h2_frt_decode_headers(h2s, buf, count);
2904 break;
2905
Willy Tarreau454f9052017-10-26 19:40:35 +02002906 case H2_FT_DATA:
2907 ret = h2_frt_transfer_data(h2s, buf, count);
2908 break;
2909
Willy Tarreau13278b42017-10-13 19:23:14 +02002910 default:
2911 ret = 0;
2912 }
2913 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002914}
2915
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002916/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2917 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2918 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2919 * to the number of buffer bytes consumed.
2920 */
2921static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2922{
2923 struct http_hdr list[MAX_HTTP_HDR];
2924 struct h2c *h2c = h2s->h2c;
2925 struct h1m *h1m = &h2s->res;
2926 struct chunk outbuf;
2927 int es_now = 0;
2928 int ret = 0;
2929 int hdr;
2930
2931 if (h2c_mux_busy(h2c, h2s)) {
2932 h2s->flags |= H2_SF_BLK_MBUSY;
2933 return 0;
2934 }
2935
Willy Tarreau44e973f2018-03-01 17:49:30 +01002936 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002937 h2c->flags |= H2_CF_MUX_MALLOC;
2938 h2s->flags |= H2_SF_BLK_MROOM;
2939 return 0;
2940 }
2941
2942 /* First, try to parse the H1 response and index it into <list>.
2943 * NOTE! Since it comes from haproxy, we *know* that a response header
2944 * block does not wrap and we can safely read it this way without
2945 * having to realign the buffer.
2946 */
2947 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2948 list, sizeof(list)/sizeof(list[0]), h1m);
2949 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002950 /* incomplete or invalid response, this is abnormal coming from
2951 * haproxy and may only result in a bad errorfile or bad Lua code
2952 * so that won't be fixed, raise an error now.
2953 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002954 * FIXME: we should instead add the ability to only return a
2955 * 502 bad gateway. But in theory this is not supposed to
2956 * happen.
2957 */
2958 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2959 ret = 0;
2960 goto end;
2961 }
2962
2963 chunk_reset(&outbuf);
2964
2965 while (1) {
2966 outbuf.str = bo_end(h2c->mbuf);
2967 outbuf.size = bo_contig_space(h2c->mbuf);
2968 outbuf.len = 0;
2969
2970 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2971 break;
2972 realign_again:
2973 buffer_slow_realign(h2c->mbuf);
2974 }
2975
2976 if (outbuf.size < 9) {
2977 h2c->flags |= H2_CF_MUX_MFULL;
2978 h2s->flags |= H2_SF_BLK_MROOM;
2979 ret = 0;
2980 goto end;
2981 }
2982
2983 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2984 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2985 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2986 outbuf.len = 9;
2987
2988 /* encode status, which necessarily is the first one */
2989 if (outbuf.len < outbuf.size && h1m->status == 200)
2990 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2991 else if (outbuf.len < outbuf.size && h1m->status == 304)
2992 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002993 else if (unlikely(list[0].v.len != 3)) {
2994 /* this is an unparsable response */
2995 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2996 ret = 0;
2997 goto end;
2998 }
2999 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003000 /* basic encoding of the status code */
3001 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3002 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3003 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3004 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3005 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3006 }
3007 else {
3008 if (buffer_space_wraps(h2c->mbuf))
3009 goto realign_again;
3010
3011 h2c->flags |= H2_CF_MUX_MFULL;
3012 h2s->flags |= H2_SF_BLK_MROOM;
3013 ret = 0;
3014 goto end;
3015 }
3016
3017 /* encode all headers, stop at empty name */
3018 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003019 /* these ones do not exist in H2 and must be dropped. */
3020 if (isteq(list[hdr].n, ist("connection")) ||
3021 isteq(list[hdr].n, ist("proxy-connection")) ||
3022 isteq(list[hdr].n, ist("keep-alive")) ||
3023 isteq(list[hdr].n, ist("upgrade")) ||
3024 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003025 continue;
3026
3027 if (isteq(list[hdr].n, ist("")))
3028 break; // end
3029
3030 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3031 /* output full */
3032 if (buffer_space_wraps(h2c->mbuf))
3033 goto realign_again;
3034
3035 h2c->flags |= H2_CF_MUX_MFULL;
3036 h2s->flags |= H2_SF_BLK_MROOM;
3037 ret = 0;
3038 goto end;
3039 }
3040 }
3041
3042 /* we may need to add END_STREAM */
3043 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3044 es_now = 1;
3045
3046 /* update the frame's size */
3047 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3048
3049 if (es_now)
3050 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3051
3052 /* consume incoming H1 response */
3053 bo_del(buf, ret);
3054
3055 /* commit the H2 response */
3056 h2c->mbuf->o += outbuf.len;
3057 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003058 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003059
3060 /* for now we don't implemented CONTINUATION, so we wait for a
3061 * body or directly end in TRL2.
3062 */
3063 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003064 // trim any possibly pending data (eg: inconsistent content-length)
3065 bo_del(buf, buf->o);
3066
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003067 h1m->state = HTTP_MSG_DONE;
3068 h2s->flags |= H2_SF_ES_SENT;
3069 if (h2s->st == H2_SS_OPEN)
3070 h2s->st = H2_SS_HLOC;
3071 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003072 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003073 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003074 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003075 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003076 h1m->state = HTTP_MSG_RPBEFORE;
3077 h1m->status = 0;
3078 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003079 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003080 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003081 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003082 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003083
3084 end:
3085 //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));
3086 return ret;
3087}
3088
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003089/* Try to send a DATA frame matching HTTP/1 response present in the response
3090 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3091 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3092 * corresponds to the number of buffer bytes consumed.
3093 */
3094static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3095{
3096 struct h2c *h2c = h2s->h2c;
3097 struct h1m *h1m = &h2s->res;
3098 struct chunk outbuf;
3099 int ret = 0;
3100 int total = 0;
3101 int es_now = 0;
3102 int size = 0;
3103 char *blk1, *blk2;
3104 int len1, len2;
3105
3106 if (h2c_mux_busy(h2c, h2s)) {
3107 h2s->flags |= H2_SF_BLK_MBUSY;
3108 goto end;
3109 }
3110
Willy Tarreau44e973f2018-03-01 17:49:30 +01003111 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003112 h2c->flags |= H2_CF_MUX_MALLOC;
3113 h2s->flags |= H2_SF_BLK_MROOM;
3114 goto end;
3115 }
3116
3117 new_frame:
3118 if (!buf->o)
3119 goto end;
3120
3121 chunk_reset(&outbuf);
3122
3123 while (1) {
3124 outbuf.str = bo_end(h2c->mbuf);
3125 outbuf.size = bo_contig_space(h2c->mbuf);
3126 outbuf.len = 0;
3127
3128 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3129 break;
3130 realign_again:
3131 buffer_slow_realign(h2c->mbuf);
3132 }
3133
3134 if (outbuf.size < 9) {
3135 h2c->flags |= H2_CF_MUX_MFULL;
3136 h2s->flags |= H2_SF_BLK_MROOM;
3137 goto end;
3138 }
3139
3140 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3141 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3142 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3143 outbuf.len = 9;
3144
3145 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3146 case 0: /* no content length, read till SHUTW */
3147 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003148 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003149 break;
3150 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3151 size = buf->o;
3152 if ((long long)size > h1m->curr_len)
3153 size = h1m->curr_len;
3154 break;
3155 default: /* te:chunked : parse chunks */
3156 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3157 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3158 if (!ret)
3159 goto end;
3160
3161 if (ret < 0) {
3162 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3163 h1m->err_pos = ret;
3164 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3165 goto end;
3166 }
3167 bo_del(buf, ret);
3168 total += ret;
3169 h1m->state = HTTP_MSG_CHUNK_SIZE;
3170 }
3171
3172 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3173 unsigned int chunk;
3174
3175 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3176 if (!ret)
3177 goto end;
3178
3179 if (ret < 0) {
3180 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3181 h1m->err_pos = ret;
3182 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3183 goto end;
3184 }
3185
3186 size = chunk;
3187 h1m->curr_len = chunk;
3188 h1m->body_len += chunk;
3189 bo_del(buf, ret);
3190 total += ret;
3191 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3192 if (!size)
3193 goto send_empty;
3194 }
3195
3196 /* in MSG_DATA state, continue below */
3197 size = h1m->curr_len;
3198 break;
3199 }
3200
3201 /* we have in <size> the exact number of bytes we need to copy from
3202 * the H1 buffer. We need to check this against the connection's and
3203 * the stream's send windows, and to ensure that this fits in the max
3204 * frame size and in the buffer's available space minus 9 bytes (for
3205 * the frame header). The connection's flow control is applied last so
3206 * that we can use a separate list of streams which are immediately
3207 * unblocked on window opening. Note: we don't implement padding.
3208 */
3209
3210 if (size > buf->o)
3211 size = buf->o;
3212
3213 if (size > h2s->mws)
3214 size = h2s->mws;
3215
3216 if (size <= 0) {
3217 h2s->flags |= H2_SF_BLK_SFCTL;
3218 goto end;
3219 }
3220
3221 if (h2c->mfs && size > h2c->mfs)
3222 size = h2c->mfs;
3223
3224 if (size + 9 > outbuf.size) {
3225 /* we have an opportunity for enlarging the too small
3226 * available space, let's try.
3227 */
3228 if (buffer_space_wraps(h2c->mbuf))
3229 goto realign_again;
3230 size = outbuf.size - 9;
3231 }
3232
3233 if (size <= 0) {
3234 h2c->flags |= H2_CF_MUX_MFULL;
3235 h2s->flags |= H2_SF_BLK_MROOM;
3236 goto end;
3237 }
3238
3239 if (size > h2c->mws)
3240 size = h2c->mws;
3241
3242 if (size <= 0) {
3243 h2s->flags |= H2_SF_BLK_MFCTL;
3244 goto end;
3245 }
3246
3247 /* copy whatever we can */
3248 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3249 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3250 if (ret == 1)
3251 len2 = 0;
3252
3253 if (!ret || len1 + len2 < size) {
3254 /* FIXME: must normally never happen */
3255 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3256 goto end;
3257 }
3258
3259 /* limit len1/len2 to size */
3260 if (len1 + len2 > size) {
3261 int sub = len1 + len2 - size;
3262
3263 if (len2 > sub)
3264 len2 -= sub;
3265 else {
3266 sub -= len2;
3267 len2 = 0;
3268 len1 -= sub;
3269 }
3270 }
3271
3272 /* now let's copy this this into the output buffer */
3273 memcpy(outbuf.str + 9, blk1, len1);
3274 if (len2)
3275 memcpy(outbuf.str + 9 + len1, blk2, len2);
3276
3277 send_empty:
3278 /* we may need to add END_STREAM */
3279 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3280 * could rely on the MSG_MORE flag as a hint for this ?
3281 */
3282 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3283 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3284 es_now = 1;
3285
3286 /* update the frame's size */
3287 h2_set_frame_size(outbuf.str, size);
3288
3289 if (es_now)
3290 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3291
3292 /* commit the H2 response */
3293 h2c->mbuf->o += size + 9;
3294 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3295
3296 /* consume incoming H1 response */
3297 if (size > 0) {
3298 bo_del(buf, size);
3299 total += size;
3300 h1m->curr_len -= size;
3301 h2s->mws -= size;
3302 h2c->mws -= size;
3303
3304 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3305 h1m->state = HTTP_MSG_CHUNK_CRLF;
3306 goto new_frame;
3307 }
3308 }
3309
3310 if (es_now) {
3311 if (h2s->st == H2_SS_OPEN)
3312 h2s->st = H2_SS_HLOC;
3313 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003314 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003315
Willy Tarreau35a62702018-02-27 15:37:25 +01003316 if (!(h1m->flags & H1_MF_CHNK)) {
3317 // trim any possibly pending data (eg: inconsistent content-length)
3318 bo_del(buf, buf->o);
3319
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003320 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003321 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003322
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 h2s->flags |= H2_SF_ES_SENT;
3324 }
3325
3326 end:
3327 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);
3328 return total;
3329}
3330
Willy Tarreau62f52692017-10-08 23:01:42 +02003331/* Called from the upper layer, to send data */
3332static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3333{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003334 struct h2s *h2s = cs->ctx;
3335 int total = 0;
3336
Willy Tarreauc4312d32017-11-07 12:01:53 +01003337 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3338 h2s->flags |= H2_SF_OUTGOING_DATA;
3339
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003340 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3341 if (h2s->res.state < HTTP_MSG_BODY) {
3342 total += h2s_frt_make_resp_headers(h2s, buf);
3343
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003344 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003345 break;
3346
3347 if (h2s->flags & H2_SF_BLK_ANY)
3348 break;
3349 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003350 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3351 total += h2s_frt_make_resp_data(h2s, buf);
3352
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003353 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003354 break;
3355
3356 if (h2s->flags & H2_SF_BLK_ANY)
3357 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003358 }
3359 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3360 /* consume the trailers if any (we don't forward them for now) */
3361 int count = h1_measure_trailers(buf);
3362
3363 if (unlikely(count <= 0)) {
3364 if (count < 0)
3365 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3366 break;
3367 }
3368 total += count;
3369 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003370
3371 // trim any possibly pending data (eg: extra CR-LF, ...)
3372 bo_del(buf, buf->o);
3373
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003374 h2s->res.state = HTTP_MSG_DONE;
3375 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003376 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003377 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003378 cs->flags |= CS_FL_ERROR;
3379 break;
3380 }
3381 }
3382
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003383 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003384 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003385 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003386 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003387 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003388 }
3389
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003390 if (h2s->flags & H2_SF_BLK_SFCTL) {
3391 /* stream flow control, quit the list */
3392 LIST_DEL(&h2s->list);
3393 LIST_INIT(&h2s->list);
3394 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003395 else if (LIST_ISEMPTY(&h2s->list)) {
3396 if (h2s->flags & H2_SF_BLK_MFCTL)
3397 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3398 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3399 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3400 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003401
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003402 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003403}
3404
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003405/* for debugging with CLI's "show fd" command */
3406static void h2_show_fd(struct chunk *msg, struct connection *conn)
3407{
3408 struct h2c *h2c = conn->mux_ctx;
3409 struct h2s *h2s;
3410 struct eb32_node *node;
3411 int fctl_cnt = 0;
3412 int send_cnt = 0;
3413 int tree_cnt = 0;
3414 int orph_cnt = 0;
3415
3416 if (!h2c)
3417 return;
3418
3419 list_for_each_entry(h2s, &h2c->fctl_list, list)
3420 fctl_cnt++;
3421
3422 list_for_each_entry(h2s, &h2c->send_list, list)
3423 send_cnt++;
3424
3425 node = eb32_first(&h2c->streams_by_id);
3426 while (node) {
3427 h2s = container_of(node, struct h2s, by_id);
3428 tree_cnt++;
3429 if (!h2s->cs)
3430 orph_cnt++;
3431 node = eb32_next(node);
3432 }
3433
3434 chunk_appendf(msg, " st0=%d flg=0x%08x fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d",
3435 h2c->st0, h2c->flags, fctl_cnt, send_cnt, tree_cnt, orph_cnt);
3436}
Willy Tarreau62f52692017-10-08 23:01:42 +02003437
3438/*******************************************************/
3439/* functions below are dedicated to the config parsers */
3440/*******************************************************/
3441
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003442/* config parser for global "tune.h2.header-table-size" */
3443static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3444 struct proxy *defpx, const char *file, int line,
3445 char **err)
3446{
3447 if (too_many_args(1, args, err, NULL))
3448 return -1;
3449
3450 h2_settings_header_table_size = atoi(args[1]);
3451 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3452 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3453 return -1;
3454 }
3455 return 0;
3456}
Willy Tarreau62f52692017-10-08 23:01:42 +02003457
Willy Tarreaue6baec02017-07-27 11:45:11 +02003458/* config parser for global "tune.h2.initial-window-size" */
3459static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3460 struct proxy *defpx, const char *file, int line,
3461 char **err)
3462{
3463 if (too_many_args(1, args, err, NULL))
3464 return -1;
3465
3466 h2_settings_initial_window_size = atoi(args[1]);
3467 if (h2_settings_initial_window_size < 0) {
3468 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3469 return -1;
3470 }
3471 return 0;
3472}
3473
Willy Tarreau5242ef82017-07-27 11:47:28 +02003474/* config parser for global "tune.h2.max-concurrent-streams" */
3475static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3476 struct proxy *defpx, const char *file, int line,
3477 char **err)
3478{
3479 if (too_many_args(1, args, err, NULL))
3480 return -1;
3481
3482 h2_settings_max_concurrent_streams = atoi(args[1]);
3483 if (h2_settings_max_concurrent_streams < 0) {
3484 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3485 return -1;
3486 }
3487 return 0;
3488}
3489
Willy Tarreau62f52692017-10-08 23:01:42 +02003490
3491/****************************************/
3492/* MUX initialization and instanciation */
3493/***************************************/
3494
3495/* The mux operations */
3496const struct mux_ops h2_ops = {
3497 .init = h2_init,
3498 .recv = h2_recv,
3499 .send = h2_send,
3500 .wake = h2_wake,
3501 .update_poll = h2_update_poll,
3502 .rcv_buf = h2_rcv_buf,
3503 .snd_buf = h2_snd_buf,
3504 .attach = h2_attach,
3505 .detach = h2_detach,
3506 .shutr = h2_shutr,
3507 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003508 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003509 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003510 .name = "H2",
3511};
3512
3513/* ALPN selection : this mux registers ALPN tolen "h2" */
3514static struct alpn_mux_list alpn_mux_h2 =
3515 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3516
3517/* config keyword parsers */
3518static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003519 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003520 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003521 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003522 { 0, NULL, NULL }
3523}};
3524
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003525static void __h2_deinit(void)
3526{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003527 pool_destroy(pool_head_h2s);
3528 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003529}
3530
Willy Tarreau62f52692017-10-08 23:01:42 +02003531__attribute__((constructor))
3532static void __h2_init(void)
3533{
3534 alpn_register_mux(&alpn_mux_h2);
3535 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003536 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003537 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3538 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003539}