blob: 7824cfe40dda4b44e721e16d9d1df5e8ea4c511a [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
Olivier Houchard638b7992018-08-16 15:41:52 +0200186 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreau18312642017-10-11 07:57:07 +0200187};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200188
Willy Tarreauc6405142017-09-21 20:23:50 +0200189/* descriptor for an h2 frame header */
190struct h2_fh {
191 uint32_t len; /* length, host order, 24 bits */
192 uint32_t sid; /* stream id, host order, 31 bits */
193 uint8_t ft; /* frame type */
194 uint8_t ff; /* frame flags */
195};
196
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197/* a few settings from the global section */
198static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200199static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200200static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200201
Willy Tarreau2a856182017-05-16 15:20:39 +0200202/* a dmumy closed stream */
203static const struct h2s *h2_closed_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100208 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200209 .id = 0,
210};
211
212/* and a dummy idle stream for use with any unannounced stream */
213static const struct h2s *h2_idle_stream = &(const struct h2s){
214 .cs = NULL,
215 .h2c = NULL,
216 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100217 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
Olivier Houchard9f6af332018-05-25 14:04:04 +0200221static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200222static void h2_send(struct h2c *h2c);
223static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100224static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100225static int h2_frt_decode_headers(struct h2s *h2s);
226static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200227
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200228/*****************************************************/
229/* functions below are for dynamic buffer management */
230/*****************************************************/
231
Willy Tarreau315d8072017-12-10 22:17:57 +0100232/* indicates whether or not the we may call the h2_recv() function to attempt
233 * to receive data into the buffer and/or demux pending data. The condition is
234 * a bit complex due to some API limits for now. The rules are the following :
235 * - if an error or a shutdown was detected on the connection and the buffer
236 * is empty, we must not attempt to receive
237 * - if the demux buf failed to be allocated, we must not try to receive and
238 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100239 * - if no flag indicates a blocking condition, we may attempt to receive,
240 * regardless of whether the demux buffer is full or not, so that only
241 * de demux part decides whether or not to block. This is needed because
242 * the connection API indeed prevents us from re-enabling receipt that is
243 * already enabled in a polled state, so we must always immediately stop
244 * as soon as the demux can't proceed so as never to hit an end of read
245 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100246 * - otherwise must may not attempt
247 */
248static inline int h2_recv_allowed(const struct h2c *h2c)
249{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200250 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100251 (h2c->st0 >= H2_CS_ERROR ||
252 h2c->conn->flags & CO_FL_ERROR ||
253 conn_xprt_read0_pending(h2c->conn)))
254 return 0;
255
256 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100257 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100258 return 1;
259
260 return 0;
261}
262
Willy Tarreauf2101912018-07-19 10:11:38 +0200263/* returns true if the connection has too many conn_streams attached */
264static inline int h2_has_too_many_cs(const struct h2c *h2c)
265{
266 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
267}
268
Willy Tarreau44e973f2018-03-01 17:49:30 +0100269/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
270 * flags are used to figure what buffer was requested. It returns 1 if the
271 * allocation succeeds, in which case the connection is woken up, or 0 if it's
272 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200273 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100274static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200275{
276 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100277 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200278
Willy Tarreau44e973f2018-03-01 17:49:30 +0100279 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200280 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100281 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200282 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283 return 1;
284 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200285
Willy Tarreau44e973f2018-03-01 17:49:30 +0100286 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
287 h2c->flags &= ~H2_CF_MUX_MALLOC;
288 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
289 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200290
291 if (h2c->flags & H2_CF_DEM_MROOM) {
292 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100293 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200294 conn_xprt_want_recv(h2c->conn);
295 }
Willy Tarreau14398122017-09-22 14:26:04 +0200296 return 1;
297 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100298
299 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
300 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200301 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100302 h2c->flags &= ~H2_CF_DEM_SALLOC;
303 if (h2_recv_allowed(h2c))
304 conn_xprt_want_recv(h2c->conn);
305 return 1;
306 }
307
Willy Tarreau14398122017-09-22 14:26:04 +0200308 return 0;
309}
310
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200311static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200312{
313 struct buffer *buf = NULL;
314
Willy Tarreau44e973f2018-03-01 17:49:30 +0100315 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
316 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
317 h2c->buf_wait.target = h2c;
318 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100320 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100321 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200322 __conn_xprt_stop_recv(h2c->conn);
323 }
324 return buf;
325}
326
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200327static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200328{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200329 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100330 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200331 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200332 }
333}
334
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200335
Willy Tarreau62f52692017-10-08 23:01:42 +0200336/*****************************************************************/
337/* functions below are dedicated to the mux setup and management */
338/*****************************************************************/
339
Willy Tarreau32218eb2017-09-22 08:07:25 +0200340/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
341static int h2c_frt_init(struct connection *conn)
342{
343 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100344 struct task *t = NULL;
345 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200346
Willy Tarreaubafbe012017-11-24 17:34:44 +0100347 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200348 if (!h2c)
349 goto fail;
350
Willy Tarreau3f133572017-10-31 19:21:06 +0100351
Willy Tarreau599391a2017-11-24 10:16:00 +0100352 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
353 if (tick_isset(sess->fe->timeout.clientfin))
354 h2c->shut_timeout = sess->fe->timeout.clientfin;
355
Willy Tarreau33400292017-11-05 11:23:40 +0100356 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100357 if (tick_isset(h2c->timeout)) {
358 t = task_new(tid_bit);
359 if (!t)
360 goto fail;
361
362 h2c->task = t;
363 t->process = h2_timeout_task;
364 t->context = h2c;
365 t->expire = tick_add(now_ms, h2c->timeout);
366 }
Willy Tarreauea392822017-10-31 10:02:25 +0100367
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200368 h2c->wait_list.task = tasklet_new();
369 if (!h2c->wait_list.task)
370 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200371 h2c->wait_list.task->process = h2_io_cb;
372 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200373 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200374
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
376 if (!h2c->ddht)
377 goto fail;
378
379 /* Initialise the context. */
380 h2c->st0 = H2_CS_PREFACE;
381 h2c->conn = conn;
382 h2c->max_id = -1;
383 h2c->errcode = H2_ERR_NO_ERROR;
384 h2c->flags = H2_CF_NONE;
385 h2c->rcvd_c = 0;
386 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100387 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200388 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200390 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200391 h2c->dsi = -1;
392 h2c->msi = -1;
393 h2c->last_sid = -1;
394
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200395 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200396 h2c->miw = 65535; /* mux initial window size */
397 h2c->mws = 65535; /* mux window size */
398 h2c->mfs = 16384; /* initial max frame size */
399 h2c->streams_by_id = EB_ROOT_UNIQUE;
400 LIST_INIT(&h2c->send_list);
401 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100402 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200403 conn->mux_ctx = h2c;
404
Willy Tarreau3f133572017-10-31 19:21:06 +0100405 if (t)
406 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200407 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200408 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200409 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100410
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411 /* mux->wake will be called soon to complete the operation */
412 return 0;
413 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100414 if (t)
415 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200416 if (h2c->wait_list.task)
417 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100418 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 return -1;
420}
421
Willy Tarreau62f52692017-10-08 23:01:42 +0200422/* Initialize the mux once it's attached. For outgoing connections, the context
423 * is already initialized before installing the mux, so we detect incoming
424 * connections from the fact that the context is still NULL. Returns < 0 on
425 * error.
426 */
427static int h2_init(struct connection *conn)
428{
429 if (conn->mux_ctx) {
430 /* we don't support outgoing connections for now */
431 return -1;
432 }
433
Willy Tarreau32218eb2017-09-22 08:07:25 +0200434 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200435}
436
Willy Tarreau2373acc2017-10-12 17:35:14 +0200437/* returns the stream associated with id <id> or NULL if not found */
438static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
439{
440 struct eb32_node *node;
441
Willy Tarreau2a856182017-05-16 15:20:39 +0200442 if (id > h2c->max_id)
443 return (struct h2s *)h2_idle_stream;
444
Willy Tarreau2373acc2017-10-12 17:35:14 +0200445 node = eb32_lookup(&h2c->streams_by_id, id);
446 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200447 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200448
449 return container_of(node, struct h2s, by_id);
450}
451
Willy Tarreau62f52692017-10-08 23:01:42 +0200452/* release function for a connection. This one should be called to free all
453 * resources allocated to the mux.
454 */
455static void h2_release(struct connection *conn)
456{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200457 struct h2c *h2c = conn->mux_ctx;
458
459 LIST_DEL(&conn->list);
460
461 if (h2c) {
462 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200463
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100464 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100465 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100466 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200467
Willy Tarreau44e973f2018-03-01 17:49:30 +0100468 h2_release_buf(h2c, &h2c->dbuf);
469 h2_release_buf(h2c, &h2c->mbuf);
470
Willy Tarreauea392822017-10-31 10:02:25 +0100471 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200472 h2c->task->context = NULL;
473 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100474 h2c->task = NULL;
475 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200476 if (h2c->wait_list.task)
477 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100478
Willy Tarreaubafbe012017-11-24 17:34:44 +0100479 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200480 }
481
482 conn->mux = NULL;
483 conn->mux_ctx = NULL;
484
485 conn_stop_tracking(conn);
486 conn_full_close(conn);
487 if (conn->destroy_cb)
488 conn->destroy_cb(conn);
489 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200490}
491
492
Willy Tarreau71681172017-10-23 14:39:06 +0200493/******************************************************/
494/* functions below are for the H2 protocol processing */
495/******************************************************/
496
497/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100498static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200499{
500 return h2s ? h2s->id : 0;
501}
502
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200503/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100504static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200505{
506 if (h2c->msi < 0)
507 return 0;
508
509 if (h2c->msi == h2s_id(h2s))
510 return 0;
511
512 return 1;
513}
514
Willy Tarreau741d6df2017-10-17 08:00:59 +0200515/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100516static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200517{
518 h2c->errcode = err;
519 h2c->st0 = H2_CS_ERROR;
520}
521
Willy Tarreau2e43f082017-10-17 08:03:59 +0200522/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100523static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200524{
525 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
526 h2s->errcode = err;
527 h2s->st = H2_SS_ERROR;
528 if (h2s->cs)
529 h2s->cs->flags |= CS_FL_ERROR;
530 }
531}
532
Willy Tarreaue4820742017-07-27 13:37:23 +0200533/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100534static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200535{
536 uint8_t *out = frame;
537
538 *out = len >> 16;
539 write_n16(out + 1, len);
540}
541
Willy Tarreau54c15062017-10-10 17:10:03 +0200542/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
543 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
544 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200545 * available in the buffer's input prior to calling this function. The buffer
546 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200547 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100548static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200549 const struct buffer *b, int o)
550{
Willy Tarreau591d4452018-06-15 17:21:00 +0200551 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200552}
553
Willy Tarreau1f094672017-11-20 21:27:45 +0100554static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200555{
Willy Tarreau591d4452018-06-15 17:21:00 +0200556 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200557}
558
Willy Tarreau1f094672017-11-20 21:27:45 +0100559static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200560{
Willy Tarreau591d4452018-06-15 17:21:00 +0200561 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200562}
563
Willy Tarreau1f094672017-11-20 21:27:45 +0100564static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200565{
Willy Tarreau591d4452018-06-15 17:21:00 +0200566 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200567}
568
569
Willy Tarreau715d5312017-07-11 15:20:24 +0200570/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
571 * is not obvious. It turns out that H2 headers are neither aligned nor do they
572 * use regular sizes. And to add to the trouble, the buffer may wrap so each
573 * byte read must be checked. The header is formed like this :
574 *
575 * b0 b1 b2 b3 b4 b5..b8
576 * +----------+---------+--------+----+----+----------------------+
577 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
578 * +----------+---------+--------+----+----+----------------------+
579 *
580 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
581 * we get the sid properly aligned and ordered, and 16 bits of len properly
582 * ordered as well. The type and flags can be extracted using bit shifts from
583 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200584 * Returns zero if some bytes are missing, otherwise non-zero on success. The
585 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200586 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100587static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200588{
589 uint64_t w;
590
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200591 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200592 return 0;
593
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200594 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200595 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200596 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
597 h->ff = w >> 32;
598 h->ft = w >> 40;
599 h->len += w >> 48;
600 return 1;
601}
602
603/* skip the next 9 bytes corresponding to the frame header possibly parsed by
604 * h2_peek_frame_hdr() above.
605 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100606static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200607{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200608 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200609}
610
611/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100612static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200613{
614 int ret;
615
616 ret = h2_peek_frame_hdr(b, h);
617 if (ret > 0)
618 h2_skip_frame_hdr(b);
619 return ret;
620}
621
Willy Tarreau00dd0782018-03-01 16:31:34 +0100622/* marks stream <h2s> as CLOSED and decrement the number of active streams for
623 * its connection if the stream was not yet closed. Please use this exclusively
624 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100625 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100626static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100627{
628 if (h2s->st != H2_SS_CLOSED)
629 h2s->h2c->nb_streams--;
630 h2s->st = H2_SS_CLOSED;
631}
632
Willy Tarreau71049cc2018-03-28 13:56:39 +0200633/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
634static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100635{
636 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200637 LIST_DEL(&h2s->list);
638 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100639 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200640 if (b_size(&h2s->rxbuf)) {
641 b_free(&h2s->rxbuf);
642 offer_buffers(NULL, tasks_run_queue);
643 }
Willy Tarreau0a10de62018-03-01 16:27:53 +0100644 pool_free(pool_head_h2s, h2s);
645}
646
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200647/* creates a new stream <id> on the h2c connection and returns it, or NULL in
648 * case of memory allocation error.
649 */
650static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
651{
652 struct conn_stream *cs;
653 struct h2s *h2s;
654
Willy Tarreaubafbe012017-11-24 17:34:44 +0100655 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200656 if (!h2s)
657 goto out;
658
659 h2s->h2c = h2c;
660 h2s->mws = h2c->miw;
661 h2s->flags = H2_SF_NONE;
662 h2s->errcode = H2_ERR_NO_ERROR;
663 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200664 h2s->rxbuf = BUF_NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200665 h1m_init(&h2s->req);
666 h1m_init(&h2s->res);
667 h2s->by_id.key = h2s->id = id;
668 h2c->max_id = id;
669 LIST_INIT(&h2s->list);
670
671 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100672 h2c->nb_streams++;
673 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
674 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675
676 cs = cs_new(h2c->conn);
677 if (!cs)
678 goto out_close;
679
680 h2s->cs = cs;
681 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200682 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200683
684 if (stream_create_from_cs(cs) < 0)
685 goto out_free_cs;
686
687 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200688 if (h2_has_too_many_cs(h2c))
689 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200690 return h2s;
691
692 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200693 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200694 cs_free(cs);
695 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200696 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200697 h2s = NULL;
698 out:
699 return h2s;
700}
701
Willy Tarreaube5b7152017-09-25 16:25:39 +0200702/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
703 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
704 * the various settings codes.
705 */
706static int h2c_snd_settings(struct h2c *h2c)
707{
708 struct buffer *res;
709 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200710 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200711 int ret;
712
713 if (h2c_mux_busy(h2c, NULL)) {
714 h2c->flags |= H2_CF_DEM_MBUSY;
715 return 0;
716 }
717
Willy Tarreau44e973f2018-03-01 17:49:30 +0100718 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200719 if (!res) {
720 h2c->flags |= H2_CF_MUX_MALLOC;
721 h2c->flags |= H2_CF_DEM_MROOM;
722 return 0;
723 }
724
725 chunk_init(&buf, buf_data, sizeof(buf_data));
726 chunk_memcpy(&buf,
727 "\x00\x00\x00" /* length : 0 for now */
728 "\x04\x00" /* type : 4 (settings), flags : 0 */
729 "\x00\x00\x00\x00", /* stream ID : 0 */
730 9);
731
732 if (h2_settings_header_table_size != 4096) {
733 char str[6] = "\x00\x01"; /* header_table_size */
734
735 write_n32(str + 2, h2_settings_header_table_size);
736 chunk_memcat(&buf, str, 6);
737 }
738
739 if (h2_settings_initial_window_size != 65535) {
740 char str[6] = "\x00\x04"; /* initial_window_size */
741
742 write_n32(str + 2, h2_settings_initial_window_size);
743 chunk_memcat(&buf, str, 6);
744 }
745
746 if (h2_settings_max_concurrent_streams != 0) {
747 char str[6] = "\x00\x03"; /* max_concurrent_streams */
748
749 /* Note: 0 means "unlimited" for haproxy's config but not for
750 * the protocol, so never send this value!
751 */
752 write_n32(str + 2, h2_settings_max_concurrent_streams);
753 chunk_memcat(&buf, str, 6);
754 }
755
756 if (global.tune.bufsize != 16384) {
757 char str[6] = "\x00\x05"; /* max_frame_size */
758
759 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
760 * match bufsize - rewrite size, but at the moment it seems
761 * that clients don't take care of it.
762 */
763 write_n32(str + 2, global.tune.bufsize);
764 chunk_memcat(&buf, str, 6);
765 }
766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 h2_set_frame_size(buf.area, buf.data - 9);
768 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200769 if (unlikely(ret <= 0)) {
770 if (!ret) {
771 h2c->flags |= H2_CF_MUX_MFULL;
772 h2c->flags |= H2_CF_DEM_MROOM;
773 return 0;
774 }
775 else {
776 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
777 return 0;
778 }
779 }
780 return ret;
781}
782
Willy Tarreau52eed752017-09-22 15:05:09 +0200783/* Try to receive a connection preface, then upon success try to send our
784 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
785 * missing data. It may return an error in h2c.
786 */
787static int h2c_frt_recv_preface(struct h2c *h2c)
788{
789 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200790 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200791
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200792 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200793
794 if (unlikely(ret1 <= 0)) {
795 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
796 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
797 return 0;
798 }
799
Willy Tarreaube5b7152017-09-25 16:25:39 +0200800 ret2 = h2c_snd_settings(h2c);
801 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200802 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200803
Willy Tarreaube5b7152017-09-25 16:25:39 +0200804 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200805}
806
Willy Tarreau081d4722017-05-16 21:51:05 +0200807/* try to send a GOAWAY frame on the connection to report an error or a graceful
808 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
809 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
810 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
811 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
812 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
813 * on unrecoverable failure. It will not attempt to send one again in this last
814 * case so that it is safe to use h2c_error() to report such errors.
815 */
816static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
817{
818 struct buffer *res;
819 char str[17];
820 int ret;
821
822 if (h2c->flags & H2_CF_GOAWAY_FAILED)
823 return 1; // claim that it worked
824
825 if (h2c_mux_busy(h2c, h2s)) {
826 if (h2s)
827 h2s->flags |= H2_SF_BLK_MBUSY;
828 else
829 h2c->flags |= H2_CF_DEM_MBUSY;
830 return 0;
831 }
832
Willy Tarreau44e973f2018-03-01 17:49:30 +0100833 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200834 if (!res) {
835 h2c->flags |= H2_CF_MUX_MALLOC;
836 if (h2s)
837 h2s->flags |= H2_SF_BLK_MROOM;
838 else
839 h2c->flags |= H2_CF_DEM_MROOM;
840 return 0;
841 }
842
843 /* len: 8, type: 7, flags: none, sid: 0 */
844 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
845
846 if (h2c->last_sid < 0)
847 h2c->last_sid = h2c->max_id;
848
849 write_n32(str + 9, h2c->last_sid);
850 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200851 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200852 if (unlikely(ret <= 0)) {
853 if (!ret) {
854 h2c->flags |= H2_CF_MUX_MFULL;
855 if (h2s)
856 h2s->flags |= H2_SF_BLK_MROOM;
857 else
858 h2c->flags |= H2_CF_DEM_MROOM;
859 return 0;
860 }
861 else {
862 /* we cannot report this error using GOAWAY, so we mark
863 * it and claim a success.
864 */
865 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
866 h2c->flags |= H2_CF_GOAWAY_FAILED;
867 return 1;
868 }
869 }
870 h2c->flags |= H2_CF_GOAWAY_SENT;
871 return ret;
872}
873
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100874/* Try to send an RST_STREAM frame on the connection for the indicated stream
875 * during mux operations. This stream must be valid and cannot be closed
876 * already. h2s->id will be used for the stream ID and h2s->errcode will be
877 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
878 * not yet.
879 *
880 * Returns > 0 on success or zero if nothing was done. In case of lack of room
881 * to write the message, it subscribes the stream to future notifications.
882 */
883static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
884{
885 struct buffer *res;
886 char str[13];
887 int ret;
888
889 if (!h2s || h2s->st == H2_SS_CLOSED)
890 return 1;
891
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100892 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
893 * RST_STREAM in response to a RST_STREAM frame.
894 */
895 if (h2c->dft == H2_FT_RST_STREAM) {
896 ret = 1;
897 goto ignore;
898 }
899
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100900 if (h2c_mux_busy(h2c, h2s)) {
901 h2s->flags |= H2_SF_BLK_MBUSY;
902 return 0;
903 }
904
Willy Tarreau44e973f2018-03-01 17:49:30 +0100905 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100906 if (!res) {
907 h2c->flags |= H2_CF_MUX_MALLOC;
908 h2s->flags |= H2_SF_BLK_MROOM;
909 return 0;
910 }
911
912 /* len: 4, type: 3, flags: none */
913 memcpy(str, "\x00\x00\x04\x03\x00", 5);
914 write_n32(str + 5, h2s->id);
915 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200916 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100917
918 if (unlikely(ret <= 0)) {
919 if (!ret) {
920 h2c->flags |= H2_CF_MUX_MFULL;
921 h2s->flags |= H2_SF_BLK_MROOM;
922 return 0;
923 }
924 else {
925 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
926 return 0;
927 }
928 }
929
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100930 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100932 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100933 return ret;
934}
935
936/* Try to send an RST_STREAM frame on the connection for the stream being
937 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
938 * error code unless the stream's state already is IDLE or CLOSED in which
939 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
940 * it was not yet.
941 *
942 * Returns > 0 on success or zero if nothing was done. In case of lack of room
943 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200944 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200946 */
947static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
948{
949 struct buffer *res;
950 char str[13];
951 int ret;
952
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100953 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
954 * RST_STREAM in response to a RST_STREAM frame.
955 */
956 if (h2c->dft == H2_FT_RST_STREAM) {
957 ret = 1;
958 goto ignore;
959 }
960
Willy Tarreau27a84c92017-10-17 08:10:17 +0200961 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100962 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200963 return 0;
964 }
965
Willy Tarreau44e973f2018-03-01 17:49:30 +0100966 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200967 if (!res) {
968 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100969 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200970 return 0;
971 }
972
973 /* len: 4, type: 3, flags: none */
974 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100975
Willy Tarreau27a84c92017-10-17 08:10:17 +0200976 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100977 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200978 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200979 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100980
Willy Tarreau27a84c92017-10-17 08:10:17 +0200981 if (unlikely(ret <= 0)) {
982 if (!ret) {
983 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100984 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200985 return 0;
986 }
987 else {
988 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
989 return 0;
990 }
991 }
992
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100993 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100994 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200995 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100996 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100997 }
998
Willy Tarreau27a84c92017-10-17 08:10:17 +0200999 return ret;
1000}
1001
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001002/* try to send an empty DATA frame with the ES flag set to notify about the
1003 * end of stream and match a shutdown(write). If an ES was already sent as
1004 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1005 * on success or zero if nothing was done. In case of lack of room to write the
1006 * message, it subscribes the requesting stream to future notifications.
1007 */
1008static int h2_send_empty_data_es(struct h2s *h2s)
1009{
1010 struct h2c *h2c = h2s->h2c;
1011 struct buffer *res;
1012 char str[9];
1013 int ret;
1014
Willy Tarreau721c9742017-11-07 11:05:42 +01001015 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001016 return 1;
1017
1018 if (h2c_mux_busy(h2c, h2s)) {
1019 h2s->flags |= H2_SF_BLK_MBUSY;
1020 return 0;
1021 }
1022
Willy Tarreau44e973f2018-03-01 17:49:30 +01001023 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001024 if (!res) {
1025 h2c->flags |= H2_CF_MUX_MALLOC;
1026 h2s->flags |= H2_SF_BLK_MROOM;
1027 return 0;
1028 }
1029
1030 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1031 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1032 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001033 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001034 if (likely(ret > 0)) {
1035 h2s->flags |= H2_SF_ES_SENT;
1036 }
1037 else if (!ret) {
1038 h2c->flags |= H2_CF_MUX_MFULL;
1039 h2s->flags |= H2_SF_BLK_MROOM;
1040 return 0;
1041 }
1042 else {
1043 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1044 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001045 }
1046 return ret;
1047}
1048
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001049/* wake the streams attached to the connection, whose id is greater than <last>,
1050 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1051 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1052 * stream's state is automatically updated accordingly.
1053 */
1054static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1055{
1056 struct eb32_node *node;
1057 struct h2s *h2s;
1058
1059 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1060 flags |= CS_FL_ERROR;
1061
1062 if (conn_xprt_read0_pending(h2c->conn))
1063 flags |= CS_FL_EOS;
1064
1065 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1066 while (node) {
1067 h2s = container_of(node, struct h2s, by_id);
1068 if (h2s->id <= last)
1069 break;
1070 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001071
1072 if (!h2s->cs) {
1073 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001074 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001075 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001076 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001077
1078 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001079 h2s->cs->data_cb->wake(h2s->cs);
1080
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001081 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1082 h2s->st = H2_SS_ERROR;
1083 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1084 h2s->st = H2_SS_HREM;
1085 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001086 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001087 }
1088}
1089
Willy Tarreau3421aba2017-07-27 15:41:03 +02001090/* Increase all streams' outgoing window size by the difference passed in
1091 * argument. This is needed upon receipt of the settings frame if the initial
1092 * window size is different. The difference may be negative and the resulting
1093 * window size as well, for the time it takes to receive some window updates.
1094 */
1095static void h2c_update_all_ws(struct h2c *h2c, int diff)
1096{
1097 struct h2s *h2s;
1098 struct eb32_node *node;
1099
1100 if (!diff)
1101 return;
1102
1103 node = eb32_first(&h2c->streams_by_id);
1104 while (node) {
1105 h2s = container_of(node, struct h2s, by_id);
1106 h2s->mws += diff;
1107 node = eb32_next(node);
1108 }
1109}
1110
1111/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1112 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1113 * return an error in h2c. Described in RFC7540#6.5.
1114 */
1115static int h2c_handle_settings(struct h2c *h2c)
1116{
1117 unsigned int offset;
1118 int error;
1119
1120 if (h2c->dff & H2_F_SETTINGS_ACK) {
1121 if (h2c->dfl) {
1122 error = H2_ERR_FRAME_SIZE_ERROR;
1123 goto fail;
1124 }
1125 return 1;
1126 }
1127
1128 if (h2c->dsi != 0) {
1129 error = H2_ERR_PROTOCOL_ERROR;
1130 goto fail;
1131 }
1132
1133 if (h2c->dfl % 6) {
1134 error = H2_ERR_FRAME_SIZE_ERROR;
1135 goto fail;
1136 }
1137
1138 /* that's the limit we can process */
1139 if (h2c->dfl > global.tune.bufsize) {
1140 error = H2_ERR_FRAME_SIZE_ERROR;
1141 goto fail;
1142 }
1143
1144 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001145 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001146 return 0;
1147
1148 /* parse the frame */
1149 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001150 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1151 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001152
1153 switch (type) {
1154 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1155 /* we need to update all existing streams with the
1156 * difference from the previous iws.
1157 */
1158 if (arg < 0) { // RFC7540#6.5.2
1159 error = H2_ERR_FLOW_CONTROL_ERROR;
1160 goto fail;
1161 }
1162 h2c_update_all_ws(h2c, arg - h2c->miw);
1163 h2c->miw = arg;
1164 break;
1165 case H2_SETTINGS_MAX_FRAME_SIZE:
1166 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1167 error = H2_ERR_PROTOCOL_ERROR;
1168 goto fail;
1169 }
1170 h2c->mfs = arg;
1171 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001172 case H2_SETTINGS_ENABLE_PUSH:
1173 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1174 error = H2_ERR_PROTOCOL_ERROR;
1175 goto fail;
1176 }
1177 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001178 }
1179 }
1180
1181 /* need to ACK this frame now */
1182 h2c->st0 = H2_CS_FRAME_A;
1183 return 1;
1184 fail:
1185 h2c_error(h2c, error);
1186 return 0;
1187}
1188
1189/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1190 * success or one of the h2_status values.
1191 */
1192static int h2c_ack_settings(struct h2c *h2c)
1193{
1194 struct buffer *res;
1195 char str[9];
1196 int ret = -1;
1197
1198 if (h2c_mux_busy(h2c, NULL)) {
1199 h2c->flags |= H2_CF_DEM_MBUSY;
1200 return 0;
1201 }
1202
Willy Tarreau44e973f2018-03-01 17:49:30 +01001203 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001204 if (!res) {
1205 h2c->flags |= H2_CF_MUX_MALLOC;
1206 h2c->flags |= H2_CF_DEM_MROOM;
1207 return 0;
1208 }
1209
1210 memcpy(str,
1211 "\x00\x00\x00" /* length : 0 (no data) */
1212 "\x04" "\x01" /* type : 4, flags : ACK */
1213 "\x00\x00\x00\x00" /* stream ID */, 9);
1214
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001215 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001216 if (unlikely(ret <= 0)) {
1217 if (!ret) {
1218 h2c->flags |= H2_CF_MUX_MFULL;
1219 h2c->flags |= H2_CF_DEM_MROOM;
1220 return 0;
1221 }
1222 else {
1223 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1224 return 0;
1225 }
1226 }
1227 return ret;
1228}
1229
Willy Tarreaucf68c782017-10-10 17:11:41 +02001230/* processes a PING frame and schedules an ACK if needed. The caller must pass
1231 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1232 * missing data. It may return an error in h2c.
1233 */
1234static int h2c_handle_ping(struct h2c *h2c)
1235{
1236 /* frame length must be exactly 8 */
1237 if (h2c->dfl != 8) {
1238 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1239 return 0;
1240 }
1241
1242 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001243 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001244 h2c->st0 = H2_CS_FRAME_A;
1245 return 1;
1246}
1247
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001248/* Try to send a window update for stream id <sid> and value <increment>.
1249 * Returns > 0 on success or zero on missing room or failure. It may return an
1250 * error in h2c.
1251 */
1252static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1253{
1254 struct buffer *res;
1255 char str[13];
1256 int ret = -1;
1257
1258 if (h2c_mux_busy(h2c, NULL)) {
1259 h2c->flags |= H2_CF_DEM_MBUSY;
1260 return 0;
1261 }
1262
Willy Tarreau44e973f2018-03-01 17:49:30 +01001263 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001264 if (!res) {
1265 h2c->flags |= H2_CF_MUX_MALLOC;
1266 h2c->flags |= H2_CF_DEM_MROOM;
1267 return 0;
1268 }
1269
1270 /* length: 4, type: 8, flags: none */
1271 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1272 write_n32(str + 5, sid);
1273 write_n32(str + 9, increment);
1274
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001275 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001276
1277 if (unlikely(ret <= 0)) {
1278 if (!ret) {
1279 h2c->flags |= H2_CF_MUX_MFULL;
1280 h2c->flags |= H2_CF_DEM_MROOM;
1281 return 0;
1282 }
1283 else {
1284 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1285 return 0;
1286 }
1287 }
1288 return ret;
1289}
1290
1291/* try to send pending window update for the connection. It's safe to call it
1292 * with no pending updates. Returns > 0 on success or zero on missing room or
1293 * failure. It may return an error in h2c.
1294 */
1295static int h2c_send_conn_wu(struct h2c *h2c)
1296{
1297 int ret = 1;
1298
1299 if (h2c->rcvd_c <= 0)
1300 return 1;
1301
1302 /* send WU for the connection */
1303 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1304 if (ret > 0)
1305 h2c->rcvd_c = 0;
1306
1307 return ret;
1308}
1309
1310/* try to send pending window update for the current dmux stream. It's safe to
1311 * call it with no pending updates. Returns > 0 on success or zero on missing
1312 * room or failure. It may return an error in h2c.
1313 */
1314static int h2c_send_strm_wu(struct h2c *h2c)
1315{
1316 int ret = 1;
1317
1318 if (h2c->rcvd_s <= 0)
1319 return 1;
1320
1321 /* send WU for the stream */
1322 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1323 if (ret > 0)
1324 h2c->rcvd_s = 0;
1325
1326 return ret;
1327}
1328
Willy Tarreaucf68c782017-10-10 17:11:41 +02001329/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1330 * success, 0 on missing data or one of the h2_status values.
1331 */
1332static int h2c_ack_ping(struct h2c *h2c)
1333{
1334 struct buffer *res;
1335 char str[17];
1336 int ret = -1;
1337
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001338 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001339 return 0;
1340
1341 if (h2c_mux_busy(h2c, NULL)) {
1342 h2c->flags |= H2_CF_DEM_MBUSY;
1343 return 0;
1344 }
1345
Willy Tarreau44e973f2018-03-01 17:49:30 +01001346 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001347 if (!res) {
1348 h2c->flags |= H2_CF_MUX_MALLOC;
1349 h2c->flags |= H2_CF_DEM_MROOM;
1350 return 0;
1351 }
1352
1353 memcpy(str,
1354 "\x00\x00\x08" /* length : 8 (same payload) */
1355 "\x06" "\x01" /* type : 6, flags : ACK */
1356 "\x00\x00\x00\x00" /* stream ID */, 9);
1357
1358 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001359 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001360
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001361 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001362 if (unlikely(ret <= 0)) {
1363 if (!ret) {
1364 h2c->flags |= H2_CF_MUX_MFULL;
1365 h2c->flags |= H2_CF_DEM_MROOM;
1366 return 0;
1367 }
1368 else {
1369 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1370 return 0;
1371 }
1372 }
1373 return ret;
1374}
1375
Willy Tarreau26f95952017-07-27 17:18:30 +02001376/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1377 * Returns > 0 on success or zero on missing data. It may return an error in
1378 * h2c or h2s. Described in RFC7540#6.9.
1379 */
1380static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1381{
1382 int32_t inc;
1383 int error;
1384
1385 if (h2c->dfl != 4) {
1386 error = H2_ERR_FRAME_SIZE_ERROR;
1387 goto conn_err;
1388 }
1389
1390 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001391 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001392 return 0;
1393
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001394 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001395
1396 if (h2c->dsi != 0) {
1397 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001398
1399 /* it's not an error to receive WU on a closed stream */
1400 if (h2s->st == H2_SS_CLOSED)
1401 return 1;
1402
1403 if (!inc) {
1404 error = H2_ERR_PROTOCOL_ERROR;
1405 goto strm_err;
1406 }
1407
1408 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1409 error = H2_ERR_FLOW_CONTROL_ERROR;
1410 goto strm_err;
1411 }
1412
1413 h2s->mws += inc;
1414 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1415 h2s->flags &= ~H2_SF_BLK_SFCTL;
1416 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1417 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1418 /* This stream wanted to send but could not due to its
1419 * own flow control. We can put it back into the send
1420 * list now, it will be handled upon next send() call.
1421 */
1422 LIST_ADDQ(&h2c->send_list, &h2s->list);
1423 }
1424 }
1425 }
1426 else {
1427 /* connection window update */
1428 if (!inc) {
1429 error = H2_ERR_PROTOCOL_ERROR;
1430 goto conn_err;
1431 }
1432
1433 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1434 error = H2_ERR_FLOW_CONTROL_ERROR;
1435 goto conn_err;
1436 }
1437
1438 h2c->mws += inc;
1439 }
1440
1441 return 1;
1442
1443 conn_err:
1444 h2c_error(h2c, error);
1445 return 0;
1446
1447 strm_err:
1448 if (h2s) {
1449 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001450 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001451 }
1452 else
1453 h2c_error(h2c, error);
1454 return 0;
1455}
1456
Willy Tarreaue96b0922017-10-30 00:28:29 +01001457/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1458 * the last ID. Returns > 0 on success or zero on missing data. It may return
1459 * an error in h2c. Described in RFC7540#6.8.
1460 */
1461static int h2c_handle_goaway(struct h2c *h2c)
1462{
1463 int error;
1464 int last;
1465
1466 if (h2c->dsi != 0) {
1467 error = H2_ERR_PROTOCOL_ERROR;
1468 goto conn_err;
1469 }
1470
1471 if (h2c->dfl < 8) {
1472 error = H2_ERR_FRAME_SIZE_ERROR;
1473 goto conn_err;
1474 }
1475
1476 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001477 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001478 return 0;
1479
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001480 last = h2_get_n32(&h2c->dbuf, 0);
1481 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001482 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001483 if (h2c->last_sid < 0)
1484 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001485 return 1;
1486
1487 conn_err:
1488 h2c_error(h2c, error);
1489 return 0;
1490}
1491
Willy Tarreau92153fc2017-12-03 19:46:19 +01001492/* processes a PRIORITY frame, and either skips it or rejects if it is
1493 * invalid. Returns > 0 on success or zero on missing data. It may return
1494 * an error in h2c. Described in RFC7540#6.3.
1495 */
1496static int h2c_handle_priority(struct h2c *h2c)
1497{
1498 int error;
1499
1500 if (h2c->dsi == 0) {
1501 error = H2_ERR_PROTOCOL_ERROR;
1502 goto conn_err;
1503 }
1504
1505 if (h2c->dfl != 5) {
1506 error = H2_ERR_FRAME_SIZE_ERROR;
1507 goto conn_err;
1508 }
1509
1510 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001511 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001512 return 0;
1513
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001514 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001515 /* 7540#5.3 : can't depend on itself */
1516 error = H2_ERR_PROTOCOL_ERROR;
1517 goto conn_err;
1518 }
1519 return 1;
1520
1521 conn_err:
1522 h2c_error(h2c, error);
1523 return 0;
1524}
1525
Willy Tarreaucd234e92017-08-18 10:59:39 +02001526/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1527 * Returns > 0 on success or zero on missing data. It may return an error in
1528 * h2c. Described in RFC7540#6.4.
1529 */
1530static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1531{
1532 int error;
1533
1534 if (h2c->dsi == 0) {
1535 error = H2_ERR_PROTOCOL_ERROR;
1536 goto conn_err;
1537 }
1538
Willy Tarreaucd234e92017-08-18 10:59:39 +02001539 if (h2c->dfl != 4) {
1540 error = H2_ERR_FRAME_SIZE_ERROR;
1541 goto conn_err;
1542 }
1543
1544 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001545 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001546 return 0;
1547
1548 /* late RST, already handled */
1549 if (h2s->st == H2_SS_CLOSED)
1550 return 1;
1551
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001552 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001553 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001554
1555 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001556 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001557 h2s->cs->data_cb->wake(h2s->cs);
1558 }
1559
1560 h2s->flags |= H2_SF_RST_RCVD;
1561 return 1;
1562
1563 conn_err:
1564 h2c_error(h2c, error);
1565 return 0;
1566}
1567
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001568/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1569 * It may return an error in h2c or h2s. The caller must consider that the
1570 * return value is the new h2s in case one was allocated (most common case).
1571 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001572 * errors here are reported as connection errors since it's impossible to
1573 * recover from such errors after the compression context has been altered.
1574 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001575static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001576{
1577 int error;
1578
1579 if (!h2c->dfl) {
1580 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1581 goto strm_err;
1582 }
1583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001584 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001585 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001586
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001587 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001588 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001589
Willy Tarreauf2101912018-07-19 10:11:38 +02001590 if (h2c->flags & H2_CF_DEM_TOOMANY)
1591 return 0; // too many cs still present
1592
Willy Tarreau13278b42017-10-13 19:23:14 +02001593 /* now either the frame is complete or the buffer is complete */
1594 if (h2s->st != H2_SS_IDLE) {
1595 /* FIXME: stream already exists, this is only allowed for
1596 * trailers (not supported for now).
1597 */
1598 error = H2_ERR_PROTOCOL_ERROR;
1599 goto conn_err;
1600 }
1601 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1602 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1603 error = H2_ERR_PROTOCOL_ERROR;
1604 goto conn_err;
1605 }
1606
1607 h2s = h2c_stream_new(h2c, h2c->dsi);
1608 if (!h2s) {
1609 error = H2_ERR_INTERNAL_ERROR;
1610 goto conn_err;
1611 }
1612
1613 h2s->st = H2_SS_OPEN;
1614 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1615 h2s->st = H2_SS_HREM;
1616 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001617 /* note: cs cannot be null for now (just created above) */
1618 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001619 }
1620
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001621 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001622 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001623
Willy Tarreau8f650c32017-11-21 19:36:21 +01001624 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001625 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001626
Willy Tarreau721c9742017-11-07 11:05:42 +01001627 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001628 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001629 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001630 }
1631 else {
1632 /* update the max stream ID if the request is being processed */
1633 if (h2s->id > h2c->max_id)
1634 h2c->max_id = h2s->id;
1635 }
1636
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001637 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001638
1639 conn_err:
1640 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001641 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001642
1643 strm_err:
1644 if (h2s) {
1645 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001646 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001647 }
1648 else
1649 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001650 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001651}
1652
Willy Tarreau454f9052017-10-26 19:40:35 +02001653/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1654 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1655 */
1656static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1657{
1658 int error;
1659
1660 /* note that empty DATA frames are perfectly valid and sometimes used
1661 * to signal an end of stream (with the ES flag).
1662 */
1663
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001664 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001665 return 0; // empty buffer
1666
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001667 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001668 return 0; // incomplete frame
1669
1670 /* now either the frame is complete or the buffer is complete */
1671
1672 if (!h2c->dsi) {
1673 /* RFC7540#6.1 */
1674 error = H2_ERR_PROTOCOL_ERROR;
1675 goto conn_err;
1676 }
1677
1678 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1679 /* RFC7540#6.1 */
1680 error = H2_ERR_STREAM_CLOSED;
1681 goto strm_err;
1682 }
1683
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001684 if (!h2_frt_transfer_data(h2s))
1685 return 0;
1686
Willy Tarreau454f9052017-10-26 19:40:35 +02001687 /* call the upper layers to process the frame, then let the upper layer
1688 * notify the stream about any change.
1689 */
1690 if (!h2s->cs) {
1691 error = H2_ERR_STREAM_CLOSED;
1692 goto strm_err;
1693 }
1694
Willy Tarreau8f650c32017-11-21 19:36:21 +01001695 if (h2c->st0 >= H2_CS_ERROR)
1696 return 0;
1697
Willy Tarreau721c9742017-11-07 11:05:42 +01001698 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001699 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001700 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001701 }
1702
1703 /* check for completion : the callee will change this to FRAME_A or
1704 * FRAME_H once done.
1705 */
1706 if (h2c->st0 == H2_CS_FRAME_P)
1707 return 0;
1708
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001709
1710 /* last frame */
1711 if (h2c->dff & H2_F_DATA_END_STREAM) {
1712 h2s->st = H2_SS_HREM;
1713 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001714 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001715 }
1716
Willy Tarreau454f9052017-10-26 19:40:35 +02001717 return 1;
1718
1719 conn_err:
1720 h2c_error(h2c, error);
1721 return 0;
1722
1723 strm_err:
1724 if (h2s) {
1725 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001726 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001727 }
1728 else
1729 h2c_error(h2c, error);
1730 return 0;
1731}
1732
Willy Tarreaubc933932017-10-09 16:21:43 +02001733/* process Rx frames to be demultiplexed */
1734static void h2_process_demux(struct h2c *h2c)
1735{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001736 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001737
Willy Tarreau081d4722017-05-16 21:51:05 +02001738 if (h2c->st0 >= H2_CS_ERROR)
1739 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001740
1741 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1742 if (h2c->st0 == H2_CS_PREFACE) {
1743 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1744 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1745 if (h2c->st0 == H2_CS_ERROR)
1746 h2c->st0 = H2_CS_ERROR2;
1747 goto fail;
1748 }
1749
1750 h2c->max_id = 0;
1751 h2c->st0 = H2_CS_SETTINGS1;
1752 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001753
1754 if (h2c->st0 == H2_CS_SETTINGS1) {
1755 struct h2_fh hdr;
1756
1757 /* ensure that what is pending is a valid SETTINGS frame
1758 * without an ACK.
1759 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001760 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001761 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1762 if (h2c->st0 == H2_CS_ERROR)
1763 h2c->st0 = H2_CS_ERROR2;
1764 goto fail;
1765 }
1766
1767 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1768 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1769 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1770 h2c->st0 = H2_CS_ERROR2;
1771 goto fail;
1772 }
1773
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001774 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001775 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1776 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1777 h2c->st0 = H2_CS_ERROR2;
1778 goto fail;
1779 }
1780
1781 /* that's OK, switch to FRAME_P to process it */
1782 h2c->dfl = hdr.len;
1783 h2c->dsi = hdr.sid;
1784 h2c->dft = hdr.ft;
1785 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001786 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001787 h2c->st0 = H2_CS_FRAME_P;
1788 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001789 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001790
1791 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001792 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001793 int ret = 0;
1794
1795 if (h2c->st0 >= H2_CS_ERROR)
1796 break;
1797
1798 if (h2c->st0 == H2_CS_FRAME_H) {
1799 struct h2_fh hdr;
1800
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001801 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001802 break;
1803
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001804 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001805 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1806 h2c->st0 = H2_CS_ERROR;
1807 break;
1808 }
1809
1810 h2c->dfl = hdr.len;
1811 h2c->dsi = hdr.sid;
1812 h2c->dft = hdr.ft;
1813 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001814 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001815 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001816 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001817 }
1818
1819 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001820 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1821
Olivier Houchard638b7992018-08-16 15:41:52 +02001822 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001823 /* we may have to signal the upper layers */
1824 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001825 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1826 /* cs has just been destroyed, we have to kill h2s. */
1827 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1828 goto strm_err;
1829 }
1830
1831 if (h2c->st0 >= H2_CS_ERROR)
1832 goto strm_err;
1833
1834 if (h2s->st >= H2_SS_ERROR) {
1835 /* stream error : send RST_STREAM */
1836 h2c->st0 = H2_CS_FRAME_E;
1837 }
1838 }
1839 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001840
Willy Tarreaud7901432017-12-29 11:34:40 +01001841 if (h2c->st0 == H2_CS_FRAME_E)
1842 goto strm_err;
1843
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001844 if (h2s->st == H2_SS_IDLE &&
1845 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1846 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1847 * this state MUST be treated as a connection error
1848 */
1849 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1850 h2c->st0 = H2_CS_ERROR;
1851 break;
1852 }
1853
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001854 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1855 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1856 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1857 * this state MUST be treated as a stream error
1858 */
1859 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1860 goto strm_err;
1861 }
1862
Willy Tarreauab837502017-12-27 15:07:30 +01001863 /* Below the management of frames received in closed state is a
1864 * bit hackish because the spec makes strong differences between
1865 * streams closed by receiving RST, sending RST, and seeing ES
1866 * in both directions. In addition to this, the creation of a
1867 * new stream reusing the identifier of a closed one will be
1868 * detected here. Given that we cannot keep track of all closed
1869 * streams forever, we consider that unknown closed streams were
1870 * closed on RST received, which allows us to respond with an
1871 * RST without breaking the connection (eg: to abort a transfer).
1872 * Some frames have to be silently ignored as well.
1873 */
1874 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1875 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1876 /* #5.1.1: The identifier of a newly
1877 * established stream MUST be numerically
1878 * greater than all streams that the initiating
1879 * endpoint has opened or reserved. This
1880 * governs streams that are opened using a
1881 * HEADERS frame and streams that are reserved
1882 * using PUSH_PROMISE. An endpoint that
1883 * receives an unexpected stream identifier
1884 * MUST respond with a connection error.
1885 */
1886 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1887 goto strm_err;
1888 }
1889
1890 if (h2s->flags & H2_SF_RST_RCVD) {
1891 /* RFC7540#5.1:closed: an endpoint that
1892 * receives any frame other than PRIORITY after
1893 * receiving a RST_STREAM MUST treat that as a
1894 * stream error of type STREAM_CLOSED.
1895 *
1896 * Note that old streams fall into this category
1897 * and will lead to an RST being sent.
1898 */
1899 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1900 h2c->st0 = H2_CS_FRAME_E;
1901 goto strm_err;
1902 }
1903
1904 /* RFC7540#5.1:closed: if this state is reached as a
1905 * result of sending a RST_STREAM frame, the peer that
1906 * receives the RST_STREAM might have already sent
1907 * frames on the stream that cannot be withdrawn. An
1908 * endpoint MUST ignore frames that it receives on
1909 * closed streams after it has sent a RST_STREAM
1910 * frame. An endpoint MAY choose to limit the period
1911 * over which it ignores frames and treat frames that
1912 * arrive after this time as being in error.
1913 */
1914 if (!(h2s->flags & H2_SF_RST_SENT)) {
1915 /* RFC7540#5.1:closed: any frame other than
1916 * PRIO/WU/RST in this state MUST be treated as
1917 * a connection error
1918 */
1919 if (h2c->dft != H2_FT_RST_STREAM &&
1920 h2c->dft != H2_FT_PRIORITY &&
1921 h2c->dft != H2_FT_WINDOW_UPDATE) {
1922 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1923 goto strm_err;
1924 }
1925 }
1926 }
1927
Willy Tarreauc0da1962017-10-30 18:38:00 +01001928#if 0
1929 // problem below: it is not possible to completely ignore such
1930 // streams as we need to maintain the compression state as well
1931 // and for this we need to completely process these frames (eg:
1932 // HEADERS frames) as well as counting DATA frames to emit
1933 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1934 // This is a typical case of layer violation where the
1935 // transported contents are critical to the connection's
1936 // validity and must be ignored at the same time :-(
1937
1938 /* graceful shutdown, ignore streams whose ID is higher than
1939 * the one advertised in GOAWAY. RFC7540#6.8.
1940 */
1941 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001942 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1943 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001944 h2c->dfl -= ret;
1945 ret = h2c->dfl == 0;
1946 goto strm_err;
1947 }
1948#endif
1949
Willy Tarreau7e98c052017-10-10 15:56:59 +02001950 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001951 case H2_FT_SETTINGS:
1952 if (h2c->st0 == H2_CS_FRAME_P)
1953 ret = h2c_handle_settings(h2c);
1954
1955 if (h2c->st0 == H2_CS_FRAME_A)
1956 ret = h2c_ack_settings(h2c);
1957 break;
1958
Willy Tarreaucf68c782017-10-10 17:11:41 +02001959 case H2_FT_PING:
1960 if (h2c->st0 == H2_CS_FRAME_P)
1961 ret = h2c_handle_ping(h2c);
1962
1963 if (h2c->st0 == H2_CS_FRAME_A)
1964 ret = h2c_ack_ping(h2c);
1965 break;
1966
Willy Tarreau26f95952017-07-27 17:18:30 +02001967 case H2_FT_WINDOW_UPDATE:
1968 if (h2c->st0 == H2_CS_FRAME_P)
1969 ret = h2c_handle_window_update(h2c, h2s);
1970 break;
1971
Willy Tarreau61290ec2017-10-17 08:19:21 +02001972 case H2_FT_CONTINUATION:
1973 /* we currently don't support CONTINUATION frames since
1974 * we have nowhere to store the partial HEADERS frame.
1975 * Let's abort the stream on an INTERNAL_ERROR here.
1976 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001977 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001978 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001979 h2c->st0 = H2_CS_FRAME_E;
1980 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001981 break;
1982
Willy Tarreau13278b42017-10-13 19:23:14 +02001983 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001984 if (h2c->st0 == H2_CS_FRAME_P) {
1985 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
1986 if (tmp_h2s) {
1987 h2s = tmp_h2s;
1988 ret = 1;
1989 }
1990 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001991 break;
1992
Willy Tarreau454f9052017-10-26 19:40:35 +02001993 case H2_FT_DATA:
1994 if (h2c->st0 == H2_CS_FRAME_P)
1995 ret = h2c_frt_handle_data(h2c, h2s);
1996
1997 if (h2c->st0 == H2_CS_FRAME_A)
1998 ret = h2c_send_strm_wu(h2c);
1999 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002000
Willy Tarreau92153fc2017-12-03 19:46:19 +01002001 case H2_FT_PRIORITY:
2002 if (h2c->st0 == H2_CS_FRAME_P)
2003 ret = h2c_handle_priority(h2c);
2004 break;
2005
Willy Tarreaucd234e92017-08-18 10:59:39 +02002006 case H2_FT_RST_STREAM:
2007 if (h2c->st0 == H2_CS_FRAME_P)
2008 ret = h2c_handle_rst_stream(h2c, h2s);
2009 break;
2010
Willy Tarreaue96b0922017-10-30 00:28:29 +01002011 case H2_FT_GOAWAY:
2012 if (h2c->st0 == H2_CS_FRAME_P)
2013 ret = h2c_handle_goaway(h2c);
2014 break;
2015
Willy Tarreau1c661982017-10-30 13:52:01 +01002016 case H2_FT_PUSH_PROMISE:
2017 /* not permitted here, RFC7540#5.1 */
2018 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002019 break;
2020
2021 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002022 default:
2023 /* drop frames that we ignore. They may be larger than
2024 * the buffer so we drain all of their contents until
2025 * we reach the end.
2026 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002027 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2028 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002029 h2c->dfl -= ret;
2030 ret = h2c->dfl == 0;
2031 }
2032
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002033 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002034 /* We may have to send an RST if not done yet */
2035 if (h2s->st == H2_SS_ERROR)
2036 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002037
Willy Tarreaua20a5192017-12-27 11:02:06 +01002038 if (h2c->st0 == H2_CS_FRAME_E)
2039 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002040
Willy Tarreau7e98c052017-10-10 15:56:59 +02002041 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002042 if (ret <= 0) {
2043 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002044 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002045 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002046
2047 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002048 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002049 h2c->st0 = H2_CS_FRAME_H;
2050 }
2051 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002052
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002053 if (h2c->rcvd_c > 0 &&
2054 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2055 h2c_send_conn_wu(h2c);
2056
Willy Tarreau52eed752017-09-22 15:05:09 +02002057 fail:
2058 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002059 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002060 /* we may have to signal the upper layers */
2061 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002062 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2063 /* cs has just been destroyed, we have to kill h2s. */
2064 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2065 h2c_send_rst_stream(h2c, h2s);
2066 }
2067 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002068 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002069}
2070
2071/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2072 * the end.
2073 */
2074static int h2_process_mux(struct h2c *h2c)
2075{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002076 struct h2s *h2s, *h2s_back;
2077
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002078 /* start by sending possibly pending window updates */
2079 if (h2c->rcvd_c > 0 &&
2080 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2081 h2c_send_conn_wu(h2c) < 0)
2082 goto fail;
2083
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002084 /* First we always process the flow control list because the streams
2085 * waiting there were already elected for immediate emission but were
2086 * blocked just on this.
2087 */
2088
2089 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2090 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2091 h2c->st0 >= H2_CS_ERROR)
2092 break;
2093
2094 /* In theory it's possible that h2s->cs == NULL here :
2095 * - client sends crap that causes a parse error
2096 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2097 * - RST_STREAM cannot be emitted because mux is busy/full
2098 * - stream gets notified, detaches and quits
2099 * - mux buffer gets ready and wakes pending streams up
2100 * - bam!
2101 */
2102 h2s->flags &= ~H2_SF_BLK_ANY;
2103
2104 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002105 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002106 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002107 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002108 }
2109
2110 /* depending on callee's blocking reasons, we may queue in send
2111 * list or completely dequeue.
2112 */
2113 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2114 if (h2s->flags & H2_SF_BLK_ANY) {
2115 LIST_DEL(&h2s->list);
2116 LIST_ADDQ(&h2c->send_list, &h2s->list);
2117 }
2118 else {
2119 LIST_DEL(&h2s->list);
2120 LIST_INIT(&h2s->list);
2121 if (h2s->cs)
2122 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002123 else {
2124 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002125 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002126 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002127 }
2128 }
2129 }
2130
2131 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2132 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2133 break;
2134
2135 /* In theory it's possible that h2s->cs == NULL here :
2136 * - client sends crap that causes a parse error
2137 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2138 * - RST_STREAM cannot be emitted because mux is busy/full
2139 * - stream gets notified, detaches and quits
2140 * - mux buffer gets ready and wakes pending streams up
2141 * - bam!
2142 */
2143 h2s->flags &= ~H2_SF_BLK_ANY;
2144
2145 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002146 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002147 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002148 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002149 }
2150 /* depending on callee's blocking reasons, we may queue in fctl
2151 * list or completely dequeue.
2152 */
2153 if (h2s->flags & H2_SF_BLK_MFCTL) {
2154 /* stream hit the connection's flow control */
2155 LIST_DEL(&h2s->list);
2156 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2157 }
2158 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2159 LIST_DEL(&h2s->list);
2160 LIST_INIT(&h2s->list);
2161 if (h2s->cs)
2162 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002163 else {
2164 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002165 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002166 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002167 }
2168 }
2169
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002170 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002171 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002172 if (h2c->st0 == H2_CS_ERROR) {
2173 if (h2c->max_id >= 0) {
2174 h2c_send_goaway_error(h2c, NULL);
2175 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2176 return 0;
2177 }
2178
2179 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2180 }
2181 return 1;
2182 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002183 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002184}
2185
Willy Tarreau71681172017-10-23 14:39:06 +02002186
Willy Tarreau62f52692017-10-08 23:01:42 +02002187/*********************************************************/
2188/* functions below are I/O callbacks from the connection */
2189/*********************************************************/
2190
2191/* callback called on recv event by the connection handler */
2192static void h2_recv(struct connection *conn)
2193{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002194 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002195 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002196 int max;
2197
Willy Tarreau315d8072017-12-10 22:17:57 +01002198 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002199 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002200
Willy Tarreau44e973f2018-03-01 17:49:30 +01002201 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002202 if (!buf) {
2203 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002204 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002205 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002206
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002207 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002208 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002209 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002210
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002211 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002212 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002213 return;
2214 }
2215
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002216 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002217 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002218 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002219}
2220
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002221/* Try to send data if possible */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002222static void h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002223{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002224 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002225 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226
2227 if (conn->flags & CO_FL_ERROR)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002228 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002229
2230 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2231 /* a handshake was requested */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002232 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233 }
2234
Willy Tarreaubc933932017-10-09 16:21:43 +02002235 /* This loop is quite simple : it tries to fill as much as it can from
2236 * pending streams into the existing buffer until it's reportedly full
2237 * or the end of send requests is reached. Then it tries to send this
2238 * buffer's contents out, marks it not full if at least one byte could
2239 * be sent, and tries again.
2240 *
2241 * The snd_buf() function normally takes a "flags" argument which may
2242 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2243 * data immediately comes and CO_SFL_STREAMER to indicate that the
2244 * connection is streaming lots of data (used to increase TLS record
2245 * size at the expense of latency). The former can be sent any time
2246 * there's a buffer full flag, as it indicates at least one stream
2247 * attempted to send and failed so there are pending data. An
2248 * alternative would be to set it as long as there's an active stream
2249 * but that would be problematic for ACKs until we have an absolute
2250 * guarantee that all waiters have at least one byte to send. The
2251 * latter should possibly not be set for now.
2252 */
2253
2254 done = 0;
2255 while (!done) {
2256 unsigned int flags = 0;
2257
2258 /* fill as much as we can into the current buffer */
2259 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2260 done = h2_process_mux(h2c);
2261
2262 if (conn->flags & CO_FL_ERROR)
2263 break;
2264
2265 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2266 flags |= CO_SFL_MSG_MORE;
2267
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002268 if (b_data(&h2c->mbuf)) {
2269 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002270 if (!ret)
2271 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 b_del(&h2c->mbuf, ret);
2273 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002274 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002275
2276 /* wrote at least one byte, the buffer is not full anymore */
2277 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2278 }
2279
Willy Tarreaua2af5122017-10-09 11:56:46 +02002280 if (conn->flags & CO_FL_SOCK_WR_SH) {
2281 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002282 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002283 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002284 /* We're not full anymore, so we can wake any task that are waiting
2285 * for us.
2286 */
2287 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2288 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2289 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2290 struct wait_list *, list);
2291 LIST_DEL(&sw->list);
2292 LIST_INIT(&sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02002293 sw->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02002294 tasklet_wakeup(sw->task);
2295 }
2296
2297 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002298 /* We're done, no more to send */
2299 if (!b_data(&h2c->mbuf))
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002300 return;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002301schedule:
2302 if (LIST_ISEMPTY(&h2c->wait_list.list))
2303 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002304 return;
2305}
2306
2307static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2308{
2309 struct h2c *h2c = ctx;
2310
2311 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
2312 h2_send(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002313 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002314}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002315
Willy Tarreau62f52692017-10-08 23:01:42 +02002316/* callback called on any event by the connection handler.
2317 * It applies changes and returns zero, or < 0 if it wants immediate
2318 * destruction of the connection (which normally doesn not happen in h2).
2319 */
2320static int h2_wake(struct connection *conn)
2321{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002322 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002323 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002324
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002325 h2_send(h2c);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002327 h2_process_demux(h2c);
2328
2329 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002331
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002332 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002333 h2c->flags &= ~H2_CF_DEM_DFULL;
2334 }
2335
Willy Tarreau8ec14062017-12-30 18:08:13 +01002336 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2337 /* frontend is stopping, reload likely in progress, let's try
2338 * to announce a graceful shutdown if not yet done. We don't
2339 * care if it fails, it will be tried again later.
2340 */
2341 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2342 if (h2c->last_sid < 0)
2343 h2c->last_sid = (1U << 31) - 1;
2344 h2c_send_goaway_error(h2c, NULL);
2345 }
2346 }
2347
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002348 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002349 * If we received early data, and the handshake is done, wake
2350 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002351 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002352 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2353 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2354 struct eb32_node *node;
2355 struct h2s *h2s;
2356
2357 h2c->flags |= H2_CF_WAIT_FOR_HS;
2358 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2359
2360 while (node) {
2361 h2s = container_of(node, struct h2s, by_id);
2362 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2363 h2s->cs->data_cb->wake(h2s->cs);
2364 node = eb32_next(node);
2365 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002366 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002367
Willy Tarreau26bd7612017-10-09 16:47:04 +02002368 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002369 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2370 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2371 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002372 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002373
2374 if (eb_is_empty(&h2c->streams_by_id)) {
2375 /* no more stream, kill the connection now */
2376 h2_release(conn);
2377 return -1;
2378 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002379 }
2380
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002381 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002382 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002383
2384 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002385 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002386 __conn_xprt_stop_recv(conn);
2387 }
2388 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002389 __conn_xprt_want_recv(conn);
2390 }
2391
2392 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002393 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002394 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002395 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002396 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002397 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2398 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002399 __conn_xprt_want_send(conn);
2400 }
2401 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002402 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002403 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002404 }
2405
Willy Tarreau3f133572017-10-31 19:21:06 +01002406 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002407 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002408 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002409 task_queue(h2c->task);
2410 }
2411 else
2412 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002413 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002414
Willy Tarreau62f52692017-10-08 23:01:42 +02002415 return 0;
2416}
2417
Willy Tarreauea392822017-10-31 10:02:25 +01002418/* Connection timeout management. The principle is that if there's no receipt
2419 * nor sending for a certain amount of time, the connection is closed. If the
2420 * MUX buffer still has lying data or is not allocatable, the connection is
2421 * immediately killed. If it's allocatable and empty, we attempt to send a
2422 * GOAWAY frame.
2423 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002424static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002425{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002426 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002427 int expired = tick_is_expired(t->expire, now_ms);
2428
Willy Tarreau0975f112018-03-29 15:22:59 +02002429 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002430 return t;
2431
Willy Tarreau0975f112018-03-29 15:22:59 +02002432 task_delete(t);
2433 task_free(t);
2434
2435 if (!h2c) {
2436 /* resources were already deleted */
2437 return NULL;
2438 }
2439
2440 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002441 h2c_error(h2c, H2_ERR_NO_ERROR);
2442 h2_wake_some_streams(h2c, 0, 0);
2443
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002444 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002445 /* don't even try to send a GOAWAY, the buffer is stuck */
2446 h2c->flags |= H2_CF_GOAWAY_FAILED;
2447 }
2448
2449 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002450 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002451 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2452 h2c->flags |= H2_CF_GOAWAY_FAILED;
2453
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2455 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002456 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002457 b_del(&h2c->mbuf, ret);
2458 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002459 }
2460 }
Willy Tarreauea392822017-10-31 10:02:25 +01002461
Willy Tarreau0975f112018-03-29 15:22:59 +02002462 /* either we can release everything now or it will be done later once
2463 * the last stream closes.
2464 */
2465 if (eb_is_empty(&h2c->streams_by_id))
2466 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002467
Willy Tarreauea392822017-10-31 10:02:25 +01002468 return NULL;
2469}
2470
2471
Willy Tarreau62f52692017-10-08 23:01:42 +02002472/*******************************************/
2473/* functions below are used by the streams */
2474/*******************************************/
2475
2476/*
2477 * Attach a new stream to a connection
2478 * (Used for outgoing connections)
2479 */
2480static struct conn_stream *h2_attach(struct connection *conn)
2481{
2482 return NULL;
2483}
2484
2485/* callback used to update the mux's polling flags after changing a cs' status.
2486 * The caller (cs_update_mux_polling) will take care of propagating any changes
2487 * to the transport layer.
2488 */
2489static void h2_update_poll(struct conn_stream *cs)
2490{
Willy Tarreau1d393222017-10-17 10:26:19 +02002491 struct h2s *h2s = cs->ctx;
2492
2493 if (!h2s)
2494 return;
2495
Willy Tarreaud7739c82017-10-30 15:38:23 +01002496 /* we may unblock a blocked read */
2497
Willy Tarreau315d8072017-12-10 22:17:57 +01002498 if (cs->flags & CS_FL_DATA_RD_ENA) {
2499 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002500 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002501 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002502 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002503 conn_xprt_want_send(cs->conn);
2504 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002505 }
2506
Willy Tarreau1d393222017-10-17 10:26:19 +02002507 /* Note: the stream and stream-int code doesn't allow us to perform a
2508 * synchronous send() here unfortunately, because this code is called
2509 * as si_update() from the process_stream() context. This means that
2510 * we have to queue the current cs and defer its processing after the
2511 * connection's cs list is processed anyway.
2512 */
2513
2514 if (cs->flags & CS_FL_DATA_WR_ENA) {
2515 if (LIST_ISEMPTY(&h2s->list)) {
2516 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002517 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002518 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2519 conn_xprt_want_send(cs->conn);
2520 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2521 }
2522 }
2523 else if (!LIST_ISEMPTY(&h2s->list)) {
2524 LIST_DEL(&h2s->list);
2525 LIST_INIT(&h2s->list);
2526 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2527 }
2528
2529 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002530 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002531 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002532}
2533
2534/*
2535 * Detach the stream from the connection and possibly release the connection.
2536 */
2537static void h2_detach(struct conn_stream *cs)
2538{
Willy Tarreau60935142017-10-16 18:11:19 +02002539 struct h2s *h2s = cs->ctx;
2540 struct h2c *h2c;
2541
2542 cs->ctx = NULL;
2543 if (!h2s)
2544 return;
2545
2546 h2c = h2s->h2c;
2547 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002548 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002549 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2550 !h2_has_too_many_cs(h2c)) {
2551 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2552 if (h2_recv_allowed(h2c)) {
2553 __conn_xprt_want_recv(h2c->conn);
2554 conn_xprt_want_send(h2c->conn);
2555 }
2556 }
Willy Tarreau60935142017-10-16 18:11:19 +02002557
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002558 /* this stream may be blocked waiting for some data to leave (possibly
2559 * an ES or RST frame), so orphan it in this case.
2560 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002561 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002562 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002563 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002564 return;
2565
Willy Tarreau45f752e2017-10-30 15:44:59 +01002566 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2567 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2568 /* unblock the connection if it was blocked on this
2569 * stream.
2570 */
2571 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2572 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2573 conn_xprt_want_recv(cs->conn);
2574 conn_xprt_want_send(cs->conn);
2575 }
2576
Willy Tarreau71049cc2018-03-28 13:56:39 +02002577 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002578
Willy Tarreaue323f342018-03-28 13:51:45 +02002579 /* We don't want to close right now unless we're removing the
2580 * last stream, and either the connection is in error, or it
2581 * reached the ID already specified in a GOAWAY frame received
2582 * or sent (as seen by last_sid >= 0).
2583 */
2584 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2585 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002586 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002587 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002588 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002589 (conn_xprt_read0_pending(h2c->conn) ||
2590 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2591 /* no more stream will come, kill it now */
2592 h2_release(h2c->conn);
2593 }
2594 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002595 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002596 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2597 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002598 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002599 else
2600 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002601 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002602}
2603
2604static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2605{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002606 struct h2s *h2s = cs->ctx;
2607
2608 if (!mode)
2609 return;
2610
Willy Tarreau721c9742017-11-07 11:05:42 +01002611 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002612 return;
2613
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002614 /* if no outgoing data was seen on this stream, it means it was
2615 * closed with a "tcp-request content" rule that is normally
2616 * used to kill the connection ASAP (eg: limit abuse). In this
2617 * case we send a goaway to close the connection.
2618 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002619 if (!(h2s->flags & H2_SF_RST_SENT) &&
2620 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002621 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002622
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002623 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2624 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2625 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002626 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002627
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002628 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002629 conn_xprt_want_send(cs->conn);
2630
Willy Tarreau00dd0782018-03-01 16:31:34 +01002631 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002632
2633 add_to_list:
2634 if (LIST_ISEMPTY(&h2s->list)) {
2635 if (h2s->flags & H2_SF_BLK_MFCTL)
2636 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2637 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2638 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2639 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002640}
2641
2642static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2643{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002644 struct h2s *h2s = cs->ctx;
2645
Willy Tarreau721c9742017-11-07 11:05:42 +01002646 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002647 return;
2648
Willy Tarreau67434202017-11-06 20:20:51 +01002649 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002650 /* we can cleanly close using an empty data frame only after headers */
2651
2652 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2653 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002654 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002655
2656 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002657 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002658 else
2659 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002660 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002661 /* if no outgoing data was seen on this stream, it means it was
2662 * closed with a "tcp-request content" rule that is normally
2663 * used to kill the connection ASAP (eg: limit abuse). In this
2664 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002665 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002666 if (!(h2s->flags & H2_SF_RST_SENT) &&
2667 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002668 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002669
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002670 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2671 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002672 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002673 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002674
Willy Tarreau00dd0782018-03-01 16:31:34 +01002675 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002676 }
2677
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002678 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002679 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002680
2681 add_to_list:
2682 if (LIST_ISEMPTY(&h2s->list)) {
2683 if (h2s->flags & H2_SF_BLK_MFCTL)
2684 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2685 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2686 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2687 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002688}
2689
Willy Tarreau13278b42017-10-13 19:23:14 +02002690/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2691 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2692 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002693 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002694 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002695static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002696{
2697 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002698 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002699 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002700 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002701 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002702 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002703 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002704 int flen = h2c->dfl;
2705 int outlen = 0;
2706 int wrap;
2707 int try;
2708
2709 if (!h2c->dfl) {
2710 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002711 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002712 return 0;
2713 }
2714
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002715 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002716 return 0; // incomplete input frame
2717
Willy Tarreau13278b42017-10-13 19:23:14 +02002718 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002719 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002720 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002721 copy = alloc_trash_chunk();
2722 if (!copy) {
2723 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2724 goto fail;
2725 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2727 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2728 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002729 }
2730
2731 /* The padlen is the first byte before data, and the padding appears
2732 * after data. padlen+data+padding are included in flen.
2733 */
2734 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002735 h2c->dpl = *hdrs;
2736 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002737 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2738 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002739 return 0;
2740 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002741 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002742 hdrs += 1; // skip Pad Length
2743 }
2744
2745 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2746 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002747 if (read_n32(hdrs) == h2s->id) {
2748 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2749 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2750 return 0;//goto fail_stream;
2751 }
2752
Willy Tarreau13278b42017-10-13 19:23:14 +02002753 hdrs += 5; // stream dep = 4, weight = 1
2754 flen -= 5;
2755 }
2756
2757 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2758 * don't support this for now and can't even decompress so we have to
2759 * break the connection.
2760 */
2761 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2762 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002763 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002764 }
2765
Olivier Houchard638b7992018-08-16 15:41:52 +02002766 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002767 if (!csbuf) {
2768 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002769 goto fail;
2770 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002771
Willy Tarreau937f7602018-02-26 15:22:17 +01002772 /* we can't retry a failed decompression operation so we must be very
2773 * careful not to take any risks. In practice the output buffer is
2774 * always empty except maybe for trailers, in which case we simply have
2775 * to wait for the upper layer to finish consuming what is available.
2776 */
2777 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002778 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002779
Willy Tarreau937f7602018-02-26 15:22:17 +01002780 csbuf->head = 0;
2781 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002782
2783 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2784 sizeof(list)/sizeof(list[0]), tmp);
2785 if (outlen < 0) {
2786 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2787 goto fail;
2788 }
2789
2790 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002791 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002792 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002793
2794 if (outlen < 0) {
2795 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2796 goto fail;
2797 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002798
Willy Tarreau174b06a2018-04-25 18:13:58 +02002799 if (msgf & H2_MSGF_BODY) {
2800 /* a payload is present */
2801 if (msgf & H2_MSGF_BODY_CL)
2802 h2s->flags |= H2_SF_DATA_CLEN;
2803 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2804 h2s->flags |= H2_SF_DATA_CHNK;
2805 }
2806
Willy Tarreau13278b42017-10-13 19:23:14 +02002807 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002809 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002810 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002811
Willy Tarreau39d68502018-03-02 12:26:37 +01002812 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002813 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002814 h2s->cs->flags |= CS_FL_REOS;
2815 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002816
Willy Tarreau68dd9852017-07-03 14:44:26 +02002817 leave:
2818 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002819 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002820 fail:
2821 outlen = 0;
2822 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002823}
2824
Willy Tarreau454f9052017-10-26 19:40:35 +02002825/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2826 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2827 * in use, a new chunk is emitted for each frame. This is supposed to fit
2828 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2829 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2830 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2831 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002832 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2833 * checked to know if some data remain pending (an empty DATA frame can return
2834 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2835 * connection errors in h2c->errcode. The caller must already have checked the
2836 * frame header and ensured that the frame was complete or the buffer full. It
2837 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002838 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002839static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002840{
2841 struct h2c *h2c = h2s->h2c;
2842 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002843 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002844 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002845 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002846
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002847 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002848
2849 /* The padlen is the first byte before data, and the padding appears
2850 * after data. padlen+data+padding are included in flen.
2851 */
Willy Tarreau79127812017-12-03 21:06:59 +01002852 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002854 return 0;
2855
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002856 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002857 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002858 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2859 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002860 return 0;
2861 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002862
2863 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002864 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002865 h2c->dfl--;
2866 h2c->rcvd_c++; h2c->rcvd_s++;
2867 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002868 }
2869
Olivier Houchard638b7992018-08-16 15:41:52 +02002870 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002871 if (!csbuf) {
2872 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002873 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002874 }
2875
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002876 flen = h2c->dfl - h2c->dpl;
2877 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002878 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 if (flen > b_data(&h2c->dbuf)) {
2881 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002882 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002883 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002884 }
2885
2886 if (unlikely(b_space_wraps(csbuf))) {
2887 /* it doesn't fit and the buffer is fragmented,
2888 * so let's defragment it and try again.
2889 */
2890 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002891 }
2892
Willy Tarreaueba10f22018-04-25 20:44:22 +02002893 /* chunked-encoding requires more room */
2894 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002895 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002896 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2897 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2898 (chklen < 1048576) ? 4 : 8;
2899 chklen += 4; // CRLF, CRLF
2900 }
2901
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002902 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002903 if (flen + chklen > b_room(csbuf)) {
2904 if (chklen >= b_room(csbuf)) {
2905 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002906 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002907 }
2908 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002909 }
2910
2911 if (h2s->flags & H2_SF_DATA_CHNK) {
2912 /* emit the chunk size */
2913 unsigned int chksz = flen;
2914 char str[10];
2915 char *beg;
2916
2917 beg = str + sizeof(str);
2918 *--beg = '\n';
2919 *--beg = '\r';
2920 do {
2921 *--beg = hextab[chksz & 0xF];
2922 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002923 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002924 }
2925
Willy Tarreau454f9052017-10-26 19:40:35 +02002926 /* Block1 is the length of the first block before the buffer wraps,
2927 * block2 is the optional second block to reach the end of the frame.
2928 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002929 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002930 if (block1 > flen)
2931 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002932 block2 = flen - block1;
2933
2934 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002935 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002936
2937 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002938 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002939
Willy Tarreaueba10f22018-04-25 20:44:22 +02002940 if (h2s->flags & H2_SF_DATA_CHNK) {
2941 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002942 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002943 }
2944
Willy Tarreau454f9052017-10-26 19:40:35 +02002945 /* now mark the input data as consumed (will be deleted from the buffer
2946 * by the caller when seeing FRAME_A after sending the window update).
2947 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002948 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002949 h2c->dfl -= flen;
2950 h2c->rcvd_c += flen;
2951 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2952
2953 if (h2c->dfl > h2c->dpl) {
2954 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002955 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002956 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002957 }
2958
Willy Tarreau4a28da12018-01-04 14:41:00 +01002959 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002960 /* here we're done with the frame, all the payload (except padding) was
2961 * transferred.
2962 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002963
2964 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2965 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002966 if (b_room(csbuf) < 5) {
2967 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002968 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002969 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002970 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002971 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002972 }
2973
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002974 h2c->rcvd_c += h2c->dpl;
2975 h2c->rcvd_s += h2c->dpl;
2976 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002977 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2978
Willy Tarreau39d68502018-03-02 12:26:37 +01002979 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002980 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002981 h2s->cs->flags |= CS_FL_REOS;
2982 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01002983
Willy Tarreau454b57b2018-02-26 15:50:05 +01002984 return flen + chklen;
2985 fail:
2986 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002987}
2988
Willy Tarreau5dd17352018-06-14 13:33:30 +02002989/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2990 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2991 * number of bytes sent. The caller must check the stream's status to detect
2992 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002993 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002994static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002995{
2996 struct http_hdr list[MAX_HTTP_HDR];
2997 struct h2c *h2c = h2s->h2c;
2998 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002999 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003000 int es_now = 0;
3001 int ret = 0;
3002 int hdr;
3003
3004 if (h2c_mux_busy(h2c, h2s)) {
3005 h2s->flags |= H2_SF_BLK_MBUSY;
3006 return 0;
3007 }
3008
Willy Tarreau44e973f2018-03-01 17:49:30 +01003009 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003010 h2c->flags |= H2_CF_MUX_MALLOC;
3011 h2s->flags |= H2_SF_BLK_MROOM;
3012 return 0;
3013 }
3014
3015 /* First, try to parse the H1 response and index it into <list>.
3016 * NOTE! Since it comes from haproxy, we *know* that a response header
3017 * block does not wrap and we can safely read it this way without
3018 * having to realign the buffer.
3019 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003020 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003021 list, sizeof(list)/sizeof(list[0]), h1m);
3022 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003023 /* incomplete or invalid response, this is abnormal coming from
3024 * haproxy and may only result in a bad errorfile or bad Lua code
3025 * so that won't be fixed, raise an error now.
3026 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003027 * FIXME: we should instead add the ability to only return a
3028 * 502 bad gateway. But in theory this is not supposed to
3029 * happen.
3030 */
3031 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3032 ret = 0;
3033 goto end;
3034 }
3035
3036 chunk_reset(&outbuf);
3037
3038 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003039 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003040 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003041 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003042
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003043 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003044 break;
3045 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003046 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003047 }
3048
3049 if (outbuf.size < 9) {
3050 h2c->flags |= H2_CF_MUX_MFULL;
3051 h2s->flags |= H2_SF_BLK_MROOM;
3052 ret = 0;
3053 goto end;
3054 }
3055
3056 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003057 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3058 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3059 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003060
3061 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003062 if (outbuf.data < outbuf.size && h1m->status == 200)
3063 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3064 else if (outbuf.data < outbuf.size && h1m->status == 304)
3065 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003066 else if (unlikely(list[0].v.len != 3)) {
3067 /* this is an unparsable response */
3068 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3069 ret = 0;
3070 goto end;
3071 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003072 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003073 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003074 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3075 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3076 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3077 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3078 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003079 }
3080 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003081 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003082 goto realign_again;
3083
3084 h2c->flags |= H2_CF_MUX_MFULL;
3085 h2s->flags |= H2_SF_BLK_MROOM;
3086 ret = 0;
3087 goto end;
3088 }
3089
3090 /* encode all headers, stop at empty name */
3091 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003092 /* these ones do not exist in H2 and must be dropped. */
3093 if (isteq(list[hdr].n, ist("connection")) ||
3094 isteq(list[hdr].n, ist("proxy-connection")) ||
3095 isteq(list[hdr].n, ist("keep-alive")) ||
3096 isteq(list[hdr].n, ist("upgrade")) ||
3097 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003098 continue;
3099
3100 if (isteq(list[hdr].n, ist("")))
3101 break; // end
3102
3103 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3104 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003105 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003106 goto realign_again;
3107
3108 h2c->flags |= H2_CF_MUX_MFULL;
3109 h2s->flags |= H2_SF_BLK_MROOM;
3110 ret = 0;
3111 goto end;
3112 }
3113 }
3114
3115 /* we may need to add END_STREAM */
3116 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3117 es_now = 1;
3118
3119 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003121
3122 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003123 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003124
3125 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003126 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003127
3128 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003129 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003130 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003131
3132 /* for now we don't implemented CONTINUATION, so we wait for a
3133 * body or directly end in TRL2.
3134 */
3135 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003136 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003137 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003138
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003139 h1m->state = HTTP_MSG_DONE;
3140 h2s->flags |= H2_SF_ES_SENT;
3141 if (h2s->st == H2_SS_OPEN)
3142 h2s->st = H2_SS_HLOC;
3143 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003144 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003145 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003146 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003147 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003148 h1m->state = HTTP_MSG_RPBEFORE;
3149 h1m->status = 0;
3150 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003151 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003152 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003153 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003154 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003155
3156 end:
3157 //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));
3158 return ret;
3159}
3160
Willy Tarreau5dd17352018-06-14 13:33:30 +02003161/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3162 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3163 * the number of bytes sent. The caller must check the stream's status to
3164 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003165 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003166static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003167{
3168 struct h2c *h2c = h2s->h2c;
3169 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003170 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003171 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003172 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003173 int es_now = 0;
3174 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003175 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003176 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003177
3178 if (h2c_mux_busy(h2c, h2s)) {
3179 h2s->flags |= H2_SF_BLK_MBUSY;
3180 goto end;
3181 }
3182
Willy Tarreau44e973f2018-03-01 17:49:30 +01003183 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003184 h2c->flags |= H2_CF_MUX_MALLOC;
3185 h2s->flags |= H2_SF_BLK_MROOM;
3186 goto end;
3187 }
3188
3189 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003190 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003191 goto end;
3192
3193 chunk_reset(&outbuf);
3194
3195 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003197 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003198 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003199
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003200 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003201 break;
3202 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003204 }
3205
3206 if (outbuf.size < 9) {
3207 h2c->flags |= H2_CF_MUX_MFULL;
3208 h2s->flags |= H2_SF_BLK_MROOM;
3209 goto end;
3210 }
3211
3212 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003213 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3214 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3215 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003216
3217 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3218 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003219 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003220 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003221 break;
3222 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003223 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224 if ((long long)size > h1m->curr_len)
3225 size = h1m->curr_len;
3226 break;
3227 default: /* te:chunked : parse chunks */
3228 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003229 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003230 if (!ret)
3231 goto end;
3232
3233 if (ret < 0) {
3234 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3235 h1m->err_pos = ret;
3236 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3237 goto end;
3238 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003239 max -= ret;
3240 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003241 total += ret;
3242 h1m->state = HTTP_MSG_CHUNK_SIZE;
3243 }
3244
3245 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3246 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003247 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003248 if (!ret)
3249 goto end;
3250
3251 if (ret < 0) {
3252 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3253 h1m->err_pos = ret;
3254 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3255 goto end;
3256 }
3257
3258 size = chunk;
3259 h1m->curr_len = chunk;
3260 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003261 max -= ret;
3262 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003263 total += ret;
3264 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3265 if (!size)
3266 goto send_empty;
3267 }
3268
3269 /* in MSG_DATA state, continue below */
3270 size = h1m->curr_len;
3271 break;
3272 }
3273
3274 /* we have in <size> the exact number of bytes we need to copy from
3275 * the H1 buffer. We need to check this against the connection's and
3276 * the stream's send windows, and to ensure that this fits in the max
3277 * frame size and in the buffer's available space minus 9 bytes (for
3278 * the frame header). The connection's flow control is applied last so
3279 * that we can use a separate list of streams which are immediately
3280 * unblocked on window opening. Note: we don't implement padding.
3281 */
3282
Willy Tarreau5dd17352018-06-14 13:33:30 +02003283 if (size > max)
3284 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003285
3286 if (size > h2s->mws)
3287 size = h2s->mws;
3288
3289 if (size <= 0) {
3290 h2s->flags |= H2_SF_BLK_SFCTL;
3291 goto end;
3292 }
3293
3294 if (h2c->mfs && size > h2c->mfs)
3295 size = h2c->mfs;
3296
3297 if (size + 9 > outbuf.size) {
3298 /* we have an opportunity for enlarging the too small
3299 * available space, let's try.
3300 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003301 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003302 goto realign_again;
3303 size = outbuf.size - 9;
3304 }
3305
3306 if (size <= 0) {
3307 h2c->flags |= H2_CF_MUX_MFULL;
3308 h2s->flags |= H2_SF_BLK_MROOM;
3309 goto end;
3310 }
3311
3312 if (size > h2c->mws)
3313 size = h2c->mws;
3314
3315 if (size <= 0) {
3316 h2s->flags |= H2_SF_BLK_MFCTL;
3317 goto end;
3318 }
3319
3320 /* copy whatever we can */
3321 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003322 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 if (ret == 1)
3324 len2 = 0;
3325
3326 if (!ret || len1 + len2 < size) {
3327 /* FIXME: must normally never happen */
3328 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3329 goto end;
3330 }
3331
3332 /* limit len1/len2 to size */
3333 if (len1 + len2 > size) {
3334 int sub = len1 + len2 - size;
3335
3336 if (len2 > sub)
3337 len2 -= sub;
3338 else {
3339 sub -= len2;
3340 len2 = 0;
3341 len1 -= sub;
3342 }
3343 }
3344
3345 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003346 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003347 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003348 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003349
3350 send_empty:
3351 /* we may need to add END_STREAM */
3352 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3353 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003354 *
3355 * FIXME: what we do here is not correct because we send end_stream
3356 * before knowing if we'll have to send a HEADERS frame for the
3357 * trailers. More importantly we're not consuming the trailing CRLF
3358 * after the end of trailers, so it will be left to the caller to
3359 * eat it. The right way to do it would be to measure trailers here
3360 * and to send ES only if there are no trailers.
3361 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003362 */
3363 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3364 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3365 es_now = 1;
3366
3367 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003368 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369
3370 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003371 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003372
3373 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003374 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003375
3376 /* consume incoming H1 response */
3377 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003378 max -= size;
3379 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003380 total += size;
3381 h1m->curr_len -= size;
3382 h2s->mws -= size;
3383 h2c->mws -= size;
3384
3385 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3386 h1m->state = HTTP_MSG_CHUNK_CRLF;
3387 goto new_frame;
3388 }
3389 }
3390
3391 if (es_now) {
3392 if (h2s->st == H2_SS_OPEN)
3393 h2s->st = H2_SS_HLOC;
3394 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003395 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003396
Willy Tarreau35a62702018-02-27 15:37:25 +01003397 if (!(h1m->flags & H1_MF_CHNK)) {
3398 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003399 total += max;
3400 ofs += max;
3401 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003402
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003403 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003404 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003405
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003406 h2s->flags |= H2_SF_ES_SENT;
3407 }
3408
3409 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003410 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003411 return total;
3412}
3413
Olivier Houchard6ff20392018-07-17 18:46:31 +02003414/* Called from the upper layer, to subscribe to events, such as being able to send */
3415static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3416{
3417 struct wait_list *sw;
3418 struct h2s *h2s = cs->ctx;
3419
3420 switch (event_type) {
3421 case SUB_CAN_SEND:
3422 sw = param;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003423 if (LIST_ISEMPTY(&h2s->list) &&
3424 !(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003425 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003426 sw->wait_reason |= SUB_CAN_SEND;
3427 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003428 return 0;
3429 default:
3430 break;
3431 }
3432 return -1;
3433
3434
3435}
3436
Olivier Houchard511efea2018-08-16 15:30:32 +02003437/* Called from the upper layer, to receive data */
3438static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3439{
Olivier Houchard638b7992018-08-16 15:41:52 +02003440 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003441 size_t ret = 0;
3442
3443 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003444 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003445
Olivier Houchard638b7992018-08-16 15:41:52 +02003446 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003447 cs->flags |= CS_FL_RCV_MORE;
3448 else {
3449 cs->flags &= ~CS_FL_RCV_MORE;
3450 if (cs->flags & CS_FL_REOS)
3451 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003452 if (b_size(&h2s->rxbuf)) {
3453 b_free(&h2s->rxbuf);
3454 offer_buffers(NULL, tasks_run_queue);
3455 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003456 }
3457
3458 return ret;
3459}
3460
Willy Tarreau62f52692017-10-08 23:01:42 +02003461/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003462static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003463{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003464 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003465 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003466 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003467
Willy Tarreau0bad0432018-06-14 16:54:01 +02003468 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003469 h2s->flags |= H2_SF_OUTGOING_DATA;
3470
Willy Tarreau0bad0432018-06-14 16:54:01 +02003471 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003472 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003473 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003474 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003475 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003476 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003477 }
3478 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3479 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003480 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003481
Willy Tarreau5dd17352018-06-14 13:33:30 +02003482 if (unlikely((int)ret <= 0)) {
3483 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003484 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3485 break;
3486 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003487 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003488 total += count;
3489 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003490 h2s->res.state = HTTP_MSG_DONE;
3491 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003492 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003493 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003494 cs->flags |= CS_FL_ERROR;
3495 break;
3496 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003497
3498 total += ret;
3499 count -= ret;
3500
3501 if (h2s->st >= H2_SS_ERROR)
3502 break;
3503
3504 if (h2s->flags & H2_SF_BLK_ANY)
3505 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003506 }
3507
Willy Tarreau00610962018-07-19 10:58:28 +02003508 if (h2s->st >= H2_SS_ERROR) {
3509 /* trim any possibly pending data after we close (extra CR-LF,
3510 * unprocessed trailers, abnormal extra data, ...)
3511 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003512 total += count;
3513 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003514 }
3515
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003516 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003517 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003518 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003519 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003520 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003521 }
3522
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523 if (h2s->flags & H2_SF_BLK_SFCTL) {
3524 /* stream flow control, quit the list */
3525 LIST_DEL(&h2s->list);
3526 LIST_INIT(&h2s->list);
3527 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003528 else if (LIST_ISEMPTY(&h2s->list)) {
3529 if (h2s->flags & H2_SF_BLK_MFCTL)
3530 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003531 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003532
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003533 b_del(buf, total);
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003534 if (total > 0)
3535 conn_xprt_want_send(h2s->h2c->conn);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003536 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003537}
3538
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003539/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003540static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003541{
3542 struct h2c *h2c = conn->mux_ctx;
3543 struct h2s *h2s;
3544 struct eb32_node *node;
3545 int fctl_cnt = 0;
3546 int send_cnt = 0;
3547 int tree_cnt = 0;
3548 int orph_cnt = 0;
3549
3550 if (!h2c)
3551 return;
3552
3553 list_for_each_entry(h2s, &h2c->fctl_list, list)
3554 fctl_cnt++;
3555
3556 list_for_each_entry(h2s, &h2c->send_list, list)
3557 send_cnt++;
3558
3559 node = eb32_first(&h2c->streams_by_id);
3560 while (node) {
3561 h2s = container_of(node, struct h2s, by_id);
3562 tree_cnt++;
3563 if (!h2s->cs)
3564 orph_cnt++;
3565 node = eb32_next(node);
3566 }
3567
Willy Tarreau616ac812018-07-24 14:12:42 +02003568 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3569 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3570 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3571 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3572 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3573 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003574}
Willy Tarreau62f52692017-10-08 23:01:42 +02003575
3576/*******************************************************/
3577/* functions below are dedicated to the config parsers */
3578/*******************************************************/
3579
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003580/* config parser for global "tune.h2.header-table-size" */
3581static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3582 struct proxy *defpx, const char *file, int line,
3583 char **err)
3584{
3585 if (too_many_args(1, args, err, NULL))
3586 return -1;
3587
3588 h2_settings_header_table_size = atoi(args[1]);
3589 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3590 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3591 return -1;
3592 }
3593 return 0;
3594}
Willy Tarreau62f52692017-10-08 23:01:42 +02003595
Willy Tarreaue6baec02017-07-27 11:45:11 +02003596/* config parser for global "tune.h2.initial-window-size" */
3597static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3598 struct proxy *defpx, const char *file, int line,
3599 char **err)
3600{
3601 if (too_many_args(1, args, err, NULL))
3602 return -1;
3603
3604 h2_settings_initial_window_size = atoi(args[1]);
3605 if (h2_settings_initial_window_size < 0) {
3606 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3607 return -1;
3608 }
3609 return 0;
3610}
3611
Willy Tarreau5242ef82017-07-27 11:47:28 +02003612/* config parser for global "tune.h2.max-concurrent-streams" */
3613static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3614 struct proxy *defpx, const char *file, int line,
3615 char **err)
3616{
3617 if (too_many_args(1, args, err, NULL))
3618 return -1;
3619
3620 h2_settings_max_concurrent_streams = atoi(args[1]);
3621 if (h2_settings_max_concurrent_streams < 0) {
3622 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3623 return -1;
3624 }
3625 return 0;
3626}
3627
Willy Tarreau62f52692017-10-08 23:01:42 +02003628
3629/****************************************/
3630/* MUX initialization and instanciation */
3631/***************************************/
3632
3633/* The mux operations */
3634const struct mux_ops h2_ops = {
3635 .init = h2_init,
3636 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003637 .wake = h2_wake,
3638 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003639 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003640 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003641 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003642 .attach = h2_attach,
3643 .detach = h2_detach,
3644 .shutr = h2_shutr,
3645 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003646 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003647 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003648 .name = "H2",
3649};
3650
Christopher Faulet32f61c02018-04-10 14:33:41 +02003651/* PROTO selection : this mux registers PROTO token "h2" */
3652static struct mux_proto_list mux_proto_h2 =
3653 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003654
3655/* config keyword parsers */
3656static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003657 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003658 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003659 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003660 { 0, NULL, NULL }
3661}};
3662
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003663static void __h2_deinit(void)
3664{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003665 pool_destroy(pool_head_h2s);
3666 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003667}
3668
Willy Tarreau62f52692017-10-08 23:01:42 +02003669__attribute__((constructor))
3670static void __h2_init(void)
3671{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003672 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003673 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003674 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003675 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3676 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003677}