blob: e36ca8f5face9706bc0a8c4e7aed37cd62db9480 [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 Tarreau319994a2017-11-07 11:03:56 +01002216 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002217 break;
2218
2219 /* wrote at least one byte, the buffer is not full anymore */
2220 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2221 }
2222
Willy Tarreaua2af5122017-10-09 11:56:46 +02002223 if (conn->flags & CO_FL_SOCK_WR_SH) {
2224 /* output closed, nothing to send, clear the buffer to release it */
2225 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002227}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002228
Willy Tarreau62f52692017-10-08 23:01:42 +02002229/* callback called on any event by the connection handler.
2230 * It applies changes and returns zero, or < 0 if it wants immediate
2231 * destruction of the connection (which normally doesn not happen in h2).
2232 */
2233static int h2_wake(struct connection *conn)
2234{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002236 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002237
Willy Tarreaud13bf272017-12-14 10:34:52 +01002238 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2239 h2_process_demux(h2c);
2240
2241 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2242 h2c->dbuf->i = 0;
2243
2244 if (h2c->dbuf->i != h2c->dbuf->size)
2245 h2c->flags &= ~H2_CF_DEM_DFULL;
2246 }
2247
Willy Tarreau8ec14062017-12-30 18:08:13 +01002248 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2249 /* frontend is stopping, reload likely in progress, let's try
2250 * to announce a graceful shutdown if not yet done. We don't
2251 * care if it fails, it will be tried again later.
2252 */
2253 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2254 if (h2c->last_sid < 0)
2255 h2c->last_sid = (1U << 31) - 1;
2256 h2c_send_goaway_error(h2c, NULL);
2257 }
2258 }
2259
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002260 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002261 * If we received early data, and the handshake is done, wake
2262 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002263 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002264 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2265 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2266 struct eb32_node *node;
2267 struct h2s *h2s;
2268
2269 h2c->flags |= H2_CF_WAIT_FOR_HS;
2270 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2271
2272 while (node) {
2273 h2s = container_of(node, struct h2s, by_id);
2274 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2275 h2s->cs->data_cb->wake(h2s->cs);
2276 node = eb32_next(node);
2277 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002278 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002279
Willy Tarreau26bd7612017-10-09 16:47:04 +02002280 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002281 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2282 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2283 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002284 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002285
2286 if (eb_is_empty(&h2c->streams_by_id)) {
2287 /* no more stream, kill the connection now */
2288 h2_release(conn);
2289 return -1;
2290 }
2291 else {
2292 /* some streams still there, we need to signal them all and
2293 * wait for their departure.
2294 */
2295 __conn_xprt_stop_recv(conn);
2296 __conn_xprt_stop_send(conn);
2297 return 0;
2298 }
2299 }
2300
2301 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002302 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002303
2304 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002305 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002306 __conn_xprt_stop_recv(conn);
2307 }
2308 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002309 __conn_xprt_want_recv(conn);
2310 }
2311
2312 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002313 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2314 (h2c->st0 == H2_CS_ERROR ||
2315 h2c->mbuf->o ||
2316 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2317 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002318 __conn_xprt_want_send(conn);
2319 }
2320 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002321 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002322 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002323 }
2324
Willy Tarreau3f133572017-10-31 19:21:06 +01002325 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002326 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002327 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002328 task_queue(h2c->task);
2329 }
2330 else
2331 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002332 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002333 return 0;
2334}
2335
Willy Tarreauea392822017-10-31 10:02:25 +01002336/* Connection timeout management. The principle is that if there's no receipt
2337 * nor sending for a certain amount of time, the connection is closed. If the
2338 * MUX buffer still has lying data or is not allocatable, the connection is
2339 * immediately killed. If it's allocatable and empty, we attempt to send a
2340 * GOAWAY frame.
2341 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002342static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002343{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002344 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002345 int expired = tick_is_expired(t->expire, now_ms);
2346
Willy Tarreau0975f112018-03-29 15:22:59 +02002347 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002348 return t;
2349
Willy Tarreau0975f112018-03-29 15:22:59 +02002350 task_delete(t);
2351 task_free(t);
2352
2353 if (!h2c) {
2354 /* resources were already deleted */
2355 return NULL;
2356 }
2357
2358 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002359 h2c_error(h2c, H2_ERR_NO_ERROR);
2360 h2_wake_some_streams(h2c, 0, 0);
2361
2362 if (h2c->mbuf->o) {
2363 /* don't even try to send a GOAWAY, the buffer is stuck */
2364 h2c->flags |= H2_CF_GOAWAY_FAILED;
2365 }
2366
2367 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002368 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002369 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2370 h2c->flags |= H2_CF_GOAWAY_FAILED;
2371
2372 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2373 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2374
Willy Tarreau0975f112018-03-29 15:22:59 +02002375 /* either we can release everything now or it will be done later once
2376 * the last stream closes.
2377 */
2378 if (eb_is_empty(&h2c->streams_by_id))
2379 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002380
Willy Tarreauea392822017-10-31 10:02:25 +01002381 return NULL;
2382}
2383
2384
Willy Tarreau62f52692017-10-08 23:01:42 +02002385/*******************************************/
2386/* functions below are used by the streams */
2387/*******************************************/
2388
2389/*
2390 * Attach a new stream to a connection
2391 * (Used for outgoing connections)
2392 */
2393static struct conn_stream *h2_attach(struct connection *conn)
2394{
2395 return NULL;
2396}
2397
2398/* callback used to update the mux's polling flags after changing a cs' status.
2399 * The caller (cs_update_mux_polling) will take care of propagating any changes
2400 * to the transport layer.
2401 */
2402static void h2_update_poll(struct conn_stream *cs)
2403{
Willy Tarreau1d393222017-10-17 10:26:19 +02002404 struct h2s *h2s = cs->ctx;
2405
2406 if (!h2s)
2407 return;
2408
Willy Tarreaud7739c82017-10-30 15:38:23 +01002409 /* we may unblock a blocked read */
2410
Willy Tarreau315d8072017-12-10 22:17:57 +01002411 if (cs->flags & CS_FL_DATA_RD_ENA) {
2412 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002413 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002414 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002415 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002416 conn_xprt_want_send(cs->conn);
2417 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002418 }
2419
Willy Tarreau1d393222017-10-17 10:26:19 +02002420 /* Note: the stream and stream-int code doesn't allow us to perform a
2421 * synchronous send() here unfortunately, because this code is called
2422 * as si_update() from the process_stream() context. This means that
2423 * we have to queue the current cs and defer its processing after the
2424 * connection's cs list is processed anyway.
2425 */
2426
2427 if (cs->flags & CS_FL_DATA_WR_ENA) {
2428 if (LIST_ISEMPTY(&h2s->list)) {
2429 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2430 !h2s->h2c->mbuf->o && // not yet subscribed
2431 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2432 conn_xprt_want_send(cs->conn);
2433 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2434 }
2435 }
2436 else if (!LIST_ISEMPTY(&h2s->list)) {
2437 LIST_DEL(&h2s->list);
2438 LIST_INIT(&h2s->list);
2439 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2440 }
2441
2442 /* this can happen from within si_chk_snd() */
2443 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2444 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002445}
2446
2447/*
2448 * Detach the stream from the connection and possibly release the connection.
2449 */
2450static void h2_detach(struct conn_stream *cs)
2451{
Willy Tarreau60935142017-10-16 18:11:19 +02002452 struct h2s *h2s = cs->ctx;
2453 struct h2c *h2c;
2454
2455 cs->ctx = NULL;
2456 if (!h2s)
2457 return;
2458
2459 h2c = h2s->h2c;
2460 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002461 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002462 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2463 !h2_has_too_many_cs(h2c)) {
2464 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2465 if (h2_recv_allowed(h2c)) {
2466 __conn_xprt_want_recv(h2c->conn);
2467 conn_xprt_want_send(h2c->conn);
2468 }
2469 }
Willy Tarreau60935142017-10-16 18:11:19 +02002470
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002471 /* this stream may be blocked waiting for some data to leave (possibly
2472 * an ES or RST frame), so orphan it in this case.
2473 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002474 if (!(cs->conn->flags & CO_FL_ERROR) &&
2475 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002476 return;
2477
Willy Tarreau45f752e2017-10-30 15:44:59 +01002478 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2479 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2480 /* unblock the connection if it was blocked on this
2481 * stream.
2482 */
2483 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2484 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2485 conn_xprt_want_recv(cs->conn);
2486 conn_xprt_want_send(cs->conn);
2487 }
2488
Willy Tarreau71049cc2018-03-28 13:56:39 +02002489 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002490
Willy Tarreaue323f342018-03-28 13:51:45 +02002491 /* We don't want to close right now unless we're removing the
2492 * last stream, and either the connection is in error, or it
2493 * reached the ID already specified in a GOAWAY frame received
2494 * or sent (as seen by last_sid >= 0).
2495 */
2496 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2497 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002498 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002499 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2500 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2501 (conn_xprt_read0_pending(h2c->conn) ||
2502 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2503 /* no more stream will come, kill it now */
2504 h2_release(h2c->conn);
2505 }
2506 else if (h2c->task) {
2507 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2508 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2509 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002510 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002511 else
2512 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002513 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002514}
2515
2516static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2517{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002518 struct h2s *h2s = cs->ctx;
2519
2520 if (!mode)
2521 return;
2522
Willy Tarreau721c9742017-11-07 11:05:42 +01002523 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002524 return;
2525
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002526 /* if no outgoing data was seen on this stream, it means it was
2527 * closed with a "tcp-request content" rule that is normally
2528 * used to kill the connection ASAP (eg: limit abuse). In this
2529 * case we send a goaway to close the connection.
2530 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002531 if (!(h2s->flags & H2_SF_RST_SENT) &&
2532 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002533 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002534
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002535 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2536 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2537 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002538 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002539
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002540 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2541 conn_xprt_want_send(cs->conn);
2542
Willy Tarreau00dd0782018-03-01 16:31:34 +01002543 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002544
2545 add_to_list:
2546 if (LIST_ISEMPTY(&h2s->list)) {
2547 if (h2s->flags & H2_SF_BLK_MFCTL)
2548 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2549 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2550 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2551 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002552}
2553
2554static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2555{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002556 struct h2s *h2s = cs->ctx;
2557
Willy Tarreau721c9742017-11-07 11:05:42 +01002558 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002559 return;
2560
Willy Tarreau67434202017-11-06 20:20:51 +01002561 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002562 /* we can cleanly close using an empty data frame only after headers */
2563
2564 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2565 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002566 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002567
2568 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002569 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002570 else
2571 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002572 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002573 /* if no outgoing data was seen on this stream, it means it was
2574 * closed with a "tcp-request content" rule that is normally
2575 * used to kill the connection ASAP (eg: limit abuse). In this
2576 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002577 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002578 if (!(h2s->flags & H2_SF_RST_SENT) &&
2579 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002580 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002581
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002582 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2583 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002584 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002585 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002586
Willy Tarreau00dd0782018-03-01 16:31:34 +01002587 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002588 }
2589
2590 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2591 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002592
2593 add_to_list:
2594 if (LIST_ISEMPTY(&h2s->list)) {
2595 if (h2s->flags & H2_SF_BLK_MFCTL)
2596 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2597 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2598 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2599 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002600}
2601
Willy Tarreau13278b42017-10-13 19:23:14 +02002602/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2603 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2604 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002605 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002606 */
2607static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2608{
2609 struct h2c *h2c = h2s->h2c;
2610 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002611 struct chunk *tmp = get_trash_chunk();
2612 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002613 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002614 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002615 int flen = h2c->dfl;
2616 int outlen = 0;
2617 int wrap;
2618 int try;
2619
2620 if (!h2c->dfl) {
2621 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002622 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002623 return 0;
2624 }
2625
Willy Tarreau68472622017-12-11 18:36:37 +01002626 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2627 return 0; // incomplete input frame
2628
Willy Tarreau13278b42017-10-13 19:23:14 +02002629 /* if the input buffer wraps, take a temporary copy of it (rare) */
2630 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2631 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002632 copy = alloc_trash_chunk();
2633 if (!copy) {
2634 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2635 goto fail;
2636 }
2637 memcpy(copy->str, h2c->dbuf->p, wrap);
2638 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2639 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002640 }
2641
2642 /* The padlen is the first byte before data, and the padding appears
2643 * after data. padlen+data+padding are included in flen.
2644 */
2645 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002646 h2c->dpl = *hdrs;
2647 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002648 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2649 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002650 return 0;
2651 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002652 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002653 hdrs += 1; // skip Pad Length
2654 }
2655
2656 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2657 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002658 if (read_n32(hdrs) == h2s->id) {
2659 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2660 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2661 return 0;//goto fail_stream;
2662 }
2663
Willy Tarreau13278b42017-10-13 19:23:14 +02002664 hdrs += 5; // stream dep = 4, weight = 1
2665 flen -= 5;
2666 }
2667
2668 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2669 * don't support this for now and can't even decompress so we have to
2670 * break the connection.
2671 */
2672 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2673 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002674 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002675 }
2676
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002677 /* we can't retry a failed decompression operation so we must be very
2678 * careful not to take any risks. In practice the output buffer is
2679 * always empty except maybe for trailers, so these operations almost
2680 * never happen.
2681 */
2682 if (unlikely(buf->o)) {
2683 /* need to let the output buffer flush and
2684 * mark the buffer for later wake up.
2685 */
2686 goto fail;
2687 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002688
Willy Tarreauaa7af722018-07-12 10:33:12 +02002689 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002690 /* it doesn't fit and the buffer is fragmented,
2691 * so let's defragment it and try again.
2692 */
Willy Tarreau0db4d102018-07-12 11:00:01 +02002693 b_slow_realign(buf, trash.str, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002694 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002695
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002696 /* first check if we have some room after p+i */
2697 try = buf->data + buf->size - (buf->p + buf->i);
2698
2699 /* otherwise continue between data and p-o */
2700 if (try <= 0) {
2701 try = buf->p - (buf->data + buf->o);
2702 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002703 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002704 }
2705 if (try > count)
2706 try = count;
2707
2708 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2709 sizeof(list)/sizeof(list[0]), tmp);
2710 if (outlen < 0) {
2711 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2712 goto fail;
2713 }
2714
2715 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002716 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02002717 outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002718
2719 if (outlen < 0) {
2720 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2721 goto fail;
2722 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002723
Willy Tarreau174b06a2018-04-25 18:13:58 +02002724 if (msgf & H2_MSGF_BODY) {
2725 /* a payload is present */
2726 if (msgf & H2_MSGF_BODY_CL)
2727 h2s->flags |= H2_SF_DATA_CLEN;
2728 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2729 h2s->flags |= H2_SF_DATA_CHNK;
2730 }
2731
Willy Tarreau13278b42017-10-13 19:23:14 +02002732 /* now consume the input data */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002733 b_del(h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002734 h2c->st0 = H2_CS_FRAME_H;
2735 buf->i += outlen;
2736
2737 /* don't send it before returning data!
2738 * FIXME: should we instead try to send it much later, after the
2739 * response ? This would require that we keep a copy of it in h2s.
2740 */
2741 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2742 h2s->cs->flags |= CS_FL_EOS;
2743 h2s->flags |= H2_SF_ES_RCVD;
2744 }
2745
Willy Tarreau68dd9852017-07-03 14:44:26 +02002746 leave:
2747 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002748 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002749 fail:
2750 outlen = 0;
2751 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002752}
2753
Willy Tarreau454f9052017-10-26 19:40:35 +02002754/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2755 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2756 * in use, a new chunk is emitted for each frame. This is supposed to fit
2757 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2758 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2759 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2760 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002761 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2762 * checked to know if some data remain pending (an empty DATA frame can return
2763 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2764 * connection errors in h2c->errcode. The caller must already have checked the
2765 * frame header and ensured that the frame was complete or the buffer full. It
2766 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002767 */
2768static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2769{
2770 struct h2c *h2c = h2s->h2c;
2771 int block1, block2;
2772 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002773 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002774
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002775 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002776 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002777
2778 /* The padlen is the first byte before data, and the padding appears
2779 * after data. padlen+data+padding are included in flen.
2780 */
Willy Tarreau79127812017-12-03 21:06:59 +01002781 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002782 if (h2c->dbuf->i < 1)
2783 return 0;
2784
Willy Tarreaudda2e412018-06-07 18:08:04 +02002785 h2c->dpl = *(uint8_t *)b_head(h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002786 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002787 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2788 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002789 return 0;
2790 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002791
2792 /* skip the padlen byte */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002793 b_del(h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002794 h2c->dfl--;
2795 h2c->rcvd_c++; h2c->rcvd_s++;
2796 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002797 }
2798
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002799 flen = h2c->dfl - h2c->dpl;
2800 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002801 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002802
2803 if (flen > h2c->dbuf->i) {
2804 flen = h2c->dbuf->i;
2805 if (!flen)
2806 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002807 }
2808
Willy Tarreaueba10f22018-04-25 20:44:22 +02002809 /* chunked-encoding requires more room */
2810 if (h2s->flags & H2_SF_DATA_CHNK) {
2811 chklen = MIN(flen, count);
2812 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2813 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2814 (chklen < 1048576) ? 4 : 8;
2815 chklen += 4; // CRLF, CRLF
2816 }
2817
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002818 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002819 if (flen + chklen > count) {
2820 if (chklen >= count)
2821 goto full;
2822 flen = count - chklen;
2823 }
2824
2825 if (h2s->flags & H2_SF_DATA_CHNK) {
2826 /* emit the chunk size */
2827 unsigned int chksz = flen;
2828 char str[10];
2829 char *beg;
2830
2831 beg = str + sizeof(str);
2832 *--beg = '\n';
2833 *--beg = '\r';
2834 do {
2835 *--beg = hextab[chksz & 0xF];
2836 } while (chksz >>= 4);
2837 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002838 }
2839
Willy Tarreau454f9052017-10-26 19:40:35 +02002840 /* Block1 is the length of the first block before the buffer wraps,
2841 * block2 is the optional second block to reach the end of the frame.
2842 */
Willy Tarreau7194d3c2018-06-06 16:55:45 +02002843 block1 = b_contig_data(h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002844 if (block1 > flen)
2845 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002846 block2 = flen - block1;
2847
2848 if (block1)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002849 bi_putblk(buf, b_head(h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002850
2851 if (block2)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002852 bi_putblk(buf, b_peek(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002853
Willy Tarreaueba10f22018-04-25 20:44:22 +02002854 if (h2s->flags & H2_SF_DATA_CHNK) {
2855 /* emit the CRLF */
2856 bi_putblk(buf, "\r\n", 2);
2857 }
2858
Willy Tarreau454f9052017-10-26 19:40:35 +02002859 /* now mark the input data as consumed (will be deleted from the buffer
2860 * by the caller when seeing FRAME_A after sending the window update).
2861 */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002862 b_del(h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002863 h2c->dfl -= flen;
2864 h2c->rcvd_c += flen;
2865 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2866
2867 if (h2c->dfl > h2c->dpl) {
2868 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002869 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002870 }
2871
Willy Tarreau4a28da12018-01-04 14:41:00 +01002872 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002873 /* here we're done with the frame, all the payload (except padding) was
2874 * transferred.
2875 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002876
2877 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2878 /* emit the trailing 0 CRLF CRLF */
2879 if (count < 5)
2880 goto more;
2881 chklen += 5;
2882 bi_putblk(buf, "0\r\n\r\n", 5);
2883 }
2884
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002885 h2c->rcvd_c += h2c->dpl;
2886 h2c->rcvd_s += h2c->dpl;
2887 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002888 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2889
2890 /* don't send it before returning data!
2891 * FIXME: should we instead try to send it much later, after the
2892 * response ? This would require that we keep a copy of it in h2s.
2893 */
Willy Tarreau79127812017-12-03 21:06:59 +01002894 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002895 h2s->cs->flags |= CS_FL_EOS;
2896 h2s->flags |= H2_SF_ES_RCVD;
2897 }
2898
Willy Tarreaueba10f22018-04-25 20:44:22 +02002899 return flen + chklen;
2900 full:
2901 flen = chklen = 0;
2902 more:
2903 h2c->flags |= H2_CF_DEM_SFULL;
2904 h2s->cs->flags |= CS_FL_RCV_MORE;
2905 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002906}
2907
Willy Tarreau62f52692017-10-08 23:01:42 +02002908/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002909 * Called from the upper layer to get more data, up to <count> bytes. The
2910 * caller is responsible for never asking for more data than what is available
2911 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002912 */
2913static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2914{
Willy Tarreau13278b42017-10-13 19:23:14 +02002915 struct h2s *h2s = cs->ctx;
2916 struct h2c *h2c = h2s->h2c;
2917 int ret = 0;
2918
2919 if (h2c->st0 != H2_CS_FRAME_P)
2920 return 0; // no pre-parsed frame yet
2921
2922 if (h2c->dsi != h2s->id)
2923 return 0; // not for us
2924
2925 if (!h2c->dbuf->size)
2926 return 0; // empty buffer
2927
Willy Tarreau13278b42017-10-13 19:23:14 +02002928 switch (h2c->dft) {
2929 case H2_FT_HEADERS:
2930 ret = h2_frt_decode_headers(h2s, buf, count);
2931 break;
2932
Willy Tarreau454f9052017-10-26 19:40:35 +02002933 case H2_FT_DATA:
2934 ret = h2_frt_transfer_data(h2s, buf, count);
2935 break;
2936
Willy Tarreau13278b42017-10-13 19:23:14 +02002937 default:
2938 ret = 0;
2939 }
2940 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002941}
2942
Willy Tarreau5dd17352018-06-14 13:33:30 +02002943/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2944 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2945 * number of bytes sent. The caller must check the stream's status to detect
2946 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002947 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002948static 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 +02002949{
2950 struct http_hdr list[MAX_HTTP_HDR];
2951 struct h2c *h2c = h2s->h2c;
2952 struct h1m *h1m = &h2s->res;
2953 struct chunk outbuf;
2954 int es_now = 0;
2955 int ret = 0;
2956 int hdr;
2957
2958 if (h2c_mux_busy(h2c, h2s)) {
2959 h2s->flags |= H2_SF_BLK_MBUSY;
2960 return 0;
2961 }
2962
Willy Tarreau44e973f2018-03-01 17:49:30 +01002963 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002964 h2c->flags |= H2_CF_MUX_MALLOC;
2965 h2s->flags |= H2_SF_BLK_MROOM;
2966 return 0;
2967 }
2968
2969 /* First, try to parse the H1 response and index it into <list>.
2970 * NOTE! Since it comes from haproxy, we *know* that a response header
2971 * block does not wrap and we can safely read it this way without
2972 * having to realign the buffer.
2973 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02002974 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002975 list, sizeof(list)/sizeof(list[0]), h1m);
2976 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002977 /* incomplete or invalid response, this is abnormal coming from
2978 * haproxy and may only result in a bad errorfile or bad Lua code
2979 * so that won't be fixed, raise an error now.
2980 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002981 * FIXME: we should instead add the ability to only return a
2982 * 502 bad gateway. But in theory this is not supposed to
2983 * happen.
2984 */
2985 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2986 ret = 0;
2987 goto end;
2988 }
2989
2990 chunk_reset(&outbuf);
2991
2992 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02002993 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02002994 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002995 outbuf.len = 0;
2996
Willy Tarreauaa7af722018-07-12 10:33:12 +02002997 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002998 break;
2999 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02003000 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003001 }
3002
3003 if (outbuf.size < 9) {
3004 h2c->flags |= H2_CF_MUX_MFULL;
3005 h2s->flags |= H2_SF_BLK_MROOM;
3006 ret = 0;
3007 goto end;
3008 }
3009
3010 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3011 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
3012 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3013 outbuf.len = 9;
3014
3015 /* encode status, which necessarily is the first one */
3016 if (outbuf.len < outbuf.size && h1m->status == 200)
3017 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
3018 else if (outbuf.len < outbuf.size && h1m->status == 304)
3019 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003020 else if (unlikely(list[0].v.len != 3)) {
3021 /* this is an unparsable response */
3022 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3023 ret = 0;
3024 goto end;
3025 }
3026 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003027 /* basic encoding of the status code */
3028 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3029 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3030 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3031 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3032 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3033 }
3034 else {
Willy Tarreauaa7af722018-07-12 10:33:12 +02003035 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003036 goto realign_again;
3037
3038 h2c->flags |= H2_CF_MUX_MFULL;
3039 h2s->flags |= H2_SF_BLK_MROOM;
3040 ret = 0;
3041 goto end;
3042 }
3043
3044 /* encode all headers, stop at empty name */
3045 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003046 /* these ones do not exist in H2 and must be dropped. */
3047 if (isteq(list[hdr].n, ist("connection")) ||
3048 isteq(list[hdr].n, ist("proxy-connection")) ||
3049 isteq(list[hdr].n, ist("keep-alive")) ||
3050 isteq(list[hdr].n, ist("upgrade")) ||
3051 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003052 continue;
3053
3054 if (isteq(list[hdr].n, ist("")))
3055 break; // end
3056
3057 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3058 /* output full */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003059 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003060 goto realign_again;
3061
3062 h2c->flags |= H2_CF_MUX_MFULL;
3063 h2s->flags |= H2_SF_BLK_MROOM;
3064 ret = 0;
3065 goto end;
3066 }
3067 }
3068
3069 /* we may need to add END_STREAM */
3070 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3071 es_now = 1;
3072
3073 /* update the frame's size */
3074 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3075
3076 if (es_now)
3077 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3078
3079 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003080 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003081
3082 /* commit the H2 response */
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02003083 h2c->mbuf->p = b_peek(h2c->mbuf, h2c->mbuf->o + outbuf.len);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003084 h2c->mbuf->o += outbuf.len;
Willy Tarreau67434202017-11-06 20:20:51 +01003085 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003086
3087 /* for now we don't implemented CONTINUATION, so we wait for a
3088 * body or directly end in TRL2.
3089 */
3090 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003091 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003092 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003093
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094 h1m->state = HTTP_MSG_DONE;
3095 h2s->flags |= H2_SF_ES_SENT;
3096 if (h2s->st == H2_SS_OPEN)
3097 h2s->st = H2_SS_HLOC;
3098 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003099 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003101 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003102 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003103 h1m->state = HTTP_MSG_RPBEFORE;
3104 h1m->status = 0;
3105 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003106 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003107 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003108 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003109 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110
3111 end:
3112 //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));
3113 return ret;
3114}
3115
Willy Tarreau5dd17352018-06-14 13:33:30 +02003116/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3117 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3118 * the number of bytes sent. The caller must check the stream's status to
3119 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003120 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003121static 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 +02003122{
3123 struct h2c *h2c = h2s->h2c;
3124 struct h1m *h1m = &h2s->res;
3125 struct chunk outbuf;
3126 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003127 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003128 int es_now = 0;
3129 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003130 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003131 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003132
3133 if (h2c_mux_busy(h2c, h2s)) {
3134 h2s->flags |= H2_SF_BLK_MBUSY;
3135 goto end;
3136 }
3137
Willy Tarreau44e973f2018-03-01 17:49:30 +01003138 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003139 h2c->flags |= H2_CF_MUX_MALLOC;
3140 h2s->flags |= H2_SF_BLK_MROOM;
3141 goto end;
3142 }
3143
3144 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003145 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003146 goto end;
3147
3148 chunk_reset(&outbuf);
3149
3150 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02003151 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02003152 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003153 outbuf.len = 0;
3154
Willy Tarreauaa7af722018-07-12 10:33:12 +02003155 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003156 break;
3157 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02003158 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003159 }
3160
3161 if (outbuf.size < 9) {
3162 h2c->flags |= H2_CF_MUX_MFULL;
3163 h2s->flags |= H2_SF_BLK_MROOM;
3164 goto end;
3165 }
3166
3167 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3168 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3169 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3170 outbuf.len = 9;
3171
3172 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3173 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003174 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003175 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003176 break;
3177 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003178 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003179 if ((long long)size > h1m->curr_len)
3180 size = h1m->curr_len;
3181 break;
3182 default: /* te:chunked : parse chunks */
3183 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003184 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003185 if (!ret)
3186 goto end;
3187
3188 if (ret < 0) {
3189 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3190 h1m->err_pos = ret;
3191 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3192 goto end;
3193 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003194 max -= ret;
3195 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003196 total += ret;
3197 h1m->state = HTTP_MSG_CHUNK_SIZE;
3198 }
3199
3200 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3201 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003202 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003203 if (!ret)
3204 goto end;
3205
3206 if (ret < 0) {
3207 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3208 h1m->err_pos = ret;
3209 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3210 goto end;
3211 }
3212
3213 size = chunk;
3214 h1m->curr_len = chunk;
3215 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003216 max -= ret;
3217 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003218 total += ret;
3219 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3220 if (!size)
3221 goto send_empty;
3222 }
3223
3224 /* in MSG_DATA state, continue below */
3225 size = h1m->curr_len;
3226 break;
3227 }
3228
3229 /* we have in <size> the exact number of bytes we need to copy from
3230 * the H1 buffer. We need to check this against the connection's and
3231 * the stream's send windows, and to ensure that this fits in the max
3232 * frame size and in the buffer's available space minus 9 bytes (for
3233 * the frame header). The connection's flow control is applied last so
3234 * that we can use a separate list of streams which are immediately
3235 * unblocked on window opening. Note: we don't implement padding.
3236 */
3237
Willy Tarreau5dd17352018-06-14 13:33:30 +02003238 if (size > max)
3239 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003240
3241 if (size > h2s->mws)
3242 size = h2s->mws;
3243
3244 if (size <= 0) {
3245 h2s->flags |= H2_SF_BLK_SFCTL;
3246 goto end;
3247 }
3248
3249 if (h2c->mfs && size > h2c->mfs)
3250 size = h2c->mfs;
3251
3252 if (size + 9 > outbuf.size) {
3253 /* we have an opportunity for enlarging the too small
3254 * available space, let's try.
3255 */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003256 if (b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003257 goto realign_again;
3258 size = outbuf.size - 9;
3259 }
3260
3261 if (size <= 0) {
3262 h2c->flags |= H2_CF_MUX_MFULL;
3263 h2s->flags |= H2_SF_BLK_MROOM;
3264 goto end;
3265 }
3266
3267 if (size > h2c->mws)
3268 size = h2c->mws;
3269
3270 if (size <= 0) {
3271 h2s->flags |= H2_SF_BLK_MFCTL;
3272 goto end;
3273 }
3274
3275 /* copy whatever we can */
3276 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003277 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003278 if (ret == 1)
3279 len2 = 0;
3280
3281 if (!ret || len1 + len2 < size) {
3282 /* FIXME: must normally never happen */
3283 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3284 goto end;
3285 }
3286
3287 /* limit len1/len2 to size */
3288 if (len1 + len2 > size) {
3289 int sub = len1 + len2 - size;
3290
3291 if (len2 > sub)
3292 len2 -= sub;
3293 else {
3294 sub -= len2;
3295 len2 = 0;
3296 len1 -= sub;
3297 }
3298 }
3299
3300 /* now let's copy this this into the output buffer */
3301 memcpy(outbuf.str + 9, blk1, len1);
3302 if (len2)
3303 memcpy(outbuf.str + 9 + len1, blk2, len2);
3304
3305 send_empty:
3306 /* we may need to add END_STREAM */
3307 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3308 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003309 *
3310 * FIXME: what we do here is not correct because we send end_stream
3311 * before knowing if we'll have to send a HEADERS frame for the
3312 * trailers. More importantly we're not consuming the trailing CRLF
3313 * after the end of trailers, so it will be left to the caller to
3314 * eat it. The right way to do it would be to measure trailers here
3315 * and to send ES only if there are no trailers.
3316 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003317 */
3318 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3319 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3320 es_now = 1;
3321
3322 /* update the frame's size */
3323 h2_set_frame_size(outbuf.str, size);
3324
3325 if (es_now)
3326 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3327
3328 /* commit the H2 response */
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02003329 h2c->mbuf->p = b_peek(h2c->mbuf, h2c->mbuf->o + size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003330 h2c->mbuf->o += size + 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003331
3332 /* consume incoming H1 response */
3333 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003334 max -= size;
3335 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336 total += size;
3337 h1m->curr_len -= size;
3338 h2s->mws -= size;
3339 h2c->mws -= size;
3340
3341 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3342 h1m->state = HTTP_MSG_CHUNK_CRLF;
3343 goto new_frame;
3344 }
3345 }
3346
3347 if (es_now) {
3348 if (h2s->st == H2_SS_OPEN)
3349 h2s->st = H2_SS_HLOC;
3350 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003351 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003352
Willy Tarreau35a62702018-02-27 15:37:25 +01003353 if (!(h1m->flags & H1_MF_CHNK)) {
3354 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003355 total += max;
3356 ofs += max;
3357 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003358
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003359 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003360 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003361
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003362 h2s->flags |= H2_SF_ES_SENT;
3363 }
3364
3365 end:
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003366 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 +02003367 return total;
3368}
3369
Willy Tarreau62f52692017-10-08 23:01:42 +02003370/* Called from the upper layer, to send data */
3371static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3372{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003373 struct h2s *h2s = cs->ctx;
Willy Tarreau0bad0432018-06-14 16:54:01 +02003374 size_t count = buf->o;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003375 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003376 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003377
Willy Tarreau0bad0432018-06-14 16:54:01 +02003378 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003379 h2s->flags |= H2_SF_OUTGOING_DATA;
3380
Willy Tarreau0bad0432018-06-14 16:54:01 +02003381 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003382 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003383 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003384 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003385 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003386 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003387 }
3388 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3389 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003390 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003391
Willy Tarreau5dd17352018-06-14 13:33:30 +02003392 if (unlikely((int)ret <= 0)) {
3393 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003394 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3395 break;
3396 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003397 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003398 total += count;
3399 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003400 h2s->res.state = HTTP_MSG_DONE;
3401 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003402 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003403 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003404 cs->flags |= CS_FL_ERROR;
3405 break;
3406 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003407
3408 total += ret;
3409 count -= ret;
3410
3411 if (h2s->st >= H2_SS_ERROR)
3412 break;
3413
3414 if (h2s->flags & H2_SF_BLK_ANY)
3415 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003416 }
3417
Willy Tarreau00610962018-07-19 10:58:28 +02003418 if (h2s->st >= H2_SS_ERROR) {
3419 /* trim any possibly pending data after we close (extra CR-LF,
3420 * unprocessed trailers, abnormal extra data, ...)
3421 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003422 total += count;
3423 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003424 }
3425
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003426 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003427 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003428 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003429 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003430 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003431 }
3432
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003433 if (h2s->flags & H2_SF_BLK_SFCTL) {
3434 /* stream flow control, quit the list */
3435 LIST_DEL(&h2s->list);
3436 LIST_INIT(&h2s->list);
3437 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003438 else if (LIST_ISEMPTY(&h2s->list)) {
3439 if (h2s->flags & H2_SF_BLK_MFCTL)
3440 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3441 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3442 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3443 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003444
Willy Tarreau0bad0432018-06-14 16:54:01 +02003445 b_del(buf, total);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003446 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003447}
3448
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003449/* for debugging with CLI's "show fd" command */
3450static void h2_show_fd(struct chunk *msg, struct connection *conn)
3451{
3452 struct h2c *h2c = conn->mux_ctx;
3453 struct h2s *h2s;
3454 struct eb32_node *node;
3455 int fctl_cnt = 0;
3456 int send_cnt = 0;
3457 int tree_cnt = 0;
3458 int orph_cnt = 0;
3459
3460 if (!h2c)
3461 return;
3462
3463 list_for_each_entry(h2s, &h2c->fctl_list, list)
3464 fctl_cnt++;
3465
3466 list_for_each_entry(h2s, &h2c->send_list, list)
3467 send_cnt++;
3468
3469 node = eb32_first(&h2c->streams_by_id);
3470 while (node) {
3471 h2s = container_of(node, struct h2s, by_id);
3472 tree_cnt++;
3473 if (!h2s->cs)
3474 orph_cnt++;
3475 node = eb32_next(node);
3476 }
3477
Willy Tarreauc65edac2018-07-19 10:54:43 +02003478 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 +02003479 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 +02003480}
Willy Tarreau62f52692017-10-08 23:01:42 +02003481
3482/*******************************************************/
3483/* functions below are dedicated to the config parsers */
3484/*******************************************************/
3485
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003486/* config parser for global "tune.h2.header-table-size" */
3487static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3488 struct proxy *defpx, const char *file, int line,
3489 char **err)
3490{
3491 if (too_many_args(1, args, err, NULL))
3492 return -1;
3493
3494 h2_settings_header_table_size = atoi(args[1]);
3495 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3496 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3497 return -1;
3498 }
3499 return 0;
3500}
Willy Tarreau62f52692017-10-08 23:01:42 +02003501
Willy Tarreaue6baec02017-07-27 11:45:11 +02003502/* config parser for global "tune.h2.initial-window-size" */
3503static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3504 struct proxy *defpx, const char *file, int line,
3505 char **err)
3506{
3507 if (too_many_args(1, args, err, NULL))
3508 return -1;
3509
3510 h2_settings_initial_window_size = atoi(args[1]);
3511 if (h2_settings_initial_window_size < 0) {
3512 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3513 return -1;
3514 }
3515 return 0;
3516}
3517
Willy Tarreau5242ef82017-07-27 11:47:28 +02003518/* config parser for global "tune.h2.max-concurrent-streams" */
3519static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3520 struct proxy *defpx, const char *file, int line,
3521 char **err)
3522{
3523 if (too_many_args(1, args, err, NULL))
3524 return -1;
3525
3526 h2_settings_max_concurrent_streams = atoi(args[1]);
3527 if (h2_settings_max_concurrent_streams < 0) {
3528 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3529 return -1;
3530 }
3531 return 0;
3532}
3533
Willy Tarreau62f52692017-10-08 23:01:42 +02003534
3535/****************************************/
3536/* MUX initialization and instanciation */
3537/***************************************/
3538
3539/* The mux operations */
3540const struct mux_ops h2_ops = {
3541 .init = h2_init,
3542 .recv = h2_recv,
3543 .send = h2_send,
3544 .wake = h2_wake,
3545 .update_poll = h2_update_poll,
3546 .rcv_buf = h2_rcv_buf,
3547 .snd_buf = h2_snd_buf,
3548 .attach = h2_attach,
3549 .detach = h2_detach,
3550 .shutr = h2_shutr,
3551 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003552 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003553 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003554 .name = "H2",
3555};
3556
3557/* ALPN selection : this mux registers ALPN tolen "h2" */
3558static struct alpn_mux_list alpn_mux_h2 =
3559 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3560
3561/* config keyword parsers */
3562static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003563 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003564 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003565 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003566 { 0, NULL, NULL }
3567}};
3568
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003569static void __h2_deinit(void)
3570{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003571 pool_destroy(pool_head_h2s);
3572 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003573}
3574
Willy Tarreau62f52692017-10-08 23:01:42 +02003575__attribute__((constructor))
3576static void __h2_init(void)
3577{
3578 alpn_register_mux(&alpn_mux_h2);
3579 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003580 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003581 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3582 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003583}