blob: 2f41c6fe88c5850e3a75b59d1d24a621cee00712 [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
513 * available in the buffer's input prior to calling this function.
514 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100515static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200516 const struct buffer *b, int o)
517{
518 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
519}
520
Willy Tarreau1f094672017-11-20 21:27:45 +0100521static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200522{
523 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
524}
525
Willy Tarreau1f094672017-11-20 21:27:45 +0100526static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200527{
528 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
529}
530
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200532{
533 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
534}
535
536
Willy Tarreau715d5312017-07-11 15:20:24 +0200537/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
538 * is not obvious. It turns out that H2 headers are neither aligned nor do they
539 * use regular sizes. And to add to the trouble, the buffer may wrap so each
540 * byte read must be checked. The header is formed like this :
541 *
542 * b0 b1 b2 b3 b4 b5..b8
543 * +----------+---------+--------+----+----+----------------------+
544 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
545 * +----------+---------+--------+----+----+----------------------+
546 *
547 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
548 * we get the sid properly aligned and ordered, and 16 bits of len properly
549 * ordered as well. The type and flags can be extracted using bit shifts from
550 * the word, and only one extra read is needed to fetch len[16:23].
551 * Returns zero if some bytes are missing, otherwise non-zero on success.
552 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100553static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200554{
555 uint64_t w;
556
557 if (b->i < 9)
558 return 0;
559
560 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
561 h->len = *b->p << 16;
562 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
563 h->ff = w >> 32;
564 h->ft = w >> 40;
565 h->len += w >> 48;
566 return 1;
567}
568
569/* skip the next 9 bytes corresponding to the frame header possibly parsed by
570 * h2_peek_frame_hdr() above.
571 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200573{
574 bi_del(b, 9);
575}
576
577/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100578static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200579{
580 int ret;
581
582 ret = h2_peek_frame_hdr(b, h);
583 if (ret > 0)
584 h2_skip_frame_hdr(b);
585 return ret;
586}
587
Willy Tarreau00dd0782018-03-01 16:31:34 +0100588/* marks stream <h2s> as CLOSED and decrement the number of active streams for
589 * its connection if the stream was not yet closed. Please use this exclusively
590 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100591 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100592static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100593{
594 if (h2s->st != H2_SS_CLOSED)
595 h2s->h2c->nb_streams--;
596 h2s->st = H2_SS_CLOSED;
597}
598
Willy Tarreau71049cc2018-03-28 13:56:39 +0200599/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
600static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100601{
602 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200603 LIST_DEL(&h2s->list);
604 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100605 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100606 pool_free(pool_head_h2s, h2s);
607}
608
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200609/* creates a new stream <id> on the h2c connection and returns it, or NULL in
610 * case of memory allocation error.
611 */
612static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
613{
614 struct conn_stream *cs;
615 struct h2s *h2s;
616
Willy Tarreaubafbe012017-11-24 17:34:44 +0100617 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200618 if (!h2s)
619 goto out;
620
621 h2s->h2c = h2c;
622 h2s->mws = h2c->miw;
623 h2s->flags = H2_SF_NONE;
624 h2s->errcode = H2_ERR_NO_ERROR;
625 h2s->st = H2_SS_IDLE;
626 h1m_init(&h2s->req);
627 h1m_init(&h2s->res);
628 h2s->by_id.key = h2s->id = id;
629 h2c->max_id = id;
630 LIST_INIT(&h2s->list);
631
632 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100633 h2c->nb_streams++;
634 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
635 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200636
637 cs = cs_new(h2c->conn);
638 if (!cs)
639 goto out_close;
640
641 h2s->cs = cs;
642 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200643 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200644
645 if (stream_create_from_cs(cs) < 0)
646 goto out_free_cs;
647
648 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200649 if (h2_has_too_many_cs(h2c))
650 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200651 return h2s;
652
653 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200654 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200655 cs_free(cs);
656 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200657 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200658 h2s = NULL;
659 out:
660 return h2s;
661}
662
Willy Tarreaube5b7152017-09-25 16:25:39 +0200663/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
664 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
665 * the various settings codes.
666 */
667static int h2c_snd_settings(struct h2c *h2c)
668{
669 struct buffer *res;
670 char buf_data[100]; // enough for 15 settings
671 struct chunk buf;
672 int ret;
673
674 if (h2c_mux_busy(h2c, NULL)) {
675 h2c->flags |= H2_CF_DEM_MBUSY;
676 return 0;
677 }
678
Willy Tarreau44e973f2018-03-01 17:49:30 +0100679 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200680 if (!res) {
681 h2c->flags |= H2_CF_MUX_MALLOC;
682 h2c->flags |= H2_CF_DEM_MROOM;
683 return 0;
684 }
685
686 chunk_init(&buf, buf_data, sizeof(buf_data));
687 chunk_memcpy(&buf,
688 "\x00\x00\x00" /* length : 0 for now */
689 "\x04\x00" /* type : 4 (settings), flags : 0 */
690 "\x00\x00\x00\x00", /* stream ID : 0 */
691 9);
692
693 if (h2_settings_header_table_size != 4096) {
694 char str[6] = "\x00\x01"; /* header_table_size */
695
696 write_n32(str + 2, h2_settings_header_table_size);
697 chunk_memcat(&buf, str, 6);
698 }
699
700 if (h2_settings_initial_window_size != 65535) {
701 char str[6] = "\x00\x04"; /* initial_window_size */
702
703 write_n32(str + 2, h2_settings_initial_window_size);
704 chunk_memcat(&buf, str, 6);
705 }
706
707 if (h2_settings_max_concurrent_streams != 0) {
708 char str[6] = "\x00\x03"; /* max_concurrent_streams */
709
710 /* Note: 0 means "unlimited" for haproxy's config but not for
711 * the protocol, so never send this value!
712 */
713 write_n32(str + 2, h2_settings_max_concurrent_streams);
714 chunk_memcat(&buf, str, 6);
715 }
716
717 if (global.tune.bufsize != 16384) {
718 char str[6] = "\x00\x05"; /* max_frame_size */
719
720 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
721 * match bufsize - rewrite size, but at the moment it seems
722 * that clients don't take care of it.
723 */
724 write_n32(str + 2, global.tune.bufsize);
725 chunk_memcat(&buf, str, 6);
726 }
727
728 h2_set_frame_size(buf.str, buf.len - 9);
729 ret = bo_istput(res, ist2(buf.str, buf.len));
730 if (unlikely(ret <= 0)) {
731 if (!ret) {
732 h2c->flags |= H2_CF_MUX_MFULL;
733 h2c->flags |= H2_CF_DEM_MROOM;
734 return 0;
735 }
736 else {
737 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
738 return 0;
739 }
740 }
741 return ret;
742}
743
Willy Tarreau52eed752017-09-22 15:05:09 +0200744/* Try to receive a connection preface, then upon success try to send our
745 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
746 * missing data. It may return an error in h2c.
747 */
748static int h2c_frt_recv_preface(struct h2c *h2c)
749{
750 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200751 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200752
753 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
754
755 if (unlikely(ret1 <= 0)) {
756 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
757 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
758 return 0;
759 }
760
Willy Tarreaube5b7152017-09-25 16:25:39 +0200761 ret2 = h2c_snd_settings(h2c);
762 if (ret2 > 0)
763 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200764
Willy Tarreaube5b7152017-09-25 16:25:39 +0200765 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200766}
767
Willy Tarreau081d4722017-05-16 21:51:05 +0200768/* try to send a GOAWAY frame on the connection to report an error or a graceful
769 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
770 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
771 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
772 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
773 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
774 * on unrecoverable failure. It will not attempt to send one again in this last
775 * case so that it is safe to use h2c_error() to report such errors.
776 */
777static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
778{
779 struct buffer *res;
780 char str[17];
781 int ret;
782
783 if (h2c->flags & H2_CF_GOAWAY_FAILED)
784 return 1; // claim that it worked
785
786 if (h2c_mux_busy(h2c, h2s)) {
787 if (h2s)
788 h2s->flags |= H2_SF_BLK_MBUSY;
789 else
790 h2c->flags |= H2_CF_DEM_MBUSY;
791 return 0;
792 }
793
Willy Tarreau44e973f2018-03-01 17:49:30 +0100794 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200795 if (!res) {
796 h2c->flags |= H2_CF_MUX_MALLOC;
797 if (h2s)
798 h2s->flags |= H2_SF_BLK_MROOM;
799 else
800 h2c->flags |= H2_CF_DEM_MROOM;
801 return 0;
802 }
803
804 /* len: 8, type: 7, flags: none, sid: 0 */
805 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
806
807 if (h2c->last_sid < 0)
808 h2c->last_sid = h2c->max_id;
809
810 write_n32(str + 9, h2c->last_sid);
811 write_n32(str + 13, h2c->errcode);
812 ret = bo_istput(res, ist2(str, 17));
813 if (unlikely(ret <= 0)) {
814 if (!ret) {
815 h2c->flags |= H2_CF_MUX_MFULL;
816 if (h2s)
817 h2s->flags |= H2_SF_BLK_MROOM;
818 else
819 h2c->flags |= H2_CF_DEM_MROOM;
820 return 0;
821 }
822 else {
823 /* we cannot report this error using GOAWAY, so we mark
824 * it and claim a success.
825 */
826 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
827 h2c->flags |= H2_CF_GOAWAY_FAILED;
828 return 1;
829 }
830 }
831 h2c->flags |= H2_CF_GOAWAY_SENT;
832 return ret;
833}
834
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100835/* Try to send an RST_STREAM frame on the connection for the indicated stream
836 * during mux operations. This stream must be valid and cannot be closed
837 * already. h2s->id will be used for the stream ID and h2s->errcode will be
838 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
839 * not yet.
840 *
841 * Returns > 0 on success or zero if nothing was done. In case of lack of room
842 * to write the message, it subscribes the stream to future notifications.
843 */
844static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
845{
846 struct buffer *res;
847 char str[13];
848 int ret;
849
850 if (!h2s || h2s->st == H2_SS_CLOSED)
851 return 1;
852
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100853 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
854 * RST_STREAM in response to a RST_STREAM frame.
855 */
856 if (h2c->dft == H2_FT_RST_STREAM) {
857 ret = 1;
858 goto ignore;
859 }
860
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100861 if (h2c_mux_busy(h2c, h2s)) {
862 h2s->flags |= H2_SF_BLK_MBUSY;
863 return 0;
864 }
865
Willy Tarreau44e973f2018-03-01 17:49:30 +0100866 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100867 if (!res) {
868 h2c->flags |= H2_CF_MUX_MALLOC;
869 h2s->flags |= H2_SF_BLK_MROOM;
870 return 0;
871 }
872
873 /* len: 4, type: 3, flags: none */
874 memcpy(str, "\x00\x00\x04\x03\x00", 5);
875 write_n32(str + 5, h2s->id);
876 write_n32(str + 9, h2s->errcode);
877 ret = bo_istput(res, ist2(str, 13));
878
879 if (unlikely(ret <= 0)) {
880 if (!ret) {
881 h2c->flags |= H2_CF_MUX_MFULL;
882 h2s->flags |= H2_SF_BLK_MROOM;
883 return 0;
884 }
885 else {
886 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
887 return 0;
888 }
889 }
890
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100891 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100892 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100893 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100894 return ret;
895}
896
897/* Try to send an RST_STREAM frame on the connection for the stream being
898 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
899 * error code unless the stream's state already is IDLE or CLOSED in which
900 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
901 * it was not yet.
902 *
903 * Returns > 0 on success or zero if nothing was done. In case of lack of room
904 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200905 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100906 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200907 */
908static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
909{
910 struct buffer *res;
911 char str[13];
912 int ret;
913
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100914 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
915 * RST_STREAM in response to a RST_STREAM frame.
916 */
917 if (h2c->dft == H2_FT_RST_STREAM) {
918 ret = 1;
919 goto ignore;
920 }
921
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200924 return 0;
925 }
926
Willy Tarreau44e973f2018-03-01 17:49:30 +0100927 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200928 if (!res) {
929 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100930 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200931 return 0;
932 }
933
934 /* len: 4, type: 3, flags: none */
935 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100936
Willy Tarreau27a84c92017-10-17 08:10:17 +0200937 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100938 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200939 h2s->errcode : H2_ERR_STREAM_CLOSED);
940 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100941
Willy Tarreau27a84c92017-10-17 08:10:17 +0200942 if (unlikely(ret <= 0)) {
943 if (!ret) {
944 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200946 return 0;
947 }
948 else {
949 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
950 return 0;
951 }
952 }
953
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100954 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100955 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200956 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100957 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100958 }
959
Willy Tarreau27a84c92017-10-17 08:10:17 +0200960 return ret;
961}
962
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100963/* try to send an empty DATA frame with the ES flag set to notify about the
964 * end of stream and match a shutdown(write). If an ES was already sent as
965 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
966 * on success or zero if nothing was done. In case of lack of room to write the
967 * message, it subscribes the requesting stream to future notifications.
968 */
969static int h2_send_empty_data_es(struct h2s *h2s)
970{
971 struct h2c *h2c = h2s->h2c;
972 struct buffer *res;
973 char str[9];
974 int ret;
975
Willy Tarreau721c9742017-11-07 11:05:42 +0100976 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100977 return 1;
978
979 if (h2c_mux_busy(h2c, h2s)) {
980 h2s->flags |= H2_SF_BLK_MBUSY;
981 return 0;
982 }
983
Willy Tarreau44e973f2018-03-01 17:49:30 +0100984 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100985 if (!res) {
986 h2c->flags |= H2_CF_MUX_MALLOC;
987 h2s->flags |= H2_SF_BLK_MROOM;
988 return 0;
989 }
990
991 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
992 memcpy(str, "\x00\x00\x00\x00\x01", 5);
993 write_n32(str + 5, h2s->id);
994 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100995 if (likely(ret > 0)) {
996 h2s->flags |= H2_SF_ES_SENT;
997 }
998 else if (!ret) {
999 h2c->flags |= H2_CF_MUX_MFULL;
1000 h2s->flags |= H2_SF_BLK_MROOM;
1001 return 0;
1002 }
1003 else {
1004 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1005 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001006 }
1007 return ret;
1008}
1009
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001010/* wake the streams attached to the connection, whose id is greater than <last>,
1011 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1012 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1013 * stream's state is automatically updated accordingly.
1014 */
1015static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1016{
1017 struct eb32_node *node;
1018 struct h2s *h2s;
1019
1020 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1021 flags |= CS_FL_ERROR;
1022
1023 if (conn_xprt_read0_pending(h2c->conn))
1024 flags |= CS_FL_EOS;
1025
1026 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1027 while (node) {
1028 h2s = container_of(node, struct h2s, by_id);
1029 if (h2s->id <= last)
1030 break;
1031 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001032
1033 if (!h2s->cs) {
1034 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001035 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001036 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001037 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001038
1039 h2s->cs->flags |= flags;
1040 /* recv is used to force to detect CS_FL_EOS that wake()
1041 * doesn't handle in the stream int code.
1042 */
1043 h2s->cs->data_cb->recv(h2s->cs);
1044 h2s->cs->data_cb->wake(h2s->cs);
1045
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001046 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1047 h2s->st = H2_SS_ERROR;
1048 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1049 h2s->st = H2_SS_HREM;
1050 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001051 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001052 }
1053}
1054
Willy Tarreau3421aba2017-07-27 15:41:03 +02001055/* Increase all streams' outgoing window size by the difference passed in
1056 * argument. This is needed upon receipt of the settings frame if the initial
1057 * window size is different. The difference may be negative and the resulting
1058 * window size as well, for the time it takes to receive some window updates.
1059 */
1060static void h2c_update_all_ws(struct h2c *h2c, int diff)
1061{
1062 struct h2s *h2s;
1063 struct eb32_node *node;
1064
1065 if (!diff)
1066 return;
1067
1068 node = eb32_first(&h2c->streams_by_id);
1069 while (node) {
1070 h2s = container_of(node, struct h2s, by_id);
1071 h2s->mws += diff;
1072 node = eb32_next(node);
1073 }
1074}
1075
1076/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1077 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1078 * return an error in h2c. Described in RFC7540#6.5.
1079 */
1080static int h2c_handle_settings(struct h2c *h2c)
1081{
1082 unsigned int offset;
1083 int error;
1084
1085 if (h2c->dff & H2_F_SETTINGS_ACK) {
1086 if (h2c->dfl) {
1087 error = H2_ERR_FRAME_SIZE_ERROR;
1088 goto fail;
1089 }
1090 return 1;
1091 }
1092
1093 if (h2c->dsi != 0) {
1094 error = H2_ERR_PROTOCOL_ERROR;
1095 goto fail;
1096 }
1097
1098 if (h2c->dfl % 6) {
1099 error = H2_ERR_FRAME_SIZE_ERROR;
1100 goto fail;
1101 }
1102
1103 /* that's the limit we can process */
1104 if (h2c->dfl > global.tune.bufsize) {
1105 error = H2_ERR_FRAME_SIZE_ERROR;
1106 goto fail;
1107 }
1108
1109 /* process full frame only */
1110 if (h2c->dbuf->i < h2c->dfl)
1111 return 0;
1112
1113 /* parse the frame */
1114 for (offset = 0; offset < h2c->dfl; offset += 6) {
1115 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1116 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1117
1118 switch (type) {
1119 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1120 /* we need to update all existing streams with the
1121 * difference from the previous iws.
1122 */
1123 if (arg < 0) { // RFC7540#6.5.2
1124 error = H2_ERR_FLOW_CONTROL_ERROR;
1125 goto fail;
1126 }
1127 h2c_update_all_ws(h2c, arg - h2c->miw);
1128 h2c->miw = arg;
1129 break;
1130 case H2_SETTINGS_MAX_FRAME_SIZE:
1131 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1132 error = H2_ERR_PROTOCOL_ERROR;
1133 goto fail;
1134 }
1135 h2c->mfs = arg;
1136 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001137 case H2_SETTINGS_ENABLE_PUSH:
1138 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1139 error = H2_ERR_PROTOCOL_ERROR;
1140 goto fail;
1141 }
1142 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001143 }
1144 }
1145
1146 /* need to ACK this frame now */
1147 h2c->st0 = H2_CS_FRAME_A;
1148 return 1;
1149 fail:
1150 h2c_error(h2c, error);
1151 return 0;
1152}
1153
1154/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1155 * success or one of the h2_status values.
1156 */
1157static int h2c_ack_settings(struct h2c *h2c)
1158{
1159 struct buffer *res;
1160 char str[9];
1161 int ret = -1;
1162
1163 if (h2c_mux_busy(h2c, NULL)) {
1164 h2c->flags |= H2_CF_DEM_MBUSY;
1165 return 0;
1166 }
1167
Willy Tarreau44e973f2018-03-01 17:49:30 +01001168 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001169 if (!res) {
1170 h2c->flags |= H2_CF_MUX_MALLOC;
1171 h2c->flags |= H2_CF_DEM_MROOM;
1172 return 0;
1173 }
1174
1175 memcpy(str,
1176 "\x00\x00\x00" /* length : 0 (no data) */
1177 "\x04" "\x01" /* type : 4, flags : ACK */
1178 "\x00\x00\x00\x00" /* stream ID */, 9);
1179
1180 ret = bo_istput(res, ist2(str, 9));
1181 if (unlikely(ret <= 0)) {
1182 if (!ret) {
1183 h2c->flags |= H2_CF_MUX_MFULL;
1184 h2c->flags |= H2_CF_DEM_MROOM;
1185 return 0;
1186 }
1187 else {
1188 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1189 return 0;
1190 }
1191 }
1192 return ret;
1193}
1194
Willy Tarreaucf68c782017-10-10 17:11:41 +02001195/* processes a PING frame and schedules an ACK if needed. The caller must pass
1196 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1197 * missing data. It may return an error in h2c.
1198 */
1199static int h2c_handle_ping(struct h2c *h2c)
1200{
1201 /* frame length must be exactly 8 */
1202 if (h2c->dfl != 8) {
1203 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1204 return 0;
1205 }
1206
1207 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001208 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001209 h2c->st0 = H2_CS_FRAME_A;
1210 return 1;
1211}
1212
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001213/* Try to send a window update for stream id <sid> and value <increment>.
1214 * Returns > 0 on success or zero on missing room or failure. It may return an
1215 * error in h2c.
1216 */
1217static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1218{
1219 struct buffer *res;
1220 char str[13];
1221 int ret = -1;
1222
1223 if (h2c_mux_busy(h2c, NULL)) {
1224 h2c->flags |= H2_CF_DEM_MBUSY;
1225 return 0;
1226 }
1227
Willy Tarreau44e973f2018-03-01 17:49:30 +01001228 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001229 if (!res) {
1230 h2c->flags |= H2_CF_MUX_MALLOC;
1231 h2c->flags |= H2_CF_DEM_MROOM;
1232 return 0;
1233 }
1234
1235 /* length: 4, type: 8, flags: none */
1236 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1237 write_n32(str + 5, sid);
1238 write_n32(str + 9, increment);
1239
1240 ret = bo_istput(res, ist2(str, 13));
1241
1242 if (unlikely(ret <= 0)) {
1243 if (!ret) {
1244 h2c->flags |= H2_CF_MUX_MFULL;
1245 h2c->flags |= H2_CF_DEM_MROOM;
1246 return 0;
1247 }
1248 else {
1249 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1250 return 0;
1251 }
1252 }
1253 return ret;
1254}
1255
1256/* try to send pending window update for the connection. It's safe to call it
1257 * with no pending updates. Returns > 0 on success or zero on missing room or
1258 * failure. It may return an error in h2c.
1259 */
1260static int h2c_send_conn_wu(struct h2c *h2c)
1261{
1262 int ret = 1;
1263
1264 if (h2c->rcvd_c <= 0)
1265 return 1;
1266
1267 /* send WU for the connection */
1268 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1269 if (ret > 0)
1270 h2c->rcvd_c = 0;
1271
1272 return ret;
1273}
1274
1275/* try to send pending window update for the current dmux stream. It's safe to
1276 * call it with no pending updates. Returns > 0 on success or zero on missing
1277 * room or failure. It may return an error in h2c.
1278 */
1279static int h2c_send_strm_wu(struct h2c *h2c)
1280{
1281 int ret = 1;
1282
1283 if (h2c->rcvd_s <= 0)
1284 return 1;
1285
1286 /* send WU for the stream */
1287 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1288 if (ret > 0)
1289 h2c->rcvd_s = 0;
1290
1291 return ret;
1292}
1293
Willy Tarreaucf68c782017-10-10 17:11:41 +02001294/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1295 * success, 0 on missing data or one of the h2_status values.
1296 */
1297static int h2c_ack_ping(struct h2c *h2c)
1298{
1299 struct buffer *res;
1300 char str[17];
1301 int ret = -1;
1302
1303 if (h2c->dbuf->i < 8)
1304 return 0;
1305
1306 if (h2c_mux_busy(h2c, NULL)) {
1307 h2c->flags |= H2_CF_DEM_MBUSY;
1308 return 0;
1309 }
1310
Willy Tarreau44e973f2018-03-01 17:49:30 +01001311 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001312 if (!res) {
1313 h2c->flags |= H2_CF_MUX_MALLOC;
1314 h2c->flags |= H2_CF_DEM_MROOM;
1315 return 0;
1316 }
1317
1318 memcpy(str,
1319 "\x00\x00\x08" /* length : 8 (same payload) */
1320 "\x06" "\x01" /* type : 6, flags : ACK */
1321 "\x00\x00\x00\x00" /* stream ID */, 9);
1322
1323 /* copy the original payload */
1324 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1325
1326 ret = bo_istput(res, ist2(str, 17));
1327 if (unlikely(ret <= 0)) {
1328 if (!ret) {
1329 h2c->flags |= H2_CF_MUX_MFULL;
1330 h2c->flags |= H2_CF_DEM_MROOM;
1331 return 0;
1332 }
1333 else {
1334 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1335 return 0;
1336 }
1337 }
1338 return ret;
1339}
1340
Willy Tarreau26f95952017-07-27 17:18:30 +02001341/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1342 * Returns > 0 on success or zero on missing data. It may return an error in
1343 * h2c or h2s. Described in RFC7540#6.9.
1344 */
1345static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1346{
1347 int32_t inc;
1348 int error;
1349
1350 if (h2c->dfl != 4) {
1351 error = H2_ERR_FRAME_SIZE_ERROR;
1352 goto conn_err;
1353 }
1354
1355 /* process full frame only */
1356 if (h2c->dbuf->i < h2c->dfl)
1357 return 0;
1358
1359 inc = h2_get_n32(h2c->dbuf, 0);
1360
1361 if (h2c->dsi != 0) {
1362 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001363
1364 /* it's not an error to receive WU on a closed stream */
1365 if (h2s->st == H2_SS_CLOSED)
1366 return 1;
1367
1368 if (!inc) {
1369 error = H2_ERR_PROTOCOL_ERROR;
1370 goto strm_err;
1371 }
1372
1373 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1374 error = H2_ERR_FLOW_CONTROL_ERROR;
1375 goto strm_err;
1376 }
1377
1378 h2s->mws += inc;
1379 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1380 h2s->flags &= ~H2_SF_BLK_SFCTL;
1381 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1382 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1383 /* This stream wanted to send but could not due to its
1384 * own flow control. We can put it back into the send
1385 * list now, it will be handled upon next send() call.
1386 */
1387 LIST_ADDQ(&h2c->send_list, &h2s->list);
1388 }
1389 }
1390 }
1391 else {
1392 /* connection window update */
1393 if (!inc) {
1394 error = H2_ERR_PROTOCOL_ERROR;
1395 goto conn_err;
1396 }
1397
1398 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1399 error = H2_ERR_FLOW_CONTROL_ERROR;
1400 goto conn_err;
1401 }
1402
1403 h2c->mws += inc;
1404 }
1405
1406 return 1;
1407
1408 conn_err:
1409 h2c_error(h2c, error);
1410 return 0;
1411
1412 strm_err:
1413 if (h2s) {
1414 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001415 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001416 }
1417 else
1418 h2c_error(h2c, error);
1419 return 0;
1420}
1421
Willy Tarreaue96b0922017-10-30 00:28:29 +01001422/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1423 * the last ID. Returns > 0 on success or zero on missing data. It may return
1424 * an error in h2c. Described in RFC7540#6.8.
1425 */
1426static int h2c_handle_goaway(struct h2c *h2c)
1427{
1428 int error;
1429 int last;
1430
1431 if (h2c->dsi != 0) {
1432 error = H2_ERR_PROTOCOL_ERROR;
1433 goto conn_err;
1434 }
1435
1436 if (h2c->dfl < 8) {
1437 error = H2_ERR_FRAME_SIZE_ERROR;
1438 goto conn_err;
1439 }
1440
1441 /* process full frame only */
1442 if (h2c->dbuf->i < h2c->dfl)
1443 return 0;
1444
1445 last = h2_get_n32(h2c->dbuf, 0);
1446 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1447 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001448 if (h2c->last_sid < 0)
1449 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001450 return 1;
1451
1452 conn_err:
1453 h2c_error(h2c, error);
1454 return 0;
1455}
1456
Willy Tarreau92153fc2017-12-03 19:46:19 +01001457/* processes a PRIORITY frame, and either skips it or rejects if it is
1458 * invalid. Returns > 0 on success or zero on missing data. It may return
1459 * an error in h2c. Described in RFC7540#6.3.
1460 */
1461static int h2c_handle_priority(struct h2c *h2c)
1462{
1463 int error;
1464
1465 if (h2c->dsi == 0) {
1466 error = H2_ERR_PROTOCOL_ERROR;
1467 goto conn_err;
1468 }
1469
1470 if (h2c->dfl != 5) {
1471 error = H2_ERR_FRAME_SIZE_ERROR;
1472 goto conn_err;
1473 }
1474
1475 /* process full frame only */
1476 if (h2c->dbuf->i < h2c->dfl)
1477 return 0;
1478
1479 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1480 /* 7540#5.3 : can't depend on itself */
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto conn_err;
1483 }
1484 return 1;
1485
1486 conn_err:
1487 h2c_error(h2c, error);
1488 return 0;
1489}
1490
Willy Tarreaucd234e92017-08-18 10:59:39 +02001491/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1492 * Returns > 0 on success or zero on missing data. It may return an error in
1493 * h2c. Described in RFC7540#6.4.
1494 */
1495static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1496{
1497 int error;
1498
1499 if (h2c->dsi == 0) {
1500 error = H2_ERR_PROTOCOL_ERROR;
1501 goto conn_err;
1502 }
1503
Willy Tarreaucd234e92017-08-18 10:59:39 +02001504 if (h2c->dfl != 4) {
1505 error = H2_ERR_FRAME_SIZE_ERROR;
1506 goto conn_err;
1507 }
1508
1509 /* process full frame only */
1510 if (h2c->dbuf->i < h2c->dfl)
1511 return 0;
1512
1513 /* late RST, already handled */
1514 if (h2s->st == H2_SS_CLOSED)
1515 return 1;
1516
1517 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001518 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001519
1520 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001521 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001522 /* recv is used to force to detect CS_FL_EOS that wake()
1523 * doesn't handle in the stream-int code.
1524 */
1525 h2s->cs->data_cb->recv(h2s->cs);
1526 h2s->cs->data_cb->wake(h2s->cs);
1527 }
1528
1529 h2s->flags |= H2_SF_RST_RCVD;
1530 return 1;
1531
1532 conn_err:
1533 h2c_error(h2c, error);
1534 return 0;
1535}
1536
Willy Tarreau13278b42017-10-13 19:23:14 +02001537/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1538 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1539 * errors here are reported as connection errors since it's impossible to
1540 * recover from such errors after the compression context has been altered.
1541 */
1542static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1543{
1544 int error;
1545
1546 if (!h2c->dfl) {
1547 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1548 goto strm_err;
1549 }
1550
1551 if (!h2c->dbuf->size)
1552 return 0; // empty buffer
1553
1554 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1555 return 0; // incomplete frame
1556
Willy Tarreauf2101912018-07-19 10:11:38 +02001557 if (h2c->flags & H2_CF_DEM_TOOMANY)
1558 return 0; // too many cs still present
1559
Willy Tarreau13278b42017-10-13 19:23:14 +02001560 /* now either the frame is complete or the buffer is complete */
1561 if (h2s->st != H2_SS_IDLE) {
1562 /* FIXME: stream already exists, this is only allowed for
1563 * trailers (not supported for now).
1564 */
1565 error = H2_ERR_PROTOCOL_ERROR;
1566 goto conn_err;
1567 }
1568 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1569 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1570 error = H2_ERR_PROTOCOL_ERROR;
1571 goto conn_err;
1572 }
1573
1574 h2s = h2c_stream_new(h2c, h2c->dsi);
1575 if (!h2s) {
1576 error = H2_ERR_INTERNAL_ERROR;
1577 goto conn_err;
1578 }
1579
1580 h2s->st = H2_SS_OPEN;
1581 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1582 h2s->st = H2_SS_HREM;
1583 h2s->flags |= H2_SF_ES_RCVD;
1584 }
1585
1586 /* call the upper layers to process the frame, then let the upper layer
1587 * notify the stream about any change.
1588 */
1589 h2s->cs->data_cb->recv(h2s->cs);
1590
1591 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1592 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1593 error = H2_ERR_INTERNAL_ERROR;
1594 goto conn_err;
1595 }
1596
Willy Tarreau8f650c32017-11-21 19:36:21 +01001597 if (h2c->st0 >= H2_CS_ERROR)
1598 return 0;
1599
Willy Tarreau721c9742017-11-07 11:05:42 +01001600 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001601 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001602 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001603 }
1604 else {
1605 /* update the max stream ID if the request is being processed */
1606 if (h2s->id > h2c->max_id)
1607 h2c->max_id = h2s->id;
1608 }
1609
1610 return 1;
1611
1612 conn_err:
1613 h2c_error(h2c, error);
1614 return 0;
1615
1616 strm_err:
1617 if (h2s) {
1618 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001619 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001620 }
1621 else
1622 h2c_error(h2c, error);
1623 return 0;
1624}
1625
Willy Tarreau454f9052017-10-26 19:40:35 +02001626/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1627 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1628 */
1629static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1630{
1631 int error;
1632
1633 /* note that empty DATA frames are perfectly valid and sometimes used
1634 * to signal an end of stream (with the ES flag).
1635 */
1636
1637 if (!h2c->dbuf->size && h2c->dfl)
1638 return 0; // empty buffer
1639
1640 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1641 return 0; // incomplete frame
1642
1643 /* now either the frame is complete or the buffer is complete */
1644
1645 if (!h2c->dsi) {
1646 /* RFC7540#6.1 */
1647 error = H2_ERR_PROTOCOL_ERROR;
1648 goto conn_err;
1649 }
1650
1651 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1652 /* RFC7540#6.1 */
1653 error = H2_ERR_STREAM_CLOSED;
1654 goto strm_err;
1655 }
1656
Willy Tarreau454f9052017-10-26 19:40:35 +02001657 /* call the upper layers to process the frame, then let the upper layer
1658 * notify the stream about any change.
1659 */
1660 if (!h2s->cs) {
1661 error = H2_ERR_STREAM_CLOSED;
1662 goto strm_err;
1663 }
1664
1665 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001666
Willy Tarreau454f9052017-10-26 19:40:35 +02001667 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1668 /* cs has just been destroyed, we have to kill h2s. */
1669 error = H2_ERR_STREAM_CLOSED;
1670 goto strm_err;
1671 }
1672
Willy Tarreau8f650c32017-11-21 19:36:21 +01001673 if (h2c->st0 >= H2_CS_ERROR)
1674 return 0;
1675
Willy Tarreau721c9742017-11-07 11:05:42 +01001676 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001677 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001678 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001679 }
1680
1681 /* check for completion : the callee will change this to FRAME_A or
1682 * FRAME_H once done.
1683 */
1684 if (h2c->st0 == H2_CS_FRAME_P)
1685 return 0;
1686
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001687
1688 /* last frame */
1689 if (h2c->dff & H2_F_DATA_END_STREAM) {
1690 h2s->st = H2_SS_HREM;
1691 h2s->flags |= H2_SF_ES_RCVD;
1692 }
1693
Willy Tarreau454f9052017-10-26 19:40:35 +02001694 return 1;
1695
1696 conn_err:
1697 h2c_error(h2c, error);
1698 return 0;
1699
1700 strm_err:
1701 if (h2s) {
1702 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001703 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001704 }
1705 else
1706 h2c_error(h2c, error);
1707 return 0;
1708}
1709
Willy Tarreaubc933932017-10-09 16:21:43 +02001710/* process Rx frames to be demultiplexed */
1711static void h2_process_demux(struct h2c *h2c)
1712{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001713 struct h2s *h2s;
1714
Willy Tarreau081d4722017-05-16 21:51:05 +02001715 if (h2c->st0 >= H2_CS_ERROR)
1716 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001717
1718 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1719 if (h2c->st0 == H2_CS_PREFACE) {
1720 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1721 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1722 if (h2c->st0 == H2_CS_ERROR)
1723 h2c->st0 = H2_CS_ERROR2;
1724 goto fail;
1725 }
1726
1727 h2c->max_id = 0;
1728 h2c->st0 = H2_CS_SETTINGS1;
1729 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001730
1731 if (h2c->st0 == H2_CS_SETTINGS1) {
1732 struct h2_fh hdr;
1733
1734 /* ensure that what is pending is a valid SETTINGS frame
1735 * without an ACK.
1736 */
1737 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1738 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1739 if (h2c->st0 == H2_CS_ERROR)
1740 h2c->st0 = H2_CS_ERROR2;
1741 goto fail;
1742 }
1743
1744 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1745 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1746 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1747 h2c->st0 = H2_CS_ERROR2;
1748 goto fail;
1749 }
1750
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001751 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001752 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1753 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1754 h2c->st0 = H2_CS_ERROR2;
1755 goto fail;
1756 }
1757
1758 /* that's OK, switch to FRAME_P to process it */
1759 h2c->dfl = hdr.len;
1760 h2c->dsi = hdr.sid;
1761 h2c->dft = hdr.ft;
1762 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001763 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001764 h2c->st0 = H2_CS_FRAME_P;
1765 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001766 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001767
1768 /* process as many incoming frames as possible below */
1769 while (h2c->dbuf->i) {
1770 int ret = 0;
1771
1772 if (h2c->st0 >= H2_CS_ERROR)
1773 break;
1774
1775 if (h2c->st0 == H2_CS_FRAME_H) {
1776 struct h2_fh hdr;
1777
1778 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1779 break;
1780
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001781 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001782 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1783 h2c->st0 = H2_CS_ERROR;
1784 break;
1785 }
1786
1787 h2c->dfl = hdr.len;
1788 h2c->dsi = hdr.sid;
1789 h2c->dft = hdr.ft;
1790 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001791 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001792 h2c->st0 = H2_CS_FRAME_P;
1793 h2_skip_frame_hdr(h2c->dbuf);
1794 }
1795
1796 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001797 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001798
Willy Tarreaud7901432017-12-29 11:34:40 +01001799 if (h2c->st0 == H2_CS_FRAME_E)
1800 goto strm_err;
1801
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001802 if (h2s->st == H2_SS_IDLE &&
1803 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1804 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1805 * this state MUST be treated as a connection error
1806 */
1807 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1808 h2c->st0 = H2_CS_ERROR;
1809 break;
1810 }
1811
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001812 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1813 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1814 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1815 * this state MUST be treated as a stream error
1816 */
1817 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001818 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001819 goto strm_err;
1820 }
1821
Willy Tarreauab837502017-12-27 15:07:30 +01001822 /* Below the management of frames received in closed state is a
1823 * bit hackish because the spec makes strong differences between
1824 * streams closed by receiving RST, sending RST, and seeing ES
1825 * in both directions. In addition to this, the creation of a
1826 * new stream reusing the identifier of a closed one will be
1827 * detected here. Given that we cannot keep track of all closed
1828 * streams forever, we consider that unknown closed streams were
1829 * closed on RST received, which allows us to respond with an
1830 * RST without breaking the connection (eg: to abort a transfer).
1831 * Some frames have to be silently ignored as well.
1832 */
1833 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1834 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1835 /* #5.1.1: The identifier of a newly
1836 * established stream MUST be numerically
1837 * greater than all streams that the initiating
1838 * endpoint has opened or reserved. This
1839 * governs streams that are opened using a
1840 * HEADERS frame and streams that are reserved
1841 * using PUSH_PROMISE. An endpoint that
1842 * receives an unexpected stream identifier
1843 * MUST respond with a connection error.
1844 */
1845 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1846 goto strm_err;
1847 }
1848
1849 if (h2s->flags & H2_SF_RST_RCVD) {
1850 /* RFC7540#5.1:closed: an endpoint that
1851 * receives any frame other than PRIORITY after
1852 * receiving a RST_STREAM MUST treat that as a
1853 * stream error of type STREAM_CLOSED.
1854 *
1855 * Note that old streams fall into this category
1856 * and will lead to an RST being sent.
1857 */
1858 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1859 h2c->st0 = H2_CS_FRAME_E;
1860 goto strm_err;
1861 }
1862
1863 /* RFC7540#5.1:closed: if this state is reached as a
1864 * result of sending a RST_STREAM frame, the peer that
1865 * receives the RST_STREAM might have already sent
1866 * frames on the stream that cannot be withdrawn. An
1867 * endpoint MUST ignore frames that it receives on
1868 * closed streams after it has sent a RST_STREAM
1869 * frame. An endpoint MAY choose to limit the period
1870 * over which it ignores frames and treat frames that
1871 * arrive after this time as being in error.
1872 */
1873 if (!(h2s->flags & H2_SF_RST_SENT)) {
1874 /* RFC7540#5.1:closed: any frame other than
1875 * PRIO/WU/RST in this state MUST be treated as
1876 * a connection error
1877 */
1878 if (h2c->dft != H2_FT_RST_STREAM &&
1879 h2c->dft != H2_FT_PRIORITY &&
1880 h2c->dft != H2_FT_WINDOW_UPDATE) {
1881 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1882 goto strm_err;
1883 }
1884 }
1885 }
1886
Willy Tarreauc0da1962017-10-30 18:38:00 +01001887#if 0
1888 // problem below: it is not possible to completely ignore such
1889 // streams as we need to maintain the compression state as well
1890 // and for this we need to completely process these frames (eg:
1891 // HEADERS frames) as well as counting DATA frames to emit
1892 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1893 // This is a typical case of layer violation where the
1894 // transported contents are critical to the connection's
1895 // validity and must be ignored at the same time :-(
1896
1897 /* graceful shutdown, ignore streams whose ID is higher than
1898 * the one advertised in GOAWAY. RFC7540#6.8.
1899 */
1900 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1901 ret = MIN(h2c->dbuf->i, h2c->dfl);
1902 bi_del(h2c->dbuf, ret);
1903 h2c->dfl -= ret;
1904 ret = h2c->dfl == 0;
1905 goto strm_err;
1906 }
1907#endif
1908
Willy Tarreau7e98c052017-10-10 15:56:59 +02001909 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001910 case H2_FT_SETTINGS:
1911 if (h2c->st0 == H2_CS_FRAME_P)
1912 ret = h2c_handle_settings(h2c);
1913
1914 if (h2c->st0 == H2_CS_FRAME_A)
1915 ret = h2c_ack_settings(h2c);
1916 break;
1917
Willy Tarreaucf68c782017-10-10 17:11:41 +02001918 case H2_FT_PING:
1919 if (h2c->st0 == H2_CS_FRAME_P)
1920 ret = h2c_handle_ping(h2c);
1921
1922 if (h2c->st0 == H2_CS_FRAME_A)
1923 ret = h2c_ack_ping(h2c);
1924 break;
1925
Willy Tarreau26f95952017-07-27 17:18:30 +02001926 case H2_FT_WINDOW_UPDATE:
1927 if (h2c->st0 == H2_CS_FRAME_P)
1928 ret = h2c_handle_window_update(h2c, h2s);
1929 break;
1930
Willy Tarreau61290ec2017-10-17 08:19:21 +02001931 case H2_FT_CONTINUATION:
1932 /* we currently don't support CONTINUATION frames since
1933 * we have nowhere to store the partial HEADERS frame.
1934 * Let's abort the stream on an INTERNAL_ERROR here.
1935 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001936 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001937 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001938 h2c->st0 = H2_CS_FRAME_E;
1939 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001940 break;
1941
Willy Tarreau13278b42017-10-13 19:23:14 +02001942 case H2_FT_HEADERS:
1943 if (h2c->st0 == H2_CS_FRAME_P)
1944 ret = h2c_frt_handle_headers(h2c, h2s);
1945 break;
1946
Willy Tarreau454f9052017-10-26 19:40:35 +02001947 case H2_FT_DATA:
1948 if (h2c->st0 == H2_CS_FRAME_P)
1949 ret = h2c_frt_handle_data(h2c, h2s);
1950
1951 if (h2c->st0 == H2_CS_FRAME_A)
1952 ret = h2c_send_strm_wu(h2c);
1953 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001954
Willy Tarreau92153fc2017-12-03 19:46:19 +01001955 case H2_FT_PRIORITY:
1956 if (h2c->st0 == H2_CS_FRAME_P)
1957 ret = h2c_handle_priority(h2c);
1958 break;
1959
Willy Tarreaucd234e92017-08-18 10:59:39 +02001960 case H2_FT_RST_STREAM:
1961 if (h2c->st0 == H2_CS_FRAME_P)
1962 ret = h2c_handle_rst_stream(h2c, h2s);
1963 break;
1964
Willy Tarreaue96b0922017-10-30 00:28:29 +01001965 case H2_FT_GOAWAY:
1966 if (h2c->st0 == H2_CS_FRAME_P)
1967 ret = h2c_handle_goaway(h2c);
1968 break;
1969
Willy Tarreau1c661982017-10-30 13:52:01 +01001970 case H2_FT_PUSH_PROMISE:
1971 /* not permitted here, RFC7540#5.1 */
1972 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001973 break;
1974
1975 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001976 default:
1977 /* drop frames that we ignore. They may be larger than
1978 * the buffer so we drain all of their contents until
1979 * we reach the end.
1980 */
1981 ret = MIN(h2c->dbuf->i, h2c->dfl);
1982 bi_del(h2c->dbuf, ret);
1983 h2c->dfl -= ret;
1984 ret = h2c->dfl == 0;
1985 }
1986
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001987 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001988 /* We may have to send an RST if not done yet */
1989 if (h2s->st == H2_SS_ERROR)
1990 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001991
Willy Tarreaua20a5192017-12-27 11:02:06 +01001992 if (h2c->st0 == H2_CS_FRAME_E)
1993 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001994
Willy Tarreau7e98c052017-10-10 15:56:59 +02001995 /* error or missing data condition met above ? */
1996 if (ret <= 0)
1997 break;
1998
1999 if (h2c->st0 != H2_CS_FRAME_H) {
2000 bi_del(h2c->dbuf, h2c->dfl);
2001 h2c->st0 = H2_CS_FRAME_H;
2002 }
2003 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002004
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002005 if (h2c->rcvd_c > 0 &&
2006 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2007 h2c_send_conn_wu(h2c);
2008
Willy Tarreau52eed752017-09-22 15:05:09 +02002009 fail:
2010 /* we can go here on missing data, blocked response or error */
2011 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002012}
2013
2014/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2015 * the end.
2016 */
2017static int h2_process_mux(struct h2c *h2c)
2018{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002019 struct h2s *h2s, *h2s_back;
2020
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002021 /* start by sending possibly pending window updates */
2022 if (h2c->rcvd_c > 0 &&
2023 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2024 h2c_send_conn_wu(h2c) < 0)
2025 goto fail;
2026
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002027 /* First we always process the flow control list because the streams
2028 * waiting there were already elected for immediate emission but were
2029 * blocked just on this.
2030 */
2031
2032 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2033 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2034 h2c->st0 >= H2_CS_ERROR)
2035 break;
2036
2037 /* In theory it's possible that h2s->cs == NULL here :
2038 * - client sends crap that causes a parse error
2039 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2040 * - RST_STREAM cannot be emitted because mux is busy/full
2041 * - stream gets notified, detaches and quits
2042 * - mux buffer gets ready and wakes pending streams up
2043 * - bam!
2044 */
2045 h2s->flags &= ~H2_SF_BLK_ANY;
2046
2047 if (h2s->cs) {
2048 h2s->cs->data_cb->send(h2s->cs);
2049 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002050 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002051 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002052 }
2053
2054 /* depending on callee's blocking reasons, we may queue in send
2055 * list or completely dequeue.
2056 */
2057 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2058 if (h2s->flags & H2_SF_BLK_ANY) {
2059 LIST_DEL(&h2s->list);
2060 LIST_ADDQ(&h2c->send_list, &h2s->list);
2061 }
2062 else {
2063 LIST_DEL(&h2s->list);
2064 LIST_INIT(&h2s->list);
2065 if (h2s->cs)
2066 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002067 else {
2068 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002069 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002070 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002071 }
2072 }
2073 }
2074
2075 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2076 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2077 break;
2078
2079 /* In theory it's possible that h2s->cs == NULL here :
2080 * - client sends crap that causes a parse error
2081 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2082 * - RST_STREAM cannot be emitted because mux is busy/full
2083 * - stream gets notified, detaches and quits
2084 * - mux buffer gets ready and wakes pending streams up
2085 * - bam!
2086 */
2087 h2s->flags &= ~H2_SF_BLK_ANY;
2088
2089 if (h2s->cs) {
2090 h2s->cs->data_cb->send(h2s->cs);
2091 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002092 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002093 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002094 }
2095 /* depending on callee's blocking reasons, we may queue in fctl
2096 * list or completely dequeue.
2097 */
2098 if (h2s->flags & H2_SF_BLK_MFCTL) {
2099 /* stream hit the connection's flow control */
2100 LIST_DEL(&h2s->list);
2101 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2102 }
2103 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2104 LIST_DEL(&h2s->list);
2105 LIST_INIT(&h2s->list);
2106 if (h2s->cs)
2107 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002108 else {
2109 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002110 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002111 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002112 }
2113 }
2114
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002115 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002116 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002117 if (h2c->st0 == H2_CS_ERROR) {
2118 if (h2c->max_id >= 0) {
2119 h2c_send_goaway_error(h2c, NULL);
2120 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2121 return 0;
2122 }
2123
2124 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2125 }
2126 return 1;
2127 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002128 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002129}
2130
Willy Tarreau71681172017-10-23 14:39:06 +02002131
Willy Tarreau62f52692017-10-08 23:01:42 +02002132/*********************************************************/
2133/* functions below are I/O callbacks from the connection */
2134/*********************************************************/
2135
2136/* callback called on recv event by the connection handler */
2137static void h2_recv(struct connection *conn)
2138{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002139 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002140 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002141 int max;
2142
Willy Tarreau315d8072017-12-10 22:17:57 +01002143 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002144 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002145
Willy Tarreau44e973f2018-03-01 17:49:30 +01002146 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002147 if (!buf) {
2148 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002149 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002150 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002151
Willy Tarreaua2af5122017-10-09 11:56:46 +02002152 /* note: buf->o == 0 */
2153 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002154 if (max)
2155 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002156
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002157 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002158 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002159 return;
2160 }
2161
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002162 if (buf->i == buf->size)
2163 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002164 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002165}
2166
2167/* callback called on send event by the connection handler */
2168static void h2_send(struct connection *conn)
2169{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002170 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002171 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002172
2173 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002174 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002175
2176 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2177 /* a handshake was requested */
2178 return;
2179 }
2180
Willy Tarreaubc933932017-10-09 16:21:43 +02002181 /* This loop is quite simple : it tries to fill as much as it can from
2182 * pending streams into the existing buffer until it's reportedly full
2183 * or the end of send requests is reached. Then it tries to send this
2184 * buffer's contents out, marks it not full if at least one byte could
2185 * be sent, and tries again.
2186 *
2187 * The snd_buf() function normally takes a "flags" argument which may
2188 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2189 * data immediately comes and CO_SFL_STREAMER to indicate that the
2190 * connection is streaming lots of data (used to increase TLS record
2191 * size at the expense of latency). The former can be sent any time
2192 * there's a buffer full flag, as it indicates at least one stream
2193 * attempted to send and failed so there are pending data. An
2194 * alternative would be to set it as long as there's an active stream
2195 * but that would be problematic for ACKs until we have an absolute
2196 * guarantee that all waiters have at least one byte to send. The
2197 * latter should possibly not be set for now.
2198 */
2199
2200 done = 0;
2201 while (!done) {
2202 unsigned int flags = 0;
2203
2204 /* fill as much as we can into the current buffer */
2205 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2206 done = h2_process_mux(h2c);
2207
2208 if (conn->flags & CO_FL_ERROR)
2209 break;
2210
2211 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2212 flags |= CO_SFL_MSG_MORE;
2213
Willy Tarreau319994a2017-11-07 11:03:56 +01002214 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002215 break;
2216
2217 /* wrote at least one byte, the buffer is not full anymore */
2218 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2219 }
2220
Willy Tarreaua2af5122017-10-09 11:56:46 +02002221 if (conn->flags & CO_FL_SOCK_WR_SH) {
2222 /* output closed, nothing to send, clear the buffer to release it */
2223 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002224 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002225}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226
Willy Tarreau62f52692017-10-08 23:01:42 +02002227/* callback called on any event by the connection handler.
2228 * It applies changes and returns zero, or < 0 if it wants immediate
2229 * destruction of the connection (which normally doesn not happen in h2).
2230 */
2231static int h2_wake(struct connection *conn)
2232{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002234 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235
Willy Tarreaud13bf272017-12-14 10:34:52 +01002236 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2237 h2_process_demux(h2c);
2238
2239 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2240 h2c->dbuf->i = 0;
2241
2242 if (h2c->dbuf->i != h2c->dbuf->size)
2243 h2c->flags &= ~H2_CF_DEM_DFULL;
2244 }
2245
Willy Tarreau8ec14062017-12-30 18:08:13 +01002246 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2247 /* frontend is stopping, reload likely in progress, let's try
2248 * to announce a graceful shutdown if not yet done. We don't
2249 * care if it fails, it will be tried again later.
2250 */
2251 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2252 if (h2c->last_sid < 0)
2253 h2c->last_sid = (1U << 31) - 1;
2254 h2c_send_goaway_error(h2c, NULL);
2255 }
2256 }
2257
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002258 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002259 * If we received early data, and the handshake is done, wake
2260 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002261 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002262 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2263 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2264 struct eb32_node *node;
2265 struct h2s *h2s;
2266
2267 h2c->flags |= H2_CF_WAIT_FOR_HS;
2268 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2269
2270 while (node) {
2271 h2s = container_of(node, struct h2s, by_id);
2272 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2273 h2s->cs->data_cb->wake(h2s->cs);
2274 node = eb32_next(node);
2275 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002276 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002277
Willy Tarreau26bd7612017-10-09 16:47:04 +02002278 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002279 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2280 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2281 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002282 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002283
2284 if (eb_is_empty(&h2c->streams_by_id)) {
2285 /* no more stream, kill the connection now */
2286 h2_release(conn);
2287 return -1;
2288 }
2289 else {
2290 /* some streams still there, we need to signal them all and
2291 * wait for their departure.
2292 */
2293 __conn_xprt_stop_recv(conn);
2294 __conn_xprt_stop_send(conn);
2295 return 0;
2296 }
2297 }
2298
2299 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002300 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002301
2302 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002303 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002304 __conn_xprt_stop_recv(conn);
2305 }
2306 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002307 __conn_xprt_want_recv(conn);
2308 }
2309
2310 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002311 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2312 (h2c->st0 == H2_CS_ERROR ||
2313 h2c->mbuf->o ||
2314 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2315 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002316 __conn_xprt_want_send(conn);
2317 }
2318 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002319 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002320 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002321 }
2322
Willy Tarreau3f133572017-10-31 19:21:06 +01002323 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002324 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002325 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002326 task_queue(h2c->task);
2327 }
2328 else
2329 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002330 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002331 return 0;
2332}
2333
Willy Tarreauea392822017-10-31 10:02:25 +01002334/* Connection timeout management. The principle is that if there's no receipt
2335 * nor sending for a certain amount of time, the connection is closed. If the
2336 * MUX buffer still has lying data or is not allocatable, the connection is
2337 * immediately killed. If it's allocatable and empty, we attempt to send a
2338 * GOAWAY frame.
2339 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002340static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002341{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002342 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002343 int expired = tick_is_expired(t->expire, now_ms);
2344
Willy Tarreau0975f112018-03-29 15:22:59 +02002345 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002346 return t;
2347
Willy Tarreau0975f112018-03-29 15:22:59 +02002348 task_delete(t);
2349 task_free(t);
2350
2351 if (!h2c) {
2352 /* resources were already deleted */
2353 return NULL;
2354 }
2355
2356 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002357 h2c_error(h2c, H2_ERR_NO_ERROR);
2358 h2_wake_some_streams(h2c, 0, 0);
2359
2360 if (h2c->mbuf->o) {
2361 /* don't even try to send a GOAWAY, the buffer is stuck */
2362 h2c->flags |= H2_CF_GOAWAY_FAILED;
2363 }
2364
2365 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002366 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002367 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2368 h2c->flags |= H2_CF_GOAWAY_FAILED;
2369
2370 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2371 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2372
Willy Tarreau0975f112018-03-29 15:22:59 +02002373 /* either we can release everything now or it will be done later once
2374 * the last stream closes.
2375 */
2376 if (eb_is_empty(&h2c->streams_by_id))
2377 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002378
Willy Tarreauea392822017-10-31 10:02:25 +01002379 return NULL;
2380}
2381
2382
Willy Tarreau62f52692017-10-08 23:01:42 +02002383/*******************************************/
2384/* functions below are used by the streams */
2385/*******************************************/
2386
2387/*
2388 * Attach a new stream to a connection
2389 * (Used for outgoing connections)
2390 */
2391static struct conn_stream *h2_attach(struct connection *conn)
2392{
2393 return NULL;
2394}
2395
2396/* callback used to update the mux's polling flags after changing a cs' status.
2397 * The caller (cs_update_mux_polling) will take care of propagating any changes
2398 * to the transport layer.
2399 */
2400static void h2_update_poll(struct conn_stream *cs)
2401{
Willy Tarreau1d393222017-10-17 10:26:19 +02002402 struct h2s *h2s = cs->ctx;
2403
2404 if (!h2s)
2405 return;
2406
Willy Tarreaud7739c82017-10-30 15:38:23 +01002407 /* we may unblock a blocked read */
2408
Willy Tarreau315d8072017-12-10 22:17:57 +01002409 if (cs->flags & CS_FL_DATA_RD_ENA) {
2410 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002411 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002412 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002413 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002414 conn_xprt_want_send(cs->conn);
2415 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002416 }
2417
Willy Tarreau1d393222017-10-17 10:26:19 +02002418 /* Note: the stream and stream-int code doesn't allow us to perform a
2419 * synchronous send() here unfortunately, because this code is called
2420 * as si_update() from the process_stream() context. This means that
2421 * we have to queue the current cs and defer its processing after the
2422 * connection's cs list is processed anyway.
2423 */
2424
2425 if (cs->flags & CS_FL_DATA_WR_ENA) {
2426 if (LIST_ISEMPTY(&h2s->list)) {
2427 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2428 !h2s->h2c->mbuf->o && // not yet subscribed
2429 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2430 conn_xprt_want_send(cs->conn);
2431 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2432 }
2433 }
2434 else if (!LIST_ISEMPTY(&h2s->list)) {
2435 LIST_DEL(&h2s->list);
2436 LIST_INIT(&h2s->list);
2437 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2438 }
2439
2440 /* this can happen from within si_chk_snd() */
2441 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2442 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002443}
2444
2445/*
2446 * Detach the stream from the connection and possibly release the connection.
2447 */
2448static void h2_detach(struct conn_stream *cs)
2449{
Willy Tarreau60935142017-10-16 18:11:19 +02002450 struct h2s *h2s = cs->ctx;
2451 struct h2c *h2c;
2452
2453 cs->ctx = NULL;
2454 if (!h2s)
2455 return;
2456
2457 h2c = h2s->h2c;
2458 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002459 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002460 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2461 !h2_has_too_many_cs(h2c)) {
2462 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2463 if (h2_recv_allowed(h2c)) {
2464 __conn_xprt_want_recv(h2c->conn);
2465 conn_xprt_want_send(h2c->conn);
2466 }
2467 }
Willy Tarreau60935142017-10-16 18:11:19 +02002468
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002469 /* this stream may be blocked waiting for some data to leave (possibly
2470 * an ES or RST frame), so orphan it in this case.
2471 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002472 if (!(cs->conn->flags & CO_FL_ERROR) &&
2473 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002474 return;
2475
Willy Tarreau45f752e2017-10-30 15:44:59 +01002476 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2477 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2478 /* unblock the connection if it was blocked on this
2479 * stream.
2480 */
2481 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2482 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2483 conn_xprt_want_recv(cs->conn);
2484 conn_xprt_want_send(cs->conn);
2485 }
2486
Willy Tarreau71049cc2018-03-28 13:56:39 +02002487 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002488
Willy Tarreaue323f342018-03-28 13:51:45 +02002489 /* We don't want to close right now unless we're removing the
2490 * last stream, and either the connection is in error, or it
2491 * reached the ID already specified in a GOAWAY frame received
2492 * or sent (as seen by last_sid >= 0).
2493 */
2494 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2495 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002496 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002497 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2498 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2499 (conn_xprt_read0_pending(h2c->conn) ||
2500 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2501 /* no more stream will come, kill it now */
2502 h2_release(h2c->conn);
2503 }
2504 else if (h2c->task) {
2505 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2506 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2507 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002508 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002509 else
2510 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002511 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002512}
2513
2514static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2515{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002516 struct h2s *h2s = cs->ctx;
2517
2518 if (!mode)
2519 return;
2520
Willy Tarreau721c9742017-11-07 11:05:42 +01002521 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002522 return;
2523
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002524 /* if no outgoing data was seen on this stream, it means it was
2525 * closed with a "tcp-request content" rule that is normally
2526 * used to kill the connection ASAP (eg: limit abuse). In this
2527 * case we send a goaway to close the connection.
2528 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002529 if (!(h2s->flags & H2_SF_RST_SENT) &&
2530 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002531 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002532
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002533 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2534 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2535 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002536 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002537
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002538 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2539 conn_xprt_want_send(cs->conn);
2540
Willy Tarreau00dd0782018-03-01 16:31:34 +01002541 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002542
2543 add_to_list:
2544 if (LIST_ISEMPTY(&h2s->list)) {
2545 if (h2s->flags & H2_SF_BLK_MFCTL)
2546 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2547 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2548 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2549 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002550}
2551
2552static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2553{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002554 struct h2s *h2s = cs->ctx;
2555
Willy Tarreau721c9742017-11-07 11:05:42 +01002556 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002557 return;
2558
Willy Tarreau67434202017-11-06 20:20:51 +01002559 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002560 /* we can cleanly close using an empty data frame only after headers */
2561
2562 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2563 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002564 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002565
2566 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002567 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002568 else
2569 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002570 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002571 /* if no outgoing data was seen on this stream, it means it was
2572 * closed with a "tcp-request content" rule that is normally
2573 * used to kill the connection ASAP (eg: limit abuse). In this
2574 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002575 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002576 if (!(h2s->flags & H2_SF_RST_SENT) &&
2577 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002578 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002579
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002580 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2581 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002582 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002583 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002584
Willy Tarreau00dd0782018-03-01 16:31:34 +01002585 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002586 }
2587
2588 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2589 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002590
2591 add_to_list:
2592 if (LIST_ISEMPTY(&h2s->list)) {
2593 if (h2s->flags & H2_SF_BLK_MFCTL)
2594 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2595 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2596 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2597 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002598}
2599
Willy Tarreau13278b42017-10-13 19:23:14 +02002600/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2601 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2602 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002603 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002604 */
2605static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2606{
2607 struct h2c *h2c = h2s->h2c;
2608 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002609 struct chunk *tmp = get_trash_chunk();
2610 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002611 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002612 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002613 int flen = h2c->dfl;
2614 int outlen = 0;
2615 int wrap;
2616 int try;
2617
2618 if (!h2c->dfl) {
2619 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002620 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002621 return 0;
2622 }
2623
Willy Tarreau68472622017-12-11 18:36:37 +01002624 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2625 return 0; // incomplete input frame
2626
Willy Tarreau13278b42017-10-13 19:23:14 +02002627 /* if the input buffer wraps, take a temporary copy of it (rare) */
2628 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2629 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002630 copy = alloc_trash_chunk();
2631 if (!copy) {
2632 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2633 goto fail;
2634 }
2635 memcpy(copy->str, h2c->dbuf->p, wrap);
2636 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2637 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002638 }
2639
2640 /* The padlen is the first byte before data, and the padding appears
2641 * after data. padlen+data+padding are included in flen.
2642 */
2643 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002644 h2c->dpl = *hdrs;
2645 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002646 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2647 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002648 return 0;
2649 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002650 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002651 hdrs += 1; // skip Pad Length
2652 }
2653
2654 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2655 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002656 if (read_n32(hdrs) == h2s->id) {
2657 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2658 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2659 return 0;//goto fail_stream;
2660 }
2661
Willy Tarreau13278b42017-10-13 19:23:14 +02002662 hdrs += 5; // stream dep = 4, weight = 1
2663 flen -= 5;
2664 }
2665
2666 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2667 * don't support this for now and can't even decompress so we have to
2668 * break the connection.
2669 */
2670 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2671 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002672 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002673 }
2674
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002675 /* we can't retry a failed decompression operation so we must be very
2676 * careful not to take any risks. In practice the output buffer is
2677 * always empty except maybe for trailers, so these operations almost
2678 * never happen.
2679 */
2680 if (unlikely(buf->o)) {
2681 /* need to let the output buffer flush and
2682 * mark the buffer for later wake up.
2683 */
2684 goto fail;
2685 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002686
Willy Tarreauaa7af722018-07-12 10:33:12 +02002687 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002688 /* it doesn't fit and the buffer is fragmented,
2689 * so let's defragment it and try again.
2690 */
Willy Tarreau0db4d102018-07-12 11:00:01 +02002691 b_slow_realign(buf, trash.str, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002692 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002693
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002694 /* first check if we have some room after p+i */
2695 try = buf->data + buf->size - (buf->p + buf->i);
2696
2697 /* otherwise continue between data and p-o */
2698 if (try <= 0) {
2699 try = buf->p - (buf->data + buf->o);
2700 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002701 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002702 }
2703 if (try > count)
2704 try = count;
2705
2706 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2707 sizeof(list)/sizeof(list[0]), tmp);
2708 if (outlen < 0) {
2709 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2710 goto fail;
2711 }
2712
2713 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002714 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
2715 outlen = h2_make_h1_request(list, bi_end(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002716
2717 if (outlen < 0) {
2718 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2719 goto fail;
2720 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002721
Willy Tarreau174b06a2018-04-25 18:13:58 +02002722 if (msgf & H2_MSGF_BODY) {
2723 /* a payload is present */
2724 if (msgf & H2_MSGF_BODY_CL)
2725 h2s->flags |= H2_SF_DATA_CLEN;
2726 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2727 h2s->flags |= H2_SF_DATA_CHNK;
2728 }
2729
Willy Tarreau13278b42017-10-13 19:23:14 +02002730 /* now consume the input data */
2731 bi_del(h2c->dbuf, h2c->dfl);
2732 h2c->st0 = H2_CS_FRAME_H;
2733 buf->i += outlen;
2734
2735 /* don't send it before returning data!
2736 * FIXME: should we instead try to send it much later, after the
2737 * response ? This would require that we keep a copy of it in h2s.
2738 */
2739 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2740 h2s->cs->flags |= CS_FL_EOS;
2741 h2s->flags |= H2_SF_ES_RCVD;
2742 }
2743
Willy Tarreau68dd9852017-07-03 14:44:26 +02002744 leave:
2745 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002746 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002747 fail:
2748 outlen = 0;
2749 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002750}
2751
Willy Tarreau454f9052017-10-26 19:40:35 +02002752/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2753 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2754 * in use, a new chunk is emitted for each frame. This is supposed to fit
2755 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2756 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2757 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2758 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002759 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2760 * checked to know if some data remain pending (an empty DATA frame can return
2761 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2762 * connection errors in h2c->errcode. The caller must already have checked the
2763 * frame header and ensured that the frame was complete or the buffer full. It
2764 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002765 */
2766static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2767{
2768 struct h2c *h2c = h2s->h2c;
2769 int block1, block2;
2770 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002771 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002772
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002773 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002774 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002775
2776 /* The padlen is the first byte before data, and the padding appears
2777 * after data. padlen+data+padding are included in flen.
2778 */
Willy Tarreau79127812017-12-03 21:06:59 +01002779 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002780 if (h2c->dbuf->i < 1)
2781 return 0;
2782
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002783 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2784 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002785 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2786 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002787 return 0;
2788 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002789
2790 /* skip the padlen byte */
2791 bi_del(h2c->dbuf, 1);
2792 h2c->dfl--;
2793 h2c->rcvd_c++; h2c->rcvd_s++;
2794 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002795 }
2796
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002797 flen = h2c->dfl - h2c->dpl;
2798 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002799 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002800
2801 if (flen > h2c->dbuf->i) {
2802 flen = h2c->dbuf->i;
2803 if (!flen)
2804 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002805 }
2806
Willy Tarreaueba10f22018-04-25 20:44:22 +02002807 /* chunked-encoding requires more room */
2808 if (h2s->flags & H2_SF_DATA_CHNK) {
2809 chklen = MIN(flen, count);
2810 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2811 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2812 (chklen < 1048576) ? 4 : 8;
2813 chklen += 4; // CRLF, CRLF
2814 }
2815
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002816 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002817 if (flen + chklen > count) {
2818 if (chklen >= count)
2819 goto full;
2820 flen = count - chklen;
2821 }
2822
2823 if (h2s->flags & H2_SF_DATA_CHNK) {
2824 /* emit the chunk size */
2825 unsigned int chksz = flen;
2826 char str[10];
2827 char *beg;
2828
2829 beg = str + sizeof(str);
2830 *--beg = '\n';
2831 *--beg = '\r';
2832 do {
2833 *--beg = hextab[chksz & 0xF];
2834 } while (chksz >>= 4);
2835 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002836 }
2837
Willy Tarreau454f9052017-10-26 19:40:35 +02002838 /* Block1 is the length of the first block before the buffer wraps,
2839 * block2 is the optional second block to reach the end of the frame.
2840 */
Willy Tarreau7194d3c2018-06-06 16:55:45 +02002841 block1 = b_contig_data(h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002842 if (block1 > flen)
2843 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002844 block2 = flen - block1;
2845
2846 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002847 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002848
2849 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002850 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002851
Willy Tarreaueba10f22018-04-25 20:44:22 +02002852 if (h2s->flags & H2_SF_DATA_CHNK) {
2853 /* emit the CRLF */
2854 bi_putblk(buf, "\r\n", 2);
2855 }
2856
Willy Tarreau454f9052017-10-26 19:40:35 +02002857 /* now mark the input data as consumed (will be deleted from the buffer
2858 * by the caller when seeing FRAME_A after sending the window update).
2859 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002860 bi_del(h2c->dbuf, flen);
2861 h2c->dfl -= flen;
2862 h2c->rcvd_c += flen;
2863 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2864
2865 if (h2c->dfl > h2c->dpl) {
2866 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002867 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002868 }
2869
Willy Tarreau4a28da12018-01-04 14:41:00 +01002870 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002871 /* here we're done with the frame, all the payload (except padding) was
2872 * transferred.
2873 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002874
2875 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2876 /* emit the trailing 0 CRLF CRLF */
2877 if (count < 5)
2878 goto more;
2879 chklen += 5;
2880 bi_putblk(buf, "0\r\n\r\n", 5);
2881 }
2882
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002883 h2c->rcvd_c += h2c->dpl;
2884 h2c->rcvd_s += h2c->dpl;
2885 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002886 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2887
2888 /* don't send it before returning data!
2889 * FIXME: should we instead try to send it much later, after the
2890 * response ? This would require that we keep a copy of it in h2s.
2891 */
Willy Tarreau79127812017-12-03 21:06:59 +01002892 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002893 h2s->cs->flags |= CS_FL_EOS;
2894 h2s->flags |= H2_SF_ES_RCVD;
2895 }
2896
Willy Tarreaueba10f22018-04-25 20:44:22 +02002897 return flen + chklen;
2898 full:
2899 flen = chklen = 0;
2900 more:
2901 h2c->flags |= H2_CF_DEM_SFULL;
2902 h2s->cs->flags |= CS_FL_RCV_MORE;
2903 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002904}
2905
Willy Tarreau62f52692017-10-08 23:01:42 +02002906/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002907 * Called from the upper layer to get more data, up to <count> bytes. The
2908 * caller is responsible for never asking for more data than what is available
2909 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002910 */
2911static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2912{
Willy Tarreau13278b42017-10-13 19:23:14 +02002913 struct h2s *h2s = cs->ctx;
2914 struct h2c *h2c = h2s->h2c;
2915 int ret = 0;
2916
2917 if (h2c->st0 != H2_CS_FRAME_P)
2918 return 0; // no pre-parsed frame yet
2919
2920 if (h2c->dsi != h2s->id)
2921 return 0; // not for us
2922
2923 if (!h2c->dbuf->size)
2924 return 0; // empty buffer
2925
Willy Tarreau13278b42017-10-13 19:23:14 +02002926 switch (h2c->dft) {
2927 case H2_FT_HEADERS:
2928 ret = h2_frt_decode_headers(h2s, buf, count);
2929 break;
2930
Willy Tarreau454f9052017-10-26 19:40:35 +02002931 case H2_FT_DATA:
2932 ret = h2_frt_transfer_data(h2s, buf, count);
2933 break;
2934
Willy Tarreau13278b42017-10-13 19:23:14 +02002935 default:
2936 ret = 0;
2937 }
2938 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002939}
2940
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002941/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2942 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2943 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2944 * to the number of buffer bytes consumed.
2945 */
2946static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2947{
2948 struct http_hdr list[MAX_HTTP_HDR];
2949 struct h2c *h2c = h2s->h2c;
2950 struct h1m *h1m = &h2s->res;
2951 struct chunk outbuf;
2952 int es_now = 0;
2953 int ret = 0;
2954 int hdr;
2955
2956 if (h2c_mux_busy(h2c, h2s)) {
2957 h2s->flags |= H2_SF_BLK_MBUSY;
2958 return 0;
2959 }
2960
Willy Tarreau44e973f2018-03-01 17:49:30 +01002961 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002962 h2c->flags |= H2_CF_MUX_MALLOC;
2963 h2s->flags |= H2_SF_BLK_MROOM;
2964 return 0;
2965 }
2966
2967 /* First, try to parse the H1 response and index it into <list>.
2968 * NOTE! Since it comes from haproxy, we *know* that a response header
2969 * block does not wrap and we can safely read it this way without
2970 * having to realign the buffer.
2971 */
2972 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2973 list, sizeof(list)/sizeof(list[0]), h1m);
2974 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002975 /* incomplete or invalid response, this is abnormal coming from
2976 * haproxy and may only result in a bad errorfile or bad Lua code
2977 * so that won't be fixed, raise an error now.
2978 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002979 * FIXME: we should instead add the ability to only return a
2980 * 502 bad gateway. But in theory this is not supposed to
2981 * happen.
2982 */
2983 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2984 ret = 0;
2985 goto end;
2986 }
2987
2988 chunk_reset(&outbuf);
2989
2990 while (1) {
2991 outbuf.str = bo_end(h2c->mbuf);
2992 outbuf.size = bo_contig_space(h2c->mbuf);
2993 outbuf.len = 0;
2994
Willy Tarreauaa7af722018-07-12 10:33:12 +02002995 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002996 break;
2997 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02002998 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002999 }
3000
3001 if (outbuf.size < 9) {
3002 h2c->flags |= H2_CF_MUX_MFULL;
3003 h2s->flags |= H2_SF_BLK_MROOM;
3004 ret = 0;
3005 goto end;
3006 }
3007
3008 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3009 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
3010 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3011 outbuf.len = 9;
3012
3013 /* encode status, which necessarily is the first one */
3014 if (outbuf.len < outbuf.size && h1m->status == 200)
3015 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
3016 else if (outbuf.len < outbuf.size && h1m->status == 304)
3017 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003018 else if (unlikely(list[0].v.len != 3)) {
3019 /* this is an unparsable response */
3020 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3021 ret = 0;
3022 goto end;
3023 }
3024 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003025 /* basic encoding of the status code */
3026 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3027 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3028 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3029 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3030 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3031 }
3032 else {
Willy Tarreauaa7af722018-07-12 10:33:12 +02003033 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003034 goto realign_again;
3035
3036 h2c->flags |= H2_CF_MUX_MFULL;
3037 h2s->flags |= H2_SF_BLK_MROOM;
3038 ret = 0;
3039 goto end;
3040 }
3041
3042 /* encode all headers, stop at empty name */
3043 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003044 /* these ones do not exist in H2 and must be dropped. */
3045 if (isteq(list[hdr].n, ist("connection")) ||
3046 isteq(list[hdr].n, ist("proxy-connection")) ||
3047 isteq(list[hdr].n, ist("keep-alive")) ||
3048 isteq(list[hdr].n, ist("upgrade")) ||
3049 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003050 continue;
3051
3052 if (isteq(list[hdr].n, ist("")))
3053 break; // end
3054
3055 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3056 /* output full */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003057 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003058 goto realign_again;
3059
3060 h2c->flags |= H2_CF_MUX_MFULL;
3061 h2s->flags |= H2_SF_BLK_MROOM;
3062 ret = 0;
3063 goto end;
3064 }
3065 }
3066
3067 /* we may need to add END_STREAM */
3068 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3069 es_now = 1;
3070
3071 /* update the frame's size */
3072 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3073
3074 if (es_now)
3075 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3076
3077 /* consume incoming H1 response */
3078 bo_del(buf, ret);
3079
3080 /* commit the H2 response */
3081 h2c->mbuf->o += outbuf.len;
3082 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003083 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003084
3085 /* for now we don't implemented CONTINUATION, so we wait for a
3086 * body or directly end in TRL2.
3087 */
3088 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003089 // trim any possibly pending data (eg: inconsistent content-length)
3090 bo_del(buf, buf->o);
3091
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003092 h1m->state = HTTP_MSG_DONE;
3093 h2s->flags |= H2_SF_ES_SENT;
3094 if (h2s->st == H2_SS_OPEN)
3095 h2s->st = H2_SS_HLOC;
3096 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003097 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003098 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003099 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003100 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003101 h1m->state = HTTP_MSG_RPBEFORE;
3102 h1m->status = 0;
3103 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003104 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003105 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003106 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003107 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003108
3109 end:
3110 //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));
3111 return ret;
3112}
3113
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003114/* Try to send a DATA frame matching HTTP/1 response present in the response
3115 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3116 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3117 * corresponds to the number of buffer bytes consumed.
3118 */
3119static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3120{
3121 struct h2c *h2c = h2s->h2c;
3122 struct h1m *h1m = &h2s->res;
3123 struct chunk outbuf;
3124 int ret = 0;
3125 int total = 0;
3126 int es_now = 0;
3127 int size = 0;
3128 char *blk1, *blk2;
3129 int len1, len2;
3130
3131 if (h2c_mux_busy(h2c, h2s)) {
3132 h2s->flags |= H2_SF_BLK_MBUSY;
3133 goto end;
3134 }
3135
Willy Tarreau44e973f2018-03-01 17:49:30 +01003136 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003137 h2c->flags |= H2_CF_MUX_MALLOC;
3138 h2s->flags |= H2_SF_BLK_MROOM;
3139 goto end;
3140 }
3141
3142 new_frame:
3143 if (!buf->o)
3144 goto end;
3145
3146 chunk_reset(&outbuf);
3147
3148 while (1) {
3149 outbuf.str = bo_end(h2c->mbuf);
3150 outbuf.size = bo_contig_space(h2c->mbuf);
3151 outbuf.len = 0;
3152
Willy Tarreauaa7af722018-07-12 10:33:12 +02003153 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003154 break;
3155 realign_again:
Willy Tarreau0db4d102018-07-12 11:00:01 +02003156 b_slow_realign(h2c->mbuf, trash.str, h2c->mbuf->o);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003157 }
3158
3159 if (outbuf.size < 9) {
3160 h2c->flags |= H2_CF_MUX_MFULL;
3161 h2s->flags |= H2_SF_BLK_MROOM;
3162 goto end;
3163 }
3164
3165 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3166 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3167 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3168 outbuf.len = 9;
3169
3170 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3171 case 0: /* no content length, read till SHUTW */
3172 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003173 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003174 break;
3175 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3176 size = buf->o;
3177 if ((long long)size > h1m->curr_len)
3178 size = h1m->curr_len;
3179 break;
3180 default: /* te:chunked : parse chunks */
3181 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3182 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3183 if (!ret)
3184 goto end;
3185
3186 if (ret < 0) {
3187 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3188 h1m->err_pos = ret;
3189 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3190 goto end;
3191 }
3192 bo_del(buf, ret);
3193 total += ret;
3194 h1m->state = HTTP_MSG_CHUNK_SIZE;
3195 }
3196
3197 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3198 unsigned int chunk;
3199
3200 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3201 if (!ret)
3202 goto end;
3203
3204 if (ret < 0) {
3205 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3206 h1m->err_pos = ret;
3207 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3208 goto end;
3209 }
3210
3211 size = chunk;
3212 h1m->curr_len = chunk;
3213 h1m->body_len += chunk;
3214 bo_del(buf, ret);
3215 total += ret;
3216 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3217 if (!size)
3218 goto send_empty;
3219 }
3220
3221 /* in MSG_DATA state, continue below */
3222 size = h1m->curr_len;
3223 break;
3224 }
3225
3226 /* we have in <size> the exact number of bytes we need to copy from
3227 * the H1 buffer. We need to check this against the connection's and
3228 * the stream's send windows, and to ensure that this fits in the max
3229 * frame size and in the buffer's available space minus 9 bytes (for
3230 * the frame header). The connection's flow control is applied last so
3231 * that we can use a separate list of streams which are immediately
3232 * unblocked on window opening. Note: we don't implement padding.
3233 */
3234
3235 if (size > buf->o)
3236 size = buf->o;
3237
3238 if (size > h2s->mws)
3239 size = h2s->mws;
3240
3241 if (size <= 0) {
3242 h2s->flags |= H2_SF_BLK_SFCTL;
3243 goto end;
3244 }
3245
3246 if (h2c->mfs && size > h2c->mfs)
3247 size = h2c->mfs;
3248
3249 if (size + 9 > outbuf.size) {
3250 /* we have an opportunity for enlarging the too small
3251 * available space, let's try.
3252 */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003253 if (b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003254 goto realign_again;
3255 size = outbuf.size - 9;
3256 }
3257
3258 if (size <= 0) {
3259 h2c->flags |= H2_CF_MUX_MFULL;
3260 h2s->flags |= H2_SF_BLK_MROOM;
3261 goto end;
3262 }
3263
3264 if (size > h2c->mws)
3265 size = h2c->mws;
3266
3267 if (size <= 0) {
3268 h2s->flags |= H2_SF_BLK_MFCTL;
3269 goto end;
3270 }
3271
3272 /* copy whatever we can */
3273 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3274 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3275 if (ret == 1)
3276 len2 = 0;
3277
3278 if (!ret || len1 + len2 < size) {
3279 /* FIXME: must normally never happen */
3280 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3281 goto end;
3282 }
3283
3284 /* limit len1/len2 to size */
3285 if (len1 + len2 > size) {
3286 int sub = len1 + len2 - size;
3287
3288 if (len2 > sub)
3289 len2 -= sub;
3290 else {
3291 sub -= len2;
3292 len2 = 0;
3293 len1 -= sub;
3294 }
3295 }
3296
3297 /* now let's copy this this into the output buffer */
3298 memcpy(outbuf.str + 9, blk1, len1);
3299 if (len2)
3300 memcpy(outbuf.str + 9 + len1, blk2, len2);
3301
3302 send_empty:
3303 /* we may need to add END_STREAM */
3304 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3305 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003306 *
3307 * FIXME: what we do here is not correct because we send end_stream
3308 * before knowing if we'll have to send a HEADERS frame for the
3309 * trailers. More importantly we're not consuming the trailing CRLF
3310 * after the end of trailers, so it will be left to the caller to
3311 * eat it. The right way to do it would be to measure trailers here
3312 * and to send ES only if there are no trailers.
3313 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 */
3315 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3316 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3317 es_now = 1;
3318
3319 /* update the frame's size */
3320 h2_set_frame_size(outbuf.str, size);
3321
3322 if (es_now)
3323 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3324
3325 /* commit the H2 response */
3326 h2c->mbuf->o += size + 9;
3327 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3328
3329 /* consume incoming H1 response */
3330 if (size > 0) {
3331 bo_del(buf, size);
3332 total += size;
3333 h1m->curr_len -= size;
3334 h2s->mws -= size;
3335 h2c->mws -= size;
3336
3337 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3338 h1m->state = HTTP_MSG_CHUNK_CRLF;
3339 goto new_frame;
3340 }
3341 }
3342
3343 if (es_now) {
3344 if (h2s->st == H2_SS_OPEN)
3345 h2s->st = H2_SS_HLOC;
3346 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003347 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003348
Willy Tarreau35a62702018-02-27 15:37:25 +01003349 if (!(h1m->flags & H1_MF_CHNK)) {
3350 // trim any possibly pending data (eg: inconsistent content-length)
3351 bo_del(buf, buf->o);
3352
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003353 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003354 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003355
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003356 h2s->flags |= H2_SF_ES_SENT;
3357 }
3358
3359 end:
Willy Tarreau506a29a2018-07-18 10:07:58 +02003360 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%u", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)buf->o);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003361 return total;
3362}
3363
Willy Tarreau62f52692017-10-08 23:01:42 +02003364/* Called from the upper layer, to send data */
3365static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3366{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003367 struct h2s *h2s = cs->ctx;
3368 int total = 0;
3369
Willy Tarreauc4312d32017-11-07 12:01:53 +01003370 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3371 h2s->flags |= H2_SF_OUTGOING_DATA;
3372
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003373 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3374 if (h2s->res.state < HTTP_MSG_BODY) {
3375 total += h2s_frt_make_resp_headers(h2s, buf);
3376
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003377 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003378 break;
3379
3380 if (h2s->flags & H2_SF_BLK_ANY)
3381 break;
3382 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003383 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3384 total += h2s_frt_make_resp_data(h2s, buf);
3385
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003386 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003387 break;
3388
3389 if (h2s->flags & H2_SF_BLK_ANY)
3390 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003391 }
3392 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3393 /* consume the trailers if any (we don't forward them for now) */
3394 int count = h1_measure_trailers(buf);
3395
3396 if (unlikely(count <= 0)) {
3397 if (count < 0)
3398 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3399 break;
3400 }
3401 total += count;
3402 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003403
3404 // trim any possibly pending data (eg: extra CR-LF, ...)
3405 bo_del(buf, buf->o);
3406
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003407 h2s->res.state = HTTP_MSG_DONE;
3408 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003409 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003410 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003411 cs->flags |= CS_FL_ERROR;
3412 break;
3413 }
3414 }
3415
Willy Tarreau00610962018-07-19 10:58:28 +02003416 if (h2s->st >= H2_SS_ERROR) {
3417 /* trim any possibly pending data after we close (extra CR-LF,
3418 * unprocessed trailers, abnormal extra data, ...)
3419 */
3420 bo_del(buf, buf->o);
3421 }
3422
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003423 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003424 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003425 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003426 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003427 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003428 }
3429
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003430 if (h2s->flags & H2_SF_BLK_SFCTL) {
3431 /* stream flow control, quit the list */
3432 LIST_DEL(&h2s->list);
3433 LIST_INIT(&h2s->list);
3434 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003435 else if (LIST_ISEMPTY(&h2s->list)) {
3436 if (h2s->flags & H2_SF_BLK_MFCTL)
3437 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3438 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3439 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3440 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003441
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003442 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003443}
3444
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003445/* for debugging with CLI's "show fd" command */
3446static void h2_show_fd(struct chunk *msg, struct connection *conn)
3447{
3448 struct h2c *h2c = conn->mux_ctx;
3449 struct h2s *h2s;
3450 struct eb32_node *node;
3451 int fctl_cnt = 0;
3452 int send_cnt = 0;
3453 int tree_cnt = 0;
3454 int orph_cnt = 0;
3455
3456 if (!h2c)
3457 return;
3458
3459 list_for_each_entry(h2s, &h2c->fctl_list, list)
3460 fctl_cnt++;
3461
3462 list_for_each_entry(h2s, &h2c->send_list, list)
3463 send_cnt++;
3464
3465 node = eb32_first(&h2c->streams_by_id);
3466 while (node) {
3467 h2s = container_of(node, struct h2s, by_id);
3468 tree_cnt++;
3469 if (!h2s->cs)
3470 orph_cnt++;
3471 node = eb32_next(node);
3472 }
3473
Willy Tarreauc65edac2018-07-19 10:54:43 +02003474 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 +02003475 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 +02003476}
Willy Tarreau62f52692017-10-08 23:01:42 +02003477
3478/*******************************************************/
3479/* functions below are dedicated to the config parsers */
3480/*******************************************************/
3481
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003482/* config parser for global "tune.h2.header-table-size" */
3483static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3484 struct proxy *defpx, const char *file, int line,
3485 char **err)
3486{
3487 if (too_many_args(1, args, err, NULL))
3488 return -1;
3489
3490 h2_settings_header_table_size = atoi(args[1]);
3491 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3492 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3493 return -1;
3494 }
3495 return 0;
3496}
Willy Tarreau62f52692017-10-08 23:01:42 +02003497
Willy Tarreaue6baec02017-07-27 11:45:11 +02003498/* config parser for global "tune.h2.initial-window-size" */
3499static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3500 struct proxy *defpx, const char *file, int line,
3501 char **err)
3502{
3503 if (too_many_args(1, args, err, NULL))
3504 return -1;
3505
3506 h2_settings_initial_window_size = atoi(args[1]);
3507 if (h2_settings_initial_window_size < 0) {
3508 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3509 return -1;
3510 }
3511 return 0;
3512}
3513
Willy Tarreau5242ef82017-07-27 11:47:28 +02003514/* config parser for global "tune.h2.max-concurrent-streams" */
3515static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3516 struct proxy *defpx, const char *file, int line,
3517 char **err)
3518{
3519 if (too_many_args(1, args, err, NULL))
3520 return -1;
3521
3522 h2_settings_max_concurrent_streams = atoi(args[1]);
3523 if (h2_settings_max_concurrent_streams < 0) {
3524 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3525 return -1;
3526 }
3527 return 0;
3528}
3529
Willy Tarreau62f52692017-10-08 23:01:42 +02003530
3531/****************************************/
3532/* MUX initialization and instanciation */
3533/***************************************/
3534
3535/* The mux operations */
3536const struct mux_ops h2_ops = {
3537 .init = h2_init,
3538 .recv = h2_recv,
3539 .send = h2_send,
3540 .wake = h2_wake,
3541 .update_poll = h2_update_poll,
3542 .rcv_buf = h2_rcv_buf,
3543 .snd_buf = h2_snd_buf,
3544 .attach = h2_attach,
3545 .detach = h2_detach,
3546 .shutr = h2_shutr,
3547 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003548 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003549 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003550 .name = "H2",
3551};
3552
3553/* ALPN selection : this mux registers ALPN tolen "h2" */
3554static struct alpn_mux_list alpn_mux_h2 =
3555 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3556
3557/* config keyword parsers */
3558static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003559 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003560 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003561 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003562 { 0, NULL, NULL }
3563}};
3564
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003565static void __h2_deinit(void)
3566{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003567 pool_destroy(pool_head_h2s);
3568 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003569}
3570
Willy Tarreau62f52692017-10-08 23:01:42 +02003571__attribute__((constructor))
3572static void __h2_init(void)
3573{
3574 alpn_register_mux(&alpn_mux_h2);
3575 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003576 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003577 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3578 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003579}