blob: 3dfb396a5420f185a798945baeb089634a81893a [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 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
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 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 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 */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
186};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200187
Willy Tarreauc6405142017-09-21 20:23:50 +0200188/* descriptor for an h2 frame header */
189struct h2_fh {
190 uint32_t len; /* length, host order, 24 bits */
191 uint32_t sid; /* stream id, host order, 31 bits */
192 uint8_t ft; /* frame type */
193 uint8_t ff; /* frame flags */
194};
195
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200196/* a few settings from the global section */
197static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200198static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200199static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200200
Willy Tarreau2a856182017-05-16 15:20:39 +0200201/* a dmumy closed stream */
202static const struct h2s *h2_closed_stream = &(const struct h2s){
203 .cs = NULL,
204 .h2c = NULL,
205 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100206 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100207 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200208 .id = 0,
209};
210
211/* and a dummy idle stream for use with any unannounced stream */
212static const struct h2s *h2_idle_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200217 .id = 0,
218};
219
Olivier Houchard9f6af332018-05-25 14:04:04 +0200220static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200221static struct task *h2_send(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200222
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200223/*****************************************************/
224/* functions below are for dynamic buffer management */
225/*****************************************************/
226
Willy Tarreau315d8072017-12-10 22:17:57 +0100227/* indicates whether or not the we may call the h2_recv() function to attempt
228 * to receive data into the buffer and/or demux pending data. The condition is
229 * a bit complex due to some API limits for now. The rules are the following :
230 * - if an error or a shutdown was detected on the connection and the buffer
231 * is empty, we must not attempt to receive
232 * - if the demux buf failed to be allocated, we must not try to receive and
233 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100234 * - if no flag indicates a blocking condition, we may attempt to receive,
235 * regardless of whether the demux buffer is full or not, so that only
236 * de demux part decides whether or not to block. This is needed because
237 * the connection API indeed prevents us from re-enabling receipt that is
238 * already enabled in a polled state, so we must always immediately stop
239 * as soon as the demux can't proceed so as never to hit an end of read
240 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100241 * - otherwise must may not attempt
242 */
243static inline int h2_recv_allowed(const struct h2c *h2c)
244{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200245 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100246 (h2c->st0 >= H2_CS_ERROR ||
247 h2c->conn->flags & CO_FL_ERROR ||
248 conn_xprt_read0_pending(h2c->conn)))
249 return 0;
250
251 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100252 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100253 return 1;
254
255 return 0;
256}
257
Willy Tarreauf2101912018-07-19 10:11:38 +0200258/* returns true if the connection has too many conn_streams attached */
259static inline int h2_has_too_many_cs(const struct h2c *h2c)
260{
261 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
262}
263
Willy Tarreau44e973f2018-03-01 17:49:30 +0100264/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
265 * flags are used to figure what buffer was requested. It returns 1 if the
266 * allocation succeeds, in which case the connection is woken up, or 0 if it's
267 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200268 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100269static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200270{
271 struct h2c *h2c = target;
272
Willy Tarreau44e973f2018-03-01 17:49:30 +0100273 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200274 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100275 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200276 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277 return 1;
278 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200279
Willy Tarreau44e973f2018-03-01 17:49:30 +0100280 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
281 h2c->flags &= ~H2_CF_MUX_MALLOC;
282 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
283 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200284
285 if (h2c->flags & H2_CF_DEM_MROOM) {
286 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100287 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200288 conn_xprt_want_recv(h2c->conn);
289 }
Willy Tarreau14398122017-09-22 14:26:04 +0200290 return 1;
291 }
292 return 0;
293}
294
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200295static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200296{
297 struct buffer *buf = NULL;
298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
300 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
301 h2c->buf_wait.target = h2c;
302 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100303 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100304 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100305 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200306 __conn_xprt_stop_recv(h2c->conn);
307 }
308 return buf;
309}
310
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200311static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200312{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200313 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100314 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200315 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200316 }
317}
318
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200319
Willy Tarreau62f52692017-10-08 23:01:42 +0200320/*****************************************************************/
321/* functions below are dedicated to the mux setup and management */
322/*****************************************************************/
323
Willy Tarreau32218eb2017-09-22 08:07:25 +0200324/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
325static int h2c_frt_init(struct connection *conn)
326{
327 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100328 struct task *t = NULL;
329 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200330
Willy Tarreaubafbe012017-11-24 17:34:44 +0100331 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200332 if (!h2c)
333 goto fail;
334
Willy Tarreau3f133572017-10-31 19:21:06 +0100335
Willy Tarreau599391a2017-11-24 10:16:00 +0100336 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
337 if (tick_isset(sess->fe->timeout.clientfin))
338 h2c->shut_timeout = sess->fe->timeout.clientfin;
339
Willy Tarreau33400292017-11-05 11:23:40 +0100340 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100341 if (tick_isset(h2c->timeout)) {
342 t = task_new(tid_bit);
343 if (!t)
344 goto fail;
345
346 h2c->task = t;
347 t->process = h2_timeout_task;
348 t->context = h2c;
349 t->expire = tick_add(now_ms, h2c->timeout);
350 }
Willy Tarreauea392822017-10-31 10:02:25 +0100351
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200352 h2c->wait_list.task = tasklet_new();
353 if (!h2c->wait_list.task)
354 goto fail;
355 h2c->wait_list.task->process = h2_send;
356 h2c->wait_list.task->context = conn;
357
Willy Tarreau32218eb2017-09-22 08:07:25 +0200358 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
359 if (!h2c->ddht)
360 goto fail;
361
362 /* Initialise the context. */
363 h2c->st0 = H2_CS_PREFACE;
364 h2c->conn = conn;
365 h2c->max_id = -1;
366 h2c->errcode = H2_ERR_NO_ERROR;
367 h2c->flags = H2_CF_NONE;
368 h2c->rcvd_c = 0;
369 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100370 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200371 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200373 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374 h2c->dsi = -1;
375 h2c->msi = -1;
376 h2c->last_sid = -1;
377
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200378 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200379 h2c->miw = 65535; /* mux initial window size */
380 h2c->mws = 65535; /* mux window size */
381 h2c->mfs = 16384; /* initial max frame size */
382 h2c->streams_by_id = EB_ROOT_UNIQUE;
383 LIST_INIT(&h2c->send_list);
384 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100385 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 conn->mux_ctx = h2c;
387
Willy Tarreau3f133572017-10-31 19:21:06 +0100388 if (t)
389 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200390 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200391 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200392 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100393
Willy Tarreau32218eb2017-09-22 08:07:25 +0200394 /* mux->wake will be called soon to complete the operation */
395 return 0;
396 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100397 if (t)
398 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200399 if (h2c->wait_list.task)
400 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100401 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402 return -1;
403}
404
Willy Tarreau62f52692017-10-08 23:01:42 +0200405/* Initialize the mux once it's attached. For outgoing connections, the context
406 * is already initialized before installing the mux, so we detect incoming
407 * connections from the fact that the context is still NULL. Returns < 0 on
408 * error.
409 */
410static int h2_init(struct connection *conn)
411{
412 if (conn->mux_ctx) {
413 /* we don't support outgoing connections for now */
414 return -1;
415 }
416
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200418}
419
Willy Tarreau2373acc2017-10-12 17:35:14 +0200420/* returns the stream associated with id <id> or NULL if not found */
421static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
422{
423 struct eb32_node *node;
424
Willy Tarreau2a856182017-05-16 15:20:39 +0200425 if (id > h2c->max_id)
426 return (struct h2s *)h2_idle_stream;
427
Willy Tarreau2373acc2017-10-12 17:35:14 +0200428 node = eb32_lookup(&h2c->streams_by_id, id);
429 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200430 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200431
432 return container_of(node, struct h2s, by_id);
433}
434
Willy Tarreau62f52692017-10-08 23:01:42 +0200435/* release function for a connection. This one should be called to free all
436 * resources allocated to the mux.
437 */
438static void h2_release(struct connection *conn)
439{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200440 struct h2c *h2c = conn->mux_ctx;
441
442 LIST_DEL(&conn->list);
443
444 if (h2c) {
445 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200446
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100447 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100448 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200450
Willy Tarreau44e973f2018-03-01 17:49:30 +0100451 h2_release_buf(h2c, &h2c->dbuf);
452 h2_release_buf(h2c, &h2c->mbuf);
453
Willy Tarreauea392822017-10-31 10:02:25 +0100454 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200455 h2c->task->context = NULL;
456 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100457 h2c->task = NULL;
458 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200459 if (h2c->wait_list.task)
460 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100461
Willy Tarreaubafbe012017-11-24 17:34:44 +0100462 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 }
464
465 conn->mux = NULL;
466 conn->mux_ctx = NULL;
467
468 conn_stop_tracking(conn);
469 conn_full_close(conn);
470 if (conn->destroy_cb)
471 conn->destroy_cb(conn);
472 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200473}
474
475
Willy Tarreau71681172017-10-23 14:39:06 +0200476/******************************************************/
477/* functions below are for the H2 protocol processing */
478/******************************************************/
479
480/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100481static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200482{
483 return h2s ? h2s->id : 0;
484}
485
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200486/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100487static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200488{
489 if (h2c->msi < 0)
490 return 0;
491
492 if (h2c->msi == h2s_id(h2s))
493 return 0;
494
495 return 1;
496}
497
Willy Tarreau741d6df2017-10-17 08:00:59 +0200498/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100499static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200500{
501 h2c->errcode = err;
502 h2c->st0 = H2_CS_ERROR;
503}
504
Willy Tarreau2e43f082017-10-17 08:03:59 +0200505/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100506static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200507{
508 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
509 h2s->errcode = err;
510 h2s->st = H2_SS_ERROR;
511 if (h2s->cs)
512 h2s->cs->flags |= CS_FL_ERROR;
513 }
514}
515
Willy Tarreaue4820742017-07-27 13:37:23 +0200516/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100517static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200518{
519 uint8_t *out = frame;
520
521 *out = len >> 16;
522 write_n16(out + 1, len);
523}
524
Willy Tarreau54c15062017-10-10 17:10:03 +0200525/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
526 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
527 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200528 * available in the buffer's input prior to calling this function. The buffer
529 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200530 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200532 const struct buffer *b, int o)
533{
Willy Tarreau591d4452018-06-15 17:21:00 +0200534 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200535}
536
Willy Tarreau1f094672017-11-20 21:27:45 +0100537static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200538{
Willy Tarreau591d4452018-06-15 17:21:00 +0200539 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200540}
541
Willy Tarreau1f094672017-11-20 21:27:45 +0100542static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200543{
Willy Tarreau591d4452018-06-15 17:21:00 +0200544 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200545}
546
Willy Tarreau1f094672017-11-20 21:27:45 +0100547static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200548{
Willy Tarreau591d4452018-06-15 17:21:00 +0200549 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200550}
551
552
Willy Tarreau715d5312017-07-11 15:20:24 +0200553/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
554 * is not obvious. It turns out that H2 headers are neither aligned nor do they
555 * use regular sizes. And to add to the trouble, the buffer may wrap so each
556 * byte read must be checked. The header is formed like this :
557 *
558 * b0 b1 b2 b3 b4 b5..b8
559 * +----------+---------+--------+----+----+----------------------+
560 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
561 * +----------+---------+--------+----+----+----------------------+
562 *
563 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
564 * we get the sid properly aligned and ordered, and 16 bits of len properly
565 * ordered as well. The type and flags can be extracted using bit shifts from
566 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200567 * Returns zero if some bytes are missing, otherwise non-zero on success. The
568 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200569 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100570static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200571{
572 uint64_t w;
573
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200574 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200575 return 0;
576
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200577 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200578 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200579 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
580 h->ff = w >> 32;
581 h->ft = w >> 40;
582 h->len += w >> 48;
583 return 1;
584}
585
586/* skip the next 9 bytes corresponding to the frame header possibly parsed by
587 * h2_peek_frame_hdr() above.
588 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100589static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200590{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200591 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200592}
593
594/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100595static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200596{
597 int ret;
598
599 ret = h2_peek_frame_hdr(b, h);
600 if (ret > 0)
601 h2_skip_frame_hdr(b);
602 return ret;
603}
604
Willy Tarreau00dd0782018-03-01 16:31:34 +0100605/* marks stream <h2s> as CLOSED and decrement the number of active streams for
606 * its connection if the stream was not yet closed. Please use this exclusively
607 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100608 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100609static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100610{
611 if (h2s->st != H2_SS_CLOSED)
612 h2s->h2c->nb_streams--;
613 h2s->st = H2_SS_CLOSED;
614}
615
Willy Tarreau71049cc2018-03-28 13:56:39 +0200616/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
617static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100618{
619 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200620 LIST_DEL(&h2s->list);
621 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100622 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100623 pool_free(pool_head_h2s, h2s);
624}
625
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200626/* creates a new stream <id> on the h2c connection and returns it, or NULL in
627 * case of memory allocation error.
628 */
629static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
630{
631 struct conn_stream *cs;
632 struct h2s *h2s;
633
Willy Tarreaubafbe012017-11-24 17:34:44 +0100634 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200635 if (!h2s)
636 goto out;
637
638 h2s->h2c = h2c;
639 h2s->mws = h2c->miw;
640 h2s->flags = H2_SF_NONE;
641 h2s->errcode = H2_ERR_NO_ERROR;
642 h2s->st = H2_SS_IDLE;
643 h1m_init(&h2s->req);
644 h1m_init(&h2s->res);
645 h2s->by_id.key = h2s->id = id;
646 h2c->max_id = id;
647 LIST_INIT(&h2s->list);
648
649 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100650 h2c->nb_streams++;
651 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
652 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200653
654 cs = cs_new(h2c->conn);
655 if (!cs)
656 goto out_close;
657
658 h2s->cs = cs;
659 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200660 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200661
662 if (stream_create_from_cs(cs) < 0)
663 goto out_free_cs;
664
665 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200666 if (h2_has_too_many_cs(h2c))
667 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200668 return h2s;
669
670 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200671 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200672 cs_free(cs);
673 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200674 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675 h2s = NULL;
676 out:
677 return h2s;
678}
679
Willy Tarreaube5b7152017-09-25 16:25:39 +0200680/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
681 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
682 * the various settings codes.
683 */
684static int h2c_snd_settings(struct h2c *h2c)
685{
686 struct buffer *res;
687 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200688 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200689 int ret;
690
691 if (h2c_mux_busy(h2c, NULL)) {
692 h2c->flags |= H2_CF_DEM_MBUSY;
693 return 0;
694 }
695
Willy Tarreau44e973f2018-03-01 17:49:30 +0100696 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200697 if (!res) {
698 h2c->flags |= H2_CF_MUX_MALLOC;
699 h2c->flags |= H2_CF_DEM_MROOM;
700 return 0;
701 }
702
703 chunk_init(&buf, buf_data, sizeof(buf_data));
704 chunk_memcpy(&buf,
705 "\x00\x00\x00" /* length : 0 for now */
706 "\x04\x00" /* type : 4 (settings), flags : 0 */
707 "\x00\x00\x00\x00", /* stream ID : 0 */
708 9);
709
710 if (h2_settings_header_table_size != 4096) {
711 char str[6] = "\x00\x01"; /* header_table_size */
712
713 write_n32(str + 2, h2_settings_header_table_size);
714 chunk_memcat(&buf, str, 6);
715 }
716
717 if (h2_settings_initial_window_size != 65535) {
718 char str[6] = "\x00\x04"; /* initial_window_size */
719
720 write_n32(str + 2, h2_settings_initial_window_size);
721 chunk_memcat(&buf, str, 6);
722 }
723
724 if (h2_settings_max_concurrent_streams != 0) {
725 char str[6] = "\x00\x03"; /* max_concurrent_streams */
726
727 /* Note: 0 means "unlimited" for haproxy's config but not for
728 * the protocol, so never send this value!
729 */
730 write_n32(str + 2, h2_settings_max_concurrent_streams);
731 chunk_memcat(&buf, str, 6);
732 }
733
734 if (global.tune.bufsize != 16384) {
735 char str[6] = "\x00\x05"; /* max_frame_size */
736
737 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
738 * match bufsize - rewrite size, but at the moment it seems
739 * that clients don't take care of it.
740 */
741 write_n32(str + 2, global.tune.bufsize);
742 chunk_memcat(&buf, str, 6);
743 }
744
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200745 h2_set_frame_size(buf.area, buf.data - 9);
746 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200747 if (unlikely(ret <= 0)) {
748 if (!ret) {
749 h2c->flags |= H2_CF_MUX_MFULL;
750 h2c->flags |= H2_CF_DEM_MROOM;
751 return 0;
752 }
753 else {
754 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
755 return 0;
756 }
757 }
758 return ret;
759}
760
Willy Tarreau52eed752017-09-22 15:05:09 +0200761/* Try to receive a connection preface, then upon success try to send our
762 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
763 * missing data. It may return an error in h2c.
764 */
765static int h2c_frt_recv_preface(struct h2c *h2c)
766{
767 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200768 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200769
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200770 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200771
772 if (unlikely(ret1 <= 0)) {
773 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
774 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
775 return 0;
776 }
777
Willy Tarreaube5b7152017-09-25 16:25:39 +0200778 ret2 = h2c_snd_settings(h2c);
779 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200780 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200781
Willy Tarreaube5b7152017-09-25 16:25:39 +0200782 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200783}
784
Willy Tarreau081d4722017-05-16 21:51:05 +0200785/* try to send a GOAWAY frame on the connection to report an error or a graceful
786 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
787 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
788 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
789 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
790 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
791 * on unrecoverable failure. It will not attempt to send one again in this last
792 * case so that it is safe to use h2c_error() to report such errors.
793 */
794static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
795{
796 struct buffer *res;
797 char str[17];
798 int ret;
799
800 if (h2c->flags & H2_CF_GOAWAY_FAILED)
801 return 1; // claim that it worked
802
803 if (h2c_mux_busy(h2c, h2s)) {
804 if (h2s)
805 h2s->flags |= H2_SF_BLK_MBUSY;
806 else
807 h2c->flags |= H2_CF_DEM_MBUSY;
808 return 0;
809 }
810
Willy Tarreau44e973f2018-03-01 17:49:30 +0100811 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200812 if (!res) {
813 h2c->flags |= H2_CF_MUX_MALLOC;
814 if (h2s)
815 h2s->flags |= H2_SF_BLK_MROOM;
816 else
817 h2c->flags |= H2_CF_DEM_MROOM;
818 return 0;
819 }
820
821 /* len: 8, type: 7, flags: none, sid: 0 */
822 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
823
824 if (h2c->last_sid < 0)
825 h2c->last_sid = h2c->max_id;
826
827 write_n32(str + 9, h2c->last_sid);
828 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200829 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200830 if (unlikely(ret <= 0)) {
831 if (!ret) {
832 h2c->flags |= H2_CF_MUX_MFULL;
833 if (h2s)
834 h2s->flags |= H2_SF_BLK_MROOM;
835 else
836 h2c->flags |= H2_CF_DEM_MROOM;
837 return 0;
838 }
839 else {
840 /* we cannot report this error using GOAWAY, so we mark
841 * it and claim a success.
842 */
843 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
844 h2c->flags |= H2_CF_GOAWAY_FAILED;
845 return 1;
846 }
847 }
848 h2c->flags |= H2_CF_GOAWAY_SENT;
849 return ret;
850}
851
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100852/* Try to send an RST_STREAM frame on the connection for the indicated stream
853 * during mux operations. This stream must be valid and cannot be closed
854 * already. h2s->id will be used for the stream ID and h2s->errcode will be
855 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
856 * not yet.
857 *
858 * Returns > 0 on success or zero if nothing was done. In case of lack of room
859 * to write the message, it subscribes the stream to future notifications.
860 */
861static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
862{
863 struct buffer *res;
864 char str[13];
865 int ret;
866
867 if (!h2s || h2s->st == H2_SS_CLOSED)
868 return 1;
869
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100870 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
871 * RST_STREAM in response to a RST_STREAM frame.
872 */
873 if (h2c->dft == H2_FT_RST_STREAM) {
874 ret = 1;
875 goto ignore;
876 }
877
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100878 if (h2c_mux_busy(h2c, h2s)) {
879 h2s->flags |= H2_SF_BLK_MBUSY;
880 return 0;
881 }
882
Willy Tarreau44e973f2018-03-01 17:49:30 +0100883 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100884 if (!res) {
885 h2c->flags |= H2_CF_MUX_MALLOC;
886 h2s->flags |= H2_SF_BLK_MROOM;
887 return 0;
888 }
889
890 /* len: 4, type: 3, flags: none */
891 memcpy(str, "\x00\x00\x04\x03\x00", 5);
892 write_n32(str + 5, h2s->id);
893 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200894 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100895
896 if (unlikely(ret <= 0)) {
897 if (!ret) {
898 h2c->flags |= H2_CF_MUX_MFULL;
899 h2s->flags |= H2_SF_BLK_MROOM;
900 return 0;
901 }
902 else {
903 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
904 return 0;
905 }
906 }
907
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100908 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100909 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100910 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100911 return ret;
912}
913
914/* Try to send an RST_STREAM frame on the connection for the stream being
915 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
916 * error code unless the stream's state already is IDLE or CLOSED in which
917 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
918 * it was not yet.
919 *
920 * Returns > 0 on success or zero if nothing was done. In case of lack of room
921 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200924 */
925static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
926{
927 struct buffer *res;
928 char str[13];
929 int ret;
930
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100931 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
932 * RST_STREAM in response to a RST_STREAM frame.
933 */
934 if (h2c->dft == H2_FT_RST_STREAM) {
935 ret = 1;
936 goto ignore;
937 }
938
Willy Tarreau27a84c92017-10-17 08:10:17 +0200939 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 return 0;
942 }
943
Willy Tarreau44e973f2018-03-01 17:49:30 +0100944 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200945 if (!res) {
946 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100947 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200948 return 0;
949 }
950
951 /* len: 4, type: 3, flags: none */
952 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100953
Willy Tarreau27a84c92017-10-17 08:10:17 +0200954 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100955 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200956 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200957 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100958
Willy Tarreau27a84c92017-10-17 08:10:17 +0200959 if (unlikely(ret <= 0)) {
960 if (!ret) {
961 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100962 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200963 return 0;
964 }
965 else {
966 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
967 return 0;
968 }
969 }
970
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100971 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100972 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200973 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100974 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100975 }
976
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 return ret;
978}
979
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100980/* try to send an empty DATA frame with the ES flag set to notify about the
981 * end of stream and match a shutdown(write). If an ES was already sent as
982 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
983 * on success or zero if nothing was done. In case of lack of room to write the
984 * message, it subscribes the requesting stream to future notifications.
985 */
986static int h2_send_empty_data_es(struct h2s *h2s)
987{
988 struct h2c *h2c = h2s->h2c;
989 struct buffer *res;
990 char str[9];
991 int ret;
992
Willy Tarreau721c9742017-11-07 11:05:42 +0100993 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100994 return 1;
995
996 if (h2c_mux_busy(h2c, h2s)) {
997 h2s->flags |= H2_SF_BLK_MBUSY;
998 return 0;
999 }
1000
Willy Tarreau44e973f2018-03-01 17:49:30 +01001001 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001002 if (!res) {
1003 h2c->flags |= H2_CF_MUX_MALLOC;
1004 h2s->flags |= H2_SF_BLK_MROOM;
1005 return 0;
1006 }
1007
1008 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1009 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1010 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001011 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001012 if (likely(ret > 0)) {
1013 h2s->flags |= H2_SF_ES_SENT;
1014 }
1015 else if (!ret) {
1016 h2c->flags |= H2_CF_MUX_MFULL;
1017 h2s->flags |= H2_SF_BLK_MROOM;
1018 return 0;
1019 }
1020 else {
1021 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1022 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001023 }
1024 return ret;
1025}
1026
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001027/* wake the streams attached to the connection, whose id is greater than <last>,
1028 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1029 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1030 * stream's state is automatically updated accordingly.
1031 */
1032static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1033{
1034 struct eb32_node *node;
1035 struct h2s *h2s;
1036
1037 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1038 flags |= CS_FL_ERROR;
1039
1040 if (conn_xprt_read0_pending(h2c->conn))
1041 flags |= CS_FL_EOS;
1042
1043 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1044 while (node) {
1045 h2s = container_of(node, struct h2s, by_id);
1046 if (h2s->id <= last)
1047 break;
1048 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001049
1050 if (!h2s->cs) {
1051 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001052 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001053 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001054 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001055
1056 h2s->cs->flags |= flags;
1057 /* recv is used to force to detect CS_FL_EOS that wake()
1058 * doesn't handle in the stream int code.
1059 */
1060 h2s->cs->data_cb->recv(h2s->cs);
1061 h2s->cs->data_cb->wake(h2s->cs);
1062
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001063 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1064 h2s->st = H2_SS_ERROR;
1065 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1066 h2s->st = H2_SS_HREM;
1067 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001068 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001069 }
1070}
1071
Willy Tarreau3421aba2017-07-27 15:41:03 +02001072/* Increase all streams' outgoing window size by the difference passed in
1073 * argument. This is needed upon receipt of the settings frame if the initial
1074 * window size is different. The difference may be negative and the resulting
1075 * window size as well, for the time it takes to receive some window updates.
1076 */
1077static void h2c_update_all_ws(struct h2c *h2c, int diff)
1078{
1079 struct h2s *h2s;
1080 struct eb32_node *node;
1081
1082 if (!diff)
1083 return;
1084
1085 node = eb32_first(&h2c->streams_by_id);
1086 while (node) {
1087 h2s = container_of(node, struct h2s, by_id);
1088 h2s->mws += diff;
1089 node = eb32_next(node);
1090 }
1091}
1092
1093/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1094 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1095 * return an error in h2c. Described in RFC7540#6.5.
1096 */
1097static int h2c_handle_settings(struct h2c *h2c)
1098{
1099 unsigned int offset;
1100 int error;
1101
1102 if (h2c->dff & H2_F_SETTINGS_ACK) {
1103 if (h2c->dfl) {
1104 error = H2_ERR_FRAME_SIZE_ERROR;
1105 goto fail;
1106 }
1107 return 1;
1108 }
1109
1110 if (h2c->dsi != 0) {
1111 error = H2_ERR_PROTOCOL_ERROR;
1112 goto fail;
1113 }
1114
1115 if (h2c->dfl % 6) {
1116 error = H2_ERR_FRAME_SIZE_ERROR;
1117 goto fail;
1118 }
1119
1120 /* that's the limit we can process */
1121 if (h2c->dfl > global.tune.bufsize) {
1122 error = H2_ERR_FRAME_SIZE_ERROR;
1123 goto fail;
1124 }
1125
1126 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001127 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001128 return 0;
1129
1130 /* parse the frame */
1131 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001132 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1133 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001134
1135 switch (type) {
1136 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1137 /* we need to update all existing streams with the
1138 * difference from the previous iws.
1139 */
1140 if (arg < 0) { // RFC7540#6.5.2
1141 error = H2_ERR_FLOW_CONTROL_ERROR;
1142 goto fail;
1143 }
1144 h2c_update_all_ws(h2c, arg - h2c->miw);
1145 h2c->miw = arg;
1146 break;
1147 case H2_SETTINGS_MAX_FRAME_SIZE:
1148 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1149 error = H2_ERR_PROTOCOL_ERROR;
1150 goto fail;
1151 }
1152 h2c->mfs = arg;
1153 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001154 case H2_SETTINGS_ENABLE_PUSH:
1155 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1156 error = H2_ERR_PROTOCOL_ERROR;
1157 goto fail;
1158 }
1159 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001160 }
1161 }
1162
1163 /* need to ACK this frame now */
1164 h2c->st0 = H2_CS_FRAME_A;
1165 return 1;
1166 fail:
1167 h2c_error(h2c, error);
1168 return 0;
1169}
1170
1171/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1172 * success or one of the h2_status values.
1173 */
1174static int h2c_ack_settings(struct h2c *h2c)
1175{
1176 struct buffer *res;
1177 char str[9];
1178 int ret = -1;
1179
1180 if (h2c_mux_busy(h2c, NULL)) {
1181 h2c->flags |= H2_CF_DEM_MBUSY;
1182 return 0;
1183 }
1184
Willy Tarreau44e973f2018-03-01 17:49:30 +01001185 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001186 if (!res) {
1187 h2c->flags |= H2_CF_MUX_MALLOC;
1188 h2c->flags |= H2_CF_DEM_MROOM;
1189 return 0;
1190 }
1191
1192 memcpy(str,
1193 "\x00\x00\x00" /* length : 0 (no data) */
1194 "\x04" "\x01" /* type : 4, flags : ACK */
1195 "\x00\x00\x00\x00" /* stream ID */, 9);
1196
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001197 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001198 if (unlikely(ret <= 0)) {
1199 if (!ret) {
1200 h2c->flags |= H2_CF_MUX_MFULL;
1201 h2c->flags |= H2_CF_DEM_MROOM;
1202 return 0;
1203 }
1204 else {
1205 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1206 return 0;
1207 }
1208 }
1209 return ret;
1210}
1211
Willy Tarreaucf68c782017-10-10 17:11:41 +02001212/* processes a PING frame and schedules an ACK if needed. The caller must pass
1213 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1214 * missing data. It may return an error in h2c.
1215 */
1216static int h2c_handle_ping(struct h2c *h2c)
1217{
1218 /* frame length must be exactly 8 */
1219 if (h2c->dfl != 8) {
1220 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1221 return 0;
1222 }
1223
1224 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001225 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001226 h2c->st0 = H2_CS_FRAME_A;
1227 return 1;
1228}
1229
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001230/* Try to send a window update for stream id <sid> and value <increment>.
1231 * Returns > 0 on success or zero on missing room or failure. It may return an
1232 * error in h2c.
1233 */
1234static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1235{
1236 struct buffer *res;
1237 char str[13];
1238 int ret = -1;
1239
1240 if (h2c_mux_busy(h2c, NULL)) {
1241 h2c->flags |= H2_CF_DEM_MBUSY;
1242 return 0;
1243 }
1244
Willy Tarreau44e973f2018-03-01 17:49:30 +01001245 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001246 if (!res) {
1247 h2c->flags |= H2_CF_MUX_MALLOC;
1248 h2c->flags |= H2_CF_DEM_MROOM;
1249 return 0;
1250 }
1251
1252 /* length: 4, type: 8, flags: none */
1253 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1254 write_n32(str + 5, sid);
1255 write_n32(str + 9, increment);
1256
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001257 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001258
1259 if (unlikely(ret <= 0)) {
1260 if (!ret) {
1261 h2c->flags |= H2_CF_MUX_MFULL;
1262 h2c->flags |= H2_CF_DEM_MROOM;
1263 return 0;
1264 }
1265 else {
1266 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1267 return 0;
1268 }
1269 }
1270 return ret;
1271}
1272
1273/* try to send pending window update for the connection. It's safe to call it
1274 * with no pending updates. Returns > 0 on success or zero on missing room or
1275 * failure. It may return an error in h2c.
1276 */
1277static int h2c_send_conn_wu(struct h2c *h2c)
1278{
1279 int ret = 1;
1280
1281 if (h2c->rcvd_c <= 0)
1282 return 1;
1283
1284 /* send WU for the connection */
1285 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1286 if (ret > 0)
1287 h2c->rcvd_c = 0;
1288
1289 return ret;
1290}
1291
1292/* try to send pending window update for the current dmux stream. It's safe to
1293 * call it with no pending updates. Returns > 0 on success or zero on missing
1294 * room or failure. It may return an error in h2c.
1295 */
1296static int h2c_send_strm_wu(struct h2c *h2c)
1297{
1298 int ret = 1;
1299
1300 if (h2c->rcvd_s <= 0)
1301 return 1;
1302
1303 /* send WU for the stream */
1304 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1305 if (ret > 0)
1306 h2c->rcvd_s = 0;
1307
1308 return ret;
1309}
1310
Willy Tarreaucf68c782017-10-10 17:11:41 +02001311/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1312 * success, 0 on missing data or one of the h2_status values.
1313 */
1314static int h2c_ack_ping(struct h2c *h2c)
1315{
1316 struct buffer *res;
1317 char str[17];
1318 int ret = -1;
1319
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001320 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001321 return 0;
1322
1323 if (h2c_mux_busy(h2c, NULL)) {
1324 h2c->flags |= H2_CF_DEM_MBUSY;
1325 return 0;
1326 }
1327
Willy Tarreau44e973f2018-03-01 17:49:30 +01001328 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001329 if (!res) {
1330 h2c->flags |= H2_CF_MUX_MALLOC;
1331 h2c->flags |= H2_CF_DEM_MROOM;
1332 return 0;
1333 }
1334
1335 memcpy(str,
1336 "\x00\x00\x08" /* length : 8 (same payload) */
1337 "\x06" "\x01" /* type : 6, flags : ACK */
1338 "\x00\x00\x00\x00" /* stream ID */, 9);
1339
1340 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001341 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001342
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001343 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001344 if (unlikely(ret <= 0)) {
1345 if (!ret) {
1346 h2c->flags |= H2_CF_MUX_MFULL;
1347 h2c->flags |= H2_CF_DEM_MROOM;
1348 return 0;
1349 }
1350 else {
1351 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1352 return 0;
1353 }
1354 }
1355 return ret;
1356}
1357
Willy Tarreau26f95952017-07-27 17:18:30 +02001358/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1359 * Returns > 0 on success or zero on missing data. It may return an error in
1360 * h2c or h2s. Described in RFC7540#6.9.
1361 */
1362static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1363{
1364 int32_t inc;
1365 int error;
1366
1367 if (h2c->dfl != 4) {
1368 error = H2_ERR_FRAME_SIZE_ERROR;
1369 goto conn_err;
1370 }
1371
1372 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001373 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001374 return 0;
1375
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001376 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001377
1378 if (h2c->dsi != 0) {
1379 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001380
1381 /* it's not an error to receive WU on a closed stream */
1382 if (h2s->st == H2_SS_CLOSED)
1383 return 1;
1384
1385 if (!inc) {
1386 error = H2_ERR_PROTOCOL_ERROR;
1387 goto strm_err;
1388 }
1389
1390 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1391 error = H2_ERR_FLOW_CONTROL_ERROR;
1392 goto strm_err;
1393 }
1394
1395 h2s->mws += inc;
1396 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1397 h2s->flags &= ~H2_SF_BLK_SFCTL;
1398 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1399 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1400 /* This stream wanted to send but could not due to its
1401 * own flow control. We can put it back into the send
1402 * list now, it will be handled upon next send() call.
1403 */
1404 LIST_ADDQ(&h2c->send_list, &h2s->list);
1405 }
1406 }
1407 }
1408 else {
1409 /* connection window update */
1410 if (!inc) {
1411 error = H2_ERR_PROTOCOL_ERROR;
1412 goto conn_err;
1413 }
1414
1415 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1416 error = H2_ERR_FLOW_CONTROL_ERROR;
1417 goto conn_err;
1418 }
1419
1420 h2c->mws += inc;
1421 }
1422
1423 return 1;
1424
1425 conn_err:
1426 h2c_error(h2c, error);
1427 return 0;
1428
1429 strm_err:
1430 if (h2s) {
1431 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001432 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001433 }
1434 else
1435 h2c_error(h2c, error);
1436 return 0;
1437}
1438
Willy Tarreaue96b0922017-10-30 00:28:29 +01001439/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1440 * the last ID. Returns > 0 on success or zero on missing data. It may return
1441 * an error in h2c. Described in RFC7540#6.8.
1442 */
1443static int h2c_handle_goaway(struct h2c *h2c)
1444{
1445 int error;
1446 int last;
1447
1448 if (h2c->dsi != 0) {
1449 error = H2_ERR_PROTOCOL_ERROR;
1450 goto conn_err;
1451 }
1452
1453 if (h2c->dfl < 8) {
1454 error = H2_ERR_FRAME_SIZE_ERROR;
1455 goto conn_err;
1456 }
1457
1458 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001459 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001460 return 0;
1461
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001462 last = h2_get_n32(&h2c->dbuf, 0);
1463 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001464 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001465 if (h2c->last_sid < 0)
1466 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001467 return 1;
1468
1469 conn_err:
1470 h2c_error(h2c, error);
1471 return 0;
1472}
1473
Willy Tarreau92153fc2017-12-03 19:46:19 +01001474/* processes a PRIORITY frame, and either skips it or rejects if it is
1475 * invalid. Returns > 0 on success or zero on missing data. It may return
1476 * an error in h2c. Described in RFC7540#6.3.
1477 */
1478static int h2c_handle_priority(struct h2c *h2c)
1479{
1480 int error;
1481
1482 if (h2c->dsi == 0) {
1483 error = H2_ERR_PROTOCOL_ERROR;
1484 goto conn_err;
1485 }
1486
1487 if (h2c->dfl != 5) {
1488 error = H2_ERR_FRAME_SIZE_ERROR;
1489 goto conn_err;
1490 }
1491
1492 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001493 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001494 return 0;
1495
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001496 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001497 /* 7540#5.3 : can't depend on itself */
1498 error = H2_ERR_PROTOCOL_ERROR;
1499 goto conn_err;
1500 }
1501 return 1;
1502
1503 conn_err:
1504 h2c_error(h2c, error);
1505 return 0;
1506}
1507
Willy Tarreaucd234e92017-08-18 10:59:39 +02001508/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1509 * Returns > 0 on success or zero on missing data. It may return an error in
1510 * h2c. Described in RFC7540#6.4.
1511 */
1512static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1513{
1514 int error;
1515
1516 if (h2c->dsi == 0) {
1517 error = H2_ERR_PROTOCOL_ERROR;
1518 goto conn_err;
1519 }
1520
Willy Tarreaucd234e92017-08-18 10:59:39 +02001521 if (h2c->dfl != 4) {
1522 error = H2_ERR_FRAME_SIZE_ERROR;
1523 goto conn_err;
1524 }
1525
1526 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001527 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001528 return 0;
1529
1530 /* late RST, already handled */
1531 if (h2s->st == H2_SS_CLOSED)
1532 return 1;
1533
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001534 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001535 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001536
1537 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001538 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001539 /* recv is used to force to detect CS_FL_EOS that wake()
1540 * doesn't handle in the stream-int code.
1541 */
1542 h2s->cs->data_cb->recv(h2s->cs);
1543 h2s->cs->data_cb->wake(h2s->cs);
1544 }
1545
1546 h2s->flags |= H2_SF_RST_RCVD;
1547 return 1;
1548
1549 conn_err:
1550 h2c_error(h2c, error);
1551 return 0;
1552}
1553
Willy Tarreau13278b42017-10-13 19:23:14 +02001554/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1555 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1556 * errors here are reported as connection errors since it's impossible to
1557 * recover from such errors after the compression context has been altered.
1558 */
1559static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1560{
1561 int error;
1562
1563 if (!h2c->dfl) {
1564 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1565 goto strm_err;
1566 }
1567
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001568 if (!b_size(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001569 return 0; // empty buffer
1570
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001571 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001572 return 0; // incomplete frame
1573
Willy Tarreauf2101912018-07-19 10:11:38 +02001574 if (h2c->flags & H2_CF_DEM_TOOMANY)
1575 return 0; // too many cs still present
1576
Willy Tarreau13278b42017-10-13 19:23:14 +02001577 /* now either the frame is complete or the buffer is complete */
1578 if (h2s->st != H2_SS_IDLE) {
1579 /* FIXME: stream already exists, this is only allowed for
1580 * trailers (not supported for now).
1581 */
1582 error = H2_ERR_PROTOCOL_ERROR;
1583 goto conn_err;
1584 }
1585 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1586 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1587 error = H2_ERR_PROTOCOL_ERROR;
1588 goto conn_err;
1589 }
1590
1591 h2s = h2c_stream_new(h2c, h2c->dsi);
1592 if (!h2s) {
1593 error = H2_ERR_INTERNAL_ERROR;
1594 goto conn_err;
1595 }
1596
1597 h2s->st = H2_SS_OPEN;
1598 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1599 h2s->st = H2_SS_HREM;
1600 h2s->flags |= H2_SF_ES_RCVD;
1601 }
1602
1603 /* call the upper layers to process the frame, then let the upper layer
1604 * notify the stream about any change.
1605 */
1606 h2s->cs->data_cb->recv(h2s->cs);
1607
1608 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1609 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1610 error = H2_ERR_INTERNAL_ERROR;
1611 goto conn_err;
1612 }
1613
Willy Tarreau8f650c32017-11-21 19:36:21 +01001614 if (h2c->st0 >= H2_CS_ERROR)
1615 return 0;
1616
Willy Tarreau721c9742017-11-07 11:05:42 +01001617 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001618 /* stream error : send RST_STREAM */
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 /* update the max stream ID if the request is being processed */
1623 if (h2s->id > h2c->max_id)
1624 h2c->max_id = h2s->id;
1625 }
1626
1627 return 1;
1628
1629 conn_err:
1630 h2c_error(h2c, error);
1631 return 0;
1632
1633 strm_err:
1634 if (h2s) {
1635 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001636 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001637 }
1638 else
1639 h2c_error(h2c, error);
1640 return 0;
1641}
1642
Willy Tarreau454f9052017-10-26 19:40:35 +02001643/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1644 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1645 */
1646static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1647{
1648 int error;
1649
1650 /* note that empty DATA frames are perfectly valid and sometimes used
1651 * to signal an end of stream (with the ES flag).
1652 */
1653
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001654 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001655 return 0; // empty buffer
1656
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001657 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001658 return 0; // incomplete frame
1659
1660 /* now either the frame is complete or the buffer is complete */
1661
1662 if (!h2c->dsi) {
1663 /* RFC7540#6.1 */
1664 error = H2_ERR_PROTOCOL_ERROR;
1665 goto conn_err;
1666 }
1667
1668 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1669 /* RFC7540#6.1 */
1670 error = H2_ERR_STREAM_CLOSED;
1671 goto strm_err;
1672 }
1673
Willy Tarreau454f9052017-10-26 19:40:35 +02001674 /* call the upper layers to process the frame, then let the upper layer
1675 * notify the stream about any change.
1676 */
1677 if (!h2s->cs) {
1678 error = H2_ERR_STREAM_CLOSED;
1679 goto strm_err;
1680 }
1681
1682 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001683
Willy Tarreau454f9052017-10-26 19:40:35 +02001684 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1685 /* cs has just been destroyed, we have to kill h2s. */
1686 error = H2_ERR_STREAM_CLOSED;
1687 goto strm_err;
1688 }
1689
Willy Tarreau8f650c32017-11-21 19:36:21 +01001690 if (h2c->st0 >= H2_CS_ERROR)
1691 return 0;
1692
Willy Tarreau721c9742017-11-07 11:05:42 +01001693 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001694 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001695 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001696 }
1697
1698 /* check for completion : the callee will change this to FRAME_A or
1699 * FRAME_H once done.
1700 */
1701 if (h2c->st0 == H2_CS_FRAME_P)
1702 return 0;
1703
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001704
1705 /* last frame */
1706 if (h2c->dff & H2_F_DATA_END_STREAM) {
1707 h2s->st = H2_SS_HREM;
1708 h2s->flags |= H2_SF_ES_RCVD;
1709 }
1710
Willy Tarreau454f9052017-10-26 19:40:35 +02001711 return 1;
1712
1713 conn_err:
1714 h2c_error(h2c, error);
1715 return 0;
1716
1717 strm_err:
1718 if (h2s) {
1719 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001720 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001721 }
1722 else
1723 h2c_error(h2c, error);
1724 return 0;
1725}
1726
Willy Tarreaubc933932017-10-09 16:21:43 +02001727/* process Rx frames to be demultiplexed */
1728static void h2_process_demux(struct h2c *h2c)
1729{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001730 struct h2s *h2s;
1731
Willy Tarreau081d4722017-05-16 21:51:05 +02001732 if (h2c->st0 >= H2_CS_ERROR)
1733 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001734
1735 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1736 if (h2c->st0 == H2_CS_PREFACE) {
1737 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
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 h2c->max_id = 0;
1745 h2c->st0 = H2_CS_SETTINGS1;
1746 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001747
1748 if (h2c->st0 == H2_CS_SETTINGS1) {
1749 struct h2_fh hdr;
1750
1751 /* ensure that what is pending is a valid SETTINGS frame
1752 * without an ACK.
1753 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001754 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001755 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1756 if (h2c->st0 == H2_CS_ERROR)
1757 h2c->st0 = H2_CS_ERROR2;
1758 goto fail;
1759 }
1760
1761 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1762 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1763 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1764 h2c->st0 = H2_CS_ERROR2;
1765 goto fail;
1766 }
1767
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001768 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001769 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1770 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1771 h2c->st0 = H2_CS_ERROR2;
1772 goto fail;
1773 }
1774
1775 /* that's OK, switch to FRAME_P to process it */
1776 h2c->dfl = hdr.len;
1777 h2c->dsi = hdr.sid;
1778 h2c->dft = hdr.ft;
1779 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001780 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001781 h2c->st0 = H2_CS_FRAME_P;
1782 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001783 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001784
1785 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001787 int ret = 0;
1788
1789 if (h2c->st0 >= H2_CS_ERROR)
1790 break;
1791
1792 if (h2c->st0 == H2_CS_FRAME_H) {
1793 struct h2_fh hdr;
1794
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001795 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001796 break;
1797
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001798 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001799 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1800 h2c->st0 = H2_CS_ERROR;
1801 break;
1802 }
1803
1804 h2c->dfl = hdr.len;
1805 h2c->dsi = hdr.sid;
1806 h2c->dft = hdr.ft;
1807 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001808 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001809 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001810 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001811 }
1812
1813 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001814 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001815
Willy Tarreaud7901432017-12-29 11:34:40 +01001816 if (h2c->st0 == H2_CS_FRAME_E)
1817 goto strm_err;
1818
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001819 if (h2s->st == H2_SS_IDLE &&
1820 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1821 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1822 * this state MUST be treated as a connection error
1823 */
1824 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1825 h2c->st0 = H2_CS_ERROR;
1826 break;
1827 }
1828
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001829 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1830 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1831 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1832 * this state MUST be treated as a stream error
1833 */
1834 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001835 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001836 goto strm_err;
1837 }
1838
Willy Tarreauab837502017-12-27 15:07:30 +01001839 /* Below the management of frames received in closed state is a
1840 * bit hackish because the spec makes strong differences between
1841 * streams closed by receiving RST, sending RST, and seeing ES
1842 * in both directions. In addition to this, the creation of a
1843 * new stream reusing the identifier of a closed one will be
1844 * detected here. Given that we cannot keep track of all closed
1845 * streams forever, we consider that unknown closed streams were
1846 * closed on RST received, which allows us to respond with an
1847 * RST without breaking the connection (eg: to abort a transfer).
1848 * Some frames have to be silently ignored as well.
1849 */
1850 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1851 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1852 /* #5.1.1: The identifier of a newly
1853 * established stream MUST be numerically
1854 * greater than all streams that the initiating
1855 * endpoint has opened or reserved. This
1856 * governs streams that are opened using a
1857 * HEADERS frame and streams that are reserved
1858 * using PUSH_PROMISE. An endpoint that
1859 * receives an unexpected stream identifier
1860 * MUST respond with a connection error.
1861 */
1862 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1863 goto strm_err;
1864 }
1865
1866 if (h2s->flags & H2_SF_RST_RCVD) {
1867 /* RFC7540#5.1:closed: an endpoint that
1868 * receives any frame other than PRIORITY after
1869 * receiving a RST_STREAM MUST treat that as a
1870 * stream error of type STREAM_CLOSED.
1871 *
1872 * Note that old streams fall into this category
1873 * and will lead to an RST being sent.
1874 */
1875 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1876 h2c->st0 = H2_CS_FRAME_E;
1877 goto strm_err;
1878 }
1879
1880 /* RFC7540#5.1:closed: if this state is reached as a
1881 * result of sending a RST_STREAM frame, the peer that
1882 * receives the RST_STREAM might have already sent
1883 * frames on the stream that cannot be withdrawn. An
1884 * endpoint MUST ignore frames that it receives on
1885 * closed streams after it has sent a RST_STREAM
1886 * frame. An endpoint MAY choose to limit the period
1887 * over which it ignores frames and treat frames that
1888 * arrive after this time as being in error.
1889 */
1890 if (!(h2s->flags & H2_SF_RST_SENT)) {
1891 /* RFC7540#5.1:closed: any frame other than
1892 * PRIO/WU/RST in this state MUST be treated as
1893 * a connection error
1894 */
1895 if (h2c->dft != H2_FT_RST_STREAM &&
1896 h2c->dft != H2_FT_PRIORITY &&
1897 h2c->dft != H2_FT_WINDOW_UPDATE) {
1898 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1899 goto strm_err;
1900 }
1901 }
1902 }
1903
Willy Tarreauc0da1962017-10-30 18:38:00 +01001904#if 0
1905 // problem below: it is not possible to completely ignore such
1906 // streams as we need to maintain the compression state as well
1907 // and for this we need to completely process these frames (eg:
1908 // HEADERS frames) as well as counting DATA frames to emit
1909 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1910 // This is a typical case of layer violation where the
1911 // transported contents are critical to the connection's
1912 // validity and must be ignored at the same time :-(
1913
1914 /* graceful shutdown, ignore streams whose ID is higher than
1915 * the one advertised in GOAWAY. RFC7540#6.8.
1916 */
1917 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001918 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1919 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001920 h2c->dfl -= ret;
1921 ret = h2c->dfl == 0;
1922 goto strm_err;
1923 }
1924#endif
1925
Willy Tarreau7e98c052017-10-10 15:56:59 +02001926 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001927 case H2_FT_SETTINGS:
1928 if (h2c->st0 == H2_CS_FRAME_P)
1929 ret = h2c_handle_settings(h2c);
1930
1931 if (h2c->st0 == H2_CS_FRAME_A)
1932 ret = h2c_ack_settings(h2c);
1933 break;
1934
Willy Tarreaucf68c782017-10-10 17:11:41 +02001935 case H2_FT_PING:
1936 if (h2c->st0 == H2_CS_FRAME_P)
1937 ret = h2c_handle_ping(h2c);
1938
1939 if (h2c->st0 == H2_CS_FRAME_A)
1940 ret = h2c_ack_ping(h2c);
1941 break;
1942
Willy Tarreau26f95952017-07-27 17:18:30 +02001943 case H2_FT_WINDOW_UPDATE:
1944 if (h2c->st0 == H2_CS_FRAME_P)
1945 ret = h2c_handle_window_update(h2c, h2s);
1946 break;
1947
Willy Tarreau61290ec2017-10-17 08:19:21 +02001948 case H2_FT_CONTINUATION:
1949 /* we currently don't support CONTINUATION frames since
1950 * we have nowhere to store the partial HEADERS frame.
1951 * Let's abort the stream on an INTERNAL_ERROR here.
1952 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001953 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001954 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001955 h2c->st0 = H2_CS_FRAME_E;
1956 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001957 break;
1958
Willy Tarreau13278b42017-10-13 19:23:14 +02001959 case H2_FT_HEADERS:
1960 if (h2c->st0 == H2_CS_FRAME_P)
1961 ret = h2c_frt_handle_headers(h2c, h2s);
1962 break;
1963
Willy Tarreau454f9052017-10-26 19:40:35 +02001964 case H2_FT_DATA:
1965 if (h2c->st0 == H2_CS_FRAME_P)
1966 ret = h2c_frt_handle_data(h2c, h2s);
1967
1968 if (h2c->st0 == H2_CS_FRAME_A)
1969 ret = h2c_send_strm_wu(h2c);
1970 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001971
Willy Tarreau92153fc2017-12-03 19:46:19 +01001972 case H2_FT_PRIORITY:
1973 if (h2c->st0 == H2_CS_FRAME_P)
1974 ret = h2c_handle_priority(h2c);
1975 break;
1976
Willy Tarreaucd234e92017-08-18 10:59:39 +02001977 case H2_FT_RST_STREAM:
1978 if (h2c->st0 == H2_CS_FRAME_P)
1979 ret = h2c_handle_rst_stream(h2c, h2s);
1980 break;
1981
Willy Tarreaue96b0922017-10-30 00:28:29 +01001982 case H2_FT_GOAWAY:
1983 if (h2c->st0 == H2_CS_FRAME_P)
1984 ret = h2c_handle_goaway(h2c);
1985 break;
1986
Willy Tarreau1c661982017-10-30 13:52:01 +01001987 case H2_FT_PUSH_PROMISE:
1988 /* not permitted here, RFC7540#5.1 */
1989 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001990 break;
1991
1992 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001993 default:
1994 /* drop frames that we ignore. They may be larger than
1995 * the buffer so we drain all of their contents until
1996 * we reach the end.
1997 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001998 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1999 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002000 h2c->dfl -= ret;
2001 ret = h2c->dfl == 0;
2002 }
2003
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002004 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002005 /* We may have to send an RST if not done yet */
2006 if (h2s->st == H2_SS_ERROR)
2007 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002008
Willy Tarreaua20a5192017-12-27 11:02:06 +01002009 if (h2c->st0 == H2_CS_FRAME_E)
2010 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002011
Willy Tarreau7e98c052017-10-10 15:56:59 +02002012 /* error or missing data condition met above ? */
2013 if (ret <= 0)
2014 break;
2015
2016 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002017 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002018 h2c->st0 = H2_CS_FRAME_H;
2019 }
2020 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002021
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002022 if (h2c->rcvd_c > 0 &&
2023 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2024 h2c_send_conn_wu(h2c);
2025
Willy Tarreau52eed752017-09-22 15:05:09 +02002026 fail:
2027 /* we can go here on missing data, blocked response or error */
2028 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002029}
2030
2031/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2032 * the end.
2033 */
2034static int h2_process_mux(struct h2c *h2c)
2035{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002036 struct h2s *h2s, *h2s_back;
2037
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002038 /* start by sending possibly pending window updates */
2039 if (h2c->rcvd_c > 0 &&
2040 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2041 h2c_send_conn_wu(h2c) < 0)
2042 goto fail;
2043
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002044 /* First we always process the flow control list because the streams
2045 * waiting there were already elected for immediate emission but were
2046 * blocked just on this.
2047 */
2048
2049 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2050 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2051 h2c->st0 >= H2_CS_ERROR)
2052 break;
2053
2054 /* In theory it's possible that h2s->cs == NULL here :
2055 * - client sends crap that causes a parse error
2056 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2057 * - RST_STREAM cannot be emitted because mux is busy/full
2058 * - stream gets notified, detaches and quits
2059 * - mux buffer gets ready and wakes pending streams up
2060 * - bam!
2061 */
2062 h2s->flags &= ~H2_SF_BLK_ANY;
2063
2064 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002065 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002066 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002067 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002068 }
2069
2070 /* depending on callee's blocking reasons, we may queue in send
2071 * list or completely dequeue.
2072 */
2073 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2074 if (h2s->flags & H2_SF_BLK_ANY) {
2075 LIST_DEL(&h2s->list);
2076 LIST_ADDQ(&h2c->send_list, &h2s->list);
2077 }
2078 else {
2079 LIST_DEL(&h2s->list);
2080 LIST_INIT(&h2s->list);
2081 if (h2s->cs)
2082 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002083 else {
2084 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002085 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002086 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002087 }
2088 }
2089 }
2090
2091 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2092 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2093 break;
2094
2095 /* In theory it's possible that h2s->cs == NULL here :
2096 * - client sends crap that causes a parse error
2097 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2098 * - RST_STREAM cannot be emitted because mux is busy/full
2099 * - stream gets notified, detaches and quits
2100 * - mux buffer gets ready and wakes pending streams up
2101 * - bam!
2102 */
2103 h2s->flags &= ~H2_SF_BLK_ANY;
2104
2105 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002106 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002107 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002108 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002109 }
2110 /* depending on callee's blocking reasons, we may queue in fctl
2111 * list or completely dequeue.
2112 */
2113 if (h2s->flags & H2_SF_BLK_MFCTL) {
2114 /* stream hit the connection's flow control */
2115 LIST_DEL(&h2s->list);
2116 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2117 }
2118 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2119 LIST_DEL(&h2s->list);
2120 LIST_INIT(&h2s->list);
2121 if (h2s->cs)
2122 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002123 else {
2124 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002125 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002126 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002127 }
2128 }
2129
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002130 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002131 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002132 if (h2c->st0 == H2_CS_ERROR) {
2133 if (h2c->max_id >= 0) {
2134 h2c_send_goaway_error(h2c, NULL);
2135 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2136 return 0;
2137 }
2138
2139 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2140 }
2141 return 1;
2142 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002143 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002144}
2145
Willy Tarreau71681172017-10-23 14:39:06 +02002146
Willy Tarreau62f52692017-10-08 23:01:42 +02002147/*********************************************************/
2148/* functions below are I/O callbacks from the connection */
2149/*********************************************************/
2150
2151/* callback called on recv event by the connection handler */
2152static void h2_recv(struct connection *conn)
2153{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002154 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002155 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002156 int max;
2157
Willy Tarreau315d8072017-12-10 22:17:57 +01002158 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002159 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002160
Willy Tarreau44e973f2018-03-01 17:49:30 +01002161 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002162 if (!buf) {
2163 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002164 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002165 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002166
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002167 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002168 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002169 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002170
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002171 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002172 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002173 return;
2174 }
2175
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002176 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002177 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002178 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002179}
2180
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002181/* Try to send data if possible */
2182static struct task *h2_send(struct task *t, void *ctx, unsigned short state)
Willy Tarreau62f52692017-10-08 23:01:42 +02002183{
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002184 struct connection *conn = ctx;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002185 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002186 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002187
2188 if (conn->flags & CO_FL_ERROR)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002189 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002190
2191 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2192 /* a handshake was requested */
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002193 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002194 }
2195
Willy Tarreaubc933932017-10-09 16:21:43 +02002196 /* This loop is quite simple : it tries to fill as much as it can from
2197 * pending streams into the existing buffer until it's reportedly full
2198 * or the end of send requests is reached. Then it tries to send this
2199 * buffer's contents out, marks it not full if at least one byte could
2200 * be sent, and tries again.
2201 *
2202 * The snd_buf() function normally takes a "flags" argument which may
2203 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2204 * data immediately comes and CO_SFL_STREAMER to indicate that the
2205 * connection is streaming lots of data (used to increase TLS record
2206 * size at the expense of latency). The former can be sent any time
2207 * there's a buffer full flag, as it indicates at least one stream
2208 * attempted to send and failed so there are pending data. An
2209 * alternative would be to set it as long as there's an active stream
2210 * but that would be problematic for ACKs until we have an absolute
2211 * guarantee that all waiters have at least one byte to send. The
2212 * latter should possibly not be set for now.
2213 */
2214
2215 done = 0;
2216 while (!done) {
2217 unsigned int flags = 0;
2218
2219 /* fill as much as we can into the current buffer */
2220 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2221 done = h2_process_mux(h2c);
2222
2223 if (conn->flags & CO_FL_ERROR)
2224 break;
2225
2226 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2227 flags |= CO_SFL_MSG_MORE;
2228
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002229 if (b_data(&h2c->mbuf)) {
2230 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002231 if (!ret)
2232 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002233 b_del(&h2c->mbuf, ret);
2234 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002235 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002236
2237 /* wrote at least one byte, the buffer is not full anymore */
2238 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2239 }
2240
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241 if (conn->flags & CO_FL_SOCK_WR_SH) {
2242 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002243 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002244 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002245 /* We're not full anymore, so we can wake any task that are waiting
2246 * for us.
2247 */
2248 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2249 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2250 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2251 struct wait_list *, list);
2252 LIST_DEL(&sw->list);
2253 LIST_INIT(&sw->list);
2254 tasklet_wakeup(sw->task);
2255 }
2256
2257 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002258 /* We're done, no more to send */
2259 if (!b_data(&h2c->mbuf))
2260 return NULL;
2261schedule:
2262 if (LIST_ISEMPTY(&h2c->wait_list.list))
2263 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2264 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002265}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002266
Willy Tarreau62f52692017-10-08 23:01:42 +02002267/* callback called on any event by the connection handler.
2268 * It applies changes and returns zero, or < 0 if it wants immediate
2269 * destruction of the connection (which normally doesn not happen in h2).
2270 */
2271static int h2_wake(struct connection *conn)
2272{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002273 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002274 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002275
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002276 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002277 h2_process_demux(h2c);
2278
2279 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002280 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002281
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002282 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002283 h2c->flags &= ~H2_CF_DEM_DFULL;
2284 }
2285
Willy Tarreau8ec14062017-12-30 18:08:13 +01002286 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2287 /* frontend is stopping, reload likely in progress, let's try
2288 * to announce a graceful shutdown if not yet done. We don't
2289 * care if it fails, it will be tried again later.
2290 */
2291 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2292 if (h2c->last_sid < 0)
2293 h2c->last_sid = (1U << 31) - 1;
2294 h2c_send_goaway_error(h2c, NULL);
2295 }
2296 }
2297
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002298 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002299 * If we received early data, and the handshake is done, wake
2300 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002301 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002302 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2303 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2304 struct eb32_node *node;
2305 struct h2s *h2s;
2306
2307 h2c->flags |= H2_CF_WAIT_FOR_HS;
2308 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2309
2310 while (node) {
2311 h2s = container_of(node, struct h2s, by_id);
2312 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2313 h2s->cs->data_cb->wake(h2s->cs);
2314 node = eb32_next(node);
2315 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002316 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002317
Willy Tarreau26bd7612017-10-09 16:47:04 +02002318 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002319 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2320 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2321 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002322 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002323
2324 if (eb_is_empty(&h2c->streams_by_id)) {
2325 /* no more stream, kill the connection now */
2326 h2_release(conn);
2327 return -1;
2328 }
2329 else {
2330 /* some streams still there, we need to signal them all and
2331 * wait for their departure.
2332 */
2333 __conn_xprt_stop_recv(conn);
2334 __conn_xprt_stop_send(conn);
2335 return 0;
2336 }
2337 }
2338
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002339 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002340 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002341
2342 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002343 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002344 __conn_xprt_stop_recv(conn);
2345 }
2346 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002347 __conn_xprt_want_recv(conn);
2348 }
2349
2350 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002351 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2352 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002353 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002354 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2355 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002356 __conn_xprt_want_send(conn);
2357 }
2358 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002359 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002360 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002361 }
2362
Willy Tarreau3f133572017-10-31 19:21:06 +01002363 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002364 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002365 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002366 task_queue(h2c->task);
2367 }
2368 else
2369 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002370 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002371
2372 h2_send(NULL, conn, 0);
Willy Tarreau62f52692017-10-08 23:01:42 +02002373 return 0;
2374}
2375
Willy Tarreauea392822017-10-31 10:02:25 +01002376/* Connection timeout management. The principle is that if there's no receipt
2377 * nor sending for a certain amount of time, the connection is closed. If the
2378 * MUX buffer still has lying data or is not allocatable, the connection is
2379 * immediately killed. If it's allocatable and empty, we attempt to send a
2380 * GOAWAY frame.
2381 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002382static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002383{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002384 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002385 int expired = tick_is_expired(t->expire, now_ms);
2386
Willy Tarreau0975f112018-03-29 15:22:59 +02002387 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002388 return t;
2389
Willy Tarreau0975f112018-03-29 15:22:59 +02002390 task_delete(t);
2391 task_free(t);
2392
2393 if (!h2c) {
2394 /* resources were already deleted */
2395 return NULL;
2396 }
2397
2398 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002399 h2c_error(h2c, H2_ERR_NO_ERROR);
2400 h2_wake_some_streams(h2c, 0, 0);
2401
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002402 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002403 /* don't even try to send a GOAWAY, the buffer is stuck */
2404 h2c->flags |= H2_CF_GOAWAY_FAILED;
2405 }
2406
2407 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002408 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002409 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2410 h2c->flags |= H2_CF_GOAWAY_FAILED;
2411
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002412 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2413 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002414 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002415 b_del(&h2c->mbuf, ret);
2416 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002417 }
2418 }
Willy Tarreauea392822017-10-31 10:02:25 +01002419
Willy Tarreau0975f112018-03-29 15:22:59 +02002420 /* either we can release everything now or it will be done later once
2421 * the last stream closes.
2422 */
2423 if (eb_is_empty(&h2c->streams_by_id))
2424 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002425
Willy Tarreauea392822017-10-31 10:02:25 +01002426 return NULL;
2427}
2428
2429
Willy Tarreau62f52692017-10-08 23:01:42 +02002430/*******************************************/
2431/* functions below are used by the streams */
2432/*******************************************/
2433
2434/*
2435 * Attach a new stream to a connection
2436 * (Used for outgoing connections)
2437 */
2438static struct conn_stream *h2_attach(struct connection *conn)
2439{
2440 return NULL;
2441}
2442
2443/* callback used to update the mux's polling flags after changing a cs' status.
2444 * The caller (cs_update_mux_polling) will take care of propagating any changes
2445 * to the transport layer.
2446 */
2447static void h2_update_poll(struct conn_stream *cs)
2448{
Willy Tarreau1d393222017-10-17 10:26:19 +02002449 struct h2s *h2s = cs->ctx;
2450
2451 if (!h2s)
2452 return;
2453
Willy Tarreaud7739c82017-10-30 15:38:23 +01002454 /* we may unblock a blocked read */
2455
Willy Tarreau315d8072017-12-10 22:17:57 +01002456 if (cs->flags & CS_FL_DATA_RD_ENA) {
2457 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002458 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002459 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002460 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002461 conn_xprt_want_send(cs->conn);
2462 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002463 }
2464
Willy Tarreau1d393222017-10-17 10:26:19 +02002465 /* Note: the stream and stream-int code doesn't allow us to perform a
2466 * synchronous send() here unfortunately, because this code is called
2467 * as si_update() from the process_stream() context. This means that
2468 * we have to queue the current cs and defer its processing after the
2469 * connection's cs list is processed anyway.
2470 */
2471
2472 if (cs->flags & CS_FL_DATA_WR_ENA) {
2473 if (LIST_ISEMPTY(&h2s->list)) {
2474 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002475 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002476 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2477 conn_xprt_want_send(cs->conn);
2478 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2479 }
2480 }
2481 else if (!LIST_ISEMPTY(&h2s->list)) {
2482 LIST_DEL(&h2s->list);
2483 LIST_INIT(&h2s->list);
2484 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2485 }
2486
2487 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002488 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002489 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002490}
2491
2492/*
2493 * Detach the stream from the connection and possibly release the connection.
2494 */
2495static void h2_detach(struct conn_stream *cs)
2496{
Willy Tarreau60935142017-10-16 18:11:19 +02002497 struct h2s *h2s = cs->ctx;
2498 struct h2c *h2c;
2499
2500 cs->ctx = NULL;
2501 if (!h2s)
2502 return;
2503
2504 h2c = h2s->h2c;
2505 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002506 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002507 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2508 !h2_has_too_many_cs(h2c)) {
2509 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2510 if (h2_recv_allowed(h2c)) {
2511 __conn_xprt_want_recv(h2c->conn);
2512 conn_xprt_want_send(h2c->conn);
2513 }
2514 }
Willy Tarreau60935142017-10-16 18:11:19 +02002515
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002516 /* this stream may be blocked waiting for some data to leave (possibly
2517 * an ES or RST frame), so orphan it in this case.
2518 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002519 if (!(cs->conn->flags & CO_FL_ERROR) &&
2520 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002521 return;
2522
Willy Tarreau45f752e2017-10-30 15:44:59 +01002523 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2524 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2525 /* unblock the connection if it was blocked on this
2526 * stream.
2527 */
2528 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2529 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2530 conn_xprt_want_recv(cs->conn);
2531 conn_xprt_want_send(cs->conn);
2532 }
2533
Willy Tarreau71049cc2018-03-28 13:56:39 +02002534 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002535
Willy Tarreaue323f342018-03-28 13:51:45 +02002536 /* We don't want to close right now unless we're removing the
2537 * last stream, and either the connection is in error, or it
2538 * reached the ID already specified in a GOAWAY frame received
2539 * or sent (as seen by last_sid >= 0).
2540 */
2541 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2542 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002543 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002544 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002545 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002546 (conn_xprt_read0_pending(h2c->conn) ||
2547 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2548 /* no more stream will come, kill it now */
2549 h2_release(h2c->conn);
2550 }
2551 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002552 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002553 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2554 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002555 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002556 else
2557 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002558 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002559}
2560
2561static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2562{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002563 struct h2s *h2s = cs->ctx;
2564
2565 if (!mode)
2566 return;
2567
Willy Tarreau721c9742017-11-07 11:05:42 +01002568 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002569 return;
2570
Willy 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.
2575 */
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)) &&
2582 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002583 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002584
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002585 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002586 conn_xprt_want_send(cs->conn);
2587
Willy Tarreau00dd0782018-03-01 16:31:34 +01002588 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002589
2590 add_to_list:
2591 if (LIST_ISEMPTY(&h2s->list)) {
2592 if (h2s->flags & H2_SF_BLK_MFCTL)
2593 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2594 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2595 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2596 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002597}
2598
2599static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2600{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002601 struct h2s *h2s = cs->ctx;
2602
Willy Tarreau721c9742017-11-07 11:05:42 +01002603 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002604 return;
2605
Willy Tarreau67434202017-11-06 20:20:51 +01002606 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002607 /* we can cleanly close using an empty data frame only after headers */
2608
2609 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2610 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002611 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002612
2613 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002614 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002615 else
2616 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002617 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002618 /* if no outgoing data was seen on this stream, it means it was
2619 * closed with a "tcp-request content" rule that is normally
2620 * used to kill the connection ASAP (eg: limit abuse). In this
2621 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002622 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002623 if (!(h2s->flags & H2_SF_RST_SENT) &&
2624 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002625 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002626
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002627 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2628 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002629 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002630 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002631
Willy Tarreau00dd0782018-03-01 16:31:34 +01002632 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002633 }
2634
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002635 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002636 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002637
2638 add_to_list:
2639 if (LIST_ISEMPTY(&h2s->list)) {
2640 if (h2s->flags & H2_SF_BLK_MFCTL)
2641 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2642 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2643 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2644 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002645}
2646
Willy Tarreau13278b42017-10-13 19:23:14 +02002647/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2648 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2649 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002650 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002651 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002652static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02002653{
2654 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002656 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002657 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002658 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002659 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002660 int flen = h2c->dfl;
2661 int outlen = 0;
2662 int wrap;
2663 int try;
2664
2665 if (!h2c->dfl) {
2666 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002667 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002668 return 0;
2669 }
2670
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002671 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002672 return 0; // incomplete input frame
2673
Willy Tarreau13278b42017-10-13 19:23:14 +02002674 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002675 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002676 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002677 copy = alloc_trash_chunk();
2678 if (!copy) {
2679 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2680 goto fail;
2681 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2683 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2684 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002685 }
2686
2687 /* The padlen is the first byte before data, and the padding appears
2688 * after data. padlen+data+padding are included in flen.
2689 */
2690 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002691 h2c->dpl = *hdrs;
2692 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002693 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2694 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002695 return 0;
2696 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002697 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002698 hdrs += 1; // skip Pad Length
2699 }
2700
2701 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2702 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002703 if (read_n32(hdrs) == h2s->id) {
2704 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2705 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2706 return 0;//goto fail_stream;
2707 }
2708
Willy Tarreau13278b42017-10-13 19:23:14 +02002709 hdrs += 5; // stream dep = 4, weight = 1
2710 flen -= 5;
2711 }
2712
2713 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2714 * don't support this for now and can't even decompress so we have to
2715 * break the connection.
2716 */
2717 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2718 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002719 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002720 }
2721
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002722 /* we can't retry a failed decompression operation so we must be very
2723 * careful not to take any risks. In practice the output buffer is
2724 * always empty except maybe for trailers, so these operations almost
2725 * never happen.
2726 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002727 if (flags & CO_RFL_BUF_WET) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002728 /* need to let the output buffer flush and
2729 * mark the buffer for later wake up.
2730 */
2731 goto fail;
2732 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002733
Willy Tarreauaa7af722018-07-12 10:33:12 +02002734 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002735 /* it doesn't fit and the buffer is fragmented,
2736 * so let's defragment it and try again.
2737 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002738 b_slow_realign(buf, trash.area, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002739 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002740
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002741 try = b_contig_space(buf);
2742 if (!try)
2743 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002744
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002745 if (try > count)
2746 try = count;
2747
2748 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2749 sizeof(list)/sizeof(list[0]), tmp);
2750 if (outlen < 0) {
2751 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2752 goto fail;
2753 }
2754
2755 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002756 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02002757 outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002758
2759 if (outlen < 0) {
2760 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2761 goto fail;
2762 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002763
Willy Tarreau174b06a2018-04-25 18:13:58 +02002764 if (msgf & H2_MSGF_BODY) {
2765 /* a payload is present */
2766 if (msgf & H2_MSGF_BODY_CL)
2767 h2s->flags |= H2_SF_DATA_CLEN;
2768 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2769 h2s->flags |= H2_SF_DATA_CHNK;
2770 }
2771
Willy Tarreau13278b42017-10-13 19:23:14 +02002772 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002773 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002774 h2c->st0 = H2_CS_FRAME_H;
Olivier Houchardacd14032018-06-28 18:17:23 +02002775 b_add(buf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002776
2777 /* don't send it before returning data!
2778 * FIXME: should we instead try to send it much later, after the
2779 * response ? This would require that we keep a copy of it in h2s.
2780 */
2781 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2782 h2s->cs->flags |= CS_FL_EOS;
2783 h2s->flags |= H2_SF_ES_RCVD;
2784 }
2785
Willy Tarreau68dd9852017-07-03 14:44:26 +02002786 leave:
2787 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002788 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002789 fail:
2790 outlen = 0;
2791 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002792}
2793
Willy Tarreau454f9052017-10-26 19:40:35 +02002794/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2795 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2796 * in use, a new chunk is emitted for each frame. This is supposed to fit
2797 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2798 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2799 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2800 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002801 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2802 * checked to know if some data remain pending (an empty DATA frame can return
2803 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2804 * connection errors in h2c->errcode. The caller must already have checked the
2805 * frame header and ensured that the frame was complete or the buffer full. It
2806 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002807 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002808static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau454f9052017-10-26 19:40:35 +02002809{
2810 struct h2c *h2c = h2s->h2c;
2811 int block1, block2;
2812 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002813 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002814
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002815 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002816 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002817
2818 /* The padlen is the first byte before data, and the padding appears
2819 * after data. padlen+data+padding are included in flen.
2820 */
Willy Tarreau79127812017-12-03 21:06:59 +01002821 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002822 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002823 return 0;
2824
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002825 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002826 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002827 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2828 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002829 return 0;
2830 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002831
2832 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002833 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002834 h2c->dfl--;
2835 h2c->rcvd_c++; h2c->rcvd_s++;
2836 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002837 }
2838
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002839 flen = h2c->dfl - h2c->dpl;
2840 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002841 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002842
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843 if (flen > b_data(&h2c->dbuf)) {
2844 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002845 if (!flen)
2846 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002847 }
2848
Willy Tarreaueba10f22018-04-25 20:44:22 +02002849 /* chunked-encoding requires more room */
2850 if (h2s->flags & H2_SF_DATA_CHNK) {
2851 chklen = MIN(flen, count);
2852 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2853 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2854 (chklen < 1048576) ? 4 : 8;
2855 chklen += 4; // CRLF, CRLF
2856 }
2857
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002858 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002859 if (flen + chklen > count) {
2860 if (chklen >= count)
2861 goto full;
2862 flen = count - chklen;
2863 }
2864
2865 if (h2s->flags & H2_SF_DATA_CHNK) {
2866 /* emit the chunk size */
2867 unsigned int chksz = flen;
2868 char str[10];
2869 char *beg;
2870
2871 beg = str + sizeof(str);
2872 *--beg = '\n';
2873 *--beg = '\r';
2874 do {
2875 *--beg = hextab[chksz & 0xF];
2876 } while (chksz >>= 4);
Willy Tarreau55372f62018-07-10 10:04:02 +02002877 b_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002878 }
2879
Willy Tarreau454f9052017-10-26 19:40:35 +02002880 /* Block1 is the length of the first block before the buffer wraps,
2881 * block2 is the optional second block to reach the end of the frame.
2882 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002883 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002884 if (block1 > flen)
2885 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002886 block2 = flen - block1;
2887
2888 if (block1)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002889 b_putblk(buf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002890
2891 if (block2)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002892 b_putblk(buf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002893
Willy Tarreaueba10f22018-04-25 20:44:22 +02002894 if (h2s->flags & H2_SF_DATA_CHNK) {
2895 /* emit the CRLF */
Willy Tarreau55372f62018-07-10 10:04:02 +02002896 b_putblk(buf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002897 }
2898
Willy Tarreau454f9052017-10-26 19:40:35 +02002899 /* now mark the input data as consumed (will be deleted from the buffer
2900 * by the caller when seeing FRAME_A after sending the window update).
2901 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002902 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002903 h2c->dfl -= flen;
2904 h2c->rcvd_c += flen;
2905 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2906
2907 if (h2c->dfl > h2c->dpl) {
2908 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002909 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002910 }
2911
Willy Tarreau4a28da12018-01-04 14:41:00 +01002912 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002913 /* here we're done with the frame, all the payload (except padding) was
2914 * transferred.
2915 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002916
2917 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2918 /* emit the trailing 0 CRLF CRLF */
2919 if (count < 5)
2920 goto more;
2921 chklen += 5;
Willy Tarreau55372f62018-07-10 10:04:02 +02002922 b_putblk(buf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002923 }
2924
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002925 h2c->rcvd_c += h2c->dpl;
2926 h2c->rcvd_s += h2c->dpl;
2927 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002928 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2929
2930 /* don't send it before returning data!
2931 * FIXME: should we instead try to send it much later, after the
2932 * response ? This would require that we keep a copy of it in h2s.
2933 */
Willy Tarreau79127812017-12-03 21:06:59 +01002934 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002935 h2s->cs->flags |= CS_FL_EOS;
2936 h2s->flags |= H2_SF_ES_RCVD;
2937 }
2938
Willy Tarreaueba10f22018-04-25 20:44:22 +02002939 return flen + chklen;
2940 full:
2941 flen = chklen = 0;
2942 more:
2943 h2c->flags |= H2_CF_DEM_SFULL;
2944 h2s->cs->flags |= CS_FL_RCV_MORE;
2945 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002946}
2947
Willy Tarreau62f52692017-10-08 23:01:42 +02002948/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002949 * Called from the upper layer to get more data, up to <count> bytes. The
2950 * caller is responsible for never asking for more data than what is available
2951 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002952 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002953static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02002954{
Willy Tarreau13278b42017-10-13 19:23:14 +02002955 struct h2s *h2s = cs->ctx;
2956 struct h2c *h2c = h2s->h2c;
Willy Tarreaud9cf5402018-07-18 11:29:06 +02002957 size_t ret = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002958
2959 if (h2c->st0 != H2_CS_FRAME_P)
2960 return 0; // no pre-parsed frame yet
2961
2962 if (h2c->dsi != h2s->id)
2963 return 0; // not for us
2964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 if (!b_size(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02002966 return 0; // empty buffer
2967
Willy Tarreau13278b42017-10-13 19:23:14 +02002968 switch (h2c->dft) {
2969 case H2_FT_HEADERS:
Willy Tarreau337ea572018-06-19 06:23:38 +02002970 ret = h2_frt_decode_headers(h2s, buf, count, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002971 break;
2972
Willy Tarreau454f9052017-10-26 19:40:35 +02002973 case H2_FT_DATA:
Willy Tarreau337ea572018-06-19 06:23:38 +02002974 ret = h2_frt_transfer_data(h2s, buf, count, flags);
Willy Tarreau454f9052017-10-26 19:40:35 +02002975 break;
2976
Willy Tarreau13278b42017-10-13 19:23:14 +02002977 default:
2978 ret = 0;
2979 }
2980 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002981}
2982
Willy Tarreau5dd17352018-06-14 13:33:30 +02002983/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2984 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2985 * number of bytes sent. The caller must check the stream's status to detect
2986 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002987 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002988static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002989{
2990 struct http_hdr list[MAX_HTTP_HDR];
2991 struct h2c *h2c = h2s->h2c;
2992 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002993 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002994 int es_now = 0;
2995 int ret = 0;
2996 int hdr;
2997
2998 if (h2c_mux_busy(h2c, h2s)) {
2999 h2s->flags |= H2_SF_BLK_MBUSY;
3000 return 0;
3001 }
3002
Willy Tarreau44e973f2018-03-01 17:49:30 +01003003 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003004 h2c->flags |= H2_CF_MUX_MALLOC;
3005 h2s->flags |= H2_SF_BLK_MROOM;
3006 return 0;
3007 }
3008
3009 /* First, try to parse the H1 response and index it into <list>.
3010 * NOTE! Since it comes from haproxy, we *know* that a response header
3011 * block does not wrap and we can safely read it this way without
3012 * having to realign the buffer.
3013 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003014 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003015 list, sizeof(list)/sizeof(list[0]), h1m);
3016 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003017 /* incomplete or invalid response, this is abnormal coming from
3018 * haproxy and may only result in a bad errorfile or bad Lua code
3019 * so that won't be fixed, raise an error now.
3020 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003021 * FIXME: we should instead add the ability to only return a
3022 * 502 bad gateway. But in theory this is not supposed to
3023 * happen.
3024 */
3025 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3026 ret = 0;
3027 goto end;
3028 }
3029
3030 chunk_reset(&outbuf);
3031
3032 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003033 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003035 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003036
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003037 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003038 break;
3039 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003041 }
3042
3043 if (outbuf.size < 9) {
3044 h2c->flags |= H2_CF_MUX_MFULL;
3045 h2s->flags |= H2_SF_BLK_MROOM;
3046 ret = 0;
3047 goto end;
3048 }
3049
3050 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003051 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3052 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3053 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003054
3055 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003056 if (outbuf.data < outbuf.size && h1m->status == 200)
3057 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3058 else if (outbuf.data < outbuf.size && h1m->status == 304)
3059 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003060 else if (unlikely(list[0].v.len != 3)) {
3061 /* this is an unparsable response */
3062 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3063 ret = 0;
3064 goto end;
3065 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003066 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003067 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003068 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3069 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3070 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3071 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3072 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003073 }
3074 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003075 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003076 goto realign_again;
3077
3078 h2c->flags |= H2_CF_MUX_MFULL;
3079 h2s->flags |= H2_SF_BLK_MROOM;
3080 ret = 0;
3081 goto end;
3082 }
3083
3084 /* encode all headers, stop at empty name */
3085 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003086 /* these ones do not exist in H2 and must be dropped. */
3087 if (isteq(list[hdr].n, ist("connection")) ||
3088 isteq(list[hdr].n, ist("proxy-connection")) ||
3089 isteq(list[hdr].n, ist("keep-alive")) ||
3090 isteq(list[hdr].n, ist("upgrade")) ||
3091 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003092 continue;
3093
3094 if (isteq(list[hdr].n, ist("")))
3095 break; // end
3096
3097 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3098 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003099 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100 goto realign_again;
3101
3102 h2c->flags |= H2_CF_MUX_MFULL;
3103 h2s->flags |= H2_SF_BLK_MROOM;
3104 ret = 0;
3105 goto end;
3106 }
3107 }
3108
3109 /* we may need to add END_STREAM */
3110 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3111 es_now = 1;
3112
3113 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003114 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003115
3116 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003117 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003118
3119 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003120 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003121
3122 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003123 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003124 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003125
3126 /* for now we don't implemented CONTINUATION, so we wait for a
3127 * body or directly end in TRL2.
3128 */
3129 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003130 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003131 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003132
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003133 h1m->state = HTTP_MSG_DONE;
3134 h2s->flags |= H2_SF_ES_SENT;
3135 if (h2s->st == H2_SS_OPEN)
3136 h2s->st = H2_SS_HLOC;
3137 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003138 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003139 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003140 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003141 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003142 h1m->state = HTTP_MSG_RPBEFORE;
3143 h1m->status = 0;
3144 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003145 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003146 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003147 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003148 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003149
3150 end:
3151 //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));
3152 return ret;
3153}
3154
Willy Tarreau5dd17352018-06-14 13:33:30 +02003155/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3156 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3157 * the number of bytes sent. The caller must check the stream's status to
3158 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003159 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003160static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003161{
3162 struct h2c *h2c = h2s->h2c;
3163 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003164 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003165 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003166 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003167 int es_now = 0;
3168 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003169 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003170 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003171
3172 if (h2c_mux_busy(h2c, h2s)) {
3173 h2s->flags |= H2_SF_BLK_MBUSY;
3174 goto end;
3175 }
3176
Willy Tarreau44e973f2018-03-01 17:49:30 +01003177 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003178 h2c->flags |= H2_CF_MUX_MALLOC;
3179 h2s->flags |= H2_SF_BLK_MROOM;
3180 goto end;
3181 }
3182
3183 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003184 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003185 goto end;
3186
3187 chunk_reset(&outbuf);
3188
3189 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003190 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003191 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003192 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003193
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003194 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003195 break;
3196 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003198 }
3199
3200 if (outbuf.size < 9) {
3201 h2c->flags |= H2_CF_MUX_MFULL;
3202 h2s->flags |= H2_SF_BLK_MROOM;
3203 goto end;
3204 }
3205
3206 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003207 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3208 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3209 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003210
3211 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3212 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003213 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003214 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003215 break;
3216 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003217 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003218 if ((long long)size > h1m->curr_len)
3219 size = h1m->curr_len;
3220 break;
3221 default: /* te:chunked : parse chunks */
3222 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003223 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224 if (!ret)
3225 goto end;
3226
3227 if (ret < 0) {
3228 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3229 h1m->err_pos = ret;
3230 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3231 goto end;
3232 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003233 max -= ret;
3234 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003235 total += ret;
3236 h1m->state = HTTP_MSG_CHUNK_SIZE;
3237 }
3238
3239 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3240 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003241 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003242 if (!ret)
3243 goto end;
3244
3245 if (ret < 0) {
3246 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3247 h1m->err_pos = ret;
3248 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3249 goto end;
3250 }
3251
3252 size = chunk;
3253 h1m->curr_len = chunk;
3254 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003255 max -= ret;
3256 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003257 total += ret;
3258 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3259 if (!size)
3260 goto send_empty;
3261 }
3262
3263 /* in MSG_DATA state, continue below */
3264 size = h1m->curr_len;
3265 break;
3266 }
3267
3268 /* we have in <size> the exact number of bytes we need to copy from
3269 * the H1 buffer. We need to check this against the connection's and
3270 * the stream's send windows, and to ensure that this fits in the max
3271 * frame size and in the buffer's available space minus 9 bytes (for
3272 * the frame header). The connection's flow control is applied last so
3273 * that we can use a separate list of streams which are immediately
3274 * unblocked on window opening. Note: we don't implement padding.
3275 */
3276
Willy Tarreau5dd17352018-06-14 13:33:30 +02003277 if (size > max)
3278 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003279
3280 if (size > h2s->mws)
3281 size = h2s->mws;
3282
3283 if (size <= 0) {
3284 h2s->flags |= H2_SF_BLK_SFCTL;
3285 goto end;
3286 }
3287
3288 if (h2c->mfs && size > h2c->mfs)
3289 size = h2c->mfs;
3290
3291 if (size + 9 > outbuf.size) {
3292 /* we have an opportunity for enlarging the too small
3293 * available space, let's try.
3294 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003295 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003296 goto realign_again;
3297 size = outbuf.size - 9;
3298 }
3299
3300 if (size <= 0) {
3301 h2c->flags |= H2_CF_MUX_MFULL;
3302 h2s->flags |= H2_SF_BLK_MROOM;
3303 goto end;
3304 }
3305
3306 if (size > h2c->mws)
3307 size = h2c->mws;
3308
3309 if (size <= 0) {
3310 h2s->flags |= H2_SF_BLK_MFCTL;
3311 goto end;
3312 }
3313
3314 /* copy whatever we can */
3315 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003316 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003317 if (ret == 1)
3318 len2 = 0;
3319
3320 if (!ret || len1 + len2 < size) {
3321 /* FIXME: must normally never happen */
3322 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3323 goto end;
3324 }
3325
3326 /* limit len1/len2 to size */
3327 if (len1 + len2 > size) {
3328 int sub = len1 + len2 - size;
3329
3330 if (len2 > sub)
3331 len2 -= sub;
3332 else {
3333 sub -= len2;
3334 len2 = 0;
3335 len1 -= sub;
3336 }
3337 }
3338
3339 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003340 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003342 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003343
3344 send_empty:
3345 /* we may need to add END_STREAM */
3346 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3347 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003348 *
3349 * FIXME: what we do here is not correct because we send end_stream
3350 * before knowing if we'll have to send a HEADERS frame for the
3351 * trailers. More importantly we're not consuming the trailing CRLF
3352 * after the end of trailers, so it will be left to the caller to
3353 * eat it. The right way to do it would be to measure trailers here
3354 * and to send ES only if there are no trailers.
3355 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003356 */
3357 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3358 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3359 es_now = 1;
3360
3361 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363
3364 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003365 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003366
3367 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003368 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369
3370 /* consume incoming H1 response */
3371 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003372 max -= size;
3373 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003374 total += size;
3375 h1m->curr_len -= size;
3376 h2s->mws -= size;
3377 h2c->mws -= size;
3378
3379 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3380 h1m->state = HTTP_MSG_CHUNK_CRLF;
3381 goto new_frame;
3382 }
3383 }
3384
3385 if (es_now) {
3386 if (h2s->st == H2_SS_OPEN)
3387 h2s->st = H2_SS_HLOC;
3388 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003389 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003390
Willy Tarreau35a62702018-02-27 15:37:25 +01003391 if (!(h1m->flags & H1_MF_CHNK)) {
3392 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003393 total += max;
3394 ofs += max;
3395 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003396
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003397 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003398 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003399
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003400 h2s->flags |= H2_SF_ES_SENT;
3401 }
3402
3403 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003404 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405 return total;
3406}
3407
Olivier Houchard6ff20392018-07-17 18:46:31 +02003408/* Called from the upper layer, to subscribe to events, such as being able to send */
3409static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3410{
3411 struct wait_list *sw;
3412 struct h2s *h2s = cs->ctx;
3413
3414 switch (event_type) {
3415 case SUB_CAN_SEND:
3416 sw = param;
3417 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3418 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3419 return 0;
3420 default:
3421 break;
3422 }
3423 return -1;
3424
3425
3426}
3427
Willy Tarreau62f52692017-10-08 23:01:42 +02003428/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003429static size_t h2_snd_buf(struct conn_stream *cs, const struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003430{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003431 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003432 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003433 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003434
Willy Tarreau0bad0432018-06-14 16:54:01 +02003435 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003436 h2s->flags |= H2_SF_OUTGOING_DATA;
3437
Willy Tarreau0bad0432018-06-14 16:54:01 +02003438 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003439 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003440 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003441 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003442 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003443 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003444 }
3445 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3446 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003447 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003448
Willy Tarreau5dd17352018-06-14 13:33:30 +02003449 if (unlikely((int)ret <= 0)) {
3450 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003451 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3452 break;
3453 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003454 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003455 total += count;
3456 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003457 h2s->res.state = HTTP_MSG_DONE;
3458 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003459 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003460 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003461 cs->flags |= CS_FL_ERROR;
3462 break;
3463 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003464
3465 total += ret;
3466 count -= ret;
3467
3468 if (h2s->st >= H2_SS_ERROR)
3469 break;
3470
3471 if (h2s->flags & H2_SF_BLK_ANY)
3472 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003473 }
3474
Willy Tarreau00610962018-07-19 10:58:28 +02003475 if (h2s->st >= H2_SS_ERROR) {
3476 /* trim any possibly pending data after we close (extra CR-LF,
3477 * unprocessed trailers, abnormal extra data, ...)
3478 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003479 total += count;
3480 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003481 }
3482
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003483 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003484 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003485 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003486 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003487 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003488 }
3489
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003490 if (h2s->flags & H2_SF_BLK_SFCTL) {
3491 /* stream flow control, quit the list */
3492 LIST_DEL(&h2s->list);
3493 LIST_INIT(&h2s->list);
3494 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003495 else if (LIST_ISEMPTY(&h2s->list)) {
3496 if (h2s->flags & H2_SF_BLK_MFCTL)
3497 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003498 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003499
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003500 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003501}
3502
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003503/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003504static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003505{
3506 struct h2c *h2c = conn->mux_ctx;
3507 struct h2s *h2s;
3508 struct eb32_node *node;
3509 int fctl_cnt = 0;
3510 int send_cnt = 0;
3511 int tree_cnt = 0;
3512 int orph_cnt = 0;
3513
3514 if (!h2c)
3515 return;
3516
3517 list_for_each_entry(h2s, &h2c->fctl_list, list)
3518 fctl_cnt++;
3519
3520 list_for_each_entry(h2s, &h2c->send_list, list)
3521 send_cnt++;
3522
3523 node = eb32_first(&h2c->streams_by_id);
3524 while (node) {
3525 h2s = container_of(node, struct h2s, by_id);
3526 tree_cnt++;
3527 if (!h2s->cs)
3528 orph_cnt++;
3529 node = eb32_next(node);
3530 }
3531
Willy Tarreauc65edac2018-07-19 10:54:43 +02003532 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 Tarreauc9fa0482018-07-10 17:43:27 +02003533 h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003534}
Willy Tarreau62f52692017-10-08 23:01:42 +02003535
3536/*******************************************************/
3537/* functions below are dedicated to the config parsers */
3538/*******************************************************/
3539
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003540/* config parser for global "tune.h2.header-table-size" */
3541static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3542 struct proxy *defpx, const char *file, int line,
3543 char **err)
3544{
3545 if (too_many_args(1, args, err, NULL))
3546 return -1;
3547
3548 h2_settings_header_table_size = atoi(args[1]);
3549 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3550 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3551 return -1;
3552 }
3553 return 0;
3554}
Willy Tarreau62f52692017-10-08 23:01:42 +02003555
Willy Tarreaue6baec02017-07-27 11:45:11 +02003556/* config parser for global "tune.h2.initial-window-size" */
3557static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3558 struct proxy *defpx, const char *file, int line,
3559 char **err)
3560{
3561 if (too_many_args(1, args, err, NULL))
3562 return -1;
3563
3564 h2_settings_initial_window_size = atoi(args[1]);
3565 if (h2_settings_initial_window_size < 0) {
3566 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3567 return -1;
3568 }
3569 return 0;
3570}
3571
Willy Tarreau5242ef82017-07-27 11:47:28 +02003572/* config parser for global "tune.h2.max-concurrent-streams" */
3573static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3574 struct proxy *defpx, const char *file, int line,
3575 char **err)
3576{
3577 if (too_many_args(1, args, err, NULL))
3578 return -1;
3579
3580 h2_settings_max_concurrent_streams = atoi(args[1]);
3581 if (h2_settings_max_concurrent_streams < 0) {
3582 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3583 return -1;
3584 }
3585 return 0;
3586}
3587
Willy Tarreau62f52692017-10-08 23:01:42 +02003588
3589/****************************************/
3590/* MUX initialization and instanciation */
3591/***************************************/
3592
3593/* The mux operations */
3594const struct mux_ops h2_ops = {
3595 .init = h2_init,
3596 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003597 .wake = h2_wake,
3598 .update_poll = h2_update_poll,
3599 .rcv_buf = h2_rcv_buf,
3600 .snd_buf = h2_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003601 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003602 .attach = h2_attach,
3603 .detach = h2_detach,
3604 .shutr = h2_shutr,
3605 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003606 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003607 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003608 .name = "H2",
3609};
3610
3611/* ALPN selection : this mux registers ALPN tolen "h2" */
3612static struct alpn_mux_list alpn_mux_h2 =
3613 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3614
3615/* config keyword parsers */
3616static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003617 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003618 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003619 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003620 { 0, NULL, NULL }
3621}};
3622
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003623static void __h2_deinit(void)
3624{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003625 pool_destroy(pool_head_h2s);
3626 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003627}
3628
Willy Tarreau62f52692017-10-08 23:01:42 +02003629__attribute__((constructor))
3630static void __h2_init(void)
3631{
3632 alpn_register_mux(&alpn_mux_h2);
3633 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003634 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003635 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3636 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003637}