blob: 5255ca0a1cc1966b20fc24aaf6df7cbfb4d339be [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 Tarreaub3fb56d2018-10-03 13:56:38 +020063#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020064
65
Willy Tarreau5ab6b572017-09-22 08:05:00 +020066/* H2 connection state, in h2c->st0 */
67enum h2_cs {
68 H2_CS_PREFACE, // init done, waiting for connection preface
69 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
70 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
71 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010072 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
73 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020074 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
75 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
76 H2_CS_ENTRIES // must be last
77} __attribute__((packed));
78
79/* H2 connection descriptor */
80struct h2c {
81 struct connection *conn;
82
83 enum h2_cs st0; /* mux state */
84 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
85
86 /* 16 bit hole here */
87 uint32_t flags; /* connection flags: H2_CF_* */
88 int32_t max_id; /* highest ID known on this connection, <0 before preface */
89 uint32_t rcvd_c; /* newly received data to ACK for the connection */
90 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
91
92 /* states for the demux direction */
93 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020094 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095
96 int32_t dsi; /* demux stream ID (<0 = idle) */
97 int32_t dfl; /* demux frame length (if dsi >= 0) */
98 int8_t dft; /* demux frame type (if dsi >= 0) */
99 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100100 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
101 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
103
104 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200105 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200106 int32_t msi; /* mux stream ID (<0 = idle) */
107 int32_t mfl; /* mux frame length (if dsi >= 0) */
108 int8_t mft; /* mux frame type (if dsi >= 0) */
109 int8_t mff; /* mux frame flags (if dsi >= 0) */
110 /* 16 bit hole here */
111 int32_t miw; /* mux initial window size for all new streams */
112 int32_t mws; /* mux window size. Can be negative. */
113 int32_t mfs; /* mux's max frame size */
114
Willy Tarreauea392822017-10-31 10:02:25 +0100115 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100116 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100117 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200118 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200119 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100120 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121 struct eb_root streams_by_id; /* all active streams by their ID */
122 struct list send_list; /* list of blocked streams requesting to send */
123 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100124 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200125 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126};
127
Willy Tarreau18312642017-10-11 07:57:07 +0200128/* H2 stream state, in h2s->st */
129enum h2_ss {
130 H2_SS_IDLE = 0, // idle
131 H2_SS_RLOC, // reserved(local)
132 H2_SS_RREM, // reserved(remote)
133 H2_SS_OPEN, // open
134 H2_SS_HREM, // half-closed(remote)
135 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200136 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200137 H2_SS_CLOSED, // closed
138 H2_SS_ENTRIES // must be last
139} __attribute__((packed));
140
141/* HTTP/2 stream flags (32 bit), in h2s->flags */
142#define H2_SF_NONE 0x00000000
143#define H2_SF_ES_RCVD 0x00000001
144#define H2_SF_ES_SENT 0x00000002
145
146#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
147#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
148
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200149/* stream flags indicating the reason the stream is blocked */
150#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
151#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
152#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
153#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
154#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
155
Willy Tarreau454f9052017-10-26 19:40:35 +0200156/* stream flags indicating how data is supposed to be sent */
157#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
158#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
159
160/* step we're currently in when sending chunks. This is needed because we may
161 * have to transfer chunks as large as a full buffer so there's no room left
162 * for size nor crlf around.
163 */
164#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
165#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
166#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
167
168#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
169
Willy Tarreau67434202017-11-06 20:20:51 +0100170#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100171#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100172
Willy Tarreau18312642017-10-11 07:57:07 +0200173/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
174 * it is being processed in the internal HTTP representation (H1 for now).
175 */
176struct h2s {
177 struct conn_stream *cs;
178 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200179 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200180 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200181 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;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200186 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200187 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200188 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
189 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
190 struct wait_event *send_wait; /* The streeam is waiting for flow control */
191 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200192};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200193
Willy Tarreauc6405142017-09-21 20:23:50 +0200194/* descriptor for an h2 frame header */
195struct h2_fh {
196 uint32_t len; /* length, host order, 24 bits */
197 uint32_t sid; /* stream id, host order, 31 bits */
198 uint8_t ft; /* frame type */
199 uint8_t ff; /* frame flags */
200};
201
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200202/* a few settings from the global section */
203static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200204static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200205static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200206
Willy Tarreau2a856182017-05-16 15:20:39 +0200207/* a dmumy closed stream */
208static const struct h2s *h2_closed_stream = &(const struct h2s){
209 .cs = NULL,
210 .h2c = NULL,
211 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100212 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100213 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200214 .id = 0,
215};
216
217/* and a dummy idle stream for use with any unannounced stream */
218static const struct h2s *h2_idle_stream = &(const struct h2s){
219 .cs = NULL,
220 .h2c = NULL,
221 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100222 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200223 .id = 0,
224};
225
Olivier Houchard9f6af332018-05-25 14:04:04 +0200226static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200227static int h2_send(struct h2c *h2c);
228static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200229static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200230static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100231static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100232static int h2_frt_decode_headers(struct h2s *h2s);
233static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200234static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200235
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200236/*****************************************************/
237/* functions below are for dynamic buffer management */
238/*****************************************************/
239
Willy Tarreau315d8072017-12-10 22:17:57 +0100240/* indicates whether or not the we may call the h2_recv() function to attempt
241 * to receive data into the buffer and/or demux pending data. The condition is
242 * a bit complex due to some API limits for now. The rules are the following :
243 * - if an error or a shutdown was detected on the connection and the buffer
244 * is empty, we must not attempt to receive
245 * - if the demux buf failed to be allocated, we must not try to receive and
246 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100247 * - if no flag indicates a blocking condition, we may attempt to receive,
248 * regardless of whether the demux buffer is full or not, so that only
249 * de demux part decides whether or not to block. This is needed because
250 * the connection API indeed prevents us from re-enabling receipt that is
251 * already enabled in a polled state, so we must always immediately stop
252 * as soon as the demux can't proceed so as never to hit an end of read
253 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100254 * - otherwise must may not attempt
255 */
256static inline int h2_recv_allowed(const struct h2c *h2c)
257{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200258 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100259 (h2c->st0 >= H2_CS_ERROR ||
260 h2c->conn->flags & CO_FL_ERROR ||
261 conn_xprt_read0_pending(h2c->conn)))
262 return 0;
263
264 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100265 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100266 return 1;
267
268 return 0;
269}
270
Willy Tarreauf2101912018-07-19 10:11:38 +0200271/* returns true if the connection has too many conn_streams attached */
272static inline int h2_has_too_many_cs(const struct h2c *h2c)
273{
274 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
275}
276
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
278 * flags are used to figure what buffer was requested. It returns 1 if the
279 * allocation succeeds, in which case the connection is woken up, or 0 if it's
280 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283{
284 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100285 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200286
Willy Tarreau44e973f2018-03-01 17:49:30 +0100287 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200288 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200289 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200290 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291 return 1;
292 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200293
Willy Tarreau44e973f2018-03-01 17:49:30 +0100294 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
295 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200296
297 if (h2c->flags & H2_CF_DEM_MROOM) {
298 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200299 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200300 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301 }
Willy Tarreau14398122017-09-22 14:26:04 +0200302 return 1;
303 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100304
305 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
306 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200307 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100308 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200309 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200310 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100311 return 1;
312 }
313
Willy Tarreau14398122017-09-22 14:26:04 +0200314 return 0;
315}
316
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200317static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200318{
319 struct buffer *buf = NULL;
320
Willy Tarreau44e973f2018-03-01 17:49:30 +0100321 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
322 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
323 h2c->buf_wait.target = h2c;
324 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100325 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200328 __conn_xprt_stop_recv(h2c->conn);
329 }
330 return buf;
331}
332
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200333static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200334{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100336 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200337 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200338 }
339}
340
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200341
Willy Tarreau62f52692017-10-08 23:01:42 +0200342/*****************************************************************/
343/* functions below are dedicated to the mux setup and management */
344/*****************************************************************/
345
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200346/* Initialize the mux once it's attached. For outgoing connections, the context
347 * is already initialized before installing the mux, so we detect incoming
348 * connections from the fact that the context is still NULL. Returns < 0 on
349 * error.
350 */
351static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200352{
353 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100354 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200355
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200356 /* we don't support outgoing connections for now */
357 if (conn->mux_ctx)
358 goto fail_no_h2c;
359
Willy Tarreaubafbe012017-11-24 17:34:44 +0100360 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200361 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200362 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363
Willy Tarreau0b37d652018-10-03 10:33:02 +0200364 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
365 if (tick_isset(prx->timeout.clientfin))
366 h2c->shut_timeout = prx->timeout.clientfin;
Willy Tarreau3f133572017-10-31 19:21:06 +0100367
Willy Tarreau0b37d652018-10-03 10:33:02 +0200368 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100369 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100370 if (tick_isset(h2c->timeout)) {
371 t = task_new(tid_bit);
372 if (!t)
373 goto fail;
374
375 h2c->task = t;
376 t->process = h2_timeout_task;
377 t->context = h2c;
378 t->expire = tick_add(now_ms, h2c->timeout);
379 }
Willy Tarreauea392822017-10-31 10:02:25 +0100380
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200381 h2c->wait_event.task = tasklet_new();
382 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200383 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200384 h2c->wait_event.task->process = h2_io_cb;
385 h2c->wait_event.task->context = h2c;
386 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200387
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
389 if (!h2c->ddht)
390 goto fail;
391
392 /* Initialise the context. */
393 h2c->st0 = H2_CS_PREFACE;
394 h2c->conn = conn;
395 h2c->max_id = -1;
396 h2c->errcode = H2_ERR_NO_ERROR;
397 h2c->flags = H2_CF_NONE;
398 h2c->rcvd_c = 0;
399 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100400 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200401 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200403 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->dsi = -1;
405 h2c->msi = -1;
406 h2c->last_sid = -1;
407
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200408 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 h2c->miw = 65535; /* mux initial window size */
410 h2c->mws = 65535; /* mux window size */
411 h2c->mfs = 16384; /* initial max frame size */
412 h2c->streams_by_id = EB_ROOT_UNIQUE;
413 LIST_INIT(&h2c->send_list);
414 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100415 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 conn->mux_ctx = h2c;
417
Willy Tarreau3f133572017-10-31 19:21:06 +0100418 if (t)
419 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100420
Willy Tarreau0f383582018-10-03 14:22:21 +0200421 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200422 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200423 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200424 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100425 if (t)
426 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200427 if (h2c->wait_event.task)
428 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100429 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200430 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 return -1;
432}
433
Willy Tarreau2373acc2017-10-12 17:35:14 +0200434/* returns the stream associated with id <id> or NULL if not found */
435static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
436{
437 struct eb32_node *node;
438
Willy Tarreau2a856182017-05-16 15:20:39 +0200439 if (id > h2c->max_id)
440 return (struct h2s *)h2_idle_stream;
441
Willy Tarreau2373acc2017-10-12 17:35:14 +0200442 node = eb32_lookup(&h2c->streams_by_id, id);
443 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200444 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200445
446 return container_of(node, struct h2s, by_id);
447}
448
Willy Tarreau62f52692017-10-08 23:01:42 +0200449/* release function for a connection. This one should be called to free all
450 * resources allocated to the mux.
451 */
452static void h2_release(struct connection *conn)
453{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200454 struct h2c *h2c = conn->mux_ctx;
455
456 LIST_DEL(&conn->list);
457
458 if (h2c) {
459 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200460
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100462 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100463 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200464
Willy Tarreau44e973f2018-03-01 17:49:30 +0100465 h2_release_buf(h2c, &h2c->dbuf);
466 h2_release_buf(h2c, &h2c->mbuf);
467
Willy Tarreauea392822017-10-31 10:02:25 +0100468 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200469 h2c->task->context = NULL;
470 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100471 h2c->task = NULL;
472 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200473 if (h2c->wait_event.task)
474 tasklet_free(h2c->wait_event.task);
475 if (h2c->wait_event.wait_reason != 0)
476 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
477 &h2c->wait_event);
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{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200525 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200526 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);
637 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200638 if (b_size(&h2s->rxbuf)) {
639 b_free(&h2s->rxbuf);
640 offer_buffers(NULL, tasks_run_queue);
641 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200642 if (h2s->send_wait != NULL)
643 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
644 if (h2s->recv_wait != NULL)
645 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
646 /* There's no need to explicitely call unsubscribe here, the only
647 * reference left would be in the h2c send_list/fctl_list, and if
648 * we're in it, we're getting out anyway
649 */
650 LIST_DEL(&h2s->list);
651 LIST_INIT(&h2s->list);
652 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100653 pool_free(pool_head_h2s, h2s);
654}
655
Willy Tarreaua8e49542018-10-03 18:53:55 +0200656/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
657 * stream tree. In case of error, nothing is added and NULL is returned. The
658 * causes of errors can be any failed memory allocation. The caller is
659 * responsible for checking if the connection may support an extra stream
660 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200661 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200662static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200663{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200664 struct h2s *h2s;
665
Willy Tarreaubafbe012017-11-24 17:34:44 +0100666 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200667 if (!h2s)
668 goto out;
669
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200670 h2s->wait_event.task = tasklet_new();
671 if (!h2s->wait_event.task) {
672 pool_free(pool_head_h2s, h2s);
673 goto out;
674 }
675 h2s->send_wait = NULL;
676 h2s->recv_wait = NULL;
677 h2s->wait_event.task->process = h2_deferred_shut;
678 h2s->wait_event.task->context = h2s;
679 h2s->wait_event.handle = NULL;
680 h2s->wait_event.wait_reason = 0;
681 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200682 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200683 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200684 h2s->mws = h2c->miw;
685 h2s->flags = H2_SF_NONE;
686 h2s->errcode = H2_ERR_NO_ERROR;
687 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200688 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200689 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200690 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200691 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200692 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200693 h2s->by_id.key = h2s->id = id;
694 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200695
696 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100697 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200698
699 return h2s;
700
701 out_free_h2s:
702 pool_free(pool_head_h2s, h2s);
703 out:
704 return NULL;
705}
706
707/* creates a new stream <id> on the h2c connection and returns it, or NULL in
708 * case of memory allocation error.
709 */
710static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
711{
712 struct session *sess = h2c->conn->owner;
713 struct conn_stream *cs;
714 struct h2s *h2s;
715
716 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
717 goto out;
718
719 h2s = h2s_new(h2c, id);
720 if (!h2s)
721 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200722
723 cs = cs_new(h2c->conn);
724 if (!cs)
725 goto out_close;
726
727 h2s->cs = cs;
728 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200729 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730
731 if (stream_create_from_cs(cs) < 0)
732 goto out_free_cs;
733
Willy Tarreau590a0512018-09-05 11:56:48 +0200734 /* We want the accept date presented to the next stream to be the one
735 * we have now, the handshake time to be null (since the next stream
736 * is not delayed by a handshake), and the idle time to count since
737 * right now.
738 */
739 sess->accept_date = date;
740 sess->tv_accept = now;
741 sess->t_handshake = 0;
742
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200743 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200744 if (h2_has_too_many_cs(h2c))
745 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200746 return h2s;
747
748 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200749 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200750 cs_free(cs);
751 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200752 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200753 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200754 sess_log(sess);
755 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200756}
757
Willy Tarreaube5b7152017-09-25 16:25:39 +0200758/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
759 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
760 * the various settings codes.
761 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200762static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200763{
764 struct buffer *res;
765 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200766 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767 int ret;
768
769 if (h2c_mux_busy(h2c, NULL)) {
770 h2c->flags |= H2_CF_DEM_MBUSY;
771 return 0;
772 }
773
Willy Tarreau44e973f2018-03-01 17:49:30 +0100774 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200775 if (!res) {
776 h2c->flags |= H2_CF_MUX_MALLOC;
777 h2c->flags |= H2_CF_DEM_MROOM;
778 return 0;
779 }
780
781 chunk_init(&buf, buf_data, sizeof(buf_data));
782 chunk_memcpy(&buf,
783 "\x00\x00\x00" /* length : 0 for now */
784 "\x04\x00" /* type : 4 (settings), flags : 0 */
785 "\x00\x00\x00\x00", /* stream ID : 0 */
786 9);
787
788 if (h2_settings_header_table_size != 4096) {
789 char str[6] = "\x00\x01"; /* header_table_size */
790
791 write_n32(str + 2, h2_settings_header_table_size);
792 chunk_memcat(&buf, str, 6);
793 }
794
795 if (h2_settings_initial_window_size != 65535) {
796 char str[6] = "\x00\x04"; /* initial_window_size */
797
798 write_n32(str + 2, h2_settings_initial_window_size);
799 chunk_memcat(&buf, str, 6);
800 }
801
802 if (h2_settings_max_concurrent_streams != 0) {
803 char str[6] = "\x00\x03"; /* max_concurrent_streams */
804
805 /* Note: 0 means "unlimited" for haproxy's config but not for
806 * the protocol, so never send this value!
807 */
808 write_n32(str + 2, h2_settings_max_concurrent_streams);
809 chunk_memcat(&buf, str, 6);
810 }
811
812 if (global.tune.bufsize != 16384) {
813 char str[6] = "\x00\x05"; /* max_frame_size */
814
815 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
816 * match bufsize - rewrite size, but at the moment it seems
817 * that clients don't take care of it.
818 */
819 write_n32(str + 2, global.tune.bufsize);
820 chunk_memcat(&buf, str, 6);
821 }
822
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200823 h2_set_frame_size(buf.area, buf.data - 9);
824 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200825 if (unlikely(ret <= 0)) {
826 if (!ret) {
827 h2c->flags |= H2_CF_MUX_MFULL;
828 h2c->flags |= H2_CF_DEM_MROOM;
829 return 0;
830 }
831 else {
832 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
833 return 0;
834 }
835 }
836 return ret;
837}
838
Willy Tarreau52eed752017-09-22 15:05:09 +0200839/* Try to receive a connection preface, then upon success try to send our
840 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
841 * missing data. It may return an error in h2c.
842 */
843static int h2c_frt_recv_preface(struct h2c *h2c)
844{
845 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200846 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200847
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200848 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200849
850 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200851 if (ret1 < 0)
852 sess_log(h2c->conn->owner);
853
Willy Tarreau52eed752017-09-22 15:05:09 +0200854 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
855 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
856 return 0;
857 }
858
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200859 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200860 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200861 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200862
Willy Tarreaube5b7152017-09-25 16:25:39 +0200863 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200864}
865
Willy Tarreau081d4722017-05-16 21:51:05 +0200866/* try to send a GOAWAY frame on the connection to report an error or a graceful
867 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
868 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
869 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
870 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
871 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
872 * on unrecoverable failure. It will not attempt to send one again in this last
873 * case so that it is safe to use h2c_error() to report such errors.
874 */
875static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
876{
877 struct buffer *res;
878 char str[17];
879 int ret;
880
881 if (h2c->flags & H2_CF_GOAWAY_FAILED)
882 return 1; // claim that it worked
883
884 if (h2c_mux_busy(h2c, h2s)) {
885 if (h2s)
886 h2s->flags |= H2_SF_BLK_MBUSY;
887 else
888 h2c->flags |= H2_CF_DEM_MBUSY;
889 return 0;
890 }
891
Willy Tarreau44e973f2018-03-01 17:49:30 +0100892 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200893 if (!res) {
894 h2c->flags |= H2_CF_MUX_MALLOC;
895 if (h2s)
896 h2s->flags |= H2_SF_BLK_MROOM;
897 else
898 h2c->flags |= H2_CF_DEM_MROOM;
899 return 0;
900 }
901
902 /* len: 8, type: 7, flags: none, sid: 0 */
903 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
904
905 if (h2c->last_sid < 0)
906 h2c->last_sid = h2c->max_id;
907
908 write_n32(str + 9, h2c->last_sid);
909 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200910 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200911 if (unlikely(ret <= 0)) {
912 if (!ret) {
913 h2c->flags |= H2_CF_MUX_MFULL;
914 if (h2s)
915 h2s->flags |= H2_SF_BLK_MROOM;
916 else
917 h2c->flags |= H2_CF_DEM_MROOM;
918 return 0;
919 }
920 else {
921 /* we cannot report this error using GOAWAY, so we mark
922 * it and claim a success.
923 */
924 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
925 h2c->flags |= H2_CF_GOAWAY_FAILED;
926 return 1;
927 }
928 }
929 h2c->flags |= H2_CF_GOAWAY_SENT;
930 return ret;
931}
932
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100933/* Try to send an RST_STREAM frame on the connection for the indicated stream
934 * during mux operations. This stream must be valid and cannot be closed
935 * already. h2s->id will be used for the stream ID and h2s->errcode will be
936 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
937 * not yet.
938 *
939 * Returns > 0 on success or zero if nothing was done. In case of lack of room
940 * to write the message, it subscribes the stream to future notifications.
941 */
942static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
943{
944 struct buffer *res;
945 char str[13];
946 int ret;
947
948 if (!h2s || h2s->st == H2_SS_CLOSED)
949 return 1;
950
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100951 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
952 * RST_STREAM in response to a RST_STREAM frame.
953 */
954 if (h2c->dft == H2_FT_RST_STREAM) {
955 ret = 1;
956 goto ignore;
957 }
958
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100959 if (h2c_mux_busy(h2c, h2s)) {
960 h2s->flags |= H2_SF_BLK_MBUSY;
961 return 0;
962 }
963
Willy Tarreau44e973f2018-03-01 17:49:30 +0100964 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100965 if (!res) {
966 h2c->flags |= H2_CF_MUX_MALLOC;
967 h2s->flags |= H2_SF_BLK_MROOM;
968 return 0;
969 }
970
971 /* len: 4, type: 3, flags: none */
972 memcpy(str, "\x00\x00\x04\x03\x00", 5);
973 write_n32(str + 5, h2s->id);
974 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200975 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100976
977 if (unlikely(ret <= 0)) {
978 if (!ret) {
979 h2c->flags |= H2_CF_MUX_MFULL;
980 h2s->flags |= H2_SF_BLK_MROOM;
981 return 0;
982 }
983 else {
984 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
985 return 0;
986 }
987 }
988
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100989 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100990 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100991 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100992 return ret;
993}
994
995/* Try to send an RST_STREAM frame on the connection for the stream being
996 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
997 * error code unless the stream's state already is IDLE or CLOSED in which
998 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
999 * it was not yet.
1000 *
1001 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1002 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001003 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001004 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001005 */
1006static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1007{
1008 struct buffer *res;
1009 char str[13];
1010 int ret;
1011
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001012 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1013 * RST_STREAM in response to a RST_STREAM frame.
1014 */
1015 if (h2c->dft == H2_FT_RST_STREAM) {
1016 ret = 1;
1017 goto ignore;
1018 }
1019
Willy Tarreau27a84c92017-10-17 08:10:17 +02001020 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001021 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001022 return 0;
1023 }
1024
Willy Tarreau44e973f2018-03-01 17:49:30 +01001025 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001026 if (!res) {
1027 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001028 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001029 return 0;
1030 }
1031
1032 /* len: 4, type: 3, flags: none */
1033 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001034
Willy Tarreau27a84c92017-10-17 08:10:17 +02001035 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001036 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001037 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001038
Willy Tarreau27a84c92017-10-17 08:10:17 +02001039 if (unlikely(ret <= 0)) {
1040 if (!ret) {
1041 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001042 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001043 return 0;
1044 }
1045 else {
1046 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1047 return 0;
1048 }
1049 }
1050
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001051 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001052 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001053 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001054 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001055 }
1056
Willy Tarreau27a84c92017-10-17 08:10:17 +02001057 return ret;
1058}
1059
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001060/* try to send an empty DATA frame with the ES flag set to notify about the
1061 * end of stream and match a shutdown(write). If an ES was already sent as
1062 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1063 * on success or zero if nothing was done. In case of lack of room to write the
1064 * message, it subscribes the requesting stream to future notifications.
1065 */
1066static int h2_send_empty_data_es(struct h2s *h2s)
1067{
1068 struct h2c *h2c = h2s->h2c;
1069 struct buffer *res;
1070 char str[9];
1071 int ret;
1072
Willy Tarreau721c9742017-11-07 11:05:42 +01001073 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001074 return 1;
1075
1076 if (h2c_mux_busy(h2c, h2s)) {
1077 h2s->flags |= H2_SF_BLK_MBUSY;
1078 return 0;
1079 }
1080
Willy Tarreau44e973f2018-03-01 17:49:30 +01001081 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001082 if (!res) {
1083 h2c->flags |= H2_CF_MUX_MALLOC;
1084 h2s->flags |= H2_SF_BLK_MROOM;
1085 return 0;
1086 }
1087
1088 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1089 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1090 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001091 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001092 if (likely(ret > 0)) {
1093 h2s->flags |= H2_SF_ES_SENT;
1094 }
1095 else if (!ret) {
1096 h2c->flags |= H2_CF_MUX_MFULL;
1097 h2s->flags |= H2_SF_BLK_MROOM;
1098 return 0;
1099 }
1100 else {
1101 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1102 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001103 }
1104 return ret;
1105}
1106
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001107/* wake the streams attached to the connection, whose id is greater than <last>,
1108 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001109 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1110 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001111 */
1112static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1113{
1114 struct eb32_node *node;
1115 struct h2s *h2s;
1116
1117 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1118 flags |= CS_FL_ERROR;
1119
1120 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001121 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001122
1123 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1124 while (node) {
1125 h2s = container_of(node, struct h2s, by_id);
1126 if (h2s->id <= last)
1127 break;
1128 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001129
1130 if (!h2s->cs) {
1131 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001132 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001133 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001134 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001135
1136 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001137 if (h2s->recv_wait) {
1138 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001139 sw->wait_reason &= ~SUB_CAN_RECV;
1140 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001141 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001142 } else if (h2s->cs->data_cb->wake != NULL)
1143 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001144
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001145 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1146 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001147 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001148 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001149 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001150 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001151 }
1152}
1153
Willy Tarreau3421aba2017-07-27 15:41:03 +02001154/* Increase all streams' outgoing window size by the difference passed in
1155 * argument. This is needed upon receipt of the settings frame if the initial
1156 * window size is different. The difference may be negative and the resulting
1157 * window size as well, for the time it takes to receive some window updates.
1158 */
1159static void h2c_update_all_ws(struct h2c *h2c, int diff)
1160{
1161 struct h2s *h2s;
1162 struct eb32_node *node;
1163
1164 if (!diff)
1165 return;
1166
1167 node = eb32_first(&h2c->streams_by_id);
1168 while (node) {
1169 h2s = container_of(node, struct h2s, by_id);
1170 h2s->mws += diff;
1171 node = eb32_next(node);
1172 }
1173}
1174
1175/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1176 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1177 * return an error in h2c. Described in RFC7540#6.5.
1178 */
1179static int h2c_handle_settings(struct h2c *h2c)
1180{
1181 unsigned int offset;
1182 int error;
1183
1184 if (h2c->dff & H2_F_SETTINGS_ACK) {
1185 if (h2c->dfl) {
1186 error = H2_ERR_FRAME_SIZE_ERROR;
1187 goto fail;
1188 }
1189 return 1;
1190 }
1191
1192 if (h2c->dsi != 0) {
1193 error = H2_ERR_PROTOCOL_ERROR;
1194 goto fail;
1195 }
1196
1197 if (h2c->dfl % 6) {
1198 error = H2_ERR_FRAME_SIZE_ERROR;
1199 goto fail;
1200 }
1201
1202 /* that's the limit we can process */
1203 if (h2c->dfl > global.tune.bufsize) {
1204 error = H2_ERR_FRAME_SIZE_ERROR;
1205 goto fail;
1206 }
1207
1208 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001209 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001210 return 0;
1211
1212 /* parse the frame */
1213 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001214 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1215 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001216
1217 switch (type) {
1218 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1219 /* we need to update all existing streams with the
1220 * difference from the previous iws.
1221 */
1222 if (arg < 0) { // RFC7540#6.5.2
1223 error = H2_ERR_FLOW_CONTROL_ERROR;
1224 goto fail;
1225 }
1226 h2c_update_all_ws(h2c, arg - h2c->miw);
1227 h2c->miw = arg;
1228 break;
1229 case H2_SETTINGS_MAX_FRAME_SIZE:
1230 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1231 error = H2_ERR_PROTOCOL_ERROR;
1232 goto fail;
1233 }
1234 h2c->mfs = arg;
1235 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001236 case H2_SETTINGS_ENABLE_PUSH:
1237 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1238 error = H2_ERR_PROTOCOL_ERROR;
1239 goto fail;
1240 }
1241 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001242 }
1243 }
1244
1245 /* need to ACK this frame now */
1246 h2c->st0 = H2_CS_FRAME_A;
1247 return 1;
1248 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001249 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001250 h2c_error(h2c, error);
1251 return 0;
1252}
1253
1254/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1255 * success or one of the h2_status values.
1256 */
1257static int h2c_ack_settings(struct h2c *h2c)
1258{
1259 struct buffer *res;
1260 char str[9];
1261 int ret = -1;
1262
1263 if (h2c_mux_busy(h2c, NULL)) {
1264 h2c->flags |= H2_CF_DEM_MBUSY;
1265 return 0;
1266 }
1267
Willy Tarreau44e973f2018-03-01 17:49:30 +01001268 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001269 if (!res) {
1270 h2c->flags |= H2_CF_MUX_MALLOC;
1271 h2c->flags |= H2_CF_DEM_MROOM;
1272 return 0;
1273 }
1274
1275 memcpy(str,
1276 "\x00\x00\x00" /* length : 0 (no data) */
1277 "\x04" "\x01" /* type : 4, flags : ACK */
1278 "\x00\x00\x00\x00" /* stream ID */, 9);
1279
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001280 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001281 if (unlikely(ret <= 0)) {
1282 if (!ret) {
1283 h2c->flags |= H2_CF_MUX_MFULL;
1284 h2c->flags |= H2_CF_DEM_MROOM;
1285 return 0;
1286 }
1287 else {
1288 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1289 return 0;
1290 }
1291 }
1292 return ret;
1293}
1294
Willy Tarreaucf68c782017-10-10 17:11:41 +02001295/* processes a PING frame and schedules an ACK if needed. The caller must pass
1296 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1297 * missing data. It may return an error in h2c.
1298 */
1299static int h2c_handle_ping(struct h2c *h2c)
1300{
1301 /* frame length must be exactly 8 */
1302 if (h2c->dfl != 8) {
1303 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1304 return 0;
1305 }
1306
1307 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001308 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001309 h2c->st0 = H2_CS_FRAME_A;
1310 return 1;
1311}
1312
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001313/* Try to send a window update for stream id <sid> and value <increment>.
1314 * Returns > 0 on success or zero on missing room or failure. It may return an
1315 * error in h2c.
1316 */
1317static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1318{
1319 struct buffer *res;
1320 char str[13];
1321 int ret = -1;
1322
1323 if (h2c_mux_busy(h2c, NULL)) {
1324 h2c->flags |= H2_CF_DEM_MBUSY;
1325 return 0;
1326 }
1327
Willy Tarreau44e973f2018-03-01 17:49:30 +01001328 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001329 if (!res) {
1330 h2c->flags |= H2_CF_MUX_MALLOC;
1331 h2c->flags |= H2_CF_DEM_MROOM;
1332 return 0;
1333 }
1334
1335 /* length: 4, type: 8, flags: none */
1336 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1337 write_n32(str + 5, sid);
1338 write_n32(str + 9, increment);
1339
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001340 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001341
1342 if (unlikely(ret <= 0)) {
1343 if (!ret) {
1344 h2c->flags |= H2_CF_MUX_MFULL;
1345 h2c->flags |= H2_CF_DEM_MROOM;
1346 return 0;
1347 }
1348 else {
1349 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1350 return 0;
1351 }
1352 }
1353 return ret;
1354}
1355
1356/* try to send pending window update for the connection. It's safe to call it
1357 * with no pending updates. Returns > 0 on success or zero on missing room or
1358 * failure. It may return an error in h2c.
1359 */
1360static int h2c_send_conn_wu(struct h2c *h2c)
1361{
1362 int ret = 1;
1363
1364 if (h2c->rcvd_c <= 0)
1365 return 1;
1366
1367 /* send WU for the connection */
1368 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1369 if (ret > 0)
1370 h2c->rcvd_c = 0;
1371
1372 return ret;
1373}
1374
1375/* try to send pending window update for the current dmux stream. It's safe to
1376 * call it with no pending updates. Returns > 0 on success or zero on missing
1377 * room or failure. It may return an error in h2c.
1378 */
1379static int h2c_send_strm_wu(struct h2c *h2c)
1380{
1381 int ret = 1;
1382
1383 if (h2c->rcvd_s <= 0)
1384 return 1;
1385
1386 /* send WU for the stream */
1387 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1388 if (ret > 0)
1389 h2c->rcvd_s = 0;
1390
1391 return ret;
1392}
1393
Willy Tarreaucf68c782017-10-10 17:11:41 +02001394/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1395 * success, 0 on missing data or one of the h2_status values.
1396 */
1397static int h2c_ack_ping(struct h2c *h2c)
1398{
1399 struct buffer *res;
1400 char str[17];
1401 int ret = -1;
1402
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001403 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001404 return 0;
1405
1406 if (h2c_mux_busy(h2c, NULL)) {
1407 h2c->flags |= H2_CF_DEM_MBUSY;
1408 return 0;
1409 }
1410
Willy Tarreau44e973f2018-03-01 17:49:30 +01001411 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001412 if (!res) {
1413 h2c->flags |= H2_CF_MUX_MALLOC;
1414 h2c->flags |= H2_CF_DEM_MROOM;
1415 return 0;
1416 }
1417
1418 memcpy(str,
1419 "\x00\x00\x08" /* length : 8 (same payload) */
1420 "\x06" "\x01" /* type : 6, flags : ACK */
1421 "\x00\x00\x00\x00" /* stream ID */, 9);
1422
1423 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001424 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001425
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001426 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001427 if (unlikely(ret <= 0)) {
1428 if (!ret) {
1429 h2c->flags |= H2_CF_MUX_MFULL;
1430 h2c->flags |= H2_CF_DEM_MROOM;
1431 return 0;
1432 }
1433 else {
1434 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1435 return 0;
1436 }
1437 }
1438 return ret;
1439}
1440
Willy Tarreau26f95952017-07-27 17:18:30 +02001441/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1442 * Returns > 0 on success or zero on missing data. It may return an error in
1443 * h2c or h2s. Described in RFC7540#6.9.
1444 */
1445static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1446{
1447 int32_t inc;
1448 int error;
1449
1450 if (h2c->dfl != 4) {
1451 error = H2_ERR_FRAME_SIZE_ERROR;
1452 goto conn_err;
1453 }
1454
1455 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001456 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001457 return 0;
1458
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001459 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001460
1461 if (h2c->dsi != 0) {
1462 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001463
1464 /* it's not an error to receive WU on a closed stream */
1465 if (h2s->st == H2_SS_CLOSED)
1466 return 1;
1467
1468 if (!inc) {
1469 error = H2_ERR_PROTOCOL_ERROR;
1470 goto strm_err;
1471 }
1472
1473 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1474 error = H2_ERR_FLOW_CONTROL_ERROR;
1475 goto strm_err;
1476 }
1477
1478 h2s->mws += inc;
1479 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1480 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001481 if (h2s->send_wait)
1482 LIST_ADDQ(&h2c->send_list, &h2s->list);
1483
Willy Tarreau26f95952017-07-27 17:18:30 +02001484 }
1485 }
1486 else {
1487 /* connection window update */
1488 if (!inc) {
1489 error = H2_ERR_PROTOCOL_ERROR;
1490 goto conn_err;
1491 }
1492
1493 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1494 error = H2_ERR_FLOW_CONTROL_ERROR;
1495 goto conn_err;
1496 }
1497
1498 h2c->mws += inc;
1499 }
1500
1501 return 1;
1502
1503 conn_err:
1504 h2c_error(h2c, error);
1505 return 0;
1506
1507 strm_err:
1508 if (h2s) {
1509 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001510 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001511 }
1512 else
1513 h2c_error(h2c, error);
1514 return 0;
1515}
1516
Willy Tarreaue96b0922017-10-30 00:28:29 +01001517/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1518 * the last ID. Returns > 0 on success or zero on missing data. It may return
1519 * an error in h2c. Described in RFC7540#6.8.
1520 */
1521static int h2c_handle_goaway(struct h2c *h2c)
1522{
1523 int error;
1524 int last;
1525
1526 if (h2c->dsi != 0) {
1527 error = H2_ERR_PROTOCOL_ERROR;
1528 goto conn_err;
1529 }
1530
1531 if (h2c->dfl < 8) {
1532 error = H2_ERR_FRAME_SIZE_ERROR;
1533 goto conn_err;
1534 }
1535
1536 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001537 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001538 return 0;
1539
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001540 last = h2_get_n32(&h2c->dbuf, 0);
1541 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001542 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001543 if (h2c->last_sid < 0)
1544 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001545 return 1;
1546
1547 conn_err:
1548 h2c_error(h2c, error);
1549 return 0;
1550}
1551
Willy Tarreau92153fc2017-12-03 19:46:19 +01001552/* processes a PRIORITY frame, and either skips it or rejects if it is
1553 * invalid. Returns > 0 on success or zero on missing data. It may return
1554 * an error in h2c. Described in RFC7540#6.3.
1555 */
1556static int h2c_handle_priority(struct h2c *h2c)
1557{
1558 int error;
1559
1560 if (h2c->dsi == 0) {
1561 error = H2_ERR_PROTOCOL_ERROR;
1562 goto conn_err;
1563 }
1564
1565 if (h2c->dfl != 5) {
1566 error = H2_ERR_FRAME_SIZE_ERROR;
1567 goto conn_err;
1568 }
1569
1570 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001571 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001572 return 0;
1573
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001574 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001575 /* 7540#5.3 : can't depend on itself */
1576 error = H2_ERR_PROTOCOL_ERROR;
1577 goto conn_err;
1578 }
1579 return 1;
1580
1581 conn_err:
1582 h2c_error(h2c, error);
1583 return 0;
1584}
1585
Willy Tarreaucd234e92017-08-18 10:59:39 +02001586/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1587 * Returns > 0 on success or zero on missing data. It may return an error in
1588 * h2c. Described in RFC7540#6.4.
1589 */
1590static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1591{
1592 int error;
1593
1594 if (h2c->dsi == 0) {
1595 error = H2_ERR_PROTOCOL_ERROR;
1596 goto conn_err;
1597 }
1598
Willy Tarreaucd234e92017-08-18 10:59:39 +02001599 if (h2c->dfl != 4) {
1600 error = H2_ERR_FRAME_SIZE_ERROR;
1601 goto conn_err;
1602 }
1603
1604 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001605 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001606 return 0;
1607
1608 /* late RST, already handled */
1609 if (h2s->st == H2_SS_CLOSED)
1610 return 1;
1611
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001612 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001613 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001614
1615 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001616 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001617 if (h2s->recv_wait) {
1618 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001619
1620 sw->wait_reason &= ~SUB_CAN_RECV;
1621 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001622 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001623 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001624 }
1625
1626 h2s->flags |= H2_SF_RST_RCVD;
1627 return 1;
1628
1629 conn_err:
1630 h2c_error(h2c, error);
1631 return 0;
1632}
1633
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001634/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1635 * It may return an error in h2c or h2s. The caller must consider that the
1636 * return value is the new h2s in case one was allocated (most common case).
1637 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001638 * errors here are reported as connection errors since it's impossible to
1639 * recover from such errors after the compression context has been altered.
1640 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001641static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001642{
1643 int error;
1644
1645 if (!h2c->dfl) {
1646 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001647 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001648 goto strm_err;
1649 }
1650
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001651 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001652 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001653
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001654 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001655 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001656
Willy Tarreauf2101912018-07-19 10:11:38 +02001657 if (h2c->flags & H2_CF_DEM_TOOMANY)
1658 return 0; // too many cs still present
1659
Willy Tarreau13278b42017-10-13 19:23:14 +02001660 /* now either the frame is complete or the buffer is complete */
1661 if (h2s->st != H2_SS_IDLE) {
1662 /* FIXME: stream already exists, this is only allowed for
1663 * trailers (not supported for now).
1664 */
1665 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001666 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001667 goto conn_err;
1668 }
1669 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1670 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1671 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001672 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001673 goto conn_err;
1674 }
1675
Willy Tarreau22de8d32018-09-05 19:55:58 +02001676 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001677 * positively from h2c_frt_stream_new(), the stream will report the error,
1678 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001679 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001680 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001681 if (!h2s) {
1682 error = H2_ERR_INTERNAL_ERROR;
1683 goto conn_err;
1684 }
1685
1686 h2s->st = H2_SS_OPEN;
1687 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1688 h2s->st = H2_SS_HREM;
1689 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001690 /* note: cs cannot be null for now (just created above) */
1691 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001692 }
1693
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001694 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001695 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001696
Willy Tarreau8f650c32017-11-21 19:36:21 +01001697 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001698 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001699
Willy Tarreau721c9742017-11-07 11:05:42 +01001700 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001701 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001702 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001703 }
1704 else {
1705 /* update the max stream ID if the request is being processed */
1706 if (h2s->id > h2c->max_id)
1707 h2c->max_id = h2s->id;
1708 }
1709
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001710 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001711
1712 conn_err:
1713 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001714 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001715
1716 strm_err:
1717 if (h2s) {
1718 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001719 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001720 }
1721 else
1722 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001723 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001724}
1725
Willy Tarreau454f9052017-10-26 19:40:35 +02001726/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1727 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1728 */
1729static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1730{
1731 int error;
1732
1733 /* note that empty DATA frames are perfectly valid and sometimes used
1734 * to signal an end of stream (with the ES flag).
1735 */
1736
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001737 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001738 return 0; // empty buffer
1739
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001740 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001741 return 0; // incomplete frame
1742
1743 /* now either the frame is complete or the buffer is complete */
1744
1745 if (!h2c->dsi) {
1746 /* RFC7540#6.1 */
1747 error = H2_ERR_PROTOCOL_ERROR;
1748 goto conn_err;
1749 }
1750
1751 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1752 /* RFC7540#6.1 */
1753 error = H2_ERR_STREAM_CLOSED;
1754 goto strm_err;
1755 }
1756
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001757 if (!h2_frt_transfer_data(h2s))
1758 return 0;
1759
Willy Tarreau454f9052017-10-26 19:40:35 +02001760 /* call the upper layers to process the frame, then let the upper layer
1761 * notify the stream about any change.
1762 */
1763 if (!h2s->cs) {
1764 error = H2_ERR_STREAM_CLOSED;
1765 goto strm_err;
1766 }
1767
Willy Tarreau8f650c32017-11-21 19:36:21 +01001768 if (h2c->st0 >= H2_CS_ERROR)
1769 return 0;
1770
Willy Tarreau721c9742017-11-07 11:05:42 +01001771 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001772 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001773 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001774 }
1775
1776 /* check for completion : the callee will change this to FRAME_A or
1777 * FRAME_H once done.
1778 */
1779 if (h2c->st0 == H2_CS_FRAME_P)
1780 return 0;
1781
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001782
1783 /* last frame */
1784 if (h2c->dff & H2_F_DATA_END_STREAM) {
1785 h2s->st = H2_SS_HREM;
1786 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001787 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001788 }
1789
Willy Tarreau454f9052017-10-26 19:40:35 +02001790 return 1;
1791
1792 conn_err:
1793 h2c_error(h2c, error);
1794 return 0;
1795
1796 strm_err:
1797 if (h2s) {
1798 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001799 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001800 }
1801 else
1802 h2c_error(h2c, error);
1803 return 0;
1804}
1805
Willy Tarreaubc933932017-10-09 16:21:43 +02001806/* process Rx frames to be demultiplexed */
1807static void h2_process_demux(struct h2c *h2c)
1808{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001809 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001810
Willy Tarreau081d4722017-05-16 21:51:05 +02001811 if (h2c->st0 >= H2_CS_ERROR)
1812 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001813
1814 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1815 if (h2c->st0 == H2_CS_PREFACE) {
1816 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1817 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001818 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001819 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001820 sess_log(h2c->conn->owner);
1821 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001822 goto fail;
1823 }
1824
1825 h2c->max_id = 0;
1826 h2c->st0 = H2_CS_SETTINGS1;
1827 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001828
1829 if (h2c->st0 == H2_CS_SETTINGS1) {
1830 struct h2_fh hdr;
1831
1832 /* ensure that what is pending is a valid SETTINGS frame
1833 * without an ACK.
1834 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001835 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001836 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001837 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001838 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001839 sess_log(h2c->conn->owner);
1840 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001841 goto fail;
1842 }
1843
1844 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1845 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1846 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1847 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001848 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001849 goto fail;
1850 }
1851
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001852 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001853 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1854 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1855 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001856 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001857 goto fail;
1858 }
1859
1860 /* that's OK, switch to FRAME_P to process it */
1861 h2c->dfl = hdr.len;
1862 h2c->dsi = hdr.sid;
1863 h2c->dft = hdr.ft;
1864 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001865 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001866 h2c->st0 = H2_CS_FRAME_P;
1867 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001868 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001869
1870 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001871 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001872 int ret = 0;
1873
1874 if (h2c->st0 >= H2_CS_ERROR)
1875 break;
1876
1877 if (h2c->st0 == H2_CS_FRAME_H) {
1878 struct h2_fh hdr;
1879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001880 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001881 break;
1882
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001883 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001884 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1885 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001886 if (!h2c->nb_streams) {
1887 /* only log if no other stream can report the error */
1888 sess_log(h2c->conn->owner);
1889 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001890 break;
1891 }
1892
1893 h2c->dfl = hdr.len;
1894 h2c->dsi = hdr.sid;
1895 h2c->dft = hdr.ft;
1896 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001897 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001898 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001899 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001900 }
1901
1902 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001903 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1904
Olivier Houchard638b7992018-08-16 15:41:52 +02001905 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001906 /* we may have to signal the upper layers */
1907 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001908 if (h2s->recv_wait) {
1909 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1910 tasklet_wakeup(h2s->recv_wait->task);
1911 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001913 if (h2c->st0 >= H2_CS_ERROR)
1914 goto strm_err;
1915
1916 if (h2s->st >= H2_SS_ERROR) {
1917 /* stream error : send RST_STREAM */
1918 h2c->st0 = H2_CS_FRAME_E;
1919 }
1920 }
1921 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001922
Willy Tarreaud7901432017-12-29 11:34:40 +01001923 if (h2c->st0 == H2_CS_FRAME_E)
1924 goto strm_err;
1925
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001926 if (h2s->st == H2_SS_IDLE &&
1927 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1928 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1929 * this state MUST be treated as a connection error
1930 */
1931 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1932 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001933 if (!h2c->nb_streams) {
1934 /* only log if no other stream can report the error */
1935 sess_log(h2c->conn->owner);
1936 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001937 break;
1938 }
1939
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001940 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1941 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1942 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1943 * this state MUST be treated as a stream error
1944 */
1945 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1946 goto strm_err;
1947 }
1948
Willy Tarreauab837502017-12-27 15:07:30 +01001949 /* Below the management of frames received in closed state is a
1950 * bit hackish because the spec makes strong differences between
1951 * streams closed by receiving RST, sending RST, and seeing ES
1952 * in both directions. In addition to this, the creation of a
1953 * new stream reusing the identifier of a closed one will be
1954 * detected here. Given that we cannot keep track of all closed
1955 * streams forever, we consider that unknown closed streams were
1956 * closed on RST received, which allows us to respond with an
1957 * RST without breaking the connection (eg: to abort a transfer).
1958 * Some frames have to be silently ignored as well.
1959 */
1960 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1961 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1962 /* #5.1.1: The identifier of a newly
1963 * established stream MUST be numerically
1964 * greater than all streams that the initiating
1965 * endpoint has opened or reserved. This
1966 * governs streams that are opened using a
1967 * HEADERS frame and streams that are reserved
1968 * using PUSH_PROMISE. An endpoint that
1969 * receives an unexpected stream identifier
1970 * MUST respond with a connection error.
1971 */
1972 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1973 goto strm_err;
1974 }
1975
1976 if (h2s->flags & H2_SF_RST_RCVD) {
1977 /* RFC7540#5.1:closed: an endpoint that
1978 * receives any frame other than PRIORITY after
1979 * receiving a RST_STREAM MUST treat that as a
1980 * stream error of type STREAM_CLOSED.
1981 *
1982 * Note that old streams fall into this category
1983 * and will lead to an RST being sent.
1984 */
1985 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1986 h2c->st0 = H2_CS_FRAME_E;
1987 goto strm_err;
1988 }
1989
1990 /* RFC7540#5.1:closed: if this state is reached as a
1991 * result of sending a RST_STREAM frame, the peer that
1992 * receives the RST_STREAM might have already sent
1993 * frames on the stream that cannot be withdrawn. An
1994 * endpoint MUST ignore frames that it receives on
1995 * closed streams after it has sent a RST_STREAM
1996 * frame. An endpoint MAY choose to limit the period
1997 * over which it ignores frames and treat frames that
1998 * arrive after this time as being in error.
1999 */
2000 if (!(h2s->flags & H2_SF_RST_SENT)) {
2001 /* RFC7540#5.1:closed: any frame other than
2002 * PRIO/WU/RST in this state MUST be treated as
2003 * a connection error
2004 */
2005 if (h2c->dft != H2_FT_RST_STREAM &&
2006 h2c->dft != H2_FT_PRIORITY &&
2007 h2c->dft != H2_FT_WINDOW_UPDATE) {
2008 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2009 goto strm_err;
2010 }
2011 }
2012 }
2013
Willy Tarreauc0da1962017-10-30 18:38:00 +01002014#if 0
2015 // problem below: it is not possible to completely ignore such
2016 // streams as we need to maintain the compression state as well
2017 // and for this we need to completely process these frames (eg:
2018 // HEADERS frames) as well as counting DATA frames to emit
2019 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2020 // This is a typical case of layer violation where the
2021 // transported contents are critical to the connection's
2022 // validity and must be ignored at the same time :-(
2023
2024 /* graceful shutdown, ignore streams whose ID is higher than
2025 * the one advertised in GOAWAY. RFC7540#6.8.
2026 */
2027 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002028 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2029 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002030 h2c->dfl -= ret;
2031 ret = h2c->dfl == 0;
2032 goto strm_err;
2033 }
2034#endif
2035
Willy Tarreau7e98c052017-10-10 15:56:59 +02002036 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002037 case H2_FT_SETTINGS:
2038 if (h2c->st0 == H2_CS_FRAME_P)
2039 ret = h2c_handle_settings(h2c);
2040
2041 if (h2c->st0 == H2_CS_FRAME_A)
2042 ret = h2c_ack_settings(h2c);
2043 break;
2044
Willy Tarreaucf68c782017-10-10 17:11:41 +02002045 case H2_FT_PING:
2046 if (h2c->st0 == H2_CS_FRAME_P)
2047 ret = h2c_handle_ping(h2c);
2048
2049 if (h2c->st0 == H2_CS_FRAME_A)
2050 ret = h2c_ack_ping(h2c);
2051 break;
2052
Willy Tarreau26f95952017-07-27 17:18:30 +02002053 case H2_FT_WINDOW_UPDATE:
2054 if (h2c->st0 == H2_CS_FRAME_P)
2055 ret = h2c_handle_window_update(h2c, h2s);
2056 break;
2057
Willy Tarreau61290ec2017-10-17 08:19:21 +02002058 case H2_FT_CONTINUATION:
2059 /* we currently don't support CONTINUATION frames since
2060 * we have nowhere to store the partial HEADERS frame.
2061 * Let's abort the stream on an INTERNAL_ERROR here.
2062 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002063 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002064 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002065 h2c->st0 = H2_CS_FRAME_E;
2066 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002067 break;
2068
Willy Tarreau13278b42017-10-13 19:23:14 +02002069 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002070 if (h2c->st0 == H2_CS_FRAME_P) {
2071 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2072 if (tmp_h2s) {
2073 h2s = tmp_h2s;
2074 ret = 1;
2075 }
2076 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002077 break;
2078
Willy Tarreau454f9052017-10-26 19:40:35 +02002079 case H2_FT_DATA:
2080 if (h2c->st0 == H2_CS_FRAME_P)
2081 ret = h2c_frt_handle_data(h2c, h2s);
2082
2083 if (h2c->st0 == H2_CS_FRAME_A)
2084 ret = h2c_send_strm_wu(h2c);
2085 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002086
Willy Tarreau92153fc2017-12-03 19:46:19 +01002087 case H2_FT_PRIORITY:
2088 if (h2c->st0 == H2_CS_FRAME_P)
2089 ret = h2c_handle_priority(h2c);
2090 break;
2091
Willy Tarreaucd234e92017-08-18 10:59:39 +02002092 case H2_FT_RST_STREAM:
2093 if (h2c->st0 == H2_CS_FRAME_P)
2094 ret = h2c_handle_rst_stream(h2c, h2s);
2095 break;
2096
Willy Tarreaue96b0922017-10-30 00:28:29 +01002097 case H2_FT_GOAWAY:
2098 if (h2c->st0 == H2_CS_FRAME_P)
2099 ret = h2c_handle_goaway(h2c);
2100 break;
2101
Willy Tarreau1c661982017-10-30 13:52:01 +01002102 case H2_FT_PUSH_PROMISE:
2103 /* not permitted here, RFC7540#5.1 */
2104 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002105 if (!h2c->nb_streams) {
2106 /* only log if no other stream can report the error */
2107 sess_log(h2c->conn->owner);
2108 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002109 break;
2110
2111 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002112 default:
2113 /* drop frames that we ignore. They may be larger than
2114 * the buffer so we drain all of their contents until
2115 * we reach the end.
2116 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002117 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2118 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002119 h2c->dfl -= ret;
2120 ret = h2c->dfl == 0;
2121 }
2122
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002123 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002124 /* We may have to send an RST if not done yet */
2125 if (h2s->st == H2_SS_ERROR)
2126 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002127
Willy Tarreaua20a5192017-12-27 11:02:06 +01002128 if (h2c->st0 == H2_CS_FRAME_E)
2129 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002130
Willy Tarreau7e98c052017-10-10 15:56:59 +02002131 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002132 if (ret <= 0) {
2133 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002134 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002135 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002136
2137 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002138 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002139 h2c->st0 = H2_CS_FRAME_H;
2140 }
2141 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002142
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002143 if (h2c->rcvd_c > 0 &&
2144 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2145 h2c_send_conn_wu(h2c);
2146
Willy Tarreau52eed752017-09-22 15:05:09 +02002147 fail:
2148 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002149 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002150 /* we may have to signal the upper layers */
2151 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002152 if (h2s->recv_wait) {
2153 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2154 tasklet_wakeup(h2s->recv_wait->task);
2155 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002156 }
2157 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002158 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002159}
2160
2161/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2162 * the end.
2163 */
2164static int h2_process_mux(struct h2c *h2c)
2165{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002166 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002167
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002168 /* start by sending possibly pending window updates */
2169 if (h2c->rcvd_c > 0 &&
2170 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2171 h2c_send_conn_wu(h2c) < 0)
2172 goto fail;
2173
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002174 /* First we always process the flow control list because the streams
2175 * waiting there were already elected for immediate emission but were
2176 * blocked just on this.
2177 */
2178
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002179 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002180 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2181 h2c->st0 >= H2_CS_ERROR)
2182 break;
2183
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002184 h2s->flags &= ~H2_SF_BLK_ANY;
2185 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2186 tasklet_wakeup(h2s->send_wait->task);
2187 h2s->send_wait = NULL;
2188 LIST_DEL(&h2s->list);
2189 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002190 }
2191
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002192 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002193 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2194 break;
2195
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002196 h2s->flags &= ~H2_SF_BLK_ANY;
2197 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2198 tasklet_wakeup(h2s->send_wait->task);
2199 h2s->send_wait = NULL;
2200 LIST_DEL(&h2s->list);
2201 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002202 }
2203
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002204 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002205 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002206 if (h2c->st0 == H2_CS_ERROR) {
2207 if (h2c->max_id >= 0) {
2208 h2c_send_goaway_error(h2c, NULL);
2209 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2210 return 0;
2211 }
2212
2213 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2214 }
2215 return 1;
2216 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002217 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002218}
2219
Willy Tarreau62f52692017-10-08 23:01:42 +02002220
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002221/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002222static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002223{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002224 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002225 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002227 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002228
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002229 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002230 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002231
Willy Tarreau315d8072017-12-10 22:17:57 +01002232 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002233 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002234
Willy Tarreau44e973f2018-03-01 17:49:30 +01002235 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002236 if (!buf) {
2237 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002238 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002239 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002240
Olivier Houchard7505f942018-08-21 18:10:44 +02002241 do {
2242 max = buf->size - b_data(buf);
2243 if (max)
2244 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2245 else
2246 ret = 0;
2247 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002248
Olivier Houchard53216e72018-10-10 15:46:36 +02002249 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002250 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002251
Olivier Houcharda1411e62018-08-17 18:42:48 +02002252 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002253 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002254 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002255 }
2256
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002257 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002258 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002259 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002260}
2261
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002262/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002263static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002264{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002265 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002266 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002267 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002268
2269 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002270 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002271
Olivier Houchard7505f942018-08-21 18:10:44 +02002272
Willy Tarreaua2af5122017-10-09 11:56:46 +02002273 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2274 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002275 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002276 }
2277
Willy Tarreaubc933932017-10-09 16:21:43 +02002278 /* This loop is quite simple : it tries to fill as much as it can from
2279 * pending streams into the existing buffer until it's reportedly full
2280 * or the end of send requests is reached. Then it tries to send this
2281 * buffer's contents out, marks it not full if at least one byte could
2282 * be sent, and tries again.
2283 *
2284 * The snd_buf() function normally takes a "flags" argument which may
2285 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2286 * data immediately comes and CO_SFL_STREAMER to indicate that the
2287 * connection is streaming lots of data (used to increase TLS record
2288 * size at the expense of latency). The former can be sent any time
2289 * there's a buffer full flag, as it indicates at least one stream
2290 * attempted to send and failed so there are pending data. An
2291 * alternative would be to set it as long as there's an active stream
2292 * but that would be problematic for ACKs until we have an absolute
2293 * guarantee that all waiters have at least one byte to send. The
2294 * latter should possibly not be set for now.
2295 */
2296
2297 done = 0;
2298 while (!done) {
2299 unsigned int flags = 0;
2300
2301 /* fill as much as we can into the current buffer */
2302 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2303 done = h2_process_mux(h2c);
2304
2305 if (conn->flags & CO_FL_ERROR)
2306 break;
2307
2308 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2309 flags |= CO_SFL_MSG_MORE;
2310
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 if (b_data(&h2c->mbuf)) {
2312 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002313 if (!ret)
2314 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002315 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002316 b_del(&h2c->mbuf, ret);
2317 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002318 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002319
2320 /* wrote at least one byte, the buffer is not full anymore */
2321 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2322 }
2323
Willy Tarreaua2af5122017-10-09 11:56:46 +02002324 if (conn->flags & CO_FL_SOCK_WR_SH) {
2325 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002327 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002328 /* We're not full anymore, so we can wake any task that are waiting
2329 * for us.
2330 */
2331 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002332 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002333 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2334 struct h2s *, list);
2335 LIST_DEL(&h2s->list);
2336 LIST_INIT(&h2s->list);
2337 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2338 tasklet_wakeup(h2s->send_wait->task);
2339 h2s->send_wait = NULL;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002340 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002341 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002342 /* We're done, no more to send */
2343 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002344 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002345schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002346 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2347 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002348 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002349}
2350
2351static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2352{
2353 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002354 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002355
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002356 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002357 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002358 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002359 ret |= h2_recv(h2c);
2360 if (ret)
2361 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002362 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002363}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002364
Willy Tarreau62f52692017-10-08 23:01:42 +02002365/* callback called on any event by the connection handler.
2366 * It applies changes and returns zero, or < 0 if it wants immediate
2367 * destruction of the connection (which normally doesn not happen in h2).
2368 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002369static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002370{
Olivier Houchard7505f942018-08-21 18:10:44 +02002371 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002372
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002373 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002374 h2_process_demux(h2c);
2375
2376 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002377 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002378
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002379 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002380 h2c->flags &= ~H2_CF_DEM_DFULL;
2381 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002382 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383
Willy Tarreau0b37d652018-10-03 10:33:02 +02002384 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002385 /* frontend is stopping, reload likely in progress, let's try
2386 * to announce a graceful shutdown if not yet done. We don't
2387 * care if it fails, it will be tried again later.
2388 */
2389 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2390 if (h2c->last_sid < 0)
2391 h2c->last_sid = (1U << 31) - 1;
2392 h2c_send_goaway_error(h2c, NULL);
2393 }
2394 }
2395
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002396 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002397 * If we received early data, and the handshake is done, wake
2398 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002399 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002400 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2401 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2402 struct eb32_node *node;
2403 struct h2s *h2s;
2404
2405 h2c->flags |= H2_CF_WAIT_FOR_HS;
2406 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2407
2408 while (node) {
2409 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002410 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002411 h2s->recv_wait) {
2412 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002413 sw->wait_reason &= ~SUB_CAN_RECV;
2414 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002415 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002416 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002417 node = eb32_next(node);
2418 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002419 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002420
Willy Tarreau26bd7612017-10-09 16:47:04 +02002421 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002422 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2423 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2424 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002425 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002426
2427 if (eb_is_empty(&h2c->streams_by_id)) {
2428 /* no more stream, kill the connection now */
2429 h2_release(conn);
2430 return -1;
2431 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002432 }
2433
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002434 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002435 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002436
Olivier Houchard53216e72018-10-10 15:46:36 +02002437 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2438 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2439 (h2c->st0 != H2_CS_ERROR &&
2440 !b_data(&h2c->mbuf) &&
2441 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2442 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002443 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444
Willy Tarreau3f133572017-10-31 19:21:06 +01002445 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002447 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002448 task_queue(h2c->task);
2449 }
2450 else
2451 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002452 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002453
Olivier Houchard7505f942018-08-21 18:10:44 +02002454 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002455 return 0;
2456}
2457
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002458static int h2_wake(struct connection *conn)
2459{
2460 struct h2c *h2c = conn->mux_ctx;
2461
2462 return (h2_process(h2c));
2463}
2464
2465
Willy Tarreauea392822017-10-31 10:02:25 +01002466/* Connection timeout management. The principle is that if there's no receipt
2467 * nor sending for a certain amount of time, the connection is closed. If the
2468 * MUX buffer still has lying data or is not allocatable, the connection is
2469 * immediately killed. If it's allocatable and empty, we attempt to send a
2470 * GOAWAY frame.
2471 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002472static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002473{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002474 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002475 int expired = tick_is_expired(t->expire, now_ms);
2476
Willy Tarreau0975f112018-03-29 15:22:59 +02002477 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002478 return t;
2479
Willy Tarreau0975f112018-03-29 15:22:59 +02002480 task_delete(t);
2481 task_free(t);
2482
2483 if (!h2c) {
2484 /* resources were already deleted */
2485 return NULL;
2486 }
2487
2488 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002489 h2c_error(h2c, H2_ERR_NO_ERROR);
2490 h2_wake_some_streams(h2c, 0, 0);
2491
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002492 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002493 /* don't even try to send a GOAWAY, the buffer is stuck */
2494 h2c->flags |= H2_CF_GOAWAY_FAILED;
2495 }
2496
2497 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002498 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002499 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2500 h2c->flags |= H2_CF_GOAWAY_FAILED;
2501
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002502 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2503 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002504 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002505 b_del(&h2c->mbuf, ret);
2506 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002507 }
2508 }
Willy Tarreauea392822017-10-31 10:02:25 +01002509
Willy Tarreau0975f112018-03-29 15:22:59 +02002510 /* either we can release everything now or it will be done later once
2511 * the last stream closes.
2512 */
2513 if (eb_is_empty(&h2c->streams_by_id))
2514 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002515
Willy Tarreauea392822017-10-31 10:02:25 +01002516 return NULL;
2517}
2518
2519
Willy Tarreau62f52692017-10-08 23:01:42 +02002520/*******************************************/
2521/* functions below are used by the streams */
2522/*******************************************/
2523
2524/*
2525 * Attach a new stream to a connection
2526 * (Used for outgoing connections)
2527 */
2528static struct conn_stream *h2_attach(struct connection *conn)
2529{
2530 return NULL;
2531}
2532
Willy Tarreau62f52692017-10-08 23:01:42 +02002533/*
2534 * Detach the stream from the connection and possibly release the connection.
2535 */
2536static void h2_detach(struct conn_stream *cs)
2537{
Willy Tarreau60935142017-10-16 18:11:19 +02002538 struct h2s *h2s = cs->ctx;
2539 struct h2c *h2c;
2540
2541 cs->ctx = NULL;
2542 if (!h2s)
2543 return;
2544
2545 h2c = h2s->h2c;
2546 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002547 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002548 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2549 !h2_has_too_many_cs(h2c)) {
2550 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002551 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002552 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002553 }
Willy Tarreau60935142017-10-16 18:11:19 +02002554
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002555 /* this stream may be blocked waiting for some data to leave (possibly
2556 * an ES or RST frame), so orphan it in this case.
2557 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002558 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002559 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002560 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002561 return;
2562
Willy Tarreau45f752e2017-10-30 15:44:59 +01002563 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2564 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2565 /* unblock the connection if it was blocked on this
2566 * stream.
2567 */
2568 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2569 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002570 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002571 }
2572
Willy Tarreau71049cc2018-03-28 13:56:39 +02002573 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002574
Willy Tarreaue323f342018-03-28 13:51:45 +02002575 /* We don't want to close right now unless we're removing the
2576 * last stream, and either the connection is in error, or it
2577 * reached the ID already specified in a GOAWAY frame received
2578 * or sent (as seen by last_sid >= 0).
2579 */
2580 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2581 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002582 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002583 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002584 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002585 (conn_xprt_read0_pending(h2c->conn) ||
2586 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2587 /* no more stream will come, kill it now */
2588 h2_release(h2c->conn);
2589 }
2590 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002591 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002592 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2593 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002594 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002595 else
2596 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002597 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002598}
2599
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002600static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002601{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002602 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002603 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002604
Willy Tarreau721c9742017-11-07 11:05:42 +01002605 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002606 return;
2607
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002608 /* if no outgoing data was seen on this stream, it means it was
2609 * closed with a "tcp-request content" rule that is normally
2610 * used to kill the connection ASAP (eg: limit abuse). In this
2611 * case we send a goaway to close the connection.
2612 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002613 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002614 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002615 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002616
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002617 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2618 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002619 h2c_send_goaway_error(h2c, h2s) <= 0)
2620 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002621
Willy Tarreau00dd0782018-03-01 16:31:34 +01002622 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002623
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002624 return;
2625add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002626 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002627 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002628 if (h2s->flags & H2_SF_BLK_MFCTL) {
2629 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2630 h2s->send_wait = sw;
2631 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2632 h2s->send_wait = sw;
2633 LIST_ADDQ(&h2c->send_list, &h2s->list);
2634 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002635 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002636 /* Let the handler know we want shutr */
2637 sw->handle = (void *)((long)sw->handle | 1);
2638
Willy Tarreau62f52692017-10-08 23:01:42 +02002639}
2640
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002641static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002642{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002643 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002644 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002645
Willy Tarreau721c9742017-11-07 11:05:42 +01002646 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002647 return;
2648
Willy Tarreau67434202017-11-06 20:20:51 +01002649 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002650 /* we can cleanly close using an empty data frame only after headers */
2651
2652 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2653 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002654 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002655
2656 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002657 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002658 else
2659 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002660 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002661 /* if no outgoing data was seen on this stream, it means it was
2662 * closed with a "tcp-request content" rule that is normally
2663 * used to kill the connection ASAP (eg: limit abuse). In this
2664 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002665 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002666 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002667 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002668 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002669
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002670 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2671 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002672 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002673 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002674
Willy Tarreau00dd0782018-03-01 16:31:34 +01002675 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002676 }
2677
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002678
2679 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002680 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002681 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002682 if (h2s->flags & H2_SF_BLK_MFCTL) {
2683 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2684 h2s->send_wait = sw;
2685 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2686 h2s->send_wait = sw;
2687 LIST_ADDQ(&h2c->send_list, &h2s->list);
2688 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002689 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002690 /* let the handler know we want to shutw */
2691 sw->handle = (void *)((long)(sw->handle) | 2);
2692
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002693}
2694
2695static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2696{
2697 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002698 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002699
2700 if (reason & 1)
2701 h2_do_shutr(h2s);
2702 if (reason & 2)
2703 h2_do_shutw(h2s);
2704
2705 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002706}
2707
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002708static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2709{
2710 struct h2s *h2s = cs->ctx;
2711
2712 if (!mode)
2713 return;
2714
2715 h2_do_shutr(h2s);
2716}
2717
2718static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2719{
2720 struct h2s *h2s = cs->ctx;
2721
2722 h2_do_shutw(h2s);
2723}
2724
Willy Tarreau13278b42017-10-13 19:23:14 +02002725/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2726 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2727 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002728 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002729 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002730static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002731{
2732 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002733 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002734 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002735 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002736 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002737 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002738 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002739 int flen = h2c->dfl;
2740 int outlen = 0;
2741 int wrap;
2742 int try;
2743
2744 if (!h2c->dfl) {
2745 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002746 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002747 return 0;
2748 }
2749
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002750 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002751 return 0; // incomplete input frame
2752
Willy Tarreau13278b42017-10-13 19:23:14 +02002753 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002754 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002755 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002756 copy = alloc_trash_chunk();
2757 if (!copy) {
2758 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2759 goto fail;
2760 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2762 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2763 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002764 }
2765
2766 /* The padlen is the first byte before data, and the padding appears
2767 * after data. padlen+data+padding are included in flen.
2768 */
2769 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002770 h2c->dpl = *hdrs;
2771 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002772 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2773 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002774 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002775 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002776 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 hdrs += 1; // skip Pad Length
2778 }
2779
2780 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2781 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002782 if (read_n32(hdrs) == h2s->id) {
2783 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2784 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002785 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002786 }
2787
Willy Tarreau13278b42017-10-13 19:23:14 +02002788 hdrs += 5; // stream dep = 4, weight = 1
2789 flen -= 5;
2790 }
2791
2792 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2793 * don't support this for now and can't even decompress so we have to
2794 * break the connection.
2795 */
2796 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2797 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002798 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002799 }
2800
Olivier Houchard638b7992018-08-16 15:41:52 +02002801 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002802 if (!csbuf) {
2803 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002804 goto fail;
2805 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002806
Willy Tarreau937f7602018-02-26 15:22:17 +01002807 /* we can't retry a failed decompression operation so we must be very
2808 * careful not to take any risks. In practice the output buffer is
2809 * always empty except maybe for trailers, in which case we simply have
2810 * to wait for the upper layer to finish consuming what is available.
2811 */
2812 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002813 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002814
Willy Tarreau937f7602018-02-26 15:22:17 +01002815 csbuf->head = 0;
2816 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002817
2818 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2819 sizeof(list)/sizeof(list[0]), tmp);
2820 if (outlen < 0) {
2821 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2822 goto fail;
2823 }
2824
2825 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002826 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002827 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002828
2829 if (outlen < 0) {
2830 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2831 goto fail;
2832 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002833
Willy Tarreau174b06a2018-04-25 18:13:58 +02002834 if (msgf & H2_MSGF_BODY) {
2835 /* a payload is present */
2836 if (msgf & H2_MSGF_BODY_CL)
2837 h2s->flags |= H2_SF_DATA_CLEN;
2838 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2839 h2s->flags |= H2_SF_DATA_CHNK;
2840 }
2841
Willy Tarreau13278b42017-10-13 19:23:14 +02002842 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002844 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002845 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002846
Willy Tarreau39d68502018-03-02 12:26:37 +01002847 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002848 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002849 h2s->cs->flags |= CS_FL_REOS;
2850 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002851
Willy Tarreau68dd9852017-07-03 14:44:26 +02002852 leave:
2853 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002854 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002855 fail:
2856 outlen = 0;
2857 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002858}
2859
Willy Tarreau454f9052017-10-26 19:40:35 +02002860/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2861 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2862 * in use, a new chunk is emitted for each frame. This is supposed to fit
2863 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2864 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2865 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2866 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002867 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2868 * checked to know if some data remain pending (an empty DATA frame can return
2869 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2870 * connection errors in h2c->errcode. The caller must already have checked the
2871 * frame header and ensured that the frame was complete or the buffer full. It
2872 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002873 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002874static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002875{
2876 struct h2c *h2c = h2s->h2c;
2877 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002878 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002879 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002880 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002881
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002882 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002883
2884 /* The padlen is the first byte before data, and the padding appears
2885 * after data. padlen+data+padding are included in flen.
2886 */
Willy Tarreau79127812017-12-03 21:06:59 +01002887 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002888 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002889 return 0;
2890
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002891 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002892 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002893 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2894 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002895 return 0;
2896 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002897
2898 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002899 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002900 h2c->dfl--;
2901 h2c->rcvd_c++; h2c->rcvd_s++;
2902 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002903 }
2904
Olivier Houchard638b7992018-08-16 15:41:52 +02002905 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002906 if (!csbuf) {
2907 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002908 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002909 }
2910
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002911 flen = h2c->dfl - h2c->dpl;
2912 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002913 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002914
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 if (flen > b_data(&h2c->dbuf)) {
2916 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002917 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002918 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002919 }
2920
2921 if (unlikely(b_space_wraps(csbuf))) {
2922 /* it doesn't fit and the buffer is fragmented,
2923 * so let's defragment it and try again.
2924 */
2925 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002926 }
2927
Willy Tarreaueba10f22018-04-25 20:44:22 +02002928 /* chunked-encoding requires more room */
2929 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002930 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002931 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2932 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2933 (chklen < 1048576) ? 4 : 8;
2934 chklen += 4; // CRLF, CRLF
2935 }
2936
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002937 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002938 if (flen + chklen > b_room(csbuf)) {
2939 if (chklen >= b_room(csbuf)) {
2940 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002941 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002942 }
2943 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002944 }
2945
2946 if (h2s->flags & H2_SF_DATA_CHNK) {
2947 /* emit the chunk size */
2948 unsigned int chksz = flen;
2949 char str[10];
2950 char *beg;
2951
2952 beg = str + sizeof(str);
2953 *--beg = '\n';
2954 *--beg = '\r';
2955 do {
2956 *--beg = hextab[chksz & 0xF];
2957 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002958 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002959 }
2960
Willy Tarreau454f9052017-10-26 19:40:35 +02002961 /* Block1 is the length of the first block before the buffer wraps,
2962 * block2 is the optional second block to reach the end of the frame.
2963 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002964 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002965 if (block1 > flen)
2966 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002967 block2 = flen - block1;
2968
2969 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002970 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002971
2972 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002973 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002974
Willy Tarreaueba10f22018-04-25 20:44:22 +02002975 if (h2s->flags & H2_SF_DATA_CHNK) {
2976 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002977 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002978 }
2979
Willy Tarreau454f9052017-10-26 19:40:35 +02002980 /* now mark the input data as consumed (will be deleted from the buffer
2981 * by the caller when seeing FRAME_A after sending the window update).
2982 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002983 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002984 h2c->dfl -= flen;
2985 h2c->rcvd_c += flen;
2986 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2987
2988 if (h2c->dfl > h2c->dpl) {
2989 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002990 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002991 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002992 }
2993
Willy Tarreau4a28da12018-01-04 14:41:00 +01002994 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002995 /* here we're done with the frame, all the payload (except padding) was
2996 * transferred.
2997 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002998
2999 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3000 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003001 if (b_room(csbuf) < 5) {
3002 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003003 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003004 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003005 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003006 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003007 }
3008
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003009 h2c->rcvd_c += h2c->dpl;
3010 h2c->rcvd_s += h2c->dpl;
3011 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003012 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3013
Willy Tarreau39d68502018-03-02 12:26:37 +01003014 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003015 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003016 h2s->cs->flags |= CS_FL_REOS;
3017 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003018
Willy Tarreau454b57b2018-02-26 15:50:05 +01003019 return flen + chklen;
3020 fail:
3021 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003022}
3023
Willy Tarreau5dd17352018-06-14 13:33:30 +02003024/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3025 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3026 * number of bytes sent. The caller must check the stream's status to detect
3027 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003028 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003029static 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 +02003030{
3031 struct http_hdr list[MAX_HTTP_HDR];
3032 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003033 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003034 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003035 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003036 int es_now = 0;
3037 int ret = 0;
3038 int hdr;
3039
3040 if (h2c_mux_busy(h2c, h2s)) {
3041 h2s->flags |= H2_SF_BLK_MBUSY;
3042 return 0;
3043 }
3044
Willy Tarreau44e973f2018-03-01 17:49:30 +01003045 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003046 h2c->flags |= H2_CF_MUX_MALLOC;
3047 h2s->flags |= H2_SF_BLK_MROOM;
3048 return 0;
3049 }
3050
3051 /* First, try to parse the H1 response and index it into <list>.
3052 * NOTE! Since it comes from haproxy, we *know* that a response header
3053 * block does not wrap and we can safely read it this way without
3054 * having to realign the buffer.
3055 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003056 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003057 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003058 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003059 /* incomplete or invalid response, this is abnormal coming from
3060 * haproxy and may only result in a bad errorfile or bad Lua code
3061 * so that won't be fixed, raise an error now.
3062 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003063 * FIXME: we should instead add the ability to only return a
3064 * 502 bad gateway. But in theory this is not supposed to
3065 * happen.
3066 */
3067 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3068 ret = 0;
3069 goto end;
3070 }
3071
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003072 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003073
3074 /* certain statuses have no body or an empty one, regardless of
3075 * what the headers say.
3076 */
3077 if (sl.st.status >= 100 && sl.st.status < 200) {
3078 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3079 h1m->curr_len = h1m->body_len = 0;
3080 }
3081 else if (sl.st.status == 204 || sl.st.status == 304) {
3082 /* no contents, claim c-len is present and set to zero */
3083 h1m->flags &= ~H1_MF_CHNK;
3084 h1m->flags |= H1_MF_CLEN;
3085 h1m->curr_len = h1m->body_len = 0;
3086 }
3087
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003088 chunk_reset(&outbuf);
3089
3090 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003091 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003093 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003095 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003096 break;
3097 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003098 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003099 }
3100
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003101 if (outbuf.size < 9)
3102 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003103
3104 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003105 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3106 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3107 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003108
3109 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003110 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003111 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003112 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003113 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003114 else if (unlikely(list[0].v.len != 3)) {
3115 /* this is an unparsable response */
3116 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3117 ret = 0;
3118 goto end;
3119 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003121 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003122 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3123 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3124 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3125 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3126 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003127 }
3128 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003129 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003131 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003132 }
3133
3134 /* encode all headers, stop at empty name */
3135 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003136 /* these ones do not exist in H2 and must be dropped. */
3137 if (isteq(list[hdr].n, ist("connection")) ||
3138 isteq(list[hdr].n, ist("proxy-connection")) ||
3139 isteq(list[hdr].n, ist("keep-alive")) ||
3140 isteq(list[hdr].n, ist("upgrade")) ||
3141 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003142 continue;
3143
3144 if (isteq(list[hdr].n, ist("")))
3145 break; // end
3146
3147 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3148 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003149 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003150 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003151 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003152 }
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 Tarreau801250e2018-09-11 11:45:04 +02003179 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003180 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 Tarreau9c5e22e2018-09-11 19:22:14 +02003186 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003187 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003188 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003189 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003190 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003191 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003192 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003193
3194 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003195
3196 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003197 //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, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003198 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003199 full:
3200 h1m_init_res(h1m);
3201 h1m->err_pos = -1; // don't care about errors on the response path
3202 h2c->flags |= H2_CF_MUX_MFULL;
3203 h2s->flags |= H2_SF_BLK_MROOM;
3204 ret = 0;
3205 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003206}
3207
Willy Tarreau5dd17352018-06-14 13:33:30 +02003208/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3209 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3210 * the number of bytes sent. The caller must check the stream's status to
3211 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003212 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003213static 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 +02003214{
3215 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003216 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003217 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003218 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003219 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003220 int es_now = 0;
3221 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003222 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003223 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224
3225 if (h2c_mux_busy(h2c, h2s)) {
3226 h2s->flags |= H2_SF_BLK_MBUSY;
3227 goto end;
3228 }
3229
Willy Tarreau44e973f2018-03-01 17:49:30 +01003230 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003231 h2c->flags |= H2_CF_MUX_MALLOC;
3232 h2s->flags |= H2_SF_BLK_MROOM;
3233 goto end;
3234 }
3235
3236 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003237 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003238 goto end;
3239
3240 chunk_reset(&outbuf);
3241
3242 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003243 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003244 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003245 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003246
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003247 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003248 break;
3249 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003251 }
3252
3253 if (outbuf.size < 9) {
3254 h2c->flags |= H2_CF_MUX_MFULL;
3255 h2s->flags |= H2_SF_BLK_MROOM;
3256 goto end;
3257 }
3258
3259 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003260 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3261 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3262 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003263
3264 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3265 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003266 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003267 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268 break;
3269 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003270 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003271 if ((long long)size > h1m->curr_len)
3272 size = h1m->curr_len;
3273 break;
3274 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003275 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003276 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003277 if (!ret)
3278 goto end;
3279
3280 if (ret < 0) {
3281 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003282 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003283 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3284 goto end;
3285 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003286 max -= ret;
3287 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003289 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003290 }
3291
Willy Tarreau801250e2018-09-11 11:45:04 +02003292 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003294 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003295 if (!ret)
3296 goto end;
3297
3298 if (ret < 0) {
3299 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003300 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003301 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3302 goto end;
3303 }
3304
3305 size = chunk;
3306 h1m->curr_len = chunk;
3307 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003308 max -= ret;
3309 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003310 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003311 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003312 if (!size)
3313 goto send_empty;
3314 }
3315
3316 /* in MSG_DATA state, continue below */
3317 size = h1m->curr_len;
3318 break;
3319 }
3320
3321 /* we have in <size> the exact number of bytes we need to copy from
3322 * the H1 buffer. We need to check this against the connection's and
3323 * the stream's send windows, and to ensure that this fits in the max
3324 * frame size and in the buffer's available space minus 9 bytes (for
3325 * the frame header). The connection's flow control is applied last so
3326 * that we can use a separate list of streams which are immediately
3327 * unblocked on window opening. Note: we don't implement padding.
3328 */
3329
Willy Tarreau5dd17352018-06-14 13:33:30 +02003330 if (size > max)
3331 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003332
3333 if (size > h2s->mws)
3334 size = h2s->mws;
3335
3336 if (size <= 0) {
3337 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003338 if (h2s->send_wait) {
3339 LIST_DEL(&h2s->list);
3340 LIST_INIT(&h2s->list);
3341 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 goto end;
3343 }
3344
3345 if (h2c->mfs && size > h2c->mfs)
3346 size = h2c->mfs;
3347
3348 if (size + 9 > outbuf.size) {
3349 /* we have an opportunity for enlarging the too small
3350 * available space, let's try.
3351 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003352 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003353 goto realign_again;
3354 size = outbuf.size - 9;
3355 }
3356
3357 if (size <= 0) {
3358 h2c->flags |= H2_CF_MUX_MFULL;
3359 h2s->flags |= H2_SF_BLK_MROOM;
3360 goto end;
3361 }
3362
3363 if (size > h2c->mws)
3364 size = h2c->mws;
3365
3366 if (size <= 0) {
3367 h2s->flags |= H2_SF_BLK_MFCTL;
3368 goto end;
3369 }
3370
3371 /* copy whatever we can */
3372 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003373 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003374 if (ret == 1)
3375 len2 = 0;
3376
3377 if (!ret || len1 + len2 < size) {
3378 /* FIXME: must normally never happen */
3379 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3380 goto end;
3381 }
3382
3383 /* limit len1/len2 to size */
3384 if (len1 + len2 > size) {
3385 int sub = len1 + len2 - size;
3386
3387 if (len2 > sub)
3388 len2 -= sub;
3389 else {
3390 sub -= len2;
3391 len2 = 0;
3392 len1 -= sub;
3393 }
3394 }
3395
3396 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003397 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003398 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003399 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003400
3401 send_empty:
3402 /* we may need to add END_STREAM */
3403 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3404 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003405 *
3406 * FIXME: what we do here is not correct because we send end_stream
3407 * before knowing if we'll have to send a HEADERS frame for the
3408 * trailers. More importantly we're not consuming the trailing CRLF
3409 * after the end of trailers, so it will be left to the caller to
3410 * eat it. The right way to do it would be to measure trailers here
3411 * and to send ES only if there are no trailers.
3412 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003413 */
3414 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003415 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003416 es_now = 1;
3417
3418 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003419 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003420
3421 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003422 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003423
3424 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003425 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003426
3427 /* consume incoming H1 response */
3428 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003429 max -= size;
3430 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431 total += size;
3432 h1m->curr_len -= size;
3433 h2s->mws -= size;
3434 h2c->mws -= size;
3435
3436 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003437 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003438 goto new_frame;
3439 }
3440 }
3441
3442 if (es_now) {
3443 if (h2s->st == H2_SS_OPEN)
3444 h2s->st = H2_SS_HLOC;
3445 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003446 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003447
Willy Tarreau35a62702018-02-27 15:37:25 +01003448 if (!(h1m->flags & H1_MF_CHNK)) {
3449 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003450 total += max;
3451 ofs += max;
3452 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003453
Willy Tarreau801250e2018-09-11 11:45:04 +02003454 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003455 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003456
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003457 h2s->flags |= H2_SF_ES_SENT;
3458 }
3459
3460 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003461 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, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003462 return total;
3463}
3464
Olivier Houchard6ff20392018-07-17 18:46:31 +02003465/* Called from the upper layer, to subscribe to events, such as being able to send */
3466static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3467{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003468 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003469 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003470 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003471
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003472 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003473 sw = param;
3474 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3475 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003476 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003477 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003478 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003479 event_type &= ~SUB_CAN_RECV;
3480 }
3481 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003482 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003483 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003484 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003485 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003486 h2s->send_wait = sw;
3487 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3488 if (h2s->flags & H2_SF_BLK_MFCTL)
3489 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3490 else
3491 LIST_ADDQ(&h2c->send_list, &h2s->list);
3492 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003493 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003494 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003495 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003496 if (event_type != 0)
3497 return -1;
3498 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003499
3500
3501}
3502
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003503static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3504{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003505 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003506 struct h2s *h2s = cs->ctx;
3507
3508 if (event_type & SUB_CAN_RECV) {
3509 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003510 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003511 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003512 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003513 }
3514 }
3515 if (event_type & SUB_CAN_SEND) {
3516 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003517 if (h2s->send_wait == sw) {
3518 LIST_DEL(&h2s->list);
3519 LIST_INIT(&h2s->list);
3520 sw->wait_reason &= ~SUB_CAN_SEND;
3521 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003522 }
3523 }
3524 return 0;
3525}
3526
3527
Olivier Houchard511efea2018-08-16 15:30:32 +02003528/* Called from the upper layer, to receive data */
3529static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3530{
Olivier Houchard638b7992018-08-16 15:41:52 +02003531 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003532 size_t ret = 0;
3533
3534 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003535 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003536
Olivier Houchard638b7992018-08-16 15:41:52 +02003537 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003538 cs->flags |= CS_FL_RCV_MORE;
3539 else {
3540 cs->flags &= ~CS_FL_RCV_MORE;
3541 if (cs->flags & CS_FL_REOS)
3542 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003543 if (b_size(&h2s->rxbuf)) {
3544 b_free(&h2s->rxbuf);
3545 offer_buffers(NULL, tasks_run_queue);
3546 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003547 }
3548
3549 return ret;
3550}
3551
Willy Tarreau62f52692017-10-08 23:01:42 +02003552/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003553static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003554{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003555 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003556 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003557 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003558
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003559 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3560 return 0;
3561
Willy Tarreau0bad0432018-06-14 16:54:01 +02003562 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003563 h2s->flags |= H2_SF_OUTGOING_DATA;
3564
Willy Tarreaua40704a2018-09-11 13:52:04 +02003565 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003566 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003567 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003568 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003569 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003570 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003571 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003572 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003573 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003574 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003575
Willy Tarreau5dd17352018-06-14 13:33:30 +02003576 if (unlikely((int)ret <= 0)) {
3577 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003578 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3579 break;
3580 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003581 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003582 total += count;
3583 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003584 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003585 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003586 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003587 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003588 cs->flags |= CS_FL_ERROR;
3589 break;
3590 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003591
3592 total += ret;
3593 count -= ret;
3594
3595 if (h2s->st >= H2_SS_ERROR)
3596 break;
3597
3598 if (h2s->flags & H2_SF_BLK_ANY)
3599 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003600 }
3601
Willy Tarreau00610962018-07-19 10:58:28 +02003602 if (h2s->st >= H2_SS_ERROR) {
3603 /* trim any possibly pending data after we close (extra CR-LF,
3604 * unprocessed trailers, abnormal extra data, ...)
3605 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003606 total += count;
3607 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003608 }
3609
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003610 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003611 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003612 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003613 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003614 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003615 }
3616
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003617 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003618 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003619 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3620 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchard7505f942018-08-21 18:10:44 +02003621 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003622 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003623}
3624
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003625/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003626static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003627{
3628 struct h2c *h2c = conn->mux_ctx;
3629 struct h2s *h2s;
3630 struct eb32_node *node;
3631 int fctl_cnt = 0;
3632 int send_cnt = 0;
3633 int tree_cnt = 0;
3634 int orph_cnt = 0;
3635
3636 if (!h2c)
3637 return;
3638
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003639 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003640 fctl_cnt++;
3641
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003642 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003643 send_cnt++;
3644
3645 node = eb32_first(&h2c->streams_by_id);
3646 while (node) {
3647 h2s = container_of(node, struct h2s, by_id);
3648 tree_cnt++;
3649 if (!h2s->cs)
3650 orph_cnt++;
3651 node = eb32_next(node);
3652 }
3653
Willy Tarreau616ac812018-07-24 14:12:42 +02003654 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3655 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3656 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3657 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3658 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3659 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003660}
Willy Tarreau62f52692017-10-08 23:01:42 +02003661
3662/*******************************************************/
3663/* functions below are dedicated to the config parsers */
3664/*******************************************************/
3665
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003666/* config parser for global "tune.h2.header-table-size" */
3667static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3668 struct proxy *defpx, const char *file, int line,
3669 char **err)
3670{
3671 if (too_many_args(1, args, err, NULL))
3672 return -1;
3673
3674 h2_settings_header_table_size = atoi(args[1]);
3675 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3676 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3677 return -1;
3678 }
3679 return 0;
3680}
Willy Tarreau62f52692017-10-08 23:01:42 +02003681
Willy Tarreaue6baec02017-07-27 11:45:11 +02003682/* config parser for global "tune.h2.initial-window-size" */
3683static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3684 struct proxy *defpx, const char *file, int line,
3685 char **err)
3686{
3687 if (too_many_args(1, args, err, NULL))
3688 return -1;
3689
3690 h2_settings_initial_window_size = atoi(args[1]);
3691 if (h2_settings_initial_window_size < 0) {
3692 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3693 return -1;
3694 }
3695 return 0;
3696}
3697
Willy Tarreau5242ef82017-07-27 11:47:28 +02003698/* config parser for global "tune.h2.max-concurrent-streams" */
3699static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3700 struct proxy *defpx, const char *file, int line,
3701 char **err)
3702{
3703 if (too_many_args(1, args, err, NULL))
3704 return -1;
3705
3706 h2_settings_max_concurrent_streams = atoi(args[1]);
3707 if (h2_settings_max_concurrent_streams < 0) {
3708 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3709 return -1;
3710 }
3711 return 0;
3712}
3713
Willy Tarreau62f52692017-10-08 23:01:42 +02003714
3715/****************************************/
3716/* MUX initialization and instanciation */
3717/***************************************/
3718
3719/* The mux operations */
3720const struct mux_ops h2_ops = {
3721 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003722 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003723 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003724 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003725 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003726 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003727 .attach = h2_attach,
3728 .detach = h2_detach,
3729 .shutr = h2_shutr,
3730 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003731 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003732 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003733 .name = "H2",
3734};
3735
Christopher Faulet32f61c02018-04-10 14:33:41 +02003736/* PROTO selection : this mux registers PROTO token "h2" */
3737static struct mux_proto_list mux_proto_h2 =
3738 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003739
3740/* config keyword parsers */
3741static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003742 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003743 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003744 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003745 { 0, NULL, NULL }
3746}};
3747
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003748static void __h2_deinit(void)
3749{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003750 pool_destroy(pool_head_h2s);
3751 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003752}
3753
Willy Tarreau62f52692017-10-08 23:01:42 +02003754__attribute__((constructor))
3755static void __h2_init(void)
3756{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003757 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003758 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003759 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003760 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3761 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003762}