blob: a5ea6c3ad9558d8d9aa5fd3705abcdd48613fed4 [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
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002225/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002226static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002227{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002228 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002229 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002230 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002231 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002232
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002233 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002234 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002235
Willy Tarreau315d8072017-12-10 22:17:57 +01002236 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002237 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002238
Willy Tarreau44e973f2018-03-01 17:49:30 +01002239 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002240 if (!buf) {
2241 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002242 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002243 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002244
Olivier Houchard7505f942018-08-21 18:10:44 +02002245 do {
2246 max = buf->size - b_data(buf);
2247 if (max)
2248 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2249 else
2250 ret = 0;
2251 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002252
Olivier Houchard53216e72018-10-10 15:46:36 +02002253 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002254 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002255
Olivier Houcharda1411e62018-08-17 18:42:48 +02002256 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002257 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002258 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002259 }
2260
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002261 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002262 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002263 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002264}
2265
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002266/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002267static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002268{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002269 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002270 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002271 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002272
2273 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002274 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002275
Olivier Houchard7505f942018-08-21 18:10:44 +02002276
Willy Tarreaua2af5122017-10-09 11:56:46 +02002277 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2278 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002279 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002280 }
2281
Willy Tarreaubc933932017-10-09 16:21:43 +02002282 /* This loop is quite simple : it tries to fill as much as it can from
2283 * pending streams into the existing buffer until it's reportedly full
2284 * or the end of send requests is reached. Then it tries to send this
2285 * buffer's contents out, marks it not full if at least one byte could
2286 * be sent, and tries again.
2287 *
2288 * The snd_buf() function normally takes a "flags" argument which may
2289 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2290 * data immediately comes and CO_SFL_STREAMER to indicate that the
2291 * connection is streaming lots of data (used to increase TLS record
2292 * size at the expense of latency). The former can be sent any time
2293 * there's a buffer full flag, as it indicates at least one stream
2294 * attempted to send and failed so there are pending data. An
2295 * alternative would be to set it as long as there's an active stream
2296 * but that would be problematic for ACKs until we have an absolute
2297 * guarantee that all waiters have at least one byte to send. The
2298 * latter should possibly not be set for now.
2299 */
2300
2301 done = 0;
2302 while (!done) {
2303 unsigned int flags = 0;
2304
2305 /* fill as much as we can into the current buffer */
2306 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2307 done = h2_process_mux(h2c);
2308
2309 if (conn->flags & CO_FL_ERROR)
2310 break;
2311
2312 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2313 flags |= CO_SFL_MSG_MORE;
2314
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 if (b_data(&h2c->mbuf)) {
2316 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002317 if (!ret)
2318 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002319 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 b_del(&h2c->mbuf, ret);
2321 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002322 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002323
2324 /* wrote at least one byte, the buffer is not full anymore */
2325 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2326 }
2327
Willy Tarreaua2af5122017-10-09 11:56:46 +02002328 if (conn->flags & CO_FL_SOCK_WR_SH) {
2329 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002331 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002332 /* We're not full anymore, so we can wake any task that are waiting
2333 * for us.
2334 */
2335 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002336 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002337 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2338 struct h2s *, list);
2339 LIST_DEL(&h2s->list);
2340 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002341 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002342 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002343 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002344 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002345 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002346 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002347 /* We're done, no more to send */
2348 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002349 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002350schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002351 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2352 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002353 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002354}
2355
2356static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2357{
2358 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002359 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002360
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002361 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002362 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002363 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002364 ret |= h2_recv(h2c);
2365 if (ret)
2366 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002367 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002368}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002369
Willy Tarreau62f52692017-10-08 23:01:42 +02002370/* callback called on any event by the connection handler.
2371 * It applies changes and returns zero, or < 0 if it wants immediate
2372 * destruction of the connection (which normally doesn not happen in h2).
2373 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002374static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002375{
Olivier Houchard7505f942018-08-21 18:10:44 +02002376 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002377
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002378 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002379 h2_process_demux(h2c);
2380
2381 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002382 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002384 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002385 h2c->flags &= ~H2_CF_DEM_DFULL;
2386 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002387 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002388
Willy Tarreau0b37d652018-10-03 10:33:02 +02002389 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002390 /* frontend is stopping, reload likely in progress, let's try
2391 * to announce a graceful shutdown if not yet done. We don't
2392 * care if it fails, it will be tried again later.
2393 */
2394 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2395 if (h2c->last_sid < 0)
2396 h2c->last_sid = (1U << 31) - 1;
2397 h2c_send_goaway_error(h2c, NULL);
2398 }
2399 }
2400
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002401 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002402 * If we received early data, and the handshake is done, wake
2403 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002404 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002405 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2406 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2407 struct eb32_node *node;
2408 struct h2s *h2s;
2409
2410 h2c->flags |= H2_CF_WAIT_FOR_HS;
2411 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2412
2413 while (node) {
2414 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002415 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002416 h2s->recv_wait) {
2417 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002418 sw->wait_reason &= ~SUB_CAN_RECV;
2419 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002420 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002421 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002422 node = eb32_next(node);
2423 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002424 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002425
Willy Tarreau26bd7612017-10-09 16:47:04 +02002426 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002427 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2428 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2429 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002430 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002431
2432 if (eb_is_empty(&h2c->streams_by_id)) {
2433 /* no more stream, kill the connection now */
2434 h2_release(conn);
2435 return -1;
2436 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002437 }
2438
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002439 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002440 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002441
Olivier Houchard53216e72018-10-10 15:46:36 +02002442 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2443 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2444 (h2c->st0 != H2_CS_ERROR &&
2445 !b_data(&h2c->mbuf) &&
2446 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2447 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002448 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002449
Willy Tarreau3f133572017-10-31 19:21:06 +01002450 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002451 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002452 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002453 task_queue(h2c->task);
2454 }
2455 else
2456 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002457 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002458
Olivier Houchard7505f942018-08-21 18:10:44 +02002459 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002460 return 0;
2461}
2462
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002463static int h2_wake(struct connection *conn)
2464{
2465 struct h2c *h2c = conn->mux_ctx;
2466
2467 return (h2_process(h2c));
2468}
2469
2470
Willy Tarreauea392822017-10-31 10:02:25 +01002471/* Connection timeout management. The principle is that if there's no receipt
2472 * nor sending for a certain amount of time, the connection is closed. If the
2473 * MUX buffer still has lying data or is not allocatable, the connection is
2474 * immediately killed. If it's allocatable and empty, we attempt to send a
2475 * GOAWAY frame.
2476 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002477static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002478{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002479 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002480 int expired = tick_is_expired(t->expire, now_ms);
2481
Willy Tarreau0975f112018-03-29 15:22:59 +02002482 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002483 return t;
2484
Willy Tarreau0975f112018-03-29 15:22:59 +02002485 task_delete(t);
2486 task_free(t);
2487
2488 if (!h2c) {
2489 /* resources were already deleted */
2490 return NULL;
2491 }
2492
2493 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002494 h2c_error(h2c, H2_ERR_NO_ERROR);
2495 h2_wake_some_streams(h2c, 0, 0);
2496
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002497 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002498 /* don't even try to send a GOAWAY, the buffer is stuck */
2499 h2c->flags |= H2_CF_GOAWAY_FAILED;
2500 }
2501
2502 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002503 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002504 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2505 h2c->flags |= H2_CF_GOAWAY_FAILED;
2506
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002507 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2508 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002509 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002510 b_del(&h2c->mbuf, ret);
2511 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002512 }
2513 }
Willy Tarreauea392822017-10-31 10:02:25 +01002514
Willy Tarreau0975f112018-03-29 15:22:59 +02002515 /* either we can release everything now or it will be done later once
2516 * the last stream closes.
2517 */
2518 if (eb_is_empty(&h2c->streams_by_id))
2519 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002520
Willy Tarreauea392822017-10-31 10:02:25 +01002521 return NULL;
2522}
2523
2524
Willy Tarreau62f52692017-10-08 23:01:42 +02002525/*******************************************/
2526/* functions below are used by the streams */
2527/*******************************************/
2528
2529/*
2530 * Attach a new stream to a connection
2531 * (Used for outgoing connections)
2532 */
2533static struct conn_stream *h2_attach(struct connection *conn)
2534{
2535 return NULL;
2536}
2537
Willy Tarreau62f52692017-10-08 23:01:42 +02002538/*
2539 * Detach the stream from the connection and possibly release the connection.
2540 */
2541static void h2_detach(struct conn_stream *cs)
2542{
Willy Tarreau60935142017-10-16 18:11:19 +02002543 struct h2s *h2s = cs->ctx;
2544 struct h2c *h2c;
2545
2546 cs->ctx = NULL;
2547 if (!h2s)
2548 return;
2549
2550 h2c = h2s->h2c;
2551 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002552 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002553 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2554 !h2_has_too_many_cs(h2c)) {
2555 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002556 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002557 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002558 }
Willy Tarreau60935142017-10-16 18:11:19 +02002559
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002560 /* this stream may be blocked waiting for some data to leave (possibly
2561 * an ES or RST frame), so orphan it in this case.
2562 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002563 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002564 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002565 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002566 return;
2567
Willy Tarreau45f752e2017-10-30 15:44:59 +01002568 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2569 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2570 /* unblock the connection if it was blocked on this
2571 * stream.
2572 */
2573 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2574 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002575 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002576 }
2577
Willy Tarreau71049cc2018-03-28 13:56:39 +02002578 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002579
Willy Tarreaue323f342018-03-28 13:51:45 +02002580 /* We don't want to close right now unless we're removing the
2581 * last stream, and either the connection is in error, or it
2582 * reached the ID already specified in a GOAWAY frame received
2583 * or sent (as seen by last_sid >= 0).
2584 */
2585 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2586 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002587 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002588 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002589 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002590 (conn_xprt_read0_pending(h2c->conn) ||
2591 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2592 /* no more stream will come, kill it now */
2593 h2_release(h2c->conn);
2594 }
2595 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002596 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002597 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2598 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002599 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002600 else
2601 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002602 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002603}
2604
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002605static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002606{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002607 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002608 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002609
Willy Tarreau721c9742017-11-07 11:05:42 +01002610 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002611 return;
2612
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002613 /* if no outgoing data was seen on this stream, it means it was
2614 * closed with a "tcp-request content" rule that is normally
2615 * used to kill the connection ASAP (eg: limit abuse). In this
2616 * case we send a goaway to close the connection.
2617 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002618 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002619 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002620 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002621
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002622 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2623 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002624 h2c_send_goaway_error(h2c, h2s) <= 0)
2625 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002626
Willy Tarreau00dd0782018-03-01 16:31:34 +01002627 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002628
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002629 return;
2630add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002631 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002632 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002633 if (h2s->flags & H2_SF_BLK_MFCTL) {
2634 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2635 h2s->send_wait = sw;
2636 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2637 h2s->send_wait = sw;
2638 LIST_ADDQ(&h2c->send_list, &h2s->list);
2639 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002640 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002641 /* Let the handler know we want shutr */
2642 sw->handle = (void *)((long)sw->handle | 1);
2643
Willy Tarreau62f52692017-10-08 23:01:42 +02002644}
2645
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002646static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002647{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002648 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002649 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002650
Willy Tarreau721c9742017-11-07 11:05:42 +01002651 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002652 return;
2653
Willy Tarreau67434202017-11-06 20:20:51 +01002654 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002655 /* we can cleanly close using an empty data frame only after headers */
2656
2657 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2658 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002659 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002660
2661 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002662 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002663 else
2664 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002665 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002666 /* if no outgoing data was seen on this stream, it means it was
2667 * closed with a "tcp-request content" rule that is normally
2668 * used to kill the connection ASAP (eg: limit abuse). In this
2669 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002670 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002671 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002672 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002673 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002674
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002675 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2676 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002677 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002678 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002679
Willy Tarreau00dd0782018-03-01 16:31:34 +01002680 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002681 }
2682
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002683
2684 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002685 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002686 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002687 if (h2s->flags & H2_SF_BLK_MFCTL) {
2688 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2689 h2s->send_wait = sw;
2690 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2691 h2s->send_wait = sw;
2692 LIST_ADDQ(&h2c->send_list, &h2s->list);
2693 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002694 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002695 /* let the handler know we want to shutw */
2696 sw->handle = (void *)((long)(sw->handle) | 2);
2697
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002698}
2699
2700static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2701{
2702 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002703 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002704
2705 if (reason & 1)
2706 h2_do_shutr(h2s);
2707 if (reason & 2)
2708 h2_do_shutw(h2s);
2709
2710 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002711}
2712
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002713static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2714{
2715 struct h2s *h2s = cs->ctx;
2716
2717 if (!mode)
2718 return;
2719
2720 h2_do_shutr(h2s);
2721}
2722
2723static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2724{
2725 struct h2s *h2s = cs->ctx;
2726
2727 h2_do_shutw(h2s);
2728}
2729
Willy Tarreau13278b42017-10-13 19:23:14 +02002730/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2731 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2732 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002733 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002734 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002735static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002736{
2737 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002738 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002739 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002740 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002741 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002742 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002743 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002744 int flen = h2c->dfl;
2745 int outlen = 0;
2746 int wrap;
2747 int try;
2748
2749 if (!h2c->dfl) {
2750 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002751 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002752 return 0;
2753 }
2754
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002755 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002756 return 0; // incomplete input frame
2757
Willy Tarreau13278b42017-10-13 19:23:14 +02002758 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002759 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002760 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002761 copy = alloc_trash_chunk();
2762 if (!copy) {
2763 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2764 goto fail;
2765 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2767 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2768 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002769 }
2770
2771 /* The padlen is the first byte before data, and the padding appears
2772 * after data. padlen+data+padding are included in flen.
2773 */
2774 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002775 h2c->dpl = *hdrs;
2776 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2778 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002779 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002780 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002781 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002782 hdrs += 1; // skip Pad Length
2783 }
2784
2785 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2786 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002787 if (read_n32(hdrs) == h2s->id) {
2788 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2789 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002790 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002791 }
2792
Willy Tarreau13278b42017-10-13 19:23:14 +02002793 hdrs += 5; // stream dep = 4, weight = 1
2794 flen -= 5;
2795 }
2796
2797 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2798 * don't support this for now and can't even decompress so we have to
2799 * break the connection.
2800 */
2801 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2802 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002803 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002804 }
2805
Olivier Houchard638b7992018-08-16 15:41:52 +02002806 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002807 if (!csbuf) {
2808 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002809 goto fail;
2810 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002811
Willy Tarreau937f7602018-02-26 15:22:17 +01002812 /* we can't retry a failed decompression operation so we must be very
2813 * careful not to take any risks. In practice the output buffer is
2814 * always empty except maybe for trailers, in which case we simply have
2815 * to wait for the upper layer to finish consuming what is available.
2816 */
2817 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002818 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002819
Willy Tarreau937f7602018-02-26 15:22:17 +01002820 csbuf->head = 0;
2821 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002822
2823 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2824 sizeof(list)/sizeof(list[0]), tmp);
2825 if (outlen < 0) {
2826 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2827 goto fail;
2828 }
2829
2830 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002831 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002832 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002833
2834 if (outlen < 0) {
2835 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2836 goto fail;
2837 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002838
Willy Tarreau174b06a2018-04-25 18:13:58 +02002839 if (msgf & H2_MSGF_BODY) {
2840 /* a payload is present */
2841 if (msgf & H2_MSGF_BODY_CL)
2842 h2s->flags |= H2_SF_DATA_CLEN;
2843 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2844 h2s->flags |= H2_SF_DATA_CHNK;
2845 }
2846
Willy Tarreau13278b42017-10-13 19:23:14 +02002847 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002849 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002850 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002851
Willy Tarreau39d68502018-03-02 12:26:37 +01002852 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002853 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002854 h2s->cs->flags |= CS_FL_REOS;
2855 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002856
Willy Tarreau68dd9852017-07-03 14:44:26 +02002857 leave:
2858 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002859 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002860 fail:
2861 outlen = 0;
2862 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002863}
2864
Willy Tarreau454f9052017-10-26 19:40:35 +02002865/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2866 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2867 * in use, a new chunk is emitted for each frame. This is supposed to fit
2868 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2869 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2870 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2871 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002872 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2873 * checked to know if some data remain pending (an empty DATA frame can return
2874 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2875 * connection errors in h2c->errcode. The caller must already have checked the
2876 * frame header and ensured that the frame was complete or the buffer full. It
2877 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002878 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002879static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002880{
2881 struct h2c *h2c = h2s->h2c;
2882 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002883 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002884 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002885 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002886
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002887 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002888
2889 /* The padlen is the first byte before data, and the padding appears
2890 * after data. padlen+data+padding are included in flen.
2891 */
Willy Tarreau79127812017-12-03 21:06:59 +01002892 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002894 return 0;
2895
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002896 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002897 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002898 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2899 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002900 return 0;
2901 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002902
2903 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002904 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002905 h2c->dfl--;
2906 h2c->rcvd_c++; h2c->rcvd_s++;
2907 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002908 }
2909
Olivier Houchard638b7992018-08-16 15:41:52 +02002910 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002911 if (!csbuf) {
2912 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002913 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002914 }
2915
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002916 flen = h2c->dfl - h2c->dpl;
2917 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002918 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002920 if (flen > b_data(&h2c->dbuf)) {
2921 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002922 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002923 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002924 }
2925
2926 if (unlikely(b_space_wraps(csbuf))) {
2927 /* it doesn't fit and the buffer is fragmented,
2928 * so let's defragment it and try again.
2929 */
2930 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002931 }
2932
Willy Tarreaueba10f22018-04-25 20:44:22 +02002933 /* chunked-encoding requires more room */
2934 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002935 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002936 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2937 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2938 (chklen < 1048576) ? 4 : 8;
2939 chklen += 4; // CRLF, CRLF
2940 }
2941
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002942 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002943 if (flen + chklen > b_room(csbuf)) {
2944 if (chklen >= b_room(csbuf)) {
2945 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002946 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002947 }
2948 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002949 }
2950
2951 if (h2s->flags & H2_SF_DATA_CHNK) {
2952 /* emit the chunk size */
2953 unsigned int chksz = flen;
2954 char str[10];
2955 char *beg;
2956
2957 beg = str + sizeof(str);
2958 *--beg = '\n';
2959 *--beg = '\r';
2960 do {
2961 *--beg = hextab[chksz & 0xF];
2962 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002963 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002964 }
2965
Willy Tarreau454f9052017-10-26 19:40:35 +02002966 /* Block1 is the length of the first block before the buffer wraps,
2967 * block2 is the optional second block to reach the end of the frame.
2968 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002970 if (block1 > flen)
2971 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002972 block2 = flen - block1;
2973
2974 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002975 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002976
2977 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002978 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002979
Willy Tarreaueba10f22018-04-25 20:44:22 +02002980 if (h2s->flags & H2_SF_DATA_CHNK) {
2981 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002982 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002983 }
2984
Willy Tarreau454f9052017-10-26 19:40:35 +02002985 /* now mark the input data as consumed (will be deleted from the buffer
2986 * by the caller when seeing FRAME_A after sending the window update).
2987 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989 h2c->dfl -= flen;
2990 h2c->rcvd_c += flen;
2991 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2992
2993 if (h2c->dfl > h2c->dpl) {
2994 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002995 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002996 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002997 }
2998
Willy Tarreau4a28da12018-01-04 14:41:00 +01002999 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003000 /* here we're done with the frame, all the payload (except padding) was
3001 * transferred.
3002 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003003
3004 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3005 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003006 if (b_room(csbuf) < 5) {
3007 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003008 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003009 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003010 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003011 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003012 }
3013
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003014 h2c->rcvd_c += h2c->dpl;
3015 h2c->rcvd_s += h2c->dpl;
3016 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003017 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3018
Willy Tarreau39d68502018-03-02 12:26:37 +01003019 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003020 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003021 h2s->cs->flags |= CS_FL_REOS;
3022 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003023
Willy Tarreau454b57b2018-02-26 15:50:05 +01003024 return flen + chklen;
3025 fail:
3026 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003027}
3028
Willy Tarreau5dd17352018-06-14 13:33:30 +02003029/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3030 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3031 * number of bytes sent. The caller must check the stream's status to detect
3032 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003033 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003034static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003035{
3036 struct http_hdr list[MAX_HTTP_HDR];
3037 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003038 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003039 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003040 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003041 int es_now = 0;
3042 int ret = 0;
3043 int hdr;
3044
3045 if (h2c_mux_busy(h2c, h2s)) {
3046 h2s->flags |= H2_SF_BLK_MBUSY;
3047 return 0;
3048 }
3049
Willy Tarreau44e973f2018-03-01 17:49:30 +01003050 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003051 h2c->flags |= H2_CF_MUX_MALLOC;
3052 h2s->flags |= H2_SF_BLK_MROOM;
3053 return 0;
3054 }
3055
3056 /* First, try to parse the H1 response and index it into <list>.
3057 * NOTE! Since it comes from haproxy, we *know* that a response header
3058 * block does not wrap and we can safely read it this way without
3059 * having to realign the buffer.
3060 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003061 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003062 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003063 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003064 /* incomplete or invalid response, this is abnormal coming from
3065 * haproxy and may only result in a bad errorfile or bad Lua code
3066 * so that won't be fixed, raise an error now.
3067 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003068 * FIXME: we should instead add the ability to only return a
3069 * 502 bad gateway. But in theory this is not supposed to
3070 * happen.
3071 */
3072 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3073 ret = 0;
3074 goto end;
3075 }
3076
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003077 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003078
3079 /* certain statuses have no body or an empty one, regardless of
3080 * what the headers say.
3081 */
3082 if (sl.st.status >= 100 && sl.st.status < 200) {
3083 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3084 h1m->curr_len = h1m->body_len = 0;
3085 }
3086 else if (sl.st.status == 204 || sl.st.status == 304) {
3087 /* no contents, claim c-len is present and set to zero */
3088 h1m->flags &= ~H1_MF_CHNK;
3089 h1m->flags |= H1_MF_CLEN;
3090 h1m->curr_len = h1m->body_len = 0;
3091 }
3092
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003093 chunk_reset(&outbuf);
3094
3095 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003096 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003097 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003098 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003099
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003100 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003101 break;
3102 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003103 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003104 }
3105
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003106 if (outbuf.size < 9)
3107 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003108
3109 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003110 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3111 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3112 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003113
3114 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003115 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003116 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003117 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003118 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003119 else if (unlikely(list[0].v.len != 3)) {
3120 /* this is an unparsable response */
3121 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3122 ret = 0;
3123 goto end;
3124 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003125 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003126 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003127 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3128 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3129 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3130 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3131 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003132 }
3133 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003134 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003135 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003136 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003137 }
3138
3139 /* encode all headers, stop at empty name */
3140 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003141 /* these ones do not exist in H2 and must be dropped. */
3142 if (isteq(list[hdr].n, ist("connection")) ||
3143 isteq(list[hdr].n, ist("proxy-connection")) ||
3144 isteq(list[hdr].n, ist("keep-alive")) ||
3145 isteq(list[hdr].n, ist("upgrade")) ||
3146 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003147 continue;
3148
3149 if (isteq(list[hdr].n, ist("")))
3150 break; // end
3151
3152 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3153 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003154 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003155 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003156 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003157 }
3158 }
3159
3160 /* we may need to add END_STREAM */
3161 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3162 es_now = 1;
3163
3164 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003166
3167 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003168 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169
3170 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003171 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003172
3173 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003174 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003175 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003176
3177 /* for now we don't implemented CONTINUATION, so we wait for a
3178 * body or directly end in TRL2.
3179 */
3180 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003181 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003182 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003183
Willy Tarreau801250e2018-09-11 11:45:04 +02003184 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003185 h2s->flags |= H2_SF_ES_SENT;
3186 if (h2s->st == H2_SS_OPEN)
3187 h2s->st = H2_SS_HLOC;
3188 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003189 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003190 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003191 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003192 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003193 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003194 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003195 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003196 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003197 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003198
3199 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003200
3201 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003202 //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 +02003203 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003204 full:
3205 h1m_init_res(h1m);
3206 h1m->err_pos = -1; // don't care about errors on the response path
3207 h2c->flags |= H2_CF_MUX_MFULL;
3208 h2s->flags |= H2_SF_BLK_MROOM;
3209 ret = 0;
3210 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003211}
3212
Willy Tarreau5dd17352018-06-14 13:33:30 +02003213/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3214 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3215 * the number of bytes sent. The caller must check the stream's status to
3216 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003217 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003218static 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 +02003219{
3220 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003221 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003222 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003223 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003224 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003225 int es_now = 0;
3226 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003227 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003228 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003229
3230 if (h2c_mux_busy(h2c, h2s)) {
3231 h2s->flags |= H2_SF_BLK_MBUSY;
3232 goto end;
3233 }
3234
Willy Tarreau44e973f2018-03-01 17:49:30 +01003235 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003236 h2c->flags |= H2_CF_MUX_MALLOC;
3237 h2s->flags |= H2_SF_BLK_MROOM;
3238 goto end;
3239 }
3240
3241 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003242 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003243 goto end;
3244
3245 chunk_reset(&outbuf);
3246
3247 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003248 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003249 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003251
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003252 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003253 break;
3254 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003255 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003256 }
3257
3258 if (outbuf.size < 9) {
3259 h2c->flags |= H2_CF_MUX_MFULL;
3260 h2s->flags |= H2_SF_BLK_MROOM;
3261 goto end;
3262 }
3263
3264 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3266 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3267 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268
3269 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3270 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003271 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003272 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003273 break;
3274 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003275 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003276 if ((long long)size > h1m->curr_len)
3277 size = h1m->curr_len;
3278 break;
3279 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003280 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003281 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003282 if (!ret)
3283 goto end;
3284
3285 if (ret < 0) {
3286 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003287 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3289 goto end;
3290 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003291 max -= ret;
3292 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003294 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003295 }
3296
Willy Tarreau801250e2018-09-11 11:45:04 +02003297 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003298 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003299 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003300 if (!ret)
3301 goto end;
3302
3303 if (ret < 0) {
3304 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003305 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003306 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3307 goto end;
3308 }
3309
3310 size = chunk;
3311 h1m->curr_len = chunk;
3312 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003313 max -= ret;
3314 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003315 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003316 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003317 if (!size)
3318 goto send_empty;
3319 }
3320
3321 /* in MSG_DATA state, continue below */
3322 size = h1m->curr_len;
3323 break;
3324 }
3325
3326 /* we have in <size> the exact number of bytes we need to copy from
3327 * the H1 buffer. We need to check this against the connection's and
3328 * the stream's send windows, and to ensure that this fits in the max
3329 * frame size and in the buffer's available space minus 9 bytes (for
3330 * the frame header). The connection's flow control is applied last so
3331 * that we can use a separate list of streams which are immediately
3332 * unblocked on window opening. Note: we don't implement padding.
3333 */
3334
Willy Tarreau5dd17352018-06-14 13:33:30 +02003335 if (size > max)
3336 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003337
3338 if (size > h2s->mws)
3339 size = h2s->mws;
3340
3341 if (size <= 0) {
3342 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003343 if (h2s->send_wait) {
3344 LIST_DEL(&h2s->list);
3345 LIST_INIT(&h2s->list);
3346 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003347 goto end;
3348 }
3349
3350 if (h2c->mfs && size > h2c->mfs)
3351 size = h2c->mfs;
3352
3353 if (size + 9 > outbuf.size) {
3354 /* we have an opportunity for enlarging the too small
3355 * available space, let's try.
3356 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003357 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 goto realign_again;
3359 size = outbuf.size - 9;
3360 }
3361
3362 if (size <= 0) {
3363 h2c->flags |= H2_CF_MUX_MFULL;
3364 h2s->flags |= H2_SF_BLK_MROOM;
3365 goto end;
3366 }
3367
3368 if (size > h2c->mws)
3369 size = h2c->mws;
3370
3371 if (size <= 0) {
3372 h2s->flags |= H2_SF_BLK_MFCTL;
3373 goto end;
3374 }
3375
3376 /* copy whatever we can */
3377 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003378 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003379 if (ret == 1)
3380 len2 = 0;
3381
3382 if (!ret || len1 + len2 < size) {
3383 /* FIXME: must normally never happen */
3384 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3385 goto end;
3386 }
3387
3388 /* limit len1/len2 to size */
3389 if (len1 + len2 > size) {
3390 int sub = len1 + len2 - size;
3391
3392 if (len2 > sub)
3393 len2 -= sub;
3394 else {
3395 sub -= len2;
3396 len2 = 0;
3397 len1 -= sub;
3398 }
3399 }
3400
3401 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003402 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003403 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003404 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405
3406 send_empty:
3407 /* we may need to add END_STREAM */
3408 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3409 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003410 *
3411 * FIXME: what we do here is not correct because we send end_stream
3412 * before knowing if we'll have to send a HEADERS frame for the
3413 * trailers. More importantly we're not consuming the trailing CRLF
3414 * after the end of trailers, so it will be left to the caller to
3415 * eat it. The right way to do it would be to measure trailers here
3416 * and to send ES only if there are no trailers.
3417 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003418 */
3419 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003420 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003421 es_now = 1;
3422
3423 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003424 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003425
3426 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003427 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003428
3429 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003430 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431
3432 /* consume incoming H1 response */
3433 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003434 max -= size;
3435 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003436 total += size;
3437 h1m->curr_len -= size;
3438 h2s->mws -= size;
3439 h2c->mws -= size;
3440
3441 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003442 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003443 goto new_frame;
3444 }
3445 }
3446
3447 if (es_now) {
3448 if (h2s->st == H2_SS_OPEN)
3449 h2s->st = H2_SS_HLOC;
3450 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003451 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003452
Willy Tarreau35a62702018-02-27 15:37:25 +01003453 if (!(h1m->flags & H1_MF_CHNK)) {
3454 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003455 total += max;
3456 ofs += max;
3457 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003458
Willy Tarreau801250e2018-09-11 11:45:04 +02003459 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003460 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003461
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003462 h2s->flags |= H2_SF_ES_SENT;
3463 }
3464
3465 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003466 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 +02003467 return total;
3468}
3469
Olivier Houchard6ff20392018-07-17 18:46:31 +02003470/* Called from the upper layer, to subscribe to events, such as being able to send */
3471static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3472{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003473 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003474 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003475 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003476
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003477 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003478 sw = param;
3479 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3480 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003481 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003482 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003483 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003484 event_type &= ~SUB_CAN_RECV;
3485 }
3486 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003487 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003488 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003489 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003490 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003491 h2s->send_wait = sw;
3492 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3493 if (h2s->flags & H2_SF_BLK_MFCTL)
3494 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3495 else
3496 LIST_ADDQ(&h2c->send_list, &h2s->list);
3497 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003498 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003499 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003500 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003501 if (event_type != 0)
3502 return -1;
3503 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003504
3505
3506}
3507
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003508static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3509{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003510 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003511 struct h2s *h2s = cs->ctx;
3512
3513 if (event_type & SUB_CAN_RECV) {
3514 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003515 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003516 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003517 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003518 }
3519 }
3520 if (event_type & SUB_CAN_SEND) {
3521 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003522 if (h2s->send_wait == sw) {
3523 LIST_DEL(&h2s->list);
3524 LIST_INIT(&h2s->list);
3525 sw->wait_reason &= ~SUB_CAN_SEND;
3526 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003527 }
3528 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003529 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3530 sw = param;
3531 if (h2s->send_wait == sw) {
3532 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3533 h2s->send_wait = NULL;
3534 }
3535 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003536 return 0;
3537}
3538
3539
Olivier Houchard511efea2018-08-16 15:30:32 +02003540/* Called from the upper layer, to receive data */
3541static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3542{
Olivier Houchard638b7992018-08-16 15:41:52 +02003543 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003544 size_t ret = 0;
3545
3546 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003547 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003548
Olivier Houchard638b7992018-08-16 15:41:52 +02003549 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003550 cs->flags |= CS_FL_RCV_MORE;
3551 else {
3552 cs->flags &= ~CS_FL_RCV_MORE;
3553 if (cs->flags & CS_FL_REOS)
3554 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003555 if (b_size(&h2s->rxbuf)) {
3556 b_free(&h2s->rxbuf);
3557 offer_buffers(NULL, tasks_run_queue);
3558 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003559 }
3560
3561 return ret;
3562}
3563
Olivier Houchardd846c262018-10-19 17:24:29 +02003564static void h2_stop_senders(struct h2c *h2c)
3565{
3566 struct h2s *h2s, *h2s_back;
3567
3568 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3569 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3570 if (h2c->msi == h2s_id(h2s))
3571 continue;
3572 LIST_DEL(&h2s->list);
3573 LIST_INIT(&h2s->list);
3574 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3575 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3576 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3577 LIST_ADD(&h2c->send_list, &h2s->list);
3578 }
3579}
3580
Willy Tarreau62f52692017-10-08 23:01:42 +02003581/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003582static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003583{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003584 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003585 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003586 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003587
Olivier Houchardd846c262018-10-19 17:24:29 +02003588 if (h2s->send_wait) {
3589 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3590 h2s->send_wait = NULL;
3591 LIST_DEL(&h2s->list);
3592 LIST_INIT(&h2s->list);
3593 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003594 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3595 return 0;
3596
Willy Tarreau0bad0432018-06-14 16:54:01 +02003597 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003598 h2s->flags |= H2_SF_OUTGOING_DATA;
3599
Willy Tarreaua40704a2018-09-11 13:52:04 +02003600 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003601 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003602 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003603 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003604 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003605 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003606 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003607 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003608 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003609 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003610
Willy Tarreau5dd17352018-06-14 13:33:30 +02003611 if (unlikely((int)ret <= 0)) {
3612 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003613 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3614 break;
3615 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003616 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003617 total += count;
3618 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003619 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003620 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003621 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003622 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003623 cs->flags |= CS_FL_ERROR;
3624 break;
3625 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003626
3627 total += ret;
3628 count -= ret;
3629
3630 if (h2s->st >= H2_SS_ERROR)
3631 break;
3632
3633 if (h2s->flags & H2_SF_BLK_ANY)
3634 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003635 }
3636
Willy Tarreau00610962018-07-19 10:58:28 +02003637 if (h2s->st >= H2_SS_ERROR) {
3638 /* trim any possibly pending data after we close (extra CR-LF,
3639 * unprocessed trailers, abnormal extra data, ...)
3640 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003641 total += count;
3642 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003643 }
3644
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003645 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003646 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003647 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003648 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003649 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003650 }
3651
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003652 b_del(buf, total);
Olivier Houchardd846c262018-10-19 17:24:29 +02003653
3654 /* The mux is full, cancel the pending tasks */
3655 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3656 (h2s->flags & H2_SF_BLK_MBUSY))
3657 h2_stop_senders(h2s->h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02003658 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003659 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3660 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003661
Olivier Houchard7505f942018-08-21 18:10:44 +02003662 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003663 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003664}
3665
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003666/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003667static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003668{
3669 struct h2c *h2c = conn->mux_ctx;
3670 struct h2s *h2s;
3671 struct eb32_node *node;
3672 int fctl_cnt = 0;
3673 int send_cnt = 0;
3674 int tree_cnt = 0;
3675 int orph_cnt = 0;
3676
3677 if (!h2c)
3678 return;
3679
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003680 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003681 fctl_cnt++;
3682
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003683 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003684 send_cnt++;
3685
3686 node = eb32_first(&h2c->streams_by_id);
3687 while (node) {
3688 h2s = container_of(node, struct h2s, by_id);
3689 tree_cnt++;
3690 if (!h2s->cs)
3691 orph_cnt++;
3692 node = eb32_next(node);
3693 }
3694
Willy Tarreau616ac812018-07-24 14:12:42 +02003695 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3696 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3697 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3698 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3699 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3700 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003701}
Willy Tarreau62f52692017-10-08 23:01:42 +02003702
3703/*******************************************************/
3704/* functions below are dedicated to the config parsers */
3705/*******************************************************/
3706
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003707/* config parser for global "tune.h2.header-table-size" */
3708static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3709 struct proxy *defpx, const char *file, int line,
3710 char **err)
3711{
3712 if (too_many_args(1, args, err, NULL))
3713 return -1;
3714
3715 h2_settings_header_table_size = atoi(args[1]);
3716 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3717 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3718 return -1;
3719 }
3720 return 0;
3721}
Willy Tarreau62f52692017-10-08 23:01:42 +02003722
Willy Tarreaue6baec02017-07-27 11:45:11 +02003723/* config parser for global "tune.h2.initial-window-size" */
3724static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3725 struct proxy *defpx, const char *file, int line,
3726 char **err)
3727{
3728 if (too_many_args(1, args, err, NULL))
3729 return -1;
3730
3731 h2_settings_initial_window_size = atoi(args[1]);
3732 if (h2_settings_initial_window_size < 0) {
3733 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3734 return -1;
3735 }
3736 return 0;
3737}
3738
Willy Tarreau5242ef82017-07-27 11:47:28 +02003739/* config parser for global "tune.h2.max-concurrent-streams" */
3740static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3741 struct proxy *defpx, const char *file, int line,
3742 char **err)
3743{
3744 if (too_many_args(1, args, err, NULL))
3745 return -1;
3746
3747 h2_settings_max_concurrent_streams = atoi(args[1]);
3748 if (h2_settings_max_concurrent_streams < 0) {
3749 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3750 return -1;
3751 }
3752 return 0;
3753}
3754
Willy Tarreau62f52692017-10-08 23:01:42 +02003755
3756/****************************************/
3757/* MUX initialization and instanciation */
3758/***************************************/
3759
3760/* The mux operations */
3761const struct mux_ops h2_ops = {
3762 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003763 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003764 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003765 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003766 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003767 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003768 .attach = h2_attach,
3769 .detach = h2_detach,
3770 .shutr = h2_shutr,
3771 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003772 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003773 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003774 .name = "H2",
3775};
3776
Christopher Faulet32f61c02018-04-10 14:33:41 +02003777/* PROTO selection : this mux registers PROTO token "h2" */
3778static struct mux_proto_list mux_proto_h2 =
3779 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003780
3781/* config keyword parsers */
3782static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003783 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003784 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003785 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003786 { 0, NULL, NULL }
3787}};
3788
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003789static void __h2_deinit(void)
3790{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003791 pool_destroy(pool_head_h2s);
3792 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003793}
3794
Willy Tarreau62f52692017-10-08 23:01:42 +02003795__attribute__((constructor))
3796static void __h2_init(void)
3797{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003798 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003799 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003800 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003801 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3802 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003803}