blob: 819a70a23bc5a7e3c9027689a448e73ac295b2a2 [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 */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200116 unsigned int nb_cs; /* number of attached conn_streams */
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 Tarreau7ac60e82018-07-19 09:04:05 +0200355 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200356
357 h2c->dbuf = &buf_empty;
358 h2c->dsi = -1;
359 h2c->msi = -1;
360 h2c->last_sid = -1;
361
362 h2c->mbuf = &buf_empty;
363 h2c->miw = 65535; /* mux initial window size */
364 h2c->mws = 65535; /* mux window size */
365 h2c->mfs = 16384; /* initial max frame size */
366 h2c->streams_by_id = EB_ROOT_UNIQUE;
367 LIST_INIT(&h2c->send_list);
368 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100369 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200370 conn->mux_ctx = h2c;
371
Willy Tarreau3f133572017-10-31 19:21:06 +0100372 if (t)
373 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100375
Willy Tarreau32218eb2017-09-22 08:07:25 +0200376 /* mux->wake will be called soon to complete the operation */
377 return 0;
378 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100379 if (t)
380 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100381 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 return -1;
383}
384
Willy Tarreau62f52692017-10-08 23:01:42 +0200385/* Initialize the mux once it's attached. For outgoing connections, the context
386 * is already initialized before installing the mux, so we detect incoming
387 * connections from the fact that the context is still NULL. Returns < 0 on
388 * error.
389 */
390static int h2_init(struct connection *conn)
391{
392 if (conn->mux_ctx) {
393 /* we don't support outgoing connections for now */
394 return -1;
395 }
396
Willy Tarreau32218eb2017-09-22 08:07:25 +0200397 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200398}
399
Willy Tarreau2373acc2017-10-12 17:35:14 +0200400/* returns the stream associated with id <id> or NULL if not found */
401static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
402{
403 struct eb32_node *node;
404
Willy Tarreau2a856182017-05-16 15:20:39 +0200405 if (id > h2c->max_id)
406 return (struct h2s *)h2_idle_stream;
407
Willy Tarreau2373acc2017-10-12 17:35:14 +0200408 node = eb32_lookup(&h2c->streams_by_id, id);
409 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200410 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200411
412 return container_of(node, struct h2s, by_id);
413}
414
Willy Tarreau62f52692017-10-08 23:01:42 +0200415/* release function for a connection. This one should be called to free all
416 * resources allocated to the mux.
417 */
418static void h2_release(struct connection *conn)
419{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 struct h2c *h2c = conn->mux_ctx;
421
422 LIST_DEL(&conn->list);
423
424 if (h2c) {
425 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200426
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100427 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100428 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100429 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200430
Willy Tarreau44e973f2018-03-01 17:49:30 +0100431 h2_release_buf(h2c, &h2c->dbuf);
432 h2_release_buf(h2c, &h2c->mbuf);
433
Willy Tarreauea392822017-10-31 10:02:25 +0100434 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200435 h2c->task->context = NULL;
436 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100437 h2c->task = NULL;
438 }
439
Willy Tarreaubafbe012017-11-24 17:34:44 +0100440 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441 }
442
443 conn->mux = NULL;
444 conn->mux_ctx = NULL;
445
446 conn_stop_tracking(conn);
447 conn_full_close(conn);
448 if (conn->destroy_cb)
449 conn->destroy_cb(conn);
450 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200451}
452
453
Willy Tarreau71681172017-10-23 14:39:06 +0200454/******************************************************/
455/* functions below are for the H2 protocol processing */
456/******************************************************/
457
458/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100459static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200460{
461 return h2s ? h2s->id : 0;
462}
463
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200464/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100465static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200466{
467 if (h2c->msi < 0)
468 return 0;
469
470 if (h2c->msi == h2s_id(h2s))
471 return 0;
472
473 return 1;
474}
475
Willy Tarreau741d6df2017-10-17 08:00:59 +0200476/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100477static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200478{
479 h2c->errcode = err;
480 h2c->st0 = H2_CS_ERROR;
481}
482
Willy Tarreau2e43f082017-10-17 08:03:59 +0200483/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100484static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200485{
486 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
487 h2s->errcode = err;
488 h2s->st = H2_SS_ERROR;
489 if (h2s->cs)
490 h2s->cs->flags |= CS_FL_ERROR;
491 }
492}
493
Willy Tarreaue4820742017-07-27 13:37:23 +0200494/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100495static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200496{
497 uint8_t *out = frame;
498
499 *out = len >> 16;
500 write_n16(out + 1, len);
501}
502
Willy Tarreau54c15062017-10-10 17:10:03 +0200503/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
504 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
505 * the caller's responsibility to verify that there are at least <bytes> bytes
506 * available in the buffer's input prior to calling this function.
507 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100508static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200509 const struct buffer *b, int o)
510{
511 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
512}
513
Willy Tarreau1f094672017-11-20 21:27:45 +0100514static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200515{
516 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
517}
518
Willy Tarreau1f094672017-11-20 21:27:45 +0100519static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200520{
521 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
522}
523
Willy Tarreau1f094672017-11-20 21:27:45 +0100524static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200525{
526 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
527}
528
529
Willy Tarreau715d5312017-07-11 15:20:24 +0200530/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
531 * is not obvious. It turns out that H2 headers are neither aligned nor do they
532 * use regular sizes. And to add to the trouble, the buffer may wrap so each
533 * byte read must be checked. The header is formed like this :
534 *
535 * b0 b1 b2 b3 b4 b5..b8
536 * +----------+---------+--------+----+----+----------------------+
537 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
538 * +----------+---------+--------+----+----+----------------------+
539 *
540 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
541 * we get the sid properly aligned and ordered, and 16 bits of len properly
542 * ordered as well. The type and flags can be extracted using bit shifts from
543 * the word, and only one extra read is needed to fetch len[16:23].
544 * Returns zero if some bytes are missing, otherwise non-zero on success.
545 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100546static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200547{
548 uint64_t w;
549
550 if (b->i < 9)
551 return 0;
552
553 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
554 h->len = *b->p << 16;
555 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
556 h->ff = w >> 32;
557 h->ft = w >> 40;
558 h->len += w >> 48;
559 return 1;
560}
561
562/* skip the next 9 bytes corresponding to the frame header possibly parsed by
563 * h2_peek_frame_hdr() above.
564 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100565static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200566{
567 bi_del(b, 9);
568}
569
570/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100571static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200572{
573 int ret;
574
575 ret = h2_peek_frame_hdr(b, h);
576 if (ret > 0)
577 h2_skip_frame_hdr(b);
578 return ret;
579}
580
Willy Tarreau00dd0782018-03-01 16:31:34 +0100581/* marks stream <h2s> as CLOSED and decrement the number of active streams for
582 * its connection if the stream was not yet closed. Please use this exclusively
583 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100584 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100585static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100586{
587 if (h2s->st != H2_SS_CLOSED)
588 h2s->h2c->nb_streams--;
589 h2s->st = H2_SS_CLOSED;
590}
591
Willy Tarreau71049cc2018-03-28 13:56:39 +0200592/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
593static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100594{
595 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200596 LIST_DEL(&h2s->list);
597 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100598 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100599 pool_free(pool_head_h2s, h2s);
600}
601
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200602/* creates a new stream <id> on the h2c connection and returns it, or NULL in
603 * case of memory allocation error.
604 */
605static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
606{
607 struct conn_stream *cs;
608 struct h2s *h2s;
609
Willy Tarreaubafbe012017-11-24 17:34:44 +0100610 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200611 if (!h2s)
612 goto out;
613
614 h2s->h2c = h2c;
615 h2s->mws = h2c->miw;
616 h2s->flags = H2_SF_NONE;
617 h2s->errcode = H2_ERR_NO_ERROR;
618 h2s->st = H2_SS_IDLE;
619 h1m_init(&h2s->req);
620 h1m_init(&h2s->res);
621 h2s->by_id.key = h2s->id = id;
622 h2c->max_id = id;
623 LIST_INIT(&h2s->list);
624
625 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100626 h2c->nb_streams++;
627 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
628 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200629
630 cs = cs_new(h2c->conn);
631 if (!cs)
632 goto out_close;
633
634 h2s->cs = cs;
635 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200636 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200637
638 if (stream_create_from_cs(cs) < 0)
639 goto out_free_cs;
640
641 /* OK done, the stream lives its own life now */
642 return h2s;
643
644 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200645 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646 cs_free(cs);
647 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200648 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200649 h2s = NULL;
650 out:
651 return h2s;
652}
653
Willy Tarreaube5b7152017-09-25 16:25:39 +0200654/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
655 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
656 * the various settings codes.
657 */
658static int h2c_snd_settings(struct h2c *h2c)
659{
660 struct buffer *res;
661 char buf_data[100]; // enough for 15 settings
662 struct chunk buf;
663 int ret;
664
665 if (h2c_mux_busy(h2c, NULL)) {
666 h2c->flags |= H2_CF_DEM_MBUSY;
667 return 0;
668 }
669
Willy Tarreau44e973f2018-03-01 17:49:30 +0100670 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200671 if (!res) {
672 h2c->flags |= H2_CF_MUX_MALLOC;
673 h2c->flags |= H2_CF_DEM_MROOM;
674 return 0;
675 }
676
677 chunk_init(&buf, buf_data, sizeof(buf_data));
678 chunk_memcpy(&buf,
679 "\x00\x00\x00" /* length : 0 for now */
680 "\x04\x00" /* type : 4 (settings), flags : 0 */
681 "\x00\x00\x00\x00", /* stream ID : 0 */
682 9);
683
684 if (h2_settings_header_table_size != 4096) {
685 char str[6] = "\x00\x01"; /* header_table_size */
686
687 write_n32(str + 2, h2_settings_header_table_size);
688 chunk_memcat(&buf, str, 6);
689 }
690
691 if (h2_settings_initial_window_size != 65535) {
692 char str[6] = "\x00\x04"; /* initial_window_size */
693
694 write_n32(str + 2, h2_settings_initial_window_size);
695 chunk_memcat(&buf, str, 6);
696 }
697
698 if (h2_settings_max_concurrent_streams != 0) {
699 char str[6] = "\x00\x03"; /* max_concurrent_streams */
700
701 /* Note: 0 means "unlimited" for haproxy's config but not for
702 * the protocol, so never send this value!
703 */
704 write_n32(str + 2, h2_settings_max_concurrent_streams);
705 chunk_memcat(&buf, str, 6);
706 }
707
708 if (global.tune.bufsize != 16384) {
709 char str[6] = "\x00\x05"; /* max_frame_size */
710
711 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
712 * match bufsize - rewrite size, but at the moment it seems
713 * that clients don't take care of it.
714 */
715 write_n32(str + 2, global.tune.bufsize);
716 chunk_memcat(&buf, str, 6);
717 }
718
719 h2_set_frame_size(buf.str, buf.len - 9);
720 ret = bo_istput(res, ist2(buf.str, buf.len));
721 if (unlikely(ret <= 0)) {
722 if (!ret) {
723 h2c->flags |= H2_CF_MUX_MFULL;
724 h2c->flags |= H2_CF_DEM_MROOM;
725 return 0;
726 }
727 else {
728 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
729 return 0;
730 }
731 }
732 return ret;
733}
734
Willy Tarreau52eed752017-09-22 15:05:09 +0200735/* Try to receive a connection preface, then upon success try to send our
736 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
737 * missing data. It may return an error in h2c.
738 */
739static int h2c_frt_recv_preface(struct h2c *h2c)
740{
741 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200742 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200743
744 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
745
746 if (unlikely(ret1 <= 0)) {
747 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
748 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
749 return 0;
750 }
751
Willy Tarreaube5b7152017-09-25 16:25:39 +0200752 ret2 = h2c_snd_settings(h2c);
753 if (ret2 > 0)
754 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200755
Willy Tarreaube5b7152017-09-25 16:25:39 +0200756 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200757}
758
Willy Tarreau081d4722017-05-16 21:51:05 +0200759/* try to send a GOAWAY frame on the connection to report an error or a graceful
760 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
761 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
762 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
763 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
764 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
765 * on unrecoverable failure. It will not attempt to send one again in this last
766 * case so that it is safe to use h2c_error() to report such errors.
767 */
768static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
769{
770 struct buffer *res;
771 char str[17];
772 int ret;
773
774 if (h2c->flags & H2_CF_GOAWAY_FAILED)
775 return 1; // claim that it worked
776
777 if (h2c_mux_busy(h2c, h2s)) {
778 if (h2s)
779 h2s->flags |= H2_SF_BLK_MBUSY;
780 else
781 h2c->flags |= H2_CF_DEM_MBUSY;
782 return 0;
783 }
784
Willy Tarreau44e973f2018-03-01 17:49:30 +0100785 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200786 if (!res) {
787 h2c->flags |= H2_CF_MUX_MALLOC;
788 if (h2s)
789 h2s->flags |= H2_SF_BLK_MROOM;
790 else
791 h2c->flags |= H2_CF_DEM_MROOM;
792 return 0;
793 }
794
795 /* len: 8, type: 7, flags: none, sid: 0 */
796 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
797
798 if (h2c->last_sid < 0)
799 h2c->last_sid = h2c->max_id;
800
801 write_n32(str + 9, h2c->last_sid);
802 write_n32(str + 13, h2c->errcode);
803 ret = bo_istput(res, ist2(str, 17));
804 if (unlikely(ret <= 0)) {
805 if (!ret) {
806 h2c->flags |= H2_CF_MUX_MFULL;
807 if (h2s)
808 h2s->flags |= H2_SF_BLK_MROOM;
809 else
810 h2c->flags |= H2_CF_DEM_MROOM;
811 return 0;
812 }
813 else {
814 /* we cannot report this error using GOAWAY, so we mark
815 * it and claim a success.
816 */
817 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
818 h2c->flags |= H2_CF_GOAWAY_FAILED;
819 return 1;
820 }
821 }
822 h2c->flags |= H2_CF_GOAWAY_SENT;
823 return ret;
824}
825
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100826/* Try to send an RST_STREAM frame on the connection for the indicated stream
827 * during mux operations. This stream must be valid and cannot be closed
828 * already. h2s->id will be used for the stream ID and h2s->errcode will be
829 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
830 * not yet.
831 *
832 * Returns > 0 on success or zero if nothing was done. In case of lack of room
833 * to write the message, it subscribes the stream to future notifications.
834 */
835static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
836{
837 struct buffer *res;
838 char str[13];
839 int ret;
840
841 if (!h2s || h2s->st == H2_SS_CLOSED)
842 return 1;
843
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100844 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
845 * RST_STREAM in response to a RST_STREAM frame.
846 */
847 if (h2c->dft == H2_FT_RST_STREAM) {
848 ret = 1;
849 goto ignore;
850 }
851
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100852 if (h2c_mux_busy(h2c, h2s)) {
853 h2s->flags |= H2_SF_BLK_MBUSY;
854 return 0;
855 }
856
Willy Tarreau44e973f2018-03-01 17:49:30 +0100857 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100858 if (!res) {
859 h2c->flags |= H2_CF_MUX_MALLOC;
860 h2s->flags |= H2_SF_BLK_MROOM;
861 return 0;
862 }
863
864 /* len: 4, type: 3, flags: none */
865 memcpy(str, "\x00\x00\x04\x03\x00", 5);
866 write_n32(str + 5, h2s->id);
867 write_n32(str + 9, h2s->errcode);
868 ret = bo_istput(res, ist2(str, 13));
869
870 if (unlikely(ret <= 0)) {
871 if (!ret) {
872 h2c->flags |= H2_CF_MUX_MFULL;
873 h2s->flags |= H2_SF_BLK_MROOM;
874 return 0;
875 }
876 else {
877 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
878 return 0;
879 }
880 }
881
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100882 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100883 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100884 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100885 return ret;
886}
887
888/* Try to send an RST_STREAM frame on the connection for the stream being
889 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
890 * error code unless the stream's state already is IDLE or CLOSED in which
891 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
892 * it was not yet.
893 *
894 * Returns > 0 on success or zero if nothing was done. In case of lack of room
895 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200896 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200898 */
899static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
900{
901 struct buffer *res;
902 char str[13];
903 int ret;
904
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100905 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
906 * RST_STREAM in response to a RST_STREAM frame.
907 */
908 if (h2c->dft == H2_FT_RST_STREAM) {
909 ret = 1;
910 goto ignore;
911 }
912
Willy Tarreau27a84c92017-10-17 08:10:17 +0200913 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100914 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200915 return 0;
916 }
917
Willy Tarreau44e973f2018-03-01 17:49:30 +0100918 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200919 if (!res) {
920 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100921 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 return 0;
923 }
924
925 /* len: 4, type: 3, flags: none */
926 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100927
Willy Tarreau27a84c92017-10-17 08:10:17 +0200928 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100929 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 h2s->errcode : H2_ERR_STREAM_CLOSED);
931 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100932
Willy Tarreau27a84c92017-10-17 08:10:17 +0200933 if (unlikely(ret <= 0)) {
934 if (!ret) {
935 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100936 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200937 return 0;
938 }
939 else {
940 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
941 return 0;
942 }
943 }
944
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100945 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100946 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200947 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100948 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100949 }
950
Willy Tarreau27a84c92017-10-17 08:10:17 +0200951 return ret;
952}
953
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100954/* try to send an empty DATA frame with the ES flag set to notify about the
955 * end of stream and match a shutdown(write). If an ES was already sent as
956 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
957 * on success or zero if nothing was done. In case of lack of room to write the
958 * message, it subscribes the requesting stream to future notifications.
959 */
960static int h2_send_empty_data_es(struct h2s *h2s)
961{
962 struct h2c *h2c = h2s->h2c;
963 struct buffer *res;
964 char str[9];
965 int ret;
966
Willy Tarreau721c9742017-11-07 11:05:42 +0100967 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100968 return 1;
969
970 if (h2c_mux_busy(h2c, h2s)) {
971 h2s->flags |= H2_SF_BLK_MBUSY;
972 return 0;
973 }
974
Willy Tarreau44e973f2018-03-01 17:49:30 +0100975 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100976 if (!res) {
977 h2c->flags |= H2_CF_MUX_MALLOC;
978 h2s->flags |= H2_SF_BLK_MROOM;
979 return 0;
980 }
981
982 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
983 memcpy(str, "\x00\x00\x00\x00\x01", 5);
984 write_n32(str + 5, h2s->id);
985 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100986 if (likely(ret > 0)) {
987 h2s->flags |= H2_SF_ES_SENT;
988 }
989 else if (!ret) {
990 h2c->flags |= H2_CF_MUX_MFULL;
991 h2s->flags |= H2_SF_BLK_MROOM;
992 return 0;
993 }
994 else {
995 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
996 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100997 }
998 return ret;
999}
1000
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001001/* wake the streams attached to the connection, whose id is greater than <last>,
1002 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1003 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1004 * stream's state is automatically updated accordingly.
1005 */
1006static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1007{
1008 struct eb32_node *node;
1009 struct h2s *h2s;
1010
1011 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1012 flags |= CS_FL_ERROR;
1013
1014 if (conn_xprt_read0_pending(h2c->conn))
1015 flags |= CS_FL_EOS;
1016
1017 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1018 while (node) {
1019 h2s = container_of(node, struct h2s, by_id);
1020 if (h2s->id <= last)
1021 break;
1022 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001023
1024 if (!h2s->cs) {
1025 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001026 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001027 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001028 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001029
1030 h2s->cs->flags |= flags;
1031 /* recv is used to force to detect CS_FL_EOS that wake()
1032 * doesn't handle in the stream int code.
1033 */
1034 h2s->cs->data_cb->recv(h2s->cs);
1035 h2s->cs->data_cb->wake(h2s->cs);
1036
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001037 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1038 h2s->st = H2_SS_ERROR;
1039 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1040 h2s->st = H2_SS_HREM;
1041 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001042 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001043 }
1044}
1045
Willy Tarreau3421aba2017-07-27 15:41:03 +02001046/* Increase all streams' outgoing window size by the difference passed in
1047 * argument. This is needed upon receipt of the settings frame if the initial
1048 * window size is different. The difference may be negative and the resulting
1049 * window size as well, for the time it takes to receive some window updates.
1050 */
1051static void h2c_update_all_ws(struct h2c *h2c, int diff)
1052{
1053 struct h2s *h2s;
1054 struct eb32_node *node;
1055
1056 if (!diff)
1057 return;
1058
1059 node = eb32_first(&h2c->streams_by_id);
1060 while (node) {
1061 h2s = container_of(node, struct h2s, by_id);
1062 h2s->mws += diff;
1063 node = eb32_next(node);
1064 }
1065}
1066
1067/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1068 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1069 * return an error in h2c. Described in RFC7540#6.5.
1070 */
1071static int h2c_handle_settings(struct h2c *h2c)
1072{
1073 unsigned int offset;
1074 int error;
1075
1076 if (h2c->dff & H2_F_SETTINGS_ACK) {
1077 if (h2c->dfl) {
1078 error = H2_ERR_FRAME_SIZE_ERROR;
1079 goto fail;
1080 }
1081 return 1;
1082 }
1083
1084 if (h2c->dsi != 0) {
1085 error = H2_ERR_PROTOCOL_ERROR;
1086 goto fail;
1087 }
1088
1089 if (h2c->dfl % 6) {
1090 error = H2_ERR_FRAME_SIZE_ERROR;
1091 goto fail;
1092 }
1093
1094 /* that's the limit we can process */
1095 if (h2c->dfl > global.tune.bufsize) {
1096 error = H2_ERR_FRAME_SIZE_ERROR;
1097 goto fail;
1098 }
1099
1100 /* process full frame only */
1101 if (h2c->dbuf->i < h2c->dfl)
1102 return 0;
1103
1104 /* parse the frame */
1105 for (offset = 0; offset < h2c->dfl; offset += 6) {
1106 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1107 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1108
1109 switch (type) {
1110 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1111 /* we need to update all existing streams with the
1112 * difference from the previous iws.
1113 */
1114 if (arg < 0) { // RFC7540#6.5.2
1115 error = H2_ERR_FLOW_CONTROL_ERROR;
1116 goto fail;
1117 }
1118 h2c_update_all_ws(h2c, arg - h2c->miw);
1119 h2c->miw = arg;
1120 break;
1121 case H2_SETTINGS_MAX_FRAME_SIZE:
1122 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1123 error = H2_ERR_PROTOCOL_ERROR;
1124 goto fail;
1125 }
1126 h2c->mfs = arg;
1127 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001128 case H2_SETTINGS_ENABLE_PUSH:
1129 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1130 error = H2_ERR_PROTOCOL_ERROR;
1131 goto fail;
1132 }
1133 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001134 }
1135 }
1136
1137 /* need to ACK this frame now */
1138 h2c->st0 = H2_CS_FRAME_A;
1139 return 1;
1140 fail:
1141 h2c_error(h2c, error);
1142 return 0;
1143}
1144
1145/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1146 * success or one of the h2_status values.
1147 */
1148static int h2c_ack_settings(struct h2c *h2c)
1149{
1150 struct buffer *res;
1151 char str[9];
1152 int ret = -1;
1153
1154 if (h2c_mux_busy(h2c, NULL)) {
1155 h2c->flags |= H2_CF_DEM_MBUSY;
1156 return 0;
1157 }
1158
Willy Tarreau44e973f2018-03-01 17:49:30 +01001159 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001160 if (!res) {
1161 h2c->flags |= H2_CF_MUX_MALLOC;
1162 h2c->flags |= H2_CF_DEM_MROOM;
1163 return 0;
1164 }
1165
1166 memcpy(str,
1167 "\x00\x00\x00" /* length : 0 (no data) */
1168 "\x04" "\x01" /* type : 4, flags : ACK */
1169 "\x00\x00\x00\x00" /* stream ID */, 9);
1170
1171 ret = bo_istput(res, ist2(str, 9));
1172 if (unlikely(ret <= 0)) {
1173 if (!ret) {
1174 h2c->flags |= H2_CF_MUX_MFULL;
1175 h2c->flags |= H2_CF_DEM_MROOM;
1176 return 0;
1177 }
1178 else {
1179 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1180 return 0;
1181 }
1182 }
1183 return ret;
1184}
1185
Willy Tarreaucf68c782017-10-10 17:11:41 +02001186/* processes a PING frame and schedules an ACK if needed. The caller must pass
1187 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1188 * missing data. It may return an error in h2c.
1189 */
1190static int h2c_handle_ping(struct h2c *h2c)
1191{
1192 /* frame length must be exactly 8 */
1193 if (h2c->dfl != 8) {
1194 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1195 return 0;
1196 }
1197
1198 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001199 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001200 h2c->st0 = H2_CS_FRAME_A;
1201 return 1;
1202}
1203
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001204/* Try to send a window update for stream id <sid> and value <increment>.
1205 * Returns > 0 on success or zero on missing room or failure. It may return an
1206 * error in h2c.
1207 */
1208static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1209{
1210 struct buffer *res;
1211 char str[13];
1212 int ret = -1;
1213
1214 if (h2c_mux_busy(h2c, NULL)) {
1215 h2c->flags |= H2_CF_DEM_MBUSY;
1216 return 0;
1217 }
1218
Willy Tarreau44e973f2018-03-01 17:49:30 +01001219 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001220 if (!res) {
1221 h2c->flags |= H2_CF_MUX_MALLOC;
1222 h2c->flags |= H2_CF_DEM_MROOM;
1223 return 0;
1224 }
1225
1226 /* length: 4, type: 8, flags: none */
1227 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1228 write_n32(str + 5, sid);
1229 write_n32(str + 9, increment);
1230
1231 ret = bo_istput(res, ist2(str, 13));
1232
1233 if (unlikely(ret <= 0)) {
1234 if (!ret) {
1235 h2c->flags |= H2_CF_MUX_MFULL;
1236 h2c->flags |= H2_CF_DEM_MROOM;
1237 return 0;
1238 }
1239 else {
1240 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1241 return 0;
1242 }
1243 }
1244 return ret;
1245}
1246
1247/* try to send pending window update for the connection. It's safe to call it
1248 * with no pending updates. Returns > 0 on success or zero on missing room or
1249 * failure. It may return an error in h2c.
1250 */
1251static int h2c_send_conn_wu(struct h2c *h2c)
1252{
1253 int ret = 1;
1254
1255 if (h2c->rcvd_c <= 0)
1256 return 1;
1257
1258 /* send WU for the connection */
1259 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1260 if (ret > 0)
1261 h2c->rcvd_c = 0;
1262
1263 return ret;
1264}
1265
1266/* try to send pending window update for the current dmux stream. It's safe to
1267 * call it with no pending updates. Returns > 0 on success or zero on missing
1268 * room or failure. It may return an error in h2c.
1269 */
1270static int h2c_send_strm_wu(struct h2c *h2c)
1271{
1272 int ret = 1;
1273
1274 if (h2c->rcvd_s <= 0)
1275 return 1;
1276
1277 /* send WU for the stream */
1278 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1279 if (ret > 0)
1280 h2c->rcvd_s = 0;
1281
1282 return ret;
1283}
1284
Willy Tarreaucf68c782017-10-10 17:11:41 +02001285/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1286 * success, 0 on missing data or one of the h2_status values.
1287 */
1288static int h2c_ack_ping(struct h2c *h2c)
1289{
1290 struct buffer *res;
1291 char str[17];
1292 int ret = -1;
1293
1294 if (h2c->dbuf->i < 8)
1295 return 0;
1296
1297 if (h2c_mux_busy(h2c, NULL)) {
1298 h2c->flags |= H2_CF_DEM_MBUSY;
1299 return 0;
1300 }
1301
Willy Tarreau44e973f2018-03-01 17:49:30 +01001302 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001303 if (!res) {
1304 h2c->flags |= H2_CF_MUX_MALLOC;
1305 h2c->flags |= H2_CF_DEM_MROOM;
1306 return 0;
1307 }
1308
1309 memcpy(str,
1310 "\x00\x00\x08" /* length : 8 (same payload) */
1311 "\x06" "\x01" /* type : 6, flags : ACK */
1312 "\x00\x00\x00\x00" /* stream ID */, 9);
1313
1314 /* copy the original payload */
1315 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1316
1317 ret = bo_istput(res, ist2(str, 17));
1318 if (unlikely(ret <= 0)) {
1319 if (!ret) {
1320 h2c->flags |= H2_CF_MUX_MFULL;
1321 h2c->flags |= H2_CF_DEM_MROOM;
1322 return 0;
1323 }
1324 else {
1325 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1326 return 0;
1327 }
1328 }
1329 return ret;
1330}
1331
Willy Tarreau26f95952017-07-27 17:18:30 +02001332/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1333 * Returns > 0 on success or zero on missing data. It may return an error in
1334 * h2c or h2s. Described in RFC7540#6.9.
1335 */
1336static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1337{
1338 int32_t inc;
1339 int error;
1340
1341 if (h2c->dfl != 4) {
1342 error = H2_ERR_FRAME_SIZE_ERROR;
1343 goto conn_err;
1344 }
1345
1346 /* process full frame only */
1347 if (h2c->dbuf->i < h2c->dfl)
1348 return 0;
1349
1350 inc = h2_get_n32(h2c->dbuf, 0);
1351
1352 if (h2c->dsi != 0) {
1353 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001354
1355 /* it's not an error to receive WU on a closed stream */
1356 if (h2s->st == H2_SS_CLOSED)
1357 return 1;
1358
1359 if (!inc) {
1360 error = H2_ERR_PROTOCOL_ERROR;
1361 goto strm_err;
1362 }
1363
1364 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1365 error = H2_ERR_FLOW_CONTROL_ERROR;
1366 goto strm_err;
1367 }
1368
1369 h2s->mws += inc;
1370 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1371 h2s->flags &= ~H2_SF_BLK_SFCTL;
1372 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1373 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1374 /* This stream wanted to send but could not due to its
1375 * own flow control. We can put it back into the send
1376 * list now, it will be handled upon next send() call.
1377 */
1378 LIST_ADDQ(&h2c->send_list, &h2s->list);
1379 }
1380 }
1381 }
1382 else {
1383 /* connection window update */
1384 if (!inc) {
1385 error = H2_ERR_PROTOCOL_ERROR;
1386 goto conn_err;
1387 }
1388
1389 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1390 error = H2_ERR_FLOW_CONTROL_ERROR;
1391 goto conn_err;
1392 }
1393
1394 h2c->mws += inc;
1395 }
1396
1397 return 1;
1398
1399 conn_err:
1400 h2c_error(h2c, error);
1401 return 0;
1402
1403 strm_err:
1404 if (h2s) {
1405 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001406 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001407 }
1408 else
1409 h2c_error(h2c, error);
1410 return 0;
1411}
1412
Willy Tarreaue96b0922017-10-30 00:28:29 +01001413/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1414 * the last ID. Returns > 0 on success or zero on missing data. It may return
1415 * an error in h2c. Described in RFC7540#6.8.
1416 */
1417static int h2c_handle_goaway(struct h2c *h2c)
1418{
1419 int error;
1420 int last;
1421
1422 if (h2c->dsi != 0) {
1423 error = H2_ERR_PROTOCOL_ERROR;
1424 goto conn_err;
1425 }
1426
1427 if (h2c->dfl < 8) {
1428 error = H2_ERR_FRAME_SIZE_ERROR;
1429 goto conn_err;
1430 }
1431
1432 /* process full frame only */
1433 if (h2c->dbuf->i < h2c->dfl)
1434 return 0;
1435
1436 last = h2_get_n32(h2c->dbuf, 0);
1437 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1438 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001439 if (h2c->last_sid < 0)
1440 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001441 return 1;
1442
1443 conn_err:
1444 h2c_error(h2c, error);
1445 return 0;
1446}
1447
Willy Tarreau92153fc2017-12-03 19:46:19 +01001448/* processes a PRIORITY frame, and either skips it or rejects if it is
1449 * invalid. Returns > 0 on success or zero on missing data. It may return
1450 * an error in h2c. Described in RFC7540#6.3.
1451 */
1452static int h2c_handle_priority(struct h2c *h2c)
1453{
1454 int error;
1455
1456 if (h2c->dsi == 0) {
1457 error = H2_ERR_PROTOCOL_ERROR;
1458 goto conn_err;
1459 }
1460
1461 if (h2c->dfl != 5) {
1462 error = H2_ERR_FRAME_SIZE_ERROR;
1463 goto conn_err;
1464 }
1465
1466 /* process full frame only */
1467 if (h2c->dbuf->i < h2c->dfl)
1468 return 0;
1469
1470 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1471 /* 7540#5.3 : can't depend on itself */
1472 error = H2_ERR_PROTOCOL_ERROR;
1473 goto conn_err;
1474 }
1475 return 1;
1476
1477 conn_err:
1478 h2c_error(h2c, error);
1479 return 0;
1480}
1481
Willy Tarreaucd234e92017-08-18 10:59:39 +02001482/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1483 * Returns > 0 on success or zero on missing data. It may return an error in
1484 * h2c. Described in RFC7540#6.4.
1485 */
1486static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1487{
1488 int error;
1489
1490 if (h2c->dsi == 0) {
1491 error = H2_ERR_PROTOCOL_ERROR;
1492 goto conn_err;
1493 }
1494
Willy Tarreaucd234e92017-08-18 10:59:39 +02001495 if (h2c->dfl != 4) {
1496 error = H2_ERR_FRAME_SIZE_ERROR;
1497 goto conn_err;
1498 }
1499
1500 /* process full frame only */
1501 if (h2c->dbuf->i < h2c->dfl)
1502 return 0;
1503
1504 /* late RST, already handled */
1505 if (h2s->st == H2_SS_CLOSED)
1506 return 1;
1507
1508 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001509 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001510
1511 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001512 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001513 /* recv is used to force to detect CS_FL_EOS that wake()
1514 * doesn't handle in the stream-int code.
1515 */
1516 h2s->cs->data_cb->recv(h2s->cs);
1517 h2s->cs->data_cb->wake(h2s->cs);
1518 }
1519
1520 h2s->flags |= H2_SF_RST_RCVD;
1521 return 1;
1522
1523 conn_err:
1524 h2c_error(h2c, error);
1525 return 0;
1526}
1527
Willy Tarreau13278b42017-10-13 19:23:14 +02001528/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1529 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1530 * errors here are reported as connection errors since it's impossible to
1531 * recover from such errors after the compression context has been altered.
1532 */
1533static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1534{
1535 int error;
1536
1537 if (!h2c->dfl) {
1538 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1539 goto strm_err;
1540 }
1541
1542 if (!h2c->dbuf->size)
1543 return 0; // empty buffer
1544
1545 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1546 return 0; // incomplete frame
1547
1548 /* now either the frame is complete or the buffer is complete */
1549 if (h2s->st != H2_SS_IDLE) {
1550 /* FIXME: stream already exists, this is only allowed for
1551 * trailers (not supported for now).
1552 */
1553 error = H2_ERR_PROTOCOL_ERROR;
1554 goto conn_err;
1555 }
1556 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1557 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto conn_err;
1560 }
1561
1562 h2s = h2c_stream_new(h2c, h2c->dsi);
1563 if (!h2s) {
1564 error = H2_ERR_INTERNAL_ERROR;
1565 goto conn_err;
1566 }
1567
1568 h2s->st = H2_SS_OPEN;
1569 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1570 h2s->st = H2_SS_HREM;
1571 h2s->flags |= H2_SF_ES_RCVD;
1572 }
1573
1574 /* call the upper layers to process the frame, then let the upper layer
1575 * notify the stream about any change.
1576 */
1577 h2s->cs->data_cb->recv(h2s->cs);
1578
1579 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1580 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1581 error = H2_ERR_INTERNAL_ERROR;
1582 goto conn_err;
1583 }
1584
Willy Tarreau8f650c32017-11-21 19:36:21 +01001585 if (h2c->st0 >= H2_CS_ERROR)
1586 return 0;
1587
Willy Tarreau721c9742017-11-07 11:05:42 +01001588 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001589 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001590 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001591 }
1592 else {
1593 /* update the max stream ID if the request is being processed */
1594 if (h2s->id > h2c->max_id)
1595 h2c->max_id = h2s->id;
1596 }
1597
1598 return 1;
1599
1600 conn_err:
1601 h2c_error(h2c, error);
1602 return 0;
1603
1604 strm_err:
1605 if (h2s) {
1606 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001607 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001608 }
1609 else
1610 h2c_error(h2c, error);
1611 return 0;
1612}
1613
Willy Tarreau454f9052017-10-26 19:40:35 +02001614/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1615 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1616 */
1617static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1618{
1619 int error;
1620
1621 /* note that empty DATA frames are perfectly valid and sometimes used
1622 * to signal an end of stream (with the ES flag).
1623 */
1624
1625 if (!h2c->dbuf->size && h2c->dfl)
1626 return 0; // empty buffer
1627
1628 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1629 return 0; // incomplete frame
1630
1631 /* now either the frame is complete or the buffer is complete */
1632
1633 if (!h2c->dsi) {
1634 /* RFC7540#6.1 */
1635 error = H2_ERR_PROTOCOL_ERROR;
1636 goto conn_err;
1637 }
1638
1639 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1640 /* RFC7540#6.1 */
1641 error = H2_ERR_STREAM_CLOSED;
1642 goto strm_err;
1643 }
1644
Willy Tarreau454f9052017-10-26 19:40:35 +02001645 /* call the upper layers to process the frame, then let the upper layer
1646 * notify the stream about any change.
1647 */
1648 if (!h2s->cs) {
1649 error = H2_ERR_STREAM_CLOSED;
1650 goto strm_err;
1651 }
1652
1653 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001654
Willy Tarreau454f9052017-10-26 19:40:35 +02001655 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1656 /* cs has just been destroyed, we have to kill h2s. */
1657 error = H2_ERR_STREAM_CLOSED;
1658 goto strm_err;
1659 }
1660
Willy Tarreau8f650c32017-11-21 19:36:21 +01001661 if (h2c->st0 >= H2_CS_ERROR)
1662 return 0;
1663
Willy Tarreau721c9742017-11-07 11:05:42 +01001664 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001665 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001666 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001667 }
1668
1669 /* check for completion : the callee will change this to FRAME_A or
1670 * FRAME_H once done.
1671 */
1672 if (h2c->st0 == H2_CS_FRAME_P)
1673 return 0;
1674
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001675
1676 /* last frame */
1677 if (h2c->dff & H2_F_DATA_END_STREAM) {
1678 h2s->st = H2_SS_HREM;
1679 h2s->flags |= H2_SF_ES_RCVD;
1680 }
1681
Willy Tarreau454f9052017-10-26 19:40:35 +02001682 return 1;
1683
1684 conn_err:
1685 h2c_error(h2c, error);
1686 return 0;
1687
1688 strm_err:
1689 if (h2s) {
1690 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001691 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001692 }
1693 else
1694 h2c_error(h2c, error);
1695 return 0;
1696}
1697
Willy Tarreaubc933932017-10-09 16:21:43 +02001698/* process Rx frames to be demultiplexed */
1699static void h2_process_demux(struct h2c *h2c)
1700{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001701 struct h2s *h2s;
1702
Willy Tarreau081d4722017-05-16 21:51:05 +02001703 if (h2c->st0 >= H2_CS_ERROR)
1704 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001705
1706 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1707 if (h2c->st0 == H2_CS_PREFACE) {
1708 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1709 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1710 if (h2c->st0 == H2_CS_ERROR)
1711 h2c->st0 = H2_CS_ERROR2;
1712 goto fail;
1713 }
1714
1715 h2c->max_id = 0;
1716 h2c->st0 = H2_CS_SETTINGS1;
1717 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001718
1719 if (h2c->st0 == H2_CS_SETTINGS1) {
1720 struct h2_fh hdr;
1721
1722 /* ensure that what is pending is a valid SETTINGS frame
1723 * without an ACK.
1724 */
1725 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1726 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1727 if (h2c->st0 == H2_CS_ERROR)
1728 h2c->st0 = H2_CS_ERROR2;
1729 goto fail;
1730 }
1731
1732 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1733 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1734 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1735 h2c->st0 = H2_CS_ERROR2;
1736 goto fail;
1737 }
1738
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001739 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001740 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1741 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1742 h2c->st0 = H2_CS_ERROR2;
1743 goto fail;
1744 }
1745
1746 /* that's OK, switch to FRAME_P to process it */
1747 h2c->dfl = hdr.len;
1748 h2c->dsi = hdr.sid;
1749 h2c->dft = hdr.ft;
1750 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001751 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001752 h2c->st0 = H2_CS_FRAME_P;
1753 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001754 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001755
1756 /* process as many incoming frames as possible below */
1757 while (h2c->dbuf->i) {
1758 int ret = 0;
1759
1760 if (h2c->st0 >= H2_CS_ERROR)
1761 break;
1762
1763 if (h2c->st0 == H2_CS_FRAME_H) {
1764 struct h2_fh hdr;
1765
1766 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1767 break;
1768
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001769 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001770 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1771 h2c->st0 = H2_CS_ERROR;
1772 break;
1773 }
1774
1775 h2c->dfl = hdr.len;
1776 h2c->dsi = hdr.sid;
1777 h2c->dft = hdr.ft;
1778 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001779 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001780 h2c->st0 = H2_CS_FRAME_P;
1781 h2_skip_frame_hdr(h2c->dbuf);
1782 }
1783
1784 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001785 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001786
Willy Tarreaud7901432017-12-29 11:34:40 +01001787 if (h2c->st0 == H2_CS_FRAME_E)
1788 goto strm_err;
1789
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001790 if (h2s->st == H2_SS_IDLE &&
1791 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1792 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1793 * this state MUST be treated as a connection error
1794 */
1795 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1796 h2c->st0 = H2_CS_ERROR;
1797 break;
1798 }
1799
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001800 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1801 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1802 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1803 * this state MUST be treated as a stream error
1804 */
1805 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001806 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001807 goto strm_err;
1808 }
1809
Willy Tarreauab837502017-12-27 15:07:30 +01001810 /* Below the management of frames received in closed state is a
1811 * bit hackish because the spec makes strong differences between
1812 * streams closed by receiving RST, sending RST, and seeing ES
1813 * in both directions. In addition to this, the creation of a
1814 * new stream reusing the identifier of a closed one will be
1815 * detected here. Given that we cannot keep track of all closed
1816 * streams forever, we consider that unknown closed streams were
1817 * closed on RST received, which allows us to respond with an
1818 * RST without breaking the connection (eg: to abort a transfer).
1819 * Some frames have to be silently ignored as well.
1820 */
1821 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1822 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1823 /* #5.1.1: The identifier of a newly
1824 * established stream MUST be numerically
1825 * greater than all streams that the initiating
1826 * endpoint has opened or reserved. This
1827 * governs streams that are opened using a
1828 * HEADERS frame and streams that are reserved
1829 * using PUSH_PROMISE. An endpoint that
1830 * receives an unexpected stream identifier
1831 * MUST respond with a connection error.
1832 */
1833 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1834 goto strm_err;
1835 }
1836
1837 if (h2s->flags & H2_SF_RST_RCVD) {
1838 /* RFC7540#5.1:closed: an endpoint that
1839 * receives any frame other than PRIORITY after
1840 * receiving a RST_STREAM MUST treat that as a
1841 * stream error of type STREAM_CLOSED.
1842 *
1843 * Note that old streams fall into this category
1844 * and will lead to an RST being sent.
1845 */
1846 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1847 h2c->st0 = H2_CS_FRAME_E;
1848 goto strm_err;
1849 }
1850
1851 /* RFC7540#5.1:closed: if this state is reached as a
1852 * result of sending a RST_STREAM frame, the peer that
1853 * receives the RST_STREAM might have already sent
1854 * frames on the stream that cannot be withdrawn. An
1855 * endpoint MUST ignore frames that it receives on
1856 * closed streams after it has sent a RST_STREAM
1857 * frame. An endpoint MAY choose to limit the period
1858 * over which it ignores frames and treat frames that
1859 * arrive after this time as being in error.
1860 */
1861 if (!(h2s->flags & H2_SF_RST_SENT)) {
1862 /* RFC7540#5.1:closed: any frame other than
1863 * PRIO/WU/RST in this state MUST be treated as
1864 * a connection error
1865 */
1866 if (h2c->dft != H2_FT_RST_STREAM &&
1867 h2c->dft != H2_FT_PRIORITY &&
1868 h2c->dft != H2_FT_WINDOW_UPDATE) {
1869 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1870 goto strm_err;
1871 }
1872 }
1873 }
1874
Willy Tarreauc0da1962017-10-30 18:38:00 +01001875#if 0
1876 // problem below: it is not possible to completely ignore such
1877 // streams as we need to maintain the compression state as well
1878 // and for this we need to completely process these frames (eg:
1879 // HEADERS frames) as well as counting DATA frames to emit
1880 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1881 // This is a typical case of layer violation where the
1882 // transported contents are critical to the connection's
1883 // validity and must be ignored at the same time :-(
1884
1885 /* graceful shutdown, ignore streams whose ID is higher than
1886 * the one advertised in GOAWAY. RFC7540#6.8.
1887 */
1888 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1889 ret = MIN(h2c->dbuf->i, h2c->dfl);
1890 bi_del(h2c->dbuf, ret);
1891 h2c->dfl -= ret;
1892 ret = h2c->dfl == 0;
1893 goto strm_err;
1894 }
1895#endif
1896
Willy Tarreau7e98c052017-10-10 15:56:59 +02001897 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001898 case H2_FT_SETTINGS:
1899 if (h2c->st0 == H2_CS_FRAME_P)
1900 ret = h2c_handle_settings(h2c);
1901
1902 if (h2c->st0 == H2_CS_FRAME_A)
1903 ret = h2c_ack_settings(h2c);
1904 break;
1905
Willy Tarreaucf68c782017-10-10 17:11:41 +02001906 case H2_FT_PING:
1907 if (h2c->st0 == H2_CS_FRAME_P)
1908 ret = h2c_handle_ping(h2c);
1909
1910 if (h2c->st0 == H2_CS_FRAME_A)
1911 ret = h2c_ack_ping(h2c);
1912 break;
1913
Willy Tarreau26f95952017-07-27 17:18:30 +02001914 case H2_FT_WINDOW_UPDATE:
1915 if (h2c->st0 == H2_CS_FRAME_P)
1916 ret = h2c_handle_window_update(h2c, h2s);
1917 break;
1918
Willy Tarreau61290ec2017-10-17 08:19:21 +02001919 case H2_FT_CONTINUATION:
1920 /* we currently don't support CONTINUATION frames since
1921 * we have nowhere to store the partial HEADERS frame.
1922 * Let's abort the stream on an INTERNAL_ERROR here.
1923 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001924 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001925 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001926 h2c->st0 = H2_CS_FRAME_E;
1927 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001928 break;
1929
Willy Tarreau13278b42017-10-13 19:23:14 +02001930 case H2_FT_HEADERS:
1931 if (h2c->st0 == H2_CS_FRAME_P)
1932 ret = h2c_frt_handle_headers(h2c, h2s);
1933 break;
1934
Willy Tarreau454f9052017-10-26 19:40:35 +02001935 case H2_FT_DATA:
1936 if (h2c->st0 == H2_CS_FRAME_P)
1937 ret = h2c_frt_handle_data(h2c, h2s);
1938
1939 if (h2c->st0 == H2_CS_FRAME_A)
1940 ret = h2c_send_strm_wu(h2c);
1941 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001942
Willy Tarreau92153fc2017-12-03 19:46:19 +01001943 case H2_FT_PRIORITY:
1944 if (h2c->st0 == H2_CS_FRAME_P)
1945 ret = h2c_handle_priority(h2c);
1946 break;
1947
Willy Tarreaucd234e92017-08-18 10:59:39 +02001948 case H2_FT_RST_STREAM:
1949 if (h2c->st0 == H2_CS_FRAME_P)
1950 ret = h2c_handle_rst_stream(h2c, h2s);
1951 break;
1952
Willy Tarreaue96b0922017-10-30 00:28:29 +01001953 case H2_FT_GOAWAY:
1954 if (h2c->st0 == H2_CS_FRAME_P)
1955 ret = h2c_handle_goaway(h2c);
1956 break;
1957
Willy Tarreau1c661982017-10-30 13:52:01 +01001958 case H2_FT_PUSH_PROMISE:
1959 /* not permitted here, RFC7540#5.1 */
1960 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001961 break;
1962
1963 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001964 default:
1965 /* drop frames that we ignore. They may be larger than
1966 * the buffer so we drain all of their contents until
1967 * we reach the end.
1968 */
1969 ret = MIN(h2c->dbuf->i, h2c->dfl);
1970 bi_del(h2c->dbuf, ret);
1971 h2c->dfl -= ret;
1972 ret = h2c->dfl == 0;
1973 }
1974
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001975 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001976 /* We may have to send an RST if not done yet */
1977 if (h2s->st == H2_SS_ERROR)
1978 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001979
Willy Tarreaua20a5192017-12-27 11:02:06 +01001980 if (h2c->st0 == H2_CS_FRAME_E)
1981 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001982
Willy Tarreau7e98c052017-10-10 15:56:59 +02001983 /* error or missing data condition met above ? */
1984 if (ret <= 0)
1985 break;
1986
1987 if (h2c->st0 != H2_CS_FRAME_H) {
1988 bi_del(h2c->dbuf, h2c->dfl);
1989 h2c->st0 = H2_CS_FRAME_H;
1990 }
1991 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001992
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001993 if (h2c->rcvd_c > 0 &&
1994 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1995 h2c_send_conn_wu(h2c);
1996
Willy Tarreau52eed752017-09-22 15:05:09 +02001997 fail:
1998 /* we can go here on missing data, blocked response or error */
1999 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002000}
2001
2002/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2003 * the end.
2004 */
2005static int h2_process_mux(struct h2c *h2c)
2006{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002007 struct h2s *h2s, *h2s_back;
2008
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002009 /* start by sending possibly pending window updates */
2010 if (h2c->rcvd_c > 0 &&
2011 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2012 h2c_send_conn_wu(h2c) < 0)
2013 goto fail;
2014
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002015 /* First we always process the flow control list because the streams
2016 * waiting there were already elected for immediate emission but were
2017 * blocked just on this.
2018 */
2019
2020 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2021 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2022 h2c->st0 >= H2_CS_ERROR)
2023 break;
2024
2025 /* In theory it's possible that h2s->cs == NULL here :
2026 * - client sends crap that causes a parse error
2027 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2028 * - RST_STREAM cannot be emitted because mux is busy/full
2029 * - stream gets notified, detaches and quits
2030 * - mux buffer gets ready and wakes pending streams up
2031 * - bam!
2032 */
2033 h2s->flags &= ~H2_SF_BLK_ANY;
2034
2035 if (h2s->cs) {
2036 h2s->cs->data_cb->send(h2s->cs);
2037 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002038 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002039 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002040 }
2041
2042 /* depending on callee's blocking reasons, we may queue in send
2043 * list or completely dequeue.
2044 */
2045 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2046 if (h2s->flags & H2_SF_BLK_ANY) {
2047 LIST_DEL(&h2s->list);
2048 LIST_ADDQ(&h2c->send_list, &h2s->list);
2049 }
2050 else {
2051 LIST_DEL(&h2s->list);
2052 LIST_INIT(&h2s->list);
2053 if (h2s->cs)
2054 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002055 else {
2056 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002057 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002058 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002059 }
2060 }
2061 }
2062
2063 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2064 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2065 break;
2066
2067 /* In theory it's possible that h2s->cs == NULL here :
2068 * - client sends crap that causes a parse error
2069 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2070 * - RST_STREAM cannot be emitted because mux is busy/full
2071 * - stream gets notified, detaches and quits
2072 * - mux buffer gets ready and wakes pending streams up
2073 * - bam!
2074 */
2075 h2s->flags &= ~H2_SF_BLK_ANY;
2076
2077 if (h2s->cs) {
2078 h2s->cs->data_cb->send(h2s->cs);
2079 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002080 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002081 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002082 }
2083 /* depending on callee's blocking reasons, we may queue in fctl
2084 * list or completely dequeue.
2085 */
2086 if (h2s->flags & H2_SF_BLK_MFCTL) {
2087 /* stream hit the connection's flow control */
2088 LIST_DEL(&h2s->list);
2089 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2090 }
2091 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2092 LIST_DEL(&h2s->list);
2093 LIST_INIT(&h2s->list);
2094 if (h2s->cs)
2095 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002096 else {
2097 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002098 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002099 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002100 }
2101 }
2102
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002103 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002104 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002105 if (h2c->st0 == H2_CS_ERROR) {
2106 if (h2c->max_id >= 0) {
2107 h2c_send_goaway_error(h2c, NULL);
2108 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2109 return 0;
2110 }
2111
2112 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2113 }
2114 return 1;
2115 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002116 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002117}
2118
Willy Tarreau71681172017-10-23 14:39:06 +02002119
Willy Tarreau62f52692017-10-08 23:01:42 +02002120/*********************************************************/
2121/* functions below are I/O callbacks from the connection */
2122/*********************************************************/
2123
2124/* callback called on recv event by the connection handler */
2125static void h2_recv(struct connection *conn)
2126{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002127 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002128 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002129 int max;
2130
Willy Tarreau315d8072017-12-10 22:17:57 +01002131 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002132 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002133
Willy Tarreau44e973f2018-03-01 17:49:30 +01002134 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002135 if (!buf) {
2136 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002137 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002138 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002139
Willy Tarreaua2af5122017-10-09 11:56:46 +02002140 /* note: buf->o == 0 */
2141 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002142 if (max)
2143 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002144
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002145 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002146 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002147 return;
2148 }
2149
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002150 if (buf->i == buf->size)
2151 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002152 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002153}
2154
2155/* callback called on send event by the connection handler */
2156static void h2_send(struct connection *conn)
2157{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002158 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002159 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002160
2161 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002162 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002163
2164 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2165 /* a handshake was requested */
2166 return;
2167 }
2168
Willy Tarreaubc933932017-10-09 16:21:43 +02002169 /* This loop is quite simple : it tries to fill as much as it can from
2170 * pending streams into the existing buffer until it's reportedly full
2171 * or the end of send requests is reached. Then it tries to send this
2172 * buffer's contents out, marks it not full if at least one byte could
2173 * be sent, and tries again.
2174 *
2175 * The snd_buf() function normally takes a "flags" argument which may
2176 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2177 * data immediately comes and CO_SFL_STREAMER to indicate that the
2178 * connection is streaming lots of data (used to increase TLS record
2179 * size at the expense of latency). The former can be sent any time
2180 * there's a buffer full flag, as it indicates at least one stream
2181 * attempted to send and failed so there are pending data. An
2182 * alternative would be to set it as long as there's an active stream
2183 * but that would be problematic for ACKs until we have an absolute
2184 * guarantee that all waiters have at least one byte to send. The
2185 * latter should possibly not be set for now.
2186 */
2187
2188 done = 0;
2189 while (!done) {
2190 unsigned int flags = 0;
2191
2192 /* fill as much as we can into the current buffer */
2193 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2194 done = h2_process_mux(h2c);
2195
2196 if (conn->flags & CO_FL_ERROR)
2197 break;
2198
2199 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2200 flags |= CO_SFL_MSG_MORE;
2201
Willy Tarreau319994a2017-11-07 11:03:56 +01002202 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002203 break;
2204
2205 /* wrote at least one byte, the buffer is not full anymore */
2206 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2207 }
2208
Willy Tarreaua2af5122017-10-09 11:56:46 +02002209 if (conn->flags & CO_FL_SOCK_WR_SH) {
2210 /* output closed, nothing to send, clear the buffer to release it */
2211 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002212 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002213}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002214
Willy Tarreau62f52692017-10-08 23:01:42 +02002215/* callback called on any event by the connection handler.
2216 * It applies changes and returns zero, or < 0 if it wants immediate
2217 * destruction of the connection (which normally doesn not happen in h2).
2218 */
2219static int h2_wake(struct connection *conn)
2220{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002221 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002222 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002223
Willy Tarreaud13bf272017-12-14 10:34:52 +01002224 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2225 h2_process_demux(h2c);
2226
2227 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2228 h2c->dbuf->i = 0;
2229
2230 if (h2c->dbuf->i != h2c->dbuf->size)
2231 h2c->flags &= ~H2_CF_DEM_DFULL;
2232 }
2233
Willy Tarreau8ec14062017-12-30 18:08:13 +01002234 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2235 /* frontend is stopping, reload likely in progress, let's try
2236 * to announce a graceful shutdown if not yet done. We don't
2237 * care if it fails, it will be tried again later.
2238 */
2239 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2240 if (h2c->last_sid < 0)
2241 h2c->last_sid = (1U << 31) - 1;
2242 h2c_send_goaway_error(h2c, NULL);
2243 }
2244 }
2245
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002246 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002247 * If we received early data, and the handshake is done, wake
2248 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002249 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002250 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2251 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2252 struct eb32_node *node;
2253 struct h2s *h2s;
2254
2255 h2c->flags |= H2_CF_WAIT_FOR_HS;
2256 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2257
2258 while (node) {
2259 h2s = container_of(node, struct h2s, by_id);
2260 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2261 h2s->cs->data_cb->wake(h2s->cs);
2262 node = eb32_next(node);
2263 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002264 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002265
Willy Tarreau26bd7612017-10-09 16:47:04 +02002266 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002267 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2268 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2269 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002270 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002271
2272 if (eb_is_empty(&h2c->streams_by_id)) {
2273 /* no more stream, kill the connection now */
2274 h2_release(conn);
2275 return -1;
2276 }
2277 else {
2278 /* some streams still there, we need to signal them all and
2279 * wait for their departure.
2280 */
2281 __conn_xprt_stop_recv(conn);
2282 __conn_xprt_stop_send(conn);
2283 return 0;
2284 }
2285 }
2286
2287 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002288 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002289
2290 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002291 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002292 __conn_xprt_stop_recv(conn);
2293 }
2294 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002295 __conn_xprt_want_recv(conn);
2296 }
2297
2298 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002299 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2300 (h2c->st0 == H2_CS_ERROR ||
2301 h2c->mbuf->o ||
2302 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2303 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002304 __conn_xprt_want_send(conn);
2305 }
2306 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002307 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002308 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002309 }
2310
Willy Tarreau3f133572017-10-31 19:21:06 +01002311 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002312 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002313 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002314 task_queue(h2c->task);
2315 }
2316 else
2317 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002318 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002319 return 0;
2320}
2321
Willy Tarreauea392822017-10-31 10:02:25 +01002322/* Connection timeout management. The principle is that if there's no receipt
2323 * nor sending for a certain amount of time, the connection is closed. If the
2324 * MUX buffer still has lying data or is not allocatable, the connection is
2325 * immediately killed. If it's allocatable and empty, we attempt to send a
2326 * GOAWAY frame.
2327 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002328static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002329{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002330 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002331 int expired = tick_is_expired(t->expire, now_ms);
2332
Willy Tarreau0975f112018-03-29 15:22:59 +02002333 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002334 return t;
2335
Willy Tarreau0975f112018-03-29 15:22:59 +02002336 task_delete(t);
2337 task_free(t);
2338
2339 if (!h2c) {
2340 /* resources were already deleted */
2341 return NULL;
2342 }
2343
2344 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002345 h2c_error(h2c, H2_ERR_NO_ERROR);
2346 h2_wake_some_streams(h2c, 0, 0);
2347
2348 if (h2c->mbuf->o) {
2349 /* don't even try to send a GOAWAY, the buffer is stuck */
2350 h2c->flags |= H2_CF_GOAWAY_FAILED;
2351 }
2352
2353 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002354 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002355 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2356 h2c->flags |= H2_CF_GOAWAY_FAILED;
2357
2358 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2359 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2360
Willy Tarreau0975f112018-03-29 15:22:59 +02002361 /* either we can release everything now or it will be done later once
2362 * the last stream closes.
2363 */
2364 if (eb_is_empty(&h2c->streams_by_id))
2365 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002366
Willy Tarreauea392822017-10-31 10:02:25 +01002367 return NULL;
2368}
2369
2370
Willy Tarreau62f52692017-10-08 23:01:42 +02002371/*******************************************/
2372/* functions below are used by the streams */
2373/*******************************************/
2374
2375/*
2376 * Attach a new stream to a connection
2377 * (Used for outgoing connections)
2378 */
2379static struct conn_stream *h2_attach(struct connection *conn)
2380{
2381 return NULL;
2382}
2383
2384/* callback used to update the mux's polling flags after changing a cs' status.
2385 * The caller (cs_update_mux_polling) will take care of propagating any changes
2386 * to the transport layer.
2387 */
2388static void h2_update_poll(struct conn_stream *cs)
2389{
Willy Tarreau1d393222017-10-17 10:26:19 +02002390 struct h2s *h2s = cs->ctx;
2391
2392 if (!h2s)
2393 return;
2394
Willy Tarreaud7739c82017-10-30 15:38:23 +01002395 /* we may unblock a blocked read */
2396
Willy Tarreau315d8072017-12-10 22:17:57 +01002397 if (cs->flags & CS_FL_DATA_RD_ENA) {
2398 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002399 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002400 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002401 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002402 conn_xprt_want_send(cs->conn);
2403 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002404 }
2405
Willy Tarreau1d393222017-10-17 10:26:19 +02002406 /* Note: the stream and stream-int code doesn't allow us to perform a
2407 * synchronous send() here unfortunately, because this code is called
2408 * as si_update() from the process_stream() context. This means that
2409 * we have to queue the current cs and defer its processing after the
2410 * connection's cs list is processed anyway.
2411 */
2412
2413 if (cs->flags & CS_FL_DATA_WR_ENA) {
2414 if (LIST_ISEMPTY(&h2s->list)) {
2415 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2416 !h2s->h2c->mbuf->o && // not yet subscribed
2417 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2418 conn_xprt_want_send(cs->conn);
2419 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2420 }
2421 }
2422 else if (!LIST_ISEMPTY(&h2s->list)) {
2423 LIST_DEL(&h2s->list);
2424 LIST_INIT(&h2s->list);
2425 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2426 }
2427
2428 /* this can happen from within si_chk_snd() */
2429 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2430 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002431}
2432
2433/*
2434 * Detach the stream from the connection and possibly release the connection.
2435 */
2436static void h2_detach(struct conn_stream *cs)
2437{
Willy Tarreau60935142017-10-16 18:11:19 +02002438 struct h2s *h2s = cs->ctx;
2439 struct h2c *h2c;
2440
2441 cs->ctx = NULL;
2442 if (!h2s)
2443 return;
2444
2445 h2c = h2s->h2c;
2446 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002447 h2c->nb_cs--;
Willy Tarreau60935142017-10-16 18:11:19 +02002448
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002449 /* this stream may be blocked waiting for some data to leave (possibly
2450 * an ES or RST frame), so orphan it in this case.
2451 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002452 if (!(cs->conn->flags & CO_FL_ERROR) &&
2453 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002454 return;
2455
Willy Tarreau45f752e2017-10-30 15:44:59 +01002456 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2457 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2458 /* unblock the connection if it was blocked on this
2459 * stream.
2460 */
2461 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2462 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2463 conn_xprt_want_recv(cs->conn);
2464 conn_xprt_want_send(cs->conn);
2465 }
2466
Willy Tarreau71049cc2018-03-28 13:56:39 +02002467 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002468
Willy Tarreaue323f342018-03-28 13:51:45 +02002469 /* We don't want to close right now unless we're removing the
2470 * last stream, and either the connection is in error, or it
2471 * reached the ID already specified in a GOAWAY frame received
2472 * or sent (as seen by last_sid >= 0).
2473 */
2474 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2475 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
2476 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2477 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2478 (conn_xprt_read0_pending(h2c->conn) ||
2479 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2480 /* no more stream will come, kill it now */
2481 h2_release(h2c->conn);
2482 }
2483 else if (h2c->task) {
2484 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2485 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2486 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002487 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002488 else
2489 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002490 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002491}
2492
2493static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2494{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002495 struct h2s *h2s = cs->ctx;
2496
2497 if (!mode)
2498 return;
2499
Willy Tarreau721c9742017-11-07 11:05:42 +01002500 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002501 return;
2502
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002503 /* if no outgoing data was seen on this stream, it means it was
2504 * closed with a "tcp-request content" rule that is normally
2505 * used to kill the connection ASAP (eg: limit abuse). In this
2506 * case we send a goaway to close the connection.
2507 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002508 if (!(h2s->flags & H2_SF_RST_SENT) &&
2509 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002510 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002511
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002512 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2513 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2514 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002515 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002516
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002517 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2518 conn_xprt_want_send(cs->conn);
2519
Willy Tarreau00dd0782018-03-01 16:31:34 +01002520 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002521
2522 add_to_list:
2523 if (LIST_ISEMPTY(&h2s->list)) {
2524 if (h2s->flags & H2_SF_BLK_MFCTL)
2525 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2526 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2527 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2528 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002529}
2530
2531static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2532{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002533 struct h2s *h2s = cs->ctx;
2534
Willy Tarreau721c9742017-11-07 11:05:42 +01002535 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002536 return;
2537
Willy Tarreau67434202017-11-06 20:20:51 +01002538 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002539 /* we can cleanly close using an empty data frame only after headers */
2540
2541 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2542 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002543 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002544
2545 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002546 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002547 else
2548 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002549 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002550 /* if no outgoing data was seen on this stream, it means it was
2551 * closed with a "tcp-request content" rule that is normally
2552 * used to kill the connection ASAP (eg: limit abuse). In this
2553 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002554 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002555 if (!(h2s->flags & H2_SF_RST_SENT) &&
2556 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002557 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002558
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002559 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2560 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002561 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002562 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002563
Willy Tarreau00dd0782018-03-01 16:31:34 +01002564 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002565 }
2566
2567 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2568 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002569
2570 add_to_list:
2571 if (LIST_ISEMPTY(&h2s->list)) {
2572 if (h2s->flags & H2_SF_BLK_MFCTL)
2573 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2574 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2575 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2576 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002577}
2578
Willy Tarreau13278b42017-10-13 19:23:14 +02002579/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2580 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2581 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002582 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002583 */
2584static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2585{
2586 struct h2c *h2c = h2s->h2c;
2587 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002588 struct chunk *tmp = get_trash_chunk();
2589 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002590 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002591 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002592 int flen = h2c->dfl;
2593 int outlen = 0;
2594 int wrap;
2595 int try;
2596
2597 if (!h2c->dfl) {
2598 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002599 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002600 return 0;
2601 }
2602
Willy Tarreau68472622017-12-11 18:36:37 +01002603 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2604 return 0; // incomplete input frame
2605
Willy Tarreau13278b42017-10-13 19:23:14 +02002606 /* if the input buffer wraps, take a temporary copy of it (rare) */
2607 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2608 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002609 copy = alloc_trash_chunk();
2610 if (!copy) {
2611 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2612 goto fail;
2613 }
2614 memcpy(copy->str, h2c->dbuf->p, wrap);
2615 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2616 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002617 }
2618
2619 /* The padlen is the first byte before data, and the padding appears
2620 * after data. padlen+data+padding are included in flen.
2621 */
2622 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002623 h2c->dpl = *hdrs;
2624 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002625 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2626 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002627 return 0;
2628 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002629 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002630 hdrs += 1; // skip Pad Length
2631 }
2632
2633 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2634 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002635 if (read_n32(hdrs) == h2s->id) {
2636 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2637 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2638 return 0;//goto fail_stream;
2639 }
2640
Willy Tarreau13278b42017-10-13 19:23:14 +02002641 hdrs += 5; // stream dep = 4, weight = 1
2642 flen -= 5;
2643 }
2644
2645 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2646 * don't support this for now and can't even decompress so we have to
2647 * break the connection.
2648 */
2649 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2650 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002651 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002652 }
2653
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002654 /* we can't retry a failed decompression operation so we must be very
2655 * careful not to take any risks. In practice the output buffer is
2656 * always empty except maybe for trailers, so these operations almost
2657 * never happen.
2658 */
2659 if (unlikely(buf->o)) {
2660 /* need to let the output buffer flush and
2661 * mark the buffer for later wake up.
2662 */
2663 goto fail;
2664 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002665
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002666 if (unlikely(buffer_space_wraps(buf))) {
2667 /* it doesn't fit and the buffer is fragmented,
2668 * so let's defragment it and try again.
2669 */
2670 buffer_slow_realign(buf);
2671 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002672
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002673 /* first check if we have some room after p+i */
2674 try = buf->data + buf->size - (buf->p + buf->i);
2675
2676 /* otherwise continue between data and p-o */
2677 if (try <= 0) {
2678 try = buf->p - (buf->data + buf->o);
2679 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002680 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002681 }
2682 if (try > count)
2683 try = count;
2684
2685 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2686 sizeof(list)/sizeof(list[0]), tmp);
2687 if (outlen < 0) {
2688 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2689 goto fail;
2690 }
2691
2692 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002693 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
2694 outlen = h2_make_h1_request(list, bi_end(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002695
2696 if (outlen < 0) {
2697 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2698 goto fail;
2699 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002700
Willy Tarreau174b06a2018-04-25 18:13:58 +02002701 if (msgf & H2_MSGF_BODY) {
2702 /* a payload is present */
2703 if (msgf & H2_MSGF_BODY_CL)
2704 h2s->flags |= H2_SF_DATA_CLEN;
2705 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2706 h2s->flags |= H2_SF_DATA_CHNK;
2707 }
2708
Willy Tarreau13278b42017-10-13 19:23:14 +02002709 /* now consume the input data */
2710 bi_del(h2c->dbuf, h2c->dfl);
2711 h2c->st0 = H2_CS_FRAME_H;
2712 buf->i += outlen;
2713
2714 /* don't send it before returning data!
2715 * FIXME: should we instead try to send it much later, after the
2716 * response ? This would require that we keep a copy of it in h2s.
2717 */
2718 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2719 h2s->cs->flags |= CS_FL_EOS;
2720 h2s->flags |= H2_SF_ES_RCVD;
2721 }
2722
Willy Tarreau68dd9852017-07-03 14:44:26 +02002723 leave:
2724 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002725 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002726 fail:
2727 outlen = 0;
2728 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002729}
2730
Willy Tarreau454f9052017-10-26 19:40:35 +02002731/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2732 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2733 * in use, a new chunk is emitted for each frame. This is supposed to fit
2734 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2735 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2736 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2737 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002738 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2739 * checked to know if some data remain pending (an empty DATA frame can return
2740 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2741 * connection errors in h2c->errcode. The caller must already have checked the
2742 * frame header and ensured that the frame was complete or the buffer full. It
2743 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002744 */
2745static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2746{
2747 struct h2c *h2c = h2s->h2c;
2748 int block1, block2;
2749 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002750 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002751
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002752 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002753 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002754
2755 /* The padlen is the first byte before data, and the padding appears
2756 * after data. padlen+data+padding are included in flen.
2757 */
Willy Tarreau79127812017-12-03 21:06:59 +01002758 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002759 if (h2c->dbuf->i < 1)
2760 return 0;
2761
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002762 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2763 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002764 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2765 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002766 return 0;
2767 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002768
2769 /* skip the padlen byte */
2770 bi_del(h2c->dbuf, 1);
2771 h2c->dfl--;
2772 h2c->rcvd_c++; h2c->rcvd_s++;
2773 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002774 }
2775
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002776 flen = h2c->dfl - h2c->dpl;
2777 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002778 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002779
2780 if (flen > h2c->dbuf->i) {
2781 flen = h2c->dbuf->i;
2782 if (!flen)
2783 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002784 }
2785
Willy Tarreaueba10f22018-04-25 20:44:22 +02002786 /* chunked-encoding requires more room */
2787 if (h2s->flags & H2_SF_DATA_CHNK) {
2788 chklen = MIN(flen, count);
2789 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2790 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2791 (chklen < 1048576) ? 4 : 8;
2792 chklen += 4; // CRLF, CRLF
2793 }
2794
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002795 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002796 if (flen + chklen > count) {
2797 if (chklen >= count)
2798 goto full;
2799 flen = count - chklen;
2800 }
2801
2802 if (h2s->flags & H2_SF_DATA_CHNK) {
2803 /* emit the chunk size */
2804 unsigned int chksz = flen;
2805 char str[10];
2806 char *beg;
2807
2808 beg = str + sizeof(str);
2809 *--beg = '\n';
2810 *--beg = '\r';
2811 do {
2812 *--beg = hextab[chksz & 0xF];
2813 } while (chksz >>= 4);
2814 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002815 }
2816
Willy Tarreau454f9052017-10-26 19:40:35 +02002817 /* Block1 is the length of the first block before the buffer wraps,
2818 * block2 is the optional second block to reach the end of the frame.
2819 */
2820 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002821 if (block1 > flen)
2822 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002823 block2 = flen - block1;
2824
2825 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002826 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002827
2828 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002829 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002830
Willy Tarreaueba10f22018-04-25 20:44:22 +02002831 if (h2s->flags & H2_SF_DATA_CHNK) {
2832 /* emit the CRLF */
2833 bi_putblk(buf, "\r\n", 2);
2834 }
2835
Willy Tarreau454f9052017-10-26 19:40:35 +02002836 /* now mark the input data as consumed (will be deleted from the buffer
2837 * by the caller when seeing FRAME_A after sending the window update).
2838 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002839 bi_del(h2c->dbuf, flen);
2840 h2c->dfl -= flen;
2841 h2c->rcvd_c += flen;
2842 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2843
2844 if (h2c->dfl > h2c->dpl) {
2845 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002846 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002847 }
2848
Willy Tarreau4a28da12018-01-04 14:41:00 +01002849 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002850 /* here we're done with the frame, all the payload (except padding) was
2851 * transferred.
2852 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002853
2854 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2855 /* emit the trailing 0 CRLF CRLF */
2856 if (count < 5)
2857 goto more;
2858 chklen += 5;
2859 bi_putblk(buf, "0\r\n\r\n", 5);
2860 }
2861
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002862 h2c->rcvd_c += h2c->dpl;
2863 h2c->rcvd_s += h2c->dpl;
2864 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002865 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2866
2867 /* don't send it before returning data!
2868 * FIXME: should we instead try to send it much later, after the
2869 * response ? This would require that we keep a copy of it in h2s.
2870 */
Willy Tarreau79127812017-12-03 21:06:59 +01002871 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002872 h2s->cs->flags |= CS_FL_EOS;
2873 h2s->flags |= H2_SF_ES_RCVD;
2874 }
2875
Willy Tarreaueba10f22018-04-25 20:44:22 +02002876 return flen + chklen;
2877 full:
2878 flen = chklen = 0;
2879 more:
2880 h2c->flags |= H2_CF_DEM_SFULL;
2881 h2s->cs->flags |= CS_FL_RCV_MORE;
2882 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002883}
2884
Willy Tarreau62f52692017-10-08 23:01:42 +02002885/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002886 * Called from the upper layer to get more data, up to <count> bytes. The
2887 * caller is responsible for never asking for more data than what is available
2888 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002889 */
2890static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2891{
Willy Tarreau13278b42017-10-13 19:23:14 +02002892 struct h2s *h2s = cs->ctx;
2893 struct h2c *h2c = h2s->h2c;
2894 int ret = 0;
2895
2896 if (h2c->st0 != H2_CS_FRAME_P)
2897 return 0; // no pre-parsed frame yet
2898
2899 if (h2c->dsi != h2s->id)
2900 return 0; // not for us
2901
2902 if (!h2c->dbuf->size)
2903 return 0; // empty buffer
2904
Willy Tarreau13278b42017-10-13 19:23:14 +02002905 switch (h2c->dft) {
2906 case H2_FT_HEADERS:
2907 ret = h2_frt_decode_headers(h2s, buf, count);
2908 break;
2909
Willy Tarreau454f9052017-10-26 19:40:35 +02002910 case H2_FT_DATA:
2911 ret = h2_frt_transfer_data(h2s, buf, count);
2912 break;
2913
Willy Tarreau13278b42017-10-13 19:23:14 +02002914 default:
2915 ret = 0;
2916 }
2917 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002918}
2919
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002920/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2921 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2922 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2923 * to the number of buffer bytes consumed.
2924 */
2925static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2926{
2927 struct http_hdr list[MAX_HTTP_HDR];
2928 struct h2c *h2c = h2s->h2c;
2929 struct h1m *h1m = &h2s->res;
2930 struct chunk outbuf;
2931 int es_now = 0;
2932 int ret = 0;
2933 int hdr;
2934
2935 if (h2c_mux_busy(h2c, h2s)) {
2936 h2s->flags |= H2_SF_BLK_MBUSY;
2937 return 0;
2938 }
2939
Willy Tarreau44e973f2018-03-01 17:49:30 +01002940 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002941 h2c->flags |= H2_CF_MUX_MALLOC;
2942 h2s->flags |= H2_SF_BLK_MROOM;
2943 return 0;
2944 }
2945
2946 /* First, try to parse the H1 response and index it into <list>.
2947 * NOTE! Since it comes from haproxy, we *know* that a response header
2948 * block does not wrap and we can safely read it this way without
2949 * having to realign the buffer.
2950 */
2951 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2952 list, sizeof(list)/sizeof(list[0]), h1m);
2953 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002954 /* incomplete or invalid response, this is abnormal coming from
2955 * haproxy and may only result in a bad errorfile or bad Lua code
2956 * so that won't be fixed, raise an error now.
2957 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002958 * FIXME: we should instead add the ability to only return a
2959 * 502 bad gateway. But in theory this is not supposed to
2960 * happen.
2961 */
2962 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2963 ret = 0;
2964 goto end;
2965 }
2966
2967 chunk_reset(&outbuf);
2968
2969 while (1) {
2970 outbuf.str = bo_end(h2c->mbuf);
2971 outbuf.size = bo_contig_space(h2c->mbuf);
2972 outbuf.len = 0;
2973
2974 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2975 break;
2976 realign_again:
2977 buffer_slow_realign(h2c->mbuf);
2978 }
2979
2980 if (outbuf.size < 9) {
2981 h2c->flags |= H2_CF_MUX_MFULL;
2982 h2s->flags |= H2_SF_BLK_MROOM;
2983 ret = 0;
2984 goto end;
2985 }
2986
2987 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2988 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2989 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2990 outbuf.len = 9;
2991
2992 /* encode status, which necessarily is the first one */
2993 if (outbuf.len < outbuf.size && h1m->status == 200)
2994 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2995 else if (outbuf.len < outbuf.size && h1m->status == 304)
2996 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002997 else if (unlikely(list[0].v.len != 3)) {
2998 /* this is an unparsable response */
2999 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3000 ret = 0;
3001 goto end;
3002 }
3003 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003004 /* basic encoding of the status code */
3005 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3006 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3007 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3008 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3009 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3010 }
3011 else {
3012 if (buffer_space_wraps(h2c->mbuf))
3013 goto realign_again;
3014
3015 h2c->flags |= H2_CF_MUX_MFULL;
3016 h2s->flags |= H2_SF_BLK_MROOM;
3017 ret = 0;
3018 goto end;
3019 }
3020
3021 /* encode all headers, stop at empty name */
3022 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003023 /* these ones do not exist in H2 and must be dropped. */
3024 if (isteq(list[hdr].n, ist("connection")) ||
3025 isteq(list[hdr].n, ist("proxy-connection")) ||
3026 isteq(list[hdr].n, ist("keep-alive")) ||
3027 isteq(list[hdr].n, ist("upgrade")) ||
3028 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003029 continue;
3030
3031 if (isteq(list[hdr].n, ist("")))
3032 break; // end
3033
3034 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3035 /* output full */
3036 if (buffer_space_wraps(h2c->mbuf))
3037 goto realign_again;
3038
3039 h2c->flags |= H2_CF_MUX_MFULL;
3040 h2s->flags |= H2_SF_BLK_MROOM;
3041 ret = 0;
3042 goto end;
3043 }
3044 }
3045
3046 /* we may need to add END_STREAM */
3047 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3048 es_now = 1;
3049
3050 /* update the frame's size */
3051 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3052
3053 if (es_now)
3054 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3055
3056 /* consume incoming H1 response */
3057 bo_del(buf, ret);
3058
3059 /* commit the H2 response */
3060 h2c->mbuf->o += outbuf.len;
3061 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003062 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003063
3064 /* for now we don't implemented CONTINUATION, so we wait for a
3065 * body or directly end in TRL2.
3066 */
3067 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003068 // trim any possibly pending data (eg: inconsistent content-length)
3069 bo_del(buf, buf->o);
3070
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003071 h1m->state = HTTP_MSG_DONE;
3072 h2s->flags |= H2_SF_ES_SENT;
3073 if (h2s->st == H2_SS_OPEN)
3074 h2s->st = H2_SS_HLOC;
3075 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003076 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003077 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003078 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003079 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003080 h1m->state = HTTP_MSG_RPBEFORE;
3081 h1m->status = 0;
3082 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003083 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003084 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003085 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003086 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003087
3088 end:
3089 //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));
3090 return ret;
3091}
3092
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003093/* Try to send a DATA frame matching HTTP/1 response present in the response
3094 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3095 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3096 * corresponds to the number of buffer bytes consumed.
3097 */
3098static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3099{
3100 struct h2c *h2c = h2s->h2c;
3101 struct h1m *h1m = &h2s->res;
3102 struct chunk outbuf;
3103 int ret = 0;
3104 int total = 0;
3105 int es_now = 0;
3106 int size = 0;
3107 char *blk1, *blk2;
3108 int len1, len2;
3109
3110 if (h2c_mux_busy(h2c, h2s)) {
3111 h2s->flags |= H2_SF_BLK_MBUSY;
3112 goto end;
3113 }
3114
Willy Tarreau44e973f2018-03-01 17:49:30 +01003115 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003116 h2c->flags |= H2_CF_MUX_MALLOC;
3117 h2s->flags |= H2_SF_BLK_MROOM;
3118 goto end;
3119 }
3120
3121 new_frame:
3122 if (!buf->o)
3123 goto end;
3124
3125 chunk_reset(&outbuf);
3126
3127 while (1) {
3128 outbuf.str = bo_end(h2c->mbuf);
3129 outbuf.size = bo_contig_space(h2c->mbuf);
3130 outbuf.len = 0;
3131
3132 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3133 break;
3134 realign_again:
3135 buffer_slow_realign(h2c->mbuf);
3136 }
3137
3138 if (outbuf.size < 9) {
3139 h2c->flags |= H2_CF_MUX_MFULL;
3140 h2s->flags |= H2_SF_BLK_MROOM;
3141 goto end;
3142 }
3143
3144 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3145 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3146 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3147 outbuf.len = 9;
3148
3149 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3150 case 0: /* no content length, read till SHUTW */
3151 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003152 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003153 break;
3154 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3155 size = buf->o;
3156 if ((long long)size > h1m->curr_len)
3157 size = h1m->curr_len;
3158 break;
3159 default: /* te:chunked : parse chunks */
3160 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3161 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3162 if (!ret)
3163 goto end;
3164
3165 if (ret < 0) {
3166 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3167 h1m->err_pos = ret;
3168 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3169 goto end;
3170 }
3171 bo_del(buf, ret);
3172 total += ret;
3173 h1m->state = HTTP_MSG_CHUNK_SIZE;
3174 }
3175
3176 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3177 unsigned int chunk;
3178
3179 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3180 if (!ret)
3181 goto end;
3182
3183 if (ret < 0) {
3184 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3185 h1m->err_pos = ret;
3186 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3187 goto end;
3188 }
3189
3190 size = chunk;
3191 h1m->curr_len = chunk;
3192 h1m->body_len += chunk;
3193 bo_del(buf, ret);
3194 total += ret;
3195 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3196 if (!size)
3197 goto send_empty;
3198 }
3199
3200 /* in MSG_DATA state, continue below */
3201 size = h1m->curr_len;
3202 break;
3203 }
3204
3205 /* we have in <size> the exact number of bytes we need to copy from
3206 * the H1 buffer. We need to check this against the connection's and
3207 * the stream's send windows, and to ensure that this fits in the max
3208 * frame size and in the buffer's available space minus 9 bytes (for
3209 * the frame header). The connection's flow control is applied last so
3210 * that we can use a separate list of streams which are immediately
3211 * unblocked on window opening. Note: we don't implement padding.
3212 */
3213
3214 if (size > buf->o)
3215 size = buf->o;
3216
3217 if (size > h2s->mws)
3218 size = h2s->mws;
3219
3220 if (size <= 0) {
3221 h2s->flags |= H2_SF_BLK_SFCTL;
3222 goto end;
3223 }
3224
3225 if (h2c->mfs && size > h2c->mfs)
3226 size = h2c->mfs;
3227
3228 if (size + 9 > outbuf.size) {
3229 /* we have an opportunity for enlarging the too small
3230 * available space, let's try.
3231 */
3232 if (buffer_space_wraps(h2c->mbuf))
3233 goto realign_again;
3234 size = outbuf.size - 9;
3235 }
3236
3237 if (size <= 0) {
3238 h2c->flags |= H2_CF_MUX_MFULL;
3239 h2s->flags |= H2_SF_BLK_MROOM;
3240 goto end;
3241 }
3242
3243 if (size > h2c->mws)
3244 size = h2c->mws;
3245
3246 if (size <= 0) {
3247 h2s->flags |= H2_SF_BLK_MFCTL;
3248 goto end;
3249 }
3250
3251 /* copy whatever we can */
3252 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3253 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3254 if (ret == 1)
3255 len2 = 0;
3256
3257 if (!ret || len1 + len2 < size) {
3258 /* FIXME: must normally never happen */
3259 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3260 goto end;
3261 }
3262
3263 /* limit len1/len2 to size */
3264 if (len1 + len2 > size) {
3265 int sub = len1 + len2 - size;
3266
3267 if (len2 > sub)
3268 len2 -= sub;
3269 else {
3270 sub -= len2;
3271 len2 = 0;
3272 len1 -= sub;
3273 }
3274 }
3275
3276 /* now let's copy this this into the output buffer */
3277 memcpy(outbuf.str + 9, blk1, len1);
3278 if (len2)
3279 memcpy(outbuf.str + 9 + len1, blk2, len2);
3280
3281 send_empty:
3282 /* we may need to add END_STREAM */
3283 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3284 * could rely on the MSG_MORE flag as a hint for this ?
3285 */
3286 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3287 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3288 es_now = 1;
3289
3290 /* update the frame's size */
3291 h2_set_frame_size(outbuf.str, size);
3292
3293 if (es_now)
3294 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3295
3296 /* commit the H2 response */
3297 h2c->mbuf->o += size + 9;
3298 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3299
3300 /* consume incoming H1 response */
3301 if (size > 0) {
3302 bo_del(buf, size);
3303 total += size;
3304 h1m->curr_len -= size;
3305 h2s->mws -= size;
3306 h2c->mws -= size;
3307
3308 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3309 h1m->state = HTTP_MSG_CHUNK_CRLF;
3310 goto new_frame;
3311 }
3312 }
3313
3314 if (es_now) {
3315 if (h2s->st == H2_SS_OPEN)
3316 h2s->st = H2_SS_HLOC;
3317 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003318 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003319
Willy Tarreau35a62702018-02-27 15:37:25 +01003320 if (!(h1m->flags & H1_MF_CHNK)) {
3321 // trim any possibly pending data (eg: inconsistent content-length)
3322 bo_del(buf, buf->o);
3323
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003324 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003325 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003326
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003327 h2s->flags |= H2_SF_ES_SENT;
3328 }
3329
3330 end:
3331 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);
3332 return total;
3333}
3334
Willy Tarreau62f52692017-10-08 23:01:42 +02003335/* Called from the upper layer, to send data */
3336static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3337{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003338 struct h2s *h2s = cs->ctx;
3339 int total = 0;
3340
Willy Tarreauc4312d32017-11-07 12:01:53 +01003341 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3342 h2s->flags |= H2_SF_OUTGOING_DATA;
3343
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003344 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3345 if (h2s->res.state < HTTP_MSG_BODY) {
3346 total += h2s_frt_make_resp_headers(h2s, buf);
3347
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003348 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003349 break;
3350
3351 if (h2s->flags & H2_SF_BLK_ANY)
3352 break;
3353 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003354 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3355 total += h2s_frt_make_resp_data(h2s, buf);
3356
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003357 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 break;
3359
3360 if (h2s->flags & H2_SF_BLK_ANY)
3361 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003362 }
3363 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3364 /* consume the trailers if any (we don't forward them for now) */
3365 int count = h1_measure_trailers(buf);
3366
3367 if (unlikely(count <= 0)) {
3368 if (count < 0)
3369 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3370 break;
3371 }
3372 total += count;
3373 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003374
3375 // trim any possibly pending data (eg: extra CR-LF, ...)
3376 bo_del(buf, buf->o);
3377
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003378 h2s->res.state = HTTP_MSG_DONE;
3379 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003380 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003381 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003382 cs->flags |= CS_FL_ERROR;
3383 break;
3384 }
3385 }
3386
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003387 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003388 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003389 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003390 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003391 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003392 }
3393
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003394 if (h2s->flags & H2_SF_BLK_SFCTL) {
3395 /* stream flow control, quit the list */
3396 LIST_DEL(&h2s->list);
3397 LIST_INIT(&h2s->list);
3398 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003399 else if (LIST_ISEMPTY(&h2s->list)) {
3400 if (h2s->flags & H2_SF_BLK_MFCTL)
3401 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3402 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3403 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3404 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003406 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003407}
3408
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003409/* for debugging with CLI's "show fd" command */
3410static void h2_show_fd(struct chunk *msg, struct connection *conn)
3411{
3412 struct h2c *h2c = conn->mux_ctx;
3413 struct h2s *h2s;
3414 struct eb32_node *node;
3415 int fctl_cnt = 0;
3416 int send_cnt = 0;
3417 int tree_cnt = 0;
3418 int orph_cnt = 0;
3419
3420 if (!h2c)
3421 return;
3422
3423 list_for_each_entry(h2s, &h2c->fctl_list, list)
3424 fctl_cnt++;
3425
3426 list_for_each_entry(h2s, &h2c->send_list, list)
3427 send_cnt++;
3428
3429 node = eb32_first(&h2c->streams_by_id);
3430 while (node) {
3431 h2s = container_of(node, struct h2s, by_id);
3432 tree_cnt++;
3433 if (!h2s->cs)
3434 orph_cnt++;
3435 node = eb32_next(node);
3436 }
3437
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003438 chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d",
3439 h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt);
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003440}
Willy Tarreau62f52692017-10-08 23:01:42 +02003441
3442/*******************************************************/
3443/* functions below are dedicated to the config parsers */
3444/*******************************************************/
3445
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003446/* config parser for global "tune.h2.header-table-size" */
3447static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3448 struct proxy *defpx, const char *file, int line,
3449 char **err)
3450{
3451 if (too_many_args(1, args, err, NULL))
3452 return -1;
3453
3454 h2_settings_header_table_size = atoi(args[1]);
3455 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3456 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3457 return -1;
3458 }
3459 return 0;
3460}
Willy Tarreau62f52692017-10-08 23:01:42 +02003461
Willy Tarreaue6baec02017-07-27 11:45:11 +02003462/* config parser for global "tune.h2.initial-window-size" */
3463static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3464 struct proxy *defpx, const char *file, int line,
3465 char **err)
3466{
3467 if (too_many_args(1, args, err, NULL))
3468 return -1;
3469
3470 h2_settings_initial_window_size = atoi(args[1]);
3471 if (h2_settings_initial_window_size < 0) {
3472 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3473 return -1;
3474 }
3475 return 0;
3476}
3477
Willy Tarreau5242ef82017-07-27 11:47:28 +02003478/* config parser for global "tune.h2.max-concurrent-streams" */
3479static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3480 struct proxy *defpx, const char *file, int line,
3481 char **err)
3482{
3483 if (too_many_args(1, args, err, NULL))
3484 return -1;
3485
3486 h2_settings_max_concurrent_streams = atoi(args[1]);
3487 if (h2_settings_max_concurrent_streams < 0) {
3488 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3489 return -1;
3490 }
3491 return 0;
3492}
3493
Willy Tarreau62f52692017-10-08 23:01:42 +02003494
3495/****************************************/
3496/* MUX initialization and instanciation */
3497/***************************************/
3498
3499/* The mux operations */
3500const struct mux_ops h2_ops = {
3501 .init = h2_init,
3502 .recv = h2_recv,
3503 .send = h2_send,
3504 .wake = h2_wake,
3505 .update_poll = h2_update_poll,
3506 .rcv_buf = h2_rcv_buf,
3507 .snd_buf = h2_snd_buf,
3508 .attach = h2_attach,
3509 .detach = h2_detach,
3510 .shutr = h2_shutr,
3511 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003512 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003513 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003514 .name = "H2",
3515};
3516
3517/* ALPN selection : this mux registers ALPN tolen "h2" */
3518static struct alpn_mux_list alpn_mux_h2 =
3519 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3520
3521/* config keyword parsers */
3522static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003523 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003524 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003525 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003526 { 0, NULL, NULL }
3527}};
3528
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003529static void __h2_deinit(void)
3530{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003531 pool_destroy(pool_head_h2s);
3532 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003533}
3534
Willy Tarreau62f52692017-10-08 23:01:42 +02003535__attribute__((constructor))
3536static void __h2_init(void)
3537{
3538 alpn_register_mux(&alpn_mux_h2);
3539 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003540 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003541 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3542 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003543}