blob: 4ee20b492aba4b161645d6572af4ad05f77d0e49 [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 Tarreau35dbd5d2017-09-22 09:13:49 +020020#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010057#define H2_CF_DEM_BLOCK_ANY 0x000000F0 // 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 */
60#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
62
63
Willy Tarreau5ab6b572017-09-22 08:05:00 +020064/* H2 connection state, in h2c->st0 */
65enum h2_cs {
66 H2_CS_PREFACE, // init done, waiting for connection preface
67 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
68 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
69 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
70 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
71 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
91 struct buffer *dbuf; /* demux buffer */
92
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010097 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
98 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020099 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
100
101 /* states for the mux direction */
102 struct buffer *mbuf; /* mux buffer */
103 int32_t msi; /* mux stream ID (<0 = idle) */
104 int32_t mfl; /* mux frame length (if dsi >= 0) */
105 int8_t mft; /* mux frame type (if dsi >= 0) */
106 int8_t mff; /* mux frame flags (if dsi >= 0) */
107 /* 16 bit hole here */
108 int32_t miw; /* mux initial window size for all new streams */
109 int32_t mws; /* mux window size. Can be negative. */
110 int32_t mfs; /* mux's max frame size */
111
Willy Tarreauea392822017-10-31 10:02:25 +0100112 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100113 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100114 unsigned int nb_streams; /* number of streams in the tree */
115 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100116 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200117 struct eb_root streams_by_id; /* all active streams by their ID */
118 struct list send_list; /* list of blocked streams requesting to send */
119 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200120 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200121 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200122};
123
Willy Tarreau18312642017-10-11 07:57:07 +0200124/* H2 stream state, in h2s->st */
125enum h2_ss {
126 H2_SS_IDLE = 0, // idle
127 H2_SS_RLOC, // reserved(local)
128 H2_SS_RREM, // reserved(remote)
129 H2_SS_OPEN, // open
130 H2_SS_HREM, // half-closed(remote)
131 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200132 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200133 H2_SS_CLOSED, // closed
134 H2_SS_ENTRIES // must be last
135} __attribute__((packed));
136
137/* HTTP/2 stream flags (32 bit), in h2s->flags */
138#define H2_SF_NONE 0x00000000
139#define H2_SF_ES_RCVD 0x00000001
140#define H2_SF_ES_SENT 0x00000002
141
142#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
143#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
144
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200145/* stream flags indicating the reason the stream is blocked */
146#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
147#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
148#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
149#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
150#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
151
Willy Tarreau454f9052017-10-26 19:40:35 +0200152/* stream flags indicating how data is supposed to be sent */
153#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
154#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
155
156/* step we're currently in when sending chunks. This is needed because we may
157 * have to transfer chunks as large as a full buffer so there's no room left
158 * for size nor crlf around.
159 */
160#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
161#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
162#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
163
164#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
165
Willy Tarreau67434202017-11-06 20:20:51 +0100166#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100167#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100168
Willy Tarreau18312642017-10-11 07:57:07 +0200169/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
170 * it is being processed in the internal HTTP representation (H1 for now).
171 */
172struct h2s {
173 struct conn_stream *cs;
174 struct h2c *h2c;
175 struct h1m req, res; /* request and response parser state for H1 */
176 struct eb32_node by_id; /* place in h2c's streams_by_id */
177 struct list list; /* position in active/blocked lists if blocked>0 */
178 int32_t id; /* stream ID */
179 uint32_t flags; /* H2_SF_* */
180 int mws; /* mux window size for this stream */
181 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
182 enum h2_ss st;
183};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200184
Willy Tarreauc6405142017-09-21 20:23:50 +0200185/* descriptor for an h2 frame header */
186struct h2_fh {
187 uint32_t len; /* length, host order, 24 bits */
188 uint32_t sid; /* stream id, host order, 31 bits */
189 uint8_t ft; /* frame type */
190 uint8_t ff; /* frame flags */
191};
192
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200193/* a few settings from the global section */
194static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200195static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200196static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197
Willy Tarreau2a856182017-05-16 15:20:39 +0200198/* a dmumy closed stream */
199static const struct h2s *h2_closed_stream = &(const struct h2s){
200 .cs = NULL,
201 .h2c = NULL,
202 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100203 .errcode = H2_ERR_STREAM_CLOSED,
204 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200205 .id = 0,
206};
207
208/* and a dummy idle stream for use with any unannounced stream */
209static const struct h2s *h2_idle_stream = &(const struct h2s){
210 .cs = NULL,
211 .h2c = NULL,
212 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100213 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200214 .id = 0,
215};
216
Willy Tarreauea392822017-10-31 10:02:25 +0100217static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200218
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200219/*****************************************************/
220/* functions below are for dynamic buffer management */
221/*****************************************************/
222
Willy Tarreau315d8072017-12-10 22:17:57 +0100223/* indicates whether or not the we may call the h2_recv() function to attempt
224 * to receive data into the buffer and/or demux pending data. The condition is
225 * a bit complex due to some API limits for now. The rules are the following :
226 * - if an error or a shutdown was detected on the connection and the buffer
227 * is empty, we must not attempt to receive
228 * - if the demux buf failed to be allocated, we must not try to receive and
229 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100230 * - if no flag indicates a blocking condition, we may attempt to receive,
231 * regardless of whether the demux buffer is full or not, so that only
232 * de demux part decides whether or not to block. This is needed because
233 * the connection API indeed prevents us from re-enabling receipt that is
234 * already enabled in a polled state, so we must always immediately stop
235 * as soon as the demux can't proceed so as never to hit an end of read
236 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100237 * - otherwise must may not attempt
238 */
239static inline int h2_recv_allowed(const struct h2c *h2c)
240{
241 if (h2c->dbuf->i == 0 &&
242 (h2c->st0 >= H2_CS_ERROR ||
243 h2c->conn->flags & CO_FL_ERROR ||
244 conn_xprt_read0_pending(h2c->conn)))
245 return 0;
246
247 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100248 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100249 return 1;
250
251 return 0;
252}
253
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200254/* re-enables receiving on mux <target> after a buffer was allocated. It returns
255 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
256 * if it's impossible to wake up and we prefer to be woken up later.
257 */
258static int h2_dbuf_available(void *target)
259{
260 struct h2c *h2c = target;
261
262 /* take the buffer now as we'll get scheduled waiting for ->wake() */
263 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200264 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100265 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200266 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200267 return 1;
268 }
269 return 0;
270}
271
272static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
273{
274 struct buffer *buf = NULL;
275
276 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
277 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
278 h2c->dbuf_wait.target = h2c->conn;
279 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100280 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100282 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283 __conn_xprt_stop_recv(h2c->conn);
284 }
285 return buf;
286}
287
288static inline void h2_release_dbuf(struct h2c *h2c)
289{
290 if (h2c->dbuf->size) {
291 b_free(&h2c->dbuf);
292 offer_buffers(h2c->dbuf_wait.target,
293 tasks_run_queue + applets_active_queue);
294 }
295}
296
Willy Tarreau14398122017-09-22 14:26:04 +0200297/* re-enables sending on mux <target> after a buffer was allocated. It returns
298 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
299 * if it's impossible to wake up and we prefer to be woken up later.
300 */
301static int h2_mbuf_available(void *target)
302{
303 struct h2c *h2c = target;
304
305 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
306 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200307 if (h2c->flags & H2_CF_MUX_MALLOC) {
308 h2c->flags &= ~H2_CF_MUX_MALLOC;
309 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
310 conn_xprt_want_send(h2c->conn);
311 }
312
313 if (h2c->flags & H2_CF_DEM_MROOM) {
314 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100315 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200316 conn_xprt_want_recv(h2c->conn);
317 }
318
Willy Tarreau14398122017-09-22 14:26:04 +0200319 /* FIXME: we should in fact call something like h2_update_poll()
320 * now to recompte the polling. For now it will be enough like
321 * this.
322 */
Willy Tarreau14398122017-09-22 14:26:04 +0200323 return 1;
324 }
325 return 0;
326}
327
328static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
329{
330 struct buffer *buf = NULL;
331
332 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
333 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
334 h2c->mbuf_wait.target = h2c;
335 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100336 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200337 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200339
340 /* FIXME: we should in fact only block the direction being
341 * currently used. For now it will be enough like this.
342 */
343 __conn_xprt_stop_send(h2c->conn);
344 __conn_xprt_stop_recv(h2c->conn);
345 }
346 return buf;
347}
348
349static inline void h2_release_mbuf(struct h2c *h2c)
350{
351 if (h2c->mbuf->size) {
352 b_free(&h2c->mbuf);
353 offer_buffers(h2c->mbuf_wait.target,
354 tasks_run_queue + applets_active_queue);
355 }
356}
357
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358
Willy Tarreau62f52692017-10-08 23:01:42 +0200359/*****************************************************************/
360/* functions below are dedicated to the mux setup and management */
361/*****************************************************************/
362
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
364static int h2c_frt_init(struct connection *conn)
365{
366 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100367 struct task *t = NULL;
368 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200369
Willy Tarreaubafbe012017-11-24 17:34:44 +0100370 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371 if (!h2c)
372 goto fail;
373
Willy Tarreau3f133572017-10-31 19:21:06 +0100374
Willy Tarreau599391a2017-11-24 10:16:00 +0100375 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
376 if (tick_isset(sess->fe->timeout.clientfin))
377 h2c->shut_timeout = sess->fe->timeout.clientfin;
378
Willy Tarreau33400292017-11-05 11:23:40 +0100379 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100380 if (tick_isset(h2c->timeout)) {
381 t = task_new(tid_bit);
382 if (!t)
383 goto fail;
384
385 h2c->task = t;
386 t->process = h2_timeout_task;
387 t->context = h2c;
388 t->expire = tick_add(now_ms, h2c->timeout);
389 }
Willy Tarreauea392822017-10-31 10:02:25 +0100390
Willy Tarreau32218eb2017-09-22 08:07:25 +0200391 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
392 if (!h2c->ddht)
393 goto fail;
394
395 /* Initialise the context. */
396 h2c->st0 = H2_CS_PREFACE;
397 h2c->conn = conn;
398 h2c->max_id = -1;
399 h2c->errcode = H2_ERR_NO_ERROR;
400 h2c->flags = H2_CF_NONE;
401 h2c->rcvd_c = 0;
402 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100403 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404
405 h2c->dbuf = &buf_empty;
406 h2c->dsi = -1;
407 h2c->msi = -1;
408 h2c->last_sid = -1;
409
410 h2c->mbuf = &buf_empty;
411 h2c->miw = 65535; /* mux initial window size */
412 h2c->mws = 65535; /* mux window size */
413 h2c->mfs = 16384; /* initial max frame size */
414 h2c->streams_by_id = EB_ROOT_UNIQUE;
415 LIST_INIT(&h2c->send_list);
416 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200417 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200418 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 conn->mux_ctx = h2c;
420
Willy Tarreau3f133572017-10-31 19:21:06 +0100421 if (t)
422 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200423 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100424
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 /* mux->wake will be called soon to complete the operation */
426 return 0;
427 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100428 if (t)
429 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100430 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 return -1;
432}
433
Willy Tarreau62f52692017-10-08 23:01:42 +0200434/* Initialize the mux once it's attached. For outgoing connections, the context
435 * is already initialized before installing the mux, so we detect incoming
436 * connections from the fact that the context is still NULL. Returns < 0 on
437 * error.
438 */
439static int h2_init(struct connection *conn)
440{
441 if (conn->mux_ctx) {
442 /* we don't support outgoing connections for now */
443 return -1;
444 }
445
Willy Tarreau32218eb2017-09-22 08:07:25 +0200446 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200447}
448
Willy Tarreau2373acc2017-10-12 17:35:14 +0200449/* returns the stream associated with id <id> or NULL if not found */
450static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
451{
452 struct eb32_node *node;
453
Willy Tarreau2a856182017-05-16 15:20:39 +0200454 if (id > h2c->max_id)
455 return (struct h2s *)h2_idle_stream;
456
Willy Tarreau2373acc2017-10-12 17:35:14 +0200457 node = eb32_lookup(&h2c->streams_by_id, id);
458 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200459 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200460
461 return container_of(node, struct h2s, by_id);
462}
463
Willy Tarreau62f52692017-10-08 23:01:42 +0200464/* release function for a connection. This one should be called to free all
465 * resources allocated to the mux.
466 */
467static void h2_release(struct connection *conn)
468{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200469 struct h2c *h2c = conn->mux_ctx;
470
471 LIST_DEL(&conn->list);
472
473 if (h2c) {
474 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200475 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100476 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200477 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200479
480 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100481 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200482 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100483 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200484
Willy Tarreauea392822017-10-31 10:02:25 +0100485 if (h2c->task) {
486 task_delete(h2c->task);
487 task_free(h2c->task);
488 h2c->task = NULL;
489 }
490
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200492 }
493
494 conn->mux = NULL;
495 conn->mux_ctx = NULL;
496
497 conn_stop_tracking(conn);
498 conn_full_close(conn);
499 if (conn->destroy_cb)
500 conn->destroy_cb(conn);
501 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200502}
503
504
Willy Tarreau71681172017-10-23 14:39:06 +0200505/******************************************************/
506/* functions below are for the H2 protocol processing */
507/******************************************************/
508
509/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100510static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200511{
512 return h2s ? h2s->id : 0;
513}
514
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200515/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100516static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200517{
518 if (h2c->msi < 0)
519 return 0;
520
521 if (h2c->msi == h2s_id(h2s))
522 return 0;
523
524 return 1;
525}
526
Willy Tarreau741d6df2017-10-17 08:00:59 +0200527/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100528static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200529{
530 h2c->errcode = err;
531 h2c->st0 = H2_CS_ERROR;
532}
533
Willy Tarreau2e43f082017-10-17 08:03:59 +0200534/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100535static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200536{
537 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
538 h2s->errcode = err;
539 h2s->st = H2_SS_ERROR;
540 if (h2s->cs)
541 h2s->cs->flags |= CS_FL_ERROR;
542 }
543}
544
Willy Tarreaue4820742017-07-27 13:37:23 +0200545/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100546static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200547{
548 uint8_t *out = frame;
549
550 *out = len >> 16;
551 write_n16(out + 1, len);
552}
553
Willy Tarreau54c15062017-10-10 17:10:03 +0200554/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
555 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
556 * the caller's responsibility to verify that there are at least <bytes> bytes
557 * available in the buffer's input prior to calling this function.
558 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100559static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200560 const struct buffer *b, int o)
561{
562 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
563}
564
Willy Tarreau1f094672017-11-20 21:27:45 +0100565static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200566{
567 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
568}
569
Willy Tarreau1f094672017-11-20 21:27:45 +0100570static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200571{
572 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
573}
574
Willy Tarreau1f094672017-11-20 21:27:45 +0100575static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200576{
577 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
578}
579
580
Willy Tarreau715d5312017-07-11 15:20:24 +0200581/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
582 * is not obvious. It turns out that H2 headers are neither aligned nor do they
583 * use regular sizes. And to add to the trouble, the buffer may wrap so each
584 * byte read must be checked. The header is formed like this :
585 *
586 * b0 b1 b2 b3 b4 b5..b8
587 * +----------+---------+--------+----+----+----------------------+
588 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
589 * +----------+---------+--------+----+----+----------------------+
590 *
591 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
592 * we get the sid properly aligned and ordered, and 16 bits of len properly
593 * ordered as well. The type and flags can be extracted using bit shifts from
594 * the word, and only one extra read is needed to fetch len[16:23].
595 * Returns zero if some bytes are missing, otherwise non-zero on success.
596 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100597static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200598{
599 uint64_t w;
600
601 if (b->i < 9)
602 return 0;
603
604 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
605 h->len = *b->p << 16;
606 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
607 h->ff = w >> 32;
608 h->ft = w >> 40;
609 h->len += w >> 48;
610 return 1;
611}
612
613/* skip the next 9 bytes corresponding to the frame header possibly parsed by
614 * h2_peek_frame_hdr() above.
615 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100616static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200617{
618 bi_del(b, 9);
619}
620
621/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100622static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200623{
624 int ret;
625
626 ret = h2_peek_frame_hdr(b, h);
627 if (ret > 0)
628 h2_skip_frame_hdr(b);
629 return ret;
630}
631
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100632/* marks stream <h2s> as CLOSED for connection <h2c> and decrement the number
633 * of active streams for this connection if the stream was not yet closed.
634 * Please use this exclusively before closing a stream to ensure stream count
635 * is well maintained.
636 */
637static inline void h2c_stream_close(struct h2c *h2c, struct h2s *h2s)
638{
639 if (h2s->st != H2_SS_CLOSED)
640 h2s->h2c->nb_streams--;
641 h2s->st = H2_SS_CLOSED;
642}
643
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200644/* creates a new stream <id> on the h2c connection and returns it, or NULL in
645 * case of memory allocation error.
646 */
647static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
648{
649 struct conn_stream *cs;
650 struct h2s *h2s;
651
Willy Tarreaubafbe012017-11-24 17:34:44 +0100652 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200653 if (!h2s)
654 goto out;
655
656 h2s->h2c = h2c;
657 h2s->mws = h2c->miw;
658 h2s->flags = H2_SF_NONE;
659 h2s->errcode = H2_ERR_NO_ERROR;
660 h2s->st = H2_SS_IDLE;
661 h1m_init(&h2s->req);
662 h1m_init(&h2s->res);
663 h2s->by_id.key = h2s->id = id;
664 h2c->max_id = id;
665 LIST_INIT(&h2s->list);
666
667 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100668 h2c->nb_streams++;
669 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
670 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200671
672 cs = cs_new(h2c->conn);
673 if (!cs)
674 goto out_close;
675
676 h2s->cs = cs;
677 cs->ctx = h2s;
678
679 if (stream_create_from_cs(cs) < 0)
680 goto out_free_cs;
681
682 /* OK done, the stream lives its own life now */
683 return h2s;
684
685 out_free_cs:
686 cs_free(cs);
687 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100688 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200689 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100690 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200691 h2s = NULL;
692 out:
693 return h2s;
694}
695
Willy Tarreaube5b7152017-09-25 16:25:39 +0200696/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
697 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
698 * the various settings codes.
699 */
700static int h2c_snd_settings(struct h2c *h2c)
701{
702 struct buffer *res;
703 char buf_data[100]; // enough for 15 settings
704 struct chunk buf;
705 int ret;
706
707 if (h2c_mux_busy(h2c, NULL)) {
708 h2c->flags |= H2_CF_DEM_MBUSY;
709 return 0;
710 }
711
712 res = h2_get_mbuf(h2c);
713 if (!res) {
714 h2c->flags |= H2_CF_MUX_MALLOC;
715 h2c->flags |= H2_CF_DEM_MROOM;
716 return 0;
717 }
718
719 chunk_init(&buf, buf_data, sizeof(buf_data));
720 chunk_memcpy(&buf,
721 "\x00\x00\x00" /* length : 0 for now */
722 "\x04\x00" /* type : 4 (settings), flags : 0 */
723 "\x00\x00\x00\x00", /* stream ID : 0 */
724 9);
725
726 if (h2_settings_header_table_size != 4096) {
727 char str[6] = "\x00\x01"; /* header_table_size */
728
729 write_n32(str + 2, h2_settings_header_table_size);
730 chunk_memcat(&buf, str, 6);
731 }
732
733 if (h2_settings_initial_window_size != 65535) {
734 char str[6] = "\x00\x04"; /* initial_window_size */
735
736 write_n32(str + 2, h2_settings_initial_window_size);
737 chunk_memcat(&buf, str, 6);
738 }
739
740 if (h2_settings_max_concurrent_streams != 0) {
741 char str[6] = "\x00\x03"; /* max_concurrent_streams */
742
743 /* Note: 0 means "unlimited" for haproxy's config but not for
744 * the protocol, so never send this value!
745 */
746 write_n32(str + 2, h2_settings_max_concurrent_streams);
747 chunk_memcat(&buf, str, 6);
748 }
749
750 if (global.tune.bufsize != 16384) {
751 char str[6] = "\x00\x05"; /* max_frame_size */
752
753 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
754 * match bufsize - rewrite size, but at the moment it seems
755 * that clients don't take care of it.
756 */
757 write_n32(str + 2, global.tune.bufsize);
758 chunk_memcat(&buf, str, 6);
759 }
760
761 h2_set_frame_size(buf.str, buf.len - 9);
762 ret = bo_istput(res, ist2(buf.str, buf.len));
763 if (unlikely(ret <= 0)) {
764 if (!ret) {
765 h2c->flags |= H2_CF_MUX_MFULL;
766 h2c->flags |= H2_CF_DEM_MROOM;
767 return 0;
768 }
769 else {
770 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
771 return 0;
772 }
773 }
774 return ret;
775}
776
Willy Tarreau52eed752017-09-22 15:05:09 +0200777/* Try to receive a connection preface, then upon success try to send our
778 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
779 * missing data. It may return an error in h2c.
780 */
781static int h2c_frt_recv_preface(struct h2c *h2c)
782{
783 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200784 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200785
786 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
787
788 if (unlikely(ret1 <= 0)) {
789 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
790 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
791 return 0;
792 }
793
Willy Tarreaube5b7152017-09-25 16:25:39 +0200794 ret2 = h2c_snd_settings(h2c);
795 if (ret2 > 0)
796 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200797
Willy Tarreaube5b7152017-09-25 16:25:39 +0200798 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200799}
800
Willy Tarreau081d4722017-05-16 21:51:05 +0200801/* try to send a GOAWAY frame on the connection to report an error or a graceful
802 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
803 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
804 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
805 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
806 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
807 * on unrecoverable failure. It will not attempt to send one again in this last
808 * case so that it is safe to use h2c_error() to report such errors.
809 */
810static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
811{
812 struct buffer *res;
813 char str[17];
814 int ret;
815
816 if (h2c->flags & H2_CF_GOAWAY_FAILED)
817 return 1; // claim that it worked
818
819 if (h2c_mux_busy(h2c, h2s)) {
820 if (h2s)
821 h2s->flags |= H2_SF_BLK_MBUSY;
822 else
823 h2c->flags |= H2_CF_DEM_MBUSY;
824 return 0;
825 }
826
827 res = h2_get_mbuf(h2c);
828 if (!res) {
829 h2c->flags |= H2_CF_MUX_MALLOC;
830 if (h2s)
831 h2s->flags |= H2_SF_BLK_MROOM;
832 else
833 h2c->flags |= H2_CF_DEM_MROOM;
834 return 0;
835 }
836
837 /* len: 8, type: 7, flags: none, sid: 0 */
838 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
839
840 if (h2c->last_sid < 0)
841 h2c->last_sid = h2c->max_id;
842
843 write_n32(str + 9, h2c->last_sid);
844 write_n32(str + 13, h2c->errcode);
845 ret = bo_istput(res, ist2(str, 17));
846 if (unlikely(ret <= 0)) {
847 if (!ret) {
848 h2c->flags |= H2_CF_MUX_MFULL;
849 if (h2s)
850 h2s->flags |= H2_SF_BLK_MROOM;
851 else
852 h2c->flags |= H2_CF_DEM_MROOM;
853 return 0;
854 }
855 else {
856 /* we cannot report this error using GOAWAY, so we mark
857 * it and claim a success.
858 */
859 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
860 h2c->flags |= H2_CF_GOAWAY_FAILED;
861 return 1;
862 }
863 }
864 h2c->flags |= H2_CF_GOAWAY_SENT;
865 return ret;
866}
867
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100868/* Try to send an RST_STREAM frame on the connection for the indicated stream
869 * during mux operations. This stream must be valid and cannot be closed
870 * already. h2s->id will be used for the stream ID and h2s->errcode will be
871 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
872 * not yet.
873 *
874 * Returns > 0 on success or zero if nothing was done. In case of lack of room
875 * to write the message, it subscribes the stream to future notifications.
876 */
877static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
878{
879 struct buffer *res;
880 char str[13];
881 int ret;
882
883 if (!h2s || h2s->st == H2_SS_CLOSED)
884 return 1;
885
886 if (h2c_mux_busy(h2c, h2s)) {
887 h2s->flags |= H2_SF_BLK_MBUSY;
888 return 0;
889 }
890
891 res = h2_get_mbuf(h2c);
892 if (!res) {
893 h2c->flags |= H2_CF_MUX_MALLOC;
894 h2s->flags |= H2_SF_BLK_MROOM;
895 return 0;
896 }
897
898 /* len: 4, type: 3, flags: none */
899 memcpy(str, "\x00\x00\x04\x03\x00", 5);
900 write_n32(str + 5, h2s->id);
901 write_n32(str + 9, h2s->errcode);
902 ret = bo_istput(res, ist2(str, 13));
903
904 if (unlikely(ret <= 0)) {
905 if (!ret) {
906 h2c->flags |= H2_CF_MUX_MFULL;
907 h2s->flags |= H2_SF_BLK_MROOM;
908 return 0;
909 }
910 else {
911 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
912 return 0;
913 }
914 }
915
916 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100917 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100918 return ret;
919}
920
921/* Try to send an RST_STREAM frame on the connection for the stream being
922 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
923 * error code unless the stream's state already is IDLE or CLOSED in which
924 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
925 * it was not yet.
926 *
927 * Returns > 0 on success or zero if nothing was done. In case of lack of room
928 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200929 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100930 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200931 */
932static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
933{
934 struct buffer *res;
935 char str[13];
936 int ret;
937
938 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100939 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200940 return 0;
941 }
942
943 res = h2_get_mbuf(h2c);
944 if (!res) {
945 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100946 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200947 return 0;
948 }
949
950 /* len: 4, type: 3, flags: none */
951 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100952
Willy Tarreau27a84c92017-10-17 08:10:17 +0200953 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100954 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200955 h2s->errcode : H2_ERR_STREAM_CLOSED);
956 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100957
Willy Tarreau27a84c92017-10-17 08:10:17 +0200958 if (unlikely(ret <= 0)) {
959 if (!ret) {
960 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100961 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return 0;
963 }
964 else {
965 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
966 return 0;
967 }
968 }
969
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100970 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200971 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100972 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100973 }
974
Willy Tarreau27a84c92017-10-17 08:10:17 +0200975 return ret;
976}
977
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100978/* try to send an empty DATA frame with the ES flag set to notify about the
979 * end of stream and match a shutdown(write). If an ES was already sent as
980 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
981 * on success or zero if nothing was done. In case of lack of room to write the
982 * message, it subscribes the requesting stream to future notifications.
983 */
984static int h2_send_empty_data_es(struct h2s *h2s)
985{
986 struct h2c *h2c = h2s->h2c;
987 struct buffer *res;
988 char str[9];
989 int ret;
990
Willy Tarreau721c9742017-11-07 11:05:42 +0100991 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100992 return 1;
993
994 if (h2c_mux_busy(h2c, h2s)) {
995 h2s->flags |= H2_SF_BLK_MBUSY;
996 return 0;
997 }
998
999 res = h2_get_mbuf(h2c);
1000 if (!res) {
1001 h2c->flags |= H2_CF_MUX_MALLOC;
1002 h2s->flags |= H2_SF_BLK_MROOM;
1003 return 0;
1004 }
1005
1006 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1007 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1008 write_n32(str + 5, h2s->id);
1009 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001010 if (likely(ret > 0)) {
1011 h2s->flags |= H2_SF_ES_SENT;
1012 }
1013 else if (!ret) {
1014 h2c->flags |= H2_CF_MUX_MFULL;
1015 h2s->flags |= H2_SF_BLK_MROOM;
1016 return 0;
1017 }
1018 else {
1019 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1020 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001021 }
1022 return ret;
1023}
1024
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001025/* wake the streams attached to the connection, whose id is greater than <last>,
1026 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1027 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1028 * stream's state is automatically updated accordingly.
1029 */
1030static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1031{
1032 struct eb32_node *node;
1033 struct h2s *h2s;
1034
1035 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1036 flags |= CS_FL_ERROR;
1037
1038 if (conn_xprt_read0_pending(h2c->conn))
1039 flags |= CS_FL_EOS;
1040
1041 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1042 while (node) {
1043 h2s = container_of(node, struct h2s, by_id);
1044 if (h2s->id <= last)
1045 break;
1046 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001047
1048 if (!h2s->cs) {
1049 /* this stream was already orphaned */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001050 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001051 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001052 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001053 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001054 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001055
1056 h2s->cs->flags |= flags;
1057 /* recv is used to force to detect CS_FL_EOS that wake()
1058 * doesn't handle in the stream int code.
1059 */
1060 h2s->cs->data_cb->recv(h2s->cs);
1061 h2s->cs->data_cb->wake(h2s->cs);
1062
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001063 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1064 h2s->st = H2_SS_ERROR;
1065 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1066 h2s->st = H2_SS_HREM;
1067 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001068 h2c_stream_close(h2c, h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001069 }
1070}
1071
Willy Tarreau3421aba2017-07-27 15:41:03 +02001072/* Increase all streams' outgoing window size by the difference passed in
1073 * argument. This is needed upon receipt of the settings frame if the initial
1074 * window size is different. The difference may be negative and the resulting
1075 * window size as well, for the time it takes to receive some window updates.
1076 */
1077static void h2c_update_all_ws(struct h2c *h2c, int diff)
1078{
1079 struct h2s *h2s;
1080 struct eb32_node *node;
1081
1082 if (!diff)
1083 return;
1084
1085 node = eb32_first(&h2c->streams_by_id);
1086 while (node) {
1087 h2s = container_of(node, struct h2s, by_id);
1088 h2s->mws += diff;
1089 node = eb32_next(node);
1090 }
1091}
1092
1093/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1094 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1095 * return an error in h2c. Described in RFC7540#6.5.
1096 */
1097static int h2c_handle_settings(struct h2c *h2c)
1098{
1099 unsigned int offset;
1100 int error;
1101
1102 if (h2c->dff & H2_F_SETTINGS_ACK) {
1103 if (h2c->dfl) {
1104 error = H2_ERR_FRAME_SIZE_ERROR;
1105 goto fail;
1106 }
1107 return 1;
1108 }
1109
1110 if (h2c->dsi != 0) {
1111 error = H2_ERR_PROTOCOL_ERROR;
1112 goto fail;
1113 }
1114
1115 if (h2c->dfl % 6) {
1116 error = H2_ERR_FRAME_SIZE_ERROR;
1117 goto fail;
1118 }
1119
1120 /* that's the limit we can process */
1121 if (h2c->dfl > global.tune.bufsize) {
1122 error = H2_ERR_FRAME_SIZE_ERROR;
1123 goto fail;
1124 }
1125
1126 /* process full frame only */
1127 if (h2c->dbuf->i < h2c->dfl)
1128 return 0;
1129
1130 /* parse the frame */
1131 for (offset = 0; offset < h2c->dfl; offset += 6) {
1132 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1133 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1134
1135 switch (type) {
1136 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1137 /* we need to update all existing streams with the
1138 * difference from the previous iws.
1139 */
1140 if (arg < 0) { // RFC7540#6.5.2
1141 error = H2_ERR_FLOW_CONTROL_ERROR;
1142 goto fail;
1143 }
1144 h2c_update_all_ws(h2c, arg - h2c->miw);
1145 h2c->miw = arg;
1146 break;
1147 case H2_SETTINGS_MAX_FRAME_SIZE:
1148 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1149 error = H2_ERR_PROTOCOL_ERROR;
1150 goto fail;
1151 }
1152 h2c->mfs = arg;
1153 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001154 case H2_SETTINGS_ENABLE_PUSH:
1155 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1156 error = H2_ERR_PROTOCOL_ERROR;
1157 goto fail;
1158 }
1159 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001160 }
1161 }
1162
1163 /* need to ACK this frame now */
1164 h2c->st0 = H2_CS_FRAME_A;
1165 return 1;
1166 fail:
1167 h2c_error(h2c, error);
1168 return 0;
1169}
1170
1171/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1172 * success or one of the h2_status values.
1173 */
1174static int h2c_ack_settings(struct h2c *h2c)
1175{
1176 struct buffer *res;
1177 char str[9];
1178 int ret = -1;
1179
1180 if (h2c_mux_busy(h2c, NULL)) {
1181 h2c->flags |= H2_CF_DEM_MBUSY;
1182 return 0;
1183 }
1184
1185 res = h2_get_mbuf(h2c);
1186 if (!res) {
1187 h2c->flags |= H2_CF_MUX_MALLOC;
1188 h2c->flags |= H2_CF_DEM_MROOM;
1189 return 0;
1190 }
1191
1192 memcpy(str,
1193 "\x00\x00\x00" /* length : 0 (no data) */
1194 "\x04" "\x01" /* type : 4, flags : ACK */
1195 "\x00\x00\x00\x00" /* stream ID */, 9);
1196
1197 ret = bo_istput(res, ist2(str, 9));
1198 if (unlikely(ret <= 0)) {
1199 if (!ret) {
1200 h2c->flags |= H2_CF_MUX_MFULL;
1201 h2c->flags |= H2_CF_DEM_MROOM;
1202 return 0;
1203 }
1204 else {
1205 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1206 return 0;
1207 }
1208 }
1209 return ret;
1210}
1211
Willy Tarreaucf68c782017-10-10 17:11:41 +02001212/* processes a PING frame and schedules an ACK if needed. The caller must pass
1213 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1214 * missing data. It may return an error in h2c.
1215 */
1216static int h2c_handle_ping(struct h2c *h2c)
1217{
1218 /* frame length must be exactly 8 */
1219 if (h2c->dfl != 8) {
1220 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1221 return 0;
1222 }
1223
1224 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001225 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001226 h2c->st0 = H2_CS_FRAME_A;
1227 return 1;
1228}
1229
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001230/* Try to send a window update for stream id <sid> and value <increment>.
1231 * Returns > 0 on success or zero on missing room or failure. It may return an
1232 * error in h2c.
1233 */
1234static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1235{
1236 struct buffer *res;
1237 char str[13];
1238 int ret = -1;
1239
1240 if (h2c_mux_busy(h2c, NULL)) {
1241 h2c->flags |= H2_CF_DEM_MBUSY;
1242 return 0;
1243 }
1244
1245 res = h2_get_mbuf(h2c);
1246 if (!res) {
1247 h2c->flags |= H2_CF_MUX_MALLOC;
1248 h2c->flags |= H2_CF_DEM_MROOM;
1249 return 0;
1250 }
1251
1252 /* length: 4, type: 8, flags: none */
1253 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1254 write_n32(str + 5, sid);
1255 write_n32(str + 9, increment);
1256
1257 ret = bo_istput(res, ist2(str, 13));
1258
1259 if (unlikely(ret <= 0)) {
1260 if (!ret) {
1261 h2c->flags |= H2_CF_MUX_MFULL;
1262 h2c->flags |= H2_CF_DEM_MROOM;
1263 return 0;
1264 }
1265 else {
1266 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1267 return 0;
1268 }
1269 }
1270 return ret;
1271}
1272
1273/* try to send pending window update for the connection. It's safe to call it
1274 * with no pending updates. Returns > 0 on success or zero on missing room or
1275 * failure. It may return an error in h2c.
1276 */
1277static int h2c_send_conn_wu(struct h2c *h2c)
1278{
1279 int ret = 1;
1280
1281 if (h2c->rcvd_c <= 0)
1282 return 1;
1283
1284 /* send WU for the connection */
1285 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1286 if (ret > 0)
1287 h2c->rcvd_c = 0;
1288
1289 return ret;
1290}
1291
1292/* try to send pending window update for the current dmux stream. It's safe to
1293 * call it with no pending updates. Returns > 0 on success or zero on missing
1294 * room or failure. It may return an error in h2c.
1295 */
1296static int h2c_send_strm_wu(struct h2c *h2c)
1297{
1298 int ret = 1;
1299
1300 if (h2c->rcvd_s <= 0)
1301 return 1;
1302
1303 /* send WU for the stream */
1304 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1305 if (ret > 0)
1306 h2c->rcvd_s = 0;
1307
1308 return ret;
1309}
1310
Willy Tarreaucf68c782017-10-10 17:11:41 +02001311/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1312 * success, 0 on missing data or one of the h2_status values.
1313 */
1314static int h2c_ack_ping(struct h2c *h2c)
1315{
1316 struct buffer *res;
1317 char str[17];
1318 int ret = -1;
1319
1320 if (h2c->dbuf->i < 8)
1321 return 0;
1322
1323 if (h2c_mux_busy(h2c, NULL)) {
1324 h2c->flags |= H2_CF_DEM_MBUSY;
1325 return 0;
1326 }
1327
1328 res = h2_get_mbuf(h2c);
1329 if (!res) {
1330 h2c->flags |= H2_CF_MUX_MALLOC;
1331 h2c->flags |= H2_CF_DEM_MROOM;
1332 return 0;
1333 }
1334
1335 memcpy(str,
1336 "\x00\x00\x08" /* length : 8 (same payload) */
1337 "\x06" "\x01" /* type : 6, flags : ACK */
1338 "\x00\x00\x00\x00" /* stream ID */, 9);
1339
1340 /* copy the original payload */
1341 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1342
1343 ret = bo_istput(res, ist2(str, 17));
1344 if (unlikely(ret <= 0)) {
1345 if (!ret) {
1346 h2c->flags |= H2_CF_MUX_MFULL;
1347 h2c->flags |= H2_CF_DEM_MROOM;
1348 return 0;
1349 }
1350 else {
1351 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1352 return 0;
1353 }
1354 }
1355 return ret;
1356}
1357
Willy Tarreau26f95952017-07-27 17:18:30 +02001358/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1359 * Returns > 0 on success or zero on missing data. It may return an error in
1360 * h2c or h2s. Described in RFC7540#6.9.
1361 */
1362static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1363{
1364 int32_t inc;
1365 int error;
1366
1367 if (h2c->dfl != 4) {
1368 error = H2_ERR_FRAME_SIZE_ERROR;
1369 goto conn_err;
1370 }
1371
1372 /* process full frame only */
1373 if (h2c->dbuf->i < h2c->dfl)
1374 return 0;
1375
1376 inc = h2_get_n32(h2c->dbuf, 0);
1377
1378 if (h2c->dsi != 0) {
1379 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001380
1381 /* it's not an error to receive WU on a closed stream */
1382 if (h2s->st == H2_SS_CLOSED)
1383 return 1;
1384
1385 if (!inc) {
1386 error = H2_ERR_PROTOCOL_ERROR;
1387 goto strm_err;
1388 }
1389
1390 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1391 error = H2_ERR_FLOW_CONTROL_ERROR;
1392 goto strm_err;
1393 }
1394
1395 h2s->mws += inc;
1396 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1397 h2s->flags &= ~H2_SF_BLK_SFCTL;
1398 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1399 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1400 /* This stream wanted to send but could not due to its
1401 * own flow control. We can put it back into the send
1402 * list now, it will be handled upon next send() call.
1403 */
1404 LIST_ADDQ(&h2c->send_list, &h2s->list);
1405 }
1406 }
1407 }
1408 else {
1409 /* connection window update */
1410 if (!inc) {
1411 error = H2_ERR_PROTOCOL_ERROR;
1412 goto conn_err;
1413 }
1414
1415 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1416 error = H2_ERR_FLOW_CONTROL_ERROR;
1417 goto conn_err;
1418 }
1419
1420 h2c->mws += inc;
1421 }
1422
1423 return 1;
1424
1425 conn_err:
1426 h2c_error(h2c, error);
1427 return 0;
1428
1429 strm_err:
1430 if (h2s) {
1431 h2s_error(h2s, error);
1432 h2c->st0 = H2_CS_FRAME_A;
1433 }
1434 else
1435 h2c_error(h2c, error);
1436 return 0;
1437}
1438
Willy Tarreaue96b0922017-10-30 00:28:29 +01001439/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1440 * the last ID. Returns > 0 on success or zero on missing data. It may return
1441 * an error in h2c. Described in RFC7540#6.8.
1442 */
1443static int h2c_handle_goaway(struct h2c *h2c)
1444{
1445 int error;
1446 int last;
1447
1448 if (h2c->dsi != 0) {
1449 error = H2_ERR_PROTOCOL_ERROR;
1450 goto conn_err;
1451 }
1452
1453 if (h2c->dfl < 8) {
1454 error = H2_ERR_FRAME_SIZE_ERROR;
1455 goto conn_err;
1456 }
1457
1458 /* process full frame only */
1459 if (h2c->dbuf->i < h2c->dfl)
1460 return 0;
1461
1462 last = h2_get_n32(h2c->dbuf, 0);
1463 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1464 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001465 if (h2c->last_sid < 0)
1466 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001467 return 1;
1468
1469 conn_err:
1470 h2c_error(h2c, error);
1471 return 0;
1472}
1473
Willy Tarreau92153fc2017-12-03 19:46:19 +01001474/* processes a PRIORITY frame, and either skips it or rejects if it is
1475 * invalid. Returns > 0 on success or zero on missing data. It may return
1476 * an error in h2c. Described in RFC7540#6.3.
1477 */
1478static int h2c_handle_priority(struct h2c *h2c)
1479{
1480 int error;
1481
1482 if (h2c->dsi == 0) {
1483 error = H2_ERR_PROTOCOL_ERROR;
1484 goto conn_err;
1485 }
1486
1487 if (h2c->dfl != 5) {
1488 error = H2_ERR_FRAME_SIZE_ERROR;
1489 goto conn_err;
1490 }
1491
1492 /* process full frame only */
1493 if (h2c->dbuf->i < h2c->dfl)
1494 return 0;
1495
1496 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1497 /* 7540#5.3 : can't depend on itself */
1498 error = H2_ERR_PROTOCOL_ERROR;
1499 goto conn_err;
1500 }
1501 return 1;
1502
1503 conn_err:
1504 h2c_error(h2c, error);
1505 return 0;
1506}
1507
Willy Tarreaucd234e92017-08-18 10:59:39 +02001508/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1509 * Returns > 0 on success or zero on missing data. It may return an error in
1510 * h2c. Described in RFC7540#6.4.
1511 */
1512static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1513{
1514 int error;
1515
1516 if (h2c->dsi == 0) {
1517 error = H2_ERR_PROTOCOL_ERROR;
1518 goto conn_err;
1519 }
1520
Willy Tarreaucd234e92017-08-18 10:59:39 +02001521 if (h2c->dfl != 4) {
1522 error = H2_ERR_FRAME_SIZE_ERROR;
1523 goto conn_err;
1524 }
1525
1526 /* process full frame only */
1527 if (h2c->dbuf->i < h2c->dfl)
1528 return 0;
1529
1530 /* late RST, already handled */
1531 if (h2s->st == H2_SS_CLOSED)
1532 return 1;
1533
1534 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001535 h2c_stream_close(h2c, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001536
1537 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001538 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001539 /* recv is used to force to detect CS_FL_EOS that wake()
1540 * doesn't handle in the stream-int code.
1541 */
1542 h2s->cs->data_cb->recv(h2s->cs);
1543 h2s->cs->data_cb->wake(h2s->cs);
1544 }
1545
1546 h2s->flags |= H2_SF_RST_RCVD;
1547 return 1;
1548
1549 conn_err:
1550 h2c_error(h2c, error);
1551 return 0;
1552}
1553
Willy Tarreau13278b42017-10-13 19:23:14 +02001554/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1555 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1556 * errors here are reported as connection errors since it's impossible to
1557 * recover from such errors after the compression context has been altered.
1558 */
1559static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1560{
1561 int error;
1562
1563 if (!h2c->dfl) {
1564 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1565 goto strm_err;
1566 }
1567
1568 if (!h2c->dbuf->size)
1569 return 0; // empty buffer
1570
1571 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1572 return 0; // incomplete frame
1573
1574 /* now either the frame is complete or the buffer is complete */
1575 if (h2s->st != H2_SS_IDLE) {
1576 /* FIXME: stream already exists, this is only allowed for
1577 * trailers (not supported for now).
1578 */
1579 error = H2_ERR_PROTOCOL_ERROR;
1580 goto conn_err;
1581 }
1582 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1583 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1584 error = H2_ERR_PROTOCOL_ERROR;
1585 goto conn_err;
1586 }
1587
1588 h2s = h2c_stream_new(h2c, h2c->dsi);
1589 if (!h2s) {
1590 error = H2_ERR_INTERNAL_ERROR;
1591 goto conn_err;
1592 }
1593
1594 h2s->st = H2_SS_OPEN;
1595 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1596 h2s->st = H2_SS_HREM;
1597 h2s->flags |= H2_SF_ES_RCVD;
1598 }
1599
1600 /* call the upper layers to process the frame, then let the upper layer
1601 * notify the stream about any change.
1602 */
1603 h2s->cs->data_cb->recv(h2s->cs);
1604
1605 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1606 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1607 error = H2_ERR_INTERNAL_ERROR;
1608 goto conn_err;
1609 }
1610
Willy Tarreau8f650c32017-11-21 19:36:21 +01001611 if (h2c->st0 >= H2_CS_ERROR)
1612 return 0;
1613
Willy Tarreau721c9742017-11-07 11:05:42 +01001614 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001615 /* stream error : send RST_STREAM */
1616 h2c->st0 = H2_CS_FRAME_A;
1617 }
1618 else {
1619 /* update the max stream ID if the request is being processed */
1620 if (h2s->id > h2c->max_id)
1621 h2c->max_id = h2s->id;
1622 }
1623
1624 return 1;
1625
1626 conn_err:
1627 h2c_error(h2c, error);
1628 return 0;
1629
1630 strm_err:
1631 if (h2s) {
1632 h2s_error(h2s, error);
1633 h2c->st0 = H2_CS_FRAME_A;
1634 }
1635 else
1636 h2c_error(h2c, error);
1637 return 0;
1638}
1639
Willy Tarreau454f9052017-10-26 19:40:35 +02001640/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1641 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1642 */
1643static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1644{
1645 int error;
1646
1647 /* note that empty DATA frames are perfectly valid and sometimes used
1648 * to signal an end of stream (with the ES flag).
1649 */
1650
1651 if (!h2c->dbuf->size && h2c->dfl)
1652 return 0; // empty buffer
1653
1654 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1655 return 0; // incomplete frame
1656
1657 /* now either the frame is complete or the buffer is complete */
1658
1659 if (!h2c->dsi) {
1660 /* RFC7540#6.1 */
1661 error = H2_ERR_PROTOCOL_ERROR;
1662 goto conn_err;
1663 }
1664
1665 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1666 /* RFC7540#6.1 */
1667 error = H2_ERR_STREAM_CLOSED;
1668 goto strm_err;
1669 }
1670
Willy Tarreau454f9052017-10-26 19:40:35 +02001671 /* call the upper layers to process the frame, then let the upper layer
1672 * notify the stream about any change.
1673 */
1674 if (!h2s->cs) {
1675 error = H2_ERR_STREAM_CLOSED;
1676 goto strm_err;
1677 }
1678
1679 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001680
Willy Tarreau454f9052017-10-26 19:40:35 +02001681 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1682 /* cs has just been destroyed, we have to kill h2s. */
1683 error = H2_ERR_STREAM_CLOSED;
1684 goto strm_err;
1685 }
1686
Willy Tarreau8f650c32017-11-21 19:36:21 +01001687 if (h2c->st0 >= H2_CS_ERROR)
1688 return 0;
1689
Willy Tarreau721c9742017-11-07 11:05:42 +01001690 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001691 /* stream error : send RST_STREAM */
1692 h2c->st0 = H2_CS_FRAME_A;
1693 }
1694
1695 /* check for completion : the callee will change this to FRAME_A or
1696 * FRAME_H once done.
1697 */
1698 if (h2c->st0 == H2_CS_FRAME_P)
1699 return 0;
1700
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001701
1702 /* last frame */
1703 if (h2c->dff & H2_F_DATA_END_STREAM) {
1704 h2s->st = H2_SS_HREM;
1705 h2s->flags |= H2_SF_ES_RCVD;
1706 }
1707
Willy Tarreau454f9052017-10-26 19:40:35 +02001708 return 1;
1709
1710 conn_err:
1711 h2c_error(h2c, error);
1712 return 0;
1713
1714 strm_err:
1715 if (h2s) {
1716 h2s_error(h2s, error);
1717 h2c->st0 = H2_CS_FRAME_A;
1718 }
1719 else
1720 h2c_error(h2c, error);
1721 return 0;
1722}
1723
Willy Tarreaubc933932017-10-09 16:21:43 +02001724/* process Rx frames to be demultiplexed */
1725static void h2_process_demux(struct h2c *h2c)
1726{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001727 struct h2s *h2s;
1728
Willy Tarreau081d4722017-05-16 21:51:05 +02001729 if (h2c->st0 >= H2_CS_ERROR)
1730 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001731
1732 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1733 if (h2c->st0 == H2_CS_PREFACE) {
1734 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1735 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1736 if (h2c->st0 == H2_CS_ERROR)
1737 h2c->st0 = H2_CS_ERROR2;
1738 goto fail;
1739 }
1740
1741 h2c->max_id = 0;
1742 h2c->st0 = H2_CS_SETTINGS1;
1743 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001744
1745 if (h2c->st0 == H2_CS_SETTINGS1) {
1746 struct h2_fh hdr;
1747
1748 /* ensure that what is pending is a valid SETTINGS frame
1749 * without an ACK.
1750 */
1751 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1752 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1753 if (h2c->st0 == H2_CS_ERROR)
1754 h2c->st0 = H2_CS_ERROR2;
1755 goto fail;
1756 }
1757
1758 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1759 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1760 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1761 h2c->st0 = H2_CS_ERROR2;
1762 goto fail;
1763 }
1764
1765 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1766 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1767 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1768 h2c->st0 = H2_CS_ERROR2;
1769 goto fail;
1770 }
1771
1772 /* that's OK, switch to FRAME_P to process it */
1773 h2c->dfl = hdr.len;
1774 h2c->dsi = hdr.sid;
1775 h2c->dft = hdr.ft;
1776 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001777 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001778 h2c->st0 = H2_CS_FRAME_P;
1779 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001780 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001781
1782 /* process as many incoming frames as possible below */
1783 while (h2c->dbuf->i) {
1784 int ret = 0;
1785
1786 if (h2c->st0 >= H2_CS_ERROR)
1787 break;
1788
1789 if (h2c->st0 == H2_CS_FRAME_H) {
1790 struct h2_fh hdr;
1791
1792 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1793 break;
1794
1795 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1796 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1797 h2c->st0 = H2_CS_ERROR;
1798 break;
1799 }
1800
1801 h2c->dfl = hdr.len;
1802 h2c->dsi = hdr.sid;
1803 h2c->dft = hdr.ft;
1804 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001805 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001806 h2c->st0 = H2_CS_FRAME_P;
1807 h2_skip_frame_hdr(h2c->dbuf);
1808 }
1809
1810 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001811 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001812
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001813 if (h2s->st == H2_SS_IDLE &&
1814 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1815 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1816 * this state MUST be treated as a connection error
1817 */
1818 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1819 h2c->st0 = H2_CS_ERROR;
1820 break;
1821 }
1822
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001823 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1824 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1825 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1826 * this state MUST be treated as a stream error
1827 */
1828 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1829 goto strm_err;
1830 }
1831
Willy Tarreauc0da1962017-10-30 18:38:00 +01001832#if 0
1833 // problem below: it is not possible to completely ignore such
1834 // streams as we need to maintain the compression state as well
1835 // and for this we need to completely process these frames (eg:
1836 // HEADERS frames) as well as counting DATA frames to emit
1837 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1838 // This is a typical case of layer violation where the
1839 // transported contents are critical to the connection's
1840 // validity and must be ignored at the same time :-(
1841
1842 /* graceful shutdown, ignore streams whose ID is higher than
1843 * the one advertised in GOAWAY. RFC7540#6.8.
1844 */
1845 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1846 ret = MIN(h2c->dbuf->i, h2c->dfl);
1847 bi_del(h2c->dbuf, ret);
1848 h2c->dfl -= ret;
1849 ret = h2c->dfl == 0;
1850 goto strm_err;
1851 }
1852#endif
1853
Willy Tarreau7e98c052017-10-10 15:56:59 +02001854 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001855 case H2_FT_SETTINGS:
1856 if (h2c->st0 == H2_CS_FRAME_P)
1857 ret = h2c_handle_settings(h2c);
1858
1859 if (h2c->st0 == H2_CS_FRAME_A)
1860 ret = h2c_ack_settings(h2c);
1861 break;
1862
Willy Tarreaucf68c782017-10-10 17:11:41 +02001863 case H2_FT_PING:
1864 if (h2c->st0 == H2_CS_FRAME_P)
1865 ret = h2c_handle_ping(h2c);
1866
1867 if (h2c->st0 == H2_CS_FRAME_A)
1868 ret = h2c_ack_ping(h2c);
1869 break;
1870
Willy Tarreau26f95952017-07-27 17:18:30 +02001871 case H2_FT_WINDOW_UPDATE:
1872 if (h2c->st0 == H2_CS_FRAME_P)
1873 ret = h2c_handle_window_update(h2c, h2s);
1874 break;
1875
Willy Tarreau61290ec2017-10-17 08:19:21 +02001876 case H2_FT_CONTINUATION:
1877 /* we currently don't support CONTINUATION frames since
1878 * we have nowhere to store the partial HEADERS frame.
1879 * Let's abort the stream on an INTERNAL_ERROR here.
1880 */
1881 if (h2c->st0 == H2_CS_FRAME_P)
1882 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1883 break;
1884
Willy Tarreau13278b42017-10-13 19:23:14 +02001885 case H2_FT_HEADERS:
1886 if (h2c->st0 == H2_CS_FRAME_P)
1887 ret = h2c_frt_handle_headers(h2c, h2s);
1888 break;
1889
Willy Tarreau454f9052017-10-26 19:40:35 +02001890 case H2_FT_DATA:
1891 if (h2c->st0 == H2_CS_FRAME_P)
1892 ret = h2c_frt_handle_data(h2c, h2s);
1893
1894 if (h2c->st0 == H2_CS_FRAME_A)
1895 ret = h2c_send_strm_wu(h2c);
1896 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001897
Willy Tarreau92153fc2017-12-03 19:46:19 +01001898 case H2_FT_PRIORITY:
1899 if (h2c->st0 == H2_CS_FRAME_P)
1900 ret = h2c_handle_priority(h2c);
1901 break;
1902
Willy Tarreaucd234e92017-08-18 10:59:39 +02001903 case H2_FT_RST_STREAM:
1904 if (h2c->st0 == H2_CS_FRAME_P)
1905 ret = h2c_handle_rst_stream(h2c, h2s);
1906 break;
1907
Willy Tarreaue96b0922017-10-30 00:28:29 +01001908 case H2_FT_GOAWAY:
1909 if (h2c->st0 == H2_CS_FRAME_P)
1910 ret = h2c_handle_goaway(h2c);
1911 break;
1912
Willy Tarreau1c661982017-10-30 13:52:01 +01001913 case H2_FT_PUSH_PROMISE:
1914 /* not permitted here, RFC7540#5.1 */
1915 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001916 break;
1917
1918 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001919 default:
1920 /* drop frames that we ignore. They may be larger than
1921 * the buffer so we drain all of their contents until
1922 * we reach the end.
1923 */
1924 ret = MIN(h2c->dbuf->i, h2c->dfl);
1925 bi_del(h2c->dbuf, ret);
1926 h2c->dfl -= ret;
1927 ret = h2c->dfl == 0;
1928 }
1929
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001930 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001931 /* RST are sent similarly to frame acks */
1932 if (h2s->st == H2_SS_ERROR) {
1933 if (h2c->st0 == H2_CS_FRAME_P)
1934 h2c->st0 = H2_CS_FRAME_A;
1935
1936 if (h2c->st0 == H2_CS_FRAME_A)
1937 ret = h2c_send_rst_stream(h2c, h2s);
1938 }
1939
Willy Tarreau7e98c052017-10-10 15:56:59 +02001940 /* error or missing data condition met above ? */
1941 if (ret <= 0)
1942 break;
1943
1944 if (h2c->st0 != H2_CS_FRAME_H) {
1945 bi_del(h2c->dbuf, h2c->dfl);
1946 h2c->st0 = H2_CS_FRAME_H;
1947 }
1948 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001949
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001950 if (h2c->rcvd_c > 0 &&
1951 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1952 h2c_send_conn_wu(h2c);
1953
Willy Tarreau52eed752017-09-22 15:05:09 +02001954 fail:
1955 /* we can go here on missing data, blocked response or error */
1956 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001957}
1958
1959/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1960 * the end.
1961 */
1962static int h2_process_mux(struct h2c *h2c)
1963{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001964 struct h2s *h2s, *h2s_back;
1965
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001966 /* start by sending possibly pending window updates */
1967 if (h2c->rcvd_c > 0 &&
1968 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1969 h2c_send_conn_wu(h2c) < 0)
1970 goto fail;
1971
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001972 /* First we always process the flow control list because the streams
1973 * waiting there were already elected for immediate emission but were
1974 * blocked just on this.
1975 */
1976
1977 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1978 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1979 h2c->st0 >= H2_CS_ERROR)
1980 break;
1981
1982 /* In theory it's possible that h2s->cs == NULL here :
1983 * - client sends crap that causes a parse error
1984 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1985 * - RST_STREAM cannot be emitted because mux is busy/full
1986 * - stream gets notified, detaches and quits
1987 * - mux buffer gets ready and wakes pending streams up
1988 * - bam!
1989 */
1990 h2s->flags &= ~H2_SF_BLK_ANY;
1991
1992 if (h2s->cs) {
1993 h2s->cs->data_cb->send(h2s->cs);
1994 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001995 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001996 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001997 }
1998
1999 /* depending on callee's blocking reasons, we may queue in send
2000 * list or completely dequeue.
2001 */
2002 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2003 if (h2s->flags & H2_SF_BLK_ANY) {
2004 LIST_DEL(&h2s->list);
2005 LIST_ADDQ(&h2c->send_list, &h2s->list);
2006 }
2007 else {
2008 LIST_DEL(&h2s->list);
2009 LIST_INIT(&h2s->list);
2010 if (h2s->cs)
2011 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002012 else {
2013 /* just sent the last frame for this orphaned stream */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002014 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002015 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002016 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002017 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002018 }
2019 }
2020 }
2021
2022 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2023 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2024 break;
2025
2026 /* In theory it's possible that h2s->cs == NULL here :
2027 * - client sends crap that causes a parse error
2028 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2029 * - RST_STREAM cannot be emitted because mux is busy/full
2030 * - stream gets notified, detaches and quits
2031 * - mux buffer gets ready and wakes pending streams up
2032 * - bam!
2033 */
2034 h2s->flags &= ~H2_SF_BLK_ANY;
2035
2036 if (h2s->cs) {
2037 h2s->cs->data_cb->send(h2s->cs);
2038 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002039 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002040 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002041 }
2042 /* depending on callee's blocking reasons, we may queue in fctl
2043 * list or completely dequeue.
2044 */
2045 if (h2s->flags & H2_SF_BLK_MFCTL) {
2046 /* stream hit the connection's flow control */
2047 LIST_DEL(&h2s->list);
2048 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2049 }
2050 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2051 LIST_DEL(&h2s->list);
2052 LIST_INIT(&h2s->list);
2053 if (h2s->cs)
2054 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002055 else {
2056 /* just sent the last frame for this orphaned stream */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002057 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002058 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002059 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002060 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002061 }
2062 }
2063
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002064 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002065 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002066 if (h2c->st0 == H2_CS_ERROR) {
2067 if (h2c->max_id >= 0) {
2068 h2c_send_goaway_error(h2c, NULL);
2069 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2070 return 0;
2071 }
2072
2073 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2074 }
2075 return 1;
2076 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002077 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002078}
2079
Willy Tarreau71681172017-10-23 14:39:06 +02002080
Willy Tarreau62f52692017-10-08 23:01:42 +02002081/*********************************************************/
2082/* functions below are I/O callbacks from the connection */
2083/*********************************************************/
2084
2085/* callback called on recv event by the connection handler */
2086static void h2_recv(struct connection *conn)
2087{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002088 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002089 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002090 int max;
2091
Willy Tarreau315d8072017-12-10 22:17:57 +01002092 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002093 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002094
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002095 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002096 if (!buf) {
2097 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002098 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002099 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002100
Willy Tarreaua2af5122017-10-09 11:56:46 +02002101 /* note: buf->o == 0 */
2102 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002103 if (max)
2104 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002105
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002106 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002107 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002108 return;
2109 }
2110
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002111 if (buf->i == buf->size)
2112 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002113 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002114}
2115
2116/* callback called on send event by the connection handler */
2117static void h2_send(struct connection *conn)
2118{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002119 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002120 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002121
2122 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002123 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002124
2125 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2126 /* a handshake was requested */
2127 return;
2128 }
2129
Willy Tarreaubc933932017-10-09 16:21:43 +02002130 /* This loop is quite simple : it tries to fill as much as it can from
2131 * pending streams into the existing buffer until it's reportedly full
2132 * or the end of send requests is reached. Then it tries to send this
2133 * buffer's contents out, marks it not full if at least one byte could
2134 * be sent, and tries again.
2135 *
2136 * The snd_buf() function normally takes a "flags" argument which may
2137 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2138 * data immediately comes and CO_SFL_STREAMER to indicate that the
2139 * connection is streaming lots of data (used to increase TLS record
2140 * size at the expense of latency). The former can be sent any time
2141 * there's a buffer full flag, as it indicates at least one stream
2142 * attempted to send and failed so there are pending data. An
2143 * alternative would be to set it as long as there's an active stream
2144 * but that would be problematic for ACKs until we have an absolute
2145 * guarantee that all waiters have at least one byte to send. The
2146 * latter should possibly not be set for now.
2147 */
2148
2149 done = 0;
2150 while (!done) {
2151 unsigned int flags = 0;
2152
2153 /* fill as much as we can into the current buffer */
2154 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2155 done = h2_process_mux(h2c);
2156
2157 if (conn->flags & CO_FL_ERROR)
2158 break;
2159
2160 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2161 flags |= CO_SFL_MSG_MORE;
2162
Willy Tarreau319994a2017-11-07 11:03:56 +01002163 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002164 break;
2165
2166 /* wrote at least one byte, the buffer is not full anymore */
2167 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2168 }
2169
Willy Tarreaua2af5122017-10-09 11:56:46 +02002170 if (conn->flags & CO_FL_SOCK_WR_SH) {
2171 /* output closed, nothing to send, clear the buffer to release it */
2172 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002173 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002174}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002175
Willy Tarreau62f52692017-10-08 23:01:42 +02002176/* callback called on any event by the connection handler.
2177 * It applies changes and returns zero, or < 0 if it wants immediate
2178 * destruction of the connection (which normally doesn not happen in h2).
2179 */
2180static int h2_wake(struct connection *conn)
2181{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002182 struct h2c *h2c = conn->mux_ctx;
2183
Willy Tarreaud13bf272017-12-14 10:34:52 +01002184 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2185 h2_process_demux(h2c);
2186
2187 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2188 h2c->dbuf->i = 0;
2189
2190 if (h2c->dbuf->i != h2c->dbuf->size)
2191 h2c->flags &= ~H2_CF_DEM_DFULL;
2192 }
2193
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002194 /*
2195 * If we received early data, try to wake any stream, just in case
2196 * at least one of them was waiting for the handshake
2197 */
2198 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2199 CO_FL_EARLY_DATA) {
2200 h2_wake_some_streams(h2c, 0, 0);
2201 conn->flags &= ~CO_FL_EARLY_DATA;
2202 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002203 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002204 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2205 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2206 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002207 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002208
2209 if (eb_is_empty(&h2c->streams_by_id)) {
2210 /* no more stream, kill the connection now */
2211 h2_release(conn);
2212 return -1;
2213 }
2214 else {
2215 /* some streams still there, we need to signal them all and
2216 * wait for their departure.
2217 */
2218 __conn_xprt_stop_recv(conn);
2219 __conn_xprt_stop_send(conn);
2220 return 0;
2221 }
2222 }
2223
2224 if (!h2c->dbuf->i)
2225 h2_release_dbuf(h2c);
2226
2227 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002228 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002229 __conn_xprt_stop_recv(conn);
2230 }
2231 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002232 __conn_xprt_want_recv(conn);
2233 }
2234
2235 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002236 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2237 (h2c->st0 == H2_CS_ERROR ||
2238 h2c->mbuf->o ||
2239 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2240 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002241 __conn_xprt_want_send(conn);
2242 }
2243 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002244 h2_release_mbuf(h2c);
2245 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002246 }
2247
Willy Tarreau3f133572017-10-31 19:21:06 +01002248 if (h2c->task) {
2249 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002250 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002251 task_queue(h2c->task);
2252 }
2253 else
2254 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002255 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002256 return 0;
2257}
2258
Willy Tarreauea392822017-10-31 10:02:25 +01002259/* Connection timeout management. The principle is that if there's no receipt
2260 * nor sending for a certain amount of time, the connection is closed. If the
2261 * MUX buffer still has lying data or is not allocatable, the connection is
2262 * immediately killed. If it's allocatable and empty, we attempt to send a
2263 * GOAWAY frame.
2264 */
2265static struct task *h2_timeout_task(struct task *t)
2266{
2267 struct h2c *h2c = t->context;
2268 int expired = tick_is_expired(t->expire, now_ms);
2269
2270 if (!expired)
2271 return t;
2272
2273 h2c_error(h2c, H2_ERR_NO_ERROR);
2274 h2_wake_some_streams(h2c, 0, 0);
2275
2276 if (h2c->mbuf->o) {
2277 /* don't even try to send a GOAWAY, the buffer is stuck */
2278 h2c->flags |= H2_CF_GOAWAY_FAILED;
2279 }
2280
2281 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002282 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002283 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2284 h2c->flags |= H2_CF_GOAWAY_FAILED;
2285
2286 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2287 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2288
2289 if (!eb_is_empty(&h2c->streams_by_id))
2290 goto wait;
2291
2292 h2_release(h2c->conn);
2293 return NULL;
2294
2295 wait:
2296 /* the streams have been notified, we must let them finish and close */
2297 h2c->task = NULL;
2298 task_delete(t);
2299 task_free(t);
2300 return NULL;
2301}
2302
2303
Willy Tarreau62f52692017-10-08 23:01:42 +02002304/*******************************************/
2305/* functions below are used by the streams */
2306/*******************************************/
2307
2308/*
2309 * Attach a new stream to a connection
2310 * (Used for outgoing connections)
2311 */
2312static struct conn_stream *h2_attach(struct connection *conn)
2313{
2314 return NULL;
2315}
2316
2317/* callback used to update the mux's polling flags after changing a cs' status.
2318 * The caller (cs_update_mux_polling) will take care of propagating any changes
2319 * to the transport layer.
2320 */
2321static void h2_update_poll(struct conn_stream *cs)
2322{
Willy Tarreau1d393222017-10-17 10:26:19 +02002323 struct h2s *h2s = cs->ctx;
2324
2325 if (!h2s)
2326 return;
2327
Willy Tarreaud7739c82017-10-30 15:38:23 +01002328 /* we may unblock a blocked read */
2329
Willy Tarreau315d8072017-12-10 22:17:57 +01002330 if (cs->flags & CS_FL_DATA_RD_ENA) {
2331 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002332 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002333 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002334 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002335 conn_xprt_want_send(cs->conn);
2336 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002337 }
2338
Willy Tarreau1d393222017-10-17 10:26:19 +02002339 /* Note: the stream and stream-int code doesn't allow us to perform a
2340 * synchronous send() here unfortunately, because this code is called
2341 * as si_update() from the process_stream() context. This means that
2342 * we have to queue the current cs and defer its processing after the
2343 * connection's cs list is processed anyway.
2344 */
2345
2346 if (cs->flags & CS_FL_DATA_WR_ENA) {
2347 if (LIST_ISEMPTY(&h2s->list)) {
2348 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2349 !h2s->h2c->mbuf->o && // not yet subscribed
2350 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2351 conn_xprt_want_send(cs->conn);
2352 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2353 }
2354 }
2355 else if (!LIST_ISEMPTY(&h2s->list)) {
2356 LIST_DEL(&h2s->list);
2357 LIST_INIT(&h2s->list);
2358 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2359 }
2360
2361 /* this can happen from within si_chk_snd() */
2362 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2363 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002364}
2365
2366/*
2367 * Detach the stream from the connection and possibly release the connection.
2368 */
2369static void h2_detach(struct conn_stream *cs)
2370{
Willy Tarreau60935142017-10-16 18:11:19 +02002371 struct h2s *h2s = cs->ctx;
2372 struct h2c *h2c;
2373
2374 cs->ctx = NULL;
2375 if (!h2s)
2376 return;
2377
2378 h2c = h2s->h2c;
2379 h2s->cs = NULL;
2380
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002381 /* this stream may be blocked waiting for some data to leave (possibly
2382 * an ES or RST frame), so orphan it in this case.
2383 */
2384 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2385 return;
2386
Willy Tarreau541dd822017-11-23 18:12:50 +01002387 /* the stream could be in the send list */
2388 LIST_DEL(&h2s->list);
2389
Willy Tarreau45f752e2017-10-30 15:44:59 +01002390 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2391 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2392 /* unblock the connection if it was blocked on this
2393 * stream.
2394 */
2395 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2396 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2397 conn_xprt_want_recv(cs->conn);
2398 conn_xprt_want_send(cs->conn);
2399 }
2400
Willy Tarreau60935142017-10-16 18:11:19 +02002401 if (h2s->by_id.node.leaf_p) {
2402 /* h2s still attached to the h2c */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002403 h2c_stream_close(h2c, h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002404 eb32_delete(&h2s->by_id);
2405
2406 /* We don't want to close right now unless we're removing the
2407 * last stream, and either the connection is in error, or it
2408 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002409 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002410 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002411 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2412 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002413 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002414 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2415 (conn_xprt_read0_pending(h2c->conn) ||
2416 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002417 /* no more stream will come, kill it now */
2418 h2_release(h2c->conn);
2419 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002420 else if (h2c->task) {
2421 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002422 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002423 task_queue(h2c->task);
2424 }
2425 else
2426 h2c->task->expire = TICK_ETERNITY;
2427 }
Willy Tarreau60935142017-10-16 18:11:19 +02002428 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002429 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002430}
2431
2432static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2433{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002434 struct h2s *h2s = cs->ctx;
2435
2436 if (!mode)
2437 return;
2438
Willy Tarreau721c9742017-11-07 11:05:42 +01002439 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002440 return;
2441
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002442 /* if no outgoing data was seen on this stream, it means it was
2443 * closed with a "tcp-request content" rule that is normally
2444 * used to kill the connection ASAP (eg: limit abuse). In this
2445 * case we send a goaway to close the connection.
2446 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002447 if (!(h2s->flags & H2_SF_RST_SENT) &&
2448 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2449 return;
2450
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002451 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2452 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2453 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2454 return;
2455
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002456 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2457 conn_xprt_want_send(cs->conn);
2458
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002459 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002460}
2461
2462static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2463{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002464 struct h2s *h2s = cs->ctx;
2465
Willy Tarreau721c9742017-11-07 11:05:42 +01002466 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002467 return;
2468
Willy Tarreau67434202017-11-06 20:20:51 +01002469 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002470 /* we can cleanly close using an empty data frame only after headers */
2471
2472 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2473 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002474 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002475
2476 if (h2s->st == H2_SS_HREM)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002477 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002478 else
2479 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002480 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002481 /* if no outgoing data was seen on this stream, it means it was
2482 * closed with a "tcp-request content" rule that is normally
2483 * used to kill the connection ASAP (eg: limit abuse). In this
2484 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002485 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002486 if (!(h2s->flags & H2_SF_RST_SENT) &&
2487 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2488 return;
2489
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002490 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2491 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002492 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2493 return;
2494
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002495 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002496 }
2497
2498 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2499 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002500}
2501
Willy Tarreau13278b42017-10-13 19:23:14 +02002502/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2503 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2504 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002505 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002506 */
2507static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2508{
2509 struct h2c *h2c = h2s->h2c;
2510 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002511 struct chunk *tmp = get_trash_chunk();
2512 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002513 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002514 int flen = h2c->dfl;
2515 int outlen = 0;
2516 int wrap;
2517 int try;
2518
2519 if (!h2c->dfl) {
2520 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2521 return 0;
2522 }
2523
Willy Tarreau68472622017-12-11 18:36:37 +01002524 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2525 return 0; // incomplete input frame
2526
Willy Tarreau13278b42017-10-13 19:23:14 +02002527 /* if the input buffer wraps, take a temporary copy of it (rare) */
2528 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2529 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002530 copy = alloc_trash_chunk();
2531 if (!copy) {
2532 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2533 goto fail;
2534 }
2535 memcpy(copy->str, h2c->dbuf->p, wrap);
2536 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2537 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002538 }
2539
2540 /* The padlen is the first byte before data, and the padding appears
2541 * after data. padlen+data+padding are included in flen.
2542 */
2543 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002544 h2c->dpl = *hdrs;
2545 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002546 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2547 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002548 return 0;
2549 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002550 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002551 hdrs += 1; // skip Pad Length
2552 }
2553
2554 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2555 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002556 if (read_n32(hdrs) == h2s->id) {
2557 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2558 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2559 return 0;//goto fail_stream;
2560 }
2561
Willy Tarreau13278b42017-10-13 19:23:14 +02002562 hdrs += 5; // stream dep = 4, weight = 1
2563 flen -= 5;
2564 }
2565
2566 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2567 * don't support this for now and can't even decompress so we have to
2568 * break the connection.
2569 */
2570 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2571 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002572 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002573 }
2574
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002575 /* we can't retry a failed decompression operation so we must be very
2576 * careful not to take any risks. In practice the output buffer is
2577 * always empty except maybe for trailers, so these operations almost
2578 * never happen.
2579 */
2580 if (unlikely(buf->o)) {
2581 /* need to let the output buffer flush and
2582 * mark the buffer for later wake up.
2583 */
2584 goto fail;
2585 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002586
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002587 if (unlikely(buffer_space_wraps(buf))) {
2588 /* it doesn't fit and the buffer is fragmented,
2589 * so let's defragment it and try again.
2590 */
2591 buffer_slow_realign(buf);
2592 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002593
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002594 /* first check if we have some room after p+i */
2595 try = buf->data + buf->size - (buf->p + buf->i);
2596
2597 /* otherwise continue between data and p-o */
2598 if (try <= 0) {
2599 try = buf->p - (buf->data + buf->o);
2600 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002601 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002602 }
2603 if (try > count)
2604 try = count;
2605
2606 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2607 sizeof(list)/sizeof(list[0]), tmp);
2608 if (outlen < 0) {
2609 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2610 goto fail;
2611 }
2612
2613 /* OK now we have our header list in <list> */
2614 outlen = h2_make_h1_request(list, bi_end(buf), try);
2615
2616 if (outlen < 0) {
2617 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2618 goto fail;
2619 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002620
2621 /* now consume the input data */
2622 bi_del(h2c->dbuf, h2c->dfl);
2623 h2c->st0 = H2_CS_FRAME_H;
2624 buf->i += outlen;
2625
2626 /* don't send it before returning data!
2627 * FIXME: should we instead try to send it much later, after the
2628 * response ? This would require that we keep a copy of it in h2s.
2629 */
2630 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2631 h2s->cs->flags |= CS_FL_EOS;
2632 h2s->flags |= H2_SF_ES_RCVD;
2633 }
2634
Willy Tarreau68dd9852017-07-03 14:44:26 +02002635 leave:
2636 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002637 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002638 fail:
2639 outlen = 0;
2640 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002641}
2642
Willy Tarreau454f9052017-10-26 19:40:35 +02002643/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2644 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2645 * in use, a new chunk is emitted for each frame. This is supposed to fit
2646 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2647 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2648 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2649 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002650 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2651 * checked to know if some data remain pending (an empty DATA frame can return
2652 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2653 * connection errors in h2c->errcode. The caller must already have checked the
2654 * frame header and ensured that the frame was complete or the buffer full. It
2655 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002656 */
2657static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2658{
2659 struct h2c *h2c = h2s->h2c;
2660 int block1, block2;
2661 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002662
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002663 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002664 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002665
2666 /* The padlen is the first byte before data, and the padding appears
2667 * after data. padlen+data+padding are included in flen.
2668 */
Willy Tarreau79127812017-12-03 21:06:59 +01002669 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002670 if (h2c->dbuf->i < 1)
2671 return 0;
2672
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002673 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2674 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002675 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2676 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002677 return 0;
2678 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002679
2680 /* skip the padlen byte */
2681 bi_del(h2c->dbuf, 1);
2682 h2c->dfl--;
2683 h2c->rcvd_c++; h2c->rcvd_s++;
2684 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002685 }
2686
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002687 flen = h2c->dfl - h2c->dpl;
2688 if (!flen)
Willy Tarreau454f9052017-10-26 19:40:35 +02002689 return 0;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002690
2691 if (flen > h2c->dbuf->i) {
2692 flen = h2c->dbuf->i;
2693 if (!flen)
2694 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002695 }
2696
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002697 /* does it fit in output buffer or should we wait ? */
2698 if (flen > count) {
2699 flen = count;
2700 if (!flen) {
2701 h2c->flags |= H2_CF_DEM_SFULL;
2702 h2s->cs->flags |= CS_FL_RCV_MORE;
2703 return 0;
2704 }
2705 }
2706
Willy Tarreau454f9052017-10-26 19:40:35 +02002707 /* Block1 is the length of the first block before the buffer wraps,
2708 * block2 is the optional second block to reach the end of the frame.
2709 */
2710 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002711 if (block1 > flen)
2712 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002713 block2 = flen - block1;
2714
2715 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002716 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002717
2718 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002719 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002720
2721 /* now mark the input data as consumed (will be deleted from the buffer
2722 * by the caller when seeing FRAME_A after sending the window update).
2723 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002724 bi_del(h2c->dbuf, flen);
2725 h2c->dfl -= flen;
2726 h2c->rcvd_c += flen;
2727 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2728
2729 if (h2c->dfl > h2c->dpl) {
2730 /* more data available, transfer stalled on stream full */
2731 h2c->flags |= H2_CF_DEM_SFULL;
2732 h2s->cs->flags |= CS_FL_RCV_MORE;
2733 return flen;
2734 }
2735
2736 /* here we're done with the frame, all the payload (except padding) was
2737 * transferred.
2738 */
Willy Tarreau454f9052017-10-26 19:40:35 +02002739 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2740
2741 /* don't send it before returning data!
2742 * FIXME: should we instead try to send it much later, after the
2743 * response ? This would require that we keep a copy of it in h2s.
2744 */
Willy Tarreau79127812017-12-03 21:06:59 +01002745 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002746 h2s->cs->flags |= CS_FL_EOS;
2747 h2s->flags |= H2_SF_ES_RCVD;
2748 }
2749
2750 return flen;
2751}
2752
Willy Tarreau62f52692017-10-08 23:01:42 +02002753/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002754 * Called from the upper layer to get more data, up to <count> bytes. The
2755 * caller is responsible for never asking for more data than what is available
2756 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002757 */
2758static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2759{
Willy Tarreau13278b42017-10-13 19:23:14 +02002760 struct h2s *h2s = cs->ctx;
2761 struct h2c *h2c = h2s->h2c;
2762 int ret = 0;
2763
2764 if (h2c->st0 != H2_CS_FRAME_P)
2765 return 0; // no pre-parsed frame yet
2766
2767 if (h2c->dsi != h2s->id)
2768 return 0; // not for us
2769
2770 if (!h2c->dbuf->size)
2771 return 0; // empty buffer
2772
Willy Tarreau13278b42017-10-13 19:23:14 +02002773 switch (h2c->dft) {
2774 case H2_FT_HEADERS:
2775 ret = h2_frt_decode_headers(h2s, buf, count);
2776 break;
2777
Willy Tarreau454f9052017-10-26 19:40:35 +02002778 case H2_FT_DATA:
2779 ret = h2_frt_transfer_data(h2s, buf, count);
2780 break;
2781
Willy Tarreau13278b42017-10-13 19:23:14 +02002782 default:
2783 ret = 0;
2784 }
2785 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002786}
2787
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002788/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2789 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2790 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2791 * to the number of buffer bytes consumed.
2792 */
2793static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2794{
2795 struct http_hdr list[MAX_HTTP_HDR];
2796 struct h2c *h2c = h2s->h2c;
2797 struct h1m *h1m = &h2s->res;
2798 struct chunk outbuf;
2799 int es_now = 0;
2800 int ret = 0;
2801 int hdr;
2802
2803 if (h2c_mux_busy(h2c, h2s)) {
2804 h2s->flags |= H2_SF_BLK_MBUSY;
2805 return 0;
2806 }
2807
2808 if (!h2_get_mbuf(h2c)) {
2809 h2c->flags |= H2_CF_MUX_MALLOC;
2810 h2s->flags |= H2_SF_BLK_MROOM;
2811 return 0;
2812 }
2813
2814 /* First, try to parse the H1 response and index it into <list>.
2815 * NOTE! Since it comes from haproxy, we *know* that a response header
2816 * block does not wrap and we can safely read it this way without
2817 * having to realign the buffer.
2818 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002819 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002820 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2821 list, sizeof(list)/sizeof(list[0]), h1m);
2822 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002823 /* incomplete or invalid response, this is abnormal coming from
2824 * haproxy and may only result in a bad errorfile or bad Lua code
2825 * so that won't be fixed, raise an error now.
2826 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002827 * FIXME: we should instead add the ability to only return a
2828 * 502 bad gateway. But in theory this is not supposed to
2829 * happen.
2830 */
2831 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2832 ret = 0;
2833 goto end;
2834 }
2835
2836 chunk_reset(&outbuf);
2837
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002838 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002839 while (1) {
2840 outbuf.str = bo_end(h2c->mbuf);
2841 outbuf.size = bo_contig_space(h2c->mbuf);
2842 outbuf.len = 0;
2843
2844 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2845 break;
2846 realign_again:
2847 buffer_slow_realign(h2c->mbuf);
2848 }
2849
2850 if (outbuf.size < 9) {
2851 h2c->flags |= H2_CF_MUX_MFULL;
2852 h2s->flags |= H2_SF_BLK_MROOM;
2853 ret = 0;
2854 goto end;
2855 }
2856
2857 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2858 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2859 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2860 outbuf.len = 9;
2861
2862 /* encode status, which necessarily is the first one */
2863 if (outbuf.len < outbuf.size && h1m->status == 200)
2864 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2865 else if (outbuf.len < outbuf.size && h1m->status == 304)
2866 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002867 else if (unlikely(list[0].v.len != 3)) {
2868 /* this is an unparsable response */
2869 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2870 ret = 0;
2871 goto end;
2872 }
2873 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002874 /* basic encoding of the status code */
2875 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2876 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2877 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2878 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2879 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2880 }
2881 else {
2882 if (buffer_space_wraps(h2c->mbuf))
2883 goto realign_again;
2884
2885 h2c->flags |= H2_CF_MUX_MFULL;
2886 h2s->flags |= H2_SF_BLK_MROOM;
2887 ret = 0;
2888 goto end;
2889 }
2890
2891 /* encode all headers, stop at empty name */
2892 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002893 /* these ones do not exist in H2 and must be dropped. */
2894 if (isteq(list[hdr].n, ist("connection")) ||
2895 isteq(list[hdr].n, ist("proxy-connection")) ||
2896 isteq(list[hdr].n, ist("keep-alive")) ||
2897 isteq(list[hdr].n, ist("upgrade")) ||
2898 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002899 continue;
2900
2901 if (isteq(list[hdr].n, ist("")))
2902 break; // end
2903
2904 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2905 /* output full */
2906 if (buffer_space_wraps(h2c->mbuf))
2907 goto realign_again;
2908
2909 h2c->flags |= H2_CF_MUX_MFULL;
2910 h2s->flags |= H2_SF_BLK_MROOM;
2911 ret = 0;
2912 goto end;
2913 }
2914 }
2915
2916 /* we may need to add END_STREAM */
2917 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2918 es_now = 1;
2919
2920 /* update the frame's size */
2921 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2922
2923 if (es_now)
2924 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2925
2926 /* consume incoming H1 response */
2927 bo_del(buf, ret);
2928
2929 /* commit the H2 response */
2930 h2c->mbuf->o += outbuf.len;
2931 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002932 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002933
2934 /* for now we don't implemented CONTINUATION, so we wait for a
2935 * body or directly end in TRL2.
2936 */
2937 if (es_now) {
2938 h1m->state = HTTP_MSG_DONE;
2939 h2s->flags |= H2_SF_ES_SENT;
2940 if (h2s->st == H2_SS_OPEN)
2941 h2s->st = H2_SS_HLOC;
2942 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002943 h2c_stream_close(h2c, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002944 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002945 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002946 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002947 h1m->state = HTTP_MSG_RPBEFORE;
2948 h1m->status = 0;
2949 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01002950 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01002951 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002952 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01002953 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002954
2955 end:
2956 //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));
2957 return ret;
2958}
2959
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002960/* Try to send a DATA frame matching HTTP/1 response present in the response
2961 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2962 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2963 * corresponds to the number of buffer bytes consumed.
2964 */
2965static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2966{
2967 struct h2c *h2c = h2s->h2c;
2968 struct h1m *h1m = &h2s->res;
2969 struct chunk outbuf;
2970 int ret = 0;
2971 int total = 0;
2972 int es_now = 0;
2973 int size = 0;
2974 char *blk1, *blk2;
2975 int len1, len2;
2976
2977 if (h2c_mux_busy(h2c, h2s)) {
2978 h2s->flags |= H2_SF_BLK_MBUSY;
2979 goto end;
2980 }
2981
2982 if (!h2_get_mbuf(h2c)) {
2983 h2c->flags |= H2_CF_MUX_MALLOC;
2984 h2s->flags |= H2_SF_BLK_MROOM;
2985 goto end;
2986 }
2987
2988 new_frame:
2989 if (!buf->o)
2990 goto end;
2991
2992 chunk_reset(&outbuf);
2993
2994 while (1) {
2995 outbuf.str = bo_end(h2c->mbuf);
2996 outbuf.size = bo_contig_space(h2c->mbuf);
2997 outbuf.len = 0;
2998
2999 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3000 break;
3001 realign_again:
3002 buffer_slow_realign(h2c->mbuf);
3003 }
3004
3005 if (outbuf.size < 9) {
3006 h2c->flags |= H2_CF_MUX_MFULL;
3007 h2s->flags |= H2_SF_BLK_MROOM;
3008 goto end;
3009 }
3010
3011 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3012 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3013 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3014 outbuf.len = 9;
3015
3016 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3017 case 0: /* no content length, read till SHUTW */
3018 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003019 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003020 break;
3021 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3022 size = buf->o;
3023 if ((long long)size > h1m->curr_len)
3024 size = h1m->curr_len;
3025 break;
3026 default: /* te:chunked : parse chunks */
3027 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3028 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3029 if (!ret)
3030 goto end;
3031
3032 if (ret < 0) {
3033 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3034 h1m->err_pos = ret;
3035 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3036 goto end;
3037 }
3038 bo_del(buf, ret);
3039 total += ret;
3040 h1m->state = HTTP_MSG_CHUNK_SIZE;
3041 }
3042
3043 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3044 unsigned int chunk;
3045
3046 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3047 if (!ret)
3048 goto end;
3049
3050 if (ret < 0) {
3051 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3052 h1m->err_pos = ret;
3053 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3054 goto end;
3055 }
3056
3057 size = chunk;
3058 h1m->curr_len = chunk;
3059 h1m->body_len += chunk;
3060 bo_del(buf, ret);
3061 total += ret;
3062 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3063 if (!size)
3064 goto send_empty;
3065 }
3066
3067 /* in MSG_DATA state, continue below */
3068 size = h1m->curr_len;
3069 break;
3070 }
3071
3072 /* we have in <size> the exact number of bytes we need to copy from
3073 * the H1 buffer. We need to check this against the connection's and
3074 * the stream's send windows, and to ensure that this fits in the max
3075 * frame size and in the buffer's available space minus 9 bytes (for
3076 * the frame header). The connection's flow control is applied last so
3077 * that we can use a separate list of streams which are immediately
3078 * unblocked on window opening. Note: we don't implement padding.
3079 */
3080
3081 if (size > buf->o)
3082 size = buf->o;
3083
3084 if (size > h2s->mws)
3085 size = h2s->mws;
3086
3087 if (size <= 0) {
3088 h2s->flags |= H2_SF_BLK_SFCTL;
3089 goto end;
3090 }
3091
3092 if (h2c->mfs && size > h2c->mfs)
3093 size = h2c->mfs;
3094
3095 if (size + 9 > outbuf.size) {
3096 /* we have an opportunity for enlarging the too small
3097 * available space, let's try.
3098 */
3099 if (buffer_space_wraps(h2c->mbuf))
3100 goto realign_again;
3101 size = outbuf.size - 9;
3102 }
3103
3104 if (size <= 0) {
3105 h2c->flags |= H2_CF_MUX_MFULL;
3106 h2s->flags |= H2_SF_BLK_MROOM;
3107 goto end;
3108 }
3109
3110 if (size > h2c->mws)
3111 size = h2c->mws;
3112
3113 if (size <= 0) {
3114 h2s->flags |= H2_SF_BLK_MFCTL;
3115 goto end;
3116 }
3117
3118 /* copy whatever we can */
3119 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3120 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3121 if (ret == 1)
3122 len2 = 0;
3123
3124 if (!ret || len1 + len2 < size) {
3125 /* FIXME: must normally never happen */
3126 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3127 goto end;
3128 }
3129
3130 /* limit len1/len2 to size */
3131 if (len1 + len2 > size) {
3132 int sub = len1 + len2 - size;
3133
3134 if (len2 > sub)
3135 len2 -= sub;
3136 else {
3137 sub -= len2;
3138 len2 = 0;
3139 len1 -= sub;
3140 }
3141 }
3142
3143 /* now let's copy this this into the output buffer */
3144 memcpy(outbuf.str + 9, blk1, len1);
3145 if (len2)
3146 memcpy(outbuf.str + 9 + len1, blk2, len2);
3147
3148 send_empty:
3149 /* we may need to add END_STREAM */
3150 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3151 * could rely on the MSG_MORE flag as a hint for this ?
3152 */
3153 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3154 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3155 es_now = 1;
3156
3157 /* update the frame's size */
3158 h2_set_frame_size(outbuf.str, size);
3159
3160 if (es_now)
3161 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3162
3163 /* commit the H2 response */
3164 h2c->mbuf->o += size + 9;
3165 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3166
3167 /* consume incoming H1 response */
3168 if (size > 0) {
3169 bo_del(buf, size);
3170 total += size;
3171 h1m->curr_len -= size;
3172 h2s->mws -= size;
3173 h2c->mws -= size;
3174
3175 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3176 h1m->state = HTTP_MSG_CHUNK_CRLF;
3177 goto new_frame;
3178 }
3179 }
3180
3181 if (es_now) {
3182 if (h2s->st == H2_SS_OPEN)
3183 h2s->st = H2_SS_HLOC;
3184 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003185 h2c_stream_close(h2c, h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003186
3187 if (!(h1m->flags & H1_MF_CHNK))
3188 h1m->state = HTTP_MSG_DONE;
3189
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003190 h2s->flags |= H2_SF_ES_SENT;
3191 }
3192
3193 end:
3194 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o);
3195 return total;
3196}
3197
Willy Tarreau62f52692017-10-08 23:01:42 +02003198/* Called from the upper layer, to send data */
3199static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3200{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003201 struct h2s *h2s = cs->ctx;
3202 int total = 0;
3203
Willy Tarreauc4312d32017-11-07 12:01:53 +01003204 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3205 h2s->flags |= H2_SF_OUTGOING_DATA;
3206
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003207 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3208 if (h2s->res.state < HTTP_MSG_BODY) {
3209 total += h2s_frt_make_resp_headers(h2s, buf);
3210
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003211 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003212 break;
3213
3214 if (h2s->flags & H2_SF_BLK_ANY)
3215 break;
3216 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003217 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3218 total += h2s_frt_make_resp_data(h2s, buf);
3219
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003220 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003221 break;
3222
3223 if (h2s->flags & H2_SF_BLK_ANY)
3224 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003225 }
3226 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3227 /* consume the trailers if any (we don't forward them for now) */
3228 int count = h1_measure_trailers(buf);
3229
3230 if (unlikely(count <= 0)) {
3231 if (count < 0)
3232 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3233 break;
3234 }
3235 total += count;
3236 bo_del(buf, count);
3237 h2s->res.state = HTTP_MSG_DONE;
3238 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003239 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003240 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003241 cs->flags |= CS_FL_ERROR;
3242 break;
3243 }
3244 }
3245
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003246 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003247 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003248 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003249 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003250 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003251 }
3252
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003253 if (h2s->flags & H2_SF_BLK_SFCTL) {
3254 /* stream flow control, quit the list */
3255 LIST_DEL(&h2s->list);
3256 LIST_INIT(&h2s->list);
3257 }
3258
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003259 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003260}
3261
3262
3263/*******************************************************/
3264/* functions below are dedicated to the config parsers */
3265/*******************************************************/
3266
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003267/* config parser for global "tune.h2.header-table-size" */
3268static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3269 struct proxy *defpx, const char *file, int line,
3270 char **err)
3271{
3272 if (too_many_args(1, args, err, NULL))
3273 return -1;
3274
3275 h2_settings_header_table_size = atoi(args[1]);
3276 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3277 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3278 return -1;
3279 }
3280 return 0;
3281}
Willy Tarreau62f52692017-10-08 23:01:42 +02003282
Willy Tarreaue6baec02017-07-27 11:45:11 +02003283/* config parser for global "tune.h2.initial-window-size" */
3284static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3285 struct proxy *defpx, const char *file, int line,
3286 char **err)
3287{
3288 if (too_many_args(1, args, err, NULL))
3289 return -1;
3290
3291 h2_settings_initial_window_size = atoi(args[1]);
3292 if (h2_settings_initial_window_size < 0) {
3293 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3294 return -1;
3295 }
3296 return 0;
3297}
3298
Willy Tarreau5242ef82017-07-27 11:47:28 +02003299/* config parser for global "tune.h2.max-concurrent-streams" */
3300static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3301 struct proxy *defpx, const char *file, int line,
3302 char **err)
3303{
3304 if (too_many_args(1, args, err, NULL))
3305 return -1;
3306
3307 h2_settings_max_concurrent_streams = atoi(args[1]);
3308 if (h2_settings_max_concurrent_streams < 0) {
3309 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3310 return -1;
3311 }
3312 return 0;
3313}
3314
Willy Tarreau62f52692017-10-08 23:01:42 +02003315
3316/****************************************/
3317/* MUX initialization and instanciation */
3318/***************************************/
3319
3320/* The mux operations */
3321const struct mux_ops h2_ops = {
3322 .init = h2_init,
3323 .recv = h2_recv,
3324 .send = h2_send,
3325 .wake = h2_wake,
3326 .update_poll = h2_update_poll,
3327 .rcv_buf = h2_rcv_buf,
3328 .snd_buf = h2_snd_buf,
3329 .attach = h2_attach,
3330 .detach = h2_detach,
3331 .shutr = h2_shutr,
3332 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003333 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003334 .name = "H2",
3335};
3336
3337/* ALPN selection : this mux registers ALPN tolen "h2" */
3338static struct alpn_mux_list alpn_mux_h2 =
3339 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3340
3341/* config keyword parsers */
3342static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003343 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003344 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003345 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003346 { 0, NULL, NULL }
3347}};
3348
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003349static void __h2_deinit(void)
3350{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003351 pool_destroy(pool_head_h2s);
3352 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003353}
3354
Willy Tarreau62f52692017-10-08 23:01:42 +02003355__attribute__((constructor))
3356static void __h2_init(void)
3357{
3358 alpn_register_mux(&alpn_mux_h2);
3359 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003360 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003361 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3362 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003363}