blob: a643759f915414c6577a06e4633559ba9231b6ac [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020063#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020064
65
Willy Tarreau5ab6b572017-09-22 08:05:00 +020066/* H2 connection state, in h2c->st0 */
67enum h2_cs {
68 H2_CS_PREFACE, // init done, waiting for connection preface
69 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
70 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
71 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010072 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
73 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020074 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
75 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
76 H2_CS_ENTRIES // must be last
77} __attribute__((packed));
78
79/* H2 connection descriptor */
80struct h2c {
81 struct connection *conn;
82
83 enum h2_cs st0; /* mux state */
84 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
85
86 /* 16 bit hole here */
87 uint32_t flags; /* connection flags: H2_CF_* */
88 int32_t max_id; /* highest ID known on this connection, <0 before preface */
89 uint32_t rcvd_c; /* newly received data to ACK for the connection */
90 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
91
92 /* states for the demux direction */
93 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020094 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095
96 int32_t dsi; /* demux stream ID (<0 = idle) */
97 int32_t dfl; /* demux frame length (if dsi >= 0) */
98 int8_t dft; /* demux frame type (if dsi >= 0) */
99 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100100 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
101 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
103
104 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200105 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200106 int32_t msi; /* mux stream ID (<0 = idle) */
107 int32_t mfl; /* mux frame length (if dsi >= 0) */
108 int8_t mft; /* mux frame type (if dsi >= 0) */
109 int8_t mff; /* mux frame flags (if dsi >= 0) */
110 /* 16 bit hole here */
111 int32_t miw; /* mux initial window size for all new streams */
112 int32_t mws; /* mux window size. Can be negative. */
113 int32_t mfs; /* mux's max frame size */
114
Willy Tarreauea392822017-10-31 10:02:25 +0100115 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100116 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100117 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200118 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200119 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100120 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121 struct eb_root streams_by_id; /* all active streams by their ID */
122 struct list send_list; /* list of blocked streams requesting to send */
123 struct list fctl_list; /* list of streams blocked by connection's fctl */
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
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200342
Willy Tarreau62f52692017-10-08 23:01:42 +0200343/*****************************************************************/
344/* functions below are dedicated to the mux setup and management */
345/*****************************************************************/
346
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200347/* Initialize the mux once it's attached. For outgoing connections, the context
348 * is already initialized before installing the mux, so we detect incoming
349 * connections from the fact that the context is still NULL. Returns < 0 on
350 * error.
351 */
352static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200353{
354 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100355 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200356
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200357 /* we don't support outgoing connections for now */
358 if (conn->mux_ctx)
359 goto fail_no_h2c;
360
Willy Tarreaubafbe012017-11-24 17:34:44 +0100361 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200362 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200363 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364
Willy Tarreau0b37d652018-10-03 10:33:02 +0200365 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
366 if (tick_isset(prx->timeout.clientfin))
367 h2c->shut_timeout = prx->timeout.clientfin;
Willy Tarreau3f133572017-10-31 19:21:06 +0100368
Willy Tarreau0b37d652018-10-03 10:33:02 +0200369 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100370 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100371 if (tick_isset(h2c->timeout)) {
372 t = task_new(tid_bit);
373 if (!t)
374 goto fail;
375
376 h2c->task = t;
377 t->process = h2_timeout_task;
378 t->context = h2c;
379 t->expire = tick_add(now_ms, h2c->timeout);
380 }
Willy Tarreauea392822017-10-31 10:02:25 +0100381
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200382 h2c->wait_event.task = tasklet_new();
383 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200384 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200385 h2c->wait_event.task->process = h2_io_cb;
386 h2c->wait_event.task->context = h2c;
387 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200388
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
390 if (!h2c->ddht)
391 goto fail;
392
393 /* Initialise the context. */
394 h2c->st0 = H2_CS_PREFACE;
395 h2c->conn = conn;
396 h2c->max_id = -1;
397 h2c->errcode = H2_ERR_NO_ERROR;
398 h2c->flags = H2_CF_NONE;
399 h2c->rcvd_c = 0;
400 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100401 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200402 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200403
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200404 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200405 h2c->dsi = -1;
406 h2c->msi = -1;
407 h2c->last_sid = -1;
408
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200409 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200410 h2c->miw = 65535; /* mux initial window size */
411 h2c->mws = 65535; /* mux window size */
412 h2c->mfs = 16384; /* initial max frame size */
413 h2c->streams_by_id = EB_ROOT_UNIQUE;
414 LIST_INIT(&h2c->send_list);
415 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200416 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100417 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 conn->mux_ctx = h2c;
419
Willy Tarreau3f133572017-10-31 19:21:06 +0100420 if (t)
421 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100422
Willy Tarreau0f383582018-10-03 14:22:21 +0200423 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200424 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200426 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100427 if (t)
428 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200429 if (h2c->wait_event.task)
430 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100431 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200432 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return -1;
434}
435
Willy Tarreau2373acc2017-10-12 17:35:14 +0200436/* returns the stream associated with id <id> or NULL if not found */
437static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
438{
439 struct eb32_node *node;
440
Willy Tarreau2a856182017-05-16 15:20:39 +0200441 if (id > h2c->max_id)
442 return (struct h2s *)h2_idle_stream;
443
Willy Tarreau2373acc2017-10-12 17:35:14 +0200444 node = eb32_lookup(&h2c->streams_by_id, id);
445 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200446 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200447
448 return container_of(node, struct h2s, by_id);
449}
450
Willy Tarreau62f52692017-10-08 23:01:42 +0200451/* release function for a connection. This one should be called to free all
452 * resources allocated to the mux.
453 */
454static void h2_release(struct connection *conn)
455{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200456 struct h2c *h2c = conn->mux_ctx;
457
458 LIST_DEL(&conn->list);
459
460 if (h2c) {
461 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200462
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100463 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100464 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100465 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200466
Willy Tarreau44e973f2018-03-01 17:49:30 +0100467 h2_release_buf(h2c, &h2c->dbuf);
468 h2_release_buf(h2c, &h2c->mbuf);
469
Willy Tarreauea392822017-10-31 10:02:25 +0100470 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200471 h2c->task->context = NULL;
472 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100473 h2c->task = NULL;
474 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200475 if (h2c->wait_event.task)
476 tasklet_free(h2c->wait_event.task);
477 if (h2c->wait_event.wait_reason != 0)
478 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
479 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100480
Willy Tarreaubafbe012017-11-24 17:34:44 +0100481 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200482 }
483
484 conn->mux = NULL;
485 conn->mux_ctx = NULL;
486
487 conn_stop_tracking(conn);
488 conn_full_close(conn);
489 if (conn->destroy_cb)
490 conn->destroy_cb(conn);
491 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200492}
493
494
Willy Tarreau71681172017-10-23 14:39:06 +0200495/******************************************************/
496/* functions below are for the H2 protocol processing */
497/******************************************************/
498
499/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100500static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200501{
502 return h2s ? h2s->id : 0;
503}
504
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200505/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100506static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200507{
508 if (h2c->msi < 0)
509 return 0;
510
511 if (h2c->msi == h2s_id(h2s))
512 return 0;
513
514 return 1;
515}
516
Willy Tarreau741d6df2017-10-17 08:00:59 +0200517/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100518static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200519{
520 h2c->errcode = err;
521 h2c->st0 = H2_CS_ERROR;
522}
523
Willy Tarreau2e43f082017-10-17 08:03:59 +0200524/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200526{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200527 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200528 h2s->errcode = err;
529 h2s->st = H2_SS_ERROR;
530 if (h2s->cs)
531 h2s->cs->flags |= CS_FL_ERROR;
532 }
533}
534
Willy Tarreaue4820742017-07-27 13:37:23 +0200535/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100536static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200537{
538 uint8_t *out = frame;
539
540 *out = len >> 16;
541 write_n16(out + 1, len);
542}
543
Willy Tarreau54c15062017-10-10 17:10:03 +0200544/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
545 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
546 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200547 * available in the buffer's input prior to calling this function. The buffer
548 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200549 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100550static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200551 const struct buffer *b, int o)
552{
Willy Tarreau591d4452018-06-15 17:21:00 +0200553 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 +0200554}
555
Willy Tarreau1f094672017-11-20 21:27:45 +0100556static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200557{
Willy Tarreau591d4452018-06-15 17:21:00 +0200558 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200559}
560
Willy Tarreau1f094672017-11-20 21:27:45 +0100561static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200562{
Willy Tarreau591d4452018-06-15 17:21:00 +0200563 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200564}
565
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200567{
Willy Tarreau591d4452018-06-15 17:21:00 +0200568 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200569}
570
571
Willy Tarreau715d5312017-07-11 15:20:24 +0200572/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
573 * is not obvious. It turns out that H2 headers are neither aligned nor do they
574 * use regular sizes. And to add to the trouble, the buffer may wrap so each
575 * byte read must be checked. The header is formed like this :
576 *
577 * b0 b1 b2 b3 b4 b5..b8
578 * +----------+---------+--------+----+----+----------------------+
579 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
580 * +----------+---------+--------+----+----+----------------------+
581 *
582 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
583 * we get the sid properly aligned and ordered, and 16 bits of len properly
584 * ordered as well. The type and flags can be extracted using bit shifts from
585 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200586 * Returns zero if some bytes are missing, otherwise non-zero on success. The
587 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200588 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100589static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200590{
591 uint64_t w;
592
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200593 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200594 return 0;
595
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200596 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200597 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200598 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
599 h->ff = w >> 32;
600 h->ft = w >> 40;
601 h->len += w >> 48;
602 return 1;
603}
604
605/* skip the next 9 bytes corresponding to the frame header possibly parsed by
606 * h2_peek_frame_hdr() above.
607 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100608static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200609{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200610 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200611}
612
613/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100614static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200615{
616 int ret;
617
618 ret = h2_peek_frame_hdr(b, h);
619 if (ret > 0)
620 h2_skip_frame_hdr(b);
621 return ret;
622}
623
Willy Tarreau00dd0782018-03-01 16:31:34 +0100624/* marks stream <h2s> as CLOSED and decrement the number of active streams for
625 * its connection if the stream was not yet closed. Please use this exclusively
626 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100627 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100628static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100629{
630 if (h2s->st != H2_SS_CLOSED)
631 h2s->h2c->nb_streams--;
632 h2s->st = H2_SS_CLOSED;
633}
634
Willy Tarreau71049cc2018-03-28 13:56:39 +0200635/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
636static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100637{
638 h2s_close(h2s);
639 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200640 if (b_size(&h2s->rxbuf)) {
641 b_free(&h2s->rxbuf);
642 offer_buffers(NULL, tasks_run_queue);
643 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200644 if (h2s->send_wait != NULL)
645 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
646 if (h2s->recv_wait != NULL)
647 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
648 /* There's no need to explicitely call unsubscribe here, the only
649 * reference left would be in the h2c send_list/fctl_list, and if
650 * we're in it, we're getting out anyway
651 */
652 LIST_DEL(&h2s->list);
653 LIST_INIT(&h2s->list);
654 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100655 pool_free(pool_head_h2s, h2s);
656}
657
Willy Tarreaua8e49542018-10-03 18:53:55 +0200658/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
659 * stream tree. In case of error, nothing is added and NULL is returned. The
660 * causes of errors can be any failed memory allocation. The caller is
661 * responsible for checking if the connection may support an extra stream
662 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200663 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200664static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200665{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200666 struct h2s *h2s;
667
Willy Tarreaubafbe012017-11-24 17:34:44 +0100668 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200669 if (!h2s)
670 goto out;
671
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200672 h2s->wait_event.task = tasklet_new();
673 if (!h2s->wait_event.task) {
674 pool_free(pool_head_h2s, h2s);
675 goto out;
676 }
677 h2s->send_wait = NULL;
678 h2s->recv_wait = NULL;
679 h2s->wait_event.task->process = h2_deferred_shut;
680 h2s->wait_event.task->context = h2s;
681 h2s->wait_event.handle = NULL;
682 h2s->wait_event.wait_reason = 0;
683 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200684 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200685 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200686 h2s->mws = h2c->miw;
687 h2s->flags = H2_SF_NONE;
688 h2s->errcode = H2_ERR_NO_ERROR;
689 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200690 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200691 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200692 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200693 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200694 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200695 h2s->by_id.key = h2s->id = id;
696 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200697
698 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100699 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200700
701 return h2s;
702
703 out_free_h2s:
704 pool_free(pool_head_h2s, h2s);
705 out:
706 return NULL;
707}
708
709/* creates a new stream <id> on the h2c connection and returns it, or NULL in
710 * case of memory allocation error.
711 */
712static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
713{
714 struct session *sess = h2c->conn->owner;
715 struct conn_stream *cs;
716 struct h2s *h2s;
717
718 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
719 goto out;
720
721 h2s = h2s_new(h2c, id);
722 if (!h2s)
723 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200724
725 cs = cs_new(h2c->conn);
726 if (!cs)
727 goto out_close;
728
729 h2s->cs = cs;
730 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200731 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200732
733 if (stream_create_from_cs(cs) < 0)
734 goto out_free_cs;
735
Willy Tarreau590a0512018-09-05 11:56:48 +0200736 /* We want the accept date presented to the next stream to be the one
737 * we have now, the handshake time to be null (since the next stream
738 * is not delayed by a handshake), and the idle time to count since
739 * right now.
740 */
741 sess->accept_date = date;
742 sess->tv_accept = now;
743 sess->t_handshake = 0;
744
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200745 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200746 if (h2_has_too_many_cs(h2c))
747 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200748 return h2s;
749
750 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200751 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200752 cs_free(cs);
753 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200754 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200755 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200756 sess_log(sess);
757 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200758}
759
Willy Tarreaube5b7152017-09-25 16:25:39 +0200760/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
761 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
762 * the various settings codes.
763 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200764static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200765{
766 struct buffer *res;
767 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200768 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200769 int ret;
770
771 if (h2c_mux_busy(h2c, NULL)) {
772 h2c->flags |= H2_CF_DEM_MBUSY;
773 return 0;
774 }
775
Willy Tarreau44e973f2018-03-01 17:49:30 +0100776 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200777 if (!res) {
778 h2c->flags |= H2_CF_MUX_MALLOC;
779 h2c->flags |= H2_CF_DEM_MROOM;
780 return 0;
781 }
782
783 chunk_init(&buf, buf_data, sizeof(buf_data));
784 chunk_memcpy(&buf,
785 "\x00\x00\x00" /* length : 0 for now */
786 "\x04\x00" /* type : 4 (settings), flags : 0 */
787 "\x00\x00\x00\x00", /* stream ID : 0 */
788 9);
789
790 if (h2_settings_header_table_size != 4096) {
791 char str[6] = "\x00\x01"; /* header_table_size */
792
793 write_n32(str + 2, h2_settings_header_table_size);
794 chunk_memcat(&buf, str, 6);
795 }
796
797 if (h2_settings_initial_window_size != 65535) {
798 char str[6] = "\x00\x04"; /* initial_window_size */
799
800 write_n32(str + 2, h2_settings_initial_window_size);
801 chunk_memcat(&buf, str, 6);
802 }
803
804 if (h2_settings_max_concurrent_streams != 0) {
805 char str[6] = "\x00\x03"; /* max_concurrent_streams */
806
807 /* Note: 0 means "unlimited" for haproxy's config but not for
808 * the protocol, so never send this value!
809 */
810 write_n32(str + 2, h2_settings_max_concurrent_streams);
811 chunk_memcat(&buf, str, 6);
812 }
813
814 if (global.tune.bufsize != 16384) {
815 char str[6] = "\x00\x05"; /* max_frame_size */
816
817 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
818 * match bufsize - rewrite size, but at the moment it seems
819 * that clients don't take care of it.
820 */
821 write_n32(str + 2, global.tune.bufsize);
822 chunk_memcat(&buf, str, 6);
823 }
824
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200825 h2_set_frame_size(buf.area, buf.data - 9);
826 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200827 if (unlikely(ret <= 0)) {
828 if (!ret) {
829 h2c->flags |= H2_CF_MUX_MFULL;
830 h2c->flags |= H2_CF_DEM_MROOM;
831 return 0;
832 }
833 else {
834 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
835 return 0;
836 }
837 }
838 return ret;
839}
840
Willy Tarreau52eed752017-09-22 15:05:09 +0200841/* Try to receive a connection preface, then upon success try to send our
842 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
843 * missing data. It may return an error in h2c.
844 */
845static int h2c_frt_recv_preface(struct h2c *h2c)
846{
847 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200848 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200849
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200850 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200851
852 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200853 if (ret1 < 0)
854 sess_log(h2c->conn->owner);
855
Willy Tarreau52eed752017-09-22 15:05:09 +0200856 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
857 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
858 return 0;
859 }
860
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200861 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200862 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200863 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200864
Willy Tarreaube5b7152017-09-25 16:25:39 +0200865 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200866}
867
Willy Tarreau081d4722017-05-16 21:51:05 +0200868/* try to send a GOAWAY frame on the connection to report an error or a graceful
869 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
870 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
871 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
872 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
873 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
874 * on unrecoverable failure. It will not attempt to send one again in this last
875 * case so that it is safe to use h2c_error() to report such errors.
876 */
877static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
878{
879 struct buffer *res;
880 char str[17];
881 int ret;
882
883 if (h2c->flags & H2_CF_GOAWAY_FAILED)
884 return 1; // claim that it worked
885
886 if (h2c_mux_busy(h2c, h2s)) {
887 if (h2s)
888 h2s->flags |= H2_SF_BLK_MBUSY;
889 else
890 h2c->flags |= H2_CF_DEM_MBUSY;
891 return 0;
892 }
893
Willy Tarreau44e973f2018-03-01 17:49:30 +0100894 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200895 if (!res) {
896 h2c->flags |= H2_CF_MUX_MALLOC;
897 if (h2s)
898 h2s->flags |= H2_SF_BLK_MROOM;
899 else
900 h2c->flags |= H2_CF_DEM_MROOM;
901 return 0;
902 }
903
904 /* len: 8, type: 7, flags: none, sid: 0 */
905 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
906
907 if (h2c->last_sid < 0)
908 h2c->last_sid = h2c->max_id;
909
910 write_n32(str + 9, h2c->last_sid);
911 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200912 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200913 if (unlikely(ret <= 0)) {
914 if (!ret) {
915 h2c->flags |= H2_CF_MUX_MFULL;
916 if (h2s)
917 h2s->flags |= H2_SF_BLK_MROOM;
918 else
919 h2c->flags |= H2_CF_DEM_MROOM;
920 return 0;
921 }
922 else {
923 /* we cannot report this error using GOAWAY, so we mark
924 * it and claim a success.
925 */
926 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
927 h2c->flags |= H2_CF_GOAWAY_FAILED;
928 return 1;
929 }
930 }
931 h2c->flags |= H2_CF_GOAWAY_SENT;
932 return ret;
933}
934
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100935/* Try to send an RST_STREAM frame on the connection for the indicated stream
936 * during mux operations. This stream must be valid and cannot be closed
937 * already. h2s->id will be used for the stream ID and h2s->errcode will be
938 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
939 * not yet.
940 *
941 * Returns > 0 on success or zero if nothing was done. In case of lack of room
942 * to write the message, it subscribes the stream to future notifications.
943 */
944static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
945{
946 struct buffer *res;
947 char str[13];
948 int ret;
949
950 if (!h2s || h2s->st == H2_SS_CLOSED)
951 return 1;
952
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100953 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
954 * RST_STREAM in response to a RST_STREAM frame.
955 */
956 if (h2c->dft == H2_FT_RST_STREAM) {
957 ret = 1;
958 goto ignore;
959 }
960
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100961 if (h2c_mux_busy(h2c, h2s)) {
962 h2s->flags |= H2_SF_BLK_MBUSY;
963 return 0;
964 }
965
Willy Tarreau44e973f2018-03-01 17:49:30 +0100966 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100967 if (!res) {
968 h2c->flags |= H2_CF_MUX_MALLOC;
969 h2s->flags |= H2_SF_BLK_MROOM;
970 return 0;
971 }
972
973 /* len: 4, type: 3, flags: none */
974 memcpy(str, "\x00\x00\x04\x03\x00", 5);
975 write_n32(str + 5, h2s->id);
976 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200977 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100978
979 if (unlikely(ret <= 0)) {
980 if (!ret) {
981 h2c->flags |= H2_CF_MUX_MFULL;
982 h2s->flags |= H2_SF_BLK_MROOM;
983 return 0;
984 }
985 else {
986 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
987 return 0;
988 }
989 }
990
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100991 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100992 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100993 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100994 return ret;
995}
996
997/* Try to send an RST_STREAM frame on the connection for the stream being
998 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
999 * error code unless the stream's state already is IDLE or CLOSED in which
1000 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1001 * it was not yet.
1002 *
1003 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1004 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001005 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001006 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001007 */
1008static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1009{
1010 struct buffer *res;
1011 char str[13];
1012 int ret;
1013
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001014 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1015 * RST_STREAM in response to a RST_STREAM frame.
1016 */
1017 if (h2c->dft == H2_FT_RST_STREAM) {
1018 ret = 1;
1019 goto ignore;
1020 }
1021
Willy Tarreau27a84c92017-10-17 08:10:17 +02001022 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001023 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001024 return 0;
1025 }
1026
Willy Tarreau44e973f2018-03-01 17:49:30 +01001027 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001028 if (!res) {
1029 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001030 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001031 return 0;
1032 }
1033
1034 /* len: 4, type: 3, flags: none */
1035 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001036
Willy Tarreau27a84c92017-10-17 08:10:17 +02001037 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001038 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001039 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001040
Willy Tarreau27a84c92017-10-17 08:10:17 +02001041 if (unlikely(ret <= 0)) {
1042 if (!ret) {
1043 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001044 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001045 return 0;
1046 }
1047 else {
1048 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1049 return 0;
1050 }
1051 }
1052
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001053 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001054 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001055 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001056 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001057 }
1058
Willy Tarreau27a84c92017-10-17 08:10:17 +02001059 return ret;
1060}
1061
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001062/* try to send an empty DATA frame with the ES flag set to notify about the
1063 * end of stream and match a shutdown(write). If an ES was already sent as
1064 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1065 * on success or zero if nothing was done. In case of lack of room to write the
1066 * message, it subscribes the requesting stream to future notifications.
1067 */
1068static int h2_send_empty_data_es(struct h2s *h2s)
1069{
1070 struct h2c *h2c = h2s->h2c;
1071 struct buffer *res;
1072 char str[9];
1073 int ret;
1074
Willy Tarreau721c9742017-11-07 11:05:42 +01001075 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001076 return 1;
1077
1078 if (h2c_mux_busy(h2c, h2s)) {
1079 h2s->flags |= H2_SF_BLK_MBUSY;
1080 return 0;
1081 }
1082
Willy Tarreau44e973f2018-03-01 17:49:30 +01001083 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001084 if (!res) {
1085 h2c->flags |= H2_CF_MUX_MALLOC;
1086 h2s->flags |= H2_SF_BLK_MROOM;
1087 return 0;
1088 }
1089
1090 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1091 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1092 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001093 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001094 if (likely(ret > 0)) {
1095 h2s->flags |= H2_SF_ES_SENT;
1096 }
1097 else if (!ret) {
1098 h2c->flags |= H2_CF_MUX_MFULL;
1099 h2s->flags |= H2_SF_BLK_MROOM;
1100 return 0;
1101 }
1102 else {
1103 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1104 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001105 }
1106 return ret;
1107}
1108
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001109/* wake the streams attached to the connection, whose id is greater than <last>,
1110 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001111 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1112 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001113 */
1114static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1115{
1116 struct eb32_node *node;
1117 struct h2s *h2s;
1118
1119 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1120 flags |= CS_FL_ERROR;
1121
1122 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001123 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001124
1125 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1126 while (node) {
1127 h2s = container_of(node, struct h2s, by_id);
1128 if (h2s->id <= last)
1129 break;
1130 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001131
1132 if (!h2s->cs) {
1133 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001134 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001135 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001136 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001137
1138 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001139 if (h2s->recv_wait) {
1140 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001141 sw->wait_reason &= ~SUB_CAN_RECV;
1142 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001143 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001144 } else if (h2s->cs->data_cb->wake != NULL)
1145 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001146
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001147 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1148 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001149 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001150 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001151 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001152 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001153 }
1154}
1155
Willy Tarreau3421aba2017-07-27 15:41:03 +02001156/* Increase all streams' outgoing window size by the difference passed in
1157 * argument. This is needed upon receipt of the settings frame if the initial
1158 * window size is different. The difference may be negative and the resulting
1159 * window size as well, for the time it takes to receive some window updates.
1160 */
1161static void h2c_update_all_ws(struct h2c *h2c, int diff)
1162{
1163 struct h2s *h2s;
1164 struct eb32_node *node;
1165
1166 if (!diff)
1167 return;
1168
1169 node = eb32_first(&h2c->streams_by_id);
1170 while (node) {
1171 h2s = container_of(node, struct h2s, by_id);
1172 h2s->mws += diff;
1173 node = eb32_next(node);
1174 }
1175}
1176
1177/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1178 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1179 * return an error in h2c. Described in RFC7540#6.5.
1180 */
1181static int h2c_handle_settings(struct h2c *h2c)
1182{
1183 unsigned int offset;
1184 int error;
1185
1186 if (h2c->dff & H2_F_SETTINGS_ACK) {
1187 if (h2c->dfl) {
1188 error = H2_ERR_FRAME_SIZE_ERROR;
1189 goto fail;
1190 }
1191 return 1;
1192 }
1193
1194 if (h2c->dsi != 0) {
1195 error = H2_ERR_PROTOCOL_ERROR;
1196 goto fail;
1197 }
1198
1199 if (h2c->dfl % 6) {
1200 error = H2_ERR_FRAME_SIZE_ERROR;
1201 goto fail;
1202 }
1203
1204 /* that's the limit we can process */
1205 if (h2c->dfl > global.tune.bufsize) {
1206 error = H2_ERR_FRAME_SIZE_ERROR;
1207 goto fail;
1208 }
1209
1210 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001211 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001212 return 0;
1213
1214 /* parse the frame */
1215 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001216 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1217 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001218
1219 switch (type) {
1220 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1221 /* we need to update all existing streams with the
1222 * difference from the previous iws.
1223 */
1224 if (arg < 0) { // RFC7540#6.5.2
1225 error = H2_ERR_FLOW_CONTROL_ERROR;
1226 goto fail;
1227 }
1228 h2c_update_all_ws(h2c, arg - h2c->miw);
1229 h2c->miw = arg;
1230 break;
1231 case H2_SETTINGS_MAX_FRAME_SIZE:
1232 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1233 error = H2_ERR_PROTOCOL_ERROR;
1234 goto fail;
1235 }
1236 h2c->mfs = arg;
1237 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001238 case H2_SETTINGS_ENABLE_PUSH:
1239 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1240 error = H2_ERR_PROTOCOL_ERROR;
1241 goto fail;
1242 }
1243 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001244 }
1245 }
1246
1247 /* need to ACK this frame now */
1248 h2c->st0 = H2_CS_FRAME_A;
1249 return 1;
1250 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001251 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001252 h2c_error(h2c, error);
1253 return 0;
1254}
1255
1256/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1257 * success or one of the h2_status values.
1258 */
1259static int h2c_ack_settings(struct h2c *h2c)
1260{
1261 struct buffer *res;
1262 char str[9];
1263 int ret = -1;
1264
1265 if (h2c_mux_busy(h2c, NULL)) {
1266 h2c->flags |= H2_CF_DEM_MBUSY;
1267 return 0;
1268 }
1269
Willy Tarreau44e973f2018-03-01 17:49:30 +01001270 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001271 if (!res) {
1272 h2c->flags |= H2_CF_MUX_MALLOC;
1273 h2c->flags |= H2_CF_DEM_MROOM;
1274 return 0;
1275 }
1276
1277 memcpy(str,
1278 "\x00\x00\x00" /* length : 0 (no data) */
1279 "\x04" "\x01" /* type : 4, flags : ACK */
1280 "\x00\x00\x00\x00" /* stream ID */, 9);
1281
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001282 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001283 if (unlikely(ret <= 0)) {
1284 if (!ret) {
1285 h2c->flags |= H2_CF_MUX_MFULL;
1286 h2c->flags |= H2_CF_DEM_MROOM;
1287 return 0;
1288 }
1289 else {
1290 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1291 return 0;
1292 }
1293 }
1294 return ret;
1295}
1296
Willy Tarreaucf68c782017-10-10 17:11:41 +02001297/* processes a PING frame and schedules an ACK if needed. The caller must pass
1298 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1299 * missing data. It may return an error in h2c.
1300 */
1301static int h2c_handle_ping(struct h2c *h2c)
1302{
1303 /* frame length must be exactly 8 */
1304 if (h2c->dfl != 8) {
1305 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1306 return 0;
1307 }
1308
1309 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001310 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001311 h2c->st0 = H2_CS_FRAME_A;
1312 return 1;
1313}
1314
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001315/* Try to send a window update for stream id <sid> and value <increment>.
1316 * Returns > 0 on success or zero on missing room or failure. It may return an
1317 * error in h2c.
1318 */
1319static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1320{
1321 struct buffer *res;
1322 char str[13];
1323 int ret = -1;
1324
1325 if (h2c_mux_busy(h2c, NULL)) {
1326 h2c->flags |= H2_CF_DEM_MBUSY;
1327 return 0;
1328 }
1329
Willy Tarreau44e973f2018-03-01 17:49:30 +01001330 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001331 if (!res) {
1332 h2c->flags |= H2_CF_MUX_MALLOC;
1333 h2c->flags |= H2_CF_DEM_MROOM;
1334 return 0;
1335 }
1336
1337 /* length: 4, type: 8, flags: none */
1338 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1339 write_n32(str + 5, sid);
1340 write_n32(str + 9, increment);
1341
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001342 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001343
1344 if (unlikely(ret <= 0)) {
1345 if (!ret) {
1346 h2c->flags |= H2_CF_MUX_MFULL;
1347 h2c->flags |= H2_CF_DEM_MROOM;
1348 return 0;
1349 }
1350 else {
1351 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1352 return 0;
1353 }
1354 }
1355 return ret;
1356}
1357
1358/* try to send pending window update for the connection. It's safe to call it
1359 * with no pending updates. Returns > 0 on success or zero on missing room or
1360 * failure. It may return an error in h2c.
1361 */
1362static int h2c_send_conn_wu(struct h2c *h2c)
1363{
1364 int ret = 1;
1365
1366 if (h2c->rcvd_c <= 0)
1367 return 1;
1368
1369 /* send WU for the connection */
1370 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1371 if (ret > 0)
1372 h2c->rcvd_c = 0;
1373
1374 return ret;
1375}
1376
1377/* try to send pending window update for the current dmux stream. It's safe to
1378 * call it with no pending updates. Returns > 0 on success or zero on missing
1379 * room or failure. It may return an error in h2c.
1380 */
1381static int h2c_send_strm_wu(struct h2c *h2c)
1382{
1383 int ret = 1;
1384
1385 if (h2c->rcvd_s <= 0)
1386 return 1;
1387
1388 /* send WU for the stream */
1389 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1390 if (ret > 0)
1391 h2c->rcvd_s = 0;
1392
1393 return ret;
1394}
1395
Willy Tarreaucf68c782017-10-10 17:11:41 +02001396/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1397 * success, 0 on missing data or one of the h2_status values.
1398 */
1399static int h2c_ack_ping(struct h2c *h2c)
1400{
1401 struct buffer *res;
1402 char str[17];
1403 int ret = -1;
1404
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001405 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001406 return 0;
1407
1408 if (h2c_mux_busy(h2c, NULL)) {
1409 h2c->flags |= H2_CF_DEM_MBUSY;
1410 return 0;
1411 }
1412
Willy Tarreau44e973f2018-03-01 17:49:30 +01001413 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001414 if (!res) {
1415 h2c->flags |= H2_CF_MUX_MALLOC;
1416 h2c->flags |= H2_CF_DEM_MROOM;
1417 return 0;
1418 }
1419
1420 memcpy(str,
1421 "\x00\x00\x08" /* length : 8 (same payload) */
1422 "\x06" "\x01" /* type : 6, flags : ACK */
1423 "\x00\x00\x00\x00" /* stream ID */, 9);
1424
1425 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001426 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001427
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001428 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001429 if (unlikely(ret <= 0)) {
1430 if (!ret) {
1431 h2c->flags |= H2_CF_MUX_MFULL;
1432 h2c->flags |= H2_CF_DEM_MROOM;
1433 return 0;
1434 }
1435 else {
1436 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1437 return 0;
1438 }
1439 }
1440 return ret;
1441}
1442
Willy Tarreau26f95952017-07-27 17:18:30 +02001443/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1444 * Returns > 0 on success or zero on missing data. It may return an error in
1445 * h2c or h2s. Described in RFC7540#6.9.
1446 */
1447static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1448{
1449 int32_t inc;
1450 int error;
1451
1452 if (h2c->dfl != 4) {
1453 error = H2_ERR_FRAME_SIZE_ERROR;
1454 goto conn_err;
1455 }
1456
1457 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001458 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001459 return 0;
1460
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001461 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001462
1463 if (h2c->dsi != 0) {
1464 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001465
1466 /* it's not an error to receive WU on a closed stream */
1467 if (h2s->st == H2_SS_CLOSED)
1468 return 1;
1469
1470 if (!inc) {
1471 error = H2_ERR_PROTOCOL_ERROR;
1472 goto strm_err;
1473 }
1474
1475 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1476 error = H2_ERR_FLOW_CONTROL_ERROR;
1477 goto strm_err;
1478 }
1479
1480 h2s->mws += inc;
1481 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1482 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001483 if (h2s->send_wait)
1484 LIST_ADDQ(&h2c->send_list, &h2s->list);
1485
Willy Tarreau26f95952017-07-27 17:18:30 +02001486 }
1487 }
1488 else {
1489 /* connection window update */
1490 if (!inc) {
1491 error = H2_ERR_PROTOCOL_ERROR;
1492 goto conn_err;
1493 }
1494
1495 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1496 error = H2_ERR_FLOW_CONTROL_ERROR;
1497 goto conn_err;
1498 }
1499
1500 h2c->mws += inc;
1501 }
1502
1503 return 1;
1504
1505 conn_err:
1506 h2c_error(h2c, error);
1507 return 0;
1508
1509 strm_err:
1510 if (h2s) {
1511 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001512 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001513 }
1514 else
1515 h2c_error(h2c, error);
1516 return 0;
1517}
1518
Willy Tarreaue96b0922017-10-30 00:28:29 +01001519/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1520 * the last ID. Returns > 0 on success or zero on missing data. It may return
1521 * an error in h2c. Described in RFC7540#6.8.
1522 */
1523static int h2c_handle_goaway(struct h2c *h2c)
1524{
1525 int error;
1526 int last;
1527
1528 if (h2c->dsi != 0) {
1529 error = H2_ERR_PROTOCOL_ERROR;
1530 goto conn_err;
1531 }
1532
1533 if (h2c->dfl < 8) {
1534 error = H2_ERR_FRAME_SIZE_ERROR;
1535 goto conn_err;
1536 }
1537
1538 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001539 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001540 return 0;
1541
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001542 last = h2_get_n32(&h2c->dbuf, 0);
1543 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001544 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001545 if (h2c->last_sid < 0)
1546 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001547 return 1;
1548
1549 conn_err:
1550 h2c_error(h2c, error);
1551 return 0;
1552}
1553
Willy Tarreau92153fc2017-12-03 19:46:19 +01001554/* processes a PRIORITY frame, and either skips it or rejects if it is
1555 * invalid. Returns > 0 on success or zero on missing data. It may return
1556 * an error in h2c. Described in RFC7540#6.3.
1557 */
1558static int h2c_handle_priority(struct h2c *h2c)
1559{
1560 int error;
1561
1562 if (h2c->dsi == 0) {
1563 error = H2_ERR_PROTOCOL_ERROR;
1564 goto conn_err;
1565 }
1566
1567 if (h2c->dfl != 5) {
1568 error = H2_ERR_FRAME_SIZE_ERROR;
1569 goto conn_err;
1570 }
1571
1572 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001573 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001574 return 0;
1575
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001576 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001577 /* 7540#5.3 : can't depend on itself */
1578 error = H2_ERR_PROTOCOL_ERROR;
1579 goto conn_err;
1580 }
1581 return 1;
1582
1583 conn_err:
1584 h2c_error(h2c, error);
1585 return 0;
1586}
1587
Willy Tarreaucd234e92017-08-18 10:59:39 +02001588/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1589 * Returns > 0 on success or zero on missing data. It may return an error in
1590 * h2c. Described in RFC7540#6.4.
1591 */
1592static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1593{
1594 int error;
1595
1596 if (h2c->dsi == 0) {
1597 error = H2_ERR_PROTOCOL_ERROR;
1598 goto conn_err;
1599 }
1600
Willy Tarreaucd234e92017-08-18 10:59:39 +02001601 if (h2c->dfl != 4) {
1602 error = H2_ERR_FRAME_SIZE_ERROR;
1603 goto conn_err;
1604 }
1605
1606 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001607 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001608 return 0;
1609
1610 /* late RST, already handled */
1611 if (h2s->st == H2_SS_CLOSED)
1612 return 1;
1613
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001614 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001615 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001616
1617 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001618 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001619 if (h2s->recv_wait) {
1620 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001621
1622 sw->wait_reason &= ~SUB_CAN_RECV;
1623 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001624 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001625 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001626 }
1627
1628 h2s->flags |= H2_SF_RST_RCVD;
1629 return 1;
1630
1631 conn_err:
1632 h2c_error(h2c, error);
1633 return 0;
1634}
1635
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001636/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1637 * It may return an error in h2c or h2s. The caller must consider that the
1638 * return value is the new h2s in case one was allocated (most common case).
1639 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001640 * errors here are reported as connection errors since it's impossible to
1641 * recover from such errors after the compression context has been altered.
1642 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001643static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001644{
1645 int error;
1646
1647 if (!h2c->dfl) {
1648 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001649 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001650 goto strm_err;
1651 }
1652
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001653 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001654 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001655
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001656 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001657 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001658
Willy Tarreauf2101912018-07-19 10:11:38 +02001659 if (h2c->flags & H2_CF_DEM_TOOMANY)
1660 return 0; // too many cs still present
1661
Willy Tarreau13278b42017-10-13 19:23:14 +02001662 /* now either the frame is complete or the buffer is complete */
1663 if (h2s->st != H2_SS_IDLE) {
1664 /* FIXME: stream already exists, this is only allowed for
1665 * trailers (not supported for now).
1666 */
1667 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001668 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001669 goto conn_err;
1670 }
1671 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1672 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1673 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001674 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001675 goto conn_err;
1676 }
1677
Willy Tarreau22de8d32018-09-05 19:55:58 +02001678 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001679 * positively from h2c_frt_stream_new(), the stream will report the error,
1680 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001681 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001682 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001683 if (!h2s) {
1684 error = H2_ERR_INTERNAL_ERROR;
1685 goto conn_err;
1686 }
1687
1688 h2s->st = H2_SS_OPEN;
1689 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1690 h2s->st = H2_SS_HREM;
1691 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001692 /* note: cs cannot be null for now (just created above) */
1693 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001694 }
1695
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001696 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001697 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001698
Willy Tarreau8f650c32017-11-21 19:36:21 +01001699 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001700 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001701
Willy Tarreau721c9742017-11-07 11:05:42 +01001702 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001703 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001704 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001705 }
1706 else {
1707 /* update the max stream ID if the request is being processed */
1708 if (h2s->id > h2c->max_id)
1709 h2c->max_id = h2s->id;
1710 }
1711
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001712 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001713
1714 conn_err:
1715 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001716 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001717
1718 strm_err:
1719 if (h2s) {
1720 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001721 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001722 }
1723 else
1724 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001725 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001726}
1727
Willy Tarreau454f9052017-10-26 19:40:35 +02001728/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1729 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1730 */
1731static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1732{
1733 int error;
1734
1735 /* note that empty DATA frames are perfectly valid and sometimes used
1736 * to signal an end of stream (with the ES flag).
1737 */
1738
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001739 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001740 return 0; // empty buffer
1741
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001742 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001743 return 0; // incomplete frame
1744
1745 /* now either the frame is complete or the buffer is complete */
1746
1747 if (!h2c->dsi) {
1748 /* RFC7540#6.1 */
1749 error = H2_ERR_PROTOCOL_ERROR;
1750 goto conn_err;
1751 }
1752
1753 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1754 /* RFC7540#6.1 */
1755 error = H2_ERR_STREAM_CLOSED;
1756 goto strm_err;
1757 }
1758
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001759 if (!h2_frt_transfer_data(h2s))
1760 return 0;
1761
Willy Tarreau454f9052017-10-26 19:40:35 +02001762 /* call the upper layers to process the frame, then let the upper layer
1763 * notify the stream about any change.
1764 */
1765 if (!h2s->cs) {
1766 error = H2_ERR_STREAM_CLOSED;
1767 goto strm_err;
1768 }
1769
Willy Tarreau8f650c32017-11-21 19:36:21 +01001770 if (h2c->st0 >= H2_CS_ERROR)
1771 return 0;
1772
Willy Tarreau721c9742017-11-07 11:05:42 +01001773 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001774 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001775 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001776 }
1777
1778 /* check for completion : the callee will change this to FRAME_A or
1779 * FRAME_H once done.
1780 */
1781 if (h2c->st0 == H2_CS_FRAME_P)
1782 return 0;
1783
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001784
1785 /* last frame */
1786 if (h2c->dff & H2_F_DATA_END_STREAM) {
1787 h2s->st = H2_SS_HREM;
1788 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001789 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001790 }
1791
Willy Tarreau454f9052017-10-26 19:40:35 +02001792 return 1;
1793
1794 conn_err:
1795 h2c_error(h2c, error);
1796 return 0;
1797
1798 strm_err:
1799 if (h2s) {
1800 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001801 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001802 }
1803 else
1804 h2c_error(h2c, error);
1805 return 0;
1806}
1807
Willy Tarreaubc933932017-10-09 16:21:43 +02001808/* process Rx frames to be demultiplexed */
1809static void h2_process_demux(struct h2c *h2c)
1810{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001811 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001812
Willy Tarreau081d4722017-05-16 21:51:05 +02001813 if (h2c->st0 >= H2_CS_ERROR)
1814 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001815
1816 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1817 if (h2c->st0 == H2_CS_PREFACE) {
1818 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1819 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001820 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001821 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001822 sess_log(h2c->conn->owner);
1823 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001824 goto fail;
1825 }
1826
1827 h2c->max_id = 0;
1828 h2c->st0 = H2_CS_SETTINGS1;
1829 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001830
1831 if (h2c->st0 == H2_CS_SETTINGS1) {
1832 struct h2_fh hdr;
1833
1834 /* ensure that what is pending is a valid SETTINGS frame
1835 * without an ACK.
1836 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001837 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001838 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001839 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001840 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001841 sess_log(h2c->conn->owner);
1842 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001843 goto fail;
1844 }
1845
1846 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1847 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1848 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1849 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001850 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001851 goto fail;
1852 }
1853
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001854 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001855 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1856 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1857 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001858 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001859 goto fail;
1860 }
1861
1862 /* that's OK, switch to FRAME_P to process it */
1863 h2c->dfl = hdr.len;
1864 h2c->dsi = hdr.sid;
1865 h2c->dft = hdr.ft;
1866 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001867 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001868 h2c->st0 = H2_CS_FRAME_P;
1869 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001870 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001871
1872 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001874 int ret = 0;
1875
1876 if (h2c->st0 >= H2_CS_ERROR)
1877 break;
1878
1879 if (h2c->st0 == H2_CS_FRAME_H) {
1880 struct h2_fh hdr;
1881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001883 break;
1884
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001885 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001886 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1887 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001888 if (!h2c->nb_streams) {
1889 /* only log if no other stream can report the error */
1890 sess_log(h2c->conn->owner);
1891 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001892 break;
1893 }
1894
1895 h2c->dfl = hdr.len;
1896 h2c->dsi = hdr.sid;
1897 h2c->dft = hdr.ft;
1898 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001899 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001900 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001901 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001902 }
1903
1904 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1906
Olivier Houchard638b7992018-08-16 15:41:52 +02001907 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001908 /* we may have to signal the upper layers */
1909 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001910 if (h2s->recv_wait) {
1911 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1912 tasklet_wakeup(h2s->recv_wait->task);
1913 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001914 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001915 if (h2c->st0 >= H2_CS_ERROR)
1916 goto strm_err;
1917
1918 if (h2s->st >= H2_SS_ERROR) {
1919 /* stream error : send RST_STREAM */
1920 h2c->st0 = H2_CS_FRAME_E;
1921 }
1922 }
1923 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001924
Willy Tarreaud7901432017-12-29 11:34:40 +01001925 if (h2c->st0 == H2_CS_FRAME_E)
1926 goto strm_err;
1927
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001928 if (h2s->st == H2_SS_IDLE &&
1929 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1930 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1931 * this state MUST be treated as a connection error
1932 */
1933 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1934 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001935 if (!h2c->nb_streams) {
1936 /* only log if no other stream can report the error */
1937 sess_log(h2c->conn->owner);
1938 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001939 break;
1940 }
1941
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001942 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1943 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1944 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1945 * this state MUST be treated as a stream error
1946 */
1947 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1948 goto strm_err;
1949 }
1950
Willy Tarreauab837502017-12-27 15:07:30 +01001951 /* Below the management of frames received in closed state is a
1952 * bit hackish because the spec makes strong differences between
1953 * streams closed by receiving RST, sending RST, and seeing ES
1954 * in both directions. In addition to this, the creation of a
1955 * new stream reusing the identifier of a closed one will be
1956 * detected here. Given that we cannot keep track of all closed
1957 * streams forever, we consider that unknown closed streams were
1958 * closed on RST received, which allows us to respond with an
1959 * RST without breaking the connection (eg: to abort a transfer).
1960 * Some frames have to be silently ignored as well.
1961 */
1962 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1963 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1964 /* #5.1.1: The identifier of a newly
1965 * established stream MUST be numerically
1966 * greater than all streams that the initiating
1967 * endpoint has opened or reserved. This
1968 * governs streams that are opened using a
1969 * HEADERS frame and streams that are reserved
1970 * using PUSH_PROMISE. An endpoint that
1971 * receives an unexpected stream identifier
1972 * MUST respond with a connection error.
1973 */
1974 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1975 goto strm_err;
1976 }
1977
1978 if (h2s->flags & H2_SF_RST_RCVD) {
1979 /* RFC7540#5.1:closed: an endpoint that
1980 * receives any frame other than PRIORITY after
1981 * receiving a RST_STREAM MUST treat that as a
1982 * stream error of type STREAM_CLOSED.
1983 *
1984 * Note that old streams fall into this category
1985 * and will lead to an RST being sent.
1986 */
1987 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1988 h2c->st0 = H2_CS_FRAME_E;
1989 goto strm_err;
1990 }
1991
1992 /* RFC7540#5.1:closed: if this state is reached as a
1993 * result of sending a RST_STREAM frame, the peer that
1994 * receives the RST_STREAM might have already sent
1995 * frames on the stream that cannot be withdrawn. An
1996 * endpoint MUST ignore frames that it receives on
1997 * closed streams after it has sent a RST_STREAM
1998 * frame. An endpoint MAY choose to limit the period
1999 * over which it ignores frames and treat frames that
2000 * arrive after this time as being in error.
2001 */
2002 if (!(h2s->flags & H2_SF_RST_SENT)) {
2003 /* RFC7540#5.1:closed: any frame other than
2004 * PRIO/WU/RST in this state MUST be treated as
2005 * a connection error
2006 */
2007 if (h2c->dft != H2_FT_RST_STREAM &&
2008 h2c->dft != H2_FT_PRIORITY &&
2009 h2c->dft != H2_FT_WINDOW_UPDATE) {
2010 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2011 goto strm_err;
2012 }
2013 }
2014 }
2015
Willy Tarreauc0da1962017-10-30 18:38:00 +01002016#if 0
2017 // problem below: it is not possible to completely ignore such
2018 // streams as we need to maintain the compression state as well
2019 // and for this we need to completely process these frames (eg:
2020 // HEADERS frames) as well as counting DATA frames to emit
2021 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2022 // This is a typical case of layer violation where the
2023 // transported contents are critical to the connection's
2024 // validity and must be ignored at the same time :-(
2025
2026 /* graceful shutdown, ignore streams whose ID is higher than
2027 * the one advertised in GOAWAY. RFC7540#6.8.
2028 */
2029 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002030 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2031 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002032 h2c->dfl -= ret;
2033 ret = h2c->dfl == 0;
2034 goto strm_err;
2035 }
2036#endif
2037
Willy Tarreau7e98c052017-10-10 15:56:59 +02002038 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002039 case H2_FT_SETTINGS:
2040 if (h2c->st0 == H2_CS_FRAME_P)
2041 ret = h2c_handle_settings(h2c);
2042
2043 if (h2c->st0 == H2_CS_FRAME_A)
2044 ret = h2c_ack_settings(h2c);
2045 break;
2046
Willy Tarreaucf68c782017-10-10 17:11:41 +02002047 case H2_FT_PING:
2048 if (h2c->st0 == H2_CS_FRAME_P)
2049 ret = h2c_handle_ping(h2c);
2050
2051 if (h2c->st0 == H2_CS_FRAME_A)
2052 ret = h2c_ack_ping(h2c);
2053 break;
2054
Willy Tarreau26f95952017-07-27 17:18:30 +02002055 case H2_FT_WINDOW_UPDATE:
2056 if (h2c->st0 == H2_CS_FRAME_P)
2057 ret = h2c_handle_window_update(h2c, h2s);
2058 break;
2059
Willy Tarreau61290ec2017-10-17 08:19:21 +02002060 case H2_FT_CONTINUATION:
2061 /* we currently don't support CONTINUATION frames since
2062 * we have nowhere to store the partial HEADERS frame.
2063 * Let's abort the stream on an INTERNAL_ERROR here.
2064 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002065 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002066 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002067 h2c->st0 = H2_CS_FRAME_E;
2068 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002069 break;
2070
Willy Tarreau13278b42017-10-13 19:23:14 +02002071 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002072 if (h2c->st0 == H2_CS_FRAME_P) {
2073 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2074 if (tmp_h2s) {
2075 h2s = tmp_h2s;
2076 ret = 1;
2077 }
2078 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002079 break;
2080
Willy Tarreau454f9052017-10-26 19:40:35 +02002081 case H2_FT_DATA:
2082 if (h2c->st0 == H2_CS_FRAME_P)
2083 ret = h2c_frt_handle_data(h2c, h2s);
2084
2085 if (h2c->st0 == H2_CS_FRAME_A)
2086 ret = h2c_send_strm_wu(h2c);
2087 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002088
Willy Tarreau92153fc2017-12-03 19:46:19 +01002089 case H2_FT_PRIORITY:
2090 if (h2c->st0 == H2_CS_FRAME_P)
2091 ret = h2c_handle_priority(h2c);
2092 break;
2093
Willy Tarreaucd234e92017-08-18 10:59:39 +02002094 case H2_FT_RST_STREAM:
2095 if (h2c->st0 == H2_CS_FRAME_P)
2096 ret = h2c_handle_rst_stream(h2c, h2s);
2097 break;
2098
Willy Tarreaue96b0922017-10-30 00:28:29 +01002099 case H2_FT_GOAWAY:
2100 if (h2c->st0 == H2_CS_FRAME_P)
2101 ret = h2c_handle_goaway(h2c);
2102 break;
2103
Willy Tarreau1c661982017-10-30 13:52:01 +01002104 case H2_FT_PUSH_PROMISE:
2105 /* not permitted here, RFC7540#5.1 */
2106 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002107 if (!h2c->nb_streams) {
2108 /* only log if no other stream can report the error */
2109 sess_log(h2c->conn->owner);
2110 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002111 break;
2112
2113 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002114 default:
2115 /* drop frames that we ignore. They may be larger than
2116 * the buffer so we drain all of their contents until
2117 * we reach the end.
2118 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002119 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2120 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002121 h2c->dfl -= ret;
2122 ret = h2c->dfl == 0;
2123 }
2124
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002125 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002126 /* We may have to send an RST if not done yet */
2127 if (h2s->st == H2_SS_ERROR)
2128 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002129
Willy Tarreaua20a5192017-12-27 11:02:06 +01002130 if (h2c->st0 == H2_CS_FRAME_E)
2131 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002132
Willy Tarreau7e98c052017-10-10 15:56:59 +02002133 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002134 if (ret <= 0) {
2135 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002136 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002137 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002138
2139 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002140 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002141 h2c->st0 = H2_CS_FRAME_H;
2142 }
2143 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002144
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002145 if (h2c->rcvd_c > 0 &&
2146 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2147 h2c_send_conn_wu(h2c);
2148
Willy Tarreau52eed752017-09-22 15:05:09 +02002149 fail:
2150 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002151 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002152 /* we may have to signal the upper layers */
2153 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002154 if (h2s->recv_wait) {
2155 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2156 tasklet_wakeup(h2s->recv_wait->task);
2157 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002158 }
2159 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002160 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002161}
2162
2163/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2164 * the end.
2165 */
2166static int h2_process_mux(struct h2c *h2c)
2167{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002168 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002169
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002170 /* start by sending possibly pending window updates */
2171 if (h2c->rcvd_c > 0 &&
2172 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2173 h2c_send_conn_wu(h2c) < 0)
2174 goto fail;
2175
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002176 /* First we always process the flow control list because the streams
2177 * waiting there were already elected for immediate emission but were
2178 * blocked just on this.
2179 */
2180
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002181 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002182 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2183 h2c->st0 >= H2_CS_ERROR)
2184 break;
2185
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002186 h2s->flags &= ~H2_SF_BLK_ANY;
2187 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002188 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002189 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002190 LIST_DEL(&h2s->list);
2191 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002192 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002193 }
2194
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002195 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002196 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2197 break;
2198
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002199 h2s->flags &= ~H2_SF_BLK_ANY;
2200 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002201 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002202 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002203 LIST_DEL(&h2s->list);
2204 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002205 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002206 }
2207
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002208 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002209 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002210 if (h2c->st0 == H2_CS_ERROR) {
2211 if (h2c->max_id >= 0) {
2212 h2c_send_goaway_error(h2c, NULL);
2213 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2214 return 0;
2215 }
2216
2217 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2218 }
2219 return 1;
2220 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002221 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002222}
2223
Willy Tarreau62f52692017-10-08 23:01:42 +02002224
Willy Tarreau479998a2018-11-18 06:30:59 +01002225/* Attempt to read data, and subscribe if none available.
2226 * The function returns 1 if data has been received, otherwise zero.
2227 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002228static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002229{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002230 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002231 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002232 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002233 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002234
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002235 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002236 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002237
Willy Tarreau315d8072017-12-10 22:17:57 +01002238 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002239 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240
Willy Tarreau44e973f2018-03-01 17:49:30 +01002241 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002242 if (!buf) {
2243 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002244 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002245 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002246
Olivier Houchard7505f942018-08-21 18:10:44 +02002247 do {
2248 max = buf->size - b_data(buf);
2249 if (max)
2250 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2251 else
2252 ret = 0;
2253 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002254
Olivier Houchard53216e72018-10-10 15:46:36 +02002255 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002256 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002257
Olivier Houcharda1411e62018-08-17 18:42:48 +02002258 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002259 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002260 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002261 }
2262
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002263 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002264 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002265 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002266}
2267
Willy Tarreau479998a2018-11-18 06:30:59 +01002268/* Try to send data if possible.
2269 * The function returns 1 if data have been sent, otherwise zero.
2270 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002271static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002272{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002273 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002274 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002275 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002276
2277 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002278 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002279
Olivier Houchard7505f942018-08-21 18:10:44 +02002280
Willy Tarreaua2af5122017-10-09 11:56:46 +02002281 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2282 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002283 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284 }
2285
Willy Tarreaubc933932017-10-09 16:21:43 +02002286 /* This loop is quite simple : it tries to fill as much as it can from
2287 * pending streams into the existing buffer until it's reportedly full
2288 * or the end of send requests is reached. Then it tries to send this
2289 * buffer's contents out, marks it not full if at least one byte could
2290 * be sent, and tries again.
2291 *
2292 * The snd_buf() function normally takes a "flags" argument which may
2293 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2294 * data immediately comes and CO_SFL_STREAMER to indicate that the
2295 * connection is streaming lots of data (used to increase TLS record
2296 * size at the expense of latency). The former can be sent any time
2297 * there's a buffer full flag, as it indicates at least one stream
2298 * attempted to send and failed so there are pending data. An
2299 * alternative would be to set it as long as there's an active stream
2300 * but that would be problematic for ACKs until we have an absolute
2301 * guarantee that all waiters have at least one byte to send. The
2302 * latter should possibly not be set for now.
2303 */
2304
2305 done = 0;
2306 while (!done) {
2307 unsigned int flags = 0;
2308
2309 /* fill as much as we can into the current buffer */
2310 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2311 done = h2_process_mux(h2c);
2312
2313 if (conn->flags & CO_FL_ERROR)
2314 break;
2315
2316 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2317 flags |= CO_SFL_MSG_MORE;
2318
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002319 if (b_data(&h2c->mbuf)) {
2320 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002321 if (!ret)
2322 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002323 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002324 b_del(&h2c->mbuf, ret);
2325 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002326 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002327
2328 /* wrote at least one byte, the buffer is not full anymore */
2329 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2330 }
2331
Willy Tarreaua2af5122017-10-09 11:56:46 +02002332 if (conn->flags & CO_FL_SOCK_WR_SH) {
2333 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002334 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002335 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002336 /* We're not full anymore, so we can wake any task that are waiting
2337 * for us.
2338 */
2339 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002340 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002341 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2342 struct h2s *, list);
2343 LIST_DEL(&h2s->list);
2344 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002345 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002346 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002347 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002348 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002349 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002350 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002351 /* We're done, no more to send */
2352 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002353 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002354schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002355 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2356 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002357 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002358}
2359
2360static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2361{
2362 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002363 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002364
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002365 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002366 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002367 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002368 ret |= h2_recv(h2c);
2369 if (ret)
2370 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002371 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002372}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002373
Willy Tarreau62f52692017-10-08 23:01:42 +02002374/* callback called on any event by the connection handler.
2375 * It applies changes and returns zero, or < 0 if it wants immediate
2376 * destruction of the connection (which normally doesn not happen in h2).
2377 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002378static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002379{
Olivier Houchard7505f942018-08-21 18:10:44 +02002380 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002381
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002382 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383 h2_process_demux(h2c);
2384
2385 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002386 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002387
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002388 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002389 h2c->flags &= ~H2_CF_DEM_DFULL;
2390 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002391 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002392
Willy Tarreau0b37d652018-10-03 10:33:02 +02002393 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002394 /* frontend is stopping, reload likely in progress, let's try
2395 * to announce a graceful shutdown if not yet done. We don't
2396 * care if it fails, it will be tried again later.
2397 */
2398 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2399 if (h2c->last_sid < 0)
2400 h2c->last_sid = (1U << 31) - 1;
2401 h2c_send_goaway_error(h2c, NULL);
2402 }
2403 }
2404
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002405 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002406 * If we received early data, and the handshake is done, wake
2407 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002408 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002409 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2410 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2411 struct eb32_node *node;
2412 struct h2s *h2s;
2413
2414 h2c->flags |= H2_CF_WAIT_FOR_HS;
2415 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2416
2417 while (node) {
2418 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002419 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002420 h2s->recv_wait) {
2421 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002422 sw->wait_reason &= ~SUB_CAN_RECV;
2423 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002424 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002425 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002426 node = eb32_next(node);
2427 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002428 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002429
Willy Tarreau26bd7612017-10-09 16:47:04 +02002430 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002431 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2432 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2433 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002434 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002435
2436 if (eb_is_empty(&h2c->streams_by_id)) {
2437 /* no more stream, kill the connection now */
2438 h2_release(conn);
2439 return -1;
2440 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002441 }
2442
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002443 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002444 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002445
Olivier Houchard53216e72018-10-10 15:46:36 +02002446 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2447 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2448 (h2c->st0 != H2_CS_ERROR &&
2449 !b_data(&h2c->mbuf) &&
2450 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2451 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002452 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002453
Willy Tarreau3f133572017-10-31 19:21:06 +01002454 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002455 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002456 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002457 task_queue(h2c->task);
2458 }
2459 else
2460 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002461 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002462
Olivier Houchard7505f942018-08-21 18:10:44 +02002463 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002464 return 0;
2465}
2466
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002467static int h2_wake(struct connection *conn)
2468{
2469 struct h2c *h2c = conn->mux_ctx;
2470
2471 return (h2_process(h2c));
2472}
2473
2474
Willy Tarreauea392822017-10-31 10:02:25 +01002475/* Connection timeout management. The principle is that if there's no receipt
2476 * nor sending for a certain amount of time, the connection is closed. If the
2477 * MUX buffer still has lying data or is not allocatable, the connection is
2478 * immediately killed. If it's allocatable and empty, we attempt to send a
2479 * GOAWAY frame.
2480 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002481static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002482{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002483 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002484 int expired = tick_is_expired(t->expire, now_ms);
2485
Willy Tarreau0975f112018-03-29 15:22:59 +02002486 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002487 return t;
2488
Willy Tarreau0975f112018-03-29 15:22:59 +02002489 task_delete(t);
2490 task_free(t);
2491
2492 if (!h2c) {
2493 /* resources were already deleted */
2494 return NULL;
2495 }
2496
2497 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002498 h2c_error(h2c, H2_ERR_NO_ERROR);
2499 h2_wake_some_streams(h2c, 0, 0);
2500
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002501 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002502 /* don't even try to send a GOAWAY, the buffer is stuck */
2503 h2c->flags |= H2_CF_GOAWAY_FAILED;
2504 }
2505
2506 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002507 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002508 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2509 h2c->flags |= H2_CF_GOAWAY_FAILED;
2510
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002511 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2512 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002513 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 b_del(&h2c->mbuf, ret);
2515 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002516 }
2517 }
Willy Tarreauea392822017-10-31 10:02:25 +01002518
Willy Tarreau0975f112018-03-29 15:22:59 +02002519 /* either we can release everything now or it will be done later once
2520 * the last stream closes.
2521 */
2522 if (eb_is_empty(&h2c->streams_by_id))
2523 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002524
Willy Tarreauea392822017-10-31 10:02:25 +01002525 return NULL;
2526}
2527
2528
Willy Tarreau62f52692017-10-08 23:01:42 +02002529/*******************************************/
2530/* functions below are used by the streams */
2531/*******************************************/
2532
2533/*
2534 * Attach a new stream to a connection
2535 * (Used for outgoing connections)
2536 */
2537static struct conn_stream *h2_attach(struct connection *conn)
2538{
2539 return NULL;
2540}
2541
Willy Tarreaufafd3982018-11-18 21:29:20 +01002542/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2543 * We have to scan because we may have some orphan streams. It might be
2544 * beneficial to scan backwards from the end to reduce the likeliness to find
2545 * orphans.
2546 */
2547static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2548{
2549 struct h2c *h2c = conn->mux_ctx;
2550 struct h2s *h2s;
2551 struct eb32_node *node;
2552
2553 node = eb32_first(&h2c->streams_by_id);
2554 while (node) {
2555 h2s = container_of(node, struct h2s, by_id);
2556 if (h2s->cs)
2557 return h2s->cs;
2558 node = eb32_next(node);
2559 }
2560 return NULL;
2561}
2562
Willy Tarreau62f52692017-10-08 23:01:42 +02002563/*
2564 * Detach the stream from the connection and possibly release the connection.
2565 */
2566static void h2_detach(struct conn_stream *cs)
2567{
Willy Tarreau60935142017-10-16 18:11:19 +02002568 struct h2s *h2s = cs->ctx;
2569 struct h2c *h2c;
2570
2571 cs->ctx = NULL;
2572 if (!h2s)
2573 return;
2574
2575 h2c = h2s->h2c;
2576 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002577 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002578 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2579 !h2_has_too_many_cs(h2c)) {
2580 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002581 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002582 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002583 }
Willy Tarreau60935142017-10-16 18:11:19 +02002584
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002585 /* this stream may be blocked waiting for some data to leave (possibly
2586 * an ES or RST frame), so orphan it in this case.
2587 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002588 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002589 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002590 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002591 return;
2592
Willy Tarreau45f752e2017-10-30 15:44:59 +01002593 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2594 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2595 /* unblock the connection if it was blocked on this
2596 * stream.
2597 */
2598 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2599 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002600 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002601 }
2602
Willy Tarreau71049cc2018-03-28 13:56:39 +02002603 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002604
Willy Tarreaue323f342018-03-28 13:51:45 +02002605 /* We don't want to close right now unless we're removing the
2606 * last stream, and either the connection is in error, or it
2607 * reached the ID already specified in a GOAWAY frame received
2608 * or sent (as seen by last_sid >= 0).
2609 */
2610 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2611 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002612 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002613 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002614 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002615 (conn_xprt_read0_pending(h2c->conn) ||
2616 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2617 /* no more stream will come, kill it now */
2618 h2_release(h2c->conn);
2619 }
2620 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002621 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002622 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2623 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002624 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002625 else
2626 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002627 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002628}
2629
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002630static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002631{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002632 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002633 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002634
Willy Tarreau721c9742017-11-07 11:05:42 +01002635 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002636 return;
2637
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002638 /* if no outgoing data was seen on this stream, it means it was
2639 * closed with a "tcp-request content" rule that is normally
2640 * used to kill the connection ASAP (eg: limit abuse). In this
2641 * case we send a goaway to close the connection.
2642 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002643 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002644 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002645 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002646
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002647 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2648 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002649 h2c_send_goaway_error(h2c, h2s) <= 0)
2650 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002651
Willy Tarreau00dd0782018-03-01 16:31:34 +01002652 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002653
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002654 return;
2655add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002656 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002657 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002658 if (h2s->flags & H2_SF_BLK_MFCTL) {
2659 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2660 h2s->send_wait = sw;
2661 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2662 h2s->send_wait = sw;
2663 LIST_ADDQ(&h2c->send_list, &h2s->list);
2664 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002665 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002666 /* Let the handler know we want shutr */
2667 sw->handle = (void *)((long)sw->handle | 1);
2668
Willy Tarreau62f52692017-10-08 23:01:42 +02002669}
2670
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002672{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002673 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002674 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002675
Willy Tarreau721c9742017-11-07 11:05:42 +01002676 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002677 return;
2678
Willy Tarreau67434202017-11-06 20:20:51 +01002679 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002680 /* we can cleanly close using an empty data frame only after headers */
2681
2682 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2683 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002684 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002685
2686 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002687 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002688 else
2689 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002690 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002691 /* if no outgoing data was seen on this stream, it means it was
2692 * closed with a "tcp-request content" rule that is normally
2693 * used to kill the connection ASAP (eg: limit abuse). In this
2694 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002695 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002696 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002697 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002698 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002699
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002700 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2701 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002702 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002703 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002704
Willy Tarreau00dd0782018-03-01 16:31:34 +01002705 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002706 }
2707
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002708
2709 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002710 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002711 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002712 if (h2s->flags & H2_SF_BLK_MFCTL) {
2713 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2714 h2s->send_wait = sw;
2715 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2716 h2s->send_wait = sw;
2717 LIST_ADDQ(&h2c->send_list, &h2s->list);
2718 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002719 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002720 /* let the handler know we want to shutw */
2721 sw->handle = (void *)((long)(sw->handle) | 2);
2722
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002723}
2724
2725static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2726{
2727 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002728 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002729
2730 if (reason & 1)
2731 h2_do_shutr(h2s);
2732 if (reason & 2)
2733 h2_do_shutw(h2s);
2734
2735 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002736}
2737
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002738static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2739{
2740 struct h2s *h2s = cs->ctx;
2741
2742 if (!mode)
2743 return;
2744
2745 h2_do_shutr(h2s);
2746}
2747
2748static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2749{
2750 struct h2s *h2s = cs->ctx;
2751
2752 h2_do_shutw(h2s);
2753}
2754
Willy Tarreau13278b42017-10-13 19:23:14 +02002755/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2756 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2757 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002758 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002759 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002760static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002761{
2762 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002763 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002764 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002765 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002766 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002767 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002768 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002769 int flen = h2c->dfl;
2770 int outlen = 0;
2771 int wrap;
2772 int try;
2773
2774 if (!h2c->dfl) {
2775 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002776 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 return 0;
2778 }
2779
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002780 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002781 return 0; // incomplete input frame
2782
Willy Tarreau13278b42017-10-13 19:23:14 +02002783 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002784 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002785 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002786 copy = alloc_trash_chunk();
2787 if (!copy) {
2788 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2789 goto fail;
2790 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002791 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2792 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2793 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002794 }
2795
2796 /* The padlen is the first byte before data, and the padding appears
2797 * after data. padlen+data+padding are included in flen.
2798 */
2799 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002800 h2c->dpl = *hdrs;
2801 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002802 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2803 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002804 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002805 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002806 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002807 hdrs += 1; // skip Pad Length
2808 }
2809
2810 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2811 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002812 if (read_n32(hdrs) == h2s->id) {
2813 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2814 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002815 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002816 }
2817
Willy Tarreau13278b42017-10-13 19:23:14 +02002818 hdrs += 5; // stream dep = 4, weight = 1
2819 flen -= 5;
2820 }
2821
2822 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2823 * don't support this for now and can't even decompress so we have to
2824 * break the connection.
2825 */
2826 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2827 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002828 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002829 }
2830
Olivier Houchard638b7992018-08-16 15:41:52 +02002831 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002832 if (!csbuf) {
2833 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002834 goto fail;
2835 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002836
Willy Tarreau937f7602018-02-26 15:22:17 +01002837 /* we can't retry a failed decompression operation so we must be very
2838 * careful not to take any risks. In practice the output buffer is
2839 * always empty except maybe for trailers, in which case we simply have
2840 * to wait for the upper layer to finish consuming what is available.
2841 */
2842 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002843 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002844
Willy Tarreau937f7602018-02-26 15:22:17 +01002845 csbuf->head = 0;
2846 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002847
2848 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2849 sizeof(list)/sizeof(list[0]), tmp);
2850 if (outlen < 0) {
2851 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2852 goto fail;
2853 }
2854
2855 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002856 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002857 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002858
2859 if (outlen < 0) {
2860 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2861 goto fail;
2862 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002863
Willy Tarreau174b06a2018-04-25 18:13:58 +02002864 if (msgf & H2_MSGF_BODY) {
2865 /* a payload is present */
2866 if (msgf & H2_MSGF_BODY_CL)
2867 h2s->flags |= H2_SF_DATA_CLEN;
2868 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2869 h2s->flags |= H2_SF_DATA_CHNK;
2870 }
2871
Willy Tarreau13278b42017-10-13 19:23:14 +02002872 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002873 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002874 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002875 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002876
Willy Tarreau39d68502018-03-02 12:26:37 +01002877 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002878 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002879 h2s->cs->flags |= CS_FL_REOS;
2880 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002881
Willy Tarreau68dd9852017-07-03 14:44:26 +02002882 leave:
2883 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002884 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002885 fail:
2886 outlen = 0;
2887 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002888}
2889
Willy Tarreau454f9052017-10-26 19:40:35 +02002890/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2891 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2892 * in use, a new chunk is emitted for each frame. This is supposed to fit
2893 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2894 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2895 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2896 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002897 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2898 * checked to know if some data remain pending (an empty DATA frame can return
2899 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2900 * connection errors in h2c->errcode. The caller must already have checked the
2901 * frame header and ensured that the frame was complete or the buffer full. It
2902 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002903 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002904static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002905{
2906 struct h2c *h2c = h2s->h2c;
2907 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002908 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002909 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002910 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002911
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002912 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002913
2914 /* The padlen is the first byte before data, and the padding appears
2915 * after data. padlen+data+padding are included in flen.
2916 */
Willy Tarreau79127812017-12-03 21:06:59 +01002917 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002919 return 0;
2920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002921 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002922 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002923 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2924 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002925 return 0;
2926 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002927
2928 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002929 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002930 h2c->dfl--;
2931 h2c->rcvd_c++; h2c->rcvd_s++;
2932 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002933 }
2934
Olivier Houchard638b7992018-08-16 15:41:52 +02002935 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002936 if (!csbuf) {
2937 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002938 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002939 }
2940
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002941 flen = h2c->dfl - h2c->dpl;
2942 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002943 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002944
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002945 if (flen > b_data(&h2c->dbuf)) {
2946 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002947 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002948 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002949 }
2950
2951 if (unlikely(b_space_wraps(csbuf))) {
2952 /* it doesn't fit and the buffer is fragmented,
2953 * so let's defragment it and try again.
2954 */
2955 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002956 }
2957
Willy Tarreaueba10f22018-04-25 20:44:22 +02002958 /* chunked-encoding requires more room */
2959 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002961 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2962 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2963 (chklen < 1048576) ? 4 : 8;
2964 chklen += 4; // CRLF, CRLF
2965 }
2966
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002967 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002968 if (flen + chklen > b_room(csbuf)) {
2969 if (chklen >= b_room(csbuf)) {
2970 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002971 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002972 }
2973 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002974 }
2975
2976 if (h2s->flags & H2_SF_DATA_CHNK) {
2977 /* emit the chunk size */
2978 unsigned int chksz = flen;
2979 char str[10];
2980 char *beg;
2981
2982 beg = str + sizeof(str);
2983 *--beg = '\n';
2984 *--beg = '\r';
2985 do {
2986 *--beg = hextab[chksz & 0xF];
2987 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002988 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989 }
2990
Willy Tarreau454f9052017-10-26 19:40:35 +02002991 /* Block1 is the length of the first block before the buffer wraps,
2992 * block2 is the optional second block to reach the end of the frame.
2993 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002994 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002995 if (block1 > flen)
2996 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002997 block2 = flen - block1;
2998
2999 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003000 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003001
3002 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003003 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003004
Willy Tarreaueba10f22018-04-25 20:44:22 +02003005 if (h2s->flags & H2_SF_DATA_CHNK) {
3006 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003007 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003008 }
3009
Willy Tarreau454f9052017-10-26 19:40:35 +02003010 /* now mark the input data as consumed (will be deleted from the buffer
3011 * by the caller when seeing FRAME_A after sending the window update).
3012 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003014 h2c->dfl -= flen;
3015 h2c->rcvd_c += flen;
3016 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3017
3018 if (h2c->dfl > h2c->dpl) {
3019 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003020 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003021 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003022 }
3023
Willy Tarreau4a28da12018-01-04 14:41:00 +01003024 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003025 /* here we're done with the frame, all the payload (except padding) was
3026 * transferred.
3027 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003028
3029 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3030 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003031 if (b_room(csbuf) < 5) {
3032 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003033 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003034 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003035 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003036 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003037 }
3038
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003039 h2c->rcvd_c += h2c->dpl;
3040 h2c->rcvd_s += h2c->dpl;
3041 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003042 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3043
Willy Tarreau39d68502018-03-02 12:26:37 +01003044 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003045 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003046 h2s->cs->flags |= CS_FL_REOS;
3047 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003048
Willy Tarreau454b57b2018-02-26 15:50:05 +01003049 return flen + chklen;
3050 fail:
3051 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003052}
3053
Willy Tarreau5dd17352018-06-14 13:33:30 +02003054/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3055 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3056 * number of bytes sent. The caller must check the stream's status to detect
3057 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003058 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003059static 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 +02003060{
3061 struct http_hdr list[MAX_HTTP_HDR];
3062 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003063 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003064 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003065 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003066 int es_now = 0;
3067 int ret = 0;
3068 int hdr;
3069
3070 if (h2c_mux_busy(h2c, h2s)) {
3071 h2s->flags |= H2_SF_BLK_MBUSY;
3072 return 0;
3073 }
3074
Willy Tarreau44e973f2018-03-01 17:49:30 +01003075 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003076 h2c->flags |= H2_CF_MUX_MALLOC;
3077 h2s->flags |= H2_SF_BLK_MROOM;
3078 return 0;
3079 }
3080
3081 /* First, try to parse the H1 response and index it into <list>.
3082 * NOTE! Since it comes from haproxy, we *know* that a response header
3083 * block does not wrap and we can safely read it this way without
3084 * having to realign the buffer.
3085 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003086 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003087 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003088 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003089 /* incomplete or invalid response, this is abnormal coming from
3090 * haproxy and may only result in a bad errorfile or bad Lua code
3091 * so that won't be fixed, raise an error now.
3092 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003093 * FIXME: we should instead add the ability to only return a
3094 * 502 bad gateway. But in theory this is not supposed to
3095 * happen.
3096 */
3097 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3098 ret = 0;
3099 goto end;
3100 }
3101
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003102 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003103
3104 /* certain statuses have no body or an empty one, regardless of
3105 * what the headers say.
3106 */
3107 if (sl.st.status >= 100 && sl.st.status < 200) {
3108 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3109 h1m->curr_len = h1m->body_len = 0;
3110 }
3111 else if (sl.st.status == 204 || sl.st.status == 304) {
3112 /* no contents, claim c-len is present and set to zero */
3113 h1m->flags &= ~H1_MF_CHNK;
3114 h1m->flags |= H1_MF_CLEN;
3115 h1m->curr_len = h1m->body_len = 0;
3116 }
3117
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003118 chunk_reset(&outbuf);
3119
3120 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003121 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003122 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003123 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003124
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003125 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003126 break;
3127 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003128 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003129 }
3130
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003131 if (outbuf.size < 9)
3132 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003133
3134 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003135 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3136 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3137 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138
3139 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003140 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003141 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003142 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003144 else if (unlikely(list[0].v.len != 3)) {
3145 /* this is an unparsable response */
3146 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3147 ret = 0;
3148 goto end;
3149 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003150 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003151 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003152 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3153 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3154 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3155 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3156 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003157 }
3158 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003159 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003160 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003161 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003162 }
3163
3164 /* encode all headers, stop at empty name */
3165 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003166 /* these ones do not exist in H2 and must be dropped. */
3167 if (isteq(list[hdr].n, ist("connection")) ||
3168 isteq(list[hdr].n, ist("proxy-connection")) ||
3169 isteq(list[hdr].n, ist("keep-alive")) ||
3170 isteq(list[hdr].n, ist("upgrade")) ||
3171 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003172 continue;
3173
3174 if (isteq(list[hdr].n, ist("")))
3175 break; // end
3176
3177 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3178 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003179 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003180 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003181 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003182 }
3183 }
3184
3185 /* we may need to add END_STREAM */
3186 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3187 es_now = 1;
3188
3189 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003190 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003191
3192 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003194
3195 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003196 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003197
3198 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003199 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003200 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003201
3202 /* for now we don't implemented CONTINUATION, so we wait for a
3203 * body or directly end in TRL2.
3204 */
3205 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003206 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003207 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003208
Willy Tarreau801250e2018-09-11 11:45:04 +02003209 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003210 h2s->flags |= H2_SF_ES_SENT;
3211 if (h2s->st == H2_SS_OPEN)
3212 h2s->st = H2_SS_HLOC;
3213 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003214 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003215 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003216 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003217 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003218 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003219 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003220 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003221 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003222 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003223
3224 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003225
3226 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003227 //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 +02003228 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003229 full:
3230 h1m_init_res(h1m);
3231 h1m->err_pos = -1; // don't care about errors on the response path
3232 h2c->flags |= H2_CF_MUX_MFULL;
3233 h2s->flags |= H2_SF_BLK_MROOM;
3234 ret = 0;
3235 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003236}
3237
Willy Tarreau5dd17352018-06-14 13:33:30 +02003238/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3239 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3240 * the number of bytes sent. The caller must check the stream's status to
3241 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003242 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003243static 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 +02003244{
3245 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003246 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003247 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003248 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003249 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003250 int es_now = 0;
3251 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003252 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003253 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003254
3255 if (h2c_mux_busy(h2c, h2s)) {
3256 h2s->flags |= H2_SF_BLK_MBUSY;
3257 goto end;
3258 }
3259
Willy Tarreau44e973f2018-03-01 17:49:30 +01003260 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003261 h2c->flags |= H2_CF_MUX_MALLOC;
3262 h2s->flags |= H2_SF_BLK_MROOM;
3263 goto end;
3264 }
3265
3266 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003267 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268 goto end;
3269
3270 chunk_reset(&outbuf);
3271
3272 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003274 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003275 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003276
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003277 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003278 break;
3279 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003281 }
3282
3283 if (outbuf.size < 9) {
3284 h2c->flags |= H2_CF_MUX_MFULL;
3285 h2s->flags |= H2_SF_BLK_MROOM;
3286 goto end;
3287 }
3288
3289 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003290 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3291 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3292 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293
3294 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3295 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003296 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003297 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003298 break;
3299 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003300 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003301 if ((long long)size > h1m->curr_len)
3302 size = h1m->curr_len;
3303 break;
3304 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003305 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003306 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003307 if (!ret)
3308 goto end;
3309
3310 if (ret < 0) {
3311 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003312 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003313 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3314 goto end;
3315 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003316 max -= ret;
3317 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003318 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003319 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 }
3321
Willy Tarreau801250e2018-09-11 11:45:04 +02003322 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003324 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325 if (!ret)
3326 goto end;
3327
3328 if (ret < 0) {
3329 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003330 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003331 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3332 goto end;
3333 }
3334
3335 size = chunk;
3336 h1m->curr_len = chunk;
3337 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003338 max -= ret;
3339 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003340 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003341 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 if (!size)
3343 goto send_empty;
3344 }
3345
3346 /* in MSG_DATA state, continue below */
3347 size = h1m->curr_len;
3348 break;
3349 }
3350
3351 /* we have in <size> the exact number of bytes we need to copy from
3352 * the H1 buffer. We need to check this against the connection's and
3353 * the stream's send windows, and to ensure that this fits in the max
3354 * frame size and in the buffer's available space minus 9 bytes (for
3355 * the frame header). The connection's flow control is applied last so
3356 * that we can use a separate list of streams which are immediately
3357 * unblocked on window opening. Note: we don't implement padding.
3358 */
3359
Willy Tarreau5dd17352018-06-14 13:33:30 +02003360 if (size > max)
3361 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003362
3363 if (size > h2s->mws)
3364 size = h2s->mws;
3365
3366 if (size <= 0) {
3367 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003368 if (h2s->send_wait) {
3369 LIST_DEL(&h2s->list);
3370 LIST_INIT(&h2s->list);
3371 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003372 goto end;
3373 }
3374
3375 if (h2c->mfs && size > h2c->mfs)
3376 size = h2c->mfs;
3377
3378 if (size + 9 > outbuf.size) {
3379 /* we have an opportunity for enlarging the too small
3380 * available space, let's try.
3381 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003382 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003383 goto realign_again;
3384 size = outbuf.size - 9;
3385 }
3386
3387 if (size <= 0) {
3388 h2c->flags |= H2_CF_MUX_MFULL;
3389 h2s->flags |= H2_SF_BLK_MROOM;
3390 goto end;
3391 }
3392
3393 if (size > h2c->mws)
3394 size = h2c->mws;
3395
3396 if (size <= 0) {
3397 h2s->flags |= H2_SF_BLK_MFCTL;
3398 goto end;
3399 }
3400
3401 /* copy whatever we can */
3402 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003403 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003404 if (ret == 1)
3405 len2 = 0;
3406
3407 if (!ret || len1 + len2 < size) {
3408 /* FIXME: must normally never happen */
3409 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3410 goto end;
3411 }
3412
3413 /* limit len1/len2 to size */
3414 if (len1 + len2 > size) {
3415 int sub = len1 + len2 - size;
3416
3417 if (len2 > sub)
3418 len2 -= sub;
3419 else {
3420 sub -= len2;
3421 len2 = 0;
3422 len1 -= sub;
3423 }
3424 }
3425
3426 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003427 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003428 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003429 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003430
3431 send_empty:
3432 /* we may need to add END_STREAM */
3433 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3434 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003435 *
3436 * FIXME: what we do here is not correct because we send end_stream
3437 * before knowing if we'll have to send a HEADERS frame for the
3438 * trailers. More importantly we're not consuming the trailing CRLF
3439 * after the end of trailers, so it will be left to the caller to
3440 * eat it. The right way to do it would be to measure trailers here
3441 * and to send ES only if there are no trailers.
3442 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003443 */
3444 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003445 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003446 es_now = 1;
3447
3448 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003449 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003450
3451 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003452 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003453
3454 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003455 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003456
3457 /* consume incoming H1 response */
3458 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003459 max -= size;
3460 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003461 total += size;
3462 h1m->curr_len -= size;
3463 h2s->mws -= size;
3464 h2c->mws -= size;
3465
3466 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003467 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468 goto new_frame;
3469 }
3470 }
3471
3472 if (es_now) {
3473 if (h2s->st == H2_SS_OPEN)
3474 h2s->st = H2_SS_HLOC;
3475 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003476 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003477
Willy Tarreau35a62702018-02-27 15:37:25 +01003478 if (!(h1m->flags & H1_MF_CHNK)) {
3479 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003480 total += max;
3481 ofs += max;
3482 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003483
Willy Tarreau801250e2018-09-11 11:45:04 +02003484 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003485 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003486
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003487 h2s->flags |= H2_SF_ES_SENT;
3488 }
3489
3490 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003491 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 +02003492 return total;
3493}
3494
Olivier Houchard6ff20392018-07-17 18:46:31 +02003495/* Called from the upper layer, to subscribe to events, such as being able to send */
3496static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3497{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003498 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003499 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003500 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003501
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003502 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003503 sw = param;
3504 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3505 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003506 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003507 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003508 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003509 event_type &= ~SUB_CAN_RECV;
3510 }
3511 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003512 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003513 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003514 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003515 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003516 h2s->send_wait = sw;
3517 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3518 if (h2s->flags & H2_SF_BLK_MFCTL)
3519 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3520 else
3521 LIST_ADDQ(&h2c->send_list, &h2s->list);
3522 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003523 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003524 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003525 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003526 if (event_type != 0)
3527 return -1;
3528 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003529
3530
3531}
3532
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003533static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3534{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003535 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003536 struct h2s *h2s = cs->ctx;
3537
3538 if (event_type & SUB_CAN_RECV) {
3539 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003540 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003541 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003542 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003543 }
3544 }
3545 if (event_type & SUB_CAN_SEND) {
3546 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003547 if (h2s->send_wait == sw) {
3548 LIST_DEL(&h2s->list);
3549 LIST_INIT(&h2s->list);
3550 sw->wait_reason &= ~SUB_CAN_SEND;
3551 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003552 }
3553 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003554 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3555 sw = param;
3556 if (h2s->send_wait == sw) {
3557 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3558 h2s->send_wait = NULL;
3559 }
3560 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003561 return 0;
3562}
3563
3564
Olivier Houchard511efea2018-08-16 15:30:32 +02003565/* Called from the upper layer, to receive data */
3566static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3567{
Olivier Houchard638b7992018-08-16 15:41:52 +02003568 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003569 size_t ret = 0;
3570
3571 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003572 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003573
Olivier Houchard638b7992018-08-16 15:41:52 +02003574 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003575 cs->flags |= CS_FL_RCV_MORE;
3576 else {
3577 cs->flags &= ~CS_FL_RCV_MORE;
3578 if (cs->flags & CS_FL_REOS)
3579 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003580 if (b_size(&h2s->rxbuf)) {
3581 b_free(&h2s->rxbuf);
3582 offer_buffers(NULL, tasks_run_queue);
3583 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003584 }
3585
3586 return ret;
3587}
3588
Olivier Houchardd846c262018-10-19 17:24:29 +02003589static void h2_stop_senders(struct h2c *h2c)
3590{
3591 struct h2s *h2s, *h2s_back;
3592
3593 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3594 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3595 if (h2c->msi == h2s_id(h2s))
3596 continue;
3597 LIST_DEL(&h2s->list);
3598 LIST_INIT(&h2s->list);
3599 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3600 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3601 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3602 LIST_ADD(&h2c->send_list, &h2s->list);
3603 }
3604}
3605
Willy Tarreau62f52692017-10-08 23:01:42 +02003606/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003607static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003608{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003609 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003610 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003611 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003612
Olivier Houchardd846c262018-10-19 17:24:29 +02003613 if (h2s->send_wait) {
3614 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3615 h2s->send_wait = NULL;
3616 LIST_DEL(&h2s->list);
3617 LIST_INIT(&h2s->list);
3618 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003619 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3620 return 0;
3621
Willy Tarreau0bad0432018-06-14 16:54:01 +02003622 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003623 h2s->flags |= H2_SF_OUTGOING_DATA;
3624
Willy Tarreaua40704a2018-09-11 13:52:04 +02003625 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003626 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003627 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003628 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003629 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003630 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003631 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003632 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003633 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003634 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003635
Willy Tarreau5dd17352018-06-14 13:33:30 +02003636 if (unlikely((int)ret <= 0)) {
3637 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003638 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3639 break;
3640 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003641 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003642 total += count;
3643 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003644 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003645 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003646 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003647 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003648 cs->flags |= CS_FL_ERROR;
3649 break;
3650 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003651
3652 total += ret;
3653 count -= ret;
3654
3655 if (h2s->st >= H2_SS_ERROR)
3656 break;
3657
3658 if (h2s->flags & H2_SF_BLK_ANY)
3659 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003660 }
3661
Willy Tarreau00610962018-07-19 10:58:28 +02003662 if (h2s->st >= H2_SS_ERROR) {
3663 /* trim any possibly pending data after we close (extra CR-LF,
3664 * unprocessed trailers, abnormal extra data, ...)
3665 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003666 total += count;
3667 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003668 }
3669
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003670 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003671 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003672 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003673 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003674 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003675 }
3676
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003677 b_del(buf, total);
Olivier Houchardd846c262018-10-19 17:24:29 +02003678
3679 /* The mux is full, cancel the pending tasks */
3680 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3681 (h2s->flags & H2_SF_BLK_MBUSY))
3682 h2_stop_senders(h2s->h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02003683 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003684 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3685 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003686
Olivier Houchard7505f942018-08-21 18:10:44 +02003687 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003688 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003689}
3690
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003691/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003692static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003693{
3694 struct h2c *h2c = conn->mux_ctx;
3695 struct h2s *h2s;
3696 struct eb32_node *node;
3697 int fctl_cnt = 0;
3698 int send_cnt = 0;
3699 int tree_cnt = 0;
3700 int orph_cnt = 0;
3701
3702 if (!h2c)
3703 return;
3704
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003705 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003706 fctl_cnt++;
3707
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003708 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003709 send_cnt++;
3710
3711 node = eb32_first(&h2c->streams_by_id);
3712 while (node) {
3713 h2s = container_of(node, struct h2s, by_id);
3714 tree_cnt++;
3715 if (!h2s->cs)
3716 orph_cnt++;
3717 node = eb32_next(node);
3718 }
3719
Willy Tarreau616ac812018-07-24 14:12:42 +02003720 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3721 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3722 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3723 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3724 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3725 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003726}
Willy Tarreau62f52692017-10-08 23:01:42 +02003727
3728/*******************************************************/
3729/* functions below are dedicated to the config parsers */
3730/*******************************************************/
3731
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003732/* config parser for global "tune.h2.header-table-size" */
3733static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3734 struct proxy *defpx, const char *file, int line,
3735 char **err)
3736{
3737 if (too_many_args(1, args, err, NULL))
3738 return -1;
3739
3740 h2_settings_header_table_size = atoi(args[1]);
3741 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3742 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3743 return -1;
3744 }
3745 return 0;
3746}
Willy Tarreau62f52692017-10-08 23:01:42 +02003747
Willy Tarreaue6baec02017-07-27 11:45:11 +02003748/* config parser for global "tune.h2.initial-window-size" */
3749static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3750 struct proxy *defpx, const char *file, int line,
3751 char **err)
3752{
3753 if (too_many_args(1, args, err, NULL))
3754 return -1;
3755
3756 h2_settings_initial_window_size = atoi(args[1]);
3757 if (h2_settings_initial_window_size < 0) {
3758 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3759 return -1;
3760 }
3761 return 0;
3762}
3763
Willy Tarreau5242ef82017-07-27 11:47:28 +02003764/* config parser for global "tune.h2.max-concurrent-streams" */
3765static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3766 struct proxy *defpx, const char *file, int line,
3767 char **err)
3768{
3769 if (too_many_args(1, args, err, NULL))
3770 return -1;
3771
3772 h2_settings_max_concurrent_streams = atoi(args[1]);
3773 if (h2_settings_max_concurrent_streams < 0) {
3774 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3775 return -1;
3776 }
3777 return 0;
3778}
3779
Willy Tarreau62f52692017-10-08 23:01:42 +02003780
3781/****************************************/
3782/* MUX initialization and instanciation */
3783/***************************************/
3784
3785/* The mux operations */
3786const struct mux_ops h2_ops = {
3787 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003788 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003789 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003790 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003791 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003792 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003793 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01003794 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02003795 .detach = h2_detach,
3796 .shutr = h2_shutr,
3797 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003798 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003799 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003800 .name = "H2",
3801};
3802
Christopher Faulet32f61c02018-04-10 14:33:41 +02003803/* PROTO selection : this mux registers PROTO token "h2" */
3804static struct mux_proto_list mux_proto_h2 =
3805 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003806
3807/* config keyword parsers */
3808static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003809 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003810 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003811 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003812 { 0, NULL, NULL }
3813}};
3814
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003815static void __h2_deinit(void)
3816{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003817 pool_destroy(pool_head_h2s);
3818 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003819}
3820
Willy Tarreau62f52692017-10-08 23:01:42 +02003821__attribute__((constructor))
3822static void __h2_init(void)
3823{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003824 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003825 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003826 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003827 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3828 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003829}