blob: e4005a2c366a3947d7f4bc925fdbd6f4d91619f1 [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 Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020020#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020057#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
58#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020059
Willy Tarreau081d4722017-05-16 21:51:05 +020060/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020061#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
62#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
63#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 +020064#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020065
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 */
Olivier Houchardd846c262018-10-19 17:24:29 +0200124 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100125 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200126 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200127};
128
Willy Tarreau18312642017-10-11 07:57:07 +0200129/* H2 stream state, in h2s->st */
130enum h2_ss {
131 H2_SS_IDLE = 0, // idle
132 H2_SS_RLOC, // reserved(local)
133 H2_SS_RREM, // reserved(remote)
134 H2_SS_OPEN, // open
135 H2_SS_HREM, // half-closed(remote)
136 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200137 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200138 H2_SS_CLOSED, // closed
139 H2_SS_ENTRIES // must be last
140} __attribute__((packed));
141
142/* HTTP/2 stream flags (32 bit), in h2s->flags */
143#define H2_SF_NONE 0x00000000
144#define H2_SF_ES_RCVD 0x00000001
145#define H2_SF_ES_SENT 0x00000002
146
147#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
148#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
149
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200150/* stream flags indicating the reason the stream is blocked */
151#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
152#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
153#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
154#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
155#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
156
Willy Tarreau454f9052017-10-26 19:40:35 +0200157/* stream flags indicating how data is supposed to be sent */
158#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
159#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
160
161/* step we're currently in when sending chunks. This is needed because we may
162 * have to transfer chunks as large as a full buffer so there's no room left
163 * for size nor crlf around.
164 */
165#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
166#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
167#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
168
169#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
170
Willy Tarreau67434202017-11-06 20:20:51 +0100171#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100172#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100173
Willy Tarreau18312642017-10-11 07:57:07 +0200174/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
175 * it is being processed in the internal HTTP representation (H1 for now).
176 */
177struct h2s {
178 struct conn_stream *cs;
179 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200180 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200181 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200182 int32_t id; /* stream ID */
183 uint32_t flags; /* H2_SF_* */
184 int mws; /* mux window size for this stream */
185 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
186 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200187 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200188 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200189 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
190 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
191 struct wait_event *send_wait; /* The streeam is waiting for flow control */
192 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200193};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200194
Willy Tarreauc6405142017-09-21 20:23:50 +0200195/* descriptor for an h2 frame header */
196struct h2_fh {
197 uint32_t len; /* length, host order, 24 bits */
198 uint32_t sid; /* stream id, host order, 31 bits */
199 uint8_t ft; /* frame type */
200 uint8_t ff; /* frame flags */
201};
202
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200203/* a few settings from the global section */
204static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200205static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200206static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200207
Willy Tarreau2a856182017-05-16 15:20:39 +0200208/* a dmumy closed stream */
209static const struct h2s *h2_closed_stream = &(const struct h2s){
210 .cs = NULL,
211 .h2c = NULL,
212 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100213 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100214 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200215 .id = 0,
216};
217
218/* and a dummy idle stream for use with any unannounced stream */
219static const struct h2s *h2_idle_stream = &(const struct h2s){
220 .cs = NULL,
221 .h2c = NULL,
222 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100223 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200224 .id = 0,
225};
226
Olivier Houchard9f6af332018-05-25 14:04:04 +0200227static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200228static int h2_send(struct h2c *h2c);
229static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200230static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200231static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100232static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100233static int h2_frt_decode_headers(struct h2s *h2s);
234static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200235static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200236
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200237/*****************************************************/
238/* functions below are for dynamic buffer management */
239/*****************************************************/
240
Willy Tarreau315d8072017-12-10 22:17:57 +0100241/* indicates whether or not the we may call the h2_recv() function to attempt
242 * to receive data into the buffer and/or demux pending data. The condition is
243 * a bit complex due to some API limits for now. The rules are the following :
244 * - if an error or a shutdown was detected on the connection and the buffer
245 * is empty, we must not attempt to receive
246 * - if the demux buf failed to be allocated, we must not try to receive and
247 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100248 * - if no flag indicates a blocking condition, we may attempt to receive,
249 * regardless of whether the demux buffer is full or not, so that only
250 * de demux part decides whether or not to block. This is needed because
251 * the connection API indeed prevents us from re-enabling receipt that is
252 * already enabled in a polled state, so we must always immediately stop
253 * as soon as the demux can't proceed so as never to hit an end of read
254 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100255 * - otherwise must may not attempt
256 */
257static inline int h2_recv_allowed(const struct h2c *h2c)
258{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200259 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100260 (h2c->st0 >= H2_CS_ERROR ||
261 h2c->conn->flags & CO_FL_ERROR ||
262 conn_xprt_read0_pending(h2c->conn)))
263 return 0;
264
265 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100266 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100267 return 1;
268
269 return 0;
270}
271
Willy Tarreauf2101912018-07-19 10:11:38 +0200272/* returns true if the connection has too many conn_streams attached */
273static inline int h2_has_too_many_cs(const struct h2c *h2c)
274{
275 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
276}
277
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
279 * flags are used to figure what buffer was requested. It returns 1 if the
280 * allocation succeeds, in which case the connection is woken up, or 0 if it's
281 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100283static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200284{
285 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100286 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200287
Willy Tarreau44e973f2018-03-01 17:49:30 +0100288 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200289 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200290 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200291 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200292 return 1;
293 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200294
Willy Tarreau44e973f2018-03-01 17:49:30 +0100295 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
296 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200297
298 if (h2c->flags & H2_CF_DEM_MROOM) {
299 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200300 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200301 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200302 }
Willy Tarreau14398122017-09-22 14:26:04 +0200303 return 1;
304 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100305
306 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
307 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200308 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100309 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200310 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200311 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100312 return 1;
313 }
314
Willy Tarreau14398122017-09-22 14:26:04 +0200315 return 0;
316}
317
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200318static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200319{
320 struct buffer *buf = NULL;
321
Willy Tarreau44e973f2018-03-01 17:49:30 +0100322 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
323 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
324 h2c->buf_wait.target = h2c;
325 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100327 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100328 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200329 __conn_xprt_stop_recv(h2c->conn);
330 }
331 return buf;
332}
333
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200334static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200335{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200336 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100337 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200338 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200339 }
340}
341
Olivier Houchardd540b362018-11-05 18:37:53 +0100342static int h2_avail_streams(struct connection *conn)
343{
344 struct h2c *h2c = conn->mux_ctx;
345
346 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
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);
Olivier Houchardd846c262018-10-19 17:24:29 +0200423 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100424 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 conn->mux_ctx = h2c;
426
Willy Tarreau3f133572017-10-31 19:21:06 +0100427 if (t)
428 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100429
Willy Tarreau0f383582018-10-03 14:22:21 +0200430 /* prepare to read something */
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 Tarreau1ed87b72018-11-25 08:45:16 +01002141 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002142 break;
2143
2144 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002145 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002146 h2c->st0 = H2_CS_FRAME_H;
2147 }
2148 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002149
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002150 if (h2c->rcvd_c > 0 &&
2151 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2152 h2c_send_conn_wu(h2c);
2153
Willy Tarreau52eed752017-09-22 15:05:09 +02002154 fail:
2155 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002156 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002157 /* we may have to signal the upper layers */
2158 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002159 if (h2s->recv_wait) {
2160 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2161 tasklet_wakeup(h2s->recv_wait->task);
2162 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002163 }
2164 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002165
2166 if (h2_recv_allowed(h2c))
2167 tasklet_wakeup(h2c->wait_event.task);
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;
Olivier Houchardd846c262018-10-19 17:24:29 +02002195 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002196 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002197 LIST_DEL(&h2s->list);
2198 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002199 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002200 }
2201
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002202 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002203 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2204 break;
2205
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002206 h2s->flags &= ~H2_SF_BLK_ANY;
2207 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002208 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002209 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002210 LIST_DEL(&h2s->list);
2211 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002212 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002213 }
2214
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002215 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002216 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002217 if (h2c->st0 == H2_CS_ERROR) {
2218 if (h2c->max_id >= 0) {
2219 h2c_send_goaway_error(h2c, NULL);
2220 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2221 return 0;
2222 }
2223
2224 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2225 }
2226 return 1;
2227 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002228 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002229}
2230
Willy Tarreau62f52692017-10-08 23:01:42 +02002231
Willy Tarreau479998a2018-11-18 06:30:59 +01002232/* Attempt to read data, and subscribe if none available.
2233 * The function returns 1 if data has been received, otherwise zero.
2234 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002235static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002236{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002237 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002238 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002239 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002240 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002242 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002243 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002244
Willy Tarreau315d8072017-12-10 22:17:57 +01002245 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002246 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002247
Willy Tarreau44e973f2018-03-01 17:49:30 +01002248 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002249 if (!buf) {
2250 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002251 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002252 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002253
Olivier Houchard7505f942018-08-21 18:10:44 +02002254 do {
2255 max = buf->size - b_data(buf);
2256 if (max)
2257 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2258 else
2259 ret = 0;
2260 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002261
Olivier Houchard53216e72018-10-10 15:46:36 +02002262 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002263 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002264
Olivier Houcharda1411e62018-08-17 18:42:48 +02002265 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002266 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002267 return conn_xprt_read0_pending(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002268 }
2269
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002270 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002271 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002272 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002273}
2274
Willy Tarreau479998a2018-11-18 06:30:59 +01002275/* Try to send data if possible.
2276 * The function returns 1 if data have been sent, otherwise zero.
2277 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002278static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002279{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002280 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002281 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002282 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002283
2284 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002285 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002286
Olivier Houchard7505f942018-08-21 18:10:44 +02002287
Willy Tarreaua2af5122017-10-09 11:56:46 +02002288 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2289 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002290 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002291 }
2292
Willy Tarreaubc933932017-10-09 16:21:43 +02002293 /* This loop is quite simple : it tries to fill as much as it can from
2294 * pending streams into the existing buffer until it's reportedly full
2295 * or the end of send requests is reached. Then it tries to send this
2296 * buffer's contents out, marks it not full if at least one byte could
2297 * be sent, and tries again.
2298 *
2299 * The snd_buf() function normally takes a "flags" argument which may
2300 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2301 * data immediately comes and CO_SFL_STREAMER to indicate that the
2302 * connection is streaming lots of data (used to increase TLS record
2303 * size at the expense of latency). The former can be sent any time
2304 * there's a buffer full flag, as it indicates at least one stream
2305 * attempted to send and failed so there are pending data. An
2306 * alternative would be to set it as long as there's an active stream
2307 * but that would be problematic for ACKs until we have an absolute
2308 * guarantee that all waiters have at least one byte to send. The
2309 * latter should possibly not be set for now.
2310 */
2311
2312 done = 0;
2313 while (!done) {
2314 unsigned int flags = 0;
2315
2316 /* fill as much as we can into the current buffer */
2317 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2318 done = h2_process_mux(h2c);
2319
2320 if (conn->flags & CO_FL_ERROR)
2321 break;
2322
2323 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2324 flags |= CO_SFL_MSG_MORE;
2325
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 if (b_data(&h2c->mbuf)) {
2327 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002328 if (!ret)
2329 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002330 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002331 b_del(&h2c->mbuf, ret);
2332 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002333 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002334
2335 /* wrote at least one byte, the buffer is not full anymore */
2336 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2337 }
2338
Willy Tarreaua2af5122017-10-09 11:56:46 +02002339 if (conn->flags & CO_FL_SOCK_WR_SH) {
2340 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002341 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002342 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002343 /* We're not full anymore, so we can wake any task that are waiting
2344 * for us.
2345 */
2346 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002347 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002348 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2349 struct h2s *, list);
2350 LIST_DEL(&h2s->list);
2351 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002352 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002353 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002354 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002355 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002356 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002357 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002358 /* We're done, no more to send */
2359 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002360 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002361schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002362 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2363 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002364 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002365}
2366
2367static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2368{
2369 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002370 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002371
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002372 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002373 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002374 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002375 ret |= h2_recv(h2c);
2376 if (ret)
2377 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002378 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002379}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002380
Willy Tarreau62f52692017-10-08 23:01:42 +02002381/* callback called on any event by the connection handler.
2382 * It applies changes and returns zero, or < 0 if it wants immediate
2383 * destruction of the connection (which normally doesn not happen in h2).
2384 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002385static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002386{
Olivier Houchard7505f942018-08-21 18:10:44 +02002387 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002388
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002389 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002390 h2_process_demux(h2c);
2391
2392 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002393 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002394
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002395 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002396 h2c->flags &= ~H2_CF_DEM_DFULL;
2397 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002398 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002399
Willy Tarreau0b37d652018-10-03 10:33:02 +02002400 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002401 /* frontend is stopping, reload likely in progress, let's try
2402 * to announce a graceful shutdown if not yet done. We don't
2403 * care if it fails, it will be tried again later.
2404 */
2405 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2406 if (h2c->last_sid < 0)
2407 h2c->last_sid = (1U << 31) - 1;
2408 h2c_send_goaway_error(h2c, NULL);
2409 }
2410 }
2411
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002412 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002413 * If we received early data, and the handshake is done, wake
2414 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002415 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002416 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2417 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2418 struct eb32_node *node;
2419 struct h2s *h2s;
2420
2421 h2c->flags |= H2_CF_WAIT_FOR_HS;
2422 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2423
2424 while (node) {
2425 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002426 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002427 h2s->recv_wait) {
2428 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002429 sw->wait_reason &= ~SUB_CAN_RECV;
2430 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002431 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002432 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002433 node = eb32_next(node);
2434 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002435 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002436
Willy Tarreau26bd7612017-10-09 16:47:04 +02002437 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002438 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2439 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2440 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002441 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442
2443 if (eb_is_empty(&h2c->streams_by_id)) {
2444 /* no more stream, kill the connection now */
2445 h2_release(conn);
2446 return -1;
2447 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002448 }
2449
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002450 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002451 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002452
Olivier Houchard53216e72018-10-10 15:46:36 +02002453 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2454 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2455 (h2c->st0 != H2_CS_ERROR &&
2456 !b_data(&h2c->mbuf) &&
2457 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2458 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002459 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002460
Willy Tarreau3f133572017-10-31 19:21:06 +01002461 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002462 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002463 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002464 task_queue(h2c->task);
2465 }
2466 else
2467 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002468 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002469
Olivier Houchard7505f942018-08-21 18:10:44 +02002470 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002471 return 0;
2472}
2473
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002474static int h2_wake(struct connection *conn)
2475{
2476 struct h2c *h2c = conn->mux_ctx;
2477
2478 return (h2_process(h2c));
2479}
2480
Willy Tarreauea392822017-10-31 10:02:25 +01002481/* Connection timeout management. The principle is that if there's no receipt
2482 * nor sending for a certain amount of time, the connection is closed. If the
2483 * MUX buffer still has lying data or is not allocatable, the connection is
2484 * immediately killed. If it's allocatable and empty, we attempt to send a
2485 * GOAWAY frame.
2486 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002487static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002488{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002489 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002490 int expired = tick_is_expired(t->expire, now_ms);
2491
Willy Tarreau0975f112018-03-29 15:22:59 +02002492 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002493 return t;
2494
Willy Tarreau0975f112018-03-29 15:22:59 +02002495 task_delete(t);
2496 task_free(t);
2497
2498 if (!h2c) {
2499 /* resources were already deleted */
2500 return NULL;
2501 }
2502
2503 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002504 h2c_error(h2c, H2_ERR_NO_ERROR);
2505 h2_wake_some_streams(h2c, 0, 0);
2506
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002507 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002508 /* don't even try to send a GOAWAY, the buffer is stuck */
2509 h2c->flags |= H2_CF_GOAWAY_FAILED;
2510 }
2511
2512 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002513 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002514 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2515 h2c->flags |= H2_CF_GOAWAY_FAILED;
2516
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002517 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2518 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002519 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002520 b_del(&h2c->mbuf, ret);
2521 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002522 }
2523 }
Willy Tarreauea392822017-10-31 10:02:25 +01002524
Willy Tarreau0975f112018-03-29 15:22:59 +02002525 /* either we can release everything now or it will be done later once
2526 * the last stream closes.
2527 */
2528 if (eb_is_empty(&h2c->streams_by_id))
2529 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002530
Willy Tarreauea392822017-10-31 10:02:25 +01002531 return NULL;
2532}
2533
2534
Willy Tarreau62f52692017-10-08 23:01:42 +02002535/*******************************************/
2536/* functions below are used by the streams */
2537/*******************************************/
2538
2539/*
2540 * Attach a new stream to a connection
2541 * (Used for outgoing connections)
2542 */
2543static struct conn_stream *h2_attach(struct connection *conn)
2544{
2545 return NULL;
2546}
2547
Willy Tarreaufafd3982018-11-18 21:29:20 +01002548/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2549 * We have to scan because we may have some orphan streams. It might be
2550 * beneficial to scan backwards from the end to reduce the likeliness to find
2551 * orphans.
2552 */
2553static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2554{
2555 struct h2c *h2c = conn->mux_ctx;
2556 struct h2s *h2s;
2557 struct eb32_node *node;
2558
2559 node = eb32_first(&h2c->streams_by_id);
2560 while (node) {
2561 h2s = container_of(node, struct h2s, by_id);
2562 if (h2s->cs)
2563 return h2s->cs;
2564 node = eb32_next(node);
2565 }
2566 return NULL;
2567}
2568
Willy Tarreau62f52692017-10-08 23:01:42 +02002569/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002570 * Destroy the mux and the associated connection, if it is no longer used
2571 */
2572static void h2_destroy(struct connection *conn)
2573{
2574 struct h2c *h2c = conn->mux_ctx;
2575
2576 if (eb_is_empty(&h2c->streams_by_id))
2577 h2_release(h2c->conn);
2578}
2579
2580/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002581 * Detach the stream from the connection and possibly release the connection.
2582 */
2583static void h2_detach(struct conn_stream *cs)
2584{
Willy Tarreau60935142017-10-16 18:11:19 +02002585 struct h2s *h2s = cs->ctx;
2586 struct h2c *h2c;
2587
2588 cs->ctx = NULL;
2589 if (!h2s)
2590 return;
2591
2592 h2c = h2s->h2c;
2593 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002594 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002595 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2596 !h2_has_too_many_cs(h2c)) {
2597 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002598 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002599 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002600 }
Willy Tarreau60935142017-10-16 18:11:19 +02002601
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002602 /* this stream may be blocked waiting for some data to leave (possibly
2603 * an ES or RST frame), so orphan it in this case.
2604 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002605 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002606 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002607 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002608 return;
2609
Willy Tarreau45f752e2017-10-30 15:44:59 +01002610 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2611 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2612 /* unblock the connection if it was blocked on this
2613 * stream.
2614 */
2615 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2616 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002618 }
2619
Willy Tarreau71049cc2018-03-28 13:56:39 +02002620 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002621
Willy Tarreaue323f342018-03-28 13:51:45 +02002622 /* We don't want to close right now unless we're removing the
2623 * last stream, and either the connection is in error, or it
2624 * reached the ID already specified in a GOAWAY frame received
2625 * or sent (as seen by last_sid >= 0).
2626 */
2627 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2628 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002629 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002630 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002631 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002632 (conn_xprt_read0_pending(h2c->conn) ||
2633 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2634 /* no more stream will come, kill it now */
2635 h2_release(h2c->conn);
2636 }
2637 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002638 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002639 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2640 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002641 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002642 else
2643 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002644 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002645}
2646
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002647static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002648{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002649 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002650 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002651
Willy Tarreau721c9742017-11-07 11:05:42 +01002652 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002653 return;
2654
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002655 /* if no outgoing data was seen on this stream, it means it was
2656 * closed with a "tcp-request content" rule that is normally
2657 * used to kill the connection ASAP (eg: limit abuse). In this
2658 * case we send a goaway to close the connection.
2659 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002660 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002661 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002662 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002663
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002664 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2665 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002666 h2c_send_goaway_error(h2c, h2s) <= 0)
2667 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002668
Willy Tarreau00dd0782018-03-01 16:31:34 +01002669 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002670
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671 return;
2672add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002673 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002674 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002675 if (h2s->flags & H2_SF_BLK_MFCTL) {
2676 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2677 h2s->send_wait = sw;
2678 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2679 h2s->send_wait = sw;
2680 LIST_ADDQ(&h2c->send_list, &h2s->list);
2681 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002682 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002683 /* Let the handler know we want shutr */
2684 sw->handle = (void *)((long)sw->handle | 1);
2685
Willy Tarreau62f52692017-10-08 23:01:42 +02002686}
2687
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002688static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002689{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002690 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002691 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002692
Willy Tarreau721c9742017-11-07 11:05:42 +01002693 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002694 return;
2695
Willy Tarreau67434202017-11-06 20:20:51 +01002696 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002697 /* we can cleanly close using an empty data frame only after headers */
2698
2699 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2700 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002701 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002702
2703 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002704 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002705 else
2706 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002707 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002708 /* if no outgoing data was seen on this stream, it means it was
2709 * closed with a "tcp-request content" rule that is normally
2710 * used to kill the connection ASAP (eg: limit abuse). In this
2711 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002712 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002713 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002714 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002715 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002716
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002717 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2718 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002719 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002720 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002721
Willy Tarreau00dd0782018-03-01 16:31:34 +01002722 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002723 }
2724
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002725
2726 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002727 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002728 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002729 if (h2s->flags & H2_SF_BLK_MFCTL) {
2730 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2731 h2s->send_wait = sw;
2732 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2733 h2s->send_wait = sw;
2734 LIST_ADDQ(&h2c->send_list, &h2s->list);
2735 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002736 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002737 /* let the handler know we want to shutw */
2738 sw->handle = (void *)((long)(sw->handle) | 2);
2739
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002740}
2741
2742static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2743{
2744 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002745 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002746
2747 if (reason & 1)
2748 h2_do_shutr(h2s);
2749 if (reason & 2)
2750 h2_do_shutw(h2s);
2751
2752 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002753}
2754
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002755static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2756{
2757 struct h2s *h2s = cs->ctx;
2758
2759 if (!mode)
2760 return;
2761
2762 h2_do_shutr(h2s);
2763}
2764
2765static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2766{
2767 struct h2s *h2s = cs->ctx;
2768
2769 h2_do_shutw(h2s);
2770}
2771
Willy Tarreau13278b42017-10-13 19:23:14 +02002772/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2773 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2774 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002775 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002776 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002777static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002778{
2779 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002780 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002781 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002782 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002783 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002784 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002785 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002786 int flen = h2c->dfl;
2787 int outlen = 0;
2788 int wrap;
2789 int try;
2790
2791 if (!h2c->dfl) {
2792 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002793 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002794 return 0;
2795 }
2796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002798 return 0; // incomplete input frame
2799
Willy Tarreau13278b42017-10-13 19:23:14 +02002800 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002801 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002802 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002803 copy = alloc_trash_chunk();
2804 if (!copy) {
2805 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2806 goto fail;
2807 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002808 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2809 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2810 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002811 }
2812
2813 /* The padlen is the first byte before data, and the padding appears
2814 * after data. padlen+data+padding are included in flen.
2815 */
2816 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002817 h2c->dpl = *hdrs;
2818 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002819 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2820 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002821 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002822 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002823 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002824 hdrs += 1; // skip Pad Length
2825 }
2826
2827 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2828 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002829 if (read_n32(hdrs) == h2s->id) {
2830 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2831 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002832 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002833 }
2834
Willy Tarreau13278b42017-10-13 19:23:14 +02002835 hdrs += 5; // stream dep = 4, weight = 1
2836 flen -= 5;
2837 }
2838
2839 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2840 * don't support this for now and can't even decompress so we have to
2841 * break the connection.
2842 */
2843 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2844 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002845 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002846 }
2847
Olivier Houchard638b7992018-08-16 15:41:52 +02002848 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002849 if (!csbuf) {
2850 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002851 goto fail;
2852 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002853
Willy Tarreau937f7602018-02-26 15:22:17 +01002854 /* we can't retry a failed decompression operation so we must be very
2855 * careful not to take any risks. In practice the output buffer is
2856 * always empty except maybe for trailers, in which case we simply have
2857 * to wait for the upper layer to finish consuming what is available.
2858 */
2859 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002860 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002861
Willy Tarreau937f7602018-02-26 15:22:17 +01002862 csbuf->head = 0;
2863 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002864
2865 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2866 sizeof(list)/sizeof(list[0]), tmp);
2867 if (outlen < 0) {
2868 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2869 goto fail;
2870 }
2871
2872 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002873 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002874 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002875
2876 if (outlen < 0) {
2877 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2878 goto fail;
2879 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002880
Willy Tarreau174b06a2018-04-25 18:13:58 +02002881 if (msgf & H2_MSGF_BODY) {
2882 /* a payload is present */
2883 if (msgf & H2_MSGF_BODY_CL)
2884 h2s->flags |= H2_SF_DATA_CLEN;
2885 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2886 h2s->flags |= H2_SF_DATA_CHNK;
2887 }
2888
Willy Tarreau13278b42017-10-13 19:23:14 +02002889 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002891 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002892 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002893
Willy Tarreau39d68502018-03-02 12:26:37 +01002894 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002895 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002896 h2s->cs->flags |= CS_FL_REOS;
2897 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002898
Willy Tarreau68dd9852017-07-03 14:44:26 +02002899 leave:
2900 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002901 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002902 fail:
2903 outlen = 0;
2904 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002905}
2906
Willy Tarreau454f9052017-10-26 19:40:35 +02002907/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2908 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2909 * in use, a new chunk is emitted for each frame. This is supposed to fit
2910 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2911 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2912 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2913 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002914 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2915 * checked to know if some data remain pending (an empty DATA frame can return
2916 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2917 * connection errors in h2c->errcode. The caller must already have checked the
2918 * frame header and ensured that the frame was complete or the buffer full. It
2919 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002920 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002921static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002922{
2923 struct h2c *h2c = h2s->h2c;
2924 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002925 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002926 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002927 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002928
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002929 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002930
2931 /* The padlen is the first byte before data, and the padding appears
2932 * after data. padlen+data+padding are included in flen.
2933 */
Willy Tarreau79127812017-12-03 21:06:59 +01002934 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002936 return 0;
2937
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002938 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002939 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002940 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2941 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002942 return 0;
2943 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002944
2945 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002946 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002947 h2c->dfl--;
2948 h2c->rcvd_c++; h2c->rcvd_s++;
2949 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002950 }
2951
Olivier Houchard638b7992018-08-16 15:41:52 +02002952 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002953 if (!csbuf) {
2954 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002955 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002956 }
2957
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002958 flen = h2c->dfl - h2c->dpl;
2959 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002960 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002961
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002962 if (flen > b_data(&h2c->dbuf)) {
2963 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002964 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002965 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002966 }
2967
2968 if (unlikely(b_space_wraps(csbuf))) {
2969 /* it doesn't fit and the buffer is fragmented,
2970 * so let's defragment it and try again.
2971 */
2972 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002973 }
2974
Willy Tarreaueba10f22018-04-25 20:44:22 +02002975 /* chunked-encoding requires more room */
2976 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002977 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002978 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2979 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2980 (chklen < 1048576) ? 4 : 8;
2981 chklen += 4; // CRLF, CRLF
2982 }
2983
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002984 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002985 if (flen + chklen > b_room(csbuf)) {
2986 if (chklen >= b_room(csbuf)) {
2987 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002988 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002989 }
2990 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002991 }
2992
2993 if (h2s->flags & H2_SF_DATA_CHNK) {
2994 /* emit the chunk size */
2995 unsigned int chksz = flen;
2996 char str[10];
2997 char *beg;
2998
2999 beg = str + sizeof(str);
3000 *--beg = '\n';
3001 *--beg = '\r';
3002 do {
3003 *--beg = hextab[chksz & 0xF];
3004 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003005 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003006 }
3007
Willy Tarreau454f9052017-10-26 19:40:35 +02003008 /* Block1 is the length of the first block before the buffer wraps,
3009 * block2 is the optional second block to reach the end of the frame.
3010 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003011 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003012 if (block1 > flen)
3013 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003014 block2 = flen - block1;
3015
3016 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003017 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003018
3019 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003020 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003021
Willy Tarreaueba10f22018-04-25 20:44:22 +02003022 if (h2s->flags & H2_SF_DATA_CHNK) {
3023 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003024 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003025 }
3026
Willy Tarreau454f9052017-10-26 19:40:35 +02003027 /* now mark the input data as consumed (will be deleted from the buffer
3028 * by the caller when seeing FRAME_A after sending the window update).
3029 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003030 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003031 h2c->dfl -= flen;
3032 h2c->rcvd_c += flen;
3033 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3034
3035 if (h2c->dfl > h2c->dpl) {
3036 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003037 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003038 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003039 }
3040
Willy Tarreau4a28da12018-01-04 14:41:00 +01003041 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003042 /* here we're done with the frame, all the payload (except padding) was
3043 * transferred.
3044 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003045
3046 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3047 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003048 if (b_room(csbuf) < 5) {
3049 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003050 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003051 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003052 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003053 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003054 }
3055
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003056 h2c->rcvd_c += h2c->dpl;
3057 h2c->rcvd_s += h2c->dpl;
3058 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003059 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3060
Willy Tarreau39d68502018-03-02 12:26:37 +01003061 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003062 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003063 h2s->cs->flags |= CS_FL_REOS;
3064 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003065
Willy Tarreau454b57b2018-02-26 15:50:05 +01003066 return flen + chklen;
3067 fail:
3068 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003069}
3070
Willy Tarreau5dd17352018-06-14 13:33:30 +02003071/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3072 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3073 * number of bytes sent. The caller must check the stream's status to detect
3074 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003075 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003076static 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 +02003077{
3078 struct http_hdr list[MAX_HTTP_HDR];
3079 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003080 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003081 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003082 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003083 int es_now = 0;
3084 int ret = 0;
3085 int hdr;
3086
3087 if (h2c_mux_busy(h2c, h2s)) {
3088 h2s->flags |= H2_SF_BLK_MBUSY;
3089 return 0;
3090 }
3091
Willy Tarreau44e973f2018-03-01 17:49:30 +01003092 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003093 h2c->flags |= H2_CF_MUX_MALLOC;
3094 h2s->flags |= H2_SF_BLK_MROOM;
3095 return 0;
3096 }
3097
3098 /* First, try to parse the H1 response and index it into <list>.
3099 * NOTE! Since it comes from haproxy, we *know* that a response header
3100 * block does not wrap and we can safely read it this way without
3101 * having to realign the buffer.
3102 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003103 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003104 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003105 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003106 /* incomplete or invalid response, this is abnormal coming from
3107 * haproxy and may only result in a bad errorfile or bad Lua code
3108 * so that won't be fixed, raise an error now.
3109 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110 * FIXME: we should instead add the ability to only return a
3111 * 502 bad gateway. But in theory this is not supposed to
3112 * happen.
3113 */
3114 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3115 ret = 0;
3116 goto end;
3117 }
3118
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003119 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003120
3121 /* certain statuses have no body or an empty one, regardless of
3122 * what the headers say.
3123 */
3124 if (sl.st.status >= 100 && sl.st.status < 200) {
3125 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3126 h1m->curr_len = h1m->body_len = 0;
3127 }
3128 else if (sl.st.status == 204 || sl.st.status == 304) {
3129 /* no contents, claim c-len is present and set to zero */
3130 h1m->flags &= ~H1_MF_CHNK;
3131 h1m->flags |= H1_MF_CLEN;
3132 h1m->curr_len = h1m->body_len = 0;
3133 }
3134
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003135 chunk_reset(&outbuf);
3136
3137 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003139 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003140 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003141
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003142 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003143 break;
3144 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003145 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146 }
3147
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003148 if (outbuf.size < 9)
3149 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003150
3151 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003152 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3153 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3154 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003155
3156 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003157 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003158 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003159 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003160 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003161 else if (unlikely(list[0].v.len != 3)) {
3162 /* this is an unparsable response */
3163 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3164 ret = 0;
3165 goto end;
3166 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003167 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003168 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3170 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3171 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3172 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3173 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003174 }
3175 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003176 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003177 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003178 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003179 }
3180
3181 /* encode all headers, stop at empty name */
3182 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003183 /* these ones do not exist in H2 and must be dropped. */
3184 if (isteq(list[hdr].n, ist("connection")) ||
3185 isteq(list[hdr].n, ist("proxy-connection")) ||
3186 isteq(list[hdr].n, ist("keep-alive")) ||
3187 isteq(list[hdr].n, ist("upgrade")) ||
3188 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003189 continue;
3190
3191 if (isteq(list[hdr].n, ist("")))
3192 break; // end
3193
3194 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3195 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003196 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003197 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003198 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003199 }
3200 }
3201
3202 /* we may need to add END_STREAM */
3203 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3204 es_now = 1;
3205
3206 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003207 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003208
3209 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003210 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003211
3212 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003213 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003214
3215 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003216 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003217 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003218
3219 /* for now we don't implemented CONTINUATION, so we wait for a
3220 * body or directly end in TRL2.
3221 */
3222 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003223 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003224 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003225
Willy Tarreau801250e2018-09-11 11:45:04 +02003226 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003227 h2s->flags |= H2_SF_ES_SENT;
3228 if (h2s->st == H2_SS_OPEN)
3229 h2s->st = H2_SS_HLOC;
3230 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003231 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003232 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003233 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003234 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003235 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003236 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003237 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003238 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003239 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003240
3241 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003242
3243 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003244 //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 +02003245 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003246 full:
3247 h1m_init_res(h1m);
3248 h1m->err_pos = -1; // don't care about errors on the response path
3249 h2c->flags |= H2_CF_MUX_MFULL;
3250 h2s->flags |= H2_SF_BLK_MROOM;
3251 ret = 0;
3252 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003253}
3254
Willy Tarreau5dd17352018-06-14 13:33:30 +02003255/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3256 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3257 * the number of bytes sent. The caller must check the stream's status to
3258 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003259 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003260static 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 +02003261{
3262 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003263 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003264 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003265 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003266 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003267 int es_now = 0;
3268 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003269 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003270 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003271
3272 if (h2c_mux_busy(h2c, h2s)) {
3273 h2s->flags |= H2_SF_BLK_MBUSY;
3274 goto end;
3275 }
3276
Willy Tarreau44e973f2018-03-01 17:49:30 +01003277 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003278 h2c->flags |= H2_CF_MUX_MALLOC;
3279 h2s->flags |= H2_SF_BLK_MROOM;
3280 goto end;
3281 }
3282
3283 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003284 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003285 goto end;
3286
3287 chunk_reset(&outbuf);
3288
3289 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003290 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003291 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003294 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003295 break;
3296 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003298 }
3299
3300 if (outbuf.size < 9) {
3301 h2c->flags |= H2_CF_MUX_MFULL;
3302 h2s->flags |= H2_SF_BLK_MROOM;
3303 goto end;
3304 }
3305
3306 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003307 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3308 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3309 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003310
3311 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3312 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003313 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003314 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003315 break;
3316 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003317 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003318 if ((long long)size > h1m->curr_len)
3319 size = h1m->curr_len;
3320 break;
3321 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003322 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003323 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003324 if (!ret)
3325 goto end;
3326
3327 if (ret < 0) {
3328 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003329 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003330 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3331 goto end;
3332 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003333 max -= ret;
3334 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003336 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003337 }
3338
Willy Tarreau801250e2018-09-11 11:45:04 +02003339 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003340 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003341 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 if (!ret)
3343 goto end;
3344
3345 if (ret < 0) {
3346 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003347 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003348 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3349 goto end;
3350 }
3351
3352 size = chunk;
3353 h1m->curr_len = chunk;
3354 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003355 max -= ret;
3356 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003357 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003358 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003359 if (!size)
3360 goto send_empty;
3361 }
3362
3363 /* in MSG_DATA state, continue below */
3364 size = h1m->curr_len;
3365 break;
3366 }
3367
3368 /* we have in <size> the exact number of bytes we need to copy from
3369 * the H1 buffer. We need to check this against the connection's and
3370 * the stream's send windows, and to ensure that this fits in the max
3371 * frame size and in the buffer's available space minus 9 bytes (for
3372 * the frame header). The connection's flow control is applied last so
3373 * that we can use a separate list of streams which are immediately
3374 * unblocked on window opening. Note: we don't implement padding.
3375 */
3376
Willy Tarreau5dd17352018-06-14 13:33:30 +02003377 if (size > max)
3378 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003379
3380 if (size > h2s->mws)
3381 size = h2s->mws;
3382
3383 if (size <= 0) {
3384 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003385 if (h2s->send_wait) {
3386 LIST_DEL(&h2s->list);
3387 LIST_INIT(&h2s->list);
3388 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003389 goto end;
3390 }
3391
3392 if (h2c->mfs && size > h2c->mfs)
3393 size = h2c->mfs;
3394
3395 if (size + 9 > outbuf.size) {
3396 /* we have an opportunity for enlarging the too small
3397 * available space, let's try.
3398 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003399 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003400 goto realign_again;
3401 size = outbuf.size - 9;
3402 }
3403
3404 if (size <= 0) {
3405 h2c->flags |= H2_CF_MUX_MFULL;
3406 h2s->flags |= H2_SF_BLK_MROOM;
3407 goto end;
3408 }
3409
3410 if (size > h2c->mws)
3411 size = h2c->mws;
3412
3413 if (size <= 0) {
3414 h2s->flags |= H2_SF_BLK_MFCTL;
3415 goto end;
3416 }
3417
3418 /* copy whatever we can */
3419 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003420 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003421 if (ret == 1)
3422 len2 = 0;
3423
3424 if (!ret || len1 + len2 < size) {
3425 /* FIXME: must normally never happen */
3426 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3427 goto end;
3428 }
3429
3430 /* limit len1/len2 to size */
3431 if (len1 + len2 > size) {
3432 int sub = len1 + len2 - size;
3433
3434 if (len2 > sub)
3435 len2 -= sub;
3436 else {
3437 sub -= len2;
3438 len2 = 0;
3439 len1 -= sub;
3440 }
3441 }
3442
3443 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003444 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003445 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003446 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003447
3448 send_empty:
3449 /* we may need to add END_STREAM */
3450 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3451 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003452 *
3453 * FIXME: what we do here is not correct because we send end_stream
3454 * before knowing if we'll have to send a HEADERS frame for the
3455 * trailers. More importantly we're not consuming the trailing CRLF
3456 * after the end of trailers, so it will be left to the caller to
3457 * eat it. The right way to do it would be to measure trailers here
3458 * and to send ES only if there are no trailers.
3459 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003460 */
3461 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003462 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003463 es_now = 1;
3464
3465 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003466 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003467
3468 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003469 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003470
3471 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003472 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003473
3474 /* consume incoming H1 response */
3475 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003476 max -= size;
3477 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003478 total += size;
3479 h1m->curr_len -= size;
3480 h2s->mws -= size;
3481 h2c->mws -= size;
3482
3483 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003484 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003485 goto new_frame;
3486 }
3487 }
3488
3489 if (es_now) {
3490 if (h2s->st == H2_SS_OPEN)
3491 h2s->st = H2_SS_HLOC;
3492 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003493 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003494
Willy Tarreau35a62702018-02-27 15:37:25 +01003495 if (!(h1m->flags & H1_MF_CHNK)) {
3496 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003497 total += max;
3498 ofs += max;
3499 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003500
Willy Tarreau801250e2018-09-11 11:45:04 +02003501 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003502 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003503
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 h2s->flags |= H2_SF_ES_SENT;
3505 }
3506
3507 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003508 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 +02003509 return total;
3510}
3511
Olivier Houchard6ff20392018-07-17 18:46:31 +02003512/* Called from the upper layer, to subscribe to events, such as being able to send */
3513static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3514{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003515 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003516 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003517 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003518
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003519 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003520 sw = param;
3521 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3522 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003523 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003524 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003525 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003526 event_type &= ~SUB_CAN_RECV;
3527 }
3528 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003529 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003530 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003531 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003532 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003533 h2s->send_wait = sw;
3534 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3535 if (h2s->flags & H2_SF_BLK_MFCTL)
3536 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3537 else
3538 LIST_ADDQ(&h2c->send_list, &h2s->list);
3539 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003540 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003541 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003542 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003543 if (event_type != 0)
3544 return -1;
3545 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003546
3547
3548}
3549
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003550static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3551{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003552 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003553 struct h2s *h2s = cs->ctx;
3554
3555 if (event_type & SUB_CAN_RECV) {
3556 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003557 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003558 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003559 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003560 }
3561 }
3562 if (event_type & SUB_CAN_SEND) {
3563 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003564 if (h2s->send_wait == sw) {
3565 LIST_DEL(&h2s->list);
3566 LIST_INIT(&h2s->list);
3567 sw->wait_reason &= ~SUB_CAN_SEND;
3568 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003569 }
3570 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003571 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3572 sw = param;
3573 if (h2s->send_wait == sw) {
3574 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3575 h2s->send_wait = NULL;
3576 }
3577 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003578 return 0;
3579}
3580
3581
Olivier Houchard511efea2018-08-16 15:30:32 +02003582/* Called from the upper layer, to receive data */
3583static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3584{
Olivier Houchard638b7992018-08-16 15:41:52 +02003585 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01003586 struct h2c *h2c = h2s->h2c;
Olivier Houchard511efea2018-08-16 15:30:32 +02003587 size_t ret = 0;
3588
3589 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003590 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003591
Olivier Houchard638b7992018-08-16 15:41:52 +02003592 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003593 cs->flags |= CS_FL_RCV_MORE;
3594 else {
3595 cs->flags &= ~CS_FL_RCV_MORE;
3596 if (cs->flags & CS_FL_REOS)
3597 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003598 if (b_size(&h2s->rxbuf)) {
3599 b_free(&h2s->rxbuf);
3600 offer_buffers(NULL, tasks_run_queue);
3601 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003602 }
3603
Willy Tarreau082f5592018-11-25 08:03:32 +01003604 if (ret && h2c->dsi == h2s->id) {
3605 /* demux is blocking on this stream's buffer */
3606 h2c->flags &= ~H2_CF_DEM_SFULL;
3607 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
3608 if (h2_recv_allowed(h2c))
3609 tasklet_wakeup(h2c->wait_event.task);
3610 }
3611 }
3612
Olivier Houchard511efea2018-08-16 15:30:32 +02003613 return ret;
3614}
3615
Olivier Houchardd846c262018-10-19 17:24:29 +02003616static void h2_stop_senders(struct h2c *h2c)
3617{
3618 struct h2s *h2s, *h2s_back;
3619
3620 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3621 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3622 if (h2c->msi == h2s_id(h2s))
3623 continue;
3624 LIST_DEL(&h2s->list);
3625 LIST_INIT(&h2s->list);
3626 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3627 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3628 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3629 LIST_ADD(&h2c->send_list, &h2s->list);
3630 }
3631}
3632
Willy Tarreau62f52692017-10-08 23:01:42 +02003633/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003634static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003635{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003636 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003637 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003638 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003639
Olivier Houchardd846c262018-10-19 17:24:29 +02003640 if (h2s->send_wait) {
3641 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3642 h2s->send_wait = NULL;
3643 LIST_DEL(&h2s->list);
3644 LIST_INIT(&h2s->list);
3645 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003646 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3647 return 0;
3648
Willy Tarreau0bad0432018-06-14 16:54:01 +02003649 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003650 h2s->flags |= H2_SF_OUTGOING_DATA;
3651
Willy Tarreaua40704a2018-09-11 13:52:04 +02003652 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003653 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003654 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003655 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003656 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003657 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003658 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003659 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003660 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003661 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003662
Willy Tarreau5dd17352018-06-14 13:33:30 +02003663 if (unlikely((int)ret <= 0)) {
3664 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003665 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3666 break;
3667 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003668 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003669 total += count;
3670 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003671 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003672 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003673 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003674 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003675 cs->flags |= CS_FL_ERROR;
3676 break;
3677 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003678
3679 total += ret;
3680 count -= ret;
3681
3682 if (h2s->st >= H2_SS_ERROR)
3683 break;
3684
3685 if (h2s->flags & H2_SF_BLK_ANY)
3686 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003687 }
3688
Willy Tarreau00610962018-07-19 10:58:28 +02003689 if (h2s->st >= H2_SS_ERROR) {
3690 /* trim any possibly pending data after we close (extra CR-LF,
3691 * unprocessed trailers, abnormal extra data, ...)
3692 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003693 total += count;
3694 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003695 }
3696
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003697 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003698 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003699 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003700 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003701 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003702 }
3703
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003704 b_del(buf, total);
Olivier Houchardd846c262018-10-19 17:24:29 +02003705
3706 /* The mux is full, cancel the pending tasks */
3707 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3708 (h2s->flags & H2_SF_BLK_MBUSY))
3709 h2_stop_senders(h2s->h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02003710 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003711 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3712 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003713
Olivier Houchard7505f942018-08-21 18:10:44 +02003714 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003715 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003716}
3717
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003718/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003719static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003720{
3721 struct h2c *h2c = conn->mux_ctx;
3722 struct h2s *h2s;
3723 struct eb32_node *node;
3724 int fctl_cnt = 0;
3725 int send_cnt = 0;
3726 int tree_cnt = 0;
3727 int orph_cnt = 0;
3728
3729 if (!h2c)
3730 return;
3731
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003732 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003733 fctl_cnt++;
3734
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003735 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003736 send_cnt++;
3737
3738 node = eb32_first(&h2c->streams_by_id);
3739 while (node) {
3740 h2s = container_of(node, struct h2s, by_id);
3741 tree_cnt++;
3742 if (!h2s->cs)
3743 orph_cnt++;
3744 node = eb32_next(node);
3745 }
3746
Willy Tarreau616ac812018-07-24 14:12:42 +02003747 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3748 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3749 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3750 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3751 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3752 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003753}
Willy Tarreau62f52692017-10-08 23:01:42 +02003754
3755/*******************************************************/
3756/* functions below are dedicated to the config parsers */
3757/*******************************************************/
3758
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003759/* config parser for global "tune.h2.header-table-size" */
3760static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3761 struct proxy *defpx, const char *file, int line,
3762 char **err)
3763{
3764 if (too_many_args(1, args, err, NULL))
3765 return -1;
3766
3767 h2_settings_header_table_size = atoi(args[1]);
3768 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3769 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3770 return -1;
3771 }
3772 return 0;
3773}
Willy Tarreau62f52692017-10-08 23:01:42 +02003774
Willy Tarreaue6baec02017-07-27 11:45:11 +02003775/* config parser for global "tune.h2.initial-window-size" */
3776static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3777 struct proxy *defpx, const char *file, int line,
3778 char **err)
3779{
3780 if (too_many_args(1, args, err, NULL))
3781 return -1;
3782
3783 h2_settings_initial_window_size = atoi(args[1]);
3784 if (h2_settings_initial_window_size < 0) {
3785 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3786 return -1;
3787 }
3788 return 0;
3789}
3790
Willy Tarreau5242ef82017-07-27 11:47:28 +02003791/* config parser for global "tune.h2.max-concurrent-streams" */
3792static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3793 struct proxy *defpx, const char *file, int line,
3794 char **err)
3795{
3796 if (too_many_args(1, args, err, NULL))
3797 return -1;
3798
3799 h2_settings_max_concurrent_streams = atoi(args[1]);
3800 if (h2_settings_max_concurrent_streams < 0) {
3801 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3802 return -1;
3803 }
3804 return 0;
3805}
3806
Willy Tarreau62f52692017-10-08 23:01:42 +02003807
3808/****************************************/
3809/* MUX initialization and instanciation */
3810/***************************************/
3811
3812/* The mux operations */
3813const struct mux_ops h2_ops = {
3814 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003815 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003816 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003817 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003818 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003819 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003820 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01003821 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02003822 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01003823 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01003824 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02003825 .shutr = h2_shutr,
3826 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003827 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003828 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003829 .name = "H2",
3830};
3831
Christopher Faulet32f61c02018-04-10 14:33:41 +02003832/* PROTO selection : this mux registers PROTO token "h2" */
3833static struct mux_proto_list mux_proto_h2 =
3834 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003835
Willy Tarreau0108d902018-11-25 19:14:37 +01003836INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
3837
Willy Tarreau62f52692017-10-08 23:01:42 +02003838/* config keyword parsers */
3839static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003840 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003841 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003842 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003843 { 0, NULL, NULL }
3844}};
3845
Willy Tarreau0108d902018-11-25 19:14:37 +01003846INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3847
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003848static void __h2_deinit(void)
3849{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003850 pool_destroy(pool_head_h2s);
3851 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003852}
3853
Willy Tarreau62f52692017-10-08 23:01:42 +02003854__attribute__((constructor))
3855static void __h2_init(void)
3856{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003857 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3858 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003859}
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003860
3861REGISTER_POST_DEINIT(__h2_deinit);