blob: 70a14218b6250d98746642a2bd4566e870de5417 [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{
Willy Tarreau590a0512018-09-05 11:56:48 +0200652 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200653 struct conn_stream *cs;
654 struct h2s *h2s;
655
Willy Tarreaubafbe012017-11-24 17:34:44 +0100656 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200657 if (!h2s)
658 goto out;
659
660 h2s->h2c = h2c;
661 h2s->mws = h2c->miw;
662 h2s->flags = H2_SF_NONE;
663 h2s->errcode = H2_ERR_NO_ERROR;
664 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200665 h2s->rxbuf = BUF_NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200666 h1m_init(&h2s->req);
667 h1m_init(&h2s->res);
668 h2s->by_id.key = h2s->id = id;
669 h2c->max_id = id;
670 LIST_INIT(&h2s->list);
671
672 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100673 h2c->nb_streams++;
674 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
675 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200676
677 cs = cs_new(h2c->conn);
678 if (!cs)
679 goto out_close;
680
681 h2s->cs = cs;
682 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200683 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200684
685 if (stream_create_from_cs(cs) < 0)
686 goto out_free_cs;
687
Willy Tarreau590a0512018-09-05 11:56:48 +0200688 /* We want the accept date presented to the next stream to be the one
689 * we have now, the handshake time to be null (since the next stream
690 * is not delayed by a handshake), and the idle time to count since
691 * right now.
692 */
693 sess->accept_date = date;
694 sess->tv_accept = now;
695 sess->t_handshake = 0;
696
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200697 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200698 if (h2_has_too_many_cs(h2c))
699 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200700 return h2s;
701
702 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200703 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200704 cs_free(cs);
705 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200706 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200707 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200708 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200709 out:
710 return h2s;
711}
712
Willy Tarreaube5b7152017-09-25 16:25:39 +0200713/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
714 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
715 * the various settings codes.
716 */
717static int h2c_snd_settings(struct h2c *h2c)
718{
719 struct buffer *res;
720 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200721 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200722 int ret;
723
724 if (h2c_mux_busy(h2c, NULL)) {
725 h2c->flags |= H2_CF_DEM_MBUSY;
726 return 0;
727 }
728
Willy Tarreau44e973f2018-03-01 17:49:30 +0100729 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200730 if (!res) {
731 h2c->flags |= H2_CF_MUX_MALLOC;
732 h2c->flags |= H2_CF_DEM_MROOM;
733 return 0;
734 }
735
736 chunk_init(&buf, buf_data, sizeof(buf_data));
737 chunk_memcpy(&buf,
738 "\x00\x00\x00" /* length : 0 for now */
739 "\x04\x00" /* type : 4 (settings), flags : 0 */
740 "\x00\x00\x00\x00", /* stream ID : 0 */
741 9);
742
743 if (h2_settings_header_table_size != 4096) {
744 char str[6] = "\x00\x01"; /* header_table_size */
745
746 write_n32(str + 2, h2_settings_header_table_size);
747 chunk_memcat(&buf, str, 6);
748 }
749
750 if (h2_settings_initial_window_size != 65535) {
751 char str[6] = "\x00\x04"; /* initial_window_size */
752
753 write_n32(str + 2, h2_settings_initial_window_size);
754 chunk_memcat(&buf, str, 6);
755 }
756
757 if (h2_settings_max_concurrent_streams != 0) {
758 char str[6] = "\x00\x03"; /* max_concurrent_streams */
759
760 /* Note: 0 means "unlimited" for haproxy's config but not for
761 * the protocol, so never send this value!
762 */
763 write_n32(str + 2, h2_settings_max_concurrent_streams);
764 chunk_memcat(&buf, str, 6);
765 }
766
767 if (global.tune.bufsize != 16384) {
768 char str[6] = "\x00\x05"; /* max_frame_size */
769
770 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
771 * match bufsize - rewrite size, but at the moment it seems
772 * that clients don't take care of it.
773 */
774 write_n32(str + 2, global.tune.bufsize);
775 chunk_memcat(&buf, str, 6);
776 }
777
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 h2_set_frame_size(buf.area, buf.data - 9);
779 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200780 if (unlikely(ret <= 0)) {
781 if (!ret) {
782 h2c->flags |= H2_CF_MUX_MFULL;
783 h2c->flags |= H2_CF_DEM_MROOM;
784 return 0;
785 }
786 else {
787 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
788 return 0;
789 }
790 }
791 return ret;
792}
793
Willy Tarreau52eed752017-09-22 15:05:09 +0200794/* Try to receive a connection preface, then upon success try to send our
795 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
796 * missing data. It may return an error in h2c.
797 */
798static int h2c_frt_recv_preface(struct h2c *h2c)
799{
800 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200801 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200802
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200803 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200804
805 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200806 if (ret1 < 0)
807 sess_log(h2c->conn->owner);
808
Willy Tarreau52eed752017-09-22 15:05:09 +0200809 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
810 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
811 return 0;
812 }
813
Willy Tarreaube5b7152017-09-25 16:25:39 +0200814 ret2 = h2c_snd_settings(h2c);
815 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200816 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200817
Willy Tarreaube5b7152017-09-25 16:25:39 +0200818 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200819}
820
Willy Tarreau081d4722017-05-16 21:51:05 +0200821/* try to send a GOAWAY frame on the connection to report an error or a graceful
822 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
823 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
824 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
825 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
826 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
827 * on unrecoverable failure. It will not attempt to send one again in this last
828 * case so that it is safe to use h2c_error() to report such errors.
829 */
830static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
831{
832 struct buffer *res;
833 char str[17];
834 int ret;
835
836 if (h2c->flags & H2_CF_GOAWAY_FAILED)
837 return 1; // claim that it worked
838
839 if (h2c_mux_busy(h2c, h2s)) {
840 if (h2s)
841 h2s->flags |= H2_SF_BLK_MBUSY;
842 else
843 h2c->flags |= H2_CF_DEM_MBUSY;
844 return 0;
845 }
846
Willy Tarreau44e973f2018-03-01 17:49:30 +0100847 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200848 if (!res) {
849 h2c->flags |= H2_CF_MUX_MALLOC;
850 if (h2s)
851 h2s->flags |= H2_SF_BLK_MROOM;
852 else
853 h2c->flags |= H2_CF_DEM_MROOM;
854 return 0;
855 }
856
857 /* len: 8, type: 7, flags: none, sid: 0 */
858 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
859
860 if (h2c->last_sid < 0)
861 h2c->last_sid = h2c->max_id;
862
863 write_n32(str + 9, h2c->last_sid);
864 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200865 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200866 if (unlikely(ret <= 0)) {
867 if (!ret) {
868 h2c->flags |= H2_CF_MUX_MFULL;
869 if (h2s)
870 h2s->flags |= H2_SF_BLK_MROOM;
871 else
872 h2c->flags |= H2_CF_DEM_MROOM;
873 return 0;
874 }
875 else {
876 /* we cannot report this error using GOAWAY, so we mark
877 * it and claim a success.
878 */
879 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
880 h2c->flags |= H2_CF_GOAWAY_FAILED;
881 return 1;
882 }
883 }
884 h2c->flags |= H2_CF_GOAWAY_SENT;
885 return ret;
886}
887
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100888/* Try to send an RST_STREAM frame on the connection for the indicated stream
889 * during mux operations. This stream must be valid and cannot be closed
890 * already. h2s->id will be used for the stream ID and h2s->errcode will be
891 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
892 * not yet.
893 *
894 * Returns > 0 on success or zero if nothing was done. In case of lack of room
895 * to write the message, it subscribes the stream to future notifications.
896 */
897static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
898{
899 struct buffer *res;
900 char str[13];
901 int ret;
902
903 if (!h2s || h2s->st == H2_SS_CLOSED)
904 return 1;
905
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100906 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
907 * RST_STREAM in response to a RST_STREAM frame.
908 */
909 if (h2c->dft == H2_FT_RST_STREAM) {
910 ret = 1;
911 goto ignore;
912 }
913
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100914 if (h2c_mux_busy(h2c, h2s)) {
915 h2s->flags |= H2_SF_BLK_MBUSY;
916 return 0;
917 }
918
Willy Tarreau44e973f2018-03-01 17:49:30 +0100919 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100920 if (!res) {
921 h2c->flags |= H2_CF_MUX_MALLOC;
922 h2s->flags |= H2_SF_BLK_MROOM;
923 return 0;
924 }
925
926 /* len: 4, type: 3, flags: none */
927 memcpy(str, "\x00\x00\x04\x03\x00", 5);
928 write_n32(str + 5, h2s->id);
929 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200930 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931
932 if (unlikely(ret <= 0)) {
933 if (!ret) {
934 h2c->flags |= H2_CF_MUX_MFULL;
935 h2s->flags |= H2_SF_BLK_MROOM;
936 return 0;
937 }
938 else {
939 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
940 return 0;
941 }
942 }
943
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100944 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100946 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100947 return ret;
948}
949
950/* Try to send an RST_STREAM frame on the connection for the stream being
951 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
952 * error code unless the stream's state already is IDLE or CLOSED in which
953 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
954 * it was not yet.
955 *
956 * Returns > 0 on success or zero if nothing was done. In case of lack of room
957 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200958 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100959 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200960 */
961static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
962{
963 struct buffer *res;
964 char str[13];
965 int ret;
966
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100967 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
968 * RST_STREAM in response to a RST_STREAM frame.
969 */
970 if (h2c->dft == H2_FT_RST_STREAM) {
971 ret = 1;
972 goto ignore;
973 }
974
Willy Tarreau27a84c92017-10-17 08:10:17 +0200975 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100976 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 return 0;
978 }
979
Willy Tarreau44e973f2018-03-01 17:49:30 +0100980 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200981 if (!res) {
982 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100983 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200984 return 0;
985 }
986
987 /* len: 4, type: 3, flags: none */
988 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100989
Willy Tarreau27a84c92017-10-17 08:10:17 +0200990 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100991 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200992 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200993 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100994
Willy Tarreau27a84c92017-10-17 08:10:17 +0200995 if (unlikely(ret <= 0)) {
996 if (!ret) {
997 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100998 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200999 return 0;
1000 }
1001 else {
1002 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1003 return 0;
1004 }
1005 }
1006
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001007 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001008 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001009 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001010 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001011 }
1012
Willy Tarreau27a84c92017-10-17 08:10:17 +02001013 return ret;
1014}
1015
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001016/* try to send an empty DATA frame with the ES flag set to notify about the
1017 * end of stream and match a shutdown(write). If an ES was already sent as
1018 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1019 * on success or zero if nothing was done. In case of lack of room to write the
1020 * message, it subscribes the requesting stream to future notifications.
1021 */
1022static int h2_send_empty_data_es(struct h2s *h2s)
1023{
1024 struct h2c *h2c = h2s->h2c;
1025 struct buffer *res;
1026 char str[9];
1027 int ret;
1028
Willy Tarreau721c9742017-11-07 11:05:42 +01001029 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001030 return 1;
1031
1032 if (h2c_mux_busy(h2c, h2s)) {
1033 h2s->flags |= H2_SF_BLK_MBUSY;
1034 return 0;
1035 }
1036
Willy Tarreau44e973f2018-03-01 17:49:30 +01001037 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001038 if (!res) {
1039 h2c->flags |= H2_CF_MUX_MALLOC;
1040 h2s->flags |= H2_SF_BLK_MROOM;
1041 return 0;
1042 }
1043
1044 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1045 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1046 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001047 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001048 if (likely(ret > 0)) {
1049 h2s->flags |= H2_SF_ES_SENT;
1050 }
1051 else if (!ret) {
1052 h2c->flags |= H2_CF_MUX_MFULL;
1053 h2s->flags |= H2_SF_BLK_MROOM;
1054 return 0;
1055 }
1056 else {
1057 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1058 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001059 }
1060 return ret;
1061}
1062
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001063/* wake the streams attached to the connection, whose id is greater than <last>,
1064 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1065 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1066 * stream's state is automatically updated accordingly.
1067 */
1068static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1069{
1070 struct eb32_node *node;
1071 struct h2s *h2s;
1072
1073 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1074 flags |= CS_FL_ERROR;
1075
1076 if (conn_xprt_read0_pending(h2c->conn))
1077 flags |= CS_FL_EOS;
1078
1079 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1080 while (node) {
1081 h2s = container_of(node, struct h2s, by_id);
1082 if (h2s->id <= last)
1083 break;
1084 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001085
1086 if (!h2s->cs) {
1087 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001088 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001089 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001090 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001091
1092 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001093 h2s->cs->data_cb->wake(h2s->cs);
1094
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001095 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1096 h2s->st = H2_SS_ERROR;
1097 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1098 h2s->st = H2_SS_HREM;
1099 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001100 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001101 }
1102}
1103
Willy Tarreau3421aba2017-07-27 15:41:03 +02001104/* Increase all streams' outgoing window size by the difference passed in
1105 * argument. This is needed upon receipt of the settings frame if the initial
1106 * window size is different. The difference may be negative and the resulting
1107 * window size as well, for the time it takes to receive some window updates.
1108 */
1109static void h2c_update_all_ws(struct h2c *h2c, int diff)
1110{
1111 struct h2s *h2s;
1112 struct eb32_node *node;
1113
1114 if (!diff)
1115 return;
1116
1117 node = eb32_first(&h2c->streams_by_id);
1118 while (node) {
1119 h2s = container_of(node, struct h2s, by_id);
1120 h2s->mws += diff;
1121 node = eb32_next(node);
1122 }
1123}
1124
1125/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1126 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1127 * return an error in h2c. Described in RFC7540#6.5.
1128 */
1129static int h2c_handle_settings(struct h2c *h2c)
1130{
1131 unsigned int offset;
1132 int error;
1133
1134 if (h2c->dff & H2_F_SETTINGS_ACK) {
1135 if (h2c->dfl) {
1136 error = H2_ERR_FRAME_SIZE_ERROR;
1137 goto fail;
1138 }
1139 return 1;
1140 }
1141
1142 if (h2c->dsi != 0) {
1143 error = H2_ERR_PROTOCOL_ERROR;
1144 goto fail;
1145 }
1146
1147 if (h2c->dfl % 6) {
1148 error = H2_ERR_FRAME_SIZE_ERROR;
1149 goto fail;
1150 }
1151
1152 /* that's the limit we can process */
1153 if (h2c->dfl > global.tune.bufsize) {
1154 error = H2_ERR_FRAME_SIZE_ERROR;
1155 goto fail;
1156 }
1157
1158 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001159 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001160 return 0;
1161
1162 /* parse the frame */
1163 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001164 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1165 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001166
1167 switch (type) {
1168 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1169 /* we need to update all existing streams with the
1170 * difference from the previous iws.
1171 */
1172 if (arg < 0) { // RFC7540#6.5.2
1173 error = H2_ERR_FLOW_CONTROL_ERROR;
1174 goto fail;
1175 }
1176 h2c_update_all_ws(h2c, arg - h2c->miw);
1177 h2c->miw = arg;
1178 break;
1179 case H2_SETTINGS_MAX_FRAME_SIZE:
1180 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1181 error = H2_ERR_PROTOCOL_ERROR;
1182 goto fail;
1183 }
1184 h2c->mfs = arg;
1185 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001186 case H2_SETTINGS_ENABLE_PUSH:
1187 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1188 error = H2_ERR_PROTOCOL_ERROR;
1189 goto fail;
1190 }
1191 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001192 }
1193 }
1194
1195 /* need to ACK this frame now */
1196 h2c->st0 = H2_CS_FRAME_A;
1197 return 1;
1198 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001199 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001200 h2c_error(h2c, error);
1201 return 0;
1202}
1203
1204/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1205 * success or one of the h2_status values.
1206 */
1207static int h2c_ack_settings(struct h2c *h2c)
1208{
1209 struct buffer *res;
1210 char str[9];
1211 int ret = -1;
1212
1213 if (h2c_mux_busy(h2c, NULL)) {
1214 h2c->flags |= H2_CF_DEM_MBUSY;
1215 return 0;
1216 }
1217
Willy Tarreau44e973f2018-03-01 17:49:30 +01001218 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001219 if (!res) {
1220 h2c->flags |= H2_CF_MUX_MALLOC;
1221 h2c->flags |= H2_CF_DEM_MROOM;
1222 return 0;
1223 }
1224
1225 memcpy(str,
1226 "\x00\x00\x00" /* length : 0 (no data) */
1227 "\x04" "\x01" /* type : 4, flags : ACK */
1228 "\x00\x00\x00\x00" /* stream ID */, 9);
1229
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001230 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001231 if (unlikely(ret <= 0)) {
1232 if (!ret) {
1233 h2c->flags |= H2_CF_MUX_MFULL;
1234 h2c->flags |= H2_CF_DEM_MROOM;
1235 return 0;
1236 }
1237 else {
1238 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1239 return 0;
1240 }
1241 }
1242 return ret;
1243}
1244
Willy Tarreaucf68c782017-10-10 17:11:41 +02001245/* processes a PING frame and schedules an ACK if needed. The caller must pass
1246 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1247 * missing data. It may return an error in h2c.
1248 */
1249static int h2c_handle_ping(struct h2c *h2c)
1250{
1251 /* frame length must be exactly 8 */
1252 if (h2c->dfl != 8) {
1253 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1254 return 0;
1255 }
1256
1257 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001258 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001259 h2c->st0 = H2_CS_FRAME_A;
1260 return 1;
1261}
1262
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001263/* Try to send a window update for stream id <sid> and value <increment>.
1264 * Returns > 0 on success or zero on missing room or failure. It may return an
1265 * error in h2c.
1266 */
1267static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1268{
1269 struct buffer *res;
1270 char str[13];
1271 int ret = -1;
1272
1273 if (h2c_mux_busy(h2c, NULL)) {
1274 h2c->flags |= H2_CF_DEM_MBUSY;
1275 return 0;
1276 }
1277
Willy Tarreau44e973f2018-03-01 17:49:30 +01001278 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001279 if (!res) {
1280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 h2c->flags |= H2_CF_DEM_MROOM;
1282 return 0;
1283 }
1284
1285 /* length: 4, type: 8, flags: none */
1286 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1287 write_n32(str + 5, sid);
1288 write_n32(str + 9, increment);
1289
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001290 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001291
1292 if (unlikely(ret <= 0)) {
1293 if (!ret) {
1294 h2c->flags |= H2_CF_MUX_MFULL;
1295 h2c->flags |= H2_CF_DEM_MROOM;
1296 return 0;
1297 }
1298 else {
1299 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1300 return 0;
1301 }
1302 }
1303 return ret;
1304}
1305
1306/* try to send pending window update for the connection. It's safe to call it
1307 * with no pending updates. Returns > 0 on success or zero on missing room or
1308 * failure. It may return an error in h2c.
1309 */
1310static int h2c_send_conn_wu(struct h2c *h2c)
1311{
1312 int ret = 1;
1313
1314 if (h2c->rcvd_c <= 0)
1315 return 1;
1316
1317 /* send WU for the connection */
1318 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1319 if (ret > 0)
1320 h2c->rcvd_c = 0;
1321
1322 return ret;
1323}
1324
1325/* try to send pending window update for the current dmux stream. It's safe to
1326 * call it with no pending updates. Returns > 0 on success or zero on missing
1327 * room or failure. It may return an error in h2c.
1328 */
1329static int h2c_send_strm_wu(struct h2c *h2c)
1330{
1331 int ret = 1;
1332
1333 if (h2c->rcvd_s <= 0)
1334 return 1;
1335
1336 /* send WU for the stream */
1337 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1338 if (ret > 0)
1339 h2c->rcvd_s = 0;
1340
1341 return ret;
1342}
1343
Willy Tarreaucf68c782017-10-10 17:11:41 +02001344/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1345 * success, 0 on missing data or one of the h2_status values.
1346 */
1347static int h2c_ack_ping(struct h2c *h2c)
1348{
1349 struct buffer *res;
1350 char str[17];
1351 int ret = -1;
1352
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001353 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001354 return 0;
1355
1356 if (h2c_mux_busy(h2c, NULL)) {
1357 h2c->flags |= H2_CF_DEM_MBUSY;
1358 return 0;
1359 }
1360
Willy Tarreau44e973f2018-03-01 17:49:30 +01001361 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001362 if (!res) {
1363 h2c->flags |= H2_CF_MUX_MALLOC;
1364 h2c->flags |= H2_CF_DEM_MROOM;
1365 return 0;
1366 }
1367
1368 memcpy(str,
1369 "\x00\x00\x08" /* length : 8 (same payload) */
1370 "\x06" "\x01" /* type : 6, flags : ACK */
1371 "\x00\x00\x00\x00" /* stream ID */, 9);
1372
1373 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001374 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001375
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001376 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001377 if (unlikely(ret <= 0)) {
1378 if (!ret) {
1379 h2c->flags |= H2_CF_MUX_MFULL;
1380 h2c->flags |= H2_CF_DEM_MROOM;
1381 return 0;
1382 }
1383 else {
1384 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1385 return 0;
1386 }
1387 }
1388 return ret;
1389}
1390
Willy Tarreau26f95952017-07-27 17:18:30 +02001391/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1392 * Returns > 0 on success or zero on missing data. It may return an error in
1393 * h2c or h2s. Described in RFC7540#6.9.
1394 */
1395static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1396{
1397 int32_t inc;
1398 int error;
1399
1400 if (h2c->dfl != 4) {
1401 error = H2_ERR_FRAME_SIZE_ERROR;
1402 goto conn_err;
1403 }
1404
1405 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001406 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001407 return 0;
1408
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001409 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001410
1411 if (h2c->dsi != 0) {
1412 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001413
1414 /* it's not an error to receive WU on a closed stream */
1415 if (h2s->st == H2_SS_CLOSED)
1416 return 1;
1417
1418 if (!inc) {
1419 error = H2_ERR_PROTOCOL_ERROR;
1420 goto strm_err;
1421 }
1422
1423 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1424 error = H2_ERR_FLOW_CONTROL_ERROR;
1425 goto strm_err;
1426 }
1427
1428 h2s->mws += inc;
1429 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1430 h2s->flags &= ~H2_SF_BLK_SFCTL;
1431 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1432 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1433 /* This stream wanted to send but could not due to its
1434 * own flow control. We can put it back into the send
1435 * list now, it will be handled upon next send() call.
1436 */
1437 LIST_ADDQ(&h2c->send_list, &h2s->list);
1438 }
1439 }
1440 }
1441 else {
1442 /* connection window update */
1443 if (!inc) {
1444 error = H2_ERR_PROTOCOL_ERROR;
1445 goto conn_err;
1446 }
1447
1448 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1449 error = H2_ERR_FLOW_CONTROL_ERROR;
1450 goto conn_err;
1451 }
1452
1453 h2c->mws += inc;
1454 }
1455
1456 return 1;
1457
1458 conn_err:
1459 h2c_error(h2c, error);
1460 return 0;
1461
1462 strm_err:
1463 if (h2s) {
1464 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001465 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001466 }
1467 else
1468 h2c_error(h2c, error);
1469 return 0;
1470}
1471
Willy Tarreaue96b0922017-10-30 00:28:29 +01001472/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1473 * the last ID. Returns > 0 on success or zero on missing data. It may return
1474 * an error in h2c. Described in RFC7540#6.8.
1475 */
1476static int h2c_handle_goaway(struct h2c *h2c)
1477{
1478 int error;
1479 int last;
1480
1481 if (h2c->dsi != 0) {
1482 error = H2_ERR_PROTOCOL_ERROR;
1483 goto conn_err;
1484 }
1485
1486 if (h2c->dfl < 8) {
1487 error = H2_ERR_FRAME_SIZE_ERROR;
1488 goto conn_err;
1489 }
1490
1491 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001492 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001493 return 0;
1494
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001495 last = h2_get_n32(&h2c->dbuf, 0);
1496 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001497 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001498 if (h2c->last_sid < 0)
1499 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001500 return 1;
1501
1502 conn_err:
1503 h2c_error(h2c, error);
1504 return 0;
1505}
1506
Willy Tarreau92153fc2017-12-03 19:46:19 +01001507/* processes a PRIORITY frame, and either skips it or rejects if it is
1508 * invalid. Returns > 0 on success or zero on missing data. It may return
1509 * an error in h2c. Described in RFC7540#6.3.
1510 */
1511static int h2c_handle_priority(struct h2c *h2c)
1512{
1513 int error;
1514
1515 if (h2c->dsi == 0) {
1516 error = H2_ERR_PROTOCOL_ERROR;
1517 goto conn_err;
1518 }
1519
1520 if (h2c->dfl != 5) {
1521 error = H2_ERR_FRAME_SIZE_ERROR;
1522 goto conn_err;
1523 }
1524
1525 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001526 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001527 return 0;
1528
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001530 /* 7540#5.3 : can't depend on itself */
1531 error = H2_ERR_PROTOCOL_ERROR;
1532 goto conn_err;
1533 }
1534 return 1;
1535
1536 conn_err:
1537 h2c_error(h2c, error);
1538 return 0;
1539}
1540
Willy Tarreaucd234e92017-08-18 10:59:39 +02001541/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1542 * Returns > 0 on success or zero on missing data. It may return an error in
1543 * h2c. Described in RFC7540#6.4.
1544 */
1545static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1546{
1547 int error;
1548
1549 if (h2c->dsi == 0) {
1550 error = H2_ERR_PROTOCOL_ERROR;
1551 goto conn_err;
1552 }
1553
Willy Tarreaucd234e92017-08-18 10:59:39 +02001554 if (h2c->dfl != 4) {
1555 error = H2_ERR_FRAME_SIZE_ERROR;
1556 goto conn_err;
1557 }
1558
1559 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001560 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001561 return 0;
1562
1563 /* late RST, already handled */
1564 if (h2s->st == H2_SS_CLOSED)
1565 return 1;
1566
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001567 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001568 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001569
1570 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001571 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001572 h2s->cs->data_cb->wake(h2s->cs);
1573 }
1574
1575 h2s->flags |= H2_SF_RST_RCVD;
1576 return 1;
1577
1578 conn_err:
1579 h2c_error(h2c, error);
1580 return 0;
1581}
1582
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001583/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1584 * It may return an error in h2c or h2s. The caller must consider that the
1585 * return value is the new h2s in case one was allocated (most common case).
1586 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001587 * errors here are reported as connection errors since it's impossible to
1588 * recover from such errors after the compression context has been altered.
1589 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001590static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001591{
1592 int error;
1593
1594 if (!h2c->dfl) {
1595 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001596 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001597 goto strm_err;
1598 }
1599
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001600 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001601 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001602
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001603 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001604 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001605
Willy Tarreauf2101912018-07-19 10:11:38 +02001606 if (h2c->flags & H2_CF_DEM_TOOMANY)
1607 return 0; // too many cs still present
1608
Willy Tarreau13278b42017-10-13 19:23:14 +02001609 /* now either the frame is complete or the buffer is complete */
1610 if (h2s->st != H2_SS_IDLE) {
1611 /* FIXME: stream already exists, this is only allowed for
1612 * trailers (not supported for now).
1613 */
1614 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001615 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001616 goto conn_err;
1617 }
1618 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1619 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1620 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001621 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001622 goto conn_err;
1623 }
1624
Willy Tarreau22de8d32018-09-05 19:55:58 +02001625 /* Note: we don't emit any other logs below because ff we return
1626 * positively from h2c_stream_new(), the stream will report the error,
1627 * and if we return in error, h2c_stream_new() will emit the error.
1628 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001629 h2s = h2c_stream_new(h2c, h2c->dsi);
1630 if (!h2s) {
1631 error = H2_ERR_INTERNAL_ERROR;
1632 goto conn_err;
1633 }
1634
1635 h2s->st = H2_SS_OPEN;
1636 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1637 h2s->st = H2_SS_HREM;
1638 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001639 /* note: cs cannot be null for now (just created above) */
1640 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001641 }
1642
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001643 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001644 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001645
Willy Tarreau8f650c32017-11-21 19:36:21 +01001646 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001647 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001648
Willy Tarreau721c9742017-11-07 11:05:42 +01001649 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001650 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001651 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001652 }
1653 else {
1654 /* update the max stream ID if the request is being processed */
1655 if (h2s->id > h2c->max_id)
1656 h2c->max_id = h2s->id;
1657 }
1658
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001659 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001660
1661 conn_err:
1662 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001663 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001664
1665 strm_err:
1666 if (h2s) {
1667 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001668 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001669 }
1670 else
1671 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001672 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001673}
1674
Willy Tarreau454f9052017-10-26 19:40:35 +02001675/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1676 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1677 */
1678static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1679{
1680 int error;
1681
1682 /* note that empty DATA frames are perfectly valid and sometimes used
1683 * to signal an end of stream (with the ES flag).
1684 */
1685
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001686 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001687 return 0; // empty buffer
1688
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001689 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001690 return 0; // incomplete frame
1691
1692 /* now either the frame is complete or the buffer is complete */
1693
1694 if (!h2c->dsi) {
1695 /* RFC7540#6.1 */
1696 error = H2_ERR_PROTOCOL_ERROR;
1697 goto conn_err;
1698 }
1699
1700 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1701 /* RFC7540#6.1 */
1702 error = H2_ERR_STREAM_CLOSED;
1703 goto strm_err;
1704 }
1705
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001706 if (!h2_frt_transfer_data(h2s))
1707 return 0;
1708
Willy Tarreau454f9052017-10-26 19:40:35 +02001709 /* call the upper layers to process the frame, then let the upper layer
1710 * notify the stream about any change.
1711 */
1712 if (!h2s->cs) {
1713 error = H2_ERR_STREAM_CLOSED;
1714 goto strm_err;
1715 }
1716
Willy Tarreau8f650c32017-11-21 19:36:21 +01001717 if (h2c->st0 >= H2_CS_ERROR)
1718 return 0;
1719
Willy Tarreau721c9742017-11-07 11:05:42 +01001720 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001721 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001722 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001723 }
1724
1725 /* check for completion : the callee will change this to FRAME_A or
1726 * FRAME_H once done.
1727 */
1728 if (h2c->st0 == H2_CS_FRAME_P)
1729 return 0;
1730
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001731
1732 /* last frame */
1733 if (h2c->dff & H2_F_DATA_END_STREAM) {
1734 h2s->st = H2_SS_HREM;
1735 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001736 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001737 }
1738
Willy Tarreau454f9052017-10-26 19:40:35 +02001739 return 1;
1740
1741 conn_err:
1742 h2c_error(h2c, error);
1743 return 0;
1744
1745 strm_err:
1746 if (h2s) {
1747 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001748 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001749 }
1750 else
1751 h2c_error(h2c, error);
1752 return 0;
1753}
1754
Willy Tarreaubc933932017-10-09 16:21:43 +02001755/* process Rx frames to be demultiplexed */
1756static void h2_process_demux(struct h2c *h2c)
1757{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001758 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001759
Willy Tarreau081d4722017-05-16 21:51:05 +02001760 if (h2c->st0 >= H2_CS_ERROR)
1761 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001762
1763 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1764 if (h2c->st0 == H2_CS_PREFACE) {
1765 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1766 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001767 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001768 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001769 sess_log(h2c->conn->owner);
1770 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001771 goto fail;
1772 }
1773
1774 h2c->max_id = 0;
1775 h2c->st0 = H2_CS_SETTINGS1;
1776 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001777
1778 if (h2c->st0 == H2_CS_SETTINGS1) {
1779 struct h2_fh hdr;
1780
1781 /* ensure that what is pending is a valid SETTINGS frame
1782 * without an ACK.
1783 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001785 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001786 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001787 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001788 sess_log(h2c->conn->owner);
1789 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001790 goto fail;
1791 }
1792
1793 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1794 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1795 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1796 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001797 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001798 goto fail;
1799 }
1800
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001801 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001802 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1803 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1804 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001805 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001806 goto fail;
1807 }
1808
1809 /* that's OK, switch to FRAME_P to process it */
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 Tarreau4c3690b2017-10-10 15:16:55 +02001815 h2c->st0 = H2_CS_FRAME_P;
1816 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001817 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001818
1819 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001820 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001821 int ret = 0;
1822
1823 if (h2c->st0 >= H2_CS_ERROR)
1824 break;
1825
1826 if (h2c->st0 == H2_CS_FRAME_H) {
1827 struct h2_fh hdr;
1828
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001829 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001830 break;
1831
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001832 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001833 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1834 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001835 if (!h2c->nb_streams) {
1836 /* only log if no other stream can report the error */
1837 sess_log(h2c->conn->owner);
1838 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001839 break;
1840 }
1841
1842 h2c->dfl = hdr.len;
1843 h2c->dsi = hdr.sid;
1844 h2c->dft = hdr.ft;
1845 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001846 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001847 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001848 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001849 }
1850
1851 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001852 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1853
Olivier Houchard638b7992018-08-16 15:41:52 +02001854 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001855 /* we may have to signal the upper layers */
1856 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001857 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1858 /* cs has just been destroyed, we have to kill h2s. */
1859 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1860 goto strm_err;
1861 }
1862
1863 if (h2c->st0 >= H2_CS_ERROR)
1864 goto strm_err;
1865
1866 if (h2s->st >= H2_SS_ERROR) {
1867 /* stream error : send RST_STREAM */
1868 h2c->st0 = H2_CS_FRAME_E;
1869 }
1870 }
1871 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001872
Willy Tarreaud7901432017-12-29 11:34:40 +01001873 if (h2c->st0 == H2_CS_FRAME_E)
1874 goto strm_err;
1875
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001876 if (h2s->st == H2_SS_IDLE &&
1877 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1878 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1879 * this state MUST be treated as a connection error
1880 */
1881 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1882 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001883 if (!h2c->nb_streams) {
1884 /* only log if no other stream can report the error */
1885 sess_log(h2c->conn->owner);
1886 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001887 break;
1888 }
1889
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001890 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1891 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1892 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1893 * this state MUST be treated as a stream error
1894 */
1895 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1896 goto strm_err;
1897 }
1898
Willy Tarreauab837502017-12-27 15:07:30 +01001899 /* Below the management of frames received in closed state is a
1900 * bit hackish because the spec makes strong differences between
1901 * streams closed by receiving RST, sending RST, and seeing ES
1902 * in both directions. In addition to this, the creation of a
1903 * new stream reusing the identifier of a closed one will be
1904 * detected here. Given that we cannot keep track of all closed
1905 * streams forever, we consider that unknown closed streams were
1906 * closed on RST received, which allows us to respond with an
1907 * RST without breaking the connection (eg: to abort a transfer).
1908 * Some frames have to be silently ignored as well.
1909 */
1910 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1911 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1912 /* #5.1.1: The identifier of a newly
1913 * established stream MUST be numerically
1914 * greater than all streams that the initiating
1915 * endpoint has opened or reserved. This
1916 * governs streams that are opened using a
1917 * HEADERS frame and streams that are reserved
1918 * using PUSH_PROMISE. An endpoint that
1919 * receives an unexpected stream identifier
1920 * MUST respond with a connection error.
1921 */
1922 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1923 goto strm_err;
1924 }
1925
1926 if (h2s->flags & H2_SF_RST_RCVD) {
1927 /* RFC7540#5.1:closed: an endpoint that
1928 * receives any frame other than PRIORITY after
1929 * receiving a RST_STREAM MUST treat that as a
1930 * stream error of type STREAM_CLOSED.
1931 *
1932 * Note that old streams fall into this category
1933 * and will lead to an RST being sent.
1934 */
1935 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1936 h2c->st0 = H2_CS_FRAME_E;
1937 goto strm_err;
1938 }
1939
1940 /* RFC7540#5.1:closed: if this state is reached as a
1941 * result of sending a RST_STREAM frame, the peer that
1942 * receives the RST_STREAM might have already sent
1943 * frames on the stream that cannot be withdrawn. An
1944 * endpoint MUST ignore frames that it receives on
1945 * closed streams after it has sent a RST_STREAM
1946 * frame. An endpoint MAY choose to limit the period
1947 * over which it ignores frames and treat frames that
1948 * arrive after this time as being in error.
1949 */
1950 if (!(h2s->flags & H2_SF_RST_SENT)) {
1951 /* RFC7540#5.1:closed: any frame other than
1952 * PRIO/WU/RST in this state MUST be treated as
1953 * a connection error
1954 */
1955 if (h2c->dft != H2_FT_RST_STREAM &&
1956 h2c->dft != H2_FT_PRIORITY &&
1957 h2c->dft != H2_FT_WINDOW_UPDATE) {
1958 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1959 goto strm_err;
1960 }
1961 }
1962 }
1963
Willy Tarreauc0da1962017-10-30 18:38:00 +01001964#if 0
1965 // problem below: it is not possible to completely ignore such
1966 // streams as we need to maintain the compression state as well
1967 // and for this we need to completely process these frames (eg:
1968 // HEADERS frames) as well as counting DATA frames to emit
1969 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1970 // This is a typical case of layer violation where the
1971 // transported contents are critical to the connection's
1972 // validity and must be ignored at the same time :-(
1973
1974 /* graceful shutdown, ignore streams whose ID is higher than
1975 * the one advertised in GOAWAY. RFC7540#6.8.
1976 */
1977 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001978 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1979 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001980 h2c->dfl -= ret;
1981 ret = h2c->dfl == 0;
1982 goto strm_err;
1983 }
1984#endif
1985
Willy Tarreau7e98c052017-10-10 15:56:59 +02001986 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001987 case H2_FT_SETTINGS:
1988 if (h2c->st0 == H2_CS_FRAME_P)
1989 ret = h2c_handle_settings(h2c);
1990
1991 if (h2c->st0 == H2_CS_FRAME_A)
1992 ret = h2c_ack_settings(h2c);
1993 break;
1994
Willy Tarreaucf68c782017-10-10 17:11:41 +02001995 case H2_FT_PING:
1996 if (h2c->st0 == H2_CS_FRAME_P)
1997 ret = h2c_handle_ping(h2c);
1998
1999 if (h2c->st0 == H2_CS_FRAME_A)
2000 ret = h2c_ack_ping(h2c);
2001 break;
2002
Willy Tarreau26f95952017-07-27 17:18:30 +02002003 case H2_FT_WINDOW_UPDATE:
2004 if (h2c->st0 == H2_CS_FRAME_P)
2005 ret = h2c_handle_window_update(h2c, h2s);
2006 break;
2007
Willy Tarreau61290ec2017-10-17 08:19:21 +02002008 case H2_FT_CONTINUATION:
2009 /* we currently don't support CONTINUATION frames since
2010 * we have nowhere to store the partial HEADERS frame.
2011 * Let's abort the stream on an INTERNAL_ERROR here.
2012 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002013 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002014 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002015 h2c->st0 = H2_CS_FRAME_E;
2016 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002017 break;
2018
Willy Tarreau13278b42017-10-13 19:23:14 +02002019 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002020 if (h2c->st0 == H2_CS_FRAME_P) {
2021 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2022 if (tmp_h2s) {
2023 h2s = tmp_h2s;
2024 ret = 1;
2025 }
2026 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002027 break;
2028
Willy Tarreau454f9052017-10-26 19:40:35 +02002029 case H2_FT_DATA:
2030 if (h2c->st0 == H2_CS_FRAME_P)
2031 ret = h2c_frt_handle_data(h2c, h2s);
2032
2033 if (h2c->st0 == H2_CS_FRAME_A)
2034 ret = h2c_send_strm_wu(h2c);
2035 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002036
Willy Tarreau92153fc2017-12-03 19:46:19 +01002037 case H2_FT_PRIORITY:
2038 if (h2c->st0 == H2_CS_FRAME_P)
2039 ret = h2c_handle_priority(h2c);
2040 break;
2041
Willy Tarreaucd234e92017-08-18 10:59:39 +02002042 case H2_FT_RST_STREAM:
2043 if (h2c->st0 == H2_CS_FRAME_P)
2044 ret = h2c_handle_rst_stream(h2c, h2s);
2045 break;
2046
Willy Tarreaue96b0922017-10-30 00:28:29 +01002047 case H2_FT_GOAWAY:
2048 if (h2c->st0 == H2_CS_FRAME_P)
2049 ret = h2c_handle_goaway(h2c);
2050 break;
2051
Willy Tarreau1c661982017-10-30 13:52:01 +01002052 case H2_FT_PUSH_PROMISE:
2053 /* not permitted here, RFC7540#5.1 */
2054 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002055 if (!h2c->nb_streams) {
2056 /* only log if no other stream can report the error */
2057 sess_log(h2c->conn->owner);
2058 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002059 break;
2060
2061 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002062 default:
2063 /* drop frames that we ignore. They may be larger than
2064 * the buffer so we drain all of their contents until
2065 * we reach the end.
2066 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002067 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2068 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002069 h2c->dfl -= ret;
2070 ret = h2c->dfl == 0;
2071 }
2072
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002073 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002074 /* We may have to send an RST if not done yet */
2075 if (h2s->st == H2_SS_ERROR)
2076 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002077
Willy Tarreaua20a5192017-12-27 11:02:06 +01002078 if (h2c->st0 == H2_CS_FRAME_E)
2079 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002080
Willy Tarreau7e98c052017-10-10 15:56:59 +02002081 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002082 if (ret <= 0) {
2083 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002084 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002085 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002086
2087 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002088 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002089 h2c->st0 = H2_CS_FRAME_H;
2090 }
2091 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002092
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002093 if (h2c->rcvd_c > 0 &&
2094 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2095 h2c_send_conn_wu(h2c);
2096
Willy Tarreau52eed752017-09-22 15:05:09 +02002097 fail:
2098 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002099 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002100 /* we may have to signal the upper layers */
2101 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002102 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2103 /* cs has just been destroyed, we have to kill h2s. */
2104 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2105 h2c_send_rst_stream(h2c, h2s);
2106 }
2107 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002108 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002109}
2110
2111/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2112 * the end.
2113 */
2114static int h2_process_mux(struct h2c *h2c)
2115{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002116 struct h2s *h2s, *h2s_back;
2117
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002118 /* start by sending possibly pending window updates */
2119 if (h2c->rcvd_c > 0 &&
2120 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2121 h2c_send_conn_wu(h2c) < 0)
2122 goto fail;
2123
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002124 /* First we always process the flow control list because the streams
2125 * waiting there were already elected for immediate emission but were
2126 * blocked just on this.
2127 */
2128
2129 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2130 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2131 h2c->st0 >= H2_CS_ERROR)
2132 break;
2133
2134 /* In theory it's possible that h2s->cs == NULL here :
2135 * - client sends crap that causes a parse error
2136 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2137 * - RST_STREAM cannot be emitted because mux is busy/full
2138 * - stream gets notified, detaches and quits
2139 * - mux buffer gets ready and wakes pending streams up
2140 * - bam!
2141 */
2142 h2s->flags &= ~H2_SF_BLK_ANY;
2143
2144 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002145 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002146 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002147 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002148 }
2149
2150 /* depending on callee's blocking reasons, we may queue in send
2151 * list or completely dequeue.
2152 */
2153 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2154 if (h2s->flags & H2_SF_BLK_ANY) {
2155 LIST_DEL(&h2s->list);
2156 LIST_ADDQ(&h2c->send_list, &h2s->list);
2157 }
2158 else {
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 }
2170
2171 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2172 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2173 break;
2174
2175 /* In theory it's possible that h2s->cs == NULL here :
2176 * - client sends crap that causes a parse error
2177 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2178 * - RST_STREAM cannot be emitted because mux is busy/full
2179 * - stream gets notified, detaches and quits
2180 * - mux buffer gets ready and wakes pending streams up
2181 * - bam!
2182 */
2183 h2s->flags &= ~H2_SF_BLK_ANY;
2184
2185 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002186 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002187 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002188 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002189 }
2190 /* depending on callee's blocking reasons, we may queue in fctl
2191 * list or completely dequeue.
2192 */
2193 if (h2s->flags & H2_SF_BLK_MFCTL) {
2194 /* stream hit the connection's flow control */
2195 LIST_DEL(&h2s->list);
2196 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2197 }
2198 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2199 LIST_DEL(&h2s->list);
2200 LIST_INIT(&h2s->list);
2201 if (h2s->cs)
2202 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002203 else {
2204 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002205 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002206 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002207 }
2208 }
2209
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002210 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002211 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002212 if (h2c->st0 == H2_CS_ERROR) {
2213 if (h2c->max_id >= 0) {
2214 h2c_send_goaway_error(h2c, NULL);
2215 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2216 return 0;
2217 }
2218
2219 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2220 }
2221 return 1;
2222 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002223 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002224}
2225
Willy Tarreau71681172017-10-23 14:39:06 +02002226
Willy Tarreau62f52692017-10-08 23:01:42 +02002227/*********************************************************/
2228/* functions below are I/O callbacks from the connection */
2229/*********************************************************/
2230
2231/* callback called on recv event by the connection handler */
2232static void h2_recv(struct connection *conn)
2233{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002234 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002235 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002236 int max;
2237
Willy Tarreau315d8072017-12-10 22:17:57 +01002238 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002239 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240
Willy Tarreau44e973f2018-03-01 17:49:30 +01002241 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002242 if (!buf) {
2243 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002244 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002245 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002246
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002247 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002248 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002249 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002250
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002251 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002252 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002253 return;
2254 }
2255
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002256 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002257 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002258 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002259}
2260
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002261/* Try to send data if possible */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002262static void h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002263{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002264 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002265 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002266
2267 if (conn->flags & CO_FL_ERROR)
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002268 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002269
2270 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2271 /* a handshake was requested */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002272 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002273 }
2274
Willy Tarreaubc933932017-10-09 16:21:43 +02002275 /* This loop is quite simple : it tries to fill as much as it can from
2276 * pending streams into the existing buffer until it's reportedly full
2277 * or the end of send requests is reached. Then it tries to send this
2278 * buffer's contents out, marks it not full if at least one byte could
2279 * be sent, and tries again.
2280 *
2281 * The snd_buf() function normally takes a "flags" argument which may
2282 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2283 * data immediately comes and CO_SFL_STREAMER to indicate that the
2284 * connection is streaming lots of data (used to increase TLS record
2285 * size at the expense of latency). The former can be sent any time
2286 * there's a buffer full flag, as it indicates at least one stream
2287 * attempted to send and failed so there are pending data. An
2288 * alternative would be to set it as long as there's an active stream
2289 * but that would be problematic for ACKs until we have an absolute
2290 * guarantee that all waiters have at least one byte to send. The
2291 * latter should possibly not be set for now.
2292 */
2293
2294 done = 0;
2295 while (!done) {
2296 unsigned int flags = 0;
2297
2298 /* fill as much as we can into the current buffer */
2299 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2300 done = h2_process_mux(h2c);
2301
2302 if (conn->flags & CO_FL_ERROR)
2303 break;
2304
2305 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2306 flags |= CO_SFL_MSG_MORE;
2307
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002308 if (b_data(&h2c->mbuf)) {
2309 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002310 if (!ret)
2311 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002312 b_del(&h2c->mbuf, ret);
2313 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002314 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002315
2316 /* wrote at least one byte, the buffer is not full anymore */
2317 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2318 }
2319
Willy Tarreaua2af5122017-10-09 11:56:46 +02002320 if (conn->flags & CO_FL_SOCK_WR_SH) {
2321 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002322 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002323 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002324 /* We're not full anymore, so we can wake any task that are waiting
2325 * for us.
2326 */
2327 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2328 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2329 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2330 struct wait_list *, list);
2331 LIST_DEL(&sw->list);
2332 LIST_INIT(&sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02002333 sw->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02002334 tasklet_wakeup(sw->task);
2335 }
2336
2337 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002338 /* We're done, no more to send */
2339 if (!b_data(&h2c->mbuf))
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002340 return;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002341schedule:
2342 if (LIST_ISEMPTY(&h2c->wait_list.list))
2343 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002344 return;
2345}
2346
2347static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2348{
2349 struct h2c *h2c = ctx;
2350
2351 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
2352 h2_send(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002353 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002354}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002355
Willy Tarreau62f52692017-10-08 23:01:42 +02002356/* callback called on any event by the connection handler.
2357 * It applies changes and returns zero, or < 0 if it wants immediate
2358 * destruction of the connection (which normally doesn not happen in h2).
2359 */
2360static int h2_wake(struct connection *conn)
2361{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002362 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002363 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002364
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002365 h2_send(h2c);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002366 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002367 h2_process_demux(h2c);
2368
2369 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002370 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002371
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002372 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002373 h2c->flags &= ~H2_CF_DEM_DFULL;
2374 }
2375
Willy Tarreau8ec14062017-12-30 18:08:13 +01002376 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2377 /* frontend is stopping, reload likely in progress, let's try
2378 * to announce a graceful shutdown if not yet done. We don't
2379 * care if it fails, it will be tried again later.
2380 */
2381 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2382 if (h2c->last_sid < 0)
2383 h2c->last_sid = (1U << 31) - 1;
2384 h2c_send_goaway_error(h2c, NULL);
2385 }
2386 }
2387
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002388 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002389 * If we received early data, and the handshake is done, wake
2390 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002391 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002392 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2393 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2394 struct eb32_node *node;
2395 struct h2s *h2s;
2396
2397 h2c->flags |= H2_CF_WAIT_FOR_HS;
2398 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2399
2400 while (node) {
2401 h2s = container_of(node, struct h2s, by_id);
2402 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2403 h2s->cs->data_cb->wake(h2s->cs);
2404 node = eb32_next(node);
2405 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002406 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002407
Willy Tarreau26bd7612017-10-09 16:47:04 +02002408 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002409 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2410 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2411 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002412 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002413
2414 if (eb_is_empty(&h2c->streams_by_id)) {
2415 /* no more stream, kill the connection now */
2416 h2_release(conn);
2417 return -1;
2418 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002419 }
2420
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002421 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002422 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002423
2424 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002425 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002426 __conn_xprt_stop_recv(conn);
2427 }
2428 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002429 __conn_xprt_want_recv(conn);
2430 }
2431
2432 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002433 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002434 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002435 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002436 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002437 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2438 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439 __conn_xprt_want_send(conn);
2440 }
2441 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002442 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002443 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444 }
2445
Willy Tarreau3f133572017-10-31 19:21:06 +01002446 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002447 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002448 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002449 task_queue(h2c->task);
2450 }
2451 else
2452 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002453 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002454
Willy Tarreau62f52692017-10-08 23:01:42 +02002455 return 0;
2456}
2457
Willy Tarreauea392822017-10-31 10:02:25 +01002458/* Connection timeout management. The principle is that if there's no receipt
2459 * nor sending for a certain amount of time, the connection is closed. If the
2460 * MUX buffer still has lying data or is not allocatable, the connection is
2461 * immediately killed. If it's allocatable and empty, we attempt to send a
2462 * GOAWAY frame.
2463 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002464static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002465{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002466 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002467 int expired = tick_is_expired(t->expire, now_ms);
2468
Willy Tarreau0975f112018-03-29 15:22:59 +02002469 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002470 return t;
2471
Willy Tarreau0975f112018-03-29 15:22:59 +02002472 task_delete(t);
2473 task_free(t);
2474
2475 if (!h2c) {
2476 /* resources were already deleted */
2477 return NULL;
2478 }
2479
2480 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002481 h2c_error(h2c, H2_ERR_NO_ERROR);
2482 h2_wake_some_streams(h2c, 0, 0);
2483
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002484 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002485 /* don't even try to send a GOAWAY, the buffer is stuck */
2486 h2c->flags |= H2_CF_GOAWAY_FAILED;
2487 }
2488
2489 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002490 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002491 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2492 h2c->flags |= H2_CF_GOAWAY_FAILED;
2493
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002494 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2495 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002496 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002497 b_del(&h2c->mbuf, ret);
2498 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002499 }
2500 }
Willy Tarreauea392822017-10-31 10:02:25 +01002501
Willy Tarreau0975f112018-03-29 15:22:59 +02002502 /* either we can release everything now or it will be done later once
2503 * the last stream closes.
2504 */
2505 if (eb_is_empty(&h2c->streams_by_id))
2506 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002507
Willy Tarreauea392822017-10-31 10:02:25 +01002508 return NULL;
2509}
2510
2511
Willy Tarreau62f52692017-10-08 23:01:42 +02002512/*******************************************/
2513/* functions below are used by the streams */
2514/*******************************************/
2515
2516/*
2517 * Attach a new stream to a connection
2518 * (Used for outgoing connections)
2519 */
2520static struct conn_stream *h2_attach(struct connection *conn)
2521{
2522 return NULL;
2523}
2524
2525/* callback used to update the mux's polling flags after changing a cs' status.
2526 * The caller (cs_update_mux_polling) will take care of propagating any changes
2527 * to the transport layer.
2528 */
2529static void h2_update_poll(struct conn_stream *cs)
2530{
Willy Tarreau1d393222017-10-17 10:26:19 +02002531 struct h2s *h2s = cs->ctx;
2532
2533 if (!h2s)
2534 return;
2535
Willy Tarreaud7739c82017-10-30 15:38:23 +01002536 /* we may unblock a blocked read */
2537
Willy Tarreau315d8072017-12-10 22:17:57 +01002538 if (cs->flags & CS_FL_DATA_RD_ENA) {
2539 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002540 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002541 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002542 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002543 conn_xprt_want_send(cs->conn);
2544 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002545 }
2546
Willy Tarreau1d393222017-10-17 10:26:19 +02002547 /* Note: the stream and stream-int code doesn't allow us to perform a
2548 * synchronous send() here unfortunately, because this code is called
2549 * as si_update() from the process_stream() context. This means that
2550 * we have to queue the current cs and defer its processing after the
2551 * connection's cs list is processed anyway.
2552 */
2553
2554 if (cs->flags & CS_FL_DATA_WR_ENA) {
2555 if (LIST_ISEMPTY(&h2s->list)) {
2556 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002557 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002558 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2559 conn_xprt_want_send(cs->conn);
2560 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2561 }
2562 }
2563 else if (!LIST_ISEMPTY(&h2s->list)) {
2564 LIST_DEL(&h2s->list);
2565 LIST_INIT(&h2s->list);
2566 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2567 }
2568
2569 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002570 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002571 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002572}
2573
2574/*
2575 * Detach the stream from the connection and possibly release the connection.
2576 */
2577static void h2_detach(struct conn_stream *cs)
2578{
Willy Tarreau60935142017-10-16 18:11:19 +02002579 struct h2s *h2s = cs->ctx;
2580 struct h2c *h2c;
2581
2582 cs->ctx = NULL;
2583 if (!h2s)
2584 return;
2585
2586 h2c = h2s->h2c;
2587 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002588 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002589 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2590 !h2_has_too_many_cs(h2c)) {
2591 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2592 if (h2_recv_allowed(h2c)) {
2593 __conn_xprt_want_recv(h2c->conn);
2594 conn_xprt_want_send(h2c->conn);
2595 }
2596 }
Willy Tarreau60935142017-10-16 18:11:19 +02002597
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002598 /* this stream may be blocked waiting for some data to leave (possibly
2599 * an ES or RST frame), so orphan it in this case.
2600 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002601 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002602 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002603 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002604 return;
2605
Willy Tarreau45f752e2017-10-30 15:44:59 +01002606 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2607 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2608 /* unblock the connection if it was blocked on this
2609 * stream.
2610 */
2611 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2612 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2613 conn_xprt_want_recv(cs->conn);
2614 conn_xprt_want_send(cs->conn);
2615 }
2616
Willy Tarreau71049cc2018-03-28 13:56:39 +02002617 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002618
Willy Tarreaue323f342018-03-28 13:51:45 +02002619 /* We don't want to close right now unless we're removing the
2620 * last stream, and either the connection is in error, or it
2621 * reached the ID already specified in a GOAWAY frame received
2622 * or sent (as seen by last_sid >= 0).
2623 */
2624 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2625 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002626 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002627 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002628 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002629 (conn_xprt_read0_pending(h2c->conn) ||
2630 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2631 /* no more stream will come, kill it now */
2632 h2_release(h2c->conn);
2633 }
2634 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002635 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002636 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2637 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002638 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002639 else
2640 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002641 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002642}
2643
2644static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2645{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002646 struct h2s *h2s = cs->ctx;
2647
2648 if (!mode)
2649 return;
2650
Willy Tarreau721c9742017-11-07 11:05:42 +01002651 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002652 return;
2653
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002654 /* if no outgoing data was seen on this stream, it means it was
2655 * closed with a "tcp-request content" rule that is normally
2656 * used to kill the connection ASAP (eg: limit abuse). In this
2657 * case we send a goaway to close the connection.
2658 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002659 if (!(h2s->flags & H2_SF_RST_SENT) &&
2660 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002661 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002662
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002663 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2664 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2665 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002666 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002667
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002668 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002669 conn_xprt_want_send(cs->conn);
2670
Willy Tarreau00dd0782018-03-01 16:31:34 +01002671 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002672
2673 add_to_list:
2674 if (LIST_ISEMPTY(&h2s->list)) {
2675 if (h2s->flags & H2_SF_BLK_MFCTL)
2676 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2677 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2678 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2679 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002680}
2681
2682static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2683{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002684 struct h2s *h2s = cs->ctx;
2685
Willy Tarreau721c9742017-11-07 11:05:42 +01002686 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002687 return;
2688
Willy Tarreau67434202017-11-06 20:20:51 +01002689 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002690 /* we can cleanly close using an empty data frame only after headers */
2691
2692 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2693 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002694 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002695
2696 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002697 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002698 else
2699 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002700 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002701 /* if no outgoing data was seen on this stream, it means it was
2702 * closed with a "tcp-request content" rule that is normally
2703 * used to kill the connection ASAP (eg: limit abuse). In this
2704 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002705 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002706 if (!(h2s->flags & H2_SF_RST_SENT) &&
2707 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002708 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002709
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002710 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2711 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002712 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002713 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002714
Willy Tarreau00dd0782018-03-01 16:31:34 +01002715 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002716 }
2717
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002718 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002719 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002720
2721 add_to_list:
2722 if (LIST_ISEMPTY(&h2s->list)) {
2723 if (h2s->flags & H2_SF_BLK_MFCTL)
2724 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2725 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2726 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2727 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002728}
2729
Willy Tarreau13278b42017-10-13 19:23:14 +02002730/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2731 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2732 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002733 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002734 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002735static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002736{
2737 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002739 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002740 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002741 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002742 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002743 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002744 int flen = h2c->dfl;
2745 int outlen = 0;
2746 int wrap;
2747 int try;
2748
2749 if (!h2c->dfl) {
2750 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002751 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002752 return 0;
2753 }
2754
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002755 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002756 return 0; // incomplete input frame
2757
Willy Tarreau13278b42017-10-13 19:23:14 +02002758 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002759 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002760 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002761 copy = alloc_trash_chunk();
2762 if (!copy) {
2763 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2764 goto fail;
2765 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2767 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2768 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002769 }
2770
2771 /* The padlen is the first byte before data, and the padding appears
2772 * after data. padlen+data+padding are included in flen.
2773 */
2774 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002775 h2c->dpl = *hdrs;
2776 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2778 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002779 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002780 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002781 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002782 hdrs += 1; // skip Pad Length
2783 }
2784
2785 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2786 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002787 if (read_n32(hdrs) == h2s->id) {
2788 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2789 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002790 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002791 }
2792
Willy Tarreau13278b42017-10-13 19:23:14 +02002793 hdrs += 5; // stream dep = 4, weight = 1
2794 flen -= 5;
2795 }
2796
2797 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2798 * don't support this for now and can't even decompress so we have to
2799 * break the connection.
2800 */
2801 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2802 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002803 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002804 }
2805
Olivier Houchard638b7992018-08-16 15:41:52 +02002806 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002807 if (!csbuf) {
2808 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002809 goto fail;
2810 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002811
Willy Tarreau937f7602018-02-26 15:22:17 +01002812 /* we can't retry a failed decompression operation so we must be very
2813 * careful not to take any risks. In practice the output buffer is
2814 * always empty except maybe for trailers, in which case we simply have
2815 * to wait for the upper layer to finish consuming what is available.
2816 */
2817 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002818 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002819
Willy Tarreau937f7602018-02-26 15:22:17 +01002820 csbuf->head = 0;
2821 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002822
2823 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2824 sizeof(list)/sizeof(list[0]), tmp);
2825 if (outlen < 0) {
2826 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2827 goto fail;
2828 }
2829
2830 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002831 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002832 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002833
2834 if (outlen < 0) {
2835 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2836 goto fail;
2837 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002838
Willy Tarreau174b06a2018-04-25 18:13:58 +02002839 if (msgf & H2_MSGF_BODY) {
2840 /* a payload is present */
2841 if (msgf & H2_MSGF_BODY_CL)
2842 h2s->flags |= H2_SF_DATA_CLEN;
2843 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2844 h2s->flags |= H2_SF_DATA_CHNK;
2845 }
2846
Willy Tarreau13278b42017-10-13 19:23:14 +02002847 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002849 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002850 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002851
Willy Tarreau39d68502018-03-02 12:26:37 +01002852 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002853 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002854 h2s->cs->flags |= CS_FL_REOS;
2855 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002856
Willy Tarreau68dd9852017-07-03 14:44:26 +02002857 leave:
2858 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002859 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002860 fail:
2861 outlen = 0;
2862 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002863}
2864
Willy Tarreau454f9052017-10-26 19:40:35 +02002865/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2866 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2867 * in use, a new chunk is emitted for each frame. This is supposed to fit
2868 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2869 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2870 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2871 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002872 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2873 * checked to know if some data remain pending (an empty DATA frame can return
2874 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2875 * connection errors in h2c->errcode. The caller must already have checked the
2876 * frame header and ensured that the frame was complete or the buffer full. It
2877 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002878 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002879static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002880{
2881 struct h2c *h2c = h2s->h2c;
2882 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002883 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002884 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002885 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002886
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002887 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002888
2889 /* The padlen is the first byte before data, and the padding appears
2890 * after data. padlen+data+padding are included in flen.
2891 */
Willy Tarreau79127812017-12-03 21:06:59 +01002892 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002894 return 0;
2895
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002897 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002898 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2899 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002900 return 0;
2901 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002902
2903 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002904 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002905 h2c->dfl--;
2906 h2c->rcvd_c++; h2c->rcvd_s++;
2907 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002908 }
2909
Olivier Houchard638b7992018-08-16 15:41:52 +02002910 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002911 if (!csbuf) {
2912 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002913 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002914 }
2915
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002916 flen = h2c->dfl - h2c->dpl;
2917 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002918 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002920 if (flen > b_data(&h2c->dbuf)) {
2921 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002922 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002923 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002924 }
2925
2926 if (unlikely(b_space_wraps(csbuf))) {
2927 /* it doesn't fit and the buffer is fragmented,
2928 * so let's defragment it and try again.
2929 */
2930 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002931 }
2932
Willy Tarreaueba10f22018-04-25 20:44:22 +02002933 /* chunked-encoding requires more room */
2934 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002935 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002936 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2937 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2938 (chklen < 1048576) ? 4 : 8;
2939 chklen += 4; // CRLF, CRLF
2940 }
2941
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002942 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002943 if (flen + chklen > b_room(csbuf)) {
2944 if (chklen >= b_room(csbuf)) {
2945 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002946 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002947 }
2948 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002949 }
2950
2951 if (h2s->flags & H2_SF_DATA_CHNK) {
2952 /* emit the chunk size */
2953 unsigned int chksz = flen;
2954 char str[10];
2955 char *beg;
2956
2957 beg = str + sizeof(str);
2958 *--beg = '\n';
2959 *--beg = '\r';
2960 do {
2961 *--beg = hextab[chksz & 0xF];
2962 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002963 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002964 }
2965
Willy Tarreau454f9052017-10-26 19:40:35 +02002966 /* Block1 is the length of the first block before the buffer wraps,
2967 * block2 is the optional second block to reach the end of the frame.
2968 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002970 if (block1 > flen)
2971 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002972 block2 = flen - block1;
2973
2974 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002975 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002976
2977 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002978 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002979
Willy Tarreaueba10f22018-04-25 20:44:22 +02002980 if (h2s->flags & H2_SF_DATA_CHNK) {
2981 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002982 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002983 }
2984
Willy Tarreau454f9052017-10-26 19:40:35 +02002985 /* now mark the input data as consumed (will be deleted from the buffer
2986 * by the caller when seeing FRAME_A after sending the window update).
2987 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989 h2c->dfl -= flen;
2990 h2c->rcvd_c += flen;
2991 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2992
2993 if (h2c->dfl > h2c->dpl) {
2994 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002995 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002996 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002997 }
2998
Willy Tarreau4a28da12018-01-04 14:41:00 +01002999 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003000 /* here we're done with the frame, all the payload (except padding) was
3001 * transferred.
3002 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003003
3004 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3005 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003006 if (b_room(csbuf) < 5) {
3007 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003008 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003009 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003010 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003011 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003012 }
3013
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003014 h2c->rcvd_c += h2c->dpl;
3015 h2c->rcvd_s += h2c->dpl;
3016 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003017 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3018
Willy Tarreau39d68502018-03-02 12:26:37 +01003019 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003020 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003021 h2s->cs->flags |= CS_FL_REOS;
3022 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003023
Willy Tarreau454b57b2018-02-26 15:50:05 +01003024 return flen + chklen;
3025 fail:
3026 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003027}
3028
Willy Tarreau5dd17352018-06-14 13:33:30 +02003029/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3030 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3031 * number of bytes sent. The caller must check the stream's status to detect
3032 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003033 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003034static 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 +02003035{
3036 struct http_hdr list[MAX_HTTP_HDR];
3037 struct h2c *h2c = h2s->h2c;
3038 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003039 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003040 int es_now = 0;
3041 int ret = 0;
3042 int hdr;
3043
3044 if (h2c_mux_busy(h2c, h2s)) {
3045 h2s->flags |= H2_SF_BLK_MBUSY;
3046 return 0;
3047 }
3048
Willy Tarreau44e973f2018-03-01 17:49:30 +01003049 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003050 h2c->flags |= H2_CF_MUX_MALLOC;
3051 h2s->flags |= H2_SF_BLK_MROOM;
3052 return 0;
3053 }
3054
3055 /* First, try to parse the H1 response and index it into <list>.
3056 * NOTE! Since it comes from haproxy, we *know* that a response header
3057 * block does not wrap and we can safely read it this way without
3058 * having to realign the buffer.
3059 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003060 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003061 list, sizeof(list)/sizeof(list[0]), h1m);
3062 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003063 /* incomplete or invalid response, this is abnormal coming from
3064 * haproxy and may only result in a bad errorfile or bad Lua code
3065 * so that won't be fixed, raise an error now.
3066 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003067 * FIXME: we should instead add the ability to only return a
3068 * 502 bad gateway. But in theory this is not supposed to
3069 * happen.
3070 */
3071 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3072 ret = 0;
3073 goto end;
3074 }
3075
3076 chunk_reset(&outbuf);
3077
3078 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003079 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003080 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003081 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003082
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003083 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003084 break;
3085 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003086 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003087 }
3088
3089 if (outbuf.size < 9) {
3090 h2c->flags |= H2_CF_MUX_MFULL;
3091 h2s->flags |= H2_SF_BLK_MROOM;
3092 ret = 0;
3093 goto end;
3094 }
3095
3096 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003097 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3098 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3099 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100
3101 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003102 if (outbuf.data < outbuf.size && h1m->status == 200)
3103 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3104 else if (outbuf.data < outbuf.size && h1m->status == 304)
3105 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003106 else if (unlikely(list[0].v.len != 3)) {
3107 /* this is an unparsable response */
3108 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3109 ret = 0;
3110 goto end;
3111 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003113 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003114 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3115 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3116 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3117 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3118 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003119 }
3120 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003121 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003122 goto realign_again;
3123
3124 h2c->flags |= H2_CF_MUX_MFULL;
3125 h2s->flags |= H2_SF_BLK_MROOM;
3126 ret = 0;
3127 goto end;
3128 }
3129
3130 /* encode all headers, stop at empty name */
3131 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003132 /* these ones do not exist in H2 and must be dropped. */
3133 if (isteq(list[hdr].n, ist("connection")) ||
3134 isteq(list[hdr].n, ist("proxy-connection")) ||
3135 isteq(list[hdr].n, ist("keep-alive")) ||
3136 isteq(list[hdr].n, ist("upgrade")) ||
3137 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138 continue;
3139
3140 if (isteq(list[hdr].n, ist("")))
3141 break; // end
3142
3143 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3144 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003145 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146 goto realign_again;
3147
3148 h2c->flags |= H2_CF_MUX_MFULL;
3149 h2s->flags |= H2_SF_BLK_MROOM;
3150 ret = 0;
3151 goto end;
3152 }
3153 }
3154
3155 /* we may need to add END_STREAM */
3156 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3157 es_now = 1;
3158
3159 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003161
3162 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003164
3165 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003166 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003167
3168 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003170 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003171
3172 /* for now we don't implemented CONTINUATION, so we wait for a
3173 * body or directly end in TRL2.
3174 */
3175 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003176 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003177 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003178
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003179 h1m->state = HTTP_MSG_DONE;
3180 h2s->flags |= H2_SF_ES_SENT;
3181 if (h2s->st == H2_SS_OPEN)
3182 h2s->st = H2_SS_HLOC;
3183 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003184 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003185 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003186 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003187 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003188 h1m->state = HTTP_MSG_RPBEFORE;
3189 h1m->status = 0;
3190 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003191 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003192 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003193 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003194 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003195
3196 end:
3197 //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));
3198 return ret;
3199}
3200
Willy Tarreau5dd17352018-06-14 13:33:30 +02003201/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3202 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3203 * the number of bytes sent. The caller must check the stream's status to
3204 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003205 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003206static 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 +02003207{
3208 struct h2c *h2c = h2s->h2c;
3209 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003210 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003211 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003212 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003213 int es_now = 0;
3214 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003215 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003216 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003217
3218 if (h2c_mux_busy(h2c, h2s)) {
3219 h2s->flags |= H2_SF_BLK_MBUSY;
3220 goto end;
3221 }
3222
Willy Tarreau44e973f2018-03-01 17:49:30 +01003223 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224 h2c->flags |= H2_CF_MUX_MALLOC;
3225 h2s->flags |= H2_SF_BLK_MROOM;
3226 goto end;
3227 }
3228
3229 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003230 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003231 goto end;
3232
3233 chunk_reset(&outbuf);
3234
3235 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003236 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003237 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003238 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003239
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003240 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003241 break;
3242 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003243 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003244 }
3245
3246 if (outbuf.size < 9) {
3247 h2c->flags |= H2_CF_MUX_MFULL;
3248 h2s->flags |= H2_SF_BLK_MROOM;
3249 goto end;
3250 }
3251
3252 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3254 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3255 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003256
3257 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3258 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003259 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003260 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003261 break;
3262 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003263 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003264 if ((long long)size > h1m->curr_len)
3265 size = h1m->curr_len;
3266 break;
3267 default: /* te:chunked : parse chunks */
3268 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003269 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003270 if (!ret)
3271 goto end;
3272
3273 if (ret < 0) {
3274 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3275 h1m->err_pos = ret;
3276 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3277 goto end;
3278 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003279 max -= ret;
3280 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003281 total += ret;
3282 h1m->state = HTTP_MSG_CHUNK_SIZE;
3283 }
3284
3285 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3286 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003287 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 if (!ret)
3289 goto end;
3290
3291 if (ret < 0) {
3292 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3293 h1m->err_pos = ret;
3294 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3295 goto end;
3296 }
3297
3298 size = chunk;
3299 h1m->curr_len = chunk;
3300 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003301 max -= ret;
3302 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003303 total += ret;
3304 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3305 if (!size)
3306 goto send_empty;
3307 }
3308
3309 /* in MSG_DATA state, continue below */
3310 size = h1m->curr_len;
3311 break;
3312 }
3313
3314 /* we have in <size> the exact number of bytes we need to copy from
3315 * the H1 buffer. We need to check this against the connection's and
3316 * the stream's send windows, and to ensure that this fits in the max
3317 * frame size and in the buffer's available space minus 9 bytes (for
3318 * the frame header). The connection's flow control is applied last so
3319 * that we can use a separate list of streams which are immediately
3320 * unblocked on window opening. Note: we don't implement padding.
3321 */
3322
Willy Tarreau5dd17352018-06-14 13:33:30 +02003323 if (size > max)
3324 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325
3326 if (size > h2s->mws)
3327 size = h2s->mws;
3328
3329 if (size <= 0) {
3330 h2s->flags |= H2_SF_BLK_SFCTL;
3331 goto end;
3332 }
3333
3334 if (h2c->mfs && size > h2c->mfs)
3335 size = h2c->mfs;
3336
3337 if (size + 9 > outbuf.size) {
3338 /* we have an opportunity for enlarging the too small
3339 * available space, let's try.
3340 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003341 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 goto realign_again;
3343 size = outbuf.size - 9;
3344 }
3345
3346 if (size <= 0) {
3347 h2c->flags |= H2_CF_MUX_MFULL;
3348 h2s->flags |= H2_SF_BLK_MROOM;
3349 goto end;
3350 }
3351
3352 if (size > h2c->mws)
3353 size = h2c->mws;
3354
3355 if (size <= 0) {
3356 h2s->flags |= H2_SF_BLK_MFCTL;
3357 goto end;
3358 }
3359
3360 /* copy whatever we can */
3361 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003362 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363 if (ret == 1)
3364 len2 = 0;
3365
3366 if (!ret || len1 + len2 < size) {
3367 /* FIXME: must normally never happen */
3368 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3369 goto end;
3370 }
3371
3372 /* limit len1/len2 to size */
3373 if (len1 + len2 > size) {
3374 int sub = len1 + len2 - size;
3375
3376 if (len2 > sub)
3377 len2 -= sub;
3378 else {
3379 sub -= len2;
3380 len2 = 0;
3381 len1 -= sub;
3382 }
3383 }
3384
3385 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003386 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003387 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003388 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003389
3390 send_empty:
3391 /* we may need to add END_STREAM */
3392 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3393 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003394 *
3395 * FIXME: what we do here is not correct because we send end_stream
3396 * before knowing if we'll have to send a HEADERS frame for the
3397 * trailers. More importantly we're not consuming the trailing CRLF
3398 * after the end of trailers, so it will be left to the caller to
3399 * eat it. The right way to do it would be to measure trailers here
3400 * and to send ES only if there are no trailers.
3401 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003402 */
3403 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3404 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3405 es_now = 1;
3406
3407 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003408 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003409
3410 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003411 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003412
3413 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003414 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003415
3416 /* consume incoming H1 response */
3417 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003418 max -= size;
3419 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003420 total += size;
3421 h1m->curr_len -= size;
3422 h2s->mws -= size;
3423 h2c->mws -= size;
3424
3425 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3426 h1m->state = HTTP_MSG_CHUNK_CRLF;
3427 goto new_frame;
3428 }
3429 }
3430
3431 if (es_now) {
3432 if (h2s->st == H2_SS_OPEN)
3433 h2s->st = H2_SS_HLOC;
3434 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003435 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003436
Willy Tarreau35a62702018-02-27 15:37:25 +01003437 if (!(h1m->flags & H1_MF_CHNK)) {
3438 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003439 total += max;
3440 ofs += max;
3441 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003442
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003443 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003444 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003445
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003446 h2s->flags |= H2_SF_ES_SENT;
3447 }
3448
3449 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003450 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 +02003451 return total;
3452}
3453
Olivier Houchard6ff20392018-07-17 18:46:31 +02003454/* Called from the upper layer, to subscribe to events, such as being able to send */
3455static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3456{
3457 struct wait_list *sw;
3458 struct h2s *h2s = cs->ctx;
3459
3460 switch (event_type) {
3461 case SUB_CAN_SEND:
3462 sw = param;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003463 if (LIST_ISEMPTY(&h2s->list) &&
3464 !(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003465 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003466 sw->wait_reason |= SUB_CAN_SEND;
3467 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003468 return 0;
3469 default:
3470 break;
3471 }
3472 return -1;
3473
3474
3475}
3476
Olivier Houchard511efea2018-08-16 15:30:32 +02003477/* Called from the upper layer, to receive data */
3478static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3479{
Olivier Houchard638b7992018-08-16 15:41:52 +02003480 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003481 size_t ret = 0;
3482
3483 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003484 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003485
Olivier Houchard638b7992018-08-16 15:41:52 +02003486 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003487 cs->flags |= CS_FL_RCV_MORE;
3488 else {
3489 cs->flags &= ~CS_FL_RCV_MORE;
3490 if (cs->flags & CS_FL_REOS)
3491 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003492 if (b_size(&h2s->rxbuf)) {
3493 b_free(&h2s->rxbuf);
3494 offer_buffers(NULL, tasks_run_queue);
3495 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003496 }
3497
3498 return ret;
3499}
3500
Willy Tarreau62f52692017-10-08 23:01:42 +02003501/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003502static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003503{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003504 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003505 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003506 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003507
Willy Tarreau0bad0432018-06-14 16:54:01 +02003508 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003509 h2s->flags |= H2_SF_OUTGOING_DATA;
3510
Willy Tarreau0bad0432018-06-14 16:54:01 +02003511 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003512 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003513 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003514 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003515 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003516 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003517 }
3518 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3519 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003520 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003521
Willy Tarreau5dd17352018-06-14 13:33:30 +02003522 if (unlikely((int)ret <= 0)) {
3523 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003524 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3525 break;
3526 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003527 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003528 total += count;
3529 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003530 h2s->res.state = HTTP_MSG_DONE;
3531 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003532 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003533 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003534 cs->flags |= CS_FL_ERROR;
3535 break;
3536 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003537
3538 total += ret;
3539 count -= ret;
3540
3541 if (h2s->st >= H2_SS_ERROR)
3542 break;
3543
3544 if (h2s->flags & H2_SF_BLK_ANY)
3545 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003546 }
3547
Willy Tarreau00610962018-07-19 10:58:28 +02003548 if (h2s->st >= H2_SS_ERROR) {
3549 /* trim any possibly pending data after we close (extra CR-LF,
3550 * unprocessed trailers, abnormal extra data, ...)
3551 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003552 total += count;
3553 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003554 }
3555
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003556 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003557 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003558 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003559 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003560 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003561 }
3562
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003563 if (h2s->flags & H2_SF_BLK_SFCTL) {
3564 /* stream flow control, quit the list */
3565 LIST_DEL(&h2s->list);
3566 LIST_INIT(&h2s->list);
3567 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003568 else if (LIST_ISEMPTY(&h2s->list)) {
3569 if (h2s->flags & H2_SF_BLK_MFCTL)
3570 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003571 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003572
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003573 b_del(buf, total);
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003574 if (total > 0)
3575 conn_xprt_want_send(h2s->h2c->conn);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003576 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003577}
3578
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003579/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003580static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003581{
3582 struct h2c *h2c = conn->mux_ctx;
3583 struct h2s *h2s;
3584 struct eb32_node *node;
3585 int fctl_cnt = 0;
3586 int send_cnt = 0;
3587 int tree_cnt = 0;
3588 int orph_cnt = 0;
3589
3590 if (!h2c)
3591 return;
3592
3593 list_for_each_entry(h2s, &h2c->fctl_list, list)
3594 fctl_cnt++;
3595
3596 list_for_each_entry(h2s, &h2c->send_list, list)
3597 send_cnt++;
3598
3599 node = eb32_first(&h2c->streams_by_id);
3600 while (node) {
3601 h2s = container_of(node, struct h2s, by_id);
3602 tree_cnt++;
3603 if (!h2s->cs)
3604 orph_cnt++;
3605 node = eb32_next(node);
3606 }
3607
Willy Tarreau616ac812018-07-24 14:12:42 +02003608 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3609 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3610 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3611 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3612 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3613 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003614}
Willy Tarreau62f52692017-10-08 23:01:42 +02003615
3616/*******************************************************/
3617/* functions below are dedicated to the config parsers */
3618/*******************************************************/
3619
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003620/* config parser for global "tune.h2.header-table-size" */
3621static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3622 struct proxy *defpx, const char *file, int line,
3623 char **err)
3624{
3625 if (too_many_args(1, args, err, NULL))
3626 return -1;
3627
3628 h2_settings_header_table_size = atoi(args[1]);
3629 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3630 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3631 return -1;
3632 }
3633 return 0;
3634}
Willy Tarreau62f52692017-10-08 23:01:42 +02003635
Willy Tarreaue6baec02017-07-27 11:45:11 +02003636/* config parser for global "tune.h2.initial-window-size" */
3637static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3638 struct proxy *defpx, const char *file, int line,
3639 char **err)
3640{
3641 if (too_many_args(1, args, err, NULL))
3642 return -1;
3643
3644 h2_settings_initial_window_size = atoi(args[1]);
3645 if (h2_settings_initial_window_size < 0) {
3646 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3647 return -1;
3648 }
3649 return 0;
3650}
3651
Willy Tarreau5242ef82017-07-27 11:47:28 +02003652/* config parser for global "tune.h2.max-concurrent-streams" */
3653static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3654 struct proxy *defpx, const char *file, int line,
3655 char **err)
3656{
3657 if (too_many_args(1, args, err, NULL))
3658 return -1;
3659
3660 h2_settings_max_concurrent_streams = atoi(args[1]);
3661 if (h2_settings_max_concurrent_streams < 0) {
3662 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3663 return -1;
3664 }
3665 return 0;
3666}
3667
Willy Tarreau62f52692017-10-08 23:01:42 +02003668
3669/****************************************/
3670/* MUX initialization and instanciation */
3671/***************************************/
3672
3673/* The mux operations */
3674const struct mux_ops h2_ops = {
3675 .init = h2_init,
3676 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003677 .wake = h2_wake,
3678 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003679 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003680 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003681 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003682 .attach = h2_attach,
3683 .detach = h2_detach,
3684 .shutr = h2_shutr,
3685 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003686 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003687 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003688 .name = "H2",
3689};
3690
Christopher Faulet32f61c02018-04-10 14:33:41 +02003691/* PROTO selection : this mux registers PROTO token "h2" */
3692static struct mux_proto_list mux_proto_h2 =
3693 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003694
3695/* config keyword parsers */
3696static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003697 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003698 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003699 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003700 { 0, NULL, NULL }
3701}};
3702
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003703static void __h2_deinit(void)
3704{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003705 pool_destroy(pool_head_h2s);
3706 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003707}
3708
Willy Tarreau62f52692017-10-08 23:01:42 +02003709__attribute__((constructor))
3710static void __h2_init(void)
3711{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003712 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003713 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003714 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003715 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3716 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003717}