blob: 4fe811af4b0ce0b26f0235d82188466ed7a4957d [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 Houchardaf4021e2018-08-09 13:06:55 +0200289 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200290 conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200291 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200292 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200293 return 1;
294 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200295
Willy Tarreau44e973f2018-03-01 17:49:30 +0100296 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
297 h2c->flags &= ~H2_CF_MUX_MALLOC;
298 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
299 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200300
301 if (h2c->flags & H2_CF_DEM_MROOM) {
302 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200303 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200304 conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200305 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200306 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200307 }
Willy Tarreau14398122017-09-22 14:26:04 +0200308 return 1;
309 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100310
311 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
312 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200313 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100314 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200315 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100316 conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200317 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200318 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100319 return 1;
320 }
321
Willy Tarreau14398122017-09-22 14:26:04 +0200322 return 0;
323}
324
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200325static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200326{
327 struct buffer *buf = NULL;
328
Willy Tarreau44e973f2018-03-01 17:49:30 +0100329 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
330 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
331 h2c->buf_wait.target = h2c;
332 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100334 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200336 __conn_xprt_stop_recv(h2c->conn);
337 }
338 return buf;
339}
340
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200342{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200343 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100344 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200345 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200346 }
347}
348
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200349
Willy Tarreau62f52692017-10-08 23:01:42 +0200350/*****************************************************************/
351/* functions below are dedicated to the mux setup and management */
352/*****************************************************************/
353
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200354/* Initialize the mux once it's attached. For outgoing connections, the context
355 * is already initialized before installing the mux, so we detect incoming
356 * connections from the fact that the context is still NULL. Returns < 0 on
357 * error.
358 */
359static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200360{
361 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100362 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200364 /* we don't support outgoing connections for now */
365 if (conn->mux_ctx)
366 goto fail_no_h2c;
367
Willy Tarreaubafbe012017-11-24 17:34:44 +0100368 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200369 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200370 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371
Willy Tarreau0b37d652018-10-03 10:33:02 +0200372 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
373 if (tick_isset(prx->timeout.clientfin))
374 h2c->shut_timeout = prx->timeout.clientfin;
Willy Tarreau3f133572017-10-31 19:21:06 +0100375
Willy Tarreau0b37d652018-10-03 10:33:02 +0200376 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100377 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100378 if (tick_isset(h2c->timeout)) {
379 t = task_new(tid_bit);
380 if (!t)
381 goto fail;
382
383 h2c->task = t;
384 t->process = h2_timeout_task;
385 t->context = h2c;
386 t->expire = tick_add(now_ms, h2c->timeout);
387 }
Willy Tarreauea392822017-10-31 10:02:25 +0100388
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200389 h2c->wait_event.task = tasklet_new();
390 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200391 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200392 h2c->wait_event.task->process = h2_io_cb;
393 h2c->wait_event.task->context = h2c;
394 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200395
Willy Tarreau32218eb2017-09-22 08:07:25 +0200396 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
397 if (!h2c->ddht)
398 goto fail;
399
400 /* Initialise the context. */
401 h2c->st0 = H2_CS_PREFACE;
402 h2c->conn = conn;
403 h2c->max_id = -1;
404 h2c->errcode = H2_ERR_NO_ERROR;
405 h2c->flags = H2_CF_NONE;
406 h2c->rcvd_c = 0;
407 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100408 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200409 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200410
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200411 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200412 h2c->dsi = -1;
413 h2c->msi = -1;
414 h2c->last_sid = -1;
415
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200416 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417 h2c->miw = 65535; /* mux initial window size */
418 h2c->mws = 65535; /* mux window size */
419 h2c->mfs = 16384; /* initial max frame size */
420 h2c->streams_by_id = EB_ROOT_UNIQUE;
421 LIST_INIT(&h2c->send_list);
422 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100423 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424 conn->mux_ctx = h2c;
425
Willy Tarreau3f133572017-10-31 19:21:06 +0100426 if (t)
427 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100428
Willy Tarreau0f383582018-10-03 14:22:21 +0200429 /* prepare to read something */
430 conn_xprt_want_recv(conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200431 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200433 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100434 if (t)
435 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200436 if (h2c->wait_event.task)
437 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100438 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200439 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200440 return -1;
441}
442
Willy Tarreau2373acc2017-10-12 17:35:14 +0200443/* returns the stream associated with id <id> or NULL if not found */
444static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
445{
446 struct eb32_node *node;
447
Willy Tarreau2a856182017-05-16 15:20:39 +0200448 if (id > h2c->max_id)
449 return (struct h2s *)h2_idle_stream;
450
Willy Tarreau2373acc2017-10-12 17:35:14 +0200451 node = eb32_lookup(&h2c->streams_by_id, id);
452 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200453 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200454
455 return container_of(node, struct h2s, by_id);
456}
457
Willy Tarreau62f52692017-10-08 23:01:42 +0200458/* release function for a connection. This one should be called to free all
459 * resources allocated to the mux.
460 */
461static void h2_release(struct connection *conn)
462{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 struct h2c *h2c = conn->mux_ctx;
464
465 LIST_DEL(&conn->list);
466
467 if (h2c) {
468 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200469
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100470 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100471 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100472 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200473
Willy Tarreau44e973f2018-03-01 17:49:30 +0100474 h2_release_buf(h2c, &h2c->dbuf);
475 h2_release_buf(h2c, &h2c->mbuf);
476
Willy Tarreauea392822017-10-31 10:02:25 +0100477 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200478 h2c->task->context = NULL;
479 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100480 h2c->task = NULL;
481 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200482 if (h2c->wait_event.task)
483 tasklet_free(h2c->wait_event.task);
484 if (h2c->wait_event.wait_reason != 0)
485 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
486 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100487
Willy Tarreaubafbe012017-11-24 17:34:44 +0100488 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200489 }
490
491 conn->mux = NULL;
492 conn->mux_ctx = NULL;
493
494 conn_stop_tracking(conn);
495 conn_full_close(conn);
496 if (conn->destroy_cb)
497 conn->destroy_cb(conn);
498 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200499}
500
501
Willy Tarreau71681172017-10-23 14:39:06 +0200502/******************************************************/
503/* functions below are for the H2 protocol processing */
504/******************************************************/
505
506/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100507static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200508{
509 return h2s ? h2s->id : 0;
510}
511
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200512/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200514{
515 if (h2c->msi < 0)
516 return 0;
517
518 if (h2c->msi == h2s_id(h2s))
519 return 0;
520
521 return 1;
522}
523
Willy Tarreau741d6df2017-10-17 08:00:59 +0200524/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200526{
527 h2c->errcode = err;
528 h2c->st0 = H2_CS_ERROR;
529}
530
Willy Tarreau2e43f082017-10-17 08:03:59 +0200531/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200533{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200534 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200535 h2s->errcode = err;
536 h2s->st = H2_SS_ERROR;
537 if (h2s->cs)
538 h2s->cs->flags |= CS_FL_ERROR;
539 }
540}
541
Willy Tarreaue4820742017-07-27 13:37:23 +0200542/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100543static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200544{
545 uint8_t *out = frame;
546
547 *out = len >> 16;
548 write_n16(out + 1, len);
549}
550
Willy Tarreau54c15062017-10-10 17:10:03 +0200551/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
552 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
553 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200554 * available in the buffer's input prior to calling this function. The buffer
555 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200556 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200558 const struct buffer *b, int o)
559{
Willy Tarreau591d4452018-06-15 17:21:00 +0200560 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 +0200561}
562
Willy Tarreau1f094672017-11-20 21:27:45 +0100563static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200564{
Willy Tarreau591d4452018-06-15 17:21:00 +0200565 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200566}
567
Willy Tarreau1f094672017-11-20 21:27:45 +0100568static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200569{
Willy Tarreau591d4452018-06-15 17:21:00 +0200570 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200571}
572
Willy Tarreau1f094672017-11-20 21:27:45 +0100573static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200574{
Willy Tarreau591d4452018-06-15 17:21:00 +0200575 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200576}
577
578
Willy Tarreau715d5312017-07-11 15:20:24 +0200579/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
580 * is not obvious. It turns out that H2 headers are neither aligned nor do they
581 * use regular sizes. And to add to the trouble, the buffer may wrap so each
582 * byte read must be checked. The header is formed like this :
583 *
584 * b0 b1 b2 b3 b4 b5..b8
585 * +----------+---------+--------+----+----+----------------------+
586 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
587 * +----------+---------+--------+----+----+----------------------+
588 *
589 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
590 * we get the sid properly aligned and ordered, and 16 bits of len properly
591 * ordered as well. The type and flags can be extracted using bit shifts from
592 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200593 * Returns zero if some bytes are missing, otherwise non-zero on success. The
594 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200595 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100596static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200597{
598 uint64_t w;
599
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200600 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200601 return 0;
602
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200603 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200604 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200605 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
606 h->ff = w >> 32;
607 h->ft = w >> 40;
608 h->len += w >> 48;
609 return 1;
610}
611
612/* skip the next 9 bytes corresponding to the frame header possibly parsed by
613 * h2_peek_frame_hdr() above.
614 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100615static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200616{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200617 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200618}
619
620/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100621static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200622{
623 int ret;
624
625 ret = h2_peek_frame_hdr(b, h);
626 if (ret > 0)
627 h2_skip_frame_hdr(b);
628 return ret;
629}
630
Willy Tarreau00dd0782018-03-01 16:31:34 +0100631/* marks stream <h2s> as CLOSED and decrement the number of active streams for
632 * its connection if the stream was not yet closed. Please use this exclusively
633 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100634 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100635static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100636{
637 if (h2s->st != H2_SS_CLOSED)
638 h2s->h2c->nb_streams--;
639 h2s->st = H2_SS_CLOSED;
640}
641
Willy Tarreau71049cc2018-03-28 13:56:39 +0200642/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
643static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100644{
645 h2s_close(h2s);
646 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200647 if (b_size(&h2s->rxbuf)) {
648 b_free(&h2s->rxbuf);
649 offer_buffers(NULL, tasks_run_queue);
650 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200651 if (h2s->send_wait != NULL)
652 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
653 if (h2s->recv_wait != NULL)
654 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
655 /* There's no need to explicitely call unsubscribe here, the only
656 * reference left would be in the h2c send_list/fctl_list, and if
657 * we're in it, we're getting out anyway
658 */
659 LIST_DEL(&h2s->list);
660 LIST_INIT(&h2s->list);
661 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100662 pool_free(pool_head_h2s, h2s);
663}
664
Willy Tarreaua8e49542018-10-03 18:53:55 +0200665/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
666 * stream tree. In case of error, nothing is added and NULL is returned. The
667 * causes of errors can be any failed memory allocation. The caller is
668 * responsible for checking if the connection may support an extra stream
669 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200670 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200671static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200672{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673 struct h2s *h2s;
674
Willy Tarreaubafbe012017-11-24 17:34:44 +0100675 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200676 if (!h2s)
677 goto out;
678
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200679 h2s->wait_event.task = tasklet_new();
680 if (!h2s->wait_event.task) {
681 pool_free(pool_head_h2s, h2s);
682 goto out;
683 }
684 h2s->send_wait = NULL;
685 h2s->recv_wait = NULL;
686 h2s->wait_event.task->process = h2_deferred_shut;
687 h2s->wait_event.task->context = h2s;
688 h2s->wait_event.handle = NULL;
689 h2s->wait_event.wait_reason = 0;
690 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200691 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200692 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200693 h2s->mws = h2c->miw;
694 h2s->flags = H2_SF_NONE;
695 h2s->errcode = H2_ERR_NO_ERROR;
696 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200697 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200698 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200699 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200700 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200701 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200702 h2s->by_id.key = h2s->id = id;
703 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200704
705 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100706 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200707
708 return h2s;
709
710 out_free_h2s:
711 pool_free(pool_head_h2s, h2s);
712 out:
713 return NULL;
714}
715
716/* creates a new stream <id> on the h2c connection and returns it, or NULL in
717 * case of memory allocation error.
718 */
719static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
720{
721 struct session *sess = h2c->conn->owner;
722 struct conn_stream *cs;
723 struct h2s *h2s;
724
725 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
726 goto out;
727
728 h2s = h2s_new(h2c, id);
729 if (!h2s)
730 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200731
732 cs = cs_new(h2c->conn);
733 if (!cs)
734 goto out_close;
735
736 h2s->cs = cs;
737 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200738 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200739
740 if (stream_create_from_cs(cs) < 0)
741 goto out_free_cs;
742
Willy Tarreau590a0512018-09-05 11:56:48 +0200743 /* We want the accept date presented to the next stream to be the one
744 * we have now, the handshake time to be null (since the next stream
745 * is not delayed by a handshake), and the idle time to count since
746 * right now.
747 */
748 sess->accept_date = date;
749 sess->tv_accept = now;
750 sess->t_handshake = 0;
751
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200752 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200753 if (h2_has_too_many_cs(h2c))
754 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200755 return h2s;
756
757 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200758 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200759 cs_free(cs);
760 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200761 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200762 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200763 sess_log(sess);
764 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200765}
766
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
768 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
769 * the various settings codes.
770 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200771static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200772{
773 struct buffer *res;
774 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200775 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200776 int ret;
777
778 if (h2c_mux_busy(h2c, NULL)) {
779 h2c->flags |= H2_CF_DEM_MBUSY;
780 return 0;
781 }
782
Willy Tarreau44e973f2018-03-01 17:49:30 +0100783 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200784 if (!res) {
785 h2c->flags |= H2_CF_MUX_MALLOC;
786 h2c->flags |= H2_CF_DEM_MROOM;
787 return 0;
788 }
789
790 chunk_init(&buf, buf_data, sizeof(buf_data));
791 chunk_memcpy(&buf,
792 "\x00\x00\x00" /* length : 0 for now */
793 "\x04\x00" /* type : 4 (settings), flags : 0 */
794 "\x00\x00\x00\x00", /* stream ID : 0 */
795 9);
796
797 if (h2_settings_header_table_size != 4096) {
798 char str[6] = "\x00\x01"; /* header_table_size */
799
800 write_n32(str + 2, h2_settings_header_table_size);
801 chunk_memcat(&buf, str, 6);
802 }
803
804 if (h2_settings_initial_window_size != 65535) {
805 char str[6] = "\x00\x04"; /* initial_window_size */
806
807 write_n32(str + 2, h2_settings_initial_window_size);
808 chunk_memcat(&buf, str, 6);
809 }
810
811 if (h2_settings_max_concurrent_streams != 0) {
812 char str[6] = "\x00\x03"; /* max_concurrent_streams */
813
814 /* Note: 0 means "unlimited" for haproxy's config but not for
815 * the protocol, so never send this value!
816 */
817 write_n32(str + 2, h2_settings_max_concurrent_streams);
818 chunk_memcat(&buf, str, 6);
819 }
820
821 if (global.tune.bufsize != 16384) {
822 char str[6] = "\x00\x05"; /* max_frame_size */
823
824 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
825 * match bufsize - rewrite size, but at the moment it seems
826 * that clients don't take care of it.
827 */
828 write_n32(str + 2, global.tune.bufsize);
829 chunk_memcat(&buf, str, 6);
830 }
831
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200832 h2_set_frame_size(buf.area, buf.data - 9);
833 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200834 if (unlikely(ret <= 0)) {
835 if (!ret) {
836 h2c->flags |= H2_CF_MUX_MFULL;
837 h2c->flags |= H2_CF_DEM_MROOM;
838 return 0;
839 }
840 else {
841 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
842 return 0;
843 }
844 }
845 return ret;
846}
847
Willy Tarreau52eed752017-09-22 15:05:09 +0200848/* Try to receive a connection preface, then upon success try to send our
849 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
850 * missing data. It may return an error in h2c.
851 */
852static int h2c_frt_recv_preface(struct h2c *h2c)
853{
854 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200855 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200856
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200857 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200858
859 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200860 if (ret1 < 0)
861 sess_log(h2c->conn->owner);
862
Willy Tarreau52eed752017-09-22 15:05:09 +0200863 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
864 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
865 return 0;
866 }
867
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200868 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200869 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200870 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200871
Willy Tarreaube5b7152017-09-25 16:25:39 +0200872 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200873}
874
Willy Tarreau081d4722017-05-16 21:51:05 +0200875/* try to send a GOAWAY frame on the connection to report an error or a graceful
876 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
877 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
878 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
879 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
880 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
881 * on unrecoverable failure. It will not attempt to send one again in this last
882 * case so that it is safe to use h2c_error() to report such errors.
883 */
884static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
885{
886 struct buffer *res;
887 char str[17];
888 int ret;
889
890 if (h2c->flags & H2_CF_GOAWAY_FAILED)
891 return 1; // claim that it worked
892
893 if (h2c_mux_busy(h2c, h2s)) {
894 if (h2s)
895 h2s->flags |= H2_SF_BLK_MBUSY;
896 else
897 h2c->flags |= H2_CF_DEM_MBUSY;
898 return 0;
899 }
900
Willy Tarreau44e973f2018-03-01 17:49:30 +0100901 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200902 if (!res) {
903 h2c->flags |= H2_CF_MUX_MALLOC;
904 if (h2s)
905 h2s->flags |= H2_SF_BLK_MROOM;
906 else
907 h2c->flags |= H2_CF_DEM_MROOM;
908 return 0;
909 }
910
911 /* len: 8, type: 7, flags: none, sid: 0 */
912 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
913
914 if (h2c->last_sid < 0)
915 h2c->last_sid = h2c->max_id;
916
917 write_n32(str + 9, h2c->last_sid);
918 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200919 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200920 if (unlikely(ret <= 0)) {
921 if (!ret) {
922 h2c->flags |= H2_CF_MUX_MFULL;
923 if (h2s)
924 h2s->flags |= H2_SF_BLK_MROOM;
925 else
926 h2c->flags |= H2_CF_DEM_MROOM;
927 return 0;
928 }
929 else {
930 /* we cannot report this error using GOAWAY, so we mark
931 * it and claim a success.
932 */
933 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
934 h2c->flags |= H2_CF_GOAWAY_FAILED;
935 return 1;
936 }
937 }
938 h2c->flags |= H2_CF_GOAWAY_SENT;
939 return ret;
940}
941
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100942/* Try to send an RST_STREAM frame on the connection for the indicated stream
943 * during mux operations. This stream must be valid and cannot be closed
944 * already. h2s->id will be used for the stream ID and h2s->errcode will be
945 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
946 * not yet.
947 *
948 * Returns > 0 on success or zero if nothing was done. In case of lack of room
949 * to write the message, it subscribes the stream to future notifications.
950 */
951static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
952{
953 struct buffer *res;
954 char str[13];
955 int ret;
956
957 if (!h2s || h2s->st == H2_SS_CLOSED)
958 return 1;
959
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100960 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
961 * RST_STREAM in response to a RST_STREAM frame.
962 */
963 if (h2c->dft == H2_FT_RST_STREAM) {
964 ret = 1;
965 goto ignore;
966 }
967
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100968 if (h2c_mux_busy(h2c, h2s)) {
969 h2s->flags |= H2_SF_BLK_MBUSY;
970 return 0;
971 }
972
Willy Tarreau44e973f2018-03-01 17:49:30 +0100973 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100974 if (!res) {
975 h2c->flags |= H2_CF_MUX_MALLOC;
976 h2s->flags |= H2_SF_BLK_MROOM;
977 return 0;
978 }
979
980 /* len: 4, type: 3, flags: none */
981 memcpy(str, "\x00\x00\x04\x03\x00", 5);
982 write_n32(str + 5, h2s->id);
983 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200984 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100985
986 if (unlikely(ret <= 0)) {
987 if (!ret) {
988 h2c->flags |= H2_CF_MUX_MFULL;
989 h2s->flags |= H2_SF_BLK_MROOM;
990 return 0;
991 }
992 else {
993 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
994 return 0;
995 }
996 }
997
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100998 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100999 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001000 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001001 return ret;
1002}
1003
1004/* Try to send an RST_STREAM frame on the connection for the stream being
1005 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1006 * error code unless the stream's state already is IDLE or CLOSED in which
1007 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1008 * it was not yet.
1009 *
1010 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1011 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001012 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001013 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001014 */
1015static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1016{
1017 struct buffer *res;
1018 char str[13];
1019 int ret;
1020
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001021 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1022 * RST_STREAM in response to a RST_STREAM frame.
1023 */
1024 if (h2c->dft == H2_FT_RST_STREAM) {
1025 ret = 1;
1026 goto ignore;
1027 }
1028
Willy Tarreau27a84c92017-10-17 08:10:17 +02001029 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001030 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001031 return 0;
1032 }
1033
Willy Tarreau44e973f2018-03-01 17:49:30 +01001034 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001035 if (!res) {
1036 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001037 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001038 return 0;
1039 }
1040
1041 /* len: 4, type: 3, flags: none */
1042 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001043
Willy Tarreau27a84c92017-10-17 08:10:17 +02001044 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001045 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001046 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001047
Willy Tarreau27a84c92017-10-17 08:10:17 +02001048 if (unlikely(ret <= 0)) {
1049 if (!ret) {
1050 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001051 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001052 return 0;
1053 }
1054 else {
1055 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1056 return 0;
1057 }
1058 }
1059
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001060 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001061 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001062 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001063 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001064 }
1065
Willy Tarreau27a84c92017-10-17 08:10:17 +02001066 return ret;
1067}
1068
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001069/* try to send an empty DATA frame with the ES flag set to notify about the
1070 * end of stream and match a shutdown(write). If an ES was already sent as
1071 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1072 * on success or zero if nothing was done. In case of lack of room to write the
1073 * message, it subscribes the requesting stream to future notifications.
1074 */
1075static int h2_send_empty_data_es(struct h2s *h2s)
1076{
1077 struct h2c *h2c = h2s->h2c;
1078 struct buffer *res;
1079 char str[9];
1080 int ret;
1081
Willy Tarreau721c9742017-11-07 11:05:42 +01001082 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001083 return 1;
1084
1085 if (h2c_mux_busy(h2c, h2s)) {
1086 h2s->flags |= H2_SF_BLK_MBUSY;
1087 return 0;
1088 }
1089
Willy Tarreau44e973f2018-03-01 17:49:30 +01001090 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001091 if (!res) {
1092 h2c->flags |= H2_CF_MUX_MALLOC;
1093 h2s->flags |= H2_SF_BLK_MROOM;
1094 return 0;
1095 }
1096
1097 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1098 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1099 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001100 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001101 if (likely(ret > 0)) {
1102 h2s->flags |= H2_SF_ES_SENT;
1103 }
1104 else if (!ret) {
1105 h2c->flags |= H2_CF_MUX_MFULL;
1106 h2s->flags |= H2_SF_BLK_MROOM;
1107 return 0;
1108 }
1109 else {
1110 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1111 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001112 }
1113 return ret;
1114}
1115
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001116/* wake the streams attached to the connection, whose id is greater than <last>,
1117 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001118 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1119 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001120 */
1121static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1122{
1123 struct eb32_node *node;
1124 struct h2s *h2s;
1125
1126 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1127 flags |= CS_FL_ERROR;
1128
1129 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001130 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001131
1132 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1133 while (node) {
1134 h2s = container_of(node, struct h2s, by_id);
1135 if (h2s->id <= last)
1136 break;
1137 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001138
1139 if (!h2s->cs) {
1140 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001141 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001142 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001143 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001144
1145 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001146 if (h2s->recv_wait) {
1147 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001148 sw->wait_reason &= ~SUB_CAN_RECV;
1149 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001150 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001151 } else if (h2s->cs->data_cb->wake != NULL)
1152 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001153
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001154 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1155 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001156 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001157 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001158 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001159 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001160 }
1161}
1162
Willy Tarreau3421aba2017-07-27 15:41:03 +02001163/* Increase all streams' outgoing window size by the difference passed in
1164 * argument. This is needed upon receipt of the settings frame if the initial
1165 * window size is different. The difference may be negative and the resulting
1166 * window size as well, for the time it takes to receive some window updates.
1167 */
1168static void h2c_update_all_ws(struct h2c *h2c, int diff)
1169{
1170 struct h2s *h2s;
1171 struct eb32_node *node;
1172
1173 if (!diff)
1174 return;
1175
1176 node = eb32_first(&h2c->streams_by_id);
1177 while (node) {
1178 h2s = container_of(node, struct h2s, by_id);
1179 h2s->mws += diff;
1180 node = eb32_next(node);
1181 }
1182}
1183
1184/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1185 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1186 * return an error in h2c. Described in RFC7540#6.5.
1187 */
1188static int h2c_handle_settings(struct h2c *h2c)
1189{
1190 unsigned int offset;
1191 int error;
1192
1193 if (h2c->dff & H2_F_SETTINGS_ACK) {
1194 if (h2c->dfl) {
1195 error = H2_ERR_FRAME_SIZE_ERROR;
1196 goto fail;
1197 }
1198 return 1;
1199 }
1200
1201 if (h2c->dsi != 0) {
1202 error = H2_ERR_PROTOCOL_ERROR;
1203 goto fail;
1204 }
1205
1206 if (h2c->dfl % 6) {
1207 error = H2_ERR_FRAME_SIZE_ERROR;
1208 goto fail;
1209 }
1210
1211 /* that's the limit we can process */
1212 if (h2c->dfl > global.tune.bufsize) {
1213 error = H2_ERR_FRAME_SIZE_ERROR;
1214 goto fail;
1215 }
1216
1217 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001218 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001219 return 0;
1220
1221 /* parse the frame */
1222 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001223 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1224 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001225
1226 switch (type) {
1227 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1228 /* we need to update all existing streams with the
1229 * difference from the previous iws.
1230 */
1231 if (arg < 0) { // RFC7540#6.5.2
1232 error = H2_ERR_FLOW_CONTROL_ERROR;
1233 goto fail;
1234 }
1235 h2c_update_all_ws(h2c, arg - h2c->miw);
1236 h2c->miw = arg;
1237 break;
1238 case H2_SETTINGS_MAX_FRAME_SIZE:
1239 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1240 error = H2_ERR_PROTOCOL_ERROR;
1241 goto fail;
1242 }
1243 h2c->mfs = arg;
1244 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001245 case H2_SETTINGS_ENABLE_PUSH:
1246 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1247 error = H2_ERR_PROTOCOL_ERROR;
1248 goto fail;
1249 }
1250 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001251 }
1252 }
1253
1254 /* need to ACK this frame now */
1255 h2c->st0 = H2_CS_FRAME_A;
1256 return 1;
1257 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001258 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001259 h2c_error(h2c, error);
1260 return 0;
1261}
1262
1263/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1264 * success or one of the h2_status values.
1265 */
1266static int h2c_ack_settings(struct h2c *h2c)
1267{
1268 struct buffer *res;
1269 char str[9];
1270 int ret = -1;
1271
1272 if (h2c_mux_busy(h2c, NULL)) {
1273 h2c->flags |= H2_CF_DEM_MBUSY;
1274 return 0;
1275 }
1276
Willy Tarreau44e973f2018-03-01 17:49:30 +01001277 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001278 if (!res) {
1279 h2c->flags |= H2_CF_MUX_MALLOC;
1280 h2c->flags |= H2_CF_DEM_MROOM;
1281 return 0;
1282 }
1283
1284 memcpy(str,
1285 "\x00\x00\x00" /* length : 0 (no data) */
1286 "\x04" "\x01" /* type : 4, flags : ACK */
1287 "\x00\x00\x00\x00" /* stream ID */, 9);
1288
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001289 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001290 if (unlikely(ret <= 0)) {
1291 if (!ret) {
1292 h2c->flags |= H2_CF_MUX_MFULL;
1293 h2c->flags |= H2_CF_DEM_MROOM;
1294 return 0;
1295 }
1296 else {
1297 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1298 return 0;
1299 }
1300 }
1301 return ret;
1302}
1303
Willy Tarreaucf68c782017-10-10 17:11:41 +02001304/* processes a PING frame and schedules an ACK if needed. The caller must pass
1305 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1306 * missing data. It may return an error in h2c.
1307 */
1308static int h2c_handle_ping(struct h2c *h2c)
1309{
1310 /* frame length must be exactly 8 */
1311 if (h2c->dfl != 8) {
1312 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1313 return 0;
1314 }
1315
1316 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001317 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001318 h2c->st0 = H2_CS_FRAME_A;
1319 return 1;
1320}
1321
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001322/* Try to send a window update for stream id <sid> and value <increment>.
1323 * Returns > 0 on success or zero on missing room or failure. It may return an
1324 * error in h2c.
1325 */
1326static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1327{
1328 struct buffer *res;
1329 char str[13];
1330 int ret = -1;
1331
1332 if (h2c_mux_busy(h2c, NULL)) {
1333 h2c->flags |= H2_CF_DEM_MBUSY;
1334 return 0;
1335 }
1336
Willy Tarreau44e973f2018-03-01 17:49:30 +01001337 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001338 if (!res) {
1339 h2c->flags |= H2_CF_MUX_MALLOC;
1340 h2c->flags |= H2_CF_DEM_MROOM;
1341 return 0;
1342 }
1343
1344 /* length: 4, type: 8, flags: none */
1345 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1346 write_n32(str + 5, sid);
1347 write_n32(str + 9, increment);
1348
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001349 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001350
1351 if (unlikely(ret <= 0)) {
1352 if (!ret) {
1353 h2c->flags |= H2_CF_MUX_MFULL;
1354 h2c->flags |= H2_CF_DEM_MROOM;
1355 return 0;
1356 }
1357 else {
1358 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1359 return 0;
1360 }
1361 }
1362 return ret;
1363}
1364
1365/* try to send pending window update for the connection. It's safe to call it
1366 * with no pending updates. Returns > 0 on success or zero on missing room or
1367 * failure. It may return an error in h2c.
1368 */
1369static int h2c_send_conn_wu(struct h2c *h2c)
1370{
1371 int ret = 1;
1372
1373 if (h2c->rcvd_c <= 0)
1374 return 1;
1375
1376 /* send WU for the connection */
1377 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1378 if (ret > 0)
1379 h2c->rcvd_c = 0;
1380
1381 return ret;
1382}
1383
1384/* try to send pending window update for the current dmux stream. It's safe to
1385 * call it with no pending updates. Returns > 0 on success or zero on missing
1386 * room or failure. It may return an error in h2c.
1387 */
1388static int h2c_send_strm_wu(struct h2c *h2c)
1389{
1390 int ret = 1;
1391
1392 if (h2c->rcvd_s <= 0)
1393 return 1;
1394
1395 /* send WU for the stream */
1396 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1397 if (ret > 0)
1398 h2c->rcvd_s = 0;
1399
1400 return ret;
1401}
1402
Willy Tarreaucf68c782017-10-10 17:11:41 +02001403/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1404 * success, 0 on missing data or one of the h2_status values.
1405 */
1406static int h2c_ack_ping(struct h2c *h2c)
1407{
1408 struct buffer *res;
1409 char str[17];
1410 int ret = -1;
1411
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001412 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001413 return 0;
1414
1415 if (h2c_mux_busy(h2c, NULL)) {
1416 h2c->flags |= H2_CF_DEM_MBUSY;
1417 return 0;
1418 }
1419
Willy Tarreau44e973f2018-03-01 17:49:30 +01001420 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001421 if (!res) {
1422 h2c->flags |= H2_CF_MUX_MALLOC;
1423 h2c->flags |= H2_CF_DEM_MROOM;
1424 return 0;
1425 }
1426
1427 memcpy(str,
1428 "\x00\x00\x08" /* length : 8 (same payload) */
1429 "\x06" "\x01" /* type : 6, flags : ACK */
1430 "\x00\x00\x00\x00" /* stream ID */, 9);
1431
1432 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001433 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001434
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001435 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001436 if (unlikely(ret <= 0)) {
1437 if (!ret) {
1438 h2c->flags |= H2_CF_MUX_MFULL;
1439 h2c->flags |= H2_CF_DEM_MROOM;
1440 return 0;
1441 }
1442 else {
1443 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1444 return 0;
1445 }
1446 }
1447 return ret;
1448}
1449
Willy Tarreau26f95952017-07-27 17:18:30 +02001450/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1451 * Returns > 0 on success or zero on missing data. It may return an error in
1452 * h2c or h2s. Described in RFC7540#6.9.
1453 */
1454static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1455{
1456 int32_t inc;
1457 int error;
1458
1459 if (h2c->dfl != 4) {
1460 error = H2_ERR_FRAME_SIZE_ERROR;
1461 goto conn_err;
1462 }
1463
1464 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001465 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001466 return 0;
1467
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001468 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001469
1470 if (h2c->dsi != 0) {
1471 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001472
1473 /* it's not an error to receive WU on a closed stream */
1474 if (h2s->st == H2_SS_CLOSED)
1475 return 1;
1476
1477 if (!inc) {
1478 error = H2_ERR_PROTOCOL_ERROR;
1479 goto strm_err;
1480 }
1481
1482 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1483 error = H2_ERR_FLOW_CONTROL_ERROR;
1484 goto strm_err;
1485 }
1486
1487 h2s->mws += inc;
1488 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1489 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001490 if (h2s->send_wait)
1491 LIST_ADDQ(&h2c->send_list, &h2s->list);
1492
Willy Tarreau26f95952017-07-27 17:18:30 +02001493 }
1494 }
1495 else {
1496 /* connection window update */
1497 if (!inc) {
1498 error = H2_ERR_PROTOCOL_ERROR;
1499 goto conn_err;
1500 }
1501
1502 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1503 error = H2_ERR_FLOW_CONTROL_ERROR;
1504 goto conn_err;
1505 }
1506
1507 h2c->mws += inc;
1508 }
1509
1510 return 1;
1511
1512 conn_err:
1513 h2c_error(h2c, error);
1514 return 0;
1515
1516 strm_err:
1517 if (h2s) {
1518 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001519 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001520 }
1521 else
1522 h2c_error(h2c, error);
1523 return 0;
1524}
1525
Willy Tarreaue96b0922017-10-30 00:28:29 +01001526/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1527 * the last ID. Returns > 0 on success or zero on missing data. It may return
1528 * an error in h2c. Described in RFC7540#6.8.
1529 */
1530static int h2c_handle_goaway(struct h2c *h2c)
1531{
1532 int error;
1533 int last;
1534
1535 if (h2c->dsi != 0) {
1536 error = H2_ERR_PROTOCOL_ERROR;
1537 goto conn_err;
1538 }
1539
1540 if (h2c->dfl < 8) {
1541 error = H2_ERR_FRAME_SIZE_ERROR;
1542 goto conn_err;
1543 }
1544
1545 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001546 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001547 return 0;
1548
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001549 last = h2_get_n32(&h2c->dbuf, 0);
1550 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001551 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001552 if (h2c->last_sid < 0)
1553 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001554 return 1;
1555
1556 conn_err:
1557 h2c_error(h2c, error);
1558 return 0;
1559}
1560
Willy Tarreau92153fc2017-12-03 19:46:19 +01001561/* processes a PRIORITY frame, and either skips it or rejects if it is
1562 * invalid. Returns > 0 on success or zero on missing data. It may return
1563 * an error in h2c. Described in RFC7540#6.3.
1564 */
1565static int h2c_handle_priority(struct h2c *h2c)
1566{
1567 int error;
1568
1569 if (h2c->dsi == 0) {
1570 error = H2_ERR_PROTOCOL_ERROR;
1571 goto conn_err;
1572 }
1573
1574 if (h2c->dfl != 5) {
1575 error = H2_ERR_FRAME_SIZE_ERROR;
1576 goto conn_err;
1577 }
1578
1579 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001580 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001581 return 0;
1582
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001583 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001584 /* 7540#5.3 : can't depend on itself */
1585 error = H2_ERR_PROTOCOL_ERROR;
1586 goto conn_err;
1587 }
1588 return 1;
1589
1590 conn_err:
1591 h2c_error(h2c, error);
1592 return 0;
1593}
1594
Willy Tarreaucd234e92017-08-18 10:59:39 +02001595/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1596 * Returns > 0 on success or zero on missing data. It may return an error in
1597 * h2c. Described in RFC7540#6.4.
1598 */
1599static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1600{
1601 int error;
1602
1603 if (h2c->dsi == 0) {
1604 error = H2_ERR_PROTOCOL_ERROR;
1605 goto conn_err;
1606 }
1607
Willy Tarreaucd234e92017-08-18 10:59:39 +02001608 if (h2c->dfl != 4) {
1609 error = H2_ERR_FRAME_SIZE_ERROR;
1610 goto conn_err;
1611 }
1612
1613 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001614 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001615 return 0;
1616
1617 /* late RST, already handled */
1618 if (h2s->st == H2_SS_CLOSED)
1619 return 1;
1620
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001621 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001622 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001623
1624 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001625 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001626 if (h2s->recv_wait) {
1627 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001628
1629 sw->wait_reason &= ~SUB_CAN_RECV;
1630 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001631 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001632 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001633 }
1634
1635 h2s->flags |= H2_SF_RST_RCVD;
1636 return 1;
1637
1638 conn_err:
1639 h2c_error(h2c, error);
1640 return 0;
1641}
1642
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001643/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1644 * It may return an error in h2c or h2s. The caller must consider that the
1645 * return value is the new h2s in case one was allocated (most common case).
1646 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001647 * errors here are reported as connection errors since it's impossible to
1648 * recover from such errors after the compression context has been altered.
1649 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001650static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001651{
1652 int error;
1653
1654 if (!h2c->dfl) {
1655 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001656 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001657 goto strm_err;
1658 }
1659
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001660 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001661 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001664 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001665
Willy Tarreauf2101912018-07-19 10:11:38 +02001666 if (h2c->flags & H2_CF_DEM_TOOMANY)
1667 return 0; // too many cs still present
1668
Willy Tarreau13278b42017-10-13 19:23:14 +02001669 /* now either the frame is complete or the buffer is complete */
1670 if (h2s->st != H2_SS_IDLE) {
1671 /* FIXME: stream already exists, this is only allowed for
1672 * trailers (not supported for now).
1673 */
1674 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001675 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001676 goto conn_err;
1677 }
1678 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1679 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1680 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001681 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001682 goto conn_err;
1683 }
1684
Willy Tarreau22de8d32018-09-05 19:55:58 +02001685 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001686 * positively from h2c_frt_stream_new(), the stream will report the error,
1687 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001688 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001689 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001690 if (!h2s) {
1691 error = H2_ERR_INTERNAL_ERROR;
1692 goto conn_err;
1693 }
1694
1695 h2s->st = H2_SS_OPEN;
1696 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1697 h2s->st = H2_SS_HREM;
1698 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001699 /* note: cs cannot be null for now (just created above) */
1700 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001701 }
1702
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001703 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001704 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001705
Willy Tarreau8f650c32017-11-21 19:36:21 +01001706 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001707 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001708
Willy Tarreau721c9742017-11-07 11:05:42 +01001709 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001710 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001711 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001712 }
1713 else {
1714 /* update the max stream ID if the request is being processed */
1715 if (h2s->id > h2c->max_id)
1716 h2c->max_id = h2s->id;
1717 }
1718
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001719 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001720
1721 conn_err:
1722 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001723 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001724
1725 strm_err:
1726 if (h2s) {
1727 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001728 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001729 }
1730 else
1731 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001732 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001733}
1734
Willy Tarreau454f9052017-10-26 19:40:35 +02001735/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1736 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1737 */
1738static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1739{
1740 int error;
1741
1742 /* note that empty DATA frames are perfectly valid and sometimes used
1743 * to signal an end of stream (with the ES flag).
1744 */
1745
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001746 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001747 return 0; // empty buffer
1748
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001749 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001750 return 0; // incomplete frame
1751
1752 /* now either the frame is complete or the buffer is complete */
1753
1754 if (!h2c->dsi) {
1755 /* RFC7540#6.1 */
1756 error = H2_ERR_PROTOCOL_ERROR;
1757 goto conn_err;
1758 }
1759
1760 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1761 /* RFC7540#6.1 */
1762 error = H2_ERR_STREAM_CLOSED;
1763 goto strm_err;
1764 }
1765
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001766 if (!h2_frt_transfer_data(h2s))
1767 return 0;
1768
Willy Tarreau454f9052017-10-26 19:40:35 +02001769 /* call the upper layers to process the frame, then let the upper layer
1770 * notify the stream about any change.
1771 */
1772 if (!h2s->cs) {
1773 error = H2_ERR_STREAM_CLOSED;
1774 goto strm_err;
1775 }
1776
Willy Tarreau8f650c32017-11-21 19:36:21 +01001777 if (h2c->st0 >= H2_CS_ERROR)
1778 return 0;
1779
Willy Tarreau721c9742017-11-07 11:05:42 +01001780 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001781 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001782 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001783 }
1784
1785 /* check for completion : the callee will change this to FRAME_A or
1786 * FRAME_H once done.
1787 */
1788 if (h2c->st0 == H2_CS_FRAME_P)
1789 return 0;
1790
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001791
1792 /* last frame */
1793 if (h2c->dff & H2_F_DATA_END_STREAM) {
1794 h2s->st = H2_SS_HREM;
1795 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001796 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001797 }
1798
Willy Tarreau454f9052017-10-26 19:40:35 +02001799 return 1;
1800
1801 conn_err:
1802 h2c_error(h2c, error);
1803 return 0;
1804
1805 strm_err:
1806 if (h2s) {
1807 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001808 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001809 }
1810 else
1811 h2c_error(h2c, error);
1812 return 0;
1813}
1814
Willy Tarreaubc933932017-10-09 16:21:43 +02001815/* process Rx frames to be demultiplexed */
1816static void h2_process_demux(struct h2c *h2c)
1817{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001818 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001819
Willy Tarreau081d4722017-05-16 21:51:05 +02001820 if (h2c->st0 >= H2_CS_ERROR)
1821 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001822
1823 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1824 if (h2c->st0 == H2_CS_PREFACE) {
1825 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1826 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001827 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001828 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001829 sess_log(h2c->conn->owner);
1830 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001831 goto fail;
1832 }
1833
1834 h2c->max_id = 0;
1835 h2c->st0 = H2_CS_SETTINGS1;
1836 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001837
1838 if (h2c->st0 == H2_CS_SETTINGS1) {
1839 struct h2_fh hdr;
1840
1841 /* ensure that what is pending is a valid SETTINGS frame
1842 * without an ACK.
1843 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001845 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001846 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001847 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001848 sess_log(h2c->conn->owner);
1849 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001850 goto fail;
1851 }
1852
1853 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1854 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1855 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1856 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001857 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001858 goto fail;
1859 }
1860
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001861 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001862 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1863 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1864 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001865 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001866 goto fail;
1867 }
1868
1869 /* that's OK, switch to FRAME_P to process it */
1870 h2c->dfl = hdr.len;
1871 h2c->dsi = hdr.sid;
1872 h2c->dft = hdr.ft;
1873 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001874 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001875 h2c->st0 = H2_CS_FRAME_P;
1876 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001877 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001878
1879 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001880 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001881 int ret = 0;
1882
1883 if (h2c->st0 >= H2_CS_ERROR)
1884 break;
1885
1886 if (h2c->st0 == H2_CS_FRAME_H) {
1887 struct h2_fh hdr;
1888
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001889 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001890 break;
1891
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001892 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001893 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1894 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001895 if (!h2c->nb_streams) {
1896 /* only log if no other stream can report the error */
1897 sess_log(h2c->conn->owner);
1898 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001899 break;
1900 }
1901
1902 h2c->dfl = hdr.len;
1903 h2c->dsi = hdr.sid;
1904 h2c->dft = hdr.ft;
1905 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001906 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001907 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001908 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001909 }
1910
1911 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1913
Olivier Houchard638b7992018-08-16 15:41:52 +02001914 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001915 /* we may have to signal the upper layers */
1916 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001917 if (h2s->recv_wait) {
1918 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1919 tasklet_wakeup(h2s->recv_wait->task);
1920 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001921 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 if (h2c->st0 >= H2_CS_ERROR)
1923 goto strm_err;
1924
1925 if (h2s->st >= H2_SS_ERROR) {
1926 /* stream error : send RST_STREAM */
1927 h2c->st0 = H2_CS_FRAME_E;
1928 }
1929 }
1930 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001931
Willy Tarreaud7901432017-12-29 11:34:40 +01001932 if (h2c->st0 == H2_CS_FRAME_E)
1933 goto strm_err;
1934
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001935 if (h2s->st == H2_SS_IDLE &&
1936 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1937 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1938 * this state MUST be treated as a connection error
1939 */
1940 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1941 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001942 if (!h2c->nb_streams) {
1943 /* only log if no other stream can report the error */
1944 sess_log(h2c->conn->owner);
1945 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001946 break;
1947 }
1948
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001949 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1950 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1951 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1952 * this state MUST be treated as a stream error
1953 */
1954 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1955 goto strm_err;
1956 }
1957
Willy Tarreauab837502017-12-27 15:07:30 +01001958 /* Below the management of frames received in closed state is a
1959 * bit hackish because the spec makes strong differences between
1960 * streams closed by receiving RST, sending RST, and seeing ES
1961 * in both directions. In addition to this, the creation of a
1962 * new stream reusing the identifier of a closed one will be
1963 * detected here. Given that we cannot keep track of all closed
1964 * streams forever, we consider that unknown closed streams were
1965 * closed on RST received, which allows us to respond with an
1966 * RST without breaking the connection (eg: to abort a transfer).
1967 * Some frames have to be silently ignored as well.
1968 */
1969 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1970 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1971 /* #5.1.1: The identifier of a newly
1972 * established stream MUST be numerically
1973 * greater than all streams that the initiating
1974 * endpoint has opened or reserved. This
1975 * governs streams that are opened using a
1976 * HEADERS frame and streams that are reserved
1977 * using PUSH_PROMISE. An endpoint that
1978 * receives an unexpected stream identifier
1979 * MUST respond with a connection error.
1980 */
1981 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1982 goto strm_err;
1983 }
1984
1985 if (h2s->flags & H2_SF_RST_RCVD) {
1986 /* RFC7540#5.1:closed: an endpoint that
1987 * receives any frame other than PRIORITY after
1988 * receiving a RST_STREAM MUST treat that as a
1989 * stream error of type STREAM_CLOSED.
1990 *
1991 * Note that old streams fall into this category
1992 * and will lead to an RST being sent.
1993 */
1994 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1995 h2c->st0 = H2_CS_FRAME_E;
1996 goto strm_err;
1997 }
1998
1999 /* RFC7540#5.1:closed: if this state is reached as a
2000 * result of sending a RST_STREAM frame, the peer that
2001 * receives the RST_STREAM might have already sent
2002 * frames on the stream that cannot be withdrawn. An
2003 * endpoint MUST ignore frames that it receives on
2004 * closed streams after it has sent a RST_STREAM
2005 * frame. An endpoint MAY choose to limit the period
2006 * over which it ignores frames and treat frames that
2007 * arrive after this time as being in error.
2008 */
2009 if (!(h2s->flags & H2_SF_RST_SENT)) {
2010 /* RFC7540#5.1:closed: any frame other than
2011 * PRIO/WU/RST in this state MUST be treated as
2012 * a connection error
2013 */
2014 if (h2c->dft != H2_FT_RST_STREAM &&
2015 h2c->dft != H2_FT_PRIORITY &&
2016 h2c->dft != H2_FT_WINDOW_UPDATE) {
2017 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2018 goto strm_err;
2019 }
2020 }
2021 }
2022
Willy Tarreauc0da1962017-10-30 18:38:00 +01002023#if 0
2024 // problem below: it is not possible to completely ignore such
2025 // streams as we need to maintain the compression state as well
2026 // and for this we need to completely process these frames (eg:
2027 // HEADERS frames) as well as counting DATA frames to emit
2028 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2029 // This is a typical case of layer violation where the
2030 // transported contents are critical to the connection's
2031 // validity and must be ignored at the same time :-(
2032
2033 /* graceful shutdown, ignore streams whose ID is higher than
2034 * the one advertised in GOAWAY. RFC7540#6.8.
2035 */
2036 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002037 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2038 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002039 h2c->dfl -= ret;
2040 ret = h2c->dfl == 0;
2041 goto strm_err;
2042 }
2043#endif
2044
Willy Tarreau7e98c052017-10-10 15:56:59 +02002045 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002046 case H2_FT_SETTINGS:
2047 if (h2c->st0 == H2_CS_FRAME_P)
2048 ret = h2c_handle_settings(h2c);
2049
2050 if (h2c->st0 == H2_CS_FRAME_A)
2051 ret = h2c_ack_settings(h2c);
2052 break;
2053
Willy Tarreaucf68c782017-10-10 17:11:41 +02002054 case H2_FT_PING:
2055 if (h2c->st0 == H2_CS_FRAME_P)
2056 ret = h2c_handle_ping(h2c);
2057
2058 if (h2c->st0 == H2_CS_FRAME_A)
2059 ret = h2c_ack_ping(h2c);
2060 break;
2061
Willy Tarreau26f95952017-07-27 17:18:30 +02002062 case H2_FT_WINDOW_UPDATE:
2063 if (h2c->st0 == H2_CS_FRAME_P)
2064 ret = h2c_handle_window_update(h2c, h2s);
2065 break;
2066
Willy Tarreau61290ec2017-10-17 08:19:21 +02002067 case H2_FT_CONTINUATION:
2068 /* we currently don't support CONTINUATION frames since
2069 * we have nowhere to store the partial HEADERS frame.
2070 * Let's abort the stream on an INTERNAL_ERROR here.
2071 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002072 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002073 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002074 h2c->st0 = H2_CS_FRAME_E;
2075 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002076 break;
2077
Willy Tarreau13278b42017-10-13 19:23:14 +02002078 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002079 if (h2c->st0 == H2_CS_FRAME_P) {
2080 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2081 if (tmp_h2s) {
2082 h2s = tmp_h2s;
2083 ret = 1;
2084 }
2085 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002086 break;
2087
Willy Tarreau454f9052017-10-26 19:40:35 +02002088 case H2_FT_DATA:
2089 if (h2c->st0 == H2_CS_FRAME_P)
2090 ret = h2c_frt_handle_data(h2c, h2s);
2091
2092 if (h2c->st0 == H2_CS_FRAME_A)
2093 ret = h2c_send_strm_wu(h2c);
2094 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002095
Willy Tarreau92153fc2017-12-03 19:46:19 +01002096 case H2_FT_PRIORITY:
2097 if (h2c->st0 == H2_CS_FRAME_P)
2098 ret = h2c_handle_priority(h2c);
2099 break;
2100
Willy Tarreaucd234e92017-08-18 10:59:39 +02002101 case H2_FT_RST_STREAM:
2102 if (h2c->st0 == H2_CS_FRAME_P)
2103 ret = h2c_handle_rst_stream(h2c, h2s);
2104 break;
2105
Willy Tarreaue96b0922017-10-30 00:28:29 +01002106 case H2_FT_GOAWAY:
2107 if (h2c->st0 == H2_CS_FRAME_P)
2108 ret = h2c_handle_goaway(h2c);
2109 break;
2110
Willy Tarreau1c661982017-10-30 13:52:01 +01002111 case H2_FT_PUSH_PROMISE:
2112 /* not permitted here, RFC7540#5.1 */
2113 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002114 if (!h2c->nb_streams) {
2115 /* only log if no other stream can report the error */
2116 sess_log(h2c->conn->owner);
2117 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002118 break;
2119
2120 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002121 default:
2122 /* drop frames that we ignore. They may be larger than
2123 * the buffer so we drain all of their contents until
2124 * we reach the end.
2125 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002126 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2127 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002128 h2c->dfl -= ret;
2129 ret = h2c->dfl == 0;
2130 }
2131
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002132 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002133 /* We may have to send an RST if not done yet */
2134 if (h2s->st == H2_SS_ERROR)
2135 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002136
Willy Tarreaua20a5192017-12-27 11:02:06 +01002137 if (h2c->st0 == H2_CS_FRAME_E)
2138 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002139
Willy Tarreau7e98c052017-10-10 15:56:59 +02002140 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002141 if (ret <= 0) {
2142 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002143 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002144 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002145
2146 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002147 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002148 h2c->st0 = H2_CS_FRAME_H;
2149 }
2150 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002151
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002152 if (h2c->rcvd_c > 0 &&
2153 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2154 h2c_send_conn_wu(h2c);
2155
Willy Tarreau52eed752017-09-22 15:05:09 +02002156 fail:
2157 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002158 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002159 /* we may have to signal the upper layers */
2160 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002161 if (h2s->recv_wait) {
2162 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2163 tasklet_wakeup(h2s->recv_wait->task);
2164 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002165 }
2166 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002167 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002168}
2169
2170/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2171 * the end.
2172 */
2173static int h2_process_mux(struct h2c *h2c)
2174{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002175 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002176
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002177 /* start by sending possibly pending window updates */
2178 if (h2c->rcvd_c > 0 &&
2179 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2180 h2c_send_conn_wu(h2c) < 0)
2181 goto fail;
2182
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002183 /* First we always process the flow control list because the streams
2184 * waiting there were already elected for immediate emission but were
2185 * blocked just on this.
2186 */
2187
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002188 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002189 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2190 h2c->st0 >= H2_CS_ERROR)
2191 break;
2192
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002193 h2s->flags &= ~H2_SF_BLK_ANY;
2194 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2195 tasklet_wakeup(h2s->send_wait->task);
2196 h2s->send_wait = NULL;
2197 LIST_DEL(&h2s->list);
2198 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002199 }
2200
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002201 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002202 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2203 break;
2204
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002205 h2s->flags &= ~H2_SF_BLK_ANY;
2206 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2207 tasklet_wakeup(h2s->send_wait->task);
2208 h2s->send_wait = NULL;
2209 LIST_DEL(&h2s->list);
2210 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002211 }
2212
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002213 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002214 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002215 if (h2c->st0 == H2_CS_ERROR) {
2216 if (h2c->max_id >= 0) {
2217 h2c_send_goaway_error(h2c, NULL);
2218 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2219 return 0;
2220 }
2221
2222 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2223 }
2224 return 1;
2225 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002226 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002227}
2228
Willy Tarreau62f52692017-10-08 23:01:42 +02002229
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002230/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002231static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002232{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002233 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002234 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002236 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002237
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002238 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002239 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002240
Willy Tarreau315d8072017-12-10 22:17:57 +01002241 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002242 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002243
Willy Tarreau44e973f2018-03-01 17:49:30 +01002244 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002245 if (!buf) {
2246 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002247 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002248 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002249
Olivier Houchard7505f942018-08-21 18:10:44 +02002250 do {
2251 max = buf->size - b_data(buf);
2252 if (max)
2253 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2254 else
2255 ret = 0;
2256 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002257
Olivier Houchard7505f942018-08-21 18:10:44 +02002258 if (h2_recv_allowed(h2c)) {
2259 conn_xprt_want_recv(conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002260 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard7505f942018-08-21 18:10:44 +02002261 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002262 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002263 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002264 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002265 }
2266
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002267 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002268 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002269 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002270}
2271
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002272/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002273static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002274{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002275 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002276 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002277 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002278
2279 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002280 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002281
Olivier Houchard7505f942018-08-21 18:10:44 +02002282
Willy Tarreaua2af5122017-10-09 11:56:46 +02002283 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2284 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002285 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002286 }
2287
Willy Tarreaubc933932017-10-09 16:21:43 +02002288 /* This loop is quite simple : it tries to fill as much as it can from
2289 * pending streams into the existing buffer until it's reportedly full
2290 * or the end of send requests is reached. Then it tries to send this
2291 * buffer's contents out, marks it not full if at least one byte could
2292 * be sent, and tries again.
2293 *
2294 * The snd_buf() function normally takes a "flags" argument which may
2295 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2296 * data immediately comes and CO_SFL_STREAMER to indicate that the
2297 * connection is streaming lots of data (used to increase TLS record
2298 * size at the expense of latency). The former can be sent any time
2299 * there's a buffer full flag, as it indicates at least one stream
2300 * attempted to send and failed so there are pending data. An
2301 * alternative would be to set it as long as there's an active stream
2302 * but that would be problematic for ACKs until we have an absolute
2303 * guarantee that all waiters have at least one byte to send. The
2304 * latter should possibly not be set for now.
2305 */
2306
2307 done = 0;
2308 while (!done) {
2309 unsigned int flags = 0;
2310
2311 /* fill as much as we can into the current buffer */
2312 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2313 done = h2_process_mux(h2c);
2314
2315 if (conn->flags & CO_FL_ERROR)
2316 break;
2317
2318 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2319 flags |= CO_SFL_MSG_MORE;
2320
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002321 if (b_data(&h2c->mbuf)) {
2322 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002323 if (!ret)
2324 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002325 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 b_del(&h2c->mbuf, ret);
2327 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002328 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002329
2330 /* wrote at least one byte, the buffer is not full anymore */
2331 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2332 }
2333
Willy Tarreaua2af5122017-10-09 11:56:46 +02002334 if (conn->flags & CO_FL_SOCK_WR_SH) {
2335 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002336 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002337 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002338 /* We're not full anymore, so we can wake any task that are waiting
2339 * for us.
2340 */
2341 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002342 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002343 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2344 struct h2s *, list);
2345 LIST_DEL(&h2s->list);
2346 LIST_INIT(&h2s->list);
2347 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2348 tasklet_wakeup(h2s->send_wait->task);
2349 h2s->send_wait = NULL;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002350 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002351 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002352 /* We're done, no more to send */
2353 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002354 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002355schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002356 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2357 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002358 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002359}
2360
2361static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2362{
2363 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002364 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002365
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002366 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002367 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002368 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002369 ret |= h2_recv(h2c);
2370 if (ret)
2371 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002372 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002373}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002374
Willy Tarreau62f52692017-10-08 23:01:42 +02002375/* callback called on any event by the connection handler.
2376 * It applies changes and returns zero, or < 0 if it wants immediate
2377 * destruction of the connection (which normally doesn not happen in h2).
2378 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002379static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002380{
Olivier Houchard7505f942018-08-21 18:10:44 +02002381 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002382
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002383 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002384 h2_process_demux(h2c);
2385
2386 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002387 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002388
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002389 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002390 h2c->flags &= ~H2_CF_DEM_DFULL;
2391 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002392 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002393
Willy Tarreau0b37d652018-10-03 10:33:02 +02002394 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002395 /* frontend is stopping, reload likely in progress, let's try
2396 * to announce a graceful shutdown if not yet done. We don't
2397 * care if it fails, it will be tried again later.
2398 */
2399 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2400 if (h2c->last_sid < 0)
2401 h2c->last_sid = (1U << 31) - 1;
2402 h2c_send_goaway_error(h2c, NULL);
2403 }
2404 }
2405
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002406 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002407 * If we received early data, and the handshake is done, wake
2408 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002409 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002410 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2411 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2412 struct eb32_node *node;
2413 struct h2s *h2s;
2414
2415 h2c->flags |= H2_CF_WAIT_FOR_HS;
2416 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2417
2418 while (node) {
2419 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002420 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002421 h2s->recv_wait) {
2422 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002423 sw->wait_reason &= ~SUB_CAN_RECV;
2424 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002425 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002426 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002427 node = eb32_next(node);
2428 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002429 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002430
Willy Tarreau26bd7612017-10-09 16:47:04 +02002431 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002432 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2433 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2434 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002435 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002436
2437 if (eb_is_empty(&h2c->streams_by_id)) {
2438 /* no more stream, kill the connection now */
2439 h2_release(conn);
2440 return -1;
2441 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442 }
2443
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002444 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002445 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002446
2447 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002448 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002449 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002450 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002451 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002452
2453 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002454 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002455 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002456 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002457 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002458 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2459 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002460 __conn_xprt_want_send(conn);
2461 }
2462 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002463 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002464 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002465 }
2466
Willy Tarreau3f133572017-10-31 19:21:06 +01002467 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002468 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002469 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002470 task_queue(h2c->task);
2471 }
2472 else
2473 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002474 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002475
Olivier Houchard7505f942018-08-21 18:10:44 +02002476 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002477 return 0;
2478}
2479
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002480static int h2_wake(struct connection *conn)
2481{
2482 struct h2c *h2c = conn->mux_ctx;
2483
2484 return (h2_process(h2c));
2485}
2486
2487
Willy Tarreauea392822017-10-31 10:02:25 +01002488/* Connection timeout management. The principle is that if there's no receipt
2489 * nor sending for a certain amount of time, the connection is closed. If the
2490 * MUX buffer still has lying data or is not allocatable, the connection is
2491 * immediately killed. If it's allocatable and empty, we attempt to send a
2492 * GOAWAY frame.
2493 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002494static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002495{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002496 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002497 int expired = tick_is_expired(t->expire, now_ms);
2498
Willy Tarreau0975f112018-03-29 15:22:59 +02002499 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002500 return t;
2501
Willy Tarreau0975f112018-03-29 15:22:59 +02002502 task_delete(t);
2503 task_free(t);
2504
2505 if (!h2c) {
2506 /* resources were already deleted */
2507 return NULL;
2508 }
2509
2510 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002511 h2c_error(h2c, H2_ERR_NO_ERROR);
2512 h2_wake_some_streams(h2c, 0, 0);
2513
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002515 /* don't even try to send a GOAWAY, the buffer is stuck */
2516 h2c->flags |= H2_CF_GOAWAY_FAILED;
2517 }
2518
2519 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002520 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002521 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2522 h2c->flags |= H2_CF_GOAWAY_FAILED;
2523
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2525 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002526 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002527 b_del(&h2c->mbuf, ret);
2528 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002529 }
2530 }
Willy Tarreauea392822017-10-31 10:02:25 +01002531
Willy Tarreau0975f112018-03-29 15:22:59 +02002532 /* either we can release everything now or it will be done later once
2533 * the last stream closes.
2534 */
2535 if (eb_is_empty(&h2c->streams_by_id))
2536 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002537
Willy Tarreauea392822017-10-31 10:02:25 +01002538 return NULL;
2539}
2540
2541
Willy Tarreau62f52692017-10-08 23:01:42 +02002542/*******************************************/
2543/* functions below are used by the streams */
2544/*******************************************/
2545
2546/*
2547 * Attach a new stream to a connection
2548 * (Used for outgoing connections)
2549 */
2550static struct conn_stream *h2_attach(struct connection *conn)
2551{
2552 return NULL;
2553}
2554
2555/* callback used to update the mux's polling flags after changing a cs' status.
2556 * The caller (cs_update_mux_polling) will take care of propagating any changes
2557 * to the transport layer.
2558 */
2559static void h2_update_poll(struct conn_stream *cs)
2560{
Willy Tarreau1d393222017-10-17 10:26:19 +02002561 struct h2s *h2s = cs->ctx;
2562
2563 if (!h2s)
2564 return;
2565
Willy Tarreaud7739c82017-10-30 15:38:23 +01002566 /* we may unblock a blocked read */
2567
Willy Tarreau315d8072017-12-10 22:17:57 +01002568 if (cs->flags & CS_FL_DATA_RD_ENA) {
2569 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002570 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002571 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002572 conn_xprt_want_recv(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002573 tasklet_wakeup(h2s->h2c->wait_event.task);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002574 conn_xprt_want_send(cs->conn);
2575 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002576 }
2577
Willy Tarreau1d393222017-10-17 10:26:19 +02002578 /* Note: the stream and stream-int code doesn't allow us to perform a
2579 * synchronous send() here unfortunately, because this code is called
2580 * as si_update() from the process_stream() context. This means that
2581 * we have to queue the current cs and defer its processing after the
2582 * connection's cs list is processed anyway.
2583 */
2584
2585 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002586 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2587 conn_xprt_want_send(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002588 tasklet_wakeup(h2s->h2c->wait_event.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002589 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002590 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002591
2592 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002593 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002594 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002595}
2596
2597/*
2598 * Detach the stream from the connection and possibly release the connection.
2599 */
2600static void h2_detach(struct conn_stream *cs)
2601{
Willy Tarreau60935142017-10-16 18:11:19 +02002602 struct h2s *h2s = cs->ctx;
2603 struct h2c *h2c;
2604
2605 cs->ctx = NULL;
2606 if (!h2s)
2607 return;
2608
2609 h2c = h2s->h2c;
2610 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002611 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002612 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2613 !h2_has_too_many_cs(h2c)) {
2614 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2615 if (h2_recv_allowed(h2c)) {
2616 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002618 conn_xprt_want_send(h2c->conn);
2619 }
2620 }
Willy Tarreau60935142017-10-16 18:11:19 +02002621
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002622 /* this stream may be blocked waiting for some data to leave (possibly
2623 * an ES or RST frame), so orphan it in this case.
2624 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002625 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002626 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002627 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002628 return;
2629
Willy Tarreau45f752e2017-10-30 15:44:59 +01002630 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2631 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2632 /* unblock the connection if it was blocked on this
2633 * stream.
2634 */
2635 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2636 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2637 conn_xprt_want_recv(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002638 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002639 conn_xprt_want_send(cs->conn);
2640 }
2641
Willy Tarreau71049cc2018-03-28 13:56:39 +02002642 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002643
Willy Tarreaue323f342018-03-28 13:51:45 +02002644 /* We don't want to close right now unless we're removing the
2645 * last stream, and either the connection is in error, or it
2646 * reached the ID already specified in a GOAWAY frame received
2647 * or sent (as seen by last_sid >= 0).
2648 */
2649 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2650 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002651 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002652 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002653 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002654 (conn_xprt_read0_pending(h2c->conn) ||
2655 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2656 /* no more stream will come, kill it now */
2657 h2_release(h2c->conn);
2658 }
2659 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002660 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002661 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2662 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002663 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002664 else
2665 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002666 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002667}
2668
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002669static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002670{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002672 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002673
Willy Tarreau721c9742017-11-07 11:05:42 +01002674 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002675 return;
2676
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002677 /* if no outgoing data was seen on this stream, it means it was
2678 * closed with a "tcp-request content" rule that is normally
2679 * used to kill the connection ASAP (eg: limit abuse). In this
2680 * case we send a goaway to close the connection.
2681 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002682 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002683 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002684 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002685
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002686 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2687 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002688 h2c_send_goaway_error(h2c, h2s) <= 0)
2689 return;
2690 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2691 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002692
Willy Tarreau00dd0782018-03-01 16:31:34 +01002693 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002694
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002695 return;
2696add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002697 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002698 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002699 if (h2s->flags & H2_SF_BLK_MFCTL) {
2700 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2701 h2s->send_wait = sw;
2702 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2703 h2s->send_wait = sw;
2704 LIST_ADDQ(&h2c->send_list, &h2s->list);
2705 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002706 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002707 /* Let the handler know we want shutr */
2708 sw->handle = (void *)((long)sw->handle | 1);
2709
Willy Tarreau62f52692017-10-08 23:01:42 +02002710}
2711
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002712static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002713{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002714 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002715 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002716
Willy Tarreau721c9742017-11-07 11:05:42 +01002717 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002718 return;
2719
Willy Tarreau67434202017-11-06 20:20:51 +01002720 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002721 /* we can cleanly close using an empty data frame only after headers */
2722
2723 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2724 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002725 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002726
2727 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002728 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002729 else
2730 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002731 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002732 /* if no outgoing data was seen on this stream, it means it was
2733 * closed with a "tcp-request content" rule that is normally
2734 * used to kill the connection ASAP (eg: limit abuse). In this
2735 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002736 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002737 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002738 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002739 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002740
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002741 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2742 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002743 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002744 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002745
Willy Tarreau00dd0782018-03-01 16:31:34 +01002746 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002747 }
2748
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002749 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2750 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002751
2752 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002753 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002754 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002755 if (h2s->flags & H2_SF_BLK_MFCTL) {
2756 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2757 h2s->send_wait = sw;
2758 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2759 h2s->send_wait = sw;
2760 LIST_ADDQ(&h2c->send_list, &h2s->list);
2761 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002762 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002763 /* let the handler know we want to shutw */
2764 sw->handle = (void *)((long)(sw->handle) | 2);
2765
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002766}
2767
2768static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2769{
2770 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002771 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002772
2773 if (reason & 1)
2774 h2_do_shutr(h2s);
2775 if (reason & 2)
2776 h2_do_shutw(h2s);
2777
2778 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002779}
2780
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002781static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2782{
2783 struct h2s *h2s = cs->ctx;
2784
2785 if (!mode)
2786 return;
2787
2788 h2_do_shutr(h2s);
2789}
2790
2791static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2792{
2793 struct h2s *h2s = cs->ctx;
2794
2795 h2_do_shutw(h2s);
2796}
2797
Willy Tarreau13278b42017-10-13 19:23:14 +02002798/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2799 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2800 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002801 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002802 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002803static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002804{
2805 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002807 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002808 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002809 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002810 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002811 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002812 int flen = h2c->dfl;
2813 int outlen = 0;
2814 int wrap;
2815 int try;
2816
2817 if (!h2c->dfl) {
2818 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002819 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002820 return 0;
2821 }
2822
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002823 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002824 return 0; // incomplete input frame
2825
Willy Tarreau13278b42017-10-13 19:23:14 +02002826 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002827 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002828 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002829 copy = alloc_trash_chunk();
2830 if (!copy) {
2831 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2832 goto fail;
2833 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2835 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2836 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002837 }
2838
2839 /* The padlen is the first byte before data, and the padding appears
2840 * after data. padlen+data+padding are included in flen.
2841 */
2842 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002843 h2c->dpl = *hdrs;
2844 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002845 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2846 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002847 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002848 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002849 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002850 hdrs += 1; // skip Pad Length
2851 }
2852
2853 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2854 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002855 if (read_n32(hdrs) == h2s->id) {
2856 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2857 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002858 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002859 }
2860
Willy Tarreau13278b42017-10-13 19:23:14 +02002861 hdrs += 5; // stream dep = 4, weight = 1
2862 flen -= 5;
2863 }
2864
2865 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2866 * don't support this for now and can't even decompress so we have to
2867 * break the connection.
2868 */
2869 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2870 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002871 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002872 }
2873
Olivier Houchard638b7992018-08-16 15:41:52 +02002874 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002875 if (!csbuf) {
2876 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002877 goto fail;
2878 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002879
Willy Tarreau937f7602018-02-26 15:22:17 +01002880 /* we can't retry a failed decompression operation so we must be very
2881 * careful not to take any risks. In practice the output buffer is
2882 * always empty except maybe for trailers, in which case we simply have
2883 * to wait for the upper layer to finish consuming what is available.
2884 */
2885 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002886 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002887
Willy Tarreau937f7602018-02-26 15:22:17 +01002888 csbuf->head = 0;
2889 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002890
2891 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2892 sizeof(list)/sizeof(list[0]), tmp);
2893 if (outlen < 0) {
2894 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2895 goto fail;
2896 }
2897
2898 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002899 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002900 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002901
2902 if (outlen < 0) {
2903 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2904 goto fail;
2905 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002906
Willy Tarreau174b06a2018-04-25 18:13:58 +02002907 if (msgf & H2_MSGF_BODY) {
2908 /* a payload is present */
2909 if (msgf & H2_MSGF_BODY_CL)
2910 h2s->flags |= H2_SF_DATA_CLEN;
2911 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2912 h2s->flags |= H2_SF_DATA_CHNK;
2913 }
2914
Willy Tarreau13278b42017-10-13 19:23:14 +02002915 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002916 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002917 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002918 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002919
Willy Tarreau39d68502018-03-02 12:26:37 +01002920 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002921 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002922 h2s->cs->flags |= CS_FL_REOS;
2923 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002924
Willy Tarreau68dd9852017-07-03 14:44:26 +02002925 leave:
2926 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002927 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002928 fail:
2929 outlen = 0;
2930 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002931}
2932
Willy Tarreau454f9052017-10-26 19:40:35 +02002933/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2934 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2935 * in use, a new chunk is emitted for each frame. This is supposed to fit
2936 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2937 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2938 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2939 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002940 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2941 * checked to know if some data remain pending (an empty DATA frame can return
2942 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2943 * connection errors in h2c->errcode. The caller must already have checked the
2944 * frame header and ensured that the frame was complete or the buffer full. It
2945 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002946 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002947static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002948{
2949 struct h2c *h2c = h2s->h2c;
2950 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002951 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002952 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002953 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002954
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002955 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002956
2957 /* The padlen is the first byte before data, and the padding appears
2958 * after data. padlen+data+padding are included in flen.
2959 */
Willy Tarreau79127812017-12-03 21:06:59 +01002960 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002961 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002962 return 0;
2963
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002964 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002965 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002966 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2967 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002968 return 0;
2969 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002970
2971 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002972 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002973 h2c->dfl--;
2974 h2c->rcvd_c++; h2c->rcvd_s++;
2975 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002976 }
2977
Olivier Houchard638b7992018-08-16 15:41:52 +02002978 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002979 if (!csbuf) {
2980 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002981 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002982 }
2983
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002984 flen = h2c->dfl - h2c->dpl;
2985 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002986 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002987
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 if (flen > b_data(&h2c->dbuf)) {
2989 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002990 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002991 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002992 }
2993
2994 if (unlikely(b_space_wraps(csbuf))) {
2995 /* it doesn't fit and the buffer is fragmented,
2996 * so let's defragment it and try again.
2997 */
2998 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002999 }
3000
Willy Tarreaueba10f22018-04-25 20:44:22 +02003001 /* chunked-encoding requires more room */
3002 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003003 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003004 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3005 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3006 (chklen < 1048576) ? 4 : 8;
3007 chklen += 4; // CRLF, CRLF
3008 }
3009
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003010 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003011 if (flen + chklen > b_room(csbuf)) {
3012 if (chklen >= b_room(csbuf)) {
3013 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003014 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003015 }
3016 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003017 }
3018
3019 if (h2s->flags & H2_SF_DATA_CHNK) {
3020 /* emit the chunk size */
3021 unsigned int chksz = flen;
3022 char str[10];
3023 char *beg;
3024
3025 beg = str + sizeof(str);
3026 *--beg = '\n';
3027 *--beg = '\r';
3028 do {
3029 *--beg = hextab[chksz & 0xF];
3030 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003031 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003032 }
3033
Willy Tarreau454f9052017-10-26 19:40:35 +02003034 /* Block1 is the length of the first block before the buffer wraps,
3035 * block2 is the optional second block to reach the end of the frame.
3036 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003037 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003038 if (block1 > flen)
3039 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003040 block2 = flen - block1;
3041
3042 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003043 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003044
3045 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003046 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003047
Willy Tarreaueba10f22018-04-25 20:44:22 +02003048 if (h2s->flags & H2_SF_DATA_CHNK) {
3049 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003050 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003051 }
3052
Willy Tarreau454f9052017-10-26 19:40:35 +02003053 /* now mark the input data as consumed (will be deleted from the buffer
3054 * by the caller when seeing FRAME_A after sending the window update).
3055 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003056 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003057 h2c->dfl -= flen;
3058 h2c->rcvd_c += flen;
3059 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3060
3061 if (h2c->dfl > h2c->dpl) {
3062 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003063 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003064 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003065 }
3066
Willy Tarreau4a28da12018-01-04 14:41:00 +01003067 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003068 /* here we're done with the frame, all the payload (except padding) was
3069 * transferred.
3070 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003071
3072 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3073 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003074 if (b_room(csbuf) < 5) {
3075 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003076 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003077 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003078 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003079 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003080 }
3081
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003082 h2c->rcvd_c += h2c->dpl;
3083 h2c->rcvd_s += h2c->dpl;
3084 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003085 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3086
Willy Tarreau39d68502018-03-02 12:26:37 +01003087 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003088 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003089 h2s->cs->flags |= CS_FL_REOS;
3090 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003091
Willy Tarreau454b57b2018-02-26 15:50:05 +01003092 return flen + chklen;
3093 fail:
3094 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003095}
3096
Willy Tarreau5dd17352018-06-14 13:33:30 +02003097/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3098 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3099 * number of bytes sent. The caller must check the stream's status to detect
3100 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003101 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003102static 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 +02003103{
3104 struct http_hdr list[MAX_HTTP_HDR];
3105 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003106 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003107 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003108 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003109 int es_now = 0;
3110 int ret = 0;
3111 int hdr;
3112
3113 if (h2c_mux_busy(h2c, h2s)) {
3114 h2s->flags |= H2_SF_BLK_MBUSY;
3115 return 0;
3116 }
3117
Willy Tarreau44e973f2018-03-01 17:49:30 +01003118 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003119 h2c->flags |= H2_CF_MUX_MALLOC;
3120 h2s->flags |= H2_SF_BLK_MROOM;
3121 return 0;
3122 }
3123
3124 /* First, try to parse the H1 response and index it into <list>.
3125 * NOTE! Since it comes from haproxy, we *know* that a response header
3126 * block does not wrap and we can safely read it this way without
3127 * having to realign the buffer.
3128 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003129 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003130 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003131 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003132 /* incomplete or invalid response, this is abnormal coming from
3133 * haproxy and may only result in a bad errorfile or bad Lua code
3134 * so that won't be fixed, raise an error now.
3135 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003136 * FIXME: we should instead add the ability to only return a
3137 * 502 bad gateway. But in theory this is not supposed to
3138 * happen.
3139 */
3140 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3141 ret = 0;
3142 goto end;
3143 }
3144
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003145 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003146
3147 /* certain statuses have no body or an empty one, regardless of
3148 * what the headers say.
3149 */
3150 if (sl.st.status >= 100 && sl.st.status < 200) {
3151 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3152 h1m->curr_len = h1m->body_len = 0;
3153 }
3154 else if (sl.st.status == 204 || sl.st.status == 304) {
3155 /* no contents, claim c-len is present and set to zero */
3156 h1m->flags &= ~H1_MF_CHNK;
3157 h1m->flags |= H1_MF_CLEN;
3158 h1m->curr_len = h1m->body_len = 0;
3159 }
3160
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003161 chunk_reset(&outbuf);
3162
3163 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003164 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003165 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003166 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003167
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003168 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169 break;
3170 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003172 }
3173
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003174 if (outbuf.size < 9)
3175 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003176
3177 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3179 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3180 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003181
3182 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003183 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003184 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003185 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003186 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003187 else if (unlikely(list[0].v.len != 3)) {
3188 /* this is an unparsable response */
3189 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3190 ret = 0;
3191 goto end;
3192 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003194 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003195 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3196 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3197 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3198 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3199 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003200 }
3201 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003202 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003203 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003204 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003205 }
3206
3207 /* encode all headers, stop at empty name */
3208 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003209 /* these ones do not exist in H2 and must be dropped. */
3210 if (isteq(list[hdr].n, ist("connection")) ||
3211 isteq(list[hdr].n, ist("proxy-connection")) ||
3212 isteq(list[hdr].n, ist("keep-alive")) ||
3213 isteq(list[hdr].n, ist("upgrade")) ||
3214 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003215 continue;
3216
3217 if (isteq(list[hdr].n, ist("")))
3218 break; // end
3219
3220 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3221 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003222 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003223 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003224 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003225 }
3226 }
3227
3228 /* we may need to add END_STREAM */
3229 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3230 es_now = 1;
3231
3232 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003233 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003234
3235 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003236 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003237
3238 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003239 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003240
3241 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003242 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003243 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003244
3245 /* for now we don't implemented CONTINUATION, so we wait for a
3246 * body or directly end in TRL2.
3247 */
3248 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003249 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003250 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003251
Willy Tarreau801250e2018-09-11 11:45:04 +02003252 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003253 h2s->flags |= H2_SF_ES_SENT;
3254 if (h2s->st == H2_SS_OPEN)
3255 h2s->st = H2_SS_HLOC;
3256 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003257 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003258 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003259 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003260 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003261 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003262 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003263 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003264 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003265 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003266
3267 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003268
3269 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003270 //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 +02003271 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003272 full:
3273 h1m_init_res(h1m);
3274 h1m->err_pos = -1; // don't care about errors on the response path
3275 h2c->flags |= H2_CF_MUX_MFULL;
3276 h2s->flags |= H2_SF_BLK_MROOM;
3277 ret = 0;
3278 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003279}
3280
Willy Tarreau5dd17352018-06-14 13:33:30 +02003281/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3282 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3283 * the number of bytes sent. The caller must check the stream's status to
3284 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003285 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003286static 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 +02003287{
3288 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003289 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003290 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003291 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003292 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 int es_now = 0;
3294 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003295 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003296 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003297
3298 if (h2c_mux_busy(h2c, h2s)) {
3299 h2s->flags |= H2_SF_BLK_MBUSY;
3300 goto end;
3301 }
3302
Willy Tarreau44e973f2018-03-01 17:49:30 +01003303 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003304 h2c->flags |= H2_CF_MUX_MALLOC;
3305 h2s->flags |= H2_SF_BLK_MROOM;
3306 goto end;
3307 }
3308
3309 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003310 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003311 goto end;
3312
3313 chunk_reset(&outbuf);
3314
3315 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003316 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003317 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003319
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003320 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003321 break;
3322 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003323 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003324 }
3325
3326 if (outbuf.size < 9) {
3327 h2c->flags |= H2_CF_MUX_MFULL;
3328 h2s->flags |= H2_SF_BLK_MROOM;
3329 goto end;
3330 }
3331
3332 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003333 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3334 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3335 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336
3337 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3338 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003339 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003340 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 break;
3342 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003343 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003344 if ((long long)size > h1m->curr_len)
3345 size = h1m->curr_len;
3346 break;
3347 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003348 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003349 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003350 if (!ret)
3351 goto end;
3352
3353 if (ret < 0) {
3354 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003355 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003356 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3357 goto end;
3358 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003359 max -= ret;
3360 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003361 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003362 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363 }
3364
Willy Tarreau801250e2018-09-11 11:45:04 +02003365 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003366 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003367 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003368 if (!ret)
3369 goto end;
3370
3371 if (ret < 0) {
3372 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003373 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003374 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3375 goto end;
3376 }
3377
3378 size = chunk;
3379 h1m->curr_len = chunk;
3380 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003381 max -= ret;
3382 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003383 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003384 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003385 if (!size)
3386 goto send_empty;
3387 }
3388
3389 /* in MSG_DATA state, continue below */
3390 size = h1m->curr_len;
3391 break;
3392 }
3393
3394 /* we have in <size> the exact number of bytes we need to copy from
3395 * the H1 buffer. We need to check this against the connection's and
3396 * the stream's send windows, and to ensure that this fits in the max
3397 * frame size and in the buffer's available space minus 9 bytes (for
3398 * the frame header). The connection's flow control is applied last so
3399 * that we can use a separate list of streams which are immediately
3400 * unblocked on window opening. Note: we don't implement padding.
3401 */
3402
Willy Tarreau5dd17352018-06-14 13:33:30 +02003403 if (size > max)
3404 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405
3406 if (size > h2s->mws)
3407 size = h2s->mws;
3408
3409 if (size <= 0) {
3410 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003411 if (h2s->send_wait) {
3412 LIST_DEL(&h2s->list);
3413 LIST_INIT(&h2s->list);
3414 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003415 goto end;
3416 }
3417
3418 if (h2c->mfs && size > h2c->mfs)
3419 size = h2c->mfs;
3420
3421 if (size + 9 > outbuf.size) {
3422 /* we have an opportunity for enlarging the too small
3423 * available space, let's try.
3424 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003425 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003426 goto realign_again;
3427 size = outbuf.size - 9;
3428 }
3429
3430 if (size <= 0) {
3431 h2c->flags |= H2_CF_MUX_MFULL;
3432 h2s->flags |= H2_SF_BLK_MROOM;
3433 goto end;
3434 }
3435
3436 if (size > h2c->mws)
3437 size = h2c->mws;
3438
3439 if (size <= 0) {
3440 h2s->flags |= H2_SF_BLK_MFCTL;
3441 goto end;
3442 }
3443
3444 /* copy whatever we can */
3445 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003446 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003447 if (ret == 1)
3448 len2 = 0;
3449
3450 if (!ret || len1 + len2 < size) {
3451 /* FIXME: must normally never happen */
3452 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3453 goto end;
3454 }
3455
3456 /* limit len1/len2 to size */
3457 if (len1 + len2 > size) {
3458 int sub = len1 + len2 - size;
3459
3460 if (len2 > sub)
3461 len2 -= sub;
3462 else {
3463 sub -= len2;
3464 len2 = 0;
3465 len1 -= sub;
3466 }
3467 }
3468
3469 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003471 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003472 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003473
3474 send_empty:
3475 /* we may need to add END_STREAM */
3476 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3477 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003478 *
3479 * FIXME: what we do here is not correct because we send end_stream
3480 * before knowing if we'll have to send a HEADERS frame for the
3481 * trailers. More importantly we're not consuming the trailing CRLF
3482 * after the end of trailers, so it will be left to the caller to
3483 * eat it. The right way to do it would be to measure trailers here
3484 * and to send ES only if there are no trailers.
3485 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003486 */
3487 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003488 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003489 es_now = 1;
3490
3491 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003492 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003493
3494 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003495 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003496
3497 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003498 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003499
3500 /* consume incoming H1 response */
3501 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003502 max -= size;
3503 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 total += size;
3505 h1m->curr_len -= size;
3506 h2s->mws -= size;
3507 h2c->mws -= size;
3508
3509 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003510 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003511 goto new_frame;
3512 }
3513 }
3514
3515 if (es_now) {
3516 if (h2s->st == H2_SS_OPEN)
3517 h2s->st = H2_SS_HLOC;
3518 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003519 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003520
Willy Tarreau35a62702018-02-27 15:37:25 +01003521 if (!(h1m->flags & H1_MF_CHNK)) {
3522 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003523 total += max;
3524 ofs += max;
3525 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003526
Willy Tarreau801250e2018-09-11 11:45:04 +02003527 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003528 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003529
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003530 h2s->flags |= H2_SF_ES_SENT;
3531 }
3532
3533 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003534 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 +02003535 return total;
3536}
3537
Olivier Houchard6ff20392018-07-17 18:46:31 +02003538/* Called from the upper layer, to subscribe to events, such as being able to send */
3539static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3540{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003541 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003542 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003543 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003544
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003545 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003546 sw = param;
3547 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3548 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003549 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003550 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003551 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003552 event_type &= ~SUB_CAN_RECV;
3553 }
3554 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003555 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003556 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003557 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003558 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003559 h2s->send_wait = sw;
3560 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3561 if (h2s->flags & H2_SF_BLK_MFCTL)
3562 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3563 else
3564 LIST_ADDQ(&h2c->send_list, &h2s->list);
3565 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003566 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003567 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003568 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003569 if (event_type != 0)
3570 return -1;
3571 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003572
3573
3574}
3575
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003576static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3577{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003578 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003579 struct h2s *h2s = cs->ctx;
3580
3581 if (event_type & SUB_CAN_RECV) {
3582 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003583 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003584 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003585 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003586 }
3587 }
3588 if (event_type & SUB_CAN_SEND) {
3589 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003590 if (h2s->send_wait == sw) {
3591 LIST_DEL(&h2s->list);
3592 LIST_INIT(&h2s->list);
3593 sw->wait_reason &= ~SUB_CAN_SEND;
3594 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003595 }
3596 }
3597 return 0;
3598}
3599
3600
Olivier Houchard511efea2018-08-16 15:30:32 +02003601/* Called from the upper layer, to receive data */
3602static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3603{
Olivier Houchard638b7992018-08-16 15:41:52 +02003604 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003605 size_t ret = 0;
3606
3607 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003608 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003609
Olivier Houchard638b7992018-08-16 15:41:52 +02003610 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003611 cs->flags |= CS_FL_RCV_MORE;
3612 else {
3613 cs->flags &= ~CS_FL_RCV_MORE;
3614 if (cs->flags & CS_FL_REOS)
3615 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003616 if (b_size(&h2s->rxbuf)) {
3617 b_free(&h2s->rxbuf);
3618 offer_buffers(NULL, tasks_run_queue);
3619 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003620 }
3621
3622 return ret;
3623}
3624
Willy Tarreau62f52692017-10-08 23:01:42 +02003625/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003626static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003627{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003628 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003629 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003630 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003631
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003632 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3633 return 0;
3634
Willy Tarreau0bad0432018-06-14 16:54:01 +02003635 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003636 h2s->flags |= H2_SF_OUTGOING_DATA;
3637
Willy Tarreaua40704a2018-09-11 13:52:04 +02003638 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003639 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003640 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003641 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003642 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003643 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003644 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003645 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003646 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003647 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003648
Willy Tarreau5dd17352018-06-14 13:33:30 +02003649 if (unlikely((int)ret <= 0)) {
3650 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003651 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3652 break;
3653 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003654 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003655 total += count;
3656 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003657 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003658 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003659 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003660 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003661 cs->flags |= CS_FL_ERROR;
3662 break;
3663 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003664
3665 total += ret;
3666 count -= ret;
3667
3668 if (h2s->st >= H2_SS_ERROR)
3669 break;
3670
3671 if (h2s->flags & H2_SF_BLK_ANY)
3672 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003673 }
3674
Willy Tarreau00610962018-07-19 10:58:28 +02003675 if (h2s->st >= H2_SS_ERROR) {
3676 /* trim any possibly pending data after we close (extra CR-LF,
3677 * unprocessed trailers, abnormal extra data, ...)
3678 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003679 total += count;
3680 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003681 }
3682
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003683 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003684 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003685 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003686 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003687 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003688 }
3689
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003690 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003691 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003692 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003693 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3694 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchard7505f942018-08-21 18:10:44 +02003695 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003696 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003697}
3698
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003699/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003700static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003701{
3702 struct h2c *h2c = conn->mux_ctx;
3703 struct h2s *h2s;
3704 struct eb32_node *node;
3705 int fctl_cnt = 0;
3706 int send_cnt = 0;
3707 int tree_cnt = 0;
3708 int orph_cnt = 0;
3709
3710 if (!h2c)
3711 return;
3712
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003713 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003714 fctl_cnt++;
3715
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003716 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003717 send_cnt++;
3718
3719 node = eb32_first(&h2c->streams_by_id);
3720 while (node) {
3721 h2s = container_of(node, struct h2s, by_id);
3722 tree_cnt++;
3723 if (!h2s->cs)
3724 orph_cnt++;
3725 node = eb32_next(node);
3726 }
3727
Willy Tarreau616ac812018-07-24 14:12:42 +02003728 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3729 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3730 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3731 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3732 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3733 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003734}
Willy Tarreau62f52692017-10-08 23:01:42 +02003735
3736/*******************************************************/
3737/* functions below are dedicated to the config parsers */
3738/*******************************************************/
3739
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003740/* config parser for global "tune.h2.header-table-size" */
3741static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3742 struct proxy *defpx, const char *file, int line,
3743 char **err)
3744{
3745 if (too_many_args(1, args, err, NULL))
3746 return -1;
3747
3748 h2_settings_header_table_size = atoi(args[1]);
3749 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3750 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3751 return -1;
3752 }
3753 return 0;
3754}
Willy Tarreau62f52692017-10-08 23:01:42 +02003755
Willy Tarreaue6baec02017-07-27 11:45:11 +02003756/* config parser for global "tune.h2.initial-window-size" */
3757static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3758 struct proxy *defpx, const char *file, int line,
3759 char **err)
3760{
3761 if (too_many_args(1, args, err, NULL))
3762 return -1;
3763
3764 h2_settings_initial_window_size = atoi(args[1]);
3765 if (h2_settings_initial_window_size < 0) {
3766 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3767 return -1;
3768 }
3769 return 0;
3770}
3771
Willy Tarreau5242ef82017-07-27 11:47:28 +02003772/* config parser for global "tune.h2.max-concurrent-streams" */
3773static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3774 struct proxy *defpx, const char *file, int line,
3775 char **err)
3776{
3777 if (too_many_args(1, args, err, NULL))
3778 return -1;
3779
3780 h2_settings_max_concurrent_streams = atoi(args[1]);
3781 if (h2_settings_max_concurrent_streams < 0) {
3782 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3783 return -1;
3784 }
3785 return 0;
3786}
3787
Willy Tarreau62f52692017-10-08 23:01:42 +02003788
3789/****************************************/
3790/* MUX initialization and instanciation */
3791/***************************************/
3792
3793/* The mux operations */
3794const struct mux_ops h2_ops = {
3795 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003796 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003797 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003798 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003799 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003800 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003801 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003802 .attach = h2_attach,
3803 .detach = h2_detach,
3804 .shutr = h2_shutr,
3805 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003806 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003807 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003808 .name = "H2",
3809};
3810
Christopher Faulet32f61c02018-04-10 14:33:41 +02003811/* PROTO selection : this mux registers PROTO token "h2" */
3812static struct mux_proto_list mux_proto_h2 =
3813 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003814
3815/* config keyword parsers */
3816static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003817 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003818 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003819 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003820 { 0, NULL, NULL }
3821}};
3822
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003823static void __h2_deinit(void)
3824{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003825 pool_destroy(pool_head_h2s);
3826 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003827}
3828
Willy Tarreau62f52692017-10-08 23:01:42 +02003829__attribute__((constructor))
3830static void __h2_init(void)
3831{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003832 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003833 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003834 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003835 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3836 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003837}