blob: ba6bd8d8ac2139c0723fba0023ee62f930041714 [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 */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
177 struct h1m req, res; /* request and response parser state for H1 */
178 struct eb32_node by_id; /* place in h2c's streams_by_id */
179 struct list list; /* position in active/blocked lists if blocked>0 */
180 int32_t id; /* stream ID */
181 uint32_t flags; /* H2_SF_* */
182 int mws; /* mux window size for this stream */
183 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
184 enum h2_ss st;
185};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200186
Willy Tarreauc6405142017-09-21 20:23:50 +0200187/* descriptor for an h2 frame header */
188struct h2_fh {
189 uint32_t len; /* length, host order, 24 bits */
190 uint32_t sid; /* stream id, host order, 31 bits */
191 uint8_t ft; /* frame type */
192 uint8_t ff; /* frame flags */
193};
194
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200195/* a few settings from the global section */
196static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200197static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200198static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200199
Willy Tarreau2a856182017-05-16 15:20:39 +0200200/* a dmumy closed stream */
201static const struct h2s *h2_closed_stream = &(const struct h2s){
202 .cs = NULL,
203 .h2c = NULL,
204 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100205 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100206 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200207 .id = 0,
208};
209
210/* and a dummy idle stream for use with any unannounced stream */
211static const struct h2s *h2_idle_stream = &(const struct h2s){
212 .cs = NULL,
213 .h2c = NULL,
214 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100215 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200216 .id = 0,
217};
218
Olivier Houchard9f6af332018-05-25 14:04:04 +0200219static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200220
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200221/*****************************************************/
222/* functions below are for dynamic buffer management */
223/*****************************************************/
224
Willy Tarreau315d8072017-12-10 22:17:57 +0100225/* indicates whether or not the we may call the h2_recv() function to attempt
226 * to receive data into the buffer and/or demux pending data. The condition is
227 * a bit complex due to some API limits for now. The rules are the following :
228 * - if an error or a shutdown was detected on the connection and the buffer
229 * is empty, we must not attempt to receive
230 * - if the demux buf failed to be allocated, we must not try to receive and
231 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100232 * - if no flag indicates a blocking condition, we may attempt to receive,
233 * regardless of whether the demux buffer is full or not, so that only
234 * de demux part decides whether or not to block. This is needed because
235 * the connection API indeed prevents us from re-enabling receipt that is
236 * already enabled in a polled state, so we must always immediately stop
237 * as soon as the demux can't proceed so as never to hit an end of read
238 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100239 * - otherwise must may not attempt
240 */
241static inline int h2_recv_allowed(const struct h2c *h2c)
242{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200243 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100244 (h2c->st0 >= H2_CS_ERROR ||
245 h2c->conn->flags & CO_FL_ERROR ||
246 conn_xprt_read0_pending(h2c->conn)))
247 return 0;
248
249 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100250 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100251 return 1;
252
253 return 0;
254}
255
Willy Tarreauf2101912018-07-19 10:11:38 +0200256/* returns true if the connection has too many conn_streams attached */
257static inline int h2_has_too_many_cs(const struct h2c *h2c)
258{
259 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
260}
261
Willy Tarreau44e973f2018-03-01 17:49:30 +0100262/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
263 * flags are used to figure what buffer was requested. It returns 1 if the
264 * allocation succeeds, in which case the connection is woken up, or 0 if it's
265 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200266 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100267static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200268{
269 struct h2c *h2c = target;
270
Willy Tarreau44e973f2018-03-01 17:49:30 +0100271 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200272 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100273 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200274 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200275 return 1;
276 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
279 h2c->flags &= ~H2_CF_MUX_MALLOC;
280 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
281 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200282
283 if (h2c->flags & H2_CF_DEM_MROOM) {
284 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100285 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200286 conn_xprt_want_recv(h2c->conn);
287 }
Willy Tarreau14398122017-09-22 14:26:04 +0200288 return 1;
289 }
290 return 0;
291}
292
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200293static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200294{
295 struct buffer *buf = NULL;
296
Willy Tarreau44e973f2018-03-01 17:49:30 +0100297 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
298 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
299 h2c->buf_wait.target = h2c;
300 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100301 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100302 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100303 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200304 __conn_xprt_stop_recv(h2c->conn);
305 }
306 return buf;
307}
308
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200309static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200310{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200311 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100312 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200313 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200314 }
315}
316
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200317
Willy Tarreau62f52692017-10-08 23:01:42 +0200318/*****************************************************************/
319/* functions below are dedicated to the mux setup and management */
320/*****************************************************************/
321
Willy Tarreau32218eb2017-09-22 08:07:25 +0200322/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
323static int h2c_frt_init(struct connection *conn)
324{
325 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100326 struct task *t = NULL;
327 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200328
Willy Tarreaubafbe012017-11-24 17:34:44 +0100329 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200330 if (!h2c)
331 goto fail;
332
Willy Tarreau3f133572017-10-31 19:21:06 +0100333
Willy Tarreau599391a2017-11-24 10:16:00 +0100334 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
335 if (tick_isset(sess->fe->timeout.clientfin))
336 h2c->shut_timeout = sess->fe->timeout.clientfin;
337
Willy Tarreau33400292017-11-05 11:23:40 +0100338 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100339 if (tick_isset(h2c->timeout)) {
340 t = task_new(tid_bit);
341 if (!t)
342 goto fail;
343
344 h2c->task = t;
345 t->process = h2_timeout_task;
346 t->context = h2c;
347 t->expire = tick_add(now_ms, h2c->timeout);
348 }
Willy Tarreauea392822017-10-31 10:02:25 +0100349
Willy Tarreau32218eb2017-09-22 08:07:25 +0200350 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
351 if (!h2c->ddht)
352 goto fail;
353
354 /* Initialise the context. */
355 h2c->st0 = H2_CS_PREFACE;
356 h2c->conn = conn;
357 h2c->max_id = -1;
358 h2c->errcode = H2_ERR_NO_ERROR;
359 h2c->flags = H2_CF_NONE;
360 h2c->rcvd_c = 0;
361 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100362 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200363 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200365 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200366 h2c->dsi = -1;
367 h2c->msi = -1;
368 h2c->last_sid = -1;
369
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200370 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371 h2c->miw = 65535; /* mux initial window size */
372 h2c->mws = 65535; /* mux window size */
373 h2c->mfs = 16384; /* initial max frame size */
374 h2c->streams_by_id = EB_ROOT_UNIQUE;
375 LIST_INIT(&h2c->send_list);
376 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100377 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200378 conn->mux_ctx = h2c;
379
Willy Tarreau3f133572017-10-31 19:21:06 +0100380 if (t)
381 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200383 LIST_INIT(&h2c->send_wait_list);
Willy Tarreauea392822017-10-31 10:02:25 +0100384
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385 /* mux->wake will be called soon to complete the operation */
386 return 0;
387 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100388 if (t)
389 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100390 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200391 return -1;
392}
393
Willy Tarreau62f52692017-10-08 23:01:42 +0200394/* Initialize the mux once it's attached. For outgoing connections, the context
395 * is already initialized before installing the mux, so we detect incoming
396 * connections from the fact that the context is still NULL. Returns < 0 on
397 * error.
398 */
399static int h2_init(struct connection *conn)
400{
401 if (conn->mux_ctx) {
402 /* we don't support outgoing connections for now */
403 return -1;
404 }
405
Willy Tarreau32218eb2017-09-22 08:07:25 +0200406 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200407}
408
Willy Tarreau2373acc2017-10-12 17:35:14 +0200409/* returns the stream associated with id <id> or NULL if not found */
410static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
411{
412 struct eb32_node *node;
413
Willy Tarreau2a856182017-05-16 15:20:39 +0200414 if (id > h2c->max_id)
415 return (struct h2s *)h2_idle_stream;
416
Willy Tarreau2373acc2017-10-12 17:35:14 +0200417 node = eb32_lookup(&h2c->streams_by_id, id);
418 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200419 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200420
421 return container_of(node, struct h2s, by_id);
422}
423
Willy Tarreau62f52692017-10-08 23:01:42 +0200424/* release function for a connection. This one should be called to free all
425 * resources allocated to the mux.
426 */
427static void h2_release(struct connection *conn)
428{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200429 struct h2c *h2c = conn->mux_ctx;
430
431 LIST_DEL(&conn->list);
432
433 if (h2c) {
434 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200435
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100437 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100438 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200439
Willy Tarreau44e973f2018-03-01 17:49:30 +0100440 h2_release_buf(h2c, &h2c->dbuf);
441 h2_release_buf(h2c, &h2c->mbuf);
442
Willy Tarreauea392822017-10-31 10:02:25 +0100443 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200444 h2c->task->context = NULL;
445 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100446 h2c->task = NULL;
447 }
448
Willy Tarreaubafbe012017-11-24 17:34:44 +0100449 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200450 }
451
452 conn->mux = NULL;
453 conn->mux_ctx = NULL;
454
455 conn_stop_tracking(conn);
456 conn_full_close(conn);
457 if (conn->destroy_cb)
458 conn->destroy_cb(conn);
459 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200460}
461
462
Willy Tarreau71681172017-10-23 14:39:06 +0200463/******************************************************/
464/* functions below are for the H2 protocol processing */
465/******************************************************/
466
467/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100468static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200469{
470 return h2s ? h2s->id : 0;
471}
472
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200473/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100474static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200475{
476 if (h2c->msi < 0)
477 return 0;
478
479 if (h2c->msi == h2s_id(h2s))
480 return 0;
481
482 return 1;
483}
484
Willy Tarreau741d6df2017-10-17 08:00:59 +0200485/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100486static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200487{
488 h2c->errcode = err;
489 h2c->st0 = H2_CS_ERROR;
490}
491
Willy Tarreau2e43f082017-10-17 08:03:59 +0200492/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100493static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200494{
495 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
496 h2s->errcode = err;
497 h2s->st = H2_SS_ERROR;
498 if (h2s->cs)
499 h2s->cs->flags |= CS_FL_ERROR;
500 }
501}
502
Willy Tarreaue4820742017-07-27 13:37:23 +0200503/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100504static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200505{
506 uint8_t *out = frame;
507
508 *out = len >> 16;
509 write_n16(out + 1, len);
510}
511
Willy Tarreau54c15062017-10-10 17:10:03 +0200512/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
513 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
514 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200515 * available in the buffer's input prior to calling this function. The buffer
516 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200517 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100518static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200519 const struct buffer *b, int o)
520{
Willy Tarreau591d4452018-06-15 17:21:00 +0200521 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 +0200522}
523
Willy Tarreau1f094672017-11-20 21:27:45 +0100524static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200525{
Willy Tarreau591d4452018-06-15 17:21:00 +0200526 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200527}
528
Willy Tarreau1f094672017-11-20 21:27:45 +0100529static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200530{
Willy Tarreau591d4452018-06-15 17:21:00 +0200531 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200532}
533
Willy Tarreau1f094672017-11-20 21:27:45 +0100534static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200535{
Willy Tarreau591d4452018-06-15 17:21:00 +0200536 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200537}
538
539
Willy Tarreau715d5312017-07-11 15:20:24 +0200540/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
541 * is not obvious. It turns out that H2 headers are neither aligned nor do they
542 * use regular sizes. And to add to the trouble, the buffer may wrap so each
543 * byte read must be checked. The header is formed like this :
544 *
545 * b0 b1 b2 b3 b4 b5..b8
546 * +----------+---------+--------+----+----+----------------------+
547 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
548 * +----------+---------+--------+----+----+----------------------+
549 *
550 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
551 * we get the sid properly aligned and ordered, and 16 bits of len properly
552 * ordered as well. The type and flags can be extracted using bit shifts from
553 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200554 * Returns zero if some bytes are missing, otherwise non-zero on success. The
555 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200556 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200558{
559 uint64_t w;
560
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200561 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200562 return 0;
563
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200564 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200565 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200566 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
567 h->ff = w >> 32;
568 h->ft = w >> 40;
569 h->len += w >> 48;
570 return 1;
571}
572
573/* skip the next 9 bytes corresponding to the frame header possibly parsed by
574 * h2_peek_frame_hdr() above.
575 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200577{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200578 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200579}
580
581/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100582static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200583{
584 int ret;
585
586 ret = h2_peek_frame_hdr(b, h);
587 if (ret > 0)
588 h2_skip_frame_hdr(b);
589 return ret;
590}
591
Willy Tarreau00dd0782018-03-01 16:31:34 +0100592/* marks stream <h2s> as CLOSED and decrement the number of active streams for
593 * its connection if the stream was not yet closed. Please use this exclusively
594 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100595 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100596static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100597{
598 if (h2s->st != H2_SS_CLOSED)
599 h2s->h2c->nb_streams--;
600 h2s->st = H2_SS_CLOSED;
601}
602
Willy Tarreau71049cc2018-03-28 13:56:39 +0200603/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
604static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100605{
606 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200607 LIST_DEL(&h2s->list);
608 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100609 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100610 pool_free(pool_head_h2s, h2s);
611}
612
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200613/* creates a new stream <id> on the h2c connection and returns it, or NULL in
614 * case of memory allocation error.
615 */
616static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
617{
618 struct conn_stream *cs;
619 struct h2s *h2s;
620
Willy Tarreaubafbe012017-11-24 17:34:44 +0100621 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200622 if (!h2s)
623 goto out;
624
625 h2s->h2c = h2c;
626 h2s->mws = h2c->miw;
627 h2s->flags = H2_SF_NONE;
628 h2s->errcode = H2_ERR_NO_ERROR;
629 h2s->st = H2_SS_IDLE;
630 h1m_init(&h2s->req);
631 h1m_init(&h2s->res);
632 h2s->by_id.key = h2s->id = id;
633 h2c->max_id = id;
634 LIST_INIT(&h2s->list);
635
636 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100637 h2c->nb_streams++;
638 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
639 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200640
641 cs = cs_new(h2c->conn);
642 if (!cs)
643 goto out_close;
644
645 h2s->cs = cs;
646 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200647 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200648
649 if (stream_create_from_cs(cs) < 0)
650 goto out_free_cs;
651
652 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200653 if (h2_has_too_many_cs(h2c))
654 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200655 return h2s;
656
657 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200658 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200659 cs_free(cs);
660 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200661 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200662 h2s = NULL;
663 out:
664 return h2s;
665}
666
Willy Tarreaube5b7152017-09-25 16:25:39 +0200667/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
668 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
669 * the various settings codes.
670 */
671static int h2c_snd_settings(struct h2c *h2c)
672{
673 struct buffer *res;
674 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200675 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200676 int ret;
677
678 if (h2c_mux_busy(h2c, NULL)) {
679 h2c->flags |= H2_CF_DEM_MBUSY;
680 return 0;
681 }
682
Willy Tarreau44e973f2018-03-01 17:49:30 +0100683 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200684 if (!res) {
685 h2c->flags |= H2_CF_MUX_MALLOC;
686 h2c->flags |= H2_CF_DEM_MROOM;
687 return 0;
688 }
689
690 chunk_init(&buf, buf_data, sizeof(buf_data));
691 chunk_memcpy(&buf,
692 "\x00\x00\x00" /* length : 0 for now */
693 "\x04\x00" /* type : 4 (settings), flags : 0 */
694 "\x00\x00\x00\x00", /* stream ID : 0 */
695 9);
696
697 if (h2_settings_header_table_size != 4096) {
698 char str[6] = "\x00\x01"; /* header_table_size */
699
700 write_n32(str + 2, h2_settings_header_table_size);
701 chunk_memcat(&buf, str, 6);
702 }
703
704 if (h2_settings_initial_window_size != 65535) {
705 char str[6] = "\x00\x04"; /* initial_window_size */
706
707 write_n32(str + 2, h2_settings_initial_window_size);
708 chunk_memcat(&buf, str, 6);
709 }
710
711 if (h2_settings_max_concurrent_streams != 0) {
712 char str[6] = "\x00\x03"; /* max_concurrent_streams */
713
714 /* Note: 0 means "unlimited" for haproxy's config but not for
715 * the protocol, so never send this value!
716 */
717 write_n32(str + 2, h2_settings_max_concurrent_streams);
718 chunk_memcat(&buf, str, 6);
719 }
720
721 if (global.tune.bufsize != 16384) {
722 char str[6] = "\x00\x05"; /* max_frame_size */
723
724 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
725 * match bufsize - rewrite size, but at the moment it seems
726 * that clients don't take care of it.
727 */
728 write_n32(str + 2, global.tune.bufsize);
729 chunk_memcat(&buf, str, 6);
730 }
731
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 h2_set_frame_size(buf.area, buf.data - 9);
733 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200734 if (unlikely(ret <= 0)) {
735 if (!ret) {
736 h2c->flags |= H2_CF_MUX_MFULL;
737 h2c->flags |= H2_CF_DEM_MROOM;
738 return 0;
739 }
740 else {
741 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
742 return 0;
743 }
744 }
745 return ret;
746}
747
Willy Tarreau52eed752017-09-22 15:05:09 +0200748/* Try to receive a connection preface, then upon success try to send our
749 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
750 * missing data. It may return an error in h2c.
751 */
752static int h2c_frt_recv_preface(struct h2c *h2c)
753{
754 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200755 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200756
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200757 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200758
759 if (unlikely(ret1 <= 0)) {
760 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
761 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
762 return 0;
763 }
764
Willy Tarreaube5b7152017-09-25 16:25:39 +0200765 ret2 = h2c_snd_settings(h2c);
766 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200767 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200768
Willy Tarreaube5b7152017-09-25 16:25:39 +0200769 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200770}
771
Willy Tarreau081d4722017-05-16 21:51:05 +0200772/* try to send a GOAWAY frame on the connection to report an error or a graceful
773 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
774 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
775 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
776 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
777 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
778 * on unrecoverable failure. It will not attempt to send one again in this last
779 * case so that it is safe to use h2c_error() to report such errors.
780 */
781static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
782{
783 struct buffer *res;
784 char str[17];
785 int ret;
786
787 if (h2c->flags & H2_CF_GOAWAY_FAILED)
788 return 1; // claim that it worked
789
790 if (h2c_mux_busy(h2c, h2s)) {
791 if (h2s)
792 h2s->flags |= H2_SF_BLK_MBUSY;
793 else
794 h2c->flags |= H2_CF_DEM_MBUSY;
795 return 0;
796 }
797
Willy Tarreau44e973f2018-03-01 17:49:30 +0100798 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200799 if (!res) {
800 h2c->flags |= H2_CF_MUX_MALLOC;
801 if (h2s)
802 h2s->flags |= H2_SF_BLK_MROOM;
803 else
804 h2c->flags |= H2_CF_DEM_MROOM;
805 return 0;
806 }
807
808 /* len: 8, type: 7, flags: none, sid: 0 */
809 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
810
811 if (h2c->last_sid < 0)
812 h2c->last_sid = h2c->max_id;
813
814 write_n32(str + 9, h2c->last_sid);
815 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200816 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200817 if (unlikely(ret <= 0)) {
818 if (!ret) {
819 h2c->flags |= H2_CF_MUX_MFULL;
820 if (h2s)
821 h2s->flags |= H2_SF_BLK_MROOM;
822 else
823 h2c->flags |= H2_CF_DEM_MROOM;
824 return 0;
825 }
826 else {
827 /* we cannot report this error using GOAWAY, so we mark
828 * it and claim a success.
829 */
830 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
831 h2c->flags |= H2_CF_GOAWAY_FAILED;
832 return 1;
833 }
834 }
835 h2c->flags |= H2_CF_GOAWAY_SENT;
836 return ret;
837}
838
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100839/* Try to send an RST_STREAM frame on the connection for the indicated stream
840 * during mux operations. This stream must be valid and cannot be closed
841 * already. h2s->id will be used for the stream ID and h2s->errcode will be
842 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
843 * not yet.
844 *
845 * Returns > 0 on success or zero if nothing was done. In case of lack of room
846 * to write the message, it subscribes the stream to future notifications.
847 */
848static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
849{
850 struct buffer *res;
851 char str[13];
852 int ret;
853
854 if (!h2s || h2s->st == H2_SS_CLOSED)
855 return 1;
856
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100857 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
858 * RST_STREAM in response to a RST_STREAM frame.
859 */
860 if (h2c->dft == H2_FT_RST_STREAM) {
861 ret = 1;
862 goto ignore;
863 }
864
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100865 if (h2c_mux_busy(h2c, h2s)) {
866 h2s->flags |= H2_SF_BLK_MBUSY;
867 return 0;
868 }
869
Willy Tarreau44e973f2018-03-01 17:49:30 +0100870 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100871 if (!res) {
872 h2c->flags |= H2_CF_MUX_MALLOC;
873 h2s->flags |= H2_SF_BLK_MROOM;
874 return 0;
875 }
876
877 /* len: 4, type: 3, flags: none */
878 memcpy(str, "\x00\x00\x04\x03\x00", 5);
879 write_n32(str + 5, h2s->id);
880 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200881 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100882
883 if (unlikely(ret <= 0)) {
884 if (!ret) {
885 h2c->flags |= H2_CF_MUX_MFULL;
886 h2s->flags |= H2_SF_BLK_MROOM;
887 return 0;
888 }
889 else {
890 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
891 return 0;
892 }
893 }
894
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100895 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100896 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100897 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100898 return ret;
899}
900
901/* Try to send an RST_STREAM frame on the connection for the stream being
902 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
903 * error code unless the stream's state already is IDLE or CLOSED in which
904 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
905 * it was not yet.
906 *
907 * Returns > 0 on success or zero if nothing was done. In case of lack of room
908 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200909 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100910 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200911 */
912static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
913{
914 struct buffer *res;
915 char str[13];
916 int ret;
917
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100918 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
919 * RST_STREAM in response to a RST_STREAM frame.
920 */
921 if (h2c->dft == H2_FT_RST_STREAM) {
922 ret = 1;
923 goto ignore;
924 }
925
Willy Tarreau27a84c92017-10-17 08:10:17 +0200926 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100927 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200928 return 0;
929 }
930
Willy Tarreau44e973f2018-03-01 17:49:30 +0100931 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200932 if (!res) {
933 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100934 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200935 return 0;
936 }
937
938 /* len: 4, type: 3, flags: none */
939 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100942 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200943 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200944 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945
Willy Tarreau27a84c92017-10-17 08:10:17 +0200946 if (unlikely(ret <= 0)) {
947 if (!ret) {
948 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100949 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200950 return 0;
951 }
952 else {
953 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
954 return 0;
955 }
956 }
957
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100958 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100959 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200960 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100961 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100962 }
963
Willy Tarreau27a84c92017-10-17 08:10:17 +0200964 return ret;
965}
966
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100967/* try to send an empty DATA frame with the ES flag set to notify about the
968 * end of stream and match a shutdown(write). If an ES was already sent as
969 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
970 * on success or zero if nothing was done. In case of lack of room to write the
971 * message, it subscribes the requesting stream to future notifications.
972 */
973static int h2_send_empty_data_es(struct h2s *h2s)
974{
975 struct h2c *h2c = h2s->h2c;
976 struct buffer *res;
977 char str[9];
978 int ret;
979
Willy Tarreau721c9742017-11-07 11:05:42 +0100980 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100981 return 1;
982
983 if (h2c_mux_busy(h2c, h2s)) {
984 h2s->flags |= H2_SF_BLK_MBUSY;
985 return 0;
986 }
987
Willy Tarreau44e973f2018-03-01 17:49:30 +0100988 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100989 if (!res) {
990 h2c->flags |= H2_CF_MUX_MALLOC;
991 h2s->flags |= H2_SF_BLK_MROOM;
992 return 0;
993 }
994
995 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
996 memcpy(str, "\x00\x00\x00\x00\x01", 5);
997 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200998 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100999 if (likely(ret > 0)) {
1000 h2s->flags |= H2_SF_ES_SENT;
1001 }
1002 else if (!ret) {
1003 h2c->flags |= H2_CF_MUX_MFULL;
1004 h2s->flags |= H2_SF_BLK_MROOM;
1005 return 0;
1006 }
1007 else {
1008 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1009 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001010 }
1011 return ret;
1012}
1013
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001014/* wake the streams attached to the connection, whose id is greater than <last>,
1015 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1016 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1017 * stream's state is automatically updated accordingly.
1018 */
1019static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1020{
1021 struct eb32_node *node;
1022 struct h2s *h2s;
1023
1024 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1025 flags |= CS_FL_ERROR;
1026
1027 if (conn_xprt_read0_pending(h2c->conn))
1028 flags |= CS_FL_EOS;
1029
1030 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1031 while (node) {
1032 h2s = container_of(node, struct h2s, by_id);
1033 if (h2s->id <= last)
1034 break;
1035 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001036
1037 if (!h2s->cs) {
1038 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001039 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001040 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001041 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001042
1043 h2s->cs->flags |= flags;
1044 /* recv is used to force to detect CS_FL_EOS that wake()
1045 * doesn't handle in the stream int code.
1046 */
1047 h2s->cs->data_cb->recv(h2s->cs);
1048 h2s->cs->data_cb->wake(h2s->cs);
1049
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001050 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1051 h2s->st = H2_SS_ERROR;
1052 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1053 h2s->st = H2_SS_HREM;
1054 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001055 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001056 }
1057}
1058
Willy Tarreau3421aba2017-07-27 15:41:03 +02001059/* Increase all streams' outgoing window size by the difference passed in
1060 * argument. This is needed upon receipt of the settings frame if the initial
1061 * window size is different. The difference may be negative and the resulting
1062 * window size as well, for the time it takes to receive some window updates.
1063 */
1064static void h2c_update_all_ws(struct h2c *h2c, int diff)
1065{
1066 struct h2s *h2s;
1067 struct eb32_node *node;
1068
1069 if (!diff)
1070 return;
1071
1072 node = eb32_first(&h2c->streams_by_id);
1073 while (node) {
1074 h2s = container_of(node, struct h2s, by_id);
1075 h2s->mws += diff;
1076 node = eb32_next(node);
1077 }
1078}
1079
1080/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1081 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1082 * return an error in h2c. Described in RFC7540#6.5.
1083 */
1084static int h2c_handle_settings(struct h2c *h2c)
1085{
1086 unsigned int offset;
1087 int error;
1088
1089 if (h2c->dff & H2_F_SETTINGS_ACK) {
1090 if (h2c->dfl) {
1091 error = H2_ERR_FRAME_SIZE_ERROR;
1092 goto fail;
1093 }
1094 return 1;
1095 }
1096
1097 if (h2c->dsi != 0) {
1098 error = H2_ERR_PROTOCOL_ERROR;
1099 goto fail;
1100 }
1101
1102 if (h2c->dfl % 6) {
1103 error = H2_ERR_FRAME_SIZE_ERROR;
1104 goto fail;
1105 }
1106
1107 /* that's the limit we can process */
1108 if (h2c->dfl > global.tune.bufsize) {
1109 error = H2_ERR_FRAME_SIZE_ERROR;
1110 goto fail;
1111 }
1112
1113 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001114 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001115 return 0;
1116
1117 /* parse the frame */
1118 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001119 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1120 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001121
1122 switch (type) {
1123 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1124 /* we need to update all existing streams with the
1125 * difference from the previous iws.
1126 */
1127 if (arg < 0) { // RFC7540#6.5.2
1128 error = H2_ERR_FLOW_CONTROL_ERROR;
1129 goto fail;
1130 }
1131 h2c_update_all_ws(h2c, arg - h2c->miw);
1132 h2c->miw = arg;
1133 break;
1134 case H2_SETTINGS_MAX_FRAME_SIZE:
1135 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1136 error = H2_ERR_PROTOCOL_ERROR;
1137 goto fail;
1138 }
1139 h2c->mfs = arg;
1140 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001141 case H2_SETTINGS_ENABLE_PUSH:
1142 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1143 error = H2_ERR_PROTOCOL_ERROR;
1144 goto fail;
1145 }
1146 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001147 }
1148 }
1149
1150 /* need to ACK this frame now */
1151 h2c->st0 = H2_CS_FRAME_A;
1152 return 1;
1153 fail:
1154 h2c_error(h2c, error);
1155 return 0;
1156}
1157
1158/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1159 * success or one of the h2_status values.
1160 */
1161static int h2c_ack_settings(struct h2c *h2c)
1162{
1163 struct buffer *res;
1164 char str[9];
1165 int ret = -1;
1166
1167 if (h2c_mux_busy(h2c, NULL)) {
1168 h2c->flags |= H2_CF_DEM_MBUSY;
1169 return 0;
1170 }
1171
Willy Tarreau44e973f2018-03-01 17:49:30 +01001172 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001173 if (!res) {
1174 h2c->flags |= H2_CF_MUX_MALLOC;
1175 h2c->flags |= H2_CF_DEM_MROOM;
1176 return 0;
1177 }
1178
1179 memcpy(str,
1180 "\x00\x00\x00" /* length : 0 (no data) */
1181 "\x04" "\x01" /* type : 4, flags : ACK */
1182 "\x00\x00\x00\x00" /* stream ID */, 9);
1183
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001184 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001185 if (unlikely(ret <= 0)) {
1186 if (!ret) {
1187 h2c->flags |= H2_CF_MUX_MFULL;
1188 h2c->flags |= H2_CF_DEM_MROOM;
1189 return 0;
1190 }
1191 else {
1192 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1193 return 0;
1194 }
1195 }
1196 return ret;
1197}
1198
Willy Tarreaucf68c782017-10-10 17:11:41 +02001199/* processes a PING frame and schedules an ACK if needed. The caller must pass
1200 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1201 * missing data. It may return an error in h2c.
1202 */
1203static int h2c_handle_ping(struct h2c *h2c)
1204{
1205 /* frame length must be exactly 8 */
1206 if (h2c->dfl != 8) {
1207 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1208 return 0;
1209 }
1210
1211 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001212 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001213 h2c->st0 = H2_CS_FRAME_A;
1214 return 1;
1215}
1216
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001217/* Try to send a window update for stream id <sid> and value <increment>.
1218 * Returns > 0 on success or zero on missing room or failure. It may return an
1219 * error in h2c.
1220 */
1221static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1222{
1223 struct buffer *res;
1224 char str[13];
1225 int ret = -1;
1226
1227 if (h2c_mux_busy(h2c, NULL)) {
1228 h2c->flags |= H2_CF_DEM_MBUSY;
1229 return 0;
1230 }
1231
Willy Tarreau44e973f2018-03-01 17:49:30 +01001232 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001233 if (!res) {
1234 h2c->flags |= H2_CF_MUX_MALLOC;
1235 h2c->flags |= H2_CF_DEM_MROOM;
1236 return 0;
1237 }
1238
1239 /* length: 4, type: 8, flags: none */
1240 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1241 write_n32(str + 5, sid);
1242 write_n32(str + 9, increment);
1243
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001244 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001245
1246 if (unlikely(ret <= 0)) {
1247 if (!ret) {
1248 h2c->flags |= H2_CF_MUX_MFULL;
1249 h2c->flags |= H2_CF_DEM_MROOM;
1250 return 0;
1251 }
1252 else {
1253 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1254 return 0;
1255 }
1256 }
1257 return ret;
1258}
1259
1260/* try to send pending window update for the connection. It's safe to call it
1261 * with no pending updates. Returns > 0 on success or zero on missing room or
1262 * failure. It may return an error in h2c.
1263 */
1264static int h2c_send_conn_wu(struct h2c *h2c)
1265{
1266 int ret = 1;
1267
1268 if (h2c->rcvd_c <= 0)
1269 return 1;
1270
1271 /* send WU for the connection */
1272 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1273 if (ret > 0)
1274 h2c->rcvd_c = 0;
1275
1276 return ret;
1277}
1278
1279/* try to send pending window update for the current dmux stream. It's safe to
1280 * call it with no pending updates. Returns > 0 on success or zero on missing
1281 * room or failure. It may return an error in h2c.
1282 */
1283static int h2c_send_strm_wu(struct h2c *h2c)
1284{
1285 int ret = 1;
1286
1287 if (h2c->rcvd_s <= 0)
1288 return 1;
1289
1290 /* send WU for the stream */
1291 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1292 if (ret > 0)
1293 h2c->rcvd_s = 0;
1294
1295 return ret;
1296}
1297
Willy Tarreaucf68c782017-10-10 17:11:41 +02001298/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1299 * success, 0 on missing data or one of the h2_status values.
1300 */
1301static int h2c_ack_ping(struct h2c *h2c)
1302{
1303 struct buffer *res;
1304 char str[17];
1305 int ret = -1;
1306
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001307 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001308 return 0;
1309
1310 if (h2c_mux_busy(h2c, NULL)) {
1311 h2c->flags |= H2_CF_DEM_MBUSY;
1312 return 0;
1313 }
1314
Willy Tarreau44e973f2018-03-01 17:49:30 +01001315 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001316 if (!res) {
1317 h2c->flags |= H2_CF_MUX_MALLOC;
1318 h2c->flags |= H2_CF_DEM_MROOM;
1319 return 0;
1320 }
1321
1322 memcpy(str,
1323 "\x00\x00\x08" /* length : 8 (same payload) */
1324 "\x06" "\x01" /* type : 6, flags : ACK */
1325 "\x00\x00\x00\x00" /* stream ID */, 9);
1326
1327 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001328 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001329
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001330 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001331 if (unlikely(ret <= 0)) {
1332 if (!ret) {
1333 h2c->flags |= H2_CF_MUX_MFULL;
1334 h2c->flags |= H2_CF_DEM_MROOM;
1335 return 0;
1336 }
1337 else {
1338 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1339 return 0;
1340 }
1341 }
1342 return ret;
1343}
1344
Willy Tarreau26f95952017-07-27 17:18:30 +02001345/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1346 * Returns > 0 on success or zero on missing data. It may return an error in
1347 * h2c or h2s. Described in RFC7540#6.9.
1348 */
1349static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1350{
1351 int32_t inc;
1352 int error;
1353
1354 if (h2c->dfl != 4) {
1355 error = H2_ERR_FRAME_SIZE_ERROR;
1356 goto conn_err;
1357 }
1358
1359 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001360 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001361 return 0;
1362
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001363 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001364
1365 if (h2c->dsi != 0) {
1366 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001367
1368 /* it's not an error to receive WU on a closed stream */
1369 if (h2s->st == H2_SS_CLOSED)
1370 return 1;
1371
1372 if (!inc) {
1373 error = H2_ERR_PROTOCOL_ERROR;
1374 goto strm_err;
1375 }
1376
1377 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1378 error = H2_ERR_FLOW_CONTROL_ERROR;
1379 goto strm_err;
1380 }
1381
1382 h2s->mws += inc;
1383 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1384 h2s->flags &= ~H2_SF_BLK_SFCTL;
1385 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1386 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1387 /* This stream wanted to send but could not due to its
1388 * own flow control. We can put it back into the send
1389 * list now, it will be handled upon next send() call.
1390 */
1391 LIST_ADDQ(&h2c->send_list, &h2s->list);
1392 }
1393 }
1394 }
1395 else {
1396 /* connection window update */
1397 if (!inc) {
1398 error = H2_ERR_PROTOCOL_ERROR;
1399 goto conn_err;
1400 }
1401
1402 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1403 error = H2_ERR_FLOW_CONTROL_ERROR;
1404 goto conn_err;
1405 }
1406
1407 h2c->mws += inc;
1408 }
1409
1410 return 1;
1411
1412 conn_err:
1413 h2c_error(h2c, error);
1414 return 0;
1415
1416 strm_err:
1417 if (h2s) {
1418 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001419 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001420 }
1421 else
1422 h2c_error(h2c, error);
1423 return 0;
1424}
1425
Willy Tarreaue96b0922017-10-30 00:28:29 +01001426/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1427 * the last ID. Returns > 0 on success or zero on missing data. It may return
1428 * an error in h2c. Described in RFC7540#6.8.
1429 */
1430static int h2c_handle_goaway(struct h2c *h2c)
1431{
1432 int error;
1433 int last;
1434
1435 if (h2c->dsi != 0) {
1436 error = H2_ERR_PROTOCOL_ERROR;
1437 goto conn_err;
1438 }
1439
1440 if (h2c->dfl < 8) {
1441 error = H2_ERR_FRAME_SIZE_ERROR;
1442 goto conn_err;
1443 }
1444
1445 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001446 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001447 return 0;
1448
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001449 last = h2_get_n32(&h2c->dbuf, 0);
1450 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001451 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001452 if (h2c->last_sid < 0)
1453 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001454 return 1;
1455
1456 conn_err:
1457 h2c_error(h2c, error);
1458 return 0;
1459}
1460
Willy Tarreau92153fc2017-12-03 19:46:19 +01001461/* processes a PRIORITY frame, and either skips it or rejects if it is
1462 * invalid. Returns > 0 on success or zero on missing data. It may return
1463 * an error in h2c. Described in RFC7540#6.3.
1464 */
1465static int h2c_handle_priority(struct h2c *h2c)
1466{
1467 int error;
1468
1469 if (h2c->dsi == 0) {
1470 error = H2_ERR_PROTOCOL_ERROR;
1471 goto conn_err;
1472 }
1473
1474 if (h2c->dfl != 5) {
1475 error = H2_ERR_FRAME_SIZE_ERROR;
1476 goto conn_err;
1477 }
1478
1479 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001480 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001481 return 0;
1482
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001483 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001484 /* 7540#5.3 : can't depend on itself */
1485 error = H2_ERR_PROTOCOL_ERROR;
1486 goto conn_err;
1487 }
1488 return 1;
1489
1490 conn_err:
1491 h2c_error(h2c, error);
1492 return 0;
1493}
1494
Willy Tarreaucd234e92017-08-18 10:59:39 +02001495/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1496 * Returns > 0 on success or zero on missing data. It may return an error in
1497 * h2c. Described in RFC7540#6.4.
1498 */
1499static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1500{
1501 int error;
1502
1503 if (h2c->dsi == 0) {
1504 error = H2_ERR_PROTOCOL_ERROR;
1505 goto conn_err;
1506 }
1507
Willy Tarreaucd234e92017-08-18 10:59:39 +02001508 if (h2c->dfl != 4) {
1509 error = H2_ERR_FRAME_SIZE_ERROR;
1510 goto conn_err;
1511 }
1512
1513 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001514 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001515 return 0;
1516
1517 /* late RST, already handled */
1518 if (h2s->st == H2_SS_CLOSED)
1519 return 1;
1520
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001521 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001522 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001523
1524 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001525 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001526 /* recv is used to force to detect CS_FL_EOS that wake()
1527 * doesn't handle in the stream-int code.
1528 */
1529 h2s->cs->data_cb->recv(h2s->cs);
1530 h2s->cs->data_cb->wake(h2s->cs);
1531 }
1532
1533 h2s->flags |= H2_SF_RST_RCVD;
1534 return 1;
1535
1536 conn_err:
1537 h2c_error(h2c, error);
1538 return 0;
1539}
1540
Willy Tarreau13278b42017-10-13 19:23:14 +02001541/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1542 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1543 * errors here are reported as connection errors since it's impossible to
1544 * recover from such errors after the compression context has been altered.
1545 */
1546static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1547{
1548 int error;
1549
1550 if (!h2c->dfl) {
1551 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1552 goto strm_err;
1553 }
1554
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001555 if (!b_size(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001556 return 0; // empty buffer
1557
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001558 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001559 return 0; // incomplete frame
1560
Willy Tarreauf2101912018-07-19 10:11:38 +02001561 if (h2c->flags & H2_CF_DEM_TOOMANY)
1562 return 0; // too many cs still present
1563
Willy Tarreau13278b42017-10-13 19:23:14 +02001564 /* now either the frame is complete or the buffer is complete */
1565 if (h2s->st != H2_SS_IDLE) {
1566 /* FIXME: stream already exists, this is only allowed for
1567 * trailers (not supported for now).
1568 */
1569 error = H2_ERR_PROTOCOL_ERROR;
1570 goto conn_err;
1571 }
1572 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1573 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1574 error = H2_ERR_PROTOCOL_ERROR;
1575 goto conn_err;
1576 }
1577
1578 h2s = h2c_stream_new(h2c, h2c->dsi);
1579 if (!h2s) {
1580 error = H2_ERR_INTERNAL_ERROR;
1581 goto conn_err;
1582 }
1583
1584 h2s->st = H2_SS_OPEN;
1585 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1586 h2s->st = H2_SS_HREM;
1587 h2s->flags |= H2_SF_ES_RCVD;
1588 }
1589
1590 /* call the upper layers to process the frame, then let the upper layer
1591 * notify the stream about any change.
1592 */
1593 h2s->cs->data_cb->recv(h2s->cs);
1594
1595 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1596 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1597 error = H2_ERR_INTERNAL_ERROR;
1598 goto conn_err;
1599 }
1600
Willy Tarreau8f650c32017-11-21 19:36:21 +01001601 if (h2c->st0 >= H2_CS_ERROR)
1602 return 0;
1603
Willy Tarreau721c9742017-11-07 11:05:42 +01001604 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001605 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001606 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001607 }
1608 else {
1609 /* update the max stream ID if the request is being processed */
1610 if (h2s->id > h2c->max_id)
1611 h2c->max_id = h2s->id;
1612 }
1613
1614 return 1;
1615
1616 conn_err:
1617 h2c_error(h2c, error);
1618 return 0;
1619
1620 strm_err:
1621 if (h2s) {
1622 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001623 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001624 }
1625 else
1626 h2c_error(h2c, error);
1627 return 0;
1628}
1629
Willy Tarreau454f9052017-10-26 19:40:35 +02001630/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1631 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1632 */
1633static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1634{
1635 int error;
1636
1637 /* note that empty DATA frames are perfectly valid and sometimes used
1638 * to signal an end of stream (with the ES flag).
1639 */
1640
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001641 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001642 return 0; // empty buffer
1643
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001644 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001645 return 0; // incomplete frame
1646
1647 /* now either the frame is complete or the buffer is complete */
1648
1649 if (!h2c->dsi) {
1650 /* RFC7540#6.1 */
1651 error = H2_ERR_PROTOCOL_ERROR;
1652 goto conn_err;
1653 }
1654
1655 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1656 /* RFC7540#6.1 */
1657 error = H2_ERR_STREAM_CLOSED;
1658 goto strm_err;
1659 }
1660
Willy Tarreau454f9052017-10-26 19:40:35 +02001661 /* call the upper layers to process the frame, then let the upper layer
1662 * notify the stream about any change.
1663 */
1664 if (!h2s->cs) {
1665 error = H2_ERR_STREAM_CLOSED;
1666 goto strm_err;
1667 }
1668
1669 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001670
Willy Tarreau454f9052017-10-26 19:40:35 +02001671 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1672 /* cs has just been destroyed, we have to kill h2s. */
1673 error = H2_ERR_STREAM_CLOSED;
1674 goto strm_err;
1675 }
1676
Willy Tarreau8f650c32017-11-21 19:36:21 +01001677 if (h2c->st0 >= H2_CS_ERROR)
1678 return 0;
1679
Willy Tarreau721c9742017-11-07 11:05:42 +01001680 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001681 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001682 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001683 }
1684
1685 /* check for completion : the callee will change this to FRAME_A or
1686 * FRAME_H once done.
1687 */
1688 if (h2c->st0 == H2_CS_FRAME_P)
1689 return 0;
1690
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001691
1692 /* last frame */
1693 if (h2c->dff & H2_F_DATA_END_STREAM) {
1694 h2s->st = H2_SS_HREM;
1695 h2s->flags |= H2_SF_ES_RCVD;
1696 }
1697
Willy Tarreau454f9052017-10-26 19:40:35 +02001698 return 1;
1699
1700 conn_err:
1701 h2c_error(h2c, error);
1702 return 0;
1703
1704 strm_err:
1705 if (h2s) {
1706 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001707 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001708 }
1709 else
1710 h2c_error(h2c, error);
1711 return 0;
1712}
1713
Willy Tarreaubc933932017-10-09 16:21:43 +02001714/* process Rx frames to be demultiplexed */
1715static void h2_process_demux(struct h2c *h2c)
1716{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001717 struct h2s *h2s;
1718
Willy Tarreau081d4722017-05-16 21:51:05 +02001719 if (h2c->st0 >= H2_CS_ERROR)
1720 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001721
1722 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1723 if (h2c->st0 == H2_CS_PREFACE) {
1724 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1725 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1726 if (h2c->st0 == H2_CS_ERROR)
1727 h2c->st0 = H2_CS_ERROR2;
1728 goto fail;
1729 }
1730
1731 h2c->max_id = 0;
1732 h2c->st0 = H2_CS_SETTINGS1;
1733 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001734
1735 if (h2c->st0 == H2_CS_SETTINGS1) {
1736 struct h2_fh hdr;
1737
1738 /* ensure that what is pending is a valid SETTINGS frame
1739 * without an ACK.
1740 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001741 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001742 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1743 if (h2c->st0 == H2_CS_ERROR)
1744 h2c->st0 = H2_CS_ERROR2;
1745 goto fail;
1746 }
1747
1748 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1749 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1750 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1751 h2c->st0 = H2_CS_ERROR2;
1752 goto fail;
1753 }
1754
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001755 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001756 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1757 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1758 h2c->st0 = H2_CS_ERROR2;
1759 goto fail;
1760 }
1761
1762 /* that's OK, switch to FRAME_P to process it */
1763 h2c->dfl = hdr.len;
1764 h2c->dsi = hdr.sid;
1765 h2c->dft = hdr.ft;
1766 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001767 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001768 h2c->st0 = H2_CS_FRAME_P;
1769 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001770 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001771
1772 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001773 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001774 int ret = 0;
1775
1776 if (h2c->st0 >= H2_CS_ERROR)
1777 break;
1778
1779 if (h2c->st0 == H2_CS_FRAME_H) {
1780 struct h2_fh hdr;
1781
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001783 break;
1784
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001785 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001786 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1787 h2c->st0 = H2_CS_ERROR;
1788 break;
1789 }
1790
1791 h2c->dfl = hdr.len;
1792 h2c->dsi = hdr.sid;
1793 h2c->dft = hdr.ft;
1794 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001795 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001796 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001797 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001798 }
1799
1800 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001801 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001802
Willy Tarreaud7901432017-12-29 11:34:40 +01001803 if (h2c->st0 == H2_CS_FRAME_E)
1804 goto strm_err;
1805
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001806 if (h2s->st == H2_SS_IDLE &&
1807 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1808 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1809 * this state MUST be treated as a connection error
1810 */
1811 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1812 h2c->st0 = H2_CS_ERROR;
1813 break;
1814 }
1815
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001816 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1817 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1818 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1819 * this state MUST be treated as a stream error
1820 */
1821 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001822 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001823 goto strm_err;
1824 }
1825
Willy Tarreauab837502017-12-27 15:07:30 +01001826 /* Below the management of frames received in closed state is a
1827 * bit hackish because the spec makes strong differences between
1828 * streams closed by receiving RST, sending RST, and seeing ES
1829 * in both directions. In addition to this, the creation of a
1830 * new stream reusing the identifier of a closed one will be
1831 * detected here. Given that we cannot keep track of all closed
1832 * streams forever, we consider that unknown closed streams were
1833 * closed on RST received, which allows us to respond with an
1834 * RST without breaking the connection (eg: to abort a transfer).
1835 * Some frames have to be silently ignored as well.
1836 */
1837 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1838 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1839 /* #5.1.1: The identifier of a newly
1840 * established stream MUST be numerically
1841 * greater than all streams that the initiating
1842 * endpoint has opened or reserved. This
1843 * governs streams that are opened using a
1844 * HEADERS frame and streams that are reserved
1845 * using PUSH_PROMISE. An endpoint that
1846 * receives an unexpected stream identifier
1847 * MUST respond with a connection error.
1848 */
1849 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1850 goto strm_err;
1851 }
1852
1853 if (h2s->flags & H2_SF_RST_RCVD) {
1854 /* RFC7540#5.1:closed: an endpoint that
1855 * receives any frame other than PRIORITY after
1856 * receiving a RST_STREAM MUST treat that as a
1857 * stream error of type STREAM_CLOSED.
1858 *
1859 * Note that old streams fall into this category
1860 * and will lead to an RST being sent.
1861 */
1862 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1863 h2c->st0 = H2_CS_FRAME_E;
1864 goto strm_err;
1865 }
1866
1867 /* RFC7540#5.1:closed: if this state is reached as a
1868 * result of sending a RST_STREAM frame, the peer that
1869 * receives the RST_STREAM might have already sent
1870 * frames on the stream that cannot be withdrawn. An
1871 * endpoint MUST ignore frames that it receives on
1872 * closed streams after it has sent a RST_STREAM
1873 * frame. An endpoint MAY choose to limit the period
1874 * over which it ignores frames and treat frames that
1875 * arrive after this time as being in error.
1876 */
1877 if (!(h2s->flags & H2_SF_RST_SENT)) {
1878 /* RFC7540#5.1:closed: any frame other than
1879 * PRIO/WU/RST in this state MUST be treated as
1880 * a connection error
1881 */
1882 if (h2c->dft != H2_FT_RST_STREAM &&
1883 h2c->dft != H2_FT_PRIORITY &&
1884 h2c->dft != H2_FT_WINDOW_UPDATE) {
1885 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1886 goto strm_err;
1887 }
1888 }
1889 }
1890
Willy Tarreauc0da1962017-10-30 18:38:00 +01001891#if 0
1892 // problem below: it is not possible to completely ignore such
1893 // streams as we need to maintain the compression state as well
1894 // and for this we need to completely process these frames (eg:
1895 // HEADERS frames) as well as counting DATA frames to emit
1896 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1897 // This is a typical case of layer violation where the
1898 // transported contents are critical to the connection's
1899 // validity and must be ignored at the same time :-(
1900
1901 /* graceful shutdown, ignore streams whose ID is higher than
1902 * the one advertised in GOAWAY. RFC7540#6.8.
1903 */
1904 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001905 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1906 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001907 h2c->dfl -= ret;
1908 ret = h2c->dfl == 0;
1909 goto strm_err;
1910 }
1911#endif
1912
Willy Tarreau7e98c052017-10-10 15:56:59 +02001913 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001914 case H2_FT_SETTINGS:
1915 if (h2c->st0 == H2_CS_FRAME_P)
1916 ret = h2c_handle_settings(h2c);
1917
1918 if (h2c->st0 == H2_CS_FRAME_A)
1919 ret = h2c_ack_settings(h2c);
1920 break;
1921
Willy Tarreaucf68c782017-10-10 17:11:41 +02001922 case H2_FT_PING:
1923 if (h2c->st0 == H2_CS_FRAME_P)
1924 ret = h2c_handle_ping(h2c);
1925
1926 if (h2c->st0 == H2_CS_FRAME_A)
1927 ret = h2c_ack_ping(h2c);
1928 break;
1929
Willy Tarreau26f95952017-07-27 17:18:30 +02001930 case H2_FT_WINDOW_UPDATE:
1931 if (h2c->st0 == H2_CS_FRAME_P)
1932 ret = h2c_handle_window_update(h2c, h2s);
1933 break;
1934
Willy Tarreau61290ec2017-10-17 08:19:21 +02001935 case H2_FT_CONTINUATION:
1936 /* we currently don't support CONTINUATION frames since
1937 * we have nowhere to store the partial HEADERS frame.
1938 * Let's abort the stream on an INTERNAL_ERROR here.
1939 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001940 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001941 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001942 h2c->st0 = H2_CS_FRAME_E;
1943 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001944 break;
1945
Willy Tarreau13278b42017-10-13 19:23:14 +02001946 case H2_FT_HEADERS:
1947 if (h2c->st0 == H2_CS_FRAME_P)
1948 ret = h2c_frt_handle_headers(h2c, h2s);
1949 break;
1950
Willy Tarreau454f9052017-10-26 19:40:35 +02001951 case H2_FT_DATA:
1952 if (h2c->st0 == H2_CS_FRAME_P)
1953 ret = h2c_frt_handle_data(h2c, h2s);
1954
1955 if (h2c->st0 == H2_CS_FRAME_A)
1956 ret = h2c_send_strm_wu(h2c);
1957 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001958
Willy Tarreau92153fc2017-12-03 19:46:19 +01001959 case H2_FT_PRIORITY:
1960 if (h2c->st0 == H2_CS_FRAME_P)
1961 ret = h2c_handle_priority(h2c);
1962 break;
1963
Willy Tarreaucd234e92017-08-18 10:59:39 +02001964 case H2_FT_RST_STREAM:
1965 if (h2c->st0 == H2_CS_FRAME_P)
1966 ret = h2c_handle_rst_stream(h2c, h2s);
1967 break;
1968
Willy Tarreaue96b0922017-10-30 00:28:29 +01001969 case H2_FT_GOAWAY:
1970 if (h2c->st0 == H2_CS_FRAME_P)
1971 ret = h2c_handle_goaway(h2c);
1972 break;
1973
Willy Tarreau1c661982017-10-30 13:52:01 +01001974 case H2_FT_PUSH_PROMISE:
1975 /* not permitted here, RFC7540#5.1 */
1976 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001977 break;
1978
1979 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001980 default:
1981 /* drop frames that we ignore. They may be larger than
1982 * the buffer so we drain all of their contents until
1983 * we reach the end.
1984 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001985 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1986 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001987 h2c->dfl -= ret;
1988 ret = h2c->dfl == 0;
1989 }
1990
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001991 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001992 /* We may have to send an RST if not done yet */
1993 if (h2s->st == H2_SS_ERROR)
1994 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001995
Willy Tarreaua20a5192017-12-27 11:02:06 +01001996 if (h2c->st0 == H2_CS_FRAME_E)
1997 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001998
Willy Tarreau7e98c052017-10-10 15:56:59 +02001999 /* error or missing data condition met above ? */
2000 if (ret <= 0)
2001 break;
2002
2003 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002004 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002005 h2c->st0 = H2_CS_FRAME_H;
2006 }
2007 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002008
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002009 if (h2c->rcvd_c > 0 &&
2010 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2011 h2c_send_conn_wu(h2c);
2012
Willy Tarreau52eed752017-09-22 15:05:09 +02002013 fail:
2014 /* we can go here on missing data, blocked response or error */
2015 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002016}
2017
2018/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2019 * the end.
2020 */
2021static int h2_process_mux(struct h2c *h2c)
2022{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002023 struct h2s *h2s, *h2s_back;
2024
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002025 /* start by sending possibly pending window updates */
2026 if (h2c->rcvd_c > 0 &&
2027 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2028 h2c_send_conn_wu(h2c) < 0)
2029 goto fail;
2030
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002031 /* First we always process the flow control list because the streams
2032 * waiting there were already elected for immediate emission but were
2033 * blocked just on this.
2034 */
2035
2036 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2037 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2038 h2c->st0 >= H2_CS_ERROR)
2039 break;
2040
2041 /* In theory it's possible that h2s->cs == NULL here :
2042 * - client sends crap that causes a parse error
2043 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2044 * - RST_STREAM cannot be emitted because mux is busy/full
2045 * - stream gets notified, detaches and quits
2046 * - mux buffer gets ready and wakes pending streams up
2047 * - bam!
2048 */
2049 h2s->flags &= ~H2_SF_BLK_ANY;
2050
2051 if (h2s->cs) {
2052 h2s->cs->data_cb->send(h2s->cs);
2053 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002054 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002055 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002056 }
2057
2058 /* depending on callee's blocking reasons, we may queue in send
2059 * list or completely dequeue.
2060 */
2061 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2062 if (h2s->flags & H2_SF_BLK_ANY) {
2063 LIST_DEL(&h2s->list);
2064 LIST_ADDQ(&h2c->send_list, &h2s->list);
2065 }
2066 else {
2067 LIST_DEL(&h2s->list);
2068 LIST_INIT(&h2s->list);
2069 if (h2s->cs)
2070 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002071 else {
2072 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002073 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002074 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002075 }
2076 }
2077 }
2078
2079 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2080 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2081 break;
2082
2083 /* In theory it's possible that h2s->cs == NULL here :
2084 * - client sends crap that causes a parse error
2085 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2086 * - RST_STREAM cannot be emitted because mux is busy/full
2087 * - stream gets notified, detaches and quits
2088 * - mux buffer gets ready and wakes pending streams up
2089 * - bam!
2090 */
2091 h2s->flags &= ~H2_SF_BLK_ANY;
2092
2093 if (h2s->cs) {
2094 h2s->cs->data_cb->send(h2s->cs);
2095 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002096 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002097 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002098 }
2099 /* depending on callee's blocking reasons, we may queue in fctl
2100 * list or completely dequeue.
2101 */
2102 if (h2s->flags & H2_SF_BLK_MFCTL) {
2103 /* stream hit the connection's flow control */
2104 LIST_DEL(&h2s->list);
2105 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2106 }
2107 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2108 LIST_DEL(&h2s->list);
2109 LIST_INIT(&h2s->list);
2110 if (h2s->cs)
2111 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002112 else {
2113 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002114 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002115 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002116 }
2117 }
2118
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002119 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002120 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002121 if (h2c->st0 == H2_CS_ERROR) {
2122 if (h2c->max_id >= 0) {
2123 h2c_send_goaway_error(h2c, NULL);
2124 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2125 return 0;
2126 }
2127
2128 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2129 }
2130 return 1;
2131 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002132 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002133}
2134
Willy Tarreau71681172017-10-23 14:39:06 +02002135
Willy Tarreau62f52692017-10-08 23:01:42 +02002136/*********************************************************/
2137/* functions below are I/O callbacks from the connection */
2138/*********************************************************/
2139
2140/* callback called on recv event by the connection handler */
2141static void h2_recv(struct connection *conn)
2142{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002143 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002144 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002145 int max;
2146
Willy Tarreau315d8072017-12-10 22:17:57 +01002147 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002148 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002149
Willy Tarreau44e973f2018-03-01 17:49:30 +01002150 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002151 if (!buf) {
2152 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002153 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002154 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002155
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002156 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002157 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002158 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002159
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002160 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002161 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002162 return;
2163 }
2164
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002165 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002166 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002167 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002168}
2169
2170/* callback called on send event by the connection handler */
2171static void h2_send(struct connection *conn)
2172{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002173 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002174 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002175
2176 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002177 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002178
2179 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2180 /* a handshake was requested */
2181 return;
2182 }
2183
Willy Tarreaubc933932017-10-09 16:21:43 +02002184 /* This loop is quite simple : it tries to fill as much as it can from
2185 * pending streams into the existing buffer until it's reportedly full
2186 * or the end of send requests is reached. Then it tries to send this
2187 * buffer's contents out, marks it not full if at least one byte could
2188 * be sent, and tries again.
2189 *
2190 * The snd_buf() function normally takes a "flags" argument which may
2191 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2192 * data immediately comes and CO_SFL_STREAMER to indicate that the
2193 * connection is streaming lots of data (used to increase TLS record
2194 * size at the expense of latency). The former can be sent any time
2195 * there's a buffer full flag, as it indicates at least one stream
2196 * attempted to send and failed so there are pending data. An
2197 * alternative would be to set it as long as there's an active stream
2198 * but that would be problematic for ACKs until we have an absolute
2199 * guarantee that all waiters have at least one byte to send. The
2200 * latter should possibly not be set for now.
2201 */
2202
2203 done = 0;
2204 while (!done) {
2205 unsigned int flags = 0;
2206
2207 /* fill as much as we can into the current buffer */
2208 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2209 done = h2_process_mux(h2c);
2210
2211 if (conn->flags & CO_FL_ERROR)
2212 break;
2213
2214 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2215 flags |= CO_SFL_MSG_MORE;
2216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002217 if (b_data(&h2c->mbuf)) {
2218 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002219 if (!ret)
2220 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002221 b_del(&h2c->mbuf, ret);
2222 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002223 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002224
2225 /* wrote at least one byte, the buffer is not full anymore */
2226 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2227 }
2228
Willy Tarreaua2af5122017-10-09 11:56:46 +02002229 if (conn->flags & CO_FL_SOCK_WR_SH) {
2230 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002231 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002232 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002233 /* We're not full anymore, so we can wake any task that are waiting
2234 * for us.
2235 */
2236 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2237 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2238 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2239 struct wait_list *, list);
2240 LIST_DEL(&sw->list);
2241 LIST_INIT(&sw->list);
2242 tasklet_wakeup(sw->task);
2243 }
2244
2245 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002246}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002247
Willy Tarreau62f52692017-10-08 23:01:42 +02002248/* callback called on any event by the connection handler.
2249 * It applies changes and returns zero, or < 0 if it wants immediate
2250 * destruction of the connection (which normally doesn not happen in h2).
2251 */
2252static int h2_wake(struct connection *conn)
2253{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002254 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002255 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002256
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002257 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002258 h2_process_demux(h2c);
2259
2260 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002261 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002262
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002263 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002264 h2c->flags &= ~H2_CF_DEM_DFULL;
2265 }
2266
Willy Tarreau8ec14062017-12-30 18:08:13 +01002267 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2268 /* frontend is stopping, reload likely in progress, let's try
2269 * to announce a graceful shutdown if not yet done. We don't
2270 * care if it fails, it will be tried again later.
2271 */
2272 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2273 if (h2c->last_sid < 0)
2274 h2c->last_sid = (1U << 31) - 1;
2275 h2c_send_goaway_error(h2c, NULL);
2276 }
2277 }
2278
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002279 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002280 * If we received early data, and the handshake is done, wake
2281 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002282 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002283 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2284 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2285 struct eb32_node *node;
2286 struct h2s *h2s;
2287
2288 h2c->flags |= H2_CF_WAIT_FOR_HS;
2289 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2290
2291 while (node) {
2292 h2s = container_of(node, struct h2s, by_id);
2293 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2294 h2s->cs->data_cb->wake(h2s->cs);
2295 node = eb32_next(node);
2296 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002297 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002298
Willy Tarreau26bd7612017-10-09 16:47:04 +02002299 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002300 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2301 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2302 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002303 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002304
2305 if (eb_is_empty(&h2c->streams_by_id)) {
2306 /* no more stream, kill the connection now */
2307 h2_release(conn);
2308 return -1;
2309 }
2310 else {
2311 /* some streams still there, we need to signal them all and
2312 * wait for their departure.
2313 */
2314 __conn_xprt_stop_recv(conn);
2315 __conn_xprt_stop_send(conn);
2316 return 0;
2317 }
2318 }
2319
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002321 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002322
2323 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002324 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002325 __conn_xprt_stop_recv(conn);
2326 }
2327 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002328 __conn_xprt_want_recv(conn);
2329 }
2330
2331 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002332 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2333 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002334 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002335 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2336 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002337 __conn_xprt_want_send(conn);
2338 }
2339 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002340 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002341 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002342 }
2343
Willy Tarreau3f133572017-10-31 19:21:06 +01002344 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002345 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002346 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002347 task_queue(h2c->task);
2348 }
2349 else
2350 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002351 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002352 return 0;
2353}
2354
Willy Tarreauea392822017-10-31 10:02:25 +01002355/* Connection timeout management. The principle is that if there's no receipt
2356 * nor sending for a certain amount of time, the connection is closed. If the
2357 * MUX buffer still has lying data or is not allocatable, the connection is
2358 * immediately killed. If it's allocatable and empty, we attempt to send a
2359 * GOAWAY frame.
2360 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002361static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002362{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002363 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002364 int expired = tick_is_expired(t->expire, now_ms);
2365
Willy Tarreau0975f112018-03-29 15:22:59 +02002366 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002367 return t;
2368
Willy Tarreau0975f112018-03-29 15:22:59 +02002369 task_delete(t);
2370 task_free(t);
2371
2372 if (!h2c) {
2373 /* resources were already deleted */
2374 return NULL;
2375 }
2376
2377 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002378 h2c_error(h2c, H2_ERR_NO_ERROR);
2379 h2_wake_some_streams(h2c, 0, 0);
2380
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002381 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002382 /* don't even try to send a GOAWAY, the buffer is stuck */
2383 h2c->flags |= H2_CF_GOAWAY_FAILED;
2384 }
2385
2386 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002387 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002388 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2389 h2c->flags |= H2_CF_GOAWAY_FAILED;
2390
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002391 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2392 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002393 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002394 b_del(&h2c->mbuf, ret);
2395 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002396 }
2397 }
Willy Tarreauea392822017-10-31 10:02:25 +01002398
Willy Tarreau0975f112018-03-29 15:22:59 +02002399 /* either we can release everything now or it will be done later once
2400 * the last stream closes.
2401 */
2402 if (eb_is_empty(&h2c->streams_by_id))
2403 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002404
Willy Tarreauea392822017-10-31 10:02:25 +01002405 return NULL;
2406}
2407
2408
Willy Tarreau62f52692017-10-08 23:01:42 +02002409/*******************************************/
2410/* functions below are used by the streams */
2411/*******************************************/
2412
2413/*
2414 * Attach a new stream to a connection
2415 * (Used for outgoing connections)
2416 */
2417static struct conn_stream *h2_attach(struct connection *conn)
2418{
2419 return NULL;
2420}
2421
2422/* callback used to update the mux's polling flags after changing a cs' status.
2423 * The caller (cs_update_mux_polling) will take care of propagating any changes
2424 * to the transport layer.
2425 */
2426static void h2_update_poll(struct conn_stream *cs)
2427{
Willy Tarreau1d393222017-10-17 10:26:19 +02002428 struct h2s *h2s = cs->ctx;
2429
2430 if (!h2s)
2431 return;
2432
Willy Tarreaud7739c82017-10-30 15:38:23 +01002433 /* we may unblock a blocked read */
2434
Willy Tarreau315d8072017-12-10 22:17:57 +01002435 if (cs->flags & CS_FL_DATA_RD_ENA) {
2436 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002437 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002438 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002439 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002440 conn_xprt_want_send(cs->conn);
2441 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002442 }
2443
Willy Tarreau1d393222017-10-17 10:26:19 +02002444 /* Note: the stream and stream-int code doesn't allow us to perform a
2445 * synchronous send() here unfortunately, because this code is called
2446 * as si_update() from the process_stream() context. This means that
2447 * we have to queue the current cs and defer its processing after the
2448 * connection's cs list is processed anyway.
2449 */
2450
2451 if (cs->flags & CS_FL_DATA_WR_ENA) {
2452 if (LIST_ISEMPTY(&h2s->list)) {
2453 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002455 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2456 conn_xprt_want_send(cs->conn);
2457 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2458 }
2459 }
2460 else if (!LIST_ISEMPTY(&h2s->list)) {
2461 LIST_DEL(&h2s->list);
2462 LIST_INIT(&h2s->list);
2463 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2464 }
2465
2466 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002467 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002468 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002469}
2470
2471/*
2472 * Detach the stream from the connection and possibly release the connection.
2473 */
2474static void h2_detach(struct conn_stream *cs)
2475{
Willy Tarreau60935142017-10-16 18:11:19 +02002476 struct h2s *h2s = cs->ctx;
2477 struct h2c *h2c;
2478
2479 cs->ctx = NULL;
2480 if (!h2s)
2481 return;
2482
2483 h2c = h2s->h2c;
2484 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002485 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002486 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2487 !h2_has_too_many_cs(h2c)) {
2488 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2489 if (h2_recv_allowed(h2c)) {
2490 __conn_xprt_want_recv(h2c->conn);
2491 conn_xprt_want_send(h2c->conn);
2492 }
2493 }
Willy Tarreau60935142017-10-16 18:11:19 +02002494
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002495 /* this stream may be blocked waiting for some data to leave (possibly
2496 * an ES or RST frame), so orphan it in this case.
2497 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002498 if (!(cs->conn->flags & CO_FL_ERROR) &&
2499 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002500 return;
2501
Willy Tarreau45f752e2017-10-30 15:44:59 +01002502 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2503 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2504 /* unblock the connection if it was blocked on this
2505 * stream.
2506 */
2507 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2508 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2509 conn_xprt_want_recv(cs->conn);
2510 conn_xprt_want_send(cs->conn);
2511 }
2512
Willy Tarreau71049cc2018-03-28 13:56:39 +02002513 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002514
Willy Tarreaue323f342018-03-28 13:51:45 +02002515 /* We don't want to close right now unless we're removing the
2516 * last stream, and either the connection is in error, or it
2517 * reached the ID already specified in a GOAWAY frame received
2518 * or sent (as seen by last_sid >= 0).
2519 */
2520 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2521 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002522 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002523 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002525 (conn_xprt_read0_pending(h2c->conn) ||
2526 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2527 /* no more stream will come, kill it now */
2528 h2_release(h2c->conn);
2529 }
2530 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002531 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002532 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2533 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002534 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002535 else
2536 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002537 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002538}
2539
2540static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2541{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002542 struct h2s *h2s = cs->ctx;
2543
2544 if (!mode)
2545 return;
2546
Willy Tarreau721c9742017-11-07 11:05:42 +01002547 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002548 return;
2549
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002550 /* if no outgoing data was seen on this stream, it means it was
2551 * closed with a "tcp-request content" rule that is normally
2552 * used to kill the connection ASAP (eg: limit abuse). In this
2553 * case we send a goaway to close the connection.
2554 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002555 if (!(h2s->flags & H2_SF_RST_SENT) &&
2556 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002557 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002558
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002559 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2560 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2561 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002562 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002563
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002564 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002565 conn_xprt_want_send(cs->conn);
2566
Willy Tarreau00dd0782018-03-01 16:31:34 +01002567 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002568
2569 add_to_list:
2570 if (LIST_ISEMPTY(&h2s->list)) {
2571 if (h2s->flags & H2_SF_BLK_MFCTL)
2572 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2573 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2574 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2575 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002576}
2577
2578static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2579{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002580 struct h2s *h2s = cs->ctx;
2581
Willy Tarreau721c9742017-11-07 11:05:42 +01002582 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002583 return;
2584
Willy Tarreau67434202017-11-06 20:20:51 +01002585 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002586 /* we can cleanly close using an empty data frame only after headers */
2587
2588 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2589 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002590 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002591
2592 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002593 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002594 else
2595 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002596 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002597 /* if no outgoing data was seen on this stream, it means it was
2598 * closed with a "tcp-request content" rule that is normally
2599 * used to kill the connection ASAP (eg: limit abuse). In this
2600 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002601 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002602 if (!(h2s->flags & H2_SF_RST_SENT) &&
2603 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002604 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002605
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002606 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2607 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002608 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002609 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002610
Willy Tarreau00dd0782018-03-01 16:31:34 +01002611 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002612 }
2613
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002614 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002615 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002616
2617 add_to_list:
2618 if (LIST_ISEMPTY(&h2s->list)) {
2619 if (h2s->flags & H2_SF_BLK_MFCTL)
2620 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2621 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2622 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2623 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002624}
2625
Willy Tarreau13278b42017-10-13 19:23:14 +02002626/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2627 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2628 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002629 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002630 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002631static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02002632{
2633 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002634 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002635 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002636 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002637 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002638 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002639 int flen = h2c->dfl;
2640 int outlen = 0;
2641 int wrap;
2642 int try;
2643
2644 if (!h2c->dfl) {
2645 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002646 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002647 return 0;
2648 }
2649
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002650 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002651 return 0; // incomplete input frame
2652
Willy Tarreau13278b42017-10-13 19:23:14 +02002653 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002654 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002655 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002656 copy = alloc_trash_chunk();
2657 if (!copy) {
2658 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2659 goto fail;
2660 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2662 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2663 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002664 }
2665
2666 /* The padlen is the first byte before data, and the padding appears
2667 * after data. padlen+data+padding are included in flen.
2668 */
2669 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002670 h2c->dpl = *hdrs;
2671 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002672 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2673 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002674 return 0;
2675 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002676 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002677 hdrs += 1; // skip Pad Length
2678 }
2679
2680 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2681 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002682 if (read_n32(hdrs) == h2s->id) {
2683 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2684 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2685 return 0;//goto fail_stream;
2686 }
2687
Willy Tarreau13278b42017-10-13 19:23:14 +02002688 hdrs += 5; // stream dep = 4, weight = 1
2689 flen -= 5;
2690 }
2691
2692 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2693 * don't support this for now and can't even decompress so we have to
2694 * break the connection.
2695 */
2696 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2697 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002698 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002699 }
2700
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002701 /* we can't retry a failed decompression operation so we must be very
2702 * careful not to take any risks. In practice the output buffer is
2703 * always empty except maybe for trailers, so these operations almost
2704 * never happen.
2705 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002706 if (flags & CO_RFL_BUF_WET) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002707 /* need to let the output buffer flush and
2708 * mark the buffer for later wake up.
2709 */
2710 goto fail;
2711 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002712
Willy Tarreauaa7af722018-07-12 10:33:12 +02002713 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002714 /* it doesn't fit and the buffer is fragmented,
2715 * so let's defragment it and try again.
2716 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 b_slow_realign(buf, trash.area, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002718 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002719
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002720 try = b_contig_space(buf);
2721 if (!try)
2722 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002723
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002724 if (try > count)
2725 try = count;
2726
2727 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2728 sizeof(list)/sizeof(list[0]), tmp);
2729 if (outlen < 0) {
2730 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2731 goto fail;
2732 }
2733
2734 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002735 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02002736 outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002737
2738 if (outlen < 0) {
2739 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2740 goto fail;
2741 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002742
Willy Tarreau174b06a2018-04-25 18:13:58 +02002743 if (msgf & H2_MSGF_BODY) {
2744 /* a payload is present */
2745 if (msgf & H2_MSGF_BODY_CL)
2746 h2s->flags |= H2_SF_DATA_CLEN;
2747 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2748 h2s->flags |= H2_SF_DATA_CHNK;
2749 }
2750
Willy Tarreau13278b42017-10-13 19:23:14 +02002751 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002752 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002753 h2c->st0 = H2_CS_FRAME_H;
Olivier Houchardacd14032018-06-28 18:17:23 +02002754 b_add(buf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002755
2756 /* don't send it before returning data!
2757 * FIXME: should we instead try to send it much later, after the
2758 * response ? This would require that we keep a copy of it in h2s.
2759 */
2760 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2761 h2s->cs->flags |= CS_FL_EOS;
2762 h2s->flags |= H2_SF_ES_RCVD;
2763 }
2764
Willy Tarreau68dd9852017-07-03 14:44:26 +02002765 leave:
2766 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002767 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002768 fail:
2769 outlen = 0;
2770 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002771}
2772
Willy Tarreau454f9052017-10-26 19:40:35 +02002773/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2774 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2775 * in use, a new chunk is emitted for each frame. This is supposed to fit
2776 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2777 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2778 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2779 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002780 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2781 * checked to know if some data remain pending (an empty DATA frame can return
2782 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2783 * connection errors in h2c->errcode. The caller must already have checked the
2784 * frame header and ensured that the frame was complete or the buffer full. It
2785 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002786 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002787static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau454f9052017-10-26 19:40:35 +02002788{
2789 struct h2c *h2c = h2s->h2c;
2790 int block1, block2;
2791 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002792 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002793
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002794 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002795 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002796
2797 /* The padlen is the first byte before data, and the padding appears
2798 * after data. padlen+data+padding are included in flen.
2799 */
Willy Tarreau79127812017-12-03 21:06:59 +01002800 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002802 return 0;
2803
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002804 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002805 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002806 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2807 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002808 return 0;
2809 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002810
2811 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002812 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002813 h2c->dfl--;
2814 h2c->rcvd_c++; h2c->rcvd_s++;
2815 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002816 }
2817
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002818 flen = h2c->dfl - h2c->dpl;
2819 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002820 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002822 if (flen > b_data(&h2c->dbuf)) {
2823 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002824 if (!flen)
2825 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002826 }
2827
Willy Tarreaueba10f22018-04-25 20:44:22 +02002828 /* chunked-encoding requires more room */
2829 if (h2s->flags & H2_SF_DATA_CHNK) {
2830 chklen = MIN(flen, count);
2831 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2832 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2833 (chklen < 1048576) ? 4 : 8;
2834 chklen += 4; // CRLF, CRLF
2835 }
2836
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002837 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002838 if (flen + chklen > count) {
2839 if (chklen >= count)
2840 goto full;
2841 flen = count - chklen;
2842 }
2843
2844 if (h2s->flags & H2_SF_DATA_CHNK) {
2845 /* emit the chunk size */
2846 unsigned int chksz = flen;
2847 char str[10];
2848 char *beg;
2849
2850 beg = str + sizeof(str);
2851 *--beg = '\n';
2852 *--beg = '\r';
2853 do {
2854 *--beg = hextab[chksz & 0xF];
2855 } while (chksz >>= 4);
Willy Tarreau55372f62018-07-10 10:04:02 +02002856 b_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002857 }
2858
Willy Tarreau454f9052017-10-26 19:40:35 +02002859 /* Block1 is the length of the first block before the buffer wraps,
2860 * block2 is the optional second block to reach the end of the frame.
2861 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002862 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002863 if (block1 > flen)
2864 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002865 block2 = flen - block1;
2866
2867 if (block1)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002868 b_putblk(buf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002869
2870 if (block2)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871 b_putblk(buf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002872
Willy Tarreaueba10f22018-04-25 20:44:22 +02002873 if (h2s->flags & H2_SF_DATA_CHNK) {
2874 /* emit the CRLF */
Willy Tarreau55372f62018-07-10 10:04:02 +02002875 b_putblk(buf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002876 }
2877
Willy Tarreau454f9052017-10-26 19:40:35 +02002878 /* now mark the input data as consumed (will be deleted from the buffer
2879 * by the caller when seeing FRAME_A after sending the window update).
2880 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002881 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002882 h2c->dfl -= flen;
2883 h2c->rcvd_c += flen;
2884 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2885
2886 if (h2c->dfl > h2c->dpl) {
2887 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002888 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002889 }
2890
Willy Tarreau4a28da12018-01-04 14:41:00 +01002891 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002892 /* here we're done with the frame, all the payload (except padding) was
2893 * transferred.
2894 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002895
2896 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2897 /* emit the trailing 0 CRLF CRLF */
2898 if (count < 5)
2899 goto more;
2900 chklen += 5;
Willy Tarreau55372f62018-07-10 10:04:02 +02002901 b_putblk(buf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002902 }
2903
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002904 h2c->rcvd_c += h2c->dpl;
2905 h2c->rcvd_s += h2c->dpl;
2906 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002907 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2908
2909 /* don't send it before returning data!
2910 * FIXME: should we instead try to send it much later, after the
2911 * response ? This would require that we keep a copy of it in h2s.
2912 */
Willy Tarreau79127812017-12-03 21:06:59 +01002913 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002914 h2s->cs->flags |= CS_FL_EOS;
2915 h2s->flags |= H2_SF_ES_RCVD;
2916 }
2917
Willy Tarreaueba10f22018-04-25 20:44:22 +02002918 return flen + chklen;
2919 full:
2920 flen = chklen = 0;
2921 more:
2922 h2c->flags |= H2_CF_DEM_SFULL;
2923 h2s->cs->flags |= CS_FL_RCV_MORE;
2924 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002925}
2926
Willy Tarreau62f52692017-10-08 23:01:42 +02002927/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002928 * Called from the upper layer to get more data, up to <count> bytes. The
2929 * caller is responsible for never asking for more data than what is available
2930 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002931 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002932static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02002933{
Willy Tarreau13278b42017-10-13 19:23:14 +02002934 struct h2s *h2s = cs->ctx;
2935 struct h2c *h2c = h2s->h2c;
Willy Tarreaud9cf5402018-07-18 11:29:06 +02002936 size_t ret = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002937
2938 if (h2c->st0 != H2_CS_FRAME_P)
2939 return 0; // no pre-parsed frame yet
2940
2941 if (h2c->dsi != h2s->id)
2942 return 0; // not for us
2943
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002944 if (!b_size(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02002945 return 0; // empty buffer
2946
Willy Tarreau13278b42017-10-13 19:23:14 +02002947 switch (h2c->dft) {
2948 case H2_FT_HEADERS:
Willy Tarreau337ea572018-06-19 06:23:38 +02002949 ret = h2_frt_decode_headers(h2s, buf, count, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002950 break;
2951
Willy Tarreau454f9052017-10-26 19:40:35 +02002952 case H2_FT_DATA:
Willy Tarreau337ea572018-06-19 06:23:38 +02002953 ret = h2_frt_transfer_data(h2s, buf, count, flags);
Willy Tarreau454f9052017-10-26 19:40:35 +02002954 break;
2955
Willy Tarreau13278b42017-10-13 19:23:14 +02002956 default:
2957 ret = 0;
2958 }
2959 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002960}
2961
Willy Tarreau5dd17352018-06-14 13:33:30 +02002962/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2963 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2964 * number of bytes sent. The caller must check the stream's status to detect
2965 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002966 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002967static 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 +02002968{
2969 struct http_hdr list[MAX_HTTP_HDR];
2970 struct h2c *h2c = h2s->h2c;
2971 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002972 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002973 int es_now = 0;
2974 int ret = 0;
2975 int hdr;
2976
2977 if (h2c_mux_busy(h2c, h2s)) {
2978 h2s->flags |= H2_SF_BLK_MBUSY;
2979 return 0;
2980 }
2981
Willy Tarreau44e973f2018-03-01 17:49:30 +01002982 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002983 h2c->flags |= H2_CF_MUX_MALLOC;
2984 h2s->flags |= H2_SF_BLK_MROOM;
2985 return 0;
2986 }
2987
2988 /* First, try to parse the H1 response and index it into <list>.
2989 * NOTE! Since it comes from haproxy, we *know* that a response header
2990 * block does not wrap and we can safely read it this way without
2991 * having to realign the buffer.
2992 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02002993 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002994 list, sizeof(list)/sizeof(list[0]), h1m);
2995 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002996 /* incomplete or invalid response, this is abnormal coming from
2997 * haproxy and may only result in a bad errorfile or bad Lua code
2998 * so that won't be fixed, raise an error now.
2999 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003000 * FIXME: we should instead add the ability to only return a
3001 * 502 bad gateway. But in theory this is not supposed to
3002 * happen.
3003 */
3004 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3005 ret = 0;
3006 goto end;
3007 }
3008
3009 chunk_reset(&outbuf);
3010
3011 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003012 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003014 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003015
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003016 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003017 break;
3018 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003019 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003020 }
3021
3022 if (outbuf.size < 9) {
3023 h2c->flags |= H2_CF_MUX_MFULL;
3024 h2s->flags |= H2_SF_BLK_MROOM;
3025 ret = 0;
3026 goto end;
3027 }
3028
3029 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3031 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3032 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003033
3034 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003035 if (outbuf.data < outbuf.size && h1m->status == 200)
3036 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3037 else if (outbuf.data < outbuf.size && h1m->status == 304)
3038 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003039 else if (unlikely(list[0].v.len != 3)) {
3040 /* this is an unparsable response */
3041 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3042 ret = 0;
3043 goto end;
3044 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003045 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003046 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003047 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3048 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3049 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3050 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3051 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003052 }
3053 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003054 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003055 goto realign_again;
3056
3057 h2c->flags |= H2_CF_MUX_MFULL;
3058 h2s->flags |= H2_SF_BLK_MROOM;
3059 ret = 0;
3060 goto end;
3061 }
3062
3063 /* encode all headers, stop at empty name */
3064 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003065 /* these ones do not exist in H2 and must be dropped. */
3066 if (isteq(list[hdr].n, ist("connection")) ||
3067 isteq(list[hdr].n, ist("proxy-connection")) ||
3068 isteq(list[hdr].n, ist("keep-alive")) ||
3069 isteq(list[hdr].n, ist("upgrade")) ||
3070 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003071 continue;
3072
3073 if (isteq(list[hdr].n, ist("")))
3074 break; // end
3075
3076 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3077 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003078 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003079 goto realign_again;
3080
3081 h2c->flags |= H2_CF_MUX_MFULL;
3082 h2s->flags |= H2_SF_BLK_MROOM;
3083 ret = 0;
3084 goto end;
3085 }
3086 }
3087
3088 /* we may need to add END_STREAM */
3089 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3090 es_now = 1;
3091
3092 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003093 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094
3095 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003096 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003097
3098 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003099 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100
3101 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003102 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003103 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003104
3105 /* for now we don't implemented CONTINUATION, so we wait for a
3106 * body or directly end in TRL2.
3107 */
3108 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003109 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003110 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003111
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003112 h1m->state = HTTP_MSG_DONE;
3113 h2s->flags |= H2_SF_ES_SENT;
3114 if (h2s->st == H2_SS_OPEN)
3115 h2s->st = H2_SS_HLOC;
3116 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003117 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003118 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003119 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003120 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003121 h1m->state = HTTP_MSG_RPBEFORE;
3122 h1m->status = 0;
3123 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003124 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003125 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003126 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003127 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003128
3129 end:
3130 //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));
3131 return ret;
3132}
3133
Willy Tarreau5dd17352018-06-14 13:33:30 +02003134/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3135 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3136 * the number of bytes sent. The caller must check the stream's status to
3137 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003138 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003139static 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 +02003140{
3141 struct h2c *h2c = h2s->h2c;
3142 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003143 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003144 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003145 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003146 int es_now = 0;
3147 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003148 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003149 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003150
3151 if (h2c_mux_busy(h2c, h2s)) {
3152 h2s->flags |= H2_SF_BLK_MBUSY;
3153 goto end;
3154 }
3155
Willy Tarreau44e973f2018-03-01 17:49:30 +01003156 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003157 h2c->flags |= H2_CF_MUX_MALLOC;
3158 h2s->flags |= H2_SF_BLK_MROOM;
3159 goto end;
3160 }
3161
3162 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003163 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003164 goto end;
3165
3166 chunk_reset(&outbuf);
3167
3168 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003170 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003172
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003173 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003174 break;
3175 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003176 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003177 }
3178
3179 if (outbuf.size < 9) {
3180 h2c->flags |= H2_CF_MUX_MFULL;
3181 h2s->flags |= H2_SF_BLK_MROOM;
3182 goto end;
3183 }
3184
3185 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003186 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3187 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3188 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003189
3190 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3191 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003192 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003193 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003194 break;
3195 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003196 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003197 if ((long long)size > h1m->curr_len)
3198 size = h1m->curr_len;
3199 break;
3200 default: /* te:chunked : parse chunks */
3201 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003202 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003203 if (!ret)
3204 goto end;
3205
3206 if (ret < 0) {
3207 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3208 h1m->err_pos = ret;
3209 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3210 goto end;
3211 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003212 max -= ret;
3213 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003214 total += ret;
3215 h1m->state = HTTP_MSG_CHUNK_SIZE;
3216 }
3217
3218 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3219 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003220 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003221 if (!ret)
3222 goto end;
3223
3224 if (ret < 0) {
3225 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3226 h1m->err_pos = ret;
3227 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3228 goto end;
3229 }
3230
3231 size = chunk;
3232 h1m->curr_len = chunk;
3233 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003234 max -= ret;
3235 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003236 total += ret;
3237 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3238 if (!size)
3239 goto send_empty;
3240 }
3241
3242 /* in MSG_DATA state, continue below */
3243 size = h1m->curr_len;
3244 break;
3245 }
3246
3247 /* we have in <size> the exact number of bytes we need to copy from
3248 * the H1 buffer. We need to check this against the connection's and
3249 * the stream's send windows, and to ensure that this fits in the max
3250 * frame size and in the buffer's available space minus 9 bytes (for
3251 * the frame header). The connection's flow control is applied last so
3252 * that we can use a separate list of streams which are immediately
3253 * unblocked on window opening. Note: we don't implement padding.
3254 */
3255
Willy Tarreau5dd17352018-06-14 13:33:30 +02003256 if (size > max)
3257 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003258
3259 if (size > h2s->mws)
3260 size = h2s->mws;
3261
3262 if (size <= 0) {
3263 h2s->flags |= H2_SF_BLK_SFCTL;
3264 goto end;
3265 }
3266
3267 if (h2c->mfs && size > h2c->mfs)
3268 size = h2c->mfs;
3269
3270 if (size + 9 > outbuf.size) {
3271 /* we have an opportunity for enlarging the too small
3272 * available space, let's try.
3273 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003274 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003275 goto realign_again;
3276 size = outbuf.size - 9;
3277 }
3278
3279 if (size <= 0) {
3280 h2c->flags |= H2_CF_MUX_MFULL;
3281 h2s->flags |= H2_SF_BLK_MROOM;
3282 goto end;
3283 }
3284
3285 if (size > h2c->mws)
3286 size = h2c->mws;
3287
3288 if (size <= 0) {
3289 h2s->flags |= H2_SF_BLK_MFCTL;
3290 goto end;
3291 }
3292
3293 /* copy whatever we can */
3294 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003295 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003296 if (ret == 1)
3297 len2 = 0;
3298
3299 if (!ret || len1 + len2 < size) {
3300 /* FIXME: must normally never happen */
3301 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3302 goto end;
3303 }
3304
3305 /* limit len1/len2 to size */
3306 if (len1 + len2 > size) {
3307 int sub = len1 + len2 - size;
3308
3309 if (len2 > sub)
3310 len2 -= sub;
3311 else {
3312 sub -= len2;
3313 len2 = 0;
3314 len1 -= sub;
3315 }
3316 }
3317
3318 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003321 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003322
3323 send_empty:
3324 /* we may need to add END_STREAM */
3325 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3326 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003327 *
3328 * FIXME: what we do here is not correct because we send end_stream
3329 * before knowing if we'll have to send a HEADERS frame for the
3330 * trailers. More importantly we're not consuming the trailing CRLF
3331 * after the end of trailers, so it will be left to the caller to
3332 * eat it. The right way to do it would be to measure trailers here
3333 * and to send ES only if there are no trailers.
3334 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 */
3336 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3337 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3338 es_now = 1;
3339
3340 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003341 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342
3343 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003344 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003345
3346 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003347 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003348
3349 /* consume incoming H1 response */
3350 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003351 max -= size;
3352 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003353 total += size;
3354 h1m->curr_len -= size;
3355 h2s->mws -= size;
3356 h2c->mws -= size;
3357
3358 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3359 h1m->state = HTTP_MSG_CHUNK_CRLF;
3360 goto new_frame;
3361 }
3362 }
3363
3364 if (es_now) {
3365 if (h2s->st == H2_SS_OPEN)
3366 h2s->st = H2_SS_HLOC;
3367 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003368 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003369
Willy Tarreau35a62702018-02-27 15:37:25 +01003370 if (!(h1m->flags & H1_MF_CHNK)) {
3371 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003372 total += max;
3373 ofs += max;
3374 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003375
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003376 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003377 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003378
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003379 h2s->flags |= H2_SF_ES_SENT;
3380 }
3381
3382 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003383 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 +02003384 return total;
3385}
3386
Olivier Houchard6ff20392018-07-17 18:46:31 +02003387/* Called from the upper layer, to subscribe to events, such as being able to send */
3388static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3389{
3390 struct wait_list *sw;
3391 struct h2s *h2s = cs->ctx;
3392
3393 switch (event_type) {
3394 case SUB_CAN_SEND:
3395 sw = param;
3396 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3397 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3398 return 0;
3399 default:
3400 break;
3401 }
3402 return -1;
3403
3404
3405}
3406
Willy Tarreau62f52692017-10-08 23:01:42 +02003407/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003408static 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 +02003409{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003410 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003411 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003412 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003413
Willy Tarreau0bad0432018-06-14 16:54:01 +02003414 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003415 h2s->flags |= H2_SF_OUTGOING_DATA;
3416
Willy Tarreau0bad0432018-06-14 16:54:01 +02003417 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003418 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003419 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003420 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003421 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003422 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003423 }
3424 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3425 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003426 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003427
Willy Tarreau5dd17352018-06-14 13:33:30 +02003428 if (unlikely((int)ret <= 0)) {
3429 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003430 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3431 break;
3432 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003433 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003434 total += count;
3435 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003436 h2s->res.state = HTTP_MSG_DONE;
3437 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003438 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003439 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003440 cs->flags |= CS_FL_ERROR;
3441 break;
3442 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003443
3444 total += ret;
3445 count -= ret;
3446
3447 if (h2s->st >= H2_SS_ERROR)
3448 break;
3449
3450 if (h2s->flags & H2_SF_BLK_ANY)
3451 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003452 }
3453
Willy Tarreau00610962018-07-19 10:58:28 +02003454 if (h2s->st >= H2_SS_ERROR) {
3455 /* trim any possibly pending data after we close (extra CR-LF,
3456 * unprocessed trailers, abnormal extra data, ...)
3457 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003458 total += count;
3459 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003460 }
3461
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003462 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003463 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003464 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003465 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003466 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003467 }
3468
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003469 if (h2s->flags & H2_SF_BLK_SFCTL) {
3470 /* stream flow control, quit the list */
3471 LIST_DEL(&h2s->list);
3472 LIST_INIT(&h2s->list);
3473 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003474 else if (LIST_ISEMPTY(&h2s->list)) {
3475 if (h2s->flags & H2_SF_BLK_MFCTL)
3476 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3477 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3478 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3479 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003480
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003481 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003482}
3483
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003484/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003485static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003486{
3487 struct h2c *h2c = conn->mux_ctx;
3488 struct h2s *h2s;
3489 struct eb32_node *node;
3490 int fctl_cnt = 0;
3491 int send_cnt = 0;
3492 int tree_cnt = 0;
3493 int orph_cnt = 0;
3494
3495 if (!h2c)
3496 return;
3497
3498 list_for_each_entry(h2s, &h2c->fctl_list, list)
3499 fctl_cnt++;
3500
3501 list_for_each_entry(h2s, &h2c->send_list, list)
3502 send_cnt++;
3503
3504 node = eb32_first(&h2c->streams_by_id);
3505 while (node) {
3506 h2s = container_of(node, struct h2s, by_id);
3507 tree_cnt++;
3508 if (!h2s->cs)
3509 orph_cnt++;
3510 node = eb32_next(node);
3511 }
3512
Willy Tarreauc65edac2018-07-19 10:54:43 +02003513 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 +02003514 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 +02003515}
Willy Tarreau62f52692017-10-08 23:01:42 +02003516
3517/*******************************************************/
3518/* functions below are dedicated to the config parsers */
3519/*******************************************************/
3520
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003521/* config parser for global "tune.h2.header-table-size" */
3522static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3523 struct proxy *defpx, const char *file, int line,
3524 char **err)
3525{
3526 if (too_many_args(1, args, err, NULL))
3527 return -1;
3528
3529 h2_settings_header_table_size = atoi(args[1]);
3530 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3531 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3532 return -1;
3533 }
3534 return 0;
3535}
Willy Tarreau62f52692017-10-08 23:01:42 +02003536
Willy Tarreaue6baec02017-07-27 11:45:11 +02003537/* config parser for global "tune.h2.initial-window-size" */
3538static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3539 struct proxy *defpx, const char *file, int line,
3540 char **err)
3541{
3542 if (too_many_args(1, args, err, NULL))
3543 return -1;
3544
3545 h2_settings_initial_window_size = atoi(args[1]);
3546 if (h2_settings_initial_window_size < 0) {
3547 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3548 return -1;
3549 }
3550 return 0;
3551}
3552
Willy Tarreau5242ef82017-07-27 11:47:28 +02003553/* config parser for global "tune.h2.max-concurrent-streams" */
3554static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3555 struct proxy *defpx, const char *file, int line,
3556 char **err)
3557{
3558 if (too_many_args(1, args, err, NULL))
3559 return -1;
3560
3561 h2_settings_max_concurrent_streams = atoi(args[1]);
3562 if (h2_settings_max_concurrent_streams < 0) {
3563 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3564 return -1;
3565 }
3566 return 0;
3567}
3568
Willy Tarreau62f52692017-10-08 23:01:42 +02003569
3570/****************************************/
3571/* MUX initialization and instanciation */
3572/***************************************/
3573
3574/* The mux operations */
3575const struct mux_ops h2_ops = {
3576 .init = h2_init,
3577 .recv = h2_recv,
3578 .send = h2_send,
3579 .wake = h2_wake,
3580 .update_poll = h2_update_poll,
3581 .rcv_buf = h2_rcv_buf,
3582 .snd_buf = h2_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003583 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003584 .attach = h2_attach,
3585 .detach = h2_detach,
3586 .shutr = h2_shutr,
3587 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003588 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003589 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003590 .name = "H2",
3591};
3592
3593/* ALPN selection : this mux registers ALPN tolen "h2" */
3594static struct alpn_mux_list alpn_mux_h2 =
3595 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3596
3597/* config keyword parsers */
3598static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003599 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003600 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003601 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003602 { 0, NULL, NULL }
3603}};
3604
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003605static void __h2_deinit(void)
3606{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003607 pool_destroy(pool_head_h2s);
3608 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003609}
3610
Willy Tarreau62f52692017-10-08 23:01:42 +02003611__attribute__((constructor))
3612static void __h2_init(void)
3613{
3614 alpn_register_mux(&alpn_mux_h2);
3615 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003616 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003617 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3618 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003619}