blob: 4a39b51044919c06cbe4c54b01a0630380daebff [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 Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
93 struct buffer *dbuf; /* demux buffer */
94
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
104 struct buffer *mbuf; /* mux buffer */
105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123};
124
Willy Tarreau18312642017-10-11 07:57:07 +0200125/* H2 stream state, in h2s->st */
126enum h2_ss {
127 H2_SS_IDLE = 0, // idle
128 H2_SS_RLOC, // reserved(local)
129 H2_SS_RREM, // reserved(remote)
130 H2_SS_OPEN, // open
131 H2_SS_HREM, // half-closed(remote)
132 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200133 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200134 H2_SS_CLOSED, // closed
135 H2_SS_ENTRIES // must be last
136} __attribute__((packed));
137
138/* HTTP/2 stream flags (32 bit), in h2s->flags */
139#define H2_SF_NONE 0x00000000
140#define H2_SF_ES_RCVD 0x00000001
141#define H2_SF_ES_SENT 0x00000002
142
143#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
144#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
145
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200146/* stream flags indicating the reason the stream is blocked */
147#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
148#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
149#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
150#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
151#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
152
Willy Tarreau454f9052017-10-26 19:40:35 +0200153/* stream flags indicating how data is supposed to be sent */
154#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
155#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
156
157/* step we're currently in when sending chunks. This is needed because we may
158 * have to transfer chunks as large as a full buffer so there's no room left
159 * for size nor crlf around.
160 */
161#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
162#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
163#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
164
165#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
166
Willy Tarreau67434202017-11-06 20:20:51 +0100167#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100168#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100169
Willy Tarreau18312642017-10-11 07:57:07 +0200170/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
171 * it is being processed in the internal HTTP representation (H1 for now).
172 */
173struct h2s {
174 struct conn_stream *cs;
175 struct h2c *h2c;
176 struct h1m req, res; /* request and response parser state for H1 */
177 struct eb32_node by_id; /* place in h2c's streams_by_id */
178 struct list list; /* position in active/blocked lists if blocked>0 */
179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
184};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200185
Willy Tarreauc6405142017-09-21 20:23:50 +0200186/* descriptor for an h2 frame header */
187struct h2_fh {
188 uint32_t len; /* length, host order, 24 bits */
189 uint32_t sid; /* stream id, host order, 31 bits */
190 uint8_t ft; /* frame type */
191 uint8_t ff; /* frame flags */
192};
193
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200194/* a few settings from the global section */
195static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200196static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200197static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198
Willy Tarreau2a856182017-05-16 15:20:39 +0200199/* a dmumy closed stream */
200static const struct h2s *h2_closed_stream = &(const struct h2s){
201 .cs = NULL,
202 .h2c = NULL,
203 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100204 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100205 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200206 .id = 0,
207};
208
209/* and a dummy idle stream for use with any unannounced stream */
210static const struct h2s *h2_idle_stream = &(const struct h2s){
211 .cs = NULL,
212 .h2c = NULL,
213 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100214 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200215 .id = 0,
216};
217
Olivier Houchard9f6af332018-05-25 14:04:04 +0200218static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200220/*****************************************************/
221/* functions below are for dynamic buffer management */
222/*****************************************************/
223
Willy Tarreau315d8072017-12-10 22:17:57 +0100224/* indicates whether or not the we may call the h2_recv() function to attempt
225 * to receive data into the buffer and/or demux pending data. The condition is
226 * a bit complex due to some API limits for now. The rules are the following :
227 * - if an error or a shutdown was detected on the connection and the buffer
228 * is empty, we must not attempt to receive
229 * - if the demux buf failed to be allocated, we must not try to receive and
230 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100231 * - if no flag indicates a blocking condition, we may attempt to receive,
232 * regardless of whether the demux buffer is full or not, so that only
233 * de demux part decides whether or not to block. This is needed because
234 * the connection API indeed prevents us from re-enabling receipt that is
235 * already enabled in a polled state, so we must always immediately stop
236 * as soon as the demux can't proceed so as never to hit an end of read
237 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100238 * - otherwise must may not attempt
239 */
240static inline int h2_recv_allowed(const struct h2c *h2c)
241{
242 if (h2c->dbuf->i == 0 &&
243 (h2c->st0 >= H2_CS_ERROR ||
244 h2c->conn->flags & CO_FL_ERROR ||
245 conn_xprt_read0_pending(h2c->conn)))
246 return 0;
247
248 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100249 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 return 1;
251
252 return 0;
253}
254
Willy Tarreauf2101912018-07-19 10:11:38 +0200255/* returns true if the connection has too many conn_streams attached */
256static inline int h2_has_too_many_cs(const struct h2c *h2c)
257{
258 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
259}
260
Willy Tarreau44e973f2018-03-01 17:49:30 +0100261/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
262 * flags are used to figure what buffer was requested. It returns 1 if the
263 * allocation succeeds, in which case the connection is woken up, or 0 if it's
264 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200265 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100266static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200267{
268 struct h2c *h2c = target;
269
Willy Tarreau44e973f2018-03-01 17:49:30 +0100270 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200271 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100272 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200273 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200274 return 1;
275 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
278 h2c->flags &= ~H2_CF_MUX_MALLOC;
279 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
280 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200281
282 if (h2c->flags & H2_CF_DEM_MROOM) {
283 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100284 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200285 conn_xprt_want_recv(h2c->conn);
286 }
Willy Tarreau14398122017-09-22 14:26:04 +0200287 return 1;
288 }
289 return 0;
290}
291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200293{
294 struct buffer *buf = NULL;
295
Willy Tarreau44e973f2018-03-01 17:49:30 +0100296 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
297 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
298 h2c->buf_wait.target = h2c;
299 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100300 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100301 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100302 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200303 __conn_xprt_stop_recv(h2c->conn);
304 }
305 return buf;
306}
307
Willy Tarreau44e973f2018-03-01 17:49:30 +0100308static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200309{
Willy Tarreau44e973f2018-03-01 17:49:30 +0100310 if ((*bptr)->size) {
311 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200312 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200313 }
314}
315
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200316
Willy Tarreau62f52692017-10-08 23:01:42 +0200317/*****************************************************************/
318/* functions below are dedicated to the mux setup and management */
319/*****************************************************************/
320
Willy Tarreau32218eb2017-09-22 08:07:25 +0200321/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
322static int h2c_frt_init(struct connection *conn)
323{
324 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100325 struct task *t = NULL;
326 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200327
Willy Tarreaubafbe012017-11-24 17:34:44 +0100328 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200329 if (!h2c)
330 goto fail;
331
Willy Tarreau3f133572017-10-31 19:21:06 +0100332
Willy Tarreau599391a2017-11-24 10:16:00 +0100333 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
334 if (tick_isset(sess->fe->timeout.clientfin))
335 h2c->shut_timeout = sess->fe->timeout.clientfin;
336
Willy Tarreau33400292017-11-05 11:23:40 +0100337 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100338 if (tick_isset(h2c->timeout)) {
339 t = task_new(tid_bit);
340 if (!t)
341 goto fail;
342
343 h2c->task = t;
344 t->process = h2_timeout_task;
345 t->context = h2c;
346 t->expire = tick_add(now_ms, h2c->timeout);
347 }
Willy Tarreauea392822017-10-31 10:02:25 +0100348
Willy Tarreau32218eb2017-09-22 08:07:25 +0200349 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
350 if (!h2c->ddht)
351 goto fail;
352
353 /* Initialise the context. */
354 h2c->st0 = H2_CS_PREFACE;
355 h2c->conn = conn;
356 h2c->max_id = -1;
357 h2c->errcode = H2_ERR_NO_ERROR;
358 h2c->flags = H2_CF_NONE;
359 h2c->rcvd_c = 0;
360 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100361 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200362 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363
364 h2c->dbuf = &buf_empty;
365 h2c->dsi = -1;
366 h2c->msi = -1;
367 h2c->last_sid = -1;
368
369 h2c->mbuf = &buf_empty;
370 h2c->miw = 65535; /* mux initial window size */
371 h2c->mws = 65535; /* mux window size */
372 h2c->mfs = 16384; /* initial max frame size */
373 h2c->streams_by_id = EB_ROOT_UNIQUE;
374 LIST_INIT(&h2c->send_list);
375 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100376 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200377 conn->mux_ctx = h2c;
378
Willy Tarreau3f133572017-10-31 19:21:06 +0100379 if (t)
380 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100382
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 /* mux->wake will be called soon to complete the operation */
384 return 0;
385 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100386 if (t)
387 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100388 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389 return -1;
390}
391
Willy Tarreau62f52692017-10-08 23:01:42 +0200392/* Initialize the mux once it's attached. For outgoing connections, the context
393 * is already initialized before installing the mux, so we detect incoming
394 * connections from the fact that the context is still NULL. Returns < 0 on
395 * error.
396 */
397static int h2_init(struct connection *conn)
398{
399 if (conn->mux_ctx) {
400 /* we don't support outgoing connections for now */
401 return -1;
402 }
403
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200405}
406
Willy Tarreau2373acc2017-10-12 17:35:14 +0200407/* returns the stream associated with id <id> or NULL if not found */
408static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
409{
410 struct eb32_node *node;
411
Willy Tarreau2a856182017-05-16 15:20:39 +0200412 if (id > h2c->max_id)
413 return (struct h2s *)h2_idle_stream;
414
Willy Tarreau2373acc2017-10-12 17:35:14 +0200415 node = eb32_lookup(&h2c->streams_by_id, id);
416 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200417 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200418
419 return container_of(node, struct h2s, by_id);
420}
421
Willy Tarreau62f52692017-10-08 23:01:42 +0200422/* release function for a connection. This one should be called to free all
423 * resources allocated to the mux.
424 */
425static void h2_release(struct connection *conn)
426{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200427 struct h2c *h2c = conn->mux_ctx;
428
429 LIST_DEL(&conn->list);
430
431 if (h2c) {
432 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200433
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100434 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100435 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200437
Willy Tarreau44e973f2018-03-01 17:49:30 +0100438 h2_release_buf(h2c, &h2c->dbuf);
439 h2_release_buf(h2c, &h2c->mbuf);
440
Willy Tarreauea392822017-10-31 10:02:25 +0100441 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200442 h2c->task->context = NULL;
443 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100444 h2c->task = NULL;
445 }
446
Willy Tarreaubafbe012017-11-24 17:34:44 +0100447 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448 }
449
450 conn->mux = NULL;
451 conn->mux_ctx = NULL;
452
453 conn_stop_tracking(conn);
454 conn_full_close(conn);
455 if (conn->destroy_cb)
456 conn->destroy_cb(conn);
457 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200458}
459
460
Willy Tarreau71681172017-10-23 14:39:06 +0200461/******************************************************/
462/* functions below are for the H2 protocol processing */
463/******************************************************/
464
465/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100466static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200467{
468 return h2s ? h2s->id : 0;
469}
470
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200471/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100472static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200473{
474 if (h2c->msi < 0)
475 return 0;
476
477 if (h2c->msi == h2s_id(h2s))
478 return 0;
479
480 return 1;
481}
482
Willy Tarreau741d6df2017-10-17 08:00:59 +0200483/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100484static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200485{
486 h2c->errcode = err;
487 h2c->st0 = H2_CS_ERROR;
488}
489
Willy Tarreau2e43f082017-10-17 08:03:59 +0200490/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100491static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200492{
493 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
494 h2s->errcode = err;
495 h2s->st = H2_SS_ERROR;
496 if (h2s->cs)
497 h2s->cs->flags |= CS_FL_ERROR;
498 }
499}
500
Willy Tarreaue4820742017-07-27 13:37:23 +0200501/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100502static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200503{
504 uint8_t *out = frame;
505
506 *out = len >> 16;
507 write_n16(out + 1, len);
508}
509
Willy Tarreau54c15062017-10-10 17:10:03 +0200510/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
511 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
512 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200513 * available in the buffer's input prior to calling this function. The buffer
514 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200515 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100516static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200517 const struct buffer *b, int o)
518{
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200519 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b->data);
Willy Tarreau54c15062017-10-10 17:10:03 +0200520}
521
Willy Tarreau1f094672017-11-20 21:27:45 +0100522static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200523{
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200524 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b->data);
Willy Tarreau54c15062017-10-10 17:10:03 +0200525}
526
Willy Tarreau1f094672017-11-20 21:27:45 +0100527static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200528{
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200529 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b->data);
Willy Tarreau54c15062017-10-10 17:10:03 +0200530}
531
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200533{
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200534 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b->data);
Willy Tarreau54c15062017-10-10 17:10:03 +0200535}
536
537
Willy Tarreau715d5312017-07-11 15:20:24 +0200538/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
539 * is not obvious. It turns out that H2 headers are neither aligned nor do they
540 * use regular sizes. And to add to the trouble, the buffer may wrap so each
541 * byte read must be checked. The header is formed like this :
542 *
543 * b0 b1 b2 b3 b4 b5..b8
544 * +----------+---------+--------+----+----+----------------------+
545 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
546 * +----------+---------+--------+----+----+----------------------+
547 *
548 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
549 * we get the sid properly aligned and ordered, and 16 bits of len properly
550 * ordered as well. The type and flags can be extracted using bit shifts from
551 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200552 * Returns zero if some bytes are missing, otherwise non-zero on success. The
553 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200554 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100555static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200556{
557 uint64_t w;
558
559 if (b->i < 9)
560 return 0;
561
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200562 w = h2_get_n64(b, 1);
Willy Tarreau715d5312017-07-11 15:20:24 +0200563 h->len = *b->p << 16;
564 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
565 h->ff = w >> 32;
566 h->ft = w >> 40;
567 h->len += w >> 48;
568 return 1;
569}
570
571/* skip the next 9 bytes corresponding to the frame header possibly parsed by
572 * h2_peek_frame_hdr() above.
573 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100574static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200575{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200576 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200577}
578
579/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200581{
582 int ret;
583
584 ret = h2_peek_frame_hdr(b, h);
585 if (ret > 0)
586 h2_skip_frame_hdr(b);
587 return ret;
588}
589
Willy Tarreau00dd0782018-03-01 16:31:34 +0100590/* marks stream <h2s> as CLOSED and decrement the number of active streams for
591 * its connection if the stream was not yet closed. Please use this exclusively
592 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100593 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100594static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100595{
596 if (h2s->st != H2_SS_CLOSED)
597 h2s->h2c->nb_streams--;
598 h2s->st = H2_SS_CLOSED;
599}
600
Willy Tarreau71049cc2018-03-28 13:56:39 +0200601/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
602static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100603{
604 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200605 LIST_DEL(&h2s->list);
606 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100607 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100608 pool_free(pool_head_h2s, h2s);
609}
610
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200611/* creates a new stream <id> on the h2c connection and returns it, or NULL in
612 * case of memory allocation error.
613 */
614static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
615{
616 struct conn_stream *cs;
617 struct h2s *h2s;
618
Willy Tarreaubafbe012017-11-24 17:34:44 +0100619 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200620 if (!h2s)
621 goto out;
622
623 h2s->h2c = h2c;
624 h2s->mws = h2c->miw;
625 h2s->flags = H2_SF_NONE;
626 h2s->errcode = H2_ERR_NO_ERROR;
627 h2s->st = H2_SS_IDLE;
628 h1m_init(&h2s->req);
629 h1m_init(&h2s->res);
630 h2s->by_id.key = h2s->id = id;
631 h2c->max_id = id;
632 LIST_INIT(&h2s->list);
633
634 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100635 h2c->nb_streams++;
636 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
637 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200638
639 cs = cs_new(h2c->conn);
640 if (!cs)
641 goto out_close;
642
643 h2s->cs = cs;
644 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200645 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646
647 if (stream_create_from_cs(cs) < 0)
648 goto out_free_cs;
649
650 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200651 if (h2_has_too_many_cs(h2c))
652 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200653 return h2s;
654
655 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200656 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200657 cs_free(cs);
658 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200659 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200660 h2s = NULL;
661 out:
662 return h2s;
663}
664
Willy Tarreaube5b7152017-09-25 16:25:39 +0200665/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
666 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
667 * the various settings codes.
668 */
669static int h2c_snd_settings(struct h2c *h2c)
670{
671 struct buffer *res;
672 char buf_data[100]; // enough for 15 settings
673 struct chunk buf;
674 int ret;
675
676 if (h2c_mux_busy(h2c, NULL)) {
677 h2c->flags |= H2_CF_DEM_MBUSY;
678 return 0;
679 }
680
Willy Tarreau44e973f2018-03-01 17:49:30 +0100681 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200682 if (!res) {
683 h2c->flags |= H2_CF_MUX_MALLOC;
684 h2c->flags |= H2_CF_DEM_MROOM;
685 return 0;
686 }
687
688 chunk_init(&buf, buf_data, sizeof(buf_data));
689 chunk_memcpy(&buf,
690 "\x00\x00\x00" /* length : 0 for now */
691 "\x04\x00" /* type : 4 (settings), flags : 0 */
692 "\x00\x00\x00\x00", /* stream ID : 0 */
693 9);
694
695 if (h2_settings_header_table_size != 4096) {
696 char str[6] = "\x00\x01"; /* header_table_size */
697
698 write_n32(str + 2, h2_settings_header_table_size);
699 chunk_memcat(&buf, str, 6);
700 }
701
702 if (h2_settings_initial_window_size != 65535) {
703 char str[6] = "\x00\x04"; /* initial_window_size */
704
705 write_n32(str + 2, h2_settings_initial_window_size);
706 chunk_memcat(&buf, str, 6);
707 }
708
709 if (h2_settings_max_concurrent_streams != 0) {
710 char str[6] = "\x00\x03"; /* max_concurrent_streams */
711
712 /* Note: 0 means "unlimited" for haproxy's config but not for
713 * the protocol, so never send this value!
714 */
715 write_n32(str + 2, h2_settings_max_concurrent_streams);
716 chunk_memcat(&buf, str, 6);
717 }
718
719 if (global.tune.bufsize != 16384) {
720 char str[6] = "\x00\x05"; /* max_frame_size */
721
722 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
723 * match bufsize - rewrite size, but at the moment it seems
724 * that clients don't take care of it.
725 */
726 write_n32(str + 2, global.tune.bufsize);
727 chunk_memcat(&buf, str, 6);
728 }
729
730 h2_set_frame_size(buf.str, buf.len - 9);
731 ret = bo_istput(res, ist2(buf.str, buf.len));
732 if (unlikely(ret <= 0)) {
733 if (!ret) {
734 h2c->flags |= H2_CF_MUX_MFULL;
735 h2c->flags |= H2_CF_DEM_MROOM;
736 return 0;
737 }
738 else {
739 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
740 return 0;
741 }
742 }
743 return ret;
744}
745
Willy Tarreau52eed752017-09-22 15:05:09 +0200746/* Try to receive a connection preface, then upon success try to send our
747 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
748 * missing data. It may return an error in h2c.
749 */
750static int h2c_frt_recv_preface(struct h2c *h2c)
751{
752 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200753 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200754
755 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
756
757 if (unlikely(ret1 <= 0)) {
758 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
759 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
760 return 0;
761 }
762
Willy Tarreaube5b7152017-09-25 16:25:39 +0200763 ret2 = h2c_snd_settings(h2c);
764 if (ret2 > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200765 b_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200766
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200768}
769
Willy Tarreau081d4722017-05-16 21:51:05 +0200770/* try to send a GOAWAY frame on the connection to report an error or a graceful
771 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
772 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
773 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
774 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
775 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
776 * on unrecoverable failure. It will not attempt to send one again in this last
777 * case so that it is safe to use h2c_error() to report such errors.
778 */
779static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
780{
781 struct buffer *res;
782 char str[17];
783 int ret;
784
785 if (h2c->flags & H2_CF_GOAWAY_FAILED)
786 return 1; // claim that it worked
787
788 if (h2c_mux_busy(h2c, h2s)) {
789 if (h2s)
790 h2s->flags |= H2_SF_BLK_MBUSY;
791 else
792 h2c->flags |= H2_CF_DEM_MBUSY;
793 return 0;
794 }
795
Willy Tarreau44e973f2018-03-01 17:49:30 +0100796 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200797 if (!res) {
798 h2c->flags |= H2_CF_MUX_MALLOC;
799 if (h2s)
800 h2s->flags |= H2_SF_BLK_MROOM;
801 else
802 h2c->flags |= H2_CF_DEM_MROOM;
803 return 0;
804 }
805
806 /* len: 8, type: 7, flags: none, sid: 0 */
807 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
808
809 if (h2c->last_sid < 0)
810 h2c->last_sid = h2c->max_id;
811
812 write_n32(str + 9, h2c->last_sid);
813 write_n32(str + 13, h2c->errcode);
814 ret = bo_istput(res, ist2(str, 17));
815 if (unlikely(ret <= 0)) {
816 if (!ret) {
817 h2c->flags |= H2_CF_MUX_MFULL;
818 if (h2s)
819 h2s->flags |= H2_SF_BLK_MROOM;
820 else
821 h2c->flags |= H2_CF_DEM_MROOM;
822 return 0;
823 }
824 else {
825 /* we cannot report this error using GOAWAY, so we mark
826 * it and claim a success.
827 */
828 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
829 h2c->flags |= H2_CF_GOAWAY_FAILED;
830 return 1;
831 }
832 }
833 h2c->flags |= H2_CF_GOAWAY_SENT;
834 return ret;
835}
836
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100837/* Try to send an RST_STREAM frame on the connection for the indicated stream
838 * during mux operations. This stream must be valid and cannot be closed
839 * already. h2s->id will be used for the stream ID and h2s->errcode will be
840 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
841 * not yet.
842 *
843 * Returns > 0 on success or zero if nothing was done. In case of lack of room
844 * to write the message, it subscribes the stream to future notifications.
845 */
846static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
847{
848 struct buffer *res;
849 char str[13];
850 int ret;
851
852 if (!h2s || h2s->st == H2_SS_CLOSED)
853 return 1;
854
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100855 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
856 * RST_STREAM in response to a RST_STREAM frame.
857 */
858 if (h2c->dft == H2_FT_RST_STREAM) {
859 ret = 1;
860 goto ignore;
861 }
862
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100863 if (h2c_mux_busy(h2c, h2s)) {
864 h2s->flags |= H2_SF_BLK_MBUSY;
865 return 0;
866 }
867
Willy Tarreau44e973f2018-03-01 17:49:30 +0100868 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100869 if (!res) {
870 h2c->flags |= H2_CF_MUX_MALLOC;
871 h2s->flags |= H2_SF_BLK_MROOM;
872 return 0;
873 }
874
875 /* len: 4, type: 3, flags: none */
876 memcpy(str, "\x00\x00\x04\x03\x00", 5);
877 write_n32(str + 5, h2s->id);
878 write_n32(str + 9, h2s->errcode);
879 ret = bo_istput(res, ist2(str, 13));
880
881 if (unlikely(ret <= 0)) {
882 if (!ret) {
883 h2c->flags |= H2_CF_MUX_MFULL;
884 h2s->flags |= H2_SF_BLK_MROOM;
885 return 0;
886 }
887 else {
888 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
889 return 0;
890 }
891 }
892
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100893 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100894 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100895 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100896 return ret;
897}
898
899/* Try to send an RST_STREAM frame on the connection for the stream being
900 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
901 * error code unless the stream's state already is IDLE or CLOSED in which
902 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
903 * it was not yet.
904 *
905 * Returns > 0 on success or zero if nothing was done. In case of lack of room
906 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200907 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100908 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200909 */
910static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
911{
912 struct buffer *res;
913 char str[13];
914 int ret;
915
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100916 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
917 * RST_STREAM in response to a RST_STREAM frame.
918 */
919 if (h2c->dft == H2_FT_RST_STREAM) {
920 ret = 1;
921 goto ignore;
922 }
923
Willy Tarreau27a84c92017-10-17 08:10:17 +0200924 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100925 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200926 return 0;
927 }
928
Willy Tarreau44e973f2018-03-01 17:49:30 +0100929 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 if (!res) {
931 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100932 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200933 return 0;
934 }
935
936 /* len: 4, type: 3, flags: none */
937 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100938
Willy Tarreau27a84c92017-10-17 08:10:17 +0200939 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100940 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 h2s->errcode : H2_ERR_STREAM_CLOSED);
942 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943
Willy Tarreau27a84c92017-10-17 08:10:17 +0200944 if (unlikely(ret <= 0)) {
945 if (!ret) {
946 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100947 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200948 return 0;
949 }
950 else {
951 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
952 return 0;
953 }
954 }
955
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100956 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100957 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200958 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100959 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100960 }
961
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return ret;
963}
964
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100965/* try to send an empty DATA frame with the ES flag set to notify about the
966 * end of stream and match a shutdown(write). If an ES was already sent as
967 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
968 * on success or zero if nothing was done. In case of lack of room to write the
969 * message, it subscribes the requesting stream to future notifications.
970 */
971static int h2_send_empty_data_es(struct h2s *h2s)
972{
973 struct h2c *h2c = h2s->h2c;
974 struct buffer *res;
975 char str[9];
976 int ret;
977
Willy Tarreau721c9742017-11-07 11:05:42 +0100978 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100979 return 1;
980
981 if (h2c_mux_busy(h2c, h2s)) {
982 h2s->flags |= H2_SF_BLK_MBUSY;
983 return 0;
984 }
985
Willy Tarreau44e973f2018-03-01 17:49:30 +0100986 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100987 if (!res) {
988 h2c->flags |= H2_CF_MUX_MALLOC;
989 h2s->flags |= H2_SF_BLK_MROOM;
990 return 0;
991 }
992
993 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
994 memcpy(str, "\x00\x00\x00\x00\x01", 5);
995 write_n32(str + 5, h2s->id);
996 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100997 if (likely(ret > 0)) {
998 h2s->flags |= H2_SF_ES_SENT;
999 }
1000 else if (!ret) {
1001 h2c->flags |= H2_CF_MUX_MFULL;
1002 h2s->flags |= H2_SF_BLK_MROOM;
1003 return 0;
1004 }
1005 else {
1006 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1007 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001008 }
1009 return ret;
1010}
1011
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001012/* wake the streams attached to the connection, whose id is greater than <last>,
1013 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1014 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1015 * stream's state is automatically updated accordingly.
1016 */
1017static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1018{
1019 struct eb32_node *node;
1020 struct h2s *h2s;
1021
1022 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1023 flags |= CS_FL_ERROR;
1024
1025 if (conn_xprt_read0_pending(h2c->conn))
1026 flags |= CS_FL_EOS;
1027
1028 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1029 while (node) {
1030 h2s = container_of(node, struct h2s, by_id);
1031 if (h2s->id <= last)
1032 break;
1033 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001034
1035 if (!h2s->cs) {
1036 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001037 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001038 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001039 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001040
1041 h2s->cs->flags |= flags;
1042 /* recv is used to force to detect CS_FL_EOS that wake()
1043 * doesn't handle in the stream int code.
1044 */
1045 h2s->cs->data_cb->recv(h2s->cs);
1046 h2s->cs->data_cb->wake(h2s->cs);
1047
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001048 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1049 h2s->st = H2_SS_ERROR;
1050 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1051 h2s->st = H2_SS_HREM;
1052 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001053 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001054 }
1055}
1056
Willy Tarreau3421aba2017-07-27 15:41:03 +02001057/* Increase all streams' outgoing window size by the difference passed in
1058 * argument. This is needed upon receipt of the settings frame if the initial
1059 * window size is different. The difference may be negative and the resulting
1060 * window size as well, for the time it takes to receive some window updates.
1061 */
1062static void h2c_update_all_ws(struct h2c *h2c, int diff)
1063{
1064 struct h2s *h2s;
1065 struct eb32_node *node;
1066
1067 if (!diff)
1068 return;
1069
1070 node = eb32_first(&h2c->streams_by_id);
1071 while (node) {
1072 h2s = container_of(node, struct h2s, by_id);
1073 h2s->mws += diff;
1074 node = eb32_next(node);
1075 }
1076}
1077
1078/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1079 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1080 * return an error in h2c. Described in RFC7540#6.5.
1081 */
1082static int h2c_handle_settings(struct h2c *h2c)
1083{
1084 unsigned int offset;
1085 int error;
1086
1087 if (h2c->dff & H2_F_SETTINGS_ACK) {
1088 if (h2c->dfl) {
1089 error = H2_ERR_FRAME_SIZE_ERROR;
1090 goto fail;
1091 }
1092 return 1;
1093 }
1094
1095 if (h2c->dsi != 0) {
1096 error = H2_ERR_PROTOCOL_ERROR;
1097 goto fail;
1098 }
1099
1100 if (h2c->dfl % 6) {
1101 error = H2_ERR_FRAME_SIZE_ERROR;
1102 goto fail;
1103 }
1104
1105 /* that's the limit we can process */
1106 if (h2c->dfl > global.tune.bufsize) {
1107 error = H2_ERR_FRAME_SIZE_ERROR;
1108 goto fail;
1109 }
1110
1111 /* process full frame only */
1112 if (h2c->dbuf->i < h2c->dfl)
1113 return 0;
1114
1115 /* parse the frame */
1116 for (offset = 0; offset < h2c->dfl; offset += 6) {
1117 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1118 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1119
1120 switch (type) {
1121 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1122 /* we need to update all existing streams with the
1123 * difference from the previous iws.
1124 */
1125 if (arg < 0) { // RFC7540#6.5.2
1126 error = H2_ERR_FLOW_CONTROL_ERROR;
1127 goto fail;
1128 }
1129 h2c_update_all_ws(h2c, arg - h2c->miw);
1130 h2c->miw = arg;
1131 break;
1132 case H2_SETTINGS_MAX_FRAME_SIZE:
1133 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1134 error = H2_ERR_PROTOCOL_ERROR;
1135 goto fail;
1136 }
1137 h2c->mfs = arg;
1138 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001139 case H2_SETTINGS_ENABLE_PUSH:
1140 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1141 error = H2_ERR_PROTOCOL_ERROR;
1142 goto fail;
1143 }
1144 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001145 }
1146 }
1147
1148 /* need to ACK this frame now */
1149 h2c->st0 = H2_CS_FRAME_A;
1150 return 1;
1151 fail:
1152 h2c_error(h2c, error);
1153 return 0;
1154}
1155
1156/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1157 * success or one of the h2_status values.
1158 */
1159static int h2c_ack_settings(struct h2c *h2c)
1160{
1161 struct buffer *res;
1162 char str[9];
1163 int ret = -1;
1164
1165 if (h2c_mux_busy(h2c, NULL)) {
1166 h2c->flags |= H2_CF_DEM_MBUSY;
1167 return 0;
1168 }
1169
Willy Tarreau44e973f2018-03-01 17:49:30 +01001170 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001171 if (!res) {
1172 h2c->flags |= H2_CF_MUX_MALLOC;
1173 h2c->flags |= H2_CF_DEM_MROOM;
1174 return 0;
1175 }
1176
1177 memcpy(str,
1178 "\x00\x00\x00" /* length : 0 (no data) */
1179 "\x04" "\x01" /* type : 4, flags : ACK */
1180 "\x00\x00\x00\x00" /* stream ID */, 9);
1181
1182 ret = bo_istput(res, ist2(str, 9));
1183 if (unlikely(ret <= 0)) {
1184 if (!ret) {
1185 h2c->flags |= H2_CF_MUX_MFULL;
1186 h2c->flags |= H2_CF_DEM_MROOM;
1187 return 0;
1188 }
1189 else {
1190 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1191 return 0;
1192 }
1193 }
1194 return ret;
1195}
1196
Willy Tarreaucf68c782017-10-10 17:11:41 +02001197/* processes a PING frame and schedules an ACK if needed. The caller must pass
1198 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1199 * missing data. It may return an error in h2c.
1200 */
1201static int h2c_handle_ping(struct h2c *h2c)
1202{
1203 /* frame length must be exactly 8 */
1204 if (h2c->dfl != 8) {
1205 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1206 return 0;
1207 }
1208
1209 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001210 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001211 h2c->st0 = H2_CS_FRAME_A;
1212 return 1;
1213}
1214
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001215/* Try to send a window update for stream id <sid> and value <increment>.
1216 * Returns > 0 on success or zero on missing room or failure. It may return an
1217 * error in h2c.
1218 */
1219static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1220{
1221 struct buffer *res;
1222 char str[13];
1223 int ret = -1;
1224
1225 if (h2c_mux_busy(h2c, NULL)) {
1226 h2c->flags |= H2_CF_DEM_MBUSY;
1227 return 0;
1228 }
1229
Willy Tarreau44e973f2018-03-01 17:49:30 +01001230 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001231 if (!res) {
1232 h2c->flags |= H2_CF_MUX_MALLOC;
1233 h2c->flags |= H2_CF_DEM_MROOM;
1234 return 0;
1235 }
1236
1237 /* length: 4, type: 8, flags: none */
1238 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1239 write_n32(str + 5, sid);
1240 write_n32(str + 9, increment);
1241
1242 ret = bo_istput(res, ist2(str, 13));
1243
1244 if (unlikely(ret <= 0)) {
1245 if (!ret) {
1246 h2c->flags |= H2_CF_MUX_MFULL;
1247 h2c->flags |= H2_CF_DEM_MROOM;
1248 return 0;
1249 }
1250 else {
1251 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1252 return 0;
1253 }
1254 }
1255 return ret;
1256}
1257
1258/* try to send pending window update for the connection. It's safe to call it
1259 * with no pending updates. Returns > 0 on success or zero on missing room or
1260 * failure. It may return an error in h2c.
1261 */
1262static int h2c_send_conn_wu(struct h2c *h2c)
1263{
1264 int ret = 1;
1265
1266 if (h2c->rcvd_c <= 0)
1267 return 1;
1268
1269 /* send WU for the connection */
1270 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1271 if (ret > 0)
1272 h2c->rcvd_c = 0;
1273
1274 return ret;
1275}
1276
1277/* try to send pending window update for the current dmux stream. It's safe to
1278 * call it with no pending updates. Returns > 0 on success or zero on missing
1279 * room or failure. It may return an error in h2c.
1280 */
1281static int h2c_send_strm_wu(struct h2c *h2c)
1282{
1283 int ret = 1;
1284
1285 if (h2c->rcvd_s <= 0)
1286 return 1;
1287
1288 /* send WU for the stream */
1289 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1290 if (ret > 0)
1291 h2c->rcvd_s = 0;
1292
1293 return ret;
1294}
1295
Willy Tarreaucf68c782017-10-10 17:11:41 +02001296/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1297 * success, 0 on missing data or one of the h2_status values.
1298 */
1299static int h2c_ack_ping(struct h2c *h2c)
1300{
1301 struct buffer *res;
1302 char str[17];
1303 int ret = -1;
1304
1305 if (h2c->dbuf->i < 8)
1306 return 0;
1307
1308 if (h2c_mux_busy(h2c, NULL)) {
1309 h2c->flags |= H2_CF_DEM_MBUSY;
1310 return 0;
1311 }
1312
Willy Tarreau44e973f2018-03-01 17:49:30 +01001313 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001314 if (!res) {
1315 h2c->flags |= H2_CF_MUX_MALLOC;
1316 h2c->flags |= H2_CF_DEM_MROOM;
1317 return 0;
1318 }
1319
1320 memcpy(str,
1321 "\x00\x00\x08" /* length : 8 (same payload) */
1322 "\x06" "\x01" /* type : 6, flags : ACK */
1323 "\x00\x00\x00\x00" /* stream ID */, 9);
1324
1325 /* copy the original payload */
1326 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1327
1328 ret = bo_istput(res, ist2(str, 17));
1329 if (unlikely(ret <= 0)) {
1330 if (!ret) {
1331 h2c->flags |= H2_CF_MUX_MFULL;
1332 h2c->flags |= H2_CF_DEM_MROOM;
1333 return 0;
1334 }
1335 else {
1336 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1337 return 0;
1338 }
1339 }
1340 return ret;
1341}
1342
Willy Tarreau26f95952017-07-27 17:18:30 +02001343/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1344 * Returns > 0 on success or zero on missing data. It may return an error in
1345 * h2c or h2s. Described in RFC7540#6.9.
1346 */
1347static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1348{
1349 int32_t inc;
1350 int error;
1351
1352 if (h2c->dfl != 4) {
1353 error = H2_ERR_FRAME_SIZE_ERROR;
1354 goto conn_err;
1355 }
1356
1357 /* process full frame only */
1358 if (h2c->dbuf->i < h2c->dfl)
1359 return 0;
1360
1361 inc = h2_get_n32(h2c->dbuf, 0);
1362
1363 if (h2c->dsi != 0) {
1364 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001365
1366 /* it's not an error to receive WU on a closed stream */
1367 if (h2s->st == H2_SS_CLOSED)
1368 return 1;
1369
1370 if (!inc) {
1371 error = H2_ERR_PROTOCOL_ERROR;
1372 goto strm_err;
1373 }
1374
1375 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1376 error = H2_ERR_FLOW_CONTROL_ERROR;
1377 goto strm_err;
1378 }
1379
1380 h2s->mws += inc;
1381 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1382 h2s->flags &= ~H2_SF_BLK_SFCTL;
1383 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1384 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1385 /* This stream wanted to send but could not due to its
1386 * own flow control. We can put it back into the send
1387 * list now, it will be handled upon next send() call.
1388 */
1389 LIST_ADDQ(&h2c->send_list, &h2s->list);
1390 }
1391 }
1392 }
1393 else {
1394 /* connection window update */
1395 if (!inc) {
1396 error = H2_ERR_PROTOCOL_ERROR;
1397 goto conn_err;
1398 }
1399
1400 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1401 error = H2_ERR_FLOW_CONTROL_ERROR;
1402 goto conn_err;
1403 }
1404
1405 h2c->mws += inc;
1406 }
1407
1408 return 1;
1409
1410 conn_err:
1411 h2c_error(h2c, error);
1412 return 0;
1413
1414 strm_err:
1415 if (h2s) {
1416 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001417 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001418 }
1419 else
1420 h2c_error(h2c, error);
1421 return 0;
1422}
1423
Willy Tarreaue96b0922017-10-30 00:28:29 +01001424/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1425 * the last ID. Returns > 0 on success or zero on missing data. It may return
1426 * an error in h2c. Described in RFC7540#6.8.
1427 */
1428static int h2c_handle_goaway(struct h2c *h2c)
1429{
1430 int error;
1431 int last;
1432
1433 if (h2c->dsi != 0) {
1434 error = H2_ERR_PROTOCOL_ERROR;
1435 goto conn_err;
1436 }
1437
1438 if (h2c->dfl < 8) {
1439 error = H2_ERR_FRAME_SIZE_ERROR;
1440 goto conn_err;
1441 }
1442
1443 /* process full frame only */
1444 if (h2c->dbuf->i < h2c->dfl)
1445 return 0;
1446
1447 last = h2_get_n32(h2c->dbuf, 0);
1448 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1449 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001450 if (h2c->last_sid < 0)
1451 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001452 return 1;
1453
1454 conn_err:
1455 h2c_error(h2c, error);
1456 return 0;
1457}
1458
Willy Tarreau92153fc2017-12-03 19:46:19 +01001459/* processes a PRIORITY frame, and either skips it or rejects if it is
1460 * invalid. Returns > 0 on success or zero on missing data. It may return
1461 * an error in h2c. Described in RFC7540#6.3.
1462 */
1463static int h2c_handle_priority(struct h2c *h2c)
1464{
1465 int error;
1466
1467 if (h2c->dsi == 0) {
1468 error = H2_ERR_PROTOCOL_ERROR;
1469 goto conn_err;
1470 }
1471
1472 if (h2c->dfl != 5) {
1473 error = H2_ERR_FRAME_SIZE_ERROR;
1474 goto conn_err;
1475 }
1476
1477 /* process full frame only */
1478 if (h2c->dbuf->i < h2c->dfl)
1479 return 0;
1480
1481 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1482 /* 7540#5.3 : can't depend on itself */
1483 error = H2_ERR_PROTOCOL_ERROR;
1484 goto conn_err;
1485 }
1486 return 1;
1487
1488 conn_err:
1489 h2c_error(h2c, error);
1490 return 0;
1491}
1492
Willy Tarreaucd234e92017-08-18 10:59:39 +02001493/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1494 * Returns > 0 on success or zero on missing data. It may return an error in
1495 * h2c. Described in RFC7540#6.4.
1496 */
1497static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1498{
1499 int error;
1500
1501 if (h2c->dsi == 0) {
1502 error = H2_ERR_PROTOCOL_ERROR;
1503 goto conn_err;
1504 }
1505
Willy Tarreaucd234e92017-08-18 10:59:39 +02001506 if (h2c->dfl != 4) {
1507 error = H2_ERR_FRAME_SIZE_ERROR;
1508 goto conn_err;
1509 }
1510
1511 /* process full frame only */
1512 if (h2c->dbuf->i < h2c->dfl)
1513 return 0;
1514
1515 /* late RST, already handled */
1516 if (h2s->st == H2_SS_CLOSED)
1517 return 1;
1518
1519 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001520 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001521
1522 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001523 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001524 /* recv is used to force to detect CS_FL_EOS that wake()
1525 * doesn't handle in the stream-int code.
1526 */
1527 h2s->cs->data_cb->recv(h2s->cs);
1528 h2s->cs->data_cb->wake(h2s->cs);
1529 }
1530
1531 h2s->flags |= H2_SF_RST_RCVD;
1532 return 1;
1533
1534 conn_err:
1535 h2c_error(h2c, error);
1536 return 0;
1537}
1538
Willy Tarreau13278b42017-10-13 19:23:14 +02001539/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1540 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1541 * errors here are reported as connection errors since it's impossible to
1542 * recover from such errors after the compression context has been altered.
1543 */
1544static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1545{
1546 int error;
1547
1548 if (!h2c->dfl) {
1549 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1550 goto strm_err;
1551 }
1552
1553 if (!h2c->dbuf->size)
1554 return 0; // empty buffer
1555
1556 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1557 return 0; // incomplete frame
1558
Willy Tarreauf2101912018-07-19 10:11:38 +02001559 if (h2c->flags & H2_CF_DEM_TOOMANY)
1560 return 0; // too many cs still present
1561
Willy Tarreau13278b42017-10-13 19:23:14 +02001562 /* now either the frame is complete or the buffer is complete */
1563 if (h2s->st != H2_SS_IDLE) {
1564 /* FIXME: stream already exists, this is only allowed for
1565 * trailers (not supported for now).
1566 */
1567 error = H2_ERR_PROTOCOL_ERROR;
1568 goto conn_err;
1569 }
1570 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1571 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1572 error = H2_ERR_PROTOCOL_ERROR;
1573 goto conn_err;
1574 }
1575
1576 h2s = h2c_stream_new(h2c, h2c->dsi);
1577 if (!h2s) {
1578 error = H2_ERR_INTERNAL_ERROR;
1579 goto conn_err;
1580 }
1581
1582 h2s->st = H2_SS_OPEN;
1583 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1584 h2s->st = H2_SS_HREM;
1585 h2s->flags |= H2_SF_ES_RCVD;
1586 }
1587
1588 /* call the upper layers to process the frame, then let the upper layer
1589 * notify the stream about any change.
1590 */
1591 h2s->cs->data_cb->recv(h2s->cs);
1592
1593 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1594 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1595 error = H2_ERR_INTERNAL_ERROR;
1596 goto conn_err;
1597 }
1598
Willy Tarreau8f650c32017-11-21 19:36:21 +01001599 if (h2c->st0 >= H2_CS_ERROR)
1600 return 0;
1601
Willy Tarreau721c9742017-11-07 11:05:42 +01001602 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001603 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001604 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001605 }
1606 else {
1607 /* update the max stream ID if the request is being processed */
1608 if (h2s->id > h2c->max_id)
1609 h2c->max_id = h2s->id;
1610 }
1611
1612 return 1;
1613
1614 conn_err:
1615 h2c_error(h2c, error);
1616 return 0;
1617
1618 strm_err:
1619 if (h2s) {
1620 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001621 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001622 }
1623 else
1624 h2c_error(h2c, error);
1625 return 0;
1626}
1627
Willy Tarreau454f9052017-10-26 19:40:35 +02001628/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1629 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1630 */
1631static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1632{
1633 int error;
1634
1635 /* note that empty DATA frames are perfectly valid and sometimes used
1636 * to signal an end of stream (with the ES flag).
1637 */
1638
1639 if (!h2c->dbuf->size && h2c->dfl)
1640 return 0; // empty buffer
1641
1642 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1643 return 0; // incomplete frame
1644
1645 /* now either the frame is complete or the buffer is complete */
1646
1647 if (!h2c->dsi) {
1648 /* RFC7540#6.1 */
1649 error = H2_ERR_PROTOCOL_ERROR;
1650 goto conn_err;
1651 }
1652
1653 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1654 /* RFC7540#6.1 */
1655 error = H2_ERR_STREAM_CLOSED;
1656 goto strm_err;
1657 }
1658
Willy Tarreau454f9052017-10-26 19:40:35 +02001659 /* call the upper layers to process the frame, then let the upper layer
1660 * notify the stream about any change.
1661 */
1662 if (!h2s->cs) {
1663 error = H2_ERR_STREAM_CLOSED;
1664 goto strm_err;
1665 }
1666
1667 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001668
Willy Tarreau454f9052017-10-26 19:40:35 +02001669 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1670 /* cs has just been destroyed, we have to kill h2s. */
1671 error = H2_ERR_STREAM_CLOSED;
1672 goto strm_err;
1673 }
1674
Willy Tarreau8f650c32017-11-21 19:36:21 +01001675 if (h2c->st0 >= H2_CS_ERROR)
1676 return 0;
1677
Willy Tarreau721c9742017-11-07 11:05:42 +01001678 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001679 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001680 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001681 }
1682
1683 /* check for completion : the callee will change this to FRAME_A or
1684 * FRAME_H once done.
1685 */
1686 if (h2c->st0 == H2_CS_FRAME_P)
1687 return 0;
1688
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001689
1690 /* last frame */
1691 if (h2c->dff & H2_F_DATA_END_STREAM) {
1692 h2s->st = H2_SS_HREM;
1693 h2s->flags |= H2_SF_ES_RCVD;
1694 }
1695
Willy Tarreau454f9052017-10-26 19:40:35 +02001696 return 1;
1697
1698 conn_err:
1699 h2c_error(h2c, error);
1700 return 0;
1701
1702 strm_err:
1703 if (h2s) {
1704 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001705 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001706 }
1707 else
1708 h2c_error(h2c, error);
1709 return 0;
1710}
1711
Willy Tarreaubc933932017-10-09 16:21:43 +02001712/* process Rx frames to be demultiplexed */
1713static void h2_process_demux(struct h2c *h2c)
1714{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001715 struct h2s *h2s;
1716
Willy Tarreau081d4722017-05-16 21:51:05 +02001717 if (h2c->st0 >= H2_CS_ERROR)
1718 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001719
1720 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1721 if (h2c->st0 == H2_CS_PREFACE) {
1722 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1723 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1724 if (h2c->st0 == H2_CS_ERROR)
1725 h2c->st0 = H2_CS_ERROR2;
1726 goto fail;
1727 }
1728
1729 h2c->max_id = 0;
1730 h2c->st0 = H2_CS_SETTINGS1;
1731 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001732
1733 if (h2c->st0 == H2_CS_SETTINGS1) {
1734 struct h2_fh hdr;
1735
1736 /* ensure that what is pending is a valid SETTINGS frame
1737 * without an ACK.
1738 */
1739 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1740 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1741 if (h2c->st0 == H2_CS_ERROR)
1742 h2c->st0 = H2_CS_ERROR2;
1743 goto fail;
1744 }
1745
1746 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1747 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1748 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1749 h2c->st0 = H2_CS_ERROR2;
1750 goto fail;
1751 }
1752
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001753 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001754 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1755 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1756 h2c->st0 = H2_CS_ERROR2;
1757 goto fail;
1758 }
1759
1760 /* that's OK, switch to FRAME_P to process it */
1761 h2c->dfl = hdr.len;
1762 h2c->dsi = hdr.sid;
1763 h2c->dft = hdr.ft;
1764 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001765 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001766 h2c->st0 = H2_CS_FRAME_P;
1767 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001768 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001769
1770 /* process as many incoming frames as possible below */
1771 while (h2c->dbuf->i) {
1772 int ret = 0;
1773
1774 if (h2c->st0 >= H2_CS_ERROR)
1775 break;
1776
1777 if (h2c->st0 == H2_CS_FRAME_H) {
1778 struct h2_fh hdr;
1779
1780 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1781 break;
1782
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001783 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001784 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1785 h2c->st0 = H2_CS_ERROR;
1786 break;
1787 }
1788
1789 h2c->dfl = hdr.len;
1790 h2c->dsi = hdr.sid;
1791 h2c->dft = hdr.ft;
1792 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001793 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001794 h2c->st0 = H2_CS_FRAME_P;
1795 h2_skip_frame_hdr(h2c->dbuf);
1796 }
1797
1798 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001799 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001800
Willy Tarreaud7901432017-12-29 11:34:40 +01001801 if (h2c->st0 == H2_CS_FRAME_E)
1802 goto strm_err;
1803
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001804 if (h2s->st == H2_SS_IDLE &&
1805 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1806 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1807 * this state MUST be treated as a connection error
1808 */
1809 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1810 h2c->st0 = H2_CS_ERROR;
1811 break;
1812 }
1813
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001814 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1815 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1816 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1817 * this state MUST be treated as a stream error
1818 */
1819 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001820 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001821 goto strm_err;
1822 }
1823
Willy Tarreauab837502017-12-27 15:07:30 +01001824 /* Below the management of frames received in closed state is a
1825 * bit hackish because the spec makes strong differences between
1826 * streams closed by receiving RST, sending RST, and seeing ES
1827 * in both directions. In addition to this, the creation of a
1828 * new stream reusing the identifier of a closed one will be
1829 * detected here. Given that we cannot keep track of all closed
1830 * streams forever, we consider that unknown closed streams were
1831 * closed on RST received, which allows us to respond with an
1832 * RST without breaking the connection (eg: to abort a transfer).
1833 * Some frames have to be silently ignored as well.
1834 */
1835 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1836 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1837 /* #5.1.1: The identifier of a newly
1838 * established stream MUST be numerically
1839 * greater than all streams that the initiating
1840 * endpoint has opened or reserved. This
1841 * governs streams that are opened using a
1842 * HEADERS frame and streams that are reserved
1843 * using PUSH_PROMISE. An endpoint that
1844 * receives an unexpected stream identifier
1845 * MUST respond with a connection error.
1846 */
1847 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1848 goto strm_err;
1849 }
1850
1851 if (h2s->flags & H2_SF_RST_RCVD) {
1852 /* RFC7540#5.1:closed: an endpoint that
1853 * receives any frame other than PRIORITY after
1854 * receiving a RST_STREAM MUST treat that as a
1855 * stream error of type STREAM_CLOSED.
1856 *
1857 * Note that old streams fall into this category
1858 * and will lead to an RST being sent.
1859 */
1860 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1861 h2c->st0 = H2_CS_FRAME_E;
1862 goto strm_err;
1863 }
1864
1865 /* RFC7540#5.1:closed: if this state is reached as a
1866 * result of sending a RST_STREAM frame, the peer that
1867 * receives the RST_STREAM might have already sent
1868 * frames on the stream that cannot be withdrawn. An
1869 * endpoint MUST ignore frames that it receives on
1870 * closed streams after it has sent a RST_STREAM
1871 * frame. An endpoint MAY choose to limit the period
1872 * over which it ignores frames and treat frames that
1873 * arrive after this time as being in error.
1874 */
1875 if (!(h2s->flags & H2_SF_RST_SENT)) {
1876 /* RFC7540#5.1:closed: any frame other than
1877 * PRIO/WU/RST in this state MUST be treated as
1878 * a connection error
1879 */
1880 if (h2c->dft != H2_FT_RST_STREAM &&
1881 h2c->dft != H2_FT_PRIORITY &&
1882 h2c->dft != H2_FT_WINDOW_UPDATE) {
1883 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1884 goto strm_err;
1885 }
1886 }
1887 }
1888
Willy Tarreauc0da1962017-10-30 18:38:00 +01001889#if 0
1890 // problem below: it is not possible to completely ignore such
1891 // streams as we need to maintain the compression state as well
1892 // and for this we need to completely process these frames (eg:
1893 // HEADERS frames) as well as counting DATA frames to emit
1894 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1895 // This is a typical case of layer violation where the
1896 // transported contents are critical to the connection's
1897 // validity and must be ignored at the same time :-(
1898
1899 /* graceful shutdown, ignore streams whose ID is higher than
1900 * the one advertised in GOAWAY. RFC7540#6.8.
1901 */
1902 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1903 ret = MIN(h2c->dbuf->i, h2c->dfl);
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001904 b_del(h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001905 h2c->dfl -= ret;
1906 ret = h2c->dfl == 0;
1907 goto strm_err;
1908 }
1909#endif
1910
Willy Tarreau7e98c052017-10-10 15:56:59 +02001911 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001912 case H2_FT_SETTINGS:
1913 if (h2c->st0 == H2_CS_FRAME_P)
1914 ret = h2c_handle_settings(h2c);
1915
1916 if (h2c->st0 == H2_CS_FRAME_A)
1917 ret = h2c_ack_settings(h2c);
1918 break;
1919
Willy Tarreaucf68c782017-10-10 17:11:41 +02001920 case H2_FT_PING:
1921 if (h2c->st0 == H2_CS_FRAME_P)
1922 ret = h2c_handle_ping(h2c);
1923
1924 if (h2c->st0 == H2_CS_FRAME_A)
1925 ret = h2c_ack_ping(h2c);
1926 break;
1927
Willy Tarreau26f95952017-07-27 17:18:30 +02001928 case H2_FT_WINDOW_UPDATE:
1929 if (h2c->st0 == H2_CS_FRAME_P)
1930 ret = h2c_handle_window_update(h2c, h2s);
1931 break;
1932
Willy Tarreau61290ec2017-10-17 08:19:21 +02001933 case H2_FT_CONTINUATION:
1934 /* we currently don't support CONTINUATION frames since
1935 * we have nowhere to store the partial HEADERS frame.
1936 * Let's abort the stream on an INTERNAL_ERROR here.
1937 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001938 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001939 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001940 h2c->st0 = H2_CS_FRAME_E;
1941 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001942 break;
1943
Willy Tarreau13278b42017-10-13 19:23:14 +02001944 case H2_FT_HEADERS:
1945 if (h2c->st0 == H2_CS_FRAME_P)
1946 ret = h2c_frt_handle_headers(h2c, h2s);
1947 break;
1948
Willy Tarreau454f9052017-10-26 19:40:35 +02001949 case H2_FT_DATA:
1950 if (h2c->st0 == H2_CS_FRAME_P)
1951 ret = h2c_frt_handle_data(h2c, h2s);
1952
1953 if (h2c->st0 == H2_CS_FRAME_A)
1954 ret = h2c_send_strm_wu(h2c);
1955 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001956
Willy Tarreau92153fc2017-12-03 19:46:19 +01001957 case H2_FT_PRIORITY:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_handle_priority(h2c);
1960 break;
1961
Willy Tarreaucd234e92017-08-18 10:59:39 +02001962 case H2_FT_RST_STREAM:
1963 if (h2c->st0 == H2_CS_FRAME_P)
1964 ret = h2c_handle_rst_stream(h2c, h2s);
1965 break;
1966
Willy Tarreaue96b0922017-10-30 00:28:29 +01001967 case H2_FT_GOAWAY:
1968 if (h2c->st0 == H2_CS_FRAME_P)
1969 ret = h2c_handle_goaway(h2c);
1970 break;
1971
Willy Tarreau1c661982017-10-30 13:52:01 +01001972 case H2_FT_PUSH_PROMISE:
1973 /* not permitted here, RFC7540#5.1 */
1974 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001975 break;
1976
1977 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001978 default:
1979 /* drop frames that we ignore. They may be larger than
1980 * the buffer so we drain all of their contents until
1981 * we reach the end.
1982 */
1983 ret = MIN(h2c->dbuf->i, h2c->dfl);
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001984 b_del(h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001985 h2c->dfl -= ret;
1986 ret = h2c->dfl == 0;
1987 }
1988
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001989 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001990 /* We may have to send an RST if not done yet */
1991 if (h2s->st == H2_SS_ERROR)
1992 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001993
Willy Tarreaua20a5192017-12-27 11:02:06 +01001994 if (h2c->st0 == H2_CS_FRAME_E)
1995 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001996
Willy Tarreau7e98c052017-10-10 15:56:59 +02001997 /* error or missing data condition met above ? */
1998 if (ret <= 0)
1999 break;
2000
2001 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002002 b_del(h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002003 h2c->st0 = H2_CS_FRAME_H;
2004 }
2005 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002006
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002007 if (h2c->rcvd_c > 0 &&
2008 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2009 h2c_send_conn_wu(h2c);
2010
Willy Tarreau52eed752017-09-22 15:05:09 +02002011 fail:
2012 /* we can go here on missing data, blocked response or error */
2013 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002014}
2015
2016/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2017 * the end.
2018 */
2019static int h2_process_mux(struct h2c *h2c)
2020{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002021 struct h2s *h2s, *h2s_back;
2022
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002023 /* start by sending possibly pending window updates */
2024 if (h2c->rcvd_c > 0 &&
2025 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2026 h2c_send_conn_wu(h2c) < 0)
2027 goto fail;
2028
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002029 /* First we always process the flow control list because the streams
2030 * waiting there were already elected for immediate emission but were
2031 * blocked just on this.
2032 */
2033
2034 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2035 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2036 h2c->st0 >= H2_CS_ERROR)
2037 break;
2038
2039 /* In theory it's possible that h2s->cs == NULL here :
2040 * - client sends crap that causes a parse error
2041 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2042 * - RST_STREAM cannot be emitted because mux is busy/full
2043 * - stream gets notified, detaches and quits
2044 * - mux buffer gets ready and wakes pending streams up
2045 * - bam!
2046 */
2047 h2s->flags &= ~H2_SF_BLK_ANY;
2048
2049 if (h2s->cs) {
2050 h2s->cs->data_cb->send(h2s->cs);
2051 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002052 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002053 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002054 }
2055
2056 /* depending on callee's blocking reasons, we may queue in send
2057 * list or completely dequeue.
2058 */
2059 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2060 if (h2s->flags & H2_SF_BLK_ANY) {
2061 LIST_DEL(&h2s->list);
2062 LIST_ADDQ(&h2c->send_list, &h2s->list);
2063 }
2064 else {
2065 LIST_DEL(&h2s->list);
2066 LIST_INIT(&h2s->list);
2067 if (h2s->cs)
2068 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002069 else {
2070 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002071 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002072 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002073 }
2074 }
2075 }
2076
2077 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2078 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2079 break;
2080
2081 /* In theory it's possible that h2s->cs == NULL here :
2082 * - client sends crap that causes a parse error
2083 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2084 * - RST_STREAM cannot be emitted because mux is busy/full
2085 * - stream gets notified, detaches and quits
2086 * - mux buffer gets ready and wakes pending streams up
2087 * - bam!
2088 */
2089 h2s->flags &= ~H2_SF_BLK_ANY;
2090
2091 if (h2s->cs) {
2092 h2s->cs->data_cb->send(h2s->cs);
2093 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002094 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002095 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002096 }
2097 /* depending on callee's blocking reasons, we may queue in fctl
2098 * list or completely dequeue.
2099 */
2100 if (h2s->flags & H2_SF_BLK_MFCTL) {
2101 /* stream hit the connection's flow control */
2102 LIST_DEL(&h2s->list);
2103 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2104 }
2105 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2106 LIST_DEL(&h2s->list);
2107 LIST_INIT(&h2s->list);
2108 if (h2s->cs)
2109 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002110 else {
2111 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002112 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002113 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002114 }
2115 }
2116
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002117 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002118 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002119 if (h2c->st0 == H2_CS_ERROR) {
2120 if (h2c->max_id >= 0) {
2121 h2c_send_goaway_error(h2c, NULL);
2122 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2123 return 0;
2124 }
2125
2126 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2127 }
2128 return 1;
2129 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002130 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002131}
2132
Willy Tarreau71681172017-10-23 14:39:06 +02002133
Willy Tarreau62f52692017-10-08 23:01:42 +02002134/*********************************************************/
2135/* functions below are I/O callbacks from the connection */
2136/*********************************************************/
2137
2138/* callback called on recv event by the connection handler */
2139static void h2_recv(struct connection *conn)
2140{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002141 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002142 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002143 int max;
2144
Willy Tarreau315d8072017-12-10 22:17:57 +01002145 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002146 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002147
Willy Tarreau44e973f2018-03-01 17:49:30 +01002148 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002149 if (!buf) {
2150 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002151 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002152 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002153
Willy Tarreaua2af5122017-10-09 11:56:46 +02002154 /* note: buf->o == 0 */
2155 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002156 if (max)
2157 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002158
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002159 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002160 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002161 return;
2162 }
2163
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002164 if (buf->i == buf->size)
2165 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002166 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002167}
2168
2169/* callback called on send event by the connection handler */
2170static void h2_send(struct connection *conn)
2171{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002172 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002173 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002174
2175 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002176 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002177
2178 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2179 /* a handshake was requested */
2180 return;
2181 }
2182
Willy Tarreaubc933932017-10-09 16:21:43 +02002183 /* This loop is quite simple : it tries to fill as much as it can from
2184 * pending streams into the existing buffer until it's reportedly full
2185 * or the end of send requests is reached. Then it tries to send this
2186 * buffer's contents out, marks it not full if at least one byte could
2187 * be sent, and tries again.
2188 *
2189 * The snd_buf() function normally takes a "flags" argument which may
2190 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2191 * data immediately comes and CO_SFL_STREAMER to indicate that the
2192 * connection is streaming lots of data (used to increase TLS record
2193 * size at the expense of latency). The former can be sent any time
2194 * there's a buffer full flag, as it indicates at least one stream
2195 * attempted to send and failed so there are pending data. An
2196 * alternative would be to set it as long as there's an active stream
2197 * but that would be problematic for ACKs until we have an absolute
2198 * guarantee that all waiters have at least one byte to send. The
2199 * latter should possibly not be set for now.
2200 */
2201
2202 done = 0;
2203 while (!done) {
2204 unsigned int flags = 0;
2205
2206 /* fill as much as we can into the current buffer */
2207 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2208 done = h2_process_mux(h2c);
2209
2210 if (conn->flags & CO_FL_ERROR)
2211 break;
2212
2213 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2214 flags |= CO_SFL_MSG_MORE;
2215
Willy Tarreau787db9a2018-06-14 18:31:46 +02002216 if (h2c->mbuf->o) {
2217 int ret = conn->xprt->snd_buf(conn, h2c->mbuf, h2c->mbuf->o, flags);
2218 if (!ret)
2219 break;
2220 b_del(h2c->mbuf, ret);
2221 b_realign_if_empty(h2c->mbuf);
2222 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002223
2224 /* wrote at least one byte, the buffer is not full anymore */
2225 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2226 }
2227
Willy Tarreaua2af5122017-10-09 11:56:46 +02002228 if (conn->flags & CO_FL_SOCK_WR_SH) {
2229 /* output closed, nothing to send, clear the buffer to release it */
2230 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002231 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002232}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233
Willy Tarreau62f52692017-10-08 23:01:42 +02002234/* callback called on any event by the connection handler.
2235 * It applies changes and returns zero, or < 0 if it wants immediate
2236 * destruction of the connection (which normally doesn not happen in h2).
2237 */
2238static int h2_wake(struct connection *conn)
2239{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002241 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002242
Willy Tarreaud13bf272017-12-14 10:34:52 +01002243 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2244 h2_process_demux(h2c);
2245
2246 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2247 h2c->dbuf->i = 0;
2248
2249 if (h2c->dbuf->i != h2c->dbuf->size)
2250 h2c->flags &= ~H2_CF_DEM_DFULL;
2251 }
2252
Willy Tarreau8ec14062017-12-30 18:08:13 +01002253 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2254 /* frontend is stopping, reload likely in progress, let's try
2255 * to announce a graceful shutdown if not yet done. We don't
2256 * care if it fails, it will be tried again later.
2257 */
2258 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2259 if (h2c->last_sid < 0)
2260 h2c->last_sid = (1U << 31) - 1;
2261 h2c_send_goaway_error(h2c, NULL);
2262 }
2263 }
2264
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002265 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002266 * If we received early data, and the handshake is done, wake
2267 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002268 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002269 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2270 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2271 struct eb32_node *node;
2272 struct h2s *h2s;
2273
2274 h2c->flags |= H2_CF_WAIT_FOR_HS;
2275 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2276
2277 while (node) {
2278 h2s = container_of(node, struct h2s, by_id);
2279 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2280 h2s->cs->data_cb->wake(h2s->cs);
2281 node = eb32_next(node);
2282 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002283 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002284
Willy Tarreau26bd7612017-10-09 16:47:04 +02002285 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002286 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2287 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2288 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002289 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002290
2291 if (eb_is_empty(&h2c->streams_by_id)) {
2292 /* no more stream, kill the connection now */
2293 h2_release(conn);
2294 return -1;
2295 }
2296 else {
2297 /* some streams still there, we need to signal them all and
2298 * wait for their departure.
2299 */
2300 __conn_xprt_stop_recv(conn);
2301 __conn_xprt_stop_send(conn);
2302 return 0;
2303 }
2304 }
2305
2306 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002307 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002308
2309 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002310 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002311 __conn_xprt_stop_recv(conn);
2312 }
2313 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002314 __conn_xprt_want_recv(conn);
2315 }
2316
2317 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002318 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2319 (h2c->st0 == H2_CS_ERROR ||
2320 h2c->mbuf->o ||
2321 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2322 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002323 __conn_xprt_want_send(conn);
2324 }
2325 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002326 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002327 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002328 }
2329
Willy Tarreau3f133572017-10-31 19:21:06 +01002330 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002331 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002332 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002333 task_queue(h2c->task);
2334 }
2335 else
2336 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002337 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002338 return 0;
2339}
2340
Willy Tarreauea392822017-10-31 10:02:25 +01002341/* Connection timeout management. The principle is that if there's no receipt
2342 * nor sending for a certain amount of time, the connection is closed. If the
2343 * MUX buffer still has lying data or is not allocatable, the connection is
2344 * immediately killed. If it's allocatable and empty, we attempt to send a
2345 * GOAWAY frame.
2346 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002347static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002348{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002349 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002350 int expired = tick_is_expired(t->expire, now_ms);
2351
Willy Tarreau0975f112018-03-29 15:22:59 +02002352 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002353 return t;
2354
Willy Tarreau0975f112018-03-29 15:22:59 +02002355 task_delete(t);
2356 task_free(t);
2357
2358 if (!h2c) {
2359 /* resources were already deleted */
2360 return NULL;
2361 }
2362
2363 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002364 h2c_error(h2c, H2_ERR_NO_ERROR);
2365 h2_wake_some_streams(h2c, 0, 0);
2366
2367 if (h2c->mbuf->o) {
2368 /* don't even try to send a GOAWAY, the buffer is stuck */
2369 h2c->flags |= H2_CF_GOAWAY_FAILED;
2370 }
2371
2372 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002373 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002374 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2375 h2c->flags |= H2_CF_GOAWAY_FAILED;
2376
Willy Tarreau787db9a2018-06-14 18:31:46 +02002377 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2378 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, h2c->mbuf->o, 0);
2379 if (ret > 0) {
2380 b_del(h2c->mbuf, ret);
2381 b_realign_if_empty(h2c->mbuf);
2382 }
2383 }
Willy Tarreauea392822017-10-31 10:02:25 +01002384
Willy Tarreau0975f112018-03-29 15:22:59 +02002385 /* either we can release everything now or it will be done later once
2386 * the last stream closes.
2387 */
2388 if (eb_is_empty(&h2c->streams_by_id))
2389 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002390
Willy Tarreauea392822017-10-31 10:02:25 +01002391 return NULL;
2392}
2393
2394
Willy Tarreau62f52692017-10-08 23:01:42 +02002395/*******************************************/
2396/* functions below are used by the streams */
2397/*******************************************/
2398
2399/*
2400 * Attach a new stream to a connection
2401 * (Used for outgoing connections)
2402 */
2403static struct conn_stream *h2_attach(struct connection *conn)
2404{
2405 return NULL;
2406}
2407
2408/* callback used to update the mux's polling flags after changing a cs' status.
2409 * The caller (cs_update_mux_polling) will take care of propagating any changes
2410 * to the transport layer.
2411 */
2412static void h2_update_poll(struct conn_stream *cs)
2413{
Willy Tarreau1d393222017-10-17 10:26:19 +02002414 struct h2s *h2s = cs->ctx;
2415
2416 if (!h2s)
2417 return;
2418
Willy Tarreaud7739c82017-10-30 15:38:23 +01002419 /* we may unblock a blocked read */
2420
Willy Tarreau315d8072017-12-10 22:17:57 +01002421 if (cs->flags & CS_FL_DATA_RD_ENA) {
2422 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002423 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002424 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002425 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002426 conn_xprt_want_send(cs->conn);
2427 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002428 }
2429
Willy Tarreau1d393222017-10-17 10:26:19 +02002430 /* Note: the stream and stream-int code doesn't allow us to perform a
2431 * synchronous send() here unfortunately, because this code is called
2432 * as si_update() from the process_stream() context. This means that
2433 * we have to queue the current cs and defer its processing after the
2434 * connection's cs list is processed anyway.
2435 */
2436
2437 if (cs->flags & CS_FL_DATA_WR_ENA) {
2438 if (LIST_ISEMPTY(&h2s->list)) {
2439 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2440 !h2s->h2c->mbuf->o && // not yet subscribed
2441 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2442 conn_xprt_want_send(cs->conn);
2443 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2444 }
2445 }
2446 else if (!LIST_ISEMPTY(&h2s->list)) {
2447 LIST_DEL(&h2s->list);
2448 LIST_INIT(&h2s->list);
2449 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2450 }
2451
2452 /* this can happen from within si_chk_snd() */
2453 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2454 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002455}
2456
2457/*
2458 * Detach the stream from the connection and possibly release the connection.
2459 */
2460static void h2_detach(struct conn_stream *cs)
2461{
Willy Tarreau60935142017-10-16 18:11:19 +02002462 struct h2s *h2s = cs->ctx;
2463 struct h2c *h2c;
2464
2465 cs->ctx = NULL;
2466 if (!h2s)
2467 return;
2468
2469 h2c = h2s->h2c;
2470 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002471 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002472 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2473 !h2_has_too_many_cs(h2c)) {
2474 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2475 if (h2_recv_allowed(h2c)) {
2476 __conn_xprt_want_recv(h2c->conn);
2477 conn_xprt_want_send(h2c->conn);
2478 }
2479 }
Willy Tarreau60935142017-10-16 18:11:19 +02002480
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002481 /* this stream may be blocked waiting for some data to leave (possibly
2482 * an ES or RST frame), so orphan it in this case.
2483 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002484 if (!(cs->conn->flags & CO_FL_ERROR) &&
2485 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002486 return;
2487
Willy Tarreau45f752e2017-10-30 15:44:59 +01002488 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2489 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2490 /* unblock the connection if it was blocked on this
2491 * stream.
2492 */
2493 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2494 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2495 conn_xprt_want_recv(cs->conn);
2496 conn_xprt_want_send(cs->conn);
2497 }
2498
Willy Tarreau71049cc2018-03-28 13:56:39 +02002499 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002500
Willy Tarreaue323f342018-03-28 13:51:45 +02002501 /* We don't want to close right now unless we're removing the
2502 * last stream, and either the connection is in error, or it
2503 * reached the ID already specified in a GOAWAY frame received
2504 * or sent (as seen by last_sid >= 0).
2505 */
2506 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2507 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002508 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002509 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2510 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2511 (conn_xprt_read0_pending(h2c->conn) ||
2512 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2513 /* no more stream will come, kill it now */
2514 h2_release(h2c->conn);
2515 }
2516 else if (h2c->task) {
2517 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2518 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2519 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002520 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002521 else
2522 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002523 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002524}
2525
2526static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2527{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002528 struct h2s *h2s = cs->ctx;
2529
2530 if (!mode)
2531 return;
2532
Willy Tarreau721c9742017-11-07 11:05:42 +01002533 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002534 return;
2535
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002536 /* if no outgoing data was seen on this stream, it means it was
2537 * closed with a "tcp-request content" rule that is normally
2538 * used to kill the connection ASAP (eg: limit abuse). In this
2539 * case we send a goaway to close the connection.
2540 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002541 if (!(h2s->flags & H2_SF_RST_SENT) &&
2542 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002543 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002544
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002545 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2546 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2547 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002548 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002549
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002550 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2551 conn_xprt_want_send(cs->conn);
2552
Willy Tarreau00dd0782018-03-01 16:31:34 +01002553 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002554
2555 add_to_list:
2556 if (LIST_ISEMPTY(&h2s->list)) {
2557 if (h2s->flags & H2_SF_BLK_MFCTL)
2558 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2559 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2560 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2561 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002562}
2563
2564static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2565{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002566 struct h2s *h2s = cs->ctx;
2567
Willy Tarreau721c9742017-11-07 11:05:42 +01002568 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002569 return;
2570
Willy Tarreau67434202017-11-06 20:20:51 +01002571 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002572 /* we can cleanly close using an empty data frame only after headers */
2573
2574 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2575 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002576 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002577
2578 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002579 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002580 else
2581 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002582 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002583 /* if no outgoing data was seen on this stream, it means it was
2584 * closed with a "tcp-request content" rule that is normally
2585 * used to kill the connection ASAP (eg: limit abuse). In this
2586 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002587 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002588 if (!(h2s->flags & H2_SF_RST_SENT) &&
2589 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002590 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002591
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002592 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2593 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002594 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002595 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002596
Willy Tarreau00dd0782018-03-01 16:31:34 +01002597 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002598 }
2599
2600 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2601 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002602
2603 add_to_list:
2604 if (LIST_ISEMPTY(&h2s->list)) {
2605 if (h2s->flags & H2_SF_BLK_MFCTL)
2606 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2607 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2608 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2609 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002610}
2611
Willy Tarreau13278b42017-10-13 19:23:14 +02002612/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2613 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2614 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002615 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002616 */
2617static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2618{
2619 struct h2c *h2c = h2s->h2c;
2620 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002621 struct chunk *tmp = get_trash_chunk();
2622 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002623 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002624 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002625 int flen = h2c->dfl;
2626 int outlen = 0;
2627 int wrap;
2628 int try;
2629
2630 if (!h2c->dfl) {
2631 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002632 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002633 return 0;
2634 }
2635
Willy Tarreau68472622017-12-11 18:36:37 +01002636 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2637 return 0; // incomplete input frame
2638
Willy Tarreau13278b42017-10-13 19:23:14 +02002639 /* if the input buffer wraps, take a temporary copy of it (rare) */
2640 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2641 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002642 copy = alloc_trash_chunk();
2643 if (!copy) {
2644 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2645 goto fail;
2646 }
2647 memcpy(copy->str, h2c->dbuf->p, wrap);
2648 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2649 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002650 }
2651
2652 /* The padlen is the first byte before data, and the padding appears
2653 * after data. padlen+data+padding are included in flen.
2654 */
2655 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002656 h2c->dpl = *hdrs;
2657 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002658 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2659 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002660 return 0;
2661 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002662 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002663 hdrs += 1; // skip Pad Length
2664 }
2665
2666 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2667 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002668 if (read_n32(hdrs) == h2s->id) {
2669 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2670 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2671 return 0;//goto fail_stream;
2672 }
2673
Willy Tarreau13278b42017-10-13 19:23:14 +02002674 hdrs += 5; // stream dep = 4, weight = 1
2675 flen -= 5;
2676 }
2677
2678 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2679 * don't support this for now and can't even decompress so we have to
2680 * break the connection.
2681 */
2682 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2683 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002684 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002685 }
2686
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002687 /* we can't retry a failed decompression operation so we must be very
2688 * careful not to take any risks. In practice the output buffer is
2689 * always empty except maybe for trailers, so these operations almost
2690 * never happen.
2691 */
2692 if (unlikely(buf->o)) {
2693 /* need to let the output buffer flush and
2694 * mark the buffer for later wake up.
2695 */
2696 goto fail;
2697 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002698
Willy Tarreauaa7af722018-07-12 10:33:12 +02002699 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002700 /* it doesn't fit and the buffer is fragmented,
2701 * so let's defragment it and try again.
2702 */
Willy Tarreau0db4d102018-07-12 11:00:01 +02002703 b_slow_realign(buf, trash.str, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002704 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002705
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002706 /* first check if we have some room after p+i */
2707 try = buf->data + buf->size - (buf->p + buf->i);
2708
2709 /* otherwise continue between data and p-o */
2710 if (try <= 0) {
2711 try = buf->p - (buf->data + buf->o);
2712 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002713 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002714 }
2715 if (try > count)
2716 try = count;
2717
2718 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2719 sizeof(list)/sizeof(list[0]), tmp);
2720 if (outlen < 0) {
2721 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2722 goto fail;
2723 }
2724
2725 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002726 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02002727 outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002728
2729 if (outlen < 0) {
2730 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2731 goto fail;
2732 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002733
Willy Tarreau174b06a2018-04-25 18:13:58 +02002734 if (msgf & H2_MSGF_BODY) {
2735 /* a payload is present */
2736 if (msgf & H2_MSGF_BODY_CL)
2737 h2s->flags |= H2_SF_DATA_CLEN;
2738 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2739 h2s->flags |= H2_SF_DATA_CHNK;
2740 }
2741
Willy Tarreau13278b42017-10-13 19:23:14 +02002742 /* now consume the input data */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002743 b_del(h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002744 h2c->st0 = H2_CS_FRAME_H;
2745 buf->i += outlen;
2746
2747 /* don't send it before returning data!
2748 * FIXME: should we instead try to send it much later, after the
2749 * response ? This would require that we keep a copy of it in h2s.
2750 */
2751 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2752 h2s->cs->flags |= CS_FL_EOS;
2753 h2s->flags |= H2_SF_ES_RCVD;
2754 }
2755
Willy Tarreau68dd9852017-07-03 14:44:26 +02002756 leave:
2757 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002758 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002759 fail:
2760 outlen = 0;
2761 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002762}
2763
Willy Tarreau454f9052017-10-26 19:40:35 +02002764/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2765 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2766 * in use, a new chunk is emitted for each frame. This is supposed to fit
2767 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2768 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2769 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2770 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002771 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2772 * checked to know if some data remain pending (an empty DATA frame can return
2773 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2774 * connection errors in h2c->errcode. The caller must already have checked the
2775 * frame header and ensured that the frame was complete or the buffer full. It
2776 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002777 */
2778static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2779{
2780 struct h2c *h2c = h2s->h2c;
2781 int block1, block2;
2782 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002783 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002784
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002785 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002786 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002787
2788 /* The padlen is the first byte before data, and the padding appears
2789 * after data. padlen+data+padding are included in flen.
2790 */
Willy Tarreau79127812017-12-03 21:06:59 +01002791 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002792 if (h2c->dbuf->i < 1)
2793 return 0;
2794
Willy Tarreaudda2e412018-06-07 18:08:04 +02002795 h2c->dpl = *(uint8_t *)b_head(h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002796 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002797 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2798 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002799 return 0;
2800 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002801
2802 /* skip the padlen byte */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002803 b_del(h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002804 h2c->dfl--;
2805 h2c->rcvd_c++; h2c->rcvd_s++;
2806 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002807 }
2808
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002809 flen = h2c->dfl - h2c->dpl;
2810 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002811 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002812
2813 if (flen > h2c->dbuf->i) {
2814 flen = h2c->dbuf->i;
2815 if (!flen)
2816 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002817 }
2818
Willy Tarreaueba10f22018-04-25 20:44:22 +02002819 /* chunked-encoding requires more room */
2820 if (h2s->flags & H2_SF_DATA_CHNK) {
2821 chklen = MIN(flen, count);
2822 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2823 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2824 (chklen < 1048576) ? 4 : 8;
2825 chklen += 4; // CRLF, CRLF
2826 }
2827
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002828 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002829 if (flen + chklen > count) {
2830 if (chklen >= count)
2831 goto full;
2832 flen = count - chklen;
2833 }
2834
2835 if (h2s->flags & H2_SF_DATA_CHNK) {
2836 /* emit the chunk size */
2837 unsigned int chksz = flen;
2838 char str[10];
2839 char *beg;
2840
2841 beg = str + sizeof(str);
2842 *--beg = '\n';
2843 *--beg = '\r';
2844 do {
2845 *--beg = hextab[chksz & 0xF];
2846 } while (chksz >>= 4);
2847 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002848 }
2849
Willy Tarreau454f9052017-10-26 19:40:35 +02002850 /* Block1 is the length of the first block before the buffer wraps,
2851 * block2 is the optional second block to reach the end of the frame.
2852 */
Willy Tarreau7194d3c2018-06-06 16:55:45 +02002853 block1 = b_contig_data(h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002854 if (block1 > flen)
2855 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002856 block2 = flen - block1;
2857
2858 if (block1)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002859 bi_putblk(buf, b_head(h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002860
2861 if (block2)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002862 bi_putblk(buf, b_peek(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002863
Willy Tarreaueba10f22018-04-25 20:44:22 +02002864 if (h2s->flags & H2_SF_DATA_CHNK) {
2865 /* emit the CRLF */
2866 bi_putblk(buf, "\r\n", 2);
2867 }
2868
Willy Tarreau454f9052017-10-26 19:40:35 +02002869 /* now mark the input data as consumed (will be deleted from the buffer
2870 * by the caller when seeing FRAME_A after sending the window update).
2871 */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002872 b_del(h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002873 h2c->dfl -= flen;
2874 h2c->rcvd_c += flen;
2875 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2876
2877 if (h2c->dfl > h2c->dpl) {
2878 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002879 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002880 }
2881
Willy Tarreau4a28da12018-01-04 14:41:00 +01002882 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002883 /* here we're done with the frame, all the payload (except padding) was
2884 * transferred.
2885 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002886
2887 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2888 /* emit the trailing 0 CRLF CRLF */
2889 if (count < 5)
2890 goto more;
2891 chklen += 5;
2892 bi_putblk(buf, "0\r\n\r\n", 5);
2893 }
2894
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002895 h2c->rcvd_c += h2c->dpl;
2896 h2c->rcvd_s += h2c->dpl;
2897 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002898 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2899
2900 /* don't send it before returning data!
2901 * FIXME: should we instead try to send it much later, after the
2902 * response ? This would require that we keep a copy of it in h2s.
2903 */
Willy Tarreau79127812017-12-03 21:06:59 +01002904 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002905 h2s->cs->flags |= CS_FL_EOS;
2906 h2s->flags |= H2_SF_ES_RCVD;
2907 }
2908
Willy Tarreaueba10f22018-04-25 20:44:22 +02002909 return flen + chklen;
2910 full:
2911 flen = chklen = 0;
2912 more:
2913 h2c->flags |= H2_CF_DEM_SFULL;
2914 h2s->cs->flags |= CS_FL_RCV_MORE;
2915 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002916}
2917
Willy Tarreau62f52692017-10-08 23:01:42 +02002918/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002919 * Called from the upper layer to get more data, up to <count> bytes. The
2920 * caller is responsible for never asking for more data than what is available
2921 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002922 */
2923static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2924{
Willy Tarreau13278b42017-10-13 19:23:14 +02002925 struct h2s *h2s = cs->ctx;
2926 struct h2c *h2c = h2s->h2c;
2927 int ret = 0;
2928
2929 if (h2c->st0 != H2_CS_FRAME_P)
2930 return 0; // no pre-parsed frame yet
2931
2932 if (h2c->dsi != h2s->id)
2933 return 0; // not for us
2934
2935 if (!h2c->dbuf->size)
2936 return 0; // empty buffer
2937
Willy Tarreau13278b42017-10-13 19:23:14 +02002938 switch (h2c->dft) {
2939 case H2_FT_HEADERS:
2940 ret = h2_frt_decode_headers(h2s, buf, count);
2941 break;
2942
Willy Tarreau454f9052017-10-26 19:40:35 +02002943 case H2_FT_DATA:
2944 ret = h2_frt_transfer_data(h2s, buf, count);
2945 break;
2946
Willy Tarreau13278b42017-10-13 19:23:14 +02002947 default:
2948 ret = 0;
2949 }
2950 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002951}
2952
Willy Tarreau5dd17352018-06-14 13:33:30 +02002953/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2954 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2955 * number of bytes sent. The caller must check the stream's status to detect
2956 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002957 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002958static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002959{
2960 struct http_hdr list[MAX_HTTP_HDR];
2961 struct h2c *h2c = h2s->h2c;
2962 struct h1m *h1m = &h2s->res;
2963 struct chunk outbuf;
2964 int es_now = 0;
2965 int ret = 0;
2966 int hdr;
2967
2968 if (h2c_mux_busy(h2c, h2s)) {
2969 h2s->flags |= H2_SF_BLK_MBUSY;
2970 return 0;
2971 }
2972
Willy Tarreau44e973f2018-03-01 17:49:30 +01002973 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002974 h2c->flags |= H2_CF_MUX_MALLOC;
2975 h2s->flags |= H2_SF_BLK_MROOM;
2976 return 0;
2977 }
2978
2979 /* First, try to parse the H1 response and index it into <list>.
2980 * NOTE! Since it comes from haproxy, we *know* that a response header
2981 * block does not wrap and we can safely read it this way without
2982 * having to realign the buffer.
2983 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02002984 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002985 list, sizeof(list)/sizeof(list[0]), h1m);
2986 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002987 /* incomplete or invalid response, this is abnormal coming from
2988 * haproxy and may only result in a bad errorfile or bad Lua code
2989 * so that won't be fixed, raise an error now.
2990 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002991 * FIXME: we should instead add the ability to only return a
2992 * 502 bad gateway. But in theory this is not supposed to
2993 * happen.
2994 */
2995 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2996 ret = 0;
2997 goto end;
2998 }
2999
3000 chunk_reset(&outbuf);
3001
3002 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02003003 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02003004 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003005 outbuf.len = 0;
3006
Willy Tarreauaa7af722018-07-12 10:33:12 +02003007 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003008 break;
3009 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02003010 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003011 }
3012
3013 if (outbuf.size < 9) {
3014 h2c->flags |= H2_CF_MUX_MFULL;
3015 h2s->flags |= H2_SF_BLK_MROOM;
3016 ret = 0;
3017 goto end;
3018 }
3019
3020 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3021 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
3022 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3023 outbuf.len = 9;
3024
3025 /* encode status, which necessarily is the first one */
3026 if (outbuf.len < outbuf.size && h1m->status == 200)
3027 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
3028 else if (outbuf.len < outbuf.size && h1m->status == 304)
3029 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003030 else if (unlikely(list[0].v.len != 3)) {
3031 /* this is an unparsable response */
3032 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3033 ret = 0;
3034 goto end;
3035 }
3036 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003037 /* basic encoding of the status code */
3038 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3039 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3040 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3041 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3042 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3043 }
3044 else {
Willy Tarreauaa7af722018-07-12 10:33:12 +02003045 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003046 goto realign_again;
3047
3048 h2c->flags |= H2_CF_MUX_MFULL;
3049 h2s->flags |= H2_SF_BLK_MROOM;
3050 ret = 0;
3051 goto end;
3052 }
3053
3054 /* encode all headers, stop at empty name */
3055 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003056 /* these ones do not exist in H2 and must be dropped. */
3057 if (isteq(list[hdr].n, ist("connection")) ||
3058 isteq(list[hdr].n, ist("proxy-connection")) ||
3059 isteq(list[hdr].n, ist("keep-alive")) ||
3060 isteq(list[hdr].n, ist("upgrade")) ||
3061 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003062 continue;
3063
3064 if (isteq(list[hdr].n, ist("")))
3065 break; // end
3066
3067 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3068 /* output full */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003069 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003070 goto realign_again;
3071
3072 h2c->flags |= H2_CF_MUX_MFULL;
3073 h2s->flags |= H2_SF_BLK_MROOM;
3074 ret = 0;
3075 goto end;
3076 }
3077 }
3078
3079 /* we may need to add END_STREAM */
3080 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3081 es_now = 1;
3082
3083 /* update the frame's size */
3084 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3085
3086 if (es_now)
3087 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3088
3089 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003090 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003091
3092 /* commit the H2 response */
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02003093 h2c->mbuf->p = b_peek(h2c->mbuf, h2c->mbuf->o + outbuf.len);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094 h2c->mbuf->o += outbuf.len;
Willy Tarreau67434202017-11-06 20:20:51 +01003095 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003096
3097 /* for now we don't implemented CONTINUATION, so we wait for a
3098 * body or directly end in TRL2.
3099 */
3100 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003101 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003102 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003103
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003104 h1m->state = HTTP_MSG_DONE;
3105 h2s->flags |= H2_SF_ES_SENT;
3106 if (h2s->st == H2_SS_OPEN)
3107 h2s->st = H2_SS_HLOC;
3108 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003109 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003111 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003112 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003113 h1m->state = HTTP_MSG_RPBEFORE;
3114 h1m->status = 0;
3115 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003116 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003117 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003118 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003119 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003120
3121 end:
3122 //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));
3123 return ret;
3124}
3125
Willy Tarreau5dd17352018-06-14 13:33:30 +02003126/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3127 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3128 * the number of bytes sent. The caller must check the stream's status to
3129 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003130 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003131static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003132{
3133 struct h2c *h2c = h2s->h2c;
3134 struct h1m *h1m = &h2s->res;
3135 struct chunk outbuf;
3136 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003137 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003138 int es_now = 0;
3139 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003140 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003141 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003142
3143 if (h2c_mux_busy(h2c, h2s)) {
3144 h2s->flags |= H2_SF_BLK_MBUSY;
3145 goto end;
3146 }
3147
Willy Tarreau44e973f2018-03-01 17:49:30 +01003148 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003149 h2c->flags |= H2_CF_MUX_MALLOC;
3150 h2s->flags |= H2_SF_BLK_MROOM;
3151 goto end;
3152 }
3153
3154 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003155 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003156 goto end;
3157
3158 chunk_reset(&outbuf);
3159
3160 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02003161 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02003162 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003163 outbuf.len = 0;
3164
Willy Tarreauaa7af722018-07-12 10:33:12 +02003165 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003166 break;
3167 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02003168 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003169 }
3170
3171 if (outbuf.size < 9) {
3172 h2c->flags |= H2_CF_MUX_MFULL;
3173 h2s->flags |= H2_SF_BLK_MROOM;
3174 goto end;
3175 }
3176
3177 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3178 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3179 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3180 outbuf.len = 9;
3181
3182 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3183 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003184 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003185 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003186 break;
3187 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003188 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003189 if ((long long)size > h1m->curr_len)
3190 size = h1m->curr_len;
3191 break;
3192 default: /* te:chunked : parse chunks */
3193 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003194 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003195 if (!ret)
3196 goto end;
3197
3198 if (ret < 0) {
3199 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3200 h1m->err_pos = ret;
3201 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3202 goto end;
3203 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003204 max -= ret;
3205 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003206 total += ret;
3207 h1m->state = HTTP_MSG_CHUNK_SIZE;
3208 }
3209
3210 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3211 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003212 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003213 if (!ret)
3214 goto end;
3215
3216 if (ret < 0) {
3217 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3218 h1m->err_pos = ret;
3219 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3220 goto end;
3221 }
3222
3223 size = chunk;
3224 h1m->curr_len = chunk;
3225 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003226 max -= ret;
3227 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003228 total += ret;
3229 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3230 if (!size)
3231 goto send_empty;
3232 }
3233
3234 /* in MSG_DATA state, continue below */
3235 size = h1m->curr_len;
3236 break;
3237 }
3238
3239 /* we have in <size> the exact number of bytes we need to copy from
3240 * the H1 buffer. We need to check this against the connection's and
3241 * the stream's send windows, and to ensure that this fits in the max
3242 * frame size and in the buffer's available space minus 9 bytes (for
3243 * the frame header). The connection's flow control is applied last so
3244 * that we can use a separate list of streams which are immediately
3245 * unblocked on window opening. Note: we don't implement padding.
3246 */
3247
Willy Tarreau5dd17352018-06-14 13:33:30 +02003248 if (size > max)
3249 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003250
3251 if (size > h2s->mws)
3252 size = h2s->mws;
3253
3254 if (size <= 0) {
3255 h2s->flags |= H2_SF_BLK_SFCTL;
3256 goto end;
3257 }
3258
3259 if (h2c->mfs && size > h2c->mfs)
3260 size = h2c->mfs;
3261
3262 if (size + 9 > outbuf.size) {
3263 /* we have an opportunity for enlarging the too small
3264 * available space, let's try.
3265 */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003266 if (b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003267 goto realign_again;
3268 size = outbuf.size - 9;
3269 }
3270
3271 if (size <= 0) {
3272 h2c->flags |= H2_CF_MUX_MFULL;
3273 h2s->flags |= H2_SF_BLK_MROOM;
3274 goto end;
3275 }
3276
3277 if (size > h2c->mws)
3278 size = h2c->mws;
3279
3280 if (size <= 0) {
3281 h2s->flags |= H2_SF_BLK_MFCTL;
3282 goto end;
3283 }
3284
3285 /* copy whatever we can */
3286 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003287 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 if (ret == 1)
3289 len2 = 0;
3290
3291 if (!ret || len1 + len2 < size) {
3292 /* FIXME: must normally never happen */
3293 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3294 goto end;
3295 }
3296
3297 /* limit len1/len2 to size */
3298 if (len1 + len2 > size) {
3299 int sub = len1 + len2 - size;
3300
3301 if (len2 > sub)
3302 len2 -= sub;
3303 else {
3304 sub -= len2;
3305 len2 = 0;
3306 len1 -= sub;
3307 }
3308 }
3309
3310 /* now let's copy this this into the output buffer */
3311 memcpy(outbuf.str + 9, blk1, len1);
3312 if (len2)
3313 memcpy(outbuf.str + 9 + len1, blk2, len2);
3314
3315 send_empty:
3316 /* we may need to add END_STREAM */
3317 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3318 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003319 *
3320 * FIXME: what we do here is not correct because we send end_stream
3321 * before knowing if we'll have to send a HEADERS frame for the
3322 * trailers. More importantly we're not consuming the trailing CRLF
3323 * after the end of trailers, so it will be left to the caller to
3324 * eat it. The right way to do it would be to measure trailers here
3325 * and to send ES only if there are no trailers.
3326 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003327 */
3328 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3329 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3330 es_now = 1;
3331
3332 /* update the frame's size */
3333 h2_set_frame_size(outbuf.str, size);
3334
3335 if (es_now)
3336 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3337
3338 /* commit the H2 response */
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02003339 h2c->mbuf->p = b_peek(h2c->mbuf, h2c->mbuf->o + size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003340 h2c->mbuf->o += size + 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341
3342 /* consume incoming H1 response */
3343 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003344 max -= size;
3345 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003346 total += size;
3347 h1m->curr_len -= size;
3348 h2s->mws -= size;
3349 h2c->mws -= size;
3350
3351 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3352 h1m->state = HTTP_MSG_CHUNK_CRLF;
3353 goto new_frame;
3354 }
3355 }
3356
3357 if (es_now) {
3358 if (h2s->st == H2_SS_OPEN)
3359 h2s->st = H2_SS_HLOC;
3360 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003361 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003362
Willy Tarreau35a62702018-02-27 15:37:25 +01003363 if (!(h1m->flags & H1_MF_CHNK)) {
3364 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003365 total += max;
3366 ofs += max;
3367 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003368
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003369 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003370 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003371
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003372 h2s->flags |= H2_SF_ES_SENT;
3373 }
3374
3375 end:
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003376 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)buf->o);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003377 return total;
3378}
3379
Willy Tarreau62f52692017-10-08 23:01:42 +02003380/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003381static size_t h2_snd_buf(struct conn_stream *cs, const struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003382{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003383 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003384 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003385 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003386
Willy Tarreau0bad0432018-06-14 16:54:01 +02003387 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003388 h2s->flags |= H2_SF_OUTGOING_DATA;
3389
Willy Tarreau0bad0432018-06-14 16:54:01 +02003390 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003391 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003392 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003393 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003394 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003395 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003396 }
3397 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3398 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003399 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003400
Willy Tarreau5dd17352018-06-14 13:33:30 +02003401 if (unlikely((int)ret <= 0)) {
3402 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003403 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3404 break;
3405 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003406 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003407 total += count;
3408 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003409 h2s->res.state = HTTP_MSG_DONE;
3410 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003411 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003412 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003413 cs->flags |= CS_FL_ERROR;
3414 break;
3415 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003416
3417 total += ret;
3418 count -= ret;
3419
3420 if (h2s->st >= H2_SS_ERROR)
3421 break;
3422
3423 if (h2s->flags & H2_SF_BLK_ANY)
3424 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003425 }
3426
Willy Tarreau00610962018-07-19 10:58:28 +02003427 if (h2s->st >= H2_SS_ERROR) {
3428 /* trim any possibly pending data after we close (extra CR-LF,
3429 * unprocessed trailers, abnormal extra data, ...)
3430 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003431 total += count;
3432 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003433 }
3434
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003435 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003436 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003437 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003438 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003439 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003440 }
3441
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003442 if (h2s->flags & H2_SF_BLK_SFCTL) {
3443 /* stream flow control, quit the list */
3444 LIST_DEL(&h2s->list);
3445 LIST_INIT(&h2s->list);
3446 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003447 else if (LIST_ISEMPTY(&h2s->list)) {
3448 if (h2s->flags & H2_SF_BLK_MFCTL)
3449 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3450 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3451 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3452 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003453
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003454 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003455}
3456
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003457/* for debugging with CLI's "show fd" command */
3458static void h2_show_fd(struct chunk *msg, struct connection *conn)
3459{
3460 struct h2c *h2c = conn->mux_ctx;
3461 struct h2s *h2s;
3462 struct eb32_node *node;
3463 int fctl_cnt = 0;
3464 int send_cnt = 0;
3465 int tree_cnt = 0;
3466 int orph_cnt = 0;
3467
3468 if (!h2c)
3469 return;
3470
3471 list_for_each_entry(h2s, &h2c->fctl_list, list)
3472 fctl_cnt++;
3473
3474 list_for_each_entry(h2s, &h2c->send_list, list)
3475 send_cnt++;
3476
3477 node = eb32_first(&h2c->streams_by_id);
3478 while (node) {
3479 h2s = container_of(node, struct h2s, by_id);
3480 tree_cnt++;
3481 if (!h2s->cs)
3482 orph_cnt++;
3483 node = eb32_next(node);
3484 }
3485
Willy Tarreauc65edac2018-07-19 10:54:43 +02003486 chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
Willy Tarreau506a29a2018-07-18 10:07:58 +02003487 h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)h2c->dbuf->i, (unsigned int)h2c->dbuf->size, (unsigned int)h2c->mbuf->o, (unsigned int)h2c->mbuf->size);
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003488}
Willy Tarreau62f52692017-10-08 23:01:42 +02003489
3490/*******************************************************/
3491/* functions below are dedicated to the config parsers */
3492/*******************************************************/
3493
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003494/* config parser for global "tune.h2.header-table-size" */
3495static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3496 struct proxy *defpx, const char *file, int line,
3497 char **err)
3498{
3499 if (too_many_args(1, args, err, NULL))
3500 return -1;
3501
3502 h2_settings_header_table_size = atoi(args[1]);
3503 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3504 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3505 return -1;
3506 }
3507 return 0;
3508}
Willy Tarreau62f52692017-10-08 23:01:42 +02003509
Willy Tarreaue6baec02017-07-27 11:45:11 +02003510/* config parser for global "tune.h2.initial-window-size" */
3511static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3512 struct proxy *defpx, const char *file, int line,
3513 char **err)
3514{
3515 if (too_many_args(1, args, err, NULL))
3516 return -1;
3517
3518 h2_settings_initial_window_size = atoi(args[1]);
3519 if (h2_settings_initial_window_size < 0) {
3520 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3521 return -1;
3522 }
3523 return 0;
3524}
3525
Willy Tarreau5242ef82017-07-27 11:47:28 +02003526/* config parser for global "tune.h2.max-concurrent-streams" */
3527static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3528 struct proxy *defpx, const char *file, int line,
3529 char **err)
3530{
3531 if (too_many_args(1, args, err, NULL))
3532 return -1;
3533
3534 h2_settings_max_concurrent_streams = atoi(args[1]);
3535 if (h2_settings_max_concurrent_streams < 0) {
3536 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3537 return -1;
3538 }
3539 return 0;
3540}
3541
Willy Tarreau62f52692017-10-08 23:01:42 +02003542
3543/****************************************/
3544/* MUX initialization and instanciation */
3545/***************************************/
3546
3547/* The mux operations */
3548const struct mux_ops h2_ops = {
3549 .init = h2_init,
3550 .recv = h2_recv,
3551 .send = h2_send,
3552 .wake = h2_wake,
3553 .update_poll = h2_update_poll,
3554 .rcv_buf = h2_rcv_buf,
3555 .snd_buf = h2_snd_buf,
3556 .attach = h2_attach,
3557 .detach = h2_detach,
3558 .shutr = h2_shutr,
3559 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003560 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003561 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003562 .name = "H2",
3563};
3564
3565/* ALPN selection : this mux registers ALPN tolen "h2" */
3566static struct alpn_mux_list alpn_mux_h2 =
3567 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3568
3569/* config keyword parsers */
3570static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003571 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003572 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003573 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003574 { 0, NULL, NULL }
3575}};
3576
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003577static void __h2_deinit(void)
3578{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003579 pool_destroy(pool_head_h2s);
3580 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003581}
3582
Willy Tarreau62f52692017-10-08 23:01:42 +02003583__attribute__((constructor))
3584static void __h2_init(void)
3585{
3586 alpn_register_mux(&alpn_mux_h2);
3587 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003588 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003589 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3590 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003591}