blob: c6fa1c90cea590c61dae75b742413959b10cfbe3 [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 Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
Olivier Houchard638b7992018-08-16 15:41:52 +0200186 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200187 struct wait_list *recv_wait_list; /* Somebody subscribed to be waken up on recv */
Willy Tarreau18312642017-10-11 07:57:07 +0200188};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200189
Willy Tarreauc6405142017-09-21 20:23:50 +0200190/* descriptor for an h2 frame header */
191struct h2_fh {
192 uint32_t len; /* length, host order, 24 bits */
193 uint32_t sid; /* stream id, host order, 31 bits */
194 uint8_t ft; /* frame type */
195 uint8_t ff; /* frame flags */
196};
197
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198/* a few settings from the global section */
199static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200200static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200201static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200202
Willy Tarreau2a856182017-05-16 15:20:39 +0200203/* a dmumy closed stream */
204static const struct h2s *h2_closed_stream = &(const struct h2s){
205 .cs = NULL,
206 .h2c = NULL,
207 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100208 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100209 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200210 .id = 0,
211};
212
213/* and a dummy idle stream for use with any unannounced stream */
214static const struct h2s *h2_idle_stream = &(const struct h2s){
215 .cs = NULL,
216 .h2c = NULL,
217 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100218 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200219 .id = 0,
220};
221
Olivier Houchard9f6af332018-05-25 14:04:04 +0200222static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200223static int h2_send(struct h2c *h2c);
224static int h2_recv(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200225static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100226static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100227static int h2_frt_decode_headers(struct h2s *h2s);
228static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200229
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200230/*****************************************************/
231/* functions below are for dynamic buffer management */
232/*****************************************************/
233
Willy Tarreau315d8072017-12-10 22:17:57 +0100234/* indicates whether or not the we may call the h2_recv() function to attempt
235 * to receive data into the buffer and/or demux pending data. The condition is
236 * a bit complex due to some API limits for now. The rules are the following :
237 * - if an error or a shutdown was detected on the connection and the buffer
238 * is empty, we must not attempt to receive
239 * - if the demux buf failed to be allocated, we must not try to receive and
240 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100241 * - if no flag indicates a blocking condition, we may attempt to receive,
242 * regardless of whether the demux buffer is full or not, so that only
243 * de demux part decides whether or not to block. This is needed because
244 * the connection API indeed prevents us from re-enabling receipt that is
245 * already enabled in a polled state, so we must always immediately stop
246 * as soon as the demux can't proceed so as never to hit an end of read
247 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100248 * - otherwise must may not attempt
249 */
250static inline int h2_recv_allowed(const struct h2c *h2c)
251{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200252 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100253 (h2c->st0 >= H2_CS_ERROR ||
254 h2c->conn->flags & CO_FL_ERROR ||
255 conn_xprt_read0_pending(h2c->conn)))
256 return 0;
257
258 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100259 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100260 return 1;
261
262 return 0;
263}
264
Willy Tarreauf2101912018-07-19 10:11:38 +0200265/* returns true if the connection has too many conn_streams attached */
266static inline int h2_has_too_many_cs(const struct h2c *h2c)
267{
268 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
269}
270
Willy Tarreau44e973f2018-03-01 17:49:30 +0100271/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
272 * flags are used to figure what buffer was requested. It returns 1 if the
273 * allocation succeeds, in which case the connection is woken up, or 0 if it's
274 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200275 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100276static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277{
278 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100279 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200280
Willy Tarreau44e973f2018-03-01 17:49:30 +0100281 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200282 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200283 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200284 conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200285 h2_recv(h2c);
286 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200287 return 1;
288 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200289
Willy Tarreau44e973f2018-03-01 17:49:30 +0100290 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
291 h2c->flags &= ~H2_CF_MUX_MALLOC;
292 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
293 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200294
295 if (h2c->flags & H2_CF_DEM_MROOM) {
296 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200297 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200298 conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200299 h2_recv(h2c);
300 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301 }
Willy Tarreau14398122017-09-22 14:26:04 +0200302 return 1;
303 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100304
305 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
306 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200307 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100308 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200309 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100310 conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200311 h2_recv(h2c);
312 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 return 1;
314 }
315
Willy Tarreau14398122017-09-22 14:26:04 +0200316 return 0;
317}
318
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200319static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200320{
321 struct buffer *buf = NULL;
322
Willy Tarreau44e973f2018-03-01 17:49:30 +0100323 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
324 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
325 h2c->buf_wait.target = h2c;
326 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200330 __conn_xprt_stop_recv(h2c->conn);
331 }
332 return buf;
333}
334
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200336{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100338 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200339 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200340 }
341}
342
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200343
Willy Tarreau62f52692017-10-08 23:01:42 +0200344/*****************************************************************/
345/* functions below are dedicated to the mux setup and management */
346/*****************************************************************/
347
Willy Tarreau32218eb2017-09-22 08:07:25 +0200348/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
349static int h2c_frt_init(struct connection *conn)
350{
351 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100352 struct task *t = NULL;
353 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200354
Willy Tarreaubafbe012017-11-24 17:34:44 +0100355 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200356 if (!h2c)
357 goto fail;
358
Willy Tarreau3f133572017-10-31 19:21:06 +0100359
Willy Tarreau599391a2017-11-24 10:16:00 +0100360 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
361 if (tick_isset(sess->fe->timeout.clientfin))
362 h2c->shut_timeout = sess->fe->timeout.clientfin;
363
Willy Tarreau33400292017-11-05 11:23:40 +0100364 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100365 if (tick_isset(h2c->timeout)) {
366 t = task_new(tid_bit);
367 if (!t)
368 goto fail;
369
370 h2c->task = t;
371 t->process = h2_timeout_task;
372 t->context = h2c;
373 t->expire = tick_add(now_ms, h2c->timeout);
374 }
Willy Tarreauea392822017-10-31 10:02:25 +0100375
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200376 h2c->wait_list.task = tasklet_new();
377 if (!h2c->wait_list.task)
378 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200379 h2c->wait_list.task->process = h2_io_cb;
380 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200381 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200382
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
384 if (!h2c->ddht)
385 goto fail;
386
387 /* Initialise the context. */
388 h2c->st0 = H2_CS_PREFACE;
389 h2c->conn = conn;
390 h2c->max_id = -1;
391 h2c->errcode = H2_ERR_NO_ERROR;
392 h2c->flags = H2_CF_NONE;
393 h2c->rcvd_c = 0;
394 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100395 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200396 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200397
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200398 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200399 h2c->dsi = -1;
400 h2c->msi = -1;
401 h2c->last_sid = -1;
402
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200403 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->miw = 65535; /* mux initial window size */
405 h2c->mws = 65535; /* mux window size */
406 h2c->mfs = 16384; /* initial max frame size */
407 h2c->streams_by_id = EB_ROOT_UNIQUE;
408 LIST_INIT(&h2c->send_list);
409 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100410 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411 conn->mux_ctx = h2c;
412
Willy Tarreau3f133572017-10-31 19:21:06 +0100413 if (t)
414 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200415 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200416 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200417 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100418
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200419 /* Try to read, if nothing is available yet we'll just subscribe */
420 h2_recv(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 return 0;
422 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100423 if (t)
424 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200425 if (h2c->wait_list.task)
426 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100427 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200428 return -1;
429}
430
Willy Tarreau62f52692017-10-08 23:01:42 +0200431/* Initialize the mux once it's attached. For outgoing connections, the context
432 * is already initialized before installing the mux, so we detect incoming
433 * connections from the fact that the context is still NULL. Returns < 0 on
434 * error.
435 */
436static int h2_init(struct connection *conn)
437{
438 if (conn->mux_ctx) {
439 /* we don't support outgoing connections for now */
440 return -1;
441 }
442
Willy Tarreau32218eb2017-09-22 08:07:25 +0200443 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200444}
445
Willy Tarreau2373acc2017-10-12 17:35:14 +0200446/* returns the stream associated with id <id> or NULL if not found */
447static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
448{
449 struct eb32_node *node;
450
Willy Tarreau2a856182017-05-16 15:20:39 +0200451 if (id > h2c->max_id)
452 return (struct h2s *)h2_idle_stream;
453
Willy Tarreau2373acc2017-10-12 17:35:14 +0200454 node = eb32_lookup(&h2c->streams_by_id, id);
455 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200457
458 return container_of(node, struct h2s, by_id);
459}
460
Willy Tarreau62f52692017-10-08 23:01:42 +0200461/* release function for a connection. This one should be called to free all
462 * resources allocated to the mux.
463 */
464static void h2_release(struct connection *conn)
465{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466 struct h2c *h2c = conn->mux_ctx;
467
468 LIST_DEL(&conn->list);
469
470 if (h2c) {
471 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200472
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100474 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100475 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200476
Willy Tarreau44e973f2018-03-01 17:49:30 +0100477 h2_release_buf(h2c, &h2c->dbuf);
478 h2_release_buf(h2c, &h2c->mbuf);
479
Willy Tarreauea392822017-10-31 10:02:25 +0100480 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200481 h2c->task->context = NULL;
482 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100483 h2c->task = NULL;
484 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200485 if (h2c->wait_list.task)
486 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100487
Willy Tarreaubafbe012017-11-24 17:34:44 +0100488 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200489 }
490
491 conn->mux = NULL;
492 conn->mux_ctx = NULL;
493
494 conn_stop_tracking(conn);
495 conn_full_close(conn);
496 if (conn->destroy_cb)
497 conn->destroy_cb(conn);
498 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200499}
500
501
Willy Tarreau71681172017-10-23 14:39:06 +0200502/******************************************************/
503/* functions below are for the H2 protocol processing */
504/******************************************************/
505
506/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100507static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200508{
509 return h2s ? h2s->id : 0;
510}
511
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200512/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200514{
515 if (h2c->msi < 0)
516 return 0;
517
518 if (h2c->msi == h2s_id(h2s))
519 return 0;
520
521 return 1;
522}
523
Willy Tarreau741d6df2017-10-17 08:00:59 +0200524/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200526{
527 h2c->errcode = err;
528 h2c->st0 = H2_CS_ERROR;
529}
530
Willy Tarreau2e43f082017-10-17 08:03:59 +0200531/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200533{
534 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
535 h2s->errcode = err;
536 h2s->st = H2_SS_ERROR;
537 if (h2s->cs)
538 h2s->cs->flags |= CS_FL_ERROR;
539 }
540}
541
Willy Tarreaue4820742017-07-27 13:37:23 +0200542/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100543static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200544{
545 uint8_t *out = frame;
546
547 *out = len >> 16;
548 write_n16(out + 1, len);
549}
550
Willy Tarreau54c15062017-10-10 17:10:03 +0200551/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
552 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
553 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200554 * available in the buffer's input prior to calling this function. The buffer
555 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200556 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200558 const struct buffer *b, int o)
559{
Willy Tarreau591d4452018-06-15 17:21:00 +0200560 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200561}
562
Willy Tarreau1f094672017-11-20 21:27:45 +0100563static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200564{
Willy Tarreau591d4452018-06-15 17:21:00 +0200565 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200566}
567
Willy Tarreau1f094672017-11-20 21:27:45 +0100568static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200569{
Willy Tarreau591d4452018-06-15 17:21:00 +0200570 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200571}
572
Willy Tarreau1f094672017-11-20 21:27:45 +0100573static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200574{
Willy Tarreau591d4452018-06-15 17:21:00 +0200575 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200576}
577
578
Willy Tarreau715d5312017-07-11 15:20:24 +0200579/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
580 * is not obvious. It turns out that H2 headers are neither aligned nor do they
581 * use regular sizes. And to add to the trouble, the buffer may wrap so each
582 * byte read must be checked. The header is formed like this :
583 *
584 * b0 b1 b2 b3 b4 b5..b8
585 * +----------+---------+--------+----+----+----------------------+
586 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
587 * +----------+---------+--------+----+----+----------------------+
588 *
589 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
590 * we get the sid properly aligned and ordered, and 16 bits of len properly
591 * ordered as well. The type and flags can be extracted using bit shifts from
592 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200593 * Returns zero if some bytes are missing, otherwise non-zero on success. The
594 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200595 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100596static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200597{
598 uint64_t w;
599
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200600 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200601 return 0;
602
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200603 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200604 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200605 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
606 h->ff = w >> 32;
607 h->ft = w >> 40;
608 h->len += w >> 48;
609 return 1;
610}
611
612/* skip the next 9 bytes corresponding to the frame header possibly parsed by
613 * h2_peek_frame_hdr() above.
614 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100615static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200616{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200617 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200618}
619
620/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100621static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200622{
623 int ret;
624
625 ret = h2_peek_frame_hdr(b, h);
626 if (ret > 0)
627 h2_skip_frame_hdr(b);
628 return ret;
629}
630
Willy Tarreau00dd0782018-03-01 16:31:34 +0100631/* marks stream <h2s> as CLOSED and decrement the number of active streams for
632 * its connection if the stream was not yet closed. Please use this exclusively
633 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100634 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100635static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100636{
637 if (h2s->st != H2_SS_CLOSED)
638 h2s->h2c->nb_streams--;
639 h2s->st = H2_SS_CLOSED;
640}
641
Willy Tarreau71049cc2018-03-28 13:56:39 +0200642/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
643static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100644{
645 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200646 LIST_DEL(&h2s->list);
647 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100648 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200649 if (b_size(&h2s->rxbuf)) {
650 b_free(&h2s->rxbuf);
651 offer_buffers(NULL, tasks_run_queue);
652 }
Willy Tarreau0a10de62018-03-01 16:27:53 +0100653 pool_free(pool_head_h2s, h2s);
654}
655
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200656/* creates a new stream <id> on the h2c connection and returns it, or NULL in
657 * case of memory allocation error.
658 */
659static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
660{
Willy Tarreau590a0512018-09-05 11:56:48 +0200661 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200662 struct conn_stream *cs;
663 struct h2s *h2s;
664
Willy Tarreaubafbe012017-11-24 17:34:44 +0100665 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200666 if (!h2s)
667 goto out;
668
669 h2s->h2c = h2c;
670 h2s->mws = h2c->miw;
671 h2s->flags = H2_SF_NONE;
672 h2s->errcode = H2_ERR_NO_ERROR;
673 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200674 h2s->rxbuf = BUF_NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675 h1m_init(&h2s->req);
676 h1m_init(&h2s->res);
677 h2s->by_id.key = h2s->id = id;
678 h2c->max_id = id;
679 LIST_INIT(&h2s->list);
680
681 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100682 h2c->nb_streams++;
683 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
684 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200685
686 cs = cs_new(h2c->conn);
687 if (!cs)
688 goto out_close;
689
690 h2s->cs = cs;
691 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200692 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200693
694 if (stream_create_from_cs(cs) < 0)
695 goto out_free_cs;
696
Willy Tarreau590a0512018-09-05 11:56:48 +0200697 /* We want the accept date presented to the next stream to be the one
698 * we have now, the handshake time to be null (since the next stream
699 * is not delayed by a handshake), and the idle time to count since
700 * right now.
701 */
702 sess->accept_date = date;
703 sess->tv_accept = now;
704 sess->t_handshake = 0;
705
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200706 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200707 if (h2_has_too_many_cs(h2c))
708 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200709 return h2s;
710
711 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200712 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200713 cs_free(cs);
714 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200715 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200716 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200717 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200718 out:
719 return h2s;
720}
721
Willy Tarreaube5b7152017-09-25 16:25:39 +0200722/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
723 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
724 * the various settings codes.
725 */
726static int h2c_snd_settings(struct h2c *h2c)
727{
728 struct buffer *res;
729 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200730 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200731 int ret;
732
733 if (h2c_mux_busy(h2c, NULL)) {
734 h2c->flags |= H2_CF_DEM_MBUSY;
735 return 0;
736 }
737
Willy Tarreau44e973f2018-03-01 17:49:30 +0100738 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200739 if (!res) {
740 h2c->flags |= H2_CF_MUX_MALLOC;
741 h2c->flags |= H2_CF_DEM_MROOM;
742 return 0;
743 }
744
745 chunk_init(&buf, buf_data, sizeof(buf_data));
746 chunk_memcpy(&buf,
747 "\x00\x00\x00" /* length : 0 for now */
748 "\x04\x00" /* type : 4 (settings), flags : 0 */
749 "\x00\x00\x00\x00", /* stream ID : 0 */
750 9);
751
752 if (h2_settings_header_table_size != 4096) {
753 char str[6] = "\x00\x01"; /* header_table_size */
754
755 write_n32(str + 2, h2_settings_header_table_size);
756 chunk_memcat(&buf, str, 6);
757 }
758
759 if (h2_settings_initial_window_size != 65535) {
760 char str[6] = "\x00\x04"; /* initial_window_size */
761
762 write_n32(str + 2, h2_settings_initial_window_size);
763 chunk_memcat(&buf, str, 6);
764 }
765
766 if (h2_settings_max_concurrent_streams != 0) {
767 char str[6] = "\x00\x03"; /* max_concurrent_streams */
768
769 /* Note: 0 means "unlimited" for haproxy's config but not for
770 * the protocol, so never send this value!
771 */
772 write_n32(str + 2, h2_settings_max_concurrent_streams);
773 chunk_memcat(&buf, str, 6);
774 }
775
776 if (global.tune.bufsize != 16384) {
777 char str[6] = "\x00\x05"; /* max_frame_size */
778
779 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
780 * match bufsize - rewrite size, but at the moment it seems
781 * that clients don't take care of it.
782 */
783 write_n32(str + 2, global.tune.bufsize);
784 chunk_memcat(&buf, str, 6);
785 }
786
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200787 h2_set_frame_size(buf.area, buf.data - 9);
788 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200789 if (unlikely(ret <= 0)) {
790 if (!ret) {
791 h2c->flags |= H2_CF_MUX_MFULL;
792 h2c->flags |= H2_CF_DEM_MROOM;
793 return 0;
794 }
795 else {
796 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
797 return 0;
798 }
799 }
800 return ret;
801}
802
Willy Tarreau52eed752017-09-22 15:05:09 +0200803/* Try to receive a connection preface, then upon success try to send our
804 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
805 * missing data. It may return an error in h2c.
806 */
807static int h2c_frt_recv_preface(struct h2c *h2c)
808{
809 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200810 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200811
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200812 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200813
814 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200815 if (ret1 < 0)
816 sess_log(h2c->conn->owner);
817
Willy Tarreau52eed752017-09-22 15:05:09 +0200818 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
819 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
820 return 0;
821 }
822
Willy Tarreaube5b7152017-09-25 16:25:39 +0200823 ret2 = h2c_snd_settings(h2c);
824 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200825 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200826
Willy Tarreaube5b7152017-09-25 16:25:39 +0200827 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200828}
829
Willy Tarreau081d4722017-05-16 21:51:05 +0200830/* try to send a GOAWAY frame on the connection to report an error or a graceful
831 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
832 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
833 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
834 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
835 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
836 * on unrecoverable failure. It will not attempt to send one again in this last
837 * case so that it is safe to use h2c_error() to report such errors.
838 */
839static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
840{
841 struct buffer *res;
842 char str[17];
843 int ret;
844
845 if (h2c->flags & H2_CF_GOAWAY_FAILED)
846 return 1; // claim that it worked
847
848 if (h2c_mux_busy(h2c, h2s)) {
849 if (h2s)
850 h2s->flags |= H2_SF_BLK_MBUSY;
851 else
852 h2c->flags |= H2_CF_DEM_MBUSY;
853 return 0;
854 }
855
Willy Tarreau44e973f2018-03-01 17:49:30 +0100856 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200857 if (!res) {
858 h2c->flags |= H2_CF_MUX_MALLOC;
859 if (h2s)
860 h2s->flags |= H2_SF_BLK_MROOM;
861 else
862 h2c->flags |= H2_CF_DEM_MROOM;
863 return 0;
864 }
865
866 /* len: 8, type: 7, flags: none, sid: 0 */
867 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
868
869 if (h2c->last_sid < 0)
870 h2c->last_sid = h2c->max_id;
871
872 write_n32(str + 9, h2c->last_sid);
873 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200874 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200875 if (unlikely(ret <= 0)) {
876 if (!ret) {
877 h2c->flags |= H2_CF_MUX_MFULL;
878 if (h2s)
879 h2s->flags |= H2_SF_BLK_MROOM;
880 else
881 h2c->flags |= H2_CF_DEM_MROOM;
882 return 0;
883 }
884 else {
885 /* we cannot report this error using GOAWAY, so we mark
886 * it and claim a success.
887 */
888 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
889 h2c->flags |= H2_CF_GOAWAY_FAILED;
890 return 1;
891 }
892 }
893 h2c->flags |= H2_CF_GOAWAY_SENT;
894 return ret;
895}
896
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897/* Try to send an RST_STREAM frame on the connection for the indicated stream
898 * during mux operations. This stream must be valid and cannot be closed
899 * already. h2s->id will be used for the stream ID and h2s->errcode will be
900 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
901 * not yet.
902 *
903 * Returns > 0 on success or zero if nothing was done. In case of lack of room
904 * to write the message, it subscribes the stream to future notifications.
905 */
906static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
907{
908 struct buffer *res;
909 char str[13];
910 int ret;
911
912 if (!h2s || h2s->st == H2_SS_CLOSED)
913 return 1;
914
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100915 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
916 * RST_STREAM in response to a RST_STREAM frame.
917 */
918 if (h2c->dft == H2_FT_RST_STREAM) {
919 ret = 1;
920 goto ignore;
921 }
922
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 if (h2c_mux_busy(h2c, h2s)) {
924 h2s->flags |= H2_SF_BLK_MBUSY;
925 return 0;
926 }
927
Willy Tarreau44e973f2018-03-01 17:49:30 +0100928 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100929 if (!res) {
930 h2c->flags |= H2_CF_MUX_MALLOC;
931 h2s->flags |= H2_SF_BLK_MROOM;
932 return 0;
933 }
934
935 /* len: 4, type: 3, flags: none */
936 memcpy(str, "\x00\x00\x04\x03\x00", 5);
937 write_n32(str + 5, h2s->id);
938 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200939 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940
941 if (unlikely(ret <= 0)) {
942 if (!ret) {
943 h2c->flags |= H2_CF_MUX_MFULL;
944 h2s->flags |= H2_SF_BLK_MROOM;
945 return 0;
946 }
947 else {
948 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
949 return 0;
950 }
951 }
952
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100953 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100954 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100955 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100956 return ret;
957}
958
959/* Try to send an RST_STREAM frame on the connection for the stream being
960 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
961 * error code unless the stream's state already is IDLE or CLOSED in which
962 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
963 * it was not yet.
964 *
965 * Returns > 0 on success or zero if nothing was done. In case of lack of room
966 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200967 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100968 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200969 */
970static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
971{
972 struct buffer *res;
973 char str[13];
974 int ret;
975
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100976 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
977 * RST_STREAM in response to a RST_STREAM frame.
978 */
979 if (h2c->dft == H2_FT_RST_STREAM) {
980 ret = 1;
981 goto ignore;
982 }
983
Willy Tarreau27a84c92017-10-17 08:10:17 +0200984 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100985 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200986 return 0;
987 }
988
Willy Tarreau44e973f2018-03-01 17:49:30 +0100989 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200990 if (!res) {
991 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100992 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200993 return 0;
994 }
995
996 /* len: 4, type: 3, flags: none */
997 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100998
Willy Tarreau27a84c92017-10-17 08:10:17 +0200999 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001000 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001001 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001002 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001003
Willy Tarreau27a84c92017-10-17 08:10:17 +02001004 if (unlikely(ret <= 0)) {
1005 if (!ret) {
1006 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001007 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001008 return 0;
1009 }
1010 else {
1011 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1012 return 0;
1013 }
1014 }
1015
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001016 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001017 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001018 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001019 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001020 }
1021
Willy Tarreau27a84c92017-10-17 08:10:17 +02001022 return ret;
1023}
1024
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001025/* try to send an empty DATA frame with the ES flag set to notify about the
1026 * end of stream and match a shutdown(write). If an ES was already sent as
1027 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1028 * on success or zero if nothing was done. In case of lack of room to write the
1029 * message, it subscribes the requesting stream to future notifications.
1030 */
1031static int h2_send_empty_data_es(struct h2s *h2s)
1032{
1033 struct h2c *h2c = h2s->h2c;
1034 struct buffer *res;
1035 char str[9];
1036 int ret;
1037
Willy Tarreau721c9742017-11-07 11:05:42 +01001038 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001039 return 1;
1040
1041 if (h2c_mux_busy(h2c, h2s)) {
1042 h2s->flags |= H2_SF_BLK_MBUSY;
1043 return 0;
1044 }
1045
Willy Tarreau44e973f2018-03-01 17:49:30 +01001046 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001047 if (!res) {
1048 h2c->flags |= H2_CF_MUX_MALLOC;
1049 h2s->flags |= H2_SF_BLK_MROOM;
1050 return 0;
1051 }
1052
1053 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1054 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1055 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001056 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001057 if (likely(ret > 0)) {
1058 h2s->flags |= H2_SF_ES_SENT;
1059 }
1060 else if (!ret) {
1061 h2c->flags |= H2_CF_MUX_MFULL;
1062 h2s->flags |= H2_SF_BLK_MROOM;
1063 return 0;
1064 }
1065 else {
1066 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1067 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001068 }
1069 return ret;
1070}
1071
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001072/* wake the streams attached to the connection, whose id is greater than <last>,
1073 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001074 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1075 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001076 */
1077static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1078{
1079 struct eb32_node *node;
1080 struct h2s *h2s;
1081
1082 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1083 flags |= CS_FL_ERROR;
1084
1085 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001086 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001087
1088 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1089 while (node) {
1090 h2s = container_of(node, struct h2s, by_id);
1091 if (h2s->id <= last)
1092 break;
1093 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001094
1095 if (!h2s->cs) {
1096 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001097 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001098 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001099 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001100
1101 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001102 h2s->cs->data_cb->wake(h2s->cs);
1103
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001104 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1105 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001106 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001107 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001108 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001109 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001110 }
1111}
1112
Willy Tarreau3421aba2017-07-27 15:41:03 +02001113/* Increase all streams' outgoing window size by the difference passed in
1114 * argument. This is needed upon receipt of the settings frame if the initial
1115 * window size is different. The difference may be negative and the resulting
1116 * window size as well, for the time it takes to receive some window updates.
1117 */
1118static void h2c_update_all_ws(struct h2c *h2c, int diff)
1119{
1120 struct h2s *h2s;
1121 struct eb32_node *node;
1122
1123 if (!diff)
1124 return;
1125
1126 node = eb32_first(&h2c->streams_by_id);
1127 while (node) {
1128 h2s = container_of(node, struct h2s, by_id);
1129 h2s->mws += diff;
1130 node = eb32_next(node);
1131 }
1132}
1133
1134/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1135 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1136 * return an error in h2c. Described in RFC7540#6.5.
1137 */
1138static int h2c_handle_settings(struct h2c *h2c)
1139{
1140 unsigned int offset;
1141 int error;
1142
1143 if (h2c->dff & H2_F_SETTINGS_ACK) {
1144 if (h2c->dfl) {
1145 error = H2_ERR_FRAME_SIZE_ERROR;
1146 goto fail;
1147 }
1148 return 1;
1149 }
1150
1151 if (h2c->dsi != 0) {
1152 error = H2_ERR_PROTOCOL_ERROR;
1153 goto fail;
1154 }
1155
1156 if (h2c->dfl % 6) {
1157 error = H2_ERR_FRAME_SIZE_ERROR;
1158 goto fail;
1159 }
1160
1161 /* that's the limit we can process */
1162 if (h2c->dfl > global.tune.bufsize) {
1163 error = H2_ERR_FRAME_SIZE_ERROR;
1164 goto fail;
1165 }
1166
1167 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001168 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001169 return 0;
1170
1171 /* parse the frame */
1172 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001173 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1174 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001175
1176 switch (type) {
1177 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1178 /* we need to update all existing streams with the
1179 * difference from the previous iws.
1180 */
1181 if (arg < 0) { // RFC7540#6.5.2
1182 error = H2_ERR_FLOW_CONTROL_ERROR;
1183 goto fail;
1184 }
1185 h2c_update_all_ws(h2c, arg - h2c->miw);
1186 h2c->miw = arg;
1187 break;
1188 case H2_SETTINGS_MAX_FRAME_SIZE:
1189 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1190 error = H2_ERR_PROTOCOL_ERROR;
1191 goto fail;
1192 }
1193 h2c->mfs = arg;
1194 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001195 case H2_SETTINGS_ENABLE_PUSH:
1196 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1197 error = H2_ERR_PROTOCOL_ERROR;
1198 goto fail;
1199 }
1200 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001201 }
1202 }
1203
1204 /* need to ACK this frame now */
1205 h2c->st0 = H2_CS_FRAME_A;
1206 return 1;
1207 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001208 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001209 h2c_error(h2c, error);
1210 return 0;
1211}
1212
1213/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1214 * success or one of the h2_status values.
1215 */
1216static int h2c_ack_settings(struct h2c *h2c)
1217{
1218 struct buffer *res;
1219 char str[9];
1220 int ret = -1;
1221
1222 if (h2c_mux_busy(h2c, NULL)) {
1223 h2c->flags |= H2_CF_DEM_MBUSY;
1224 return 0;
1225 }
1226
Willy Tarreau44e973f2018-03-01 17:49:30 +01001227 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001228 if (!res) {
1229 h2c->flags |= H2_CF_MUX_MALLOC;
1230 h2c->flags |= H2_CF_DEM_MROOM;
1231 return 0;
1232 }
1233
1234 memcpy(str,
1235 "\x00\x00\x00" /* length : 0 (no data) */
1236 "\x04" "\x01" /* type : 4, flags : ACK */
1237 "\x00\x00\x00\x00" /* stream ID */, 9);
1238
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001239 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001240 if (unlikely(ret <= 0)) {
1241 if (!ret) {
1242 h2c->flags |= H2_CF_MUX_MFULL;
1243 h2c->flags |= H2_CF_DEM_MROOM;
1244 return 0;
1245 }
1246 else {
1247 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1248 return 0;
1249 }
1250 }
1251 return ret;
1252}
1253
Willy Tarreaucf68c782017-10-10 17:11:41 +02001254/* processes a PING frame and schedules an ACK if needed. The caller must pass
1255 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1256 * missing data. It may return an error in h2c.
1257 */
1258static int h2c_handle_ping(struct h2c *h2c)
1259{
1260 /* frame length must be exactly 8 */
1261 if (h2c->dfl != 8) {
1262 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1263 return 0;
1264 }
1265
1266 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001267 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001268 h2c->st0 = H2_CS_FRAME_A;
1269 return 1;
1270}
1271
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001272/* Try to send a window update for stream id <sid> and value <increment>.
1273 * Returns > 0 on success or zero on missing room or failure. It may return an
1274 * error in h2c.
1275 */
1276static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1277{
1278 struct buffer *res;
1279 char str[13];
1280 int ret = -1;
1281
1282 if (h2c_mux_busy(h2c, NULL)) {
1283 h2c->flags |= H2_CF_DEM_MBUSY;
1284 return 0;
1285 }
1286
Willy Tarreau44e973f2018-03-01 17:49:30 +01001287 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001288 if (!res) {
1289 h2c->flags |= H2_CF_MUX_MALLOC;
1290 h2c->flags |= H2_CF_DEM_MROOM;
1291 return 0;
1292 }
1293
1294 /* length: 4, type: 8, flags: none */
1295 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1296 write_n32(str + 5, sid);
1297 write_n32(str + 9, increment);
1298
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001299 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001300
1301 if (unlikely(ret <= 0)) {
1302 if (!ret) {
1303 h2c->flags |= H2_CF_MUX_MFULL;
1304 h2c->flags |= H2_CF_DEM_MROOM;
1305 return 0;
1306 }
1307 else {
1308 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1309 return 0;
1310 }
1311 }
1312 return ret;
1313}
1314
1315/* try to send pending window update for the connection. It's safe to call it
1316 * with no pending updates. Returns > 0 on success or zero on missing room or
1317 * failure. It may return an error in h2c.
1318 */
1319static int h2c_send_conn_wu(struct h2c *h2c)
1320{
1321 int ret = 1;
1322
1323 if (h2c->rcvd_c <= 0)
1324 return 1;
1325
1326 /* send WU for the connection */
1327 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1328 if (ret > 0)
1329 h2c->rcvd_c = 0;
1330
1331 return ret;
1332}
1333
1334/* try to send pending window update for the current dmux stream. It's safe to
1335 * call it with no pending updates. Returns > 0 on success or zero on missing
1336 * room or failure. It may return an error in h2c.
1337 */
1338static int h2c_send_strm_wu(struct h2c *h2c)
1339{
1340 int ret = 1;
1341
1342 if (h2c->rcvd_s <= 0)
1343 return 1;
1344
1345 /* send WU for the stream */
1346 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1347 if (ret > 0)
1348 h2c->rcvd_s = 0;
1349
1350 return ret;
1351}
1352
Willy Tarreaucf68c782017-10-10 17:11:41 +02001353/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1354 * success, 0 on missing data or one of the h2_status values.
1355 */
1356static int h2c_ack_ping(struct h2c *h2c)
1357{
1358 struct buffer *res;
1359 char str[17];
1360 int ret = -1;
1361
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001362 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001363 return 0;
1364
1365 if (h2c_mux_busy(h2c, NULL)) {
1366 h2c->flags |= H2_CF_DEM_MBUSY;
1367 return 0;
1368 }
1369
Willy Tarreau44e973f2018-03-01 17:49:30 +01001370 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001371 if (!res) {
1372 h2c->flags |= H2_CF_MUX_MALLOC;
1373 h2c->flags |= H2_CF_DEM_MROOM;
1374 return 0;
1375 }
1376
1377 memcpy(str,
1378 "\x00\x00\x08" /* length : 8 (same payload) */
1379 "\x06" "\x01" /* type : 6, flags : ACK */
1380 "\x00\x00\x00\x00" /* stream ID */, 9);
1381
1382 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001383 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001384
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001385 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001386 if (unlikely(ret <= 0)) {
1387 if (!ret) {
1388 h2c->flags |= H2_CF_MUX_MFULL;
1389 h2c->flags |= H2_CF_DEM_MROOM;
1390 return 0;
1391 }
1392 else {
1393 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1394 return 0;
1395 }
1396 }
1397 return ret;
1398}
1399
Willy Tarreau26f95952017-07-27 17:18:30 +02001400/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1401 * Returns > 0 on success or zero on missing data. It may return an error in
1402 * h2c or h2s. Described in RFC7540#6.9.
1403 */
1404static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1405{
1406 int32_t inc;
1407 int error;
1408
1409 if (h2c->dfl != 4) {
1410 error = H2_ERR_FRAME_SIZE_ERROR;
1411 goto conn_err;
1412 }
1413
1414 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001415 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001416 return 0;
1417
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001418 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001419
1420 if (h2c->dsi != 0) {
1421 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001422
1423 /* it's not an error to receive WU on a closed stream */
1424 if (h2s->st == H2_SS_CLOSED)
1425 return 1;
1426
1427 if (!inc) {
1428 error = H2_ERR_PROTOCOL_ERROR;
1429 goto strm_err;
1430 }
1431
1432 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1433 error = H2_ERR_FLOW_CONTROL_ERROR;
1434 goto strm_err;
1435 }
1436
1437 h2s->mws += inc;
1438 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1439 h2s->flags &= ~H2_SF_BLK_SFCTL;
1440 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1441 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1442 /* This stream wanted to send but could not due to its
1443 * own flow control. We can put it back into the send
1444 * list now, it will be handled upon next send() call.
1445 */
1446 LIST_ADDQ(&h2c->send_list, &h2s->list);
1447 }
1448 }
1449 }
1450 else {
1451 /* connection window update */
1452 if (!inc) {
1453 error = H2_ERR_PROTOCOL_ERROR;
1454 goto conn_err;
1455 }
1456
1457 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1458 error = H2_ERR_FLOW_CONTROL_ERROR;
1459 goto conn_err;
1460 }
1461
1462 h2c->mws += inc;
1463 }
1464
1465 return 1;
1466
1467 conn_err:
1468 h2c_error(h2c, error);
1469 return 0;
1470
1471 strm_err:
1472 if (h2s) {
1473 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001474 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001475 }
1476 else
1477 h2c_error(h2c, error);
1478 return 0;
1479}
1480
Willy Tarreaue96b0922017-10-30 00:28:29 +01001481/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1482 * the last ID. Returns > 0 on success or zero on missing data. It may return
1483 * an error in h2c. Described in RFC7540#6.8.
1484 */
1485static int h2c_handle_goaway(struct h2c *h2c)
1486{
1487 int error;
1488 int last;
1489
1490 if (h2c->dsi != 0) {
1491 error = H2_ERR_PROTOCOL_ERROR;
1492 goto conn_err;
1493 }
1494
1495 if (h2c->dfl < 8) {
1496 error = H2_ERR_FRAME_SIZE_ERROR;
1497 goto conn_err;
1498 }
1499
1500 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001501 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001502 return 0;
1503
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001504 last = h2_get_n32(&h2c->dbuf, 0);
1505 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001506 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001507 if (h2c->last_sid < 0)
1508 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001509 return 1;
1510
1511 conn_err:
1512 h2c_error(h2c, error);
1513 return 0;
1514}
1515
Willy Tarreau92153fc2017-12-03 19:46:19 +01001516/* processes a PRIORITY frame, and either skips it or rejects if it is
1517 * invalid. Returns > 0 on success or zero on missing data. It may return
1518 * an error in h2c. Described in RFC7540#6.3.
1519 */
1520static int h2c_handle_priority(struct h2c *h2c)
1521{
1522 int error;
1523
1524 if (h2c->dsi == 0) {
1525 error = H2_ERR_PROTOCOL_ERROR;
1526 goto conn_err;
1527 }
1528
1529 if (h2c->dfl != 5) {
1530 error = H2_ERR_FRAME_SIZE_ERROR;
1531 goto conn_err;
1532 }
1533
1534 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001535 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001536 return 0;
1537
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001538 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001539 /* 7540#5.3 : can't depend on itself */
1540 error = H2_ERR_PROTOCOL_ERROR;
1541 goto conn_err;
1542 }
1543 return 1;
1544
1545 conn_err:
1546 h2c_error(h2c, error);
1547 return 0;
1548}
1549
Willy Tarreaucd234e92017-08-18 10:59:39 +02001550/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1551 * Returns > 0 on success or zero on missing data. It may return an error in
1552 * h2c. Described in RFC7540#6.4.
1553 */
1554static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1555{
1556 int error;
1557
1558 if (h2c->dsi == 0) {
1559 error = H2_ERR_PROTOCOL_ERROR;
1560 goto conn_err;
1561 }
1562
Willy Tarreaucd234e92017-08-18 10:59:39 +02001563 if (h2c->dfl != 4) {
1564 error = H2_ERR_FRAME_SIZE_ERROR;
1565 goto conn_err;
1566 }
1567
1568 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001569 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001570 return 0;
1571
1572 /* late RST, already handled */
1573 if (h2s->st == H2_SS_CLOSED)
1574 return 1;
1575
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001576 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001577 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001578
1579 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001580 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001581 h2s->cs->data_cb->wake(h2s->cs);
1582 }
1583
1584 h2s->flags |= H2_SF_RST_RCVD;
1585 return 1;
1586
1587 conn_err:
1588 h2c_error(h2c, error);
1589 return 0;
1590}
1591
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001592/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1593 * It may return an error in h2c or h2s. The caller must consider that the
1594 * return value is the new h2s in case one was allocated (most common case).
1595 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001596 * errors here are reported as connection errors since it's impossible to
1597 * recover from such errors after the compression context has been altered.
1598 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001599static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001600{
1601 int error;
1602
1603 if (!h2c->dfl) {
1604 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001605 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001606 goto strm_err;
1607 }
1608
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001609 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001610 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001611
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001612 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001613 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001614
Willy Tarreauf2101912018-07-19 10:11:38 +02001615 if (h2c->flags & H2_CF_DEM_TOOMANY)
1616 return 0; // too many cs still present
1617
Willy Tarreau13278b42017-10-13 19:23:14 +02001618 /* now either the frame is complete or the buffer is complete */
1619 if (h2s->st != H2_SS_IDLE) {
1620 /* FIXME: stream already exists, this is only allowed for
1621 * trailers (not supported for now).
1622 */
1623 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001624 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001625 goto conn_err;
1626 }
1627 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1628 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1629 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001630 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001631 goto conn_err;
1632 }
1633
Willy Tarreau22de8d32018-09-05 19:55:58 +02001634 /* Note: we don't emit any other logs below because ff we return
1635 * positively from h2c_stream_new(), the stream will report the error,
1636 * and if we return in error, h2c_stream_new() will emit the error.
1637 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001638 h2s = h2c_stream_new(h2c, h2c->dsi);
1639 if (!h2s) {
1640 error = H2_ERR_INTERNAL_ERROR;
1641 goto conn_err;
1642 }
1643
1644 h2s->st = H2_SS_OPEN;
1645 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1646 h2s->st = H2_SS_HREM;
1647 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001648 /* note: cs cannot be null for now (just created above) */
1649 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001650 }
1651
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001652 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001653 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001654
Willy Tarreau8f650c32017-11-21 19:36:21 +01001655 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001656 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001657
Willy Tarreau721c9742017-11-07 11:05:42 +01001658 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001659 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001660 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001661 }
1662 else {
1663 /* update the max stream ID if the request is being processed */
1664 if (h2s->id > h2c->max_id)
1665 h2c->max_id = h2s->id;
1666 }
1667
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001668 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001669
1670 conn_err:
1671 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001672 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001673
1674 strm_err:
1675 if (h2s) {
1676 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001677 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001678 }
1679 else
1680 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001681 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001682}
1683
Willy Tarreau454f9052017-10-26 19:40:35 +02001684/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1685 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1686 */
1687static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1688{
1689 int error;
1690
1691 /* note that empty DATA frames are perfectly valid and sometimes used
1692 * to signal an end of stream (with the ES flag).
1693 */
1694
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001695 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001696 return 0; // empty buffer
1697
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001698 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001699 return 0; // incomplete frame
1700
1701 /* now either the frame is complete or the buffer is complete */
1702
1703 if (!h2c->dsi) {
1704 /* RFC7540#6.1 */
1705 error = H2_ERR_PROTOCOL_ERROR;
1706 goto conn_err;
1707 }
1708
1709 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1710 /* RFC7540#6.1 */
1711 error = H2_ERR_STREAM_CLOSED;
1712 goto strm_err;
1713 }
1714
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001715 if (!h2_frt_transfer_data(h2s))
1716 return 0;
1717
Willy Tarreau454f9052017-10-26 19:40:35 +02001718 /* call the upper layers to process the frame, then let the upper layer
1719 * notify the stream about any change.
1720 */
1721 if (!h2s->cs) {
1722 error = H2_ERR_STREAM_CLOSED;
1723 goto strm_err;
1724 }
1725
Willy Tarreau8f650c32017-11-21 19:36:21 +01001726 if (h2c->st0 >= H2_CS_ERROR)
1727 return 0;
1728
Willy Tarreau721c9742017-11-07 11:05:42 +01001729 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001730 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001731 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001732 }
1733
1734 /* check for completion : the callee will change this to FRAME_A or
1735 * FRAME_H once done.
1736 */
1737 if (h2c->st0 == H2_CS_FRAME_P)
1738 return 0;
1739
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001740
1741 /* last frame */
1742 if (h2c->dff & H2_F_DATA_END_STREAM) {
1743 h2s->st = H2_SS_HREM;
1744 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001745 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001746 }
1747
Willy Tarreau454f9052017-10-26 19:40:35 +02001748 return 1;
1749
1750 conn_err:
1751 h2c_error(h2c, error);
1752 return 0;
1753
1754 strm_err:
1755 if (h2s) {
1756 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001757 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001758 }
1759 else
1760 h2c_error(h2c, error);
1761 return 0;
1762}
1763
Willy Tarreaubc933932017-10-09 16:21:43 +02001764/* process Rx frames to be demultiplexed */
1765static void h2_process_demux(struct h2c *h2c)
1766{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001767 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001768
Willy Tarreau081d4722017-05-16 21:51:05 +02001769 if (h2c->st0 >= H2_CS_ERROR)
1770 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001771
1772 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1773 if (h2c->st0 == H2_CS_PREFACE) {
1774 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1775 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001776 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001777 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001778 sess_log(h2c->conn->owner);
1779 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001780 goto fail;
1781 }
1782
1783 h2c->max_id = 0;
1784 h2c->st0 = H2_CS_SETTINGS1;
1785 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001786
1787 if (h2c->st0 == H2_CS_SETTINGS1) {
1788 struct h2_fh hdr;
1789
1790 /* ensure that what is pending is a valid SETTINGS frame
1791 * without an ACK.
1792 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001793 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001794 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001795 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001796 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001797 sess_log(h2c->conn->owner);
1798 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001799 goto fail;
1800 }
1801
1802 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1803 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1804 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1805 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001806 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001807 goto fail;
1808 }
1809
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001810 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001811 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1812 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1813 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001814 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001815 goto fail;
1816 }
1817
1818 /* that's OK, switch to FRAME_P to process it */
1819 h2c->dfl = hdr.len;
1820 h2c->dsi = hdr.sid;
1821 h2c->dft = hdr.ft;
1822 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001823 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001824 h2c->st0 = H2_CS_FRAME_P;
1825 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001826 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001827
1828 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001829 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001830 int ret = 0;
1831
1832 if (h2c->st0 >= H2_CS_ERROR)
1833 break;
1834
1835 if (h2c->st0 == H2_CS_FRAME_H) {
1836 struct h2_fh hdr;
1837
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001838 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001839 break;
1840
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001841 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001842 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1843 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001844 if (!h2c->nb_streams) {
1845 /* only log if no other stream can report the error */
1846 sess_log(h2c->conn->owner);
1847 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001848 break;
1849 }
1850
1851 h2c->dfl = hdr.len;
1852 h2c->dsi = hdr.sid;
1853 h2c->dft = hdr.ft;
1854 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001855 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001856 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001857 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001858 }
1859
1860 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001861 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1862
Olivier Houchard638b7992018-08-16 15:41:52 +02001863 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001864 /* we may have to signal the upper layers */
1865 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001866 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1867 /* cs has just been destroyed, we have to kill h2s. */
1868 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1869 goto strm_err;
1870 }
1871
1872 if (h2c->st0 >= H2_CS_ERROR)
1873 goto strm_err;
1874
1875 if (h2s->st >= H2_SS_ERROR) {
1876 /* stream error : send RST_STREAM */
1877 h2c->st0 = H2_CS_FRAME_E;
1878 }
1879 }
1880 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001881
Willy Tarreaud7901432017-12-29 11:34:40 +01001882 if (h2c->st0 == H2_CS_FRAME_E)
1883 goto strm_err;
1884
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001885 if (h2s->st == H2_SS_IDLE &&
1886 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1887 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1888 * this state MUST be treated as a connection error
1889 */
1890 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1891 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001892 if (!h2c->nb_streams) {
1893 /* only log if no other stream can report the error */
1894 sess_log(h2c->conn->owner);
1895 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001896 break;
1897 }
1898
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001899 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1900 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1901 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1902 * this state MUST be treated as a stream error
1903 */
1904 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1905 goto strm_err;
1906 }
1907
Willy Tarreauab837502017-12-27 15:07:30 +01001908 /* Below the management of frames received in closed state is a
1909 * bit hackish because the spec makes strong differences between
1910 * streams closed by receiving RST, sending RST, and seeing ES
1911 * in both directions. In addition to this, the creation of a
1912 * new stream reusing the identifier of a closed one will be
1913 * detected here. Given that we cannot keep track of all closed
1914 * streams forever, we consider that unknown closed streams were
1915 * closed on RST received, which allows us to respond with an
1916 * RST without breaking the connection (eg: to abort a transfer).
1917 * Some frames have to be silently ignored as well.
1918 */
1919 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1920 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1921 /* #5.1.1: The identifier of a newly
1922 * established stream MUST be numerically
1923 * greater than all streams that the initiating
1924 * endpoint has opened or reserved. This
1925 * governs streams that are opened using a
1926 * HEADERS frame and streams that are reserved
1927 * using PUSH_PROMISE. An endpoint that
1928 * receives an unexpected stream identifier
1929 * MUST respond with a connection error.
1930 */
1931 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1932 goto strm_err;
1933 }
1934
1935 if (h2s->flags & H2_SF_RST_RCVD) {
1936 /* RFC7540#5.1:closed: an endpoint that
1937 * receives any frame other than PRIORITY after
1938 * receiving a RST_STREAM MUST treat that as a
1939 * stream error of type STREAM_CLOSED.
1940 *
1941 * Note that old streams fall into this category
1942 * and will lead to an RST being sent.
1943 */
1944 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1945 h2c->st0 = H2_CS_FRAME_E;
1946 goto strm_err;
1947 }
1948
1949 /* RFC7540#5.1:closed: if this state is reached as a
1950 * result of sending a RST_STREAM frame, the peer that
1951 * receives the RST_STREAM might have already sent
1952 * frames on the stream that cannot be withdrawn. An
1953 * endpoint MUST ignore frames that it receives on
1954 * closed streams after it has sent a RST_STREAM
1955 * frame. An endpoint MAY choose to limit the period
1956 * over which it ignores frames and treat frames that
1957 * arrive after this time as being in error.
1958 */
1959 if (!(h2s->flags & H2_SF_RST_SENT)) {
1960 /* RFC7540#5.1:closed: any frame other than
1961 * PRIO/WU/RST in this state MUST be treated as
1962 * a connection error
1963 */
1964 if (h2c->dft != H2_FT_RST_STREAM &&
1965 h2c->dft != H2_FT_PRIORITY &&
1966 h2c->dft != H2_FT_WINDOW_UPDATE) {
1967 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1968 goto strm_err;
1969 }
1970 }
1971 }
1972
Willy Tarreauc0da1962017-10-30 18:38:00 +01001973#if 0
1974 // problem below: it is not possible to completely ignore such
1975 // streams as we need to maintain the compression state as well
1976 // and for this we need to completely process these frames (eg:
1977 // HEADERS frames) as well as counting DATA frames to emit
1978 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1979 // This is a typical case of layer violation where the
1980 // transported contents are critical to the connection's
1981 // validity and must be ignored at the same time :-(
1982
1983 /* graceful shutdown, ignore streams whose ID is higher than
1984 * the one advertised in GOAWAY. RFC7540#6.8.
1985 */
1986 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001987 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1988 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001989 h2c->dfl -= ret;
1990 ret = h2c->dfl == 0;
1991 goto strm_err;
1992 }
1993#endif
1994
Willy Tarreau7e98c052017-10-10 15:56:59 +02001995 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001996 case H2_FT_SETTINGS:
1997 if (h2c->st0 == H2_CS_FRAME_P)
1998 ret = h2c_handle_settings(h2c);
1999
2000 if (h2c->st0 == H2_CS_FRAME_A)
2001 ret = h2c_ack_settings(h2c);
2002 break;
2003
Willy Tarreaucf68c782017-10-10 17:11:41 +02002004 case H2_FT_PING:
2005 if (h2c->st0 == H2_CS_FRAME_P)
2006 ret = h2c_handle_ping(h2c);
2007
2008 if (h2c->st0 == H2_CS_FRAME_A)
2009 ret = h2c_ack_ping(h2c);
2010 break;
2011
Willy Tarreau26f95952017-07-27 17:18:30 +02002012 case H2_FT_WINDOW_UPDATE:
2013 if (h2c->st0 == H2_CS_FRAME_P)
2014 ret = h2c_handle_window_update(h2c, h2s);
2015 break;
2016
Willy Tarreau61290ec2017-10-17 08:19:21 +02002017 case H2_FT_CONTINUATION:
2018 /* we currently don't support CONTINUATION frames since
2019 * we have nowhere to store the partial HEADERS frame.
2020 * Let's abort the stream on an INTERNAL_ERROR here.
2021 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002022 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002023 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002024 h2c->st0 = H2_CS_FRAME_E;
2025 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002026 break;
2027
Willy Tarreau13278b42017-10-13 19:23:14 +02002028 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002029 if (h2c->st0 == H2_CS_FRAME_P) {
2030 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2031 if (tmp_h2s) {
2032 h2s = tmp_h2s;
2033 ret = 1;
2034 }
2035 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002036 break;
2037
Willy Tarreau454f9052017-10-26 19:40:35 +02002038 case H2_FT_DATA:
2039 if (h2c->st0 == H2_CS_FRAME_P)
2040 ret = h2c_frt_handle_data(h2c, h2s);
2041
2042 if (h2c->st0 == H2_CS_FRAME_A)
2043 ret = h2c_send_strm_wu(h2c);
2044 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002045
Willy Tarreau92153fc2017-12-03 19:46:19 +01002046 case H2_FT_PRIORITY:
2047 if (h2c->st0 == H2_CS_FRAME_P)
2048 ret = h2c_handle_priority(h2c);
2049 break;
2050
Willy Tarreaucd234e92017-08-18 10:59:39 +02002051 case H2_FT_RST_STREAM:
2052 if (h2c->st0 == H2_CS_FRAME_P)
2053 ret = h2c_handle_rst_stream(h2c, h2s);
2054 break;
2055
Willy Tarreaue96b0922017-10-30 00:28:29 +01002056 case H2_FT_GOAWAY:
2057 if (h2c->st0 == H2_CS_FRAME_P)
2058 ret = h2c_handle_goaway(h2c);
2059 break;
2060
Willy Tarreau1c661982017-10-30 13:52:01 +01002061 case H2_FT_PUSH_PROMISE:
2062 /* not permitted here, RFC7540#5.1 */
2063 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002064 if (!h2c->nb_streams) {
2065 /* only log if no other stream can report the error */
2066 sess_log(h2c->conn->owner);
2067 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002068 break;
2069
2070 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002071 default:
2072 /* drop frames that we ignore. They may be larger than
2073 * the buffer so we drain all of their contents until
2074 * we reach the end.
2075 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002076 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2077 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002078 h2c->dfl -= ret;
2079 ret = h2c->dfl == 0;
2080 }
2081
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002082 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002083 /* We may have to send an RST if not done yet */
2084 if (h2s->st == H2_SS_ERROR)
2085 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002086
Willy Tarreaua20a5192017-12-27 11:02:06 +01002087 if (h2c->st0 == H2_CS_FRAME_E)
2088 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002089
Willy Tarreau7e98c052017-10-10 15:56:59 +02002090 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 if (ret <= 0) {
2092 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002093 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002094 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002095
2096 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002097 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002098 h2c->st0 = H2_CS_FRAME_H;
2099 }
2100 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002101
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002102 if (h2c->rcvd_c > 0 &&
2103 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2104 h2c_send_conn_wu(h2c);
2105
Willy Tarreau52eed752017-09-22 15:05:09 +02002106 fail:
2107 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002108 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002109 /* we may have to signal the upper layers */
2110 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002111 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2112 /* cs has just been destroyed, we have to kill h2s. */
2113 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2114 h2c_send_rst_stream(h2c, h2s);
2115 }
2116 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002117 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002118}
2119
2120/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2121 * the end.
2122 */
2123static int h2_process_mux(struct h2c *h2c)
2124{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002125 struct h2s *h2s, *h2s_back;
2126
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002127 /* start by sending possibly pending window updates */
2128 if (h2c->rcvd_c > 0 &&
2129 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2130 h2c_send_conn_wu(h2c) < 0)
2131 goto fail;
2132
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002133 /* First we always process the flow control list because the streams
2134 * waiting there were already elected for immediate emission but were
2135 * blocked just on this.
2136 */
2137
2138 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2139 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2140 h2c->st0 >= H2_CS_ERROR)
2141 break;
2142
2143 /* In theory it's possible that h2s->cs == NULL here :
2144 * - client sends crap that causes a parse error
2145 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2146 * - RST_STREAM cannot be emitted because mux is busy/full
2147 * - stream gets notified, detaches and quits
2148 * - mux buffer gets ready and wakes pending streams up
2149 * - bam!
2150 */
2151 h2s->flags &= ~H2_SF_BLK_ANY;
2152
2153 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002154 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002155 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002156 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002157 }
2158
2159 /* depending on callee's blocking reasons, we may queue in send
2160 * list or completely dequeue.
2161 */
2162 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2163 if (h2s->flags & H2_SF_BLK_ANY) {
2164 LIST_DEL(&h2s->list);
2165 LIST_ADDQ(&h2c->send_list, &h2s->list);
2166 }
2167 else {
2168 LIST_DEL(&h2s->list);
2169 LIST_INIT(&h2s->list);
2170 if (h2s->cs)
2171 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002172 else {
2173 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002174 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002175 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002176 }
2177 }
2178 }
2179
2180 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2181 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2182 break;
2183
2184 /* In theory it's possible that h2s->cs == NULL here :
2185 * - client sends crap that causes a parse error
2186 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2187 * - RST_STREAM cannot be emitted because mux is busy/full
2188 * - stream gets notified, detaches and quits
2189 * - mux buffer gets ready and wakes pending streams up
2190 * - bam!
2191 */
2192 h2s->flags &= ~H2_SF_BLK_ANY;
2193
2194 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002195 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002196 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002197 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002198 }
2199 /* depending on callee's blocking reasons, we may queue in fctl
2200 * list or completely dequeue.
2201 */
2202 if (h2s->flags & H2_SF_BLK_MFCTL) {
2203 /* stream hit the connection's flow control */
2204 LIST_DEL(&h2s->list);
2205 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2206 }
2207 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2208 LIST_DEL(&h2s->list);
2209 LIST_INIT(&h2s->list);
2210 if (h2s->cs)
2211 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002212 else {
2213 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002214 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002215 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002216 }
2217 }
2218
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002219 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002220 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002221 if (h2c->st0 == H2_CS_ERROR) {
2222 if (h2c->max_id >= 0) {
2223 h2c_send_goaway_error(h2c, NULL);
2224 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2225 return 0;
2226 }
2227
2228 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2229 }
2230 return 1;
2231 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002232 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002233}
2234
Willy Tarreau62f52692017-10-08 23:01:42 +02002235
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002236/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002237static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002238{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002239 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002240 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241 int max;
2242
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002243 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002244 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002245
Willy Tarreau315d8072017-12-10 22:17:57 +01002246 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002247 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002248
Willy Tarreau44e973f2018-03-01 17:49:30 +01002249 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002250 if (!buf) {
2251 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002252 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002253 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002254
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002255 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002256 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002257 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002258
Olivier Houcharda1411e62018-08-17 18:42:48 +02002259 if (h2_recv_allowed(h2c))
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002260 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houcharda1411e62018-08-17 18:42:48 +02002261 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002262 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002263 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002264 }
2265
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002266 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002267 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002268 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002269}
2270
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002271/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002272static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002273{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002274 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002275 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002276 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002277
2278 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002279 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002280
2281 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2282 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002283 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284 }
2285
Willy Tarreaubc933932017-10-09 16:21:43 +02002286 /* This loop is quite simple : it tries to fill as much as it can from
2287 * pending streams into the existing buffer until it's reportedly full
2288 * or the end of send requests is reached. Then it tries to send this
2289 * buffer's contents out, marks it not full if at least one byte could
2290 * be sent, and tries again.
2291 *
2292 * The snd_buf() function normally takes a "flags" argument which may
2293 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2294 * data immediately comes and CO_SFL_STREAMER to indicate that the
2295 * connection is streaming lots of data (used to increase TLS record
2296 * size at the expense of latency). The former can be sent any time
2297 * there's a buffer full flag, as it indicates at least one stream
2298 * attempted to send and failed so there are pending data. An
2299 * alternative would be to set it as long as there's an active stream
2300 * but that would be problematic for ACKs until we have an absolute
2301 * guarantee that all waiters have at least one byte to send. The
2302 * latter should possibly not be set for now.
2303 */
2304
2305 done = 0;
2306 while (!done) {
2307 unsigned int flags = 0;
2308
2309 /* fill as much as we can into the current buffer */
2310 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2311 done = h2_process_mux(h2c);
2312
2313 if (conn->flags & CO_FL_ERROR)
2314 break;
2315
2316 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2317 flags |= CO_SFL_MSG_MORE;
2318
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002319 if (b_data(&h2c->mbuf)) {
2320 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002321 if (!ret)
2322 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002323 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002324 b_del(&h2c->mbuf, ret);
2325 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002326 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002327
2328 /* wrote at least one byte, the buffer is not full anymore */
2329 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2330 }
2331
Willy Tarreaua2af5122017-10-09 11:56:46 +02002332 if (conn->flags & CO_FL_SOCK_WR_SH) {
2333 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002334 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002335 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002336 /* We're not full anymore, so we can wake any task that are waiting
2337 * for us.
2338 */
2339 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2340 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2341 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2342 struct wait_list *, list);
2343 LIST_DEL(&sw->list);
2344 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002345 sw->wait_reason &= ~SUB_CAN_SEND;
2346 tasklet_wakeup(sw->task);
2347 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002348 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002349 /* We're done, no more to send */
2350 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002351 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002352schedule:
2353 if (LIST_ISEMPTY(&h2c->wait_list.list))
2354 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002355 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002356}
2357
2358static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2359{
2360 struct h2c *h2c = ctx;
2361
2362 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
2363 h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002364 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
2365 h2_recv(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002366 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002367}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002368
Willy Tarreau62f52692017-10-08 23:01:42 +02002369/* callback called on any event by the connection handler.
2370 * It applies changes and returns zero, or < 0 if it wants immediate
2371 * destruction of the connection (which normally doesn not happen in h2).
2372 */
2373static int h2_wake(struct connection *conn)
2374{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002375 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002376 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002377
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002378 h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002379 if (h2_recv_allowed(h2c))
2380 h2_recv(h2c);
2381
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002382 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002383 h2_process_demux(h2c);
2384
2385 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002386 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002387
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002388 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002389 h2c->flags &= ~H2_CF_DEM_DFULL;
2390 }
2391
Willy Tarreau8ec14062017-12-30 18:08:13 +01002392 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2393 /* frontend is stopping, reload likely in progress, let's try
2394 * to announce a graceful shutdown if not yet done. We don't
2395 * care if it fails, it will be tried again later.
2396 */
2397 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2398 if (h2c->last_sid < 0)
2399 h2c->last_sid = (1U << 31) - 1;
2400 h2c_send_goaway_error(h2c, NULL);
2401 }
2402 }
2403
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002404 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002405 * If we received early data, and the handshake is done, wake
2406 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002407 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002408 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2409 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2410 struct eb32_node *node;
2411 struct h2s *h2s;
2412
2413 h2c->flags |= H2_CF_WAIT_FOR_HS;
2414 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2415
2416 while (node) {
2417 h2s = container_of(node, struct h2s, by_id);
2418 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2419 h2s->cs->data_cb->wake(h2s->cs);
2420 node = eb32_next(node);
2421 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002422 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002423
Willy Tarreau26bd7612017-10-09 16:47:04 +02002424 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002425 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2426 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2427 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002428 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002429
2430 if (eb_is_empty(&h2c->streams_by_id)) {
2431 /* no more stream, kill the connection now */
2432 h2_release(conn);
2433 return -1;
2434 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002435 }
2436
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002437 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002438 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439
2440 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002441 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442 __conn_xprt_stop_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002443 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002444 __conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002445 h2_recv(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002446 }
2447
2448 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002449 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002450 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002451 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002452 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002453 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2454 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002455 __conn_xprt_want_send(conn);
2456 }
2457 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002458 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002459 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002460 }
2461
Willy Tarreau3f133572017-10-31 19:21:06 +01002462 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002463 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002464 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002465 task_queue(h2c->task);
2466 }
2467 else
2468 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002469 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002470
Willy Tarreau62f52692017-10-08 23:01:42 +02002471 return 0;
2472}
2473
Willy Tarreauea392822017-10-31 10:02:25 +01002474/* Connection timeout management. The principle is that if there's no receipt
2475 * nor sending for a certain amount of time, the connection is closed. If the
2476 * MUX buffer still has lying data or is not allocatable, the connection is
2477 * immediately killed. If it's allocatable and empty, we attempt to send a
2478 * GOAWAY frame.
2479 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002480static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002481{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002482 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002483 int expired = tick_is_expired(t->expire, now_ms);
2484
Willy Tarreau0975f112018-03-29 15:22:59 +02002485 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002486 return t;
2487
Willy Tarreau0975f112018-03-29 15:22:59 +02002488 task_delete(t);
2489 task_free(t);
2490
2491 if (!h2c) {
2492 /* resources were already deleted */
2493 return NULL;
2494 }
2495
2496 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002497 h2c_error(h2c, H2_ERR_NO_ERROR);
2498 h2_wake_some_streams(h2c, 0, 0);
2499
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002500 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002501 /* don't even try to send a GOAWAY, the buffer is stuck */
2502 h2c->flags |= H2_CF_GOAWAY_FAILED;
2503 }
2504
2505 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002506 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002507 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2508 h2c->flags |= H2_CF_GOAWAY_FAILED;
2509
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002510 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2511 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002512 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002513 b_del(&h2c->mbuf, ret);
2514 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002515 }
2516 }
Willy Tarreauea392822017-10-31 10:02:25 +01002517
Willy Tarreau0975f112018-03-29 15:22:59 +02002518 /* either we can release everything now or it will be done later once
2519 * the last stream closes.
2520 */
2521 if (eb_is_empty(&h2c->streams_by_id))
2522 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002523
Willy Tarreauea392822017-10-31 10:02:25 +01002524 return NULL;
2525}
2526
2527
Willy Tarreau62f52692017-10-08 23:01:42 +02002528/*******************************************/
2529/* functions below are used by the streams */
2530/*******************************************/
2531
2532/*
2533 * Attach a new stream to a connection
2534 * (Used for outgoing connections)
2535 */
2536static struct conn_stream *h2_attach(struct connection *conn)
2537{
2538 return NULL;
2539}
2540
2541/* callback used to update the mux's polling flags after changing a cs' status.
2542 * The caller (cs_update_mux_polling) will take care of propagating any changes
2543 * to the transport layer.
2544 */
2545static void h2_update_poll(struct conn_stream *cs)
2546{
Willy Tarreau1d393222017-10-17 10:26:19 +02002547 struct h2s *h2s = cs->ctx;
2548
2549 if (!h2s)
2550 return;
2551
Willy Tarreaud7739c82017-10-30 15:38:23 +01002552 /* we may unblock a blocked read */
2553
Willy Tarreau315d8072017-12-10 22:17:57 +01002554 if (cs->flags & CS_FL_DATA_RD_ENA) {
2555 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002556 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002557 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002558 conn_xprt_want_recv(cs->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002559 h2_recv(h2s->h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002560 conn_xprt_want_send(cs->conn);
2561 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002562 }
2563
Willy Tarreau1d393222017-10-17 10:26:19 +02002564 /* Note: the stream and stream-int code doesn't allow us to perform a
2565 * synchronous send() here unfortunately, because this code is called
2566 * as si_update() from the process_stream() context. This means that
2567 * we have to queue the current cs and defer its processing after the
2568 * connection's cs list is processed anyway.
2569 */
2570
2571 if (cs->flags & CS_FL_DATA_WR_ENA) {
2572 if (LIST_ISEMPTY(&h2s->list)) {
2573 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002574 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002575 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2576 conn_xprt_want_send(cs->conn);
2577 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2578 }
2579 }
2580 else if (!LIST_ISEMPTY(&h2s->list)) {
2581 LIST_DEL(&h2s->list);
2582 LIST_INIT(&h2s->list);
2583 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2584 }
2585
2586 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002587 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002588 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002589}
2590
2591/*
2592 * Detach the stream from the connection and possibly release the connection.
2593 */
2594static void h2_detach(struct conn_stream *cs)
2595{
Willy Tarreau60935142017-10-16 18:11:19 +02002596 struct h2s *h2s = cs->ctx;
2597 struct h2c *h2c;
2598
2599 cs->ctx = NULL;
2600 if (!h2s)
2601 return;
2602
2603 h2c = h2s->h2c;
2604 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002605 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002606 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2607 !h2_has_too_many_cs(h2c)) {
2608 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2609 if (h2_recv_allowed(h2c)) {
2610 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002611 h2_recv(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002612 conn_xprt_want_send(h2c->conn);
2613 }
2614 }
Willy Tarreau60935142017-10-16 18:11:19 +02002615
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002616 /* this stream may be blocked waiting for some data to leave (possibly
2617 * an ES or RST frame), so orphan it in this case.
2618 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002619 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002620 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002621 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002622 return;
2623
Willy Tarreau45f752e2017-10-30 15:44:59 +01002624 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2625 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2626 /* unblock the connection if it was blocked on this
2627 * stream.
2628 */
2629 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2630 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2631 conn_xprt_want_recv(cs->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002632 h2_recv(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002633 conn_xprt_want_send(cs->conn);
2634 }
2635
Willy Tarreau71049cc2018-03-28 13:56:39 +02002636 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002637
Willy Tarreaue323f342018-03-28 13:51:45 +02002638 /* We don't want to close right now unless we're removing the
2639 * last stream, and either the connection is in error, or it
2640 * reached the ID already specified in a GOAWAY frame received
2641 * or sent (as seen by last_sid >= 0).
2642 */
2643 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2644 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002645 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002646 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002647 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002648 (conn_xprt_read0_pending(h2c->conn) ||
2649 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2650 /* no more stream will come, kill it now */
2651 h2_release(h2c->conn);
2652 }
2653 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002654 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002655 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2656 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002657 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002658 else
2659 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002660 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002661}
2662
2663static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2664{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002665 struct h2s *h2s = cs->ctx;
2666
2667 if (!mode)
2668 return;
2669
Willy Tarreau721c9742017-11-07 11:05:42 +01002670 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002671 return;
2672
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002673 /* if no outgoing data was seen on this stream, it means it was
2674 * closed with a "tcp-request content" rule that is normally
2675 * used to kill the connection ASAP (eg: limit abuse). In this
2676 * case we send a goaway to close the connection.
2677 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002678 if (!(h2s->flags & H2_SF_RST_SENT) &&
2679 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002680 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002681
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002682 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2683 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2684 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002685 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002686
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002687 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002688 conn_xprt_want_send(cs->conn);
2689
Willy Tarreau00dd0782018-03-01 16:31:34 +01002690 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002691
2692 add_to_list:
2693 if (LIST_ISEMPTY(&h2s->list)) {
2694 if (h2s->flags & H2_SF_BLK_MFCTL)
2695 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2696 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2697 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2698 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002699}
2700
2701static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2702{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002703 struct h2s *h2s = cs->ctx;
2704
Willy Tarreau721c9742017-11-07 11:05:42 +01002705 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002706 return;
2707
Willy Tarreau67434202017-11-06 20:20:51 +01002708 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002709 /* we can cleanly close using an empty data frame only after headers */
2710
2711 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2712 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002713 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002714
2715 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002716 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002717 else
2718 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002719 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002720 /* if no outgoing data was seen on this stream, it means it was
2721 * closed with a "tcp-request content" rule that is normally
2722 * used to kill the connection ASAP (eg: limit abuse). In this
2723 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002724 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002725 if (!(h2s->flags & H2_SF_RST_SENT) &&
2726 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002727 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002728
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002729 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2730 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002731 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002732 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002733
Willy Tarreau00dd0782018-03-01 16:31:34 +01002734 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002735 }
2736
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002737 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002738 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002739
2740 add_to_list:
2741 if (LIST_ISEMPTY(&h2s->list)) {
2742 if (h2s->flags & H2_SF_BLK_MFCTL)
2743 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2744 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2745 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2746 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002747}
2748
Willy Tarreau13278b42017-10-13 19:23:14 +02002749/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2750 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2751 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002752 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002753 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002754static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002755{
2756 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002757 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002758 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002759 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002760 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002761 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002762 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002763 int flen = h2c->dfl;
2764 int outlen = 0;
2765 int wrap;
2766 int try;
2767
2768 if (!h2c->dfl) {
2769 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002770 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002771 return 0;
2772 }
2773
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002774 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002775 return 0; // incomplete input frame
2776
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002778 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002779 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002780 copy = alloc_trash_chunk();
2781 if (!copy) {
2782 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2783 goto fail;
2784 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002785 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2786 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2787 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002788 }
2789
2790 /* The padlen is the first byte before data, and the padding appears
2791 * after data. padlen+data+padding are included in flen.
2792 */
2793 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002794 h2c->dpl = *hdrs;
2795 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002796 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2797 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002798 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002799 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002800 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002801 hdrs += 1; // skip Pad Length
2802 }
2803
2804 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2805 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002806 if (read_n32(hdrs) == h2s->id) {
2807 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2808 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002809 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002810 }
2811
Willy Tarreau13278b42017-10-13 19:23:14 +02002812 hdrs += 5; // stream dep = 4, weight = 1
2813 flen -= 5;
2814 }
2815
2816 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2817 * don't support this for now and can't even decompress so we have to
2818 * break the connection.
2819 */
2820 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2821 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002822 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002823 }
2824
Olivier Houchard638b7992018-08-16 15:41:52 +02002825 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002826 if (!csbuf) {
2827 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002828 goto fail;
2829 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002830
Willy Tarreau937f7602018-02-26 15:22:17 +01002831 /* we can't retry a failed decompression operation so we must be very
2832 * careful not to take any risks. In practice the output buffer is
2833 * always empty except maybe for trailers, in which case we simply have
2834 * to wait for the upper layer to finish consuming what is available.
2835 */
2836 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002837 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002838
Willy Tarreau937f7602018-02-26 15:22:17 +01002839 csbuf->head = 0;
2840 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002841
2842 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2843 sizeof(list)/sizeof(list[0]), tmp);
2844 if (outlen < 0) {
2845 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2846 goto fail;
2847 }
2848
2849 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002850 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002851 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002852
2853 if (outlen < 0) {
2854 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2855 goto fail;
2856 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002857
Willy Tarreau174b06a2018-04-25 18:13:58 +02002858 if (msgf & H2_MSGF_BODY) {
2859 /* a payload is present */
2860 if (msgf & H2_MSGF_BODY_CL)
2861 h2s->flags |= H2_SF_DATA_CLEN;
2862 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2863 h2s->flags |= H2_SF_DATA_CHNK;
2864 }
2865
Willy Tarreau13278b42017-10-13 19:23:14 +02002866 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002867 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002868 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002869 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002870
Willy Tarreau39d68502018-03-02 12:26:37 +01002871 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002872 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002873 h2s->cs->flags |= CS_FL_REOS;
2874 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002875
Willy Tarreau68dd9852017-07-03 14:44:26 +02002876 leave:
2877 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002878 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002879 fail:
2880 outlen = 0;
2881 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002882}
2883
Willy Tarreau454f9052017-10-26 19:40:35 +02002884/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2885 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2886 * in use, a new chunk is emitted for each frame. This is supposed to fit
2887 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2888 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2889 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2890 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002891 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2892 * checked to know if some data remain pending (an empty DATA frame can return
2893 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2894 * connection errors in h2c->errcode. The caller must already have checked the
2895 * frame header and ensured that the frame was complete or the buffer full. It
2896 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002897 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002898static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002899{
2900 struct h2c *h2c = h2s->h2c;
2901 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002902 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002903 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002904 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002905
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002906 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002907
2908 /* The padlen is the first byte before data, and the padding appears
2909 * after data. padlen+data+padding are included in flen.
2910 */
Willy Tarreau79127812017-12-03 21:06:59 +01002911 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002912 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002913 return 0;
2914
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002916 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002917 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2918 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002919 return 0;
2920 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002921
2922 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002923 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002924 h2c->dfl--;
2925 h2c->rcvd_c++; h2c->rcvd_s++;
2926 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002927 }
2928
Olivier Houchard638b7992018-08-16 15:41:52 +02002929 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002930 if (!csbuf) {
2931 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002932 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002933 }
2934
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002935 flen = h2c->dfl - h2c->dpl;
2936 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002937 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002938
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 if (flen > b_data(&h2c->dbuf)) {
2940 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002941 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002942 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002943 }
2944
2945 if (unlikely(b_space_wraps(csbuf))) {
2946 /* it doesn't fit and the buffer is fragmented,
2947 * so let's defragment it and try again.
2948 */
2949 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002950 }
2951
Willy Tarreaueba10f22018-04-25 20:44:22 +02002952 /* chunked-encoding requires more room */
2953 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002954 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002955 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2956 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2957 (chklen < 1048576) ? 4 : 8;
2958 chklen += 4; // CRLF, CRLF
2959 }
2960
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002961 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002962 if (flen + chklen > b_room(csbuf)) {
2963 if (chklen >= b_room(csbuf)) {
2964 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002965 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002966 }
2967 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002968 }
2969
2970 if (h2s->flags & H2_SF_DATA_CHNK) {
2971 /* emit the chunk size */
2972 unsigned int chksz = flen;
2973 char str[10];
2974 char *beg;
2975
2976 beg = str + sizeof(str);
2977 *--beg = '\n';
2978 *--beg = '\r';
2979 do {
2980 *--beg = hextab[chksz & 0xF];
2981 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002982 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002983 }
2984
Willy Tarreau454f9052017-10-26 19:40:35 +02002985 /* Block1 is the length of the first block before the buffer wraps,
2986 * block2 is the optional second block to reach the end of the frame.
2987 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989 if (block1 > flen)
2990 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002991 block2 = flen - block1;
2992
2993 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002994 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002995
2996 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002997 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002998
Willy Tarreaueba10f22018-04-25 20:44:22 +02002999 if (h2s->flags & H2_SF_DATA_CHNK) {
3000 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003001 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003002 }
3003
Willy Tarreau454f9052017-10-26 19:40:35 +02003004 /* now mark the input data as consumed (will be deleted from the buffer
3005 * by the caller when seeing FRAME_A after sending the window update).
3006 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003007 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003008 h2c->dfl -= flen;
3009 h2c->rcvd_c += flen;
3010 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3011
3012 if (h2c->dfl > h2c->dpl) {
3013 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003014 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003015 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003016 }
3017
Willy Tarreau4a28da12018-01-04 14:41:00 +01003018 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003019 /* here we're done with the frame, all the payload (except padding) was
3020 * transferred.
3021 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003022
3023 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3024 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003025 if (b_room(csbuf) < 5) {
3026 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003027 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003028 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003029 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003030 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003031 }
3032
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003033 h2c->rcvd_c += h2c->dpl;
3034 h2c->rcvd_s += h2c->dpl;
3035 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003036 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3037
Willy Tarreau39d68502018-03-02 12:26:37 +01003038 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003039 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003040 h2s->cs->flags |= CS_FL_REOS;
3041 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003042
Willy Tarreau454b57b2018-02-26 15:50:05 +01003043 return flen + chklen;
3044 fail:
3045 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003046}
3047
Willy Tarreau5dd17352018-06-14 13:33:30 +02003048/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3049 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3050 * number of bytes sent. The caller must check the stream's status to detect
3051 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003052 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003053static 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 +02003054{
3055 struct http_hdr list[MAX_HTTP_HDR];
3056 struct h2c *h2c = h2s->h2c;
3057 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003058 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003059 int es_now = 0;
3060 int ret = 0;
3061 int hdr;
3062
3063 if (h2c_mux_busy(h2c, h2s)) {
3064 h2s->flags |= H2_SF_BLK_MBUSY;
3065 return 0;
3066 }
3067
Willy Tarreau44e973f2018-03-01 17:49:30 +01003068 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003069 h2c->flags |= H2_CF_MUX_MALLOC;
3070 h2s->flags |= H2_SF_BLK_MROOM;
3071 return 0;
3072 }
3073
3074 /* First, try to parse the H1 response and index it into <list>.
3075 * NOTE! Since it comes from haproxy, we *know* that a response header
3076 * block does not wrap and we can safely read it this way without
3077 * having to realign the buffer.
3078 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003079 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003080 list, sizeof(list)/sizeof(list[0]), h1m);
3081 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003082 /* incomplete or invalid response, this is abnormal coming from
3083 * haproxy and may only result in a bad errorfile or bad Lua code
3084 * so that won't be fixed, raise an error now.
3085 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003086 * FIXME: we should instead add the ability to only return a
3087 * 502 bad gateway. But in theory this is not supposed to
3088 * happen.
3089 */
3090 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3091 ret = 0;
3092 goto end;
3093 }
3094
3095 chunk_reset(&outbuf);
3096
3097 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003098 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003099 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003100 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003101
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003102 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003103 break;
3104 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003105 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003106 }
3107
3108 if (outbuf.size < 9) {
3109 h2c->flags |= H2_CF_MUX_MFULL;
3110 h2s->flags |= H2_SF_BLK_MROOM;
3111 ret = 0;
3112 goto end;
3113 }
3114
3115 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003116 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3117 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3118 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003119
3120 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003121 if (outbuf.data < outbuf.size && h1m->status == 200)
3122 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3123 else if (outbuf.data < outbuf.size && h1m->status == 304)
3124 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003125 else if (unlikely(list[0].v.len != 3)) {
3126 /* this is an unparsable response */
3127 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3128 ret = 0;
3129 goto end;
3130 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003131 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003132 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3134 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3135 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3136 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3137 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138 }
3139 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003140 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003141 goto realign_again;
3142
3143 h2c->flags |= H2_CF_MUX_MFULL;
3144 h2s->flags |= H2_SF_BLK_MROOM;
3145 ret = 0;
3146 goto end;
3147 }
3148
3149 /* encode all headers, stop at empty name */
3150 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003151 /* these ones do not exist in H2 and must be dropped. */
3152 if (isteq(list[hdr].n, ist("connection")) ||
3153 isteq(list[hdr].n, ist("proxy-connection")) ||
3154 isteq(list[hdr].n, ist("keep-alive")) ||
3155 isteq(list[hdr].n, ist("upgrade")) ||
3156 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003157 continue;
3158
3159 if (isteq(list[hdr].n, ist("")))
3160 break; // end
3161
3162 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3163 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003164 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003165 goto realign_again;
3166
3167 h2c->flags |= H2_CF_MUX_MFULL;
3168 h2s->flags |= H2_SF_BLK_MROOM;
3169 ret = 0;
3170 goto end;
3171 }
3172 }
3173
3174 /* we may need to add END_STREAM */
3175 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3176 es_now = 1;
3177
3178 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003180
3181 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003182 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003183
3184 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003185 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003186
3187 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003188 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003189 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003190
3191 /* for now we don't implemented CONTINUATION, so we wait for a
3192 * body or directly end in TRL2.
3193 */
3194 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003195 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003196 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003197
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003198 h1m->state = HTTP_MSG_DONE;
3199 h2s->flags |= H2_SF_ES_SENT;
3200 if (h2s->st == H2_SS_OPEN)
3201 h2s->st = H2_SS_HLOC;
3202 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003203 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003204 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003205 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003206 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003207 h1m->state = HTTP_MSG_RPBEFORE;
3208 h1m->status = 0;
3209 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003210 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003211 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003212 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003213 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003214
3215 end:
3216 //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, h1_msg_state_str(h1m->err_state));
3217 return ret;
3218}
3219
Willy Tarreau5dd17352018-06-14 13:33:30 +02003220/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3221 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3222 * the number of bytes sent. The caller must check the stream's status to
3223 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003225static 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 +02003226{
3227 struct h2c *h2c = h2s->h2c;
3228 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003229 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003230 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003231 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003232 int es_now = 0;
3233 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003234 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003235 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003236
3237 if (h2c_mux_busy(h2c, h2s)) {
3238 h2s->flags |= H2_SF_BLK_MBUSY;
3239 goto end;
3240 }
3241
Willy Tarreau44e973f2018-03-01 17:49:30 +01003242 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003243 h2c->flags |= H2_CF_MUX_MALLOC;
3244 h2s->flags |= H2_SF_BLK_MROOM;
3245 goto end;
3246 }
3247
3248 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003249 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003250 goto end;
3251
3252 chunk_reset(&outbuf);
3253
3254 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003255 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003256 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003257 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003258
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003259 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003260 break;
3261 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003263 }
3264
3265 if (outbuf.size < 9) {
3266 h2c->flags |= H2_CF_MUX_MFULL;
3267 h2s->flags |= H2_SF_BLK_MROOM;
3268 goto end;
3269 }
3270
3271 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003272 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3273 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3274 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003275
3276 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3277 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003278 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003279 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003280 break;
3281 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003282 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003283 if ((long long)size > h1m->curr_len)
3284 size = h1m->curr_len;
3285 break;
3286 default: /* te:chunked : parse chunks */
3287 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003288 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003289 if (!ret)
3290 goto end;
3291
3292 if (ret < 0) {
3293 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3294 h1m->err_pos = ret;
3295 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3296 goto end;
3297 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003298 max -= ret;
3299 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003300 total += ret;
3301 h1m->state = HTTP_MSG_CHUNK_SIZE;
3302 }
3303
3304 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3305 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003306 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003307 if (!ret)
3308 goto end;
3309
3310 if (ret < 0) {
3311 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3312 h1m->err_pos = ret;
3313 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3314 goto end;
3315 }
3316
3317 size = chunk;
3318 h1m->curr_len = chunk;
3319 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003320 max -= ret;
3321 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003322 total += ret;
3323 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3324 if (!size)
3325 goto send_empty;
3326 }
3327
3328 /* in MSG_DATA state, continue below */
3329 size = h1m->curr_len;
3330 break;
3331 }
3332
3333 /* we have in <size> the exact number of bytes we need to copy from
3334 * the H1 buffer. We need to check this against the connection's and
3335 * the stream's send windows, and to ensure that this fits in the max
3336 * frame size and in the buffer's available space minus 9 bytes (for
3337 * the frame header). The connection's flow control is applied last so
3338 * that we can use a separate list of streams which are immediately
3339 * unblocked on window opening. Note: we don't implement padding.
3340 */
3341
Willy Tarreau5dd17352018-06-14 13:33:30 +02003342 if (size > max)
3343 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003344
3345 if (size > h2s->mws)
3346 size = h2s->mws;
3347
3348 if (size <= 0) {
3349 h2s->flags |= H2_SF_BLK_SFCTL;
3350 goto end;
3351 }
3352
3353 if (h2c->mfs && size > h2c->mfs)
3354 size = h2c->mfs;
3355
3356 if (size + 9 > outbuf.size) {
3357 /* we have an opportunity for enlarging the too small
3358 * available space, let's try.
3359 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003360 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003361 goto realign_again;
3362 size = outbuf.size - 9;
3363 }
3364
3365 if (size <= 0) {
3366 h2c->flags |= H2_CF_MUX_MFULL;
3367 h2s->flags |= H2_SF_BLK_MROOM;
3368 goto end;
3369 }
3370
3371 if (size > h2c->mws)
3372 size = h2c->mws;
3373
3374 if (size <= 0) {
3375 h2s->flags |= H2_SF_BLK_MFCTL;
3376 goto end;
3377 }
3378
3379 /* copy whatever we can */
3380 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003381 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003382 if (ret == 1)
3383 len2 = 0;
3384
3385 if (!ret || len1 + len2 < size) {
3386 /* FIXME: must normally never happen */
3387 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3388 goto end;
3389 }
3390
3391 /* limit len1/len2 to size */
3392 if (len1 + len2 > size) {
3393 int sub = len1 + len2 - size;
3394
3395 if (len2 > sub)
3396 len2 -= sub;
3397 else {
3398 sub -= len2;
3399 len2 = 0;
3400 len1 -= sub;
3401 }
3402 }
3403
3404 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003405 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003406 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003407 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003408
3409 send_empty:
3410 /* we may need to add END_STREAM */
3411 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3412 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003413 *
3414 * FIXME: what we do here is not correct because we send end_stream
3415 * before knowing if we'll have to send a HEADERS frame for the
3416 * trailers. More importantly we're not consuming the trailing CRLF
3417 * after the end of trailers, so it will be left to the caller to
3418 * eat it. The right way to do it would be to measure trailers here
3419 * and to send ES only if there are no trailers.
3420 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003421 */
3422 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3423 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3424 es_now = 1;
3425
3426 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003427 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003428
3429 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003430 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431
3432 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003433 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003434
3435 /* consume incoming H1 response */
3436 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003437 max -= size;
3438 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003439 total += size;
3440 h1m->curr_len -= size;
3441 h2s->mws -= size;
3442 h2c->mws -= size;
3443
3444 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3445 h1m->state = HTTP_MSG_CHUNK_CRLF;
3446 goto new_frame;
3447 }
3448 }
3449
3450 if (es_now) {
3451 if (h2s->st == H2_SS_OPEN)
3452 h2s->st = H2_SS_HLOC;
3453 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003454 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003455
Willy Tarreau35a62702018-02-27 15:37:25 +01003456 if (!(h1m->flags & H1_MF_CHNK)) {
3457 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003458 total += max;
3459 ofs += max;
3460 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003461
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003462 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003463 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003464
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003465 h2s->flags |= H2_SF_ES_SENT;
3466 }
3467
3468 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003469 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, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003470 return total;
3471}
3472
Olivier Houchard6ff20392018-07-17 18:46:31 +02003473/* Called from the upper layer, to subscribe to events, such as being able to send */
3474static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3475{
3476 struct wait_list *sw;
3477 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003478 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003479
3480 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003481 case SUB_CAN_RECV:
3482 sw = param;
3483 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3484 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003485 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003486 }
3487 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003488 case SUB_CAN_SEND:
3489 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003490 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003491 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003492 LIST_ADDQ(&h2c->send_wait_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003493 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003494 return 0;
3495 default:
3496 break;
3497 }
3498 return -1;
3499
3500
3501}
3502
Olivier Houchard511efea2018-08-16 15:30:32 +02003503/* Called from the upper layer, to receive data */
3504static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3505{
Olivier Houchard638b7992018-08-16 15:41:52 +02003506 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003507 size_t ret = 0;
3508
3509 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003510 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003511
Olivier Houchard638b7992018-08-16 15:41:52 +02003512 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003513 cs->flags |= CS_FL_RCV_MORE;
3514 else {
3515 cs->flags &= ~CS_FL_RCV_MORE;
3516 if (cs->flags & CS_FL_REOS)
3517 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003518 if (b_size(&h2s->rxbuf)) {
3519 b_free(&h2s->rxbuf);
3520 offer_buffers(NULL, tasks_run_queue);
3521 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003522 }
3523
3524 return ret;
3525}
3526
Willy Tarreau62f52692017-10-08 23:01:42 +02003527/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003528static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003529{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003530 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003531 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003532 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003533
Willy Tarreau0bad0432018-06-14 16:54:01 +02003534 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003535 h2s->flags |= H2_SF_OUTGOING_DATA;
3536
Willy Tarreau0bad0432018-06-14 16:54:01 +02003537 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003538 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003539 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003540 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003541 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003542 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003543 }
3544 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3545 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003546 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003547
Willy Tarreau5dd17352018-06-14 13:33:30 +02003548 if (unlikely((int)ret <= 0)) {
3549 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003550 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3551 break;
3552 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003553 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003554 total += count;
3555 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003556 h2s->res.state = HTTP_MSG_DONE;
3557 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003558 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003559 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003560 cs->flags |= CS_FL_ERROR;
3561 break;
3562 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003563
3564 total += ret;
3565 count -= ret;
3566
3567 if (h2s->st >= H2_SS_ERROR)
3568 break;
3569
3570 if (h2s->flags & H2_SF_BLK_ANY)
3571 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003572 }
3573
Willy Tarreau00610962018-07-19 10:58:28 +02003574 if (h2s->st >= H2_SS_ERROR) {
3575 /* trim any possibly pending data after we close (extra CR-LF,
3576 * unprocessed trailers, abnormal extra data, ...)
3577 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003578 total += count;
3579 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003580 }
3581
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003582 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003583 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003584 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003585 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003586 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003587 }
3588
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003589 if (h2s->flags & H2_SF_BLK_SFCTL) {
3590 /* stream flow control, quit the list */
3591 LIST_DEL(&h2s->list);
3592 LIST_INIT(&h2s->list);
3593 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003594 else if (LIST_ISEMPTY(&h2s->list)) {
3595 if (h2s->flags & H2_SF_BLK_MFCTL)
3596 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003597 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003598
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003599 b_del(buf, total);
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003600 if (total > 0)
3601 conn_xprt_want_send(h2s->h2c->conn);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003602 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003603}
3604
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003605/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003606static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003607{
3608 struct h2c *h2c = conn->mux_ctx;
3609 struct h2s *h2s;
3610 struct eb32_node *node;
3611 int fctl_cnt = 0;
3612 int send_cnt = 0;
3613 int tree_cnt = 0;
3614 int orph_cnt = 0;
3615
3616 if (!h2c)
3617 return;
3618
3619 list_for_each_entry(h2s, &h2c->fctl_list, list)
3620 fctl_cnt++;
3621
3622 list_for_each_entry(h2s, &h2c->send_list, list)
3623 send_cnt++;
3624
3625 node = eb32_first(&h2c->streams_by_id);
3626 while (node) {
3627 h2s = container_of(node, struct h2s, by_id);
3628 tree_cnt++;
3629 if (!h2s->cs)
3630 orph_cnt++;
3631 node = eb32_next(node);
3632 }
3633
Willy Tarreau616ac812018-07-24 14:12:42 +02003634 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3635 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3636 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3637 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3638 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3639 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003640}
Willy Tarreau62f52692017-10-08 23:01:42 +02003641
3642/*******************************************************/
3643/* functions below are dedicated to the config parsers */
3644/*******************************************************/
3645
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003646/* config parser for global "tune.h2.header-table-size" */
3647static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3648 struct proxy *defpx, const char *file, int line,
3649 char **err)
3650{
3651 if (too_many_args(1, args, err, NULL))
3652 return -1;
3653
3654 h2_settings_header_table_size = atoi(args[1]);
3655 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3656 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3657 return -1;
3658 }
3659 return 0;
3660}
Willy Tarreau62f52692017-10-08 23:01:42 +02003661
Willy Tarreaue6baec02017-07-27 11:45:11 +02003662/* config parser for global "tune.h2.initial-window-size" */
3663static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3664 struct proxy *defpx, const char *file, int line,
3665 char **err)
3666{
3667 if (too_many_args(1, args, err, NULL))
3668 return -1;
3669
3670 h2_settings_initial_window_size = atoi(args[1]);
3671 if (h2_settings_initial_window_size < 0) {
3672 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3673 return -1;
3674 }
3675 return 0;
3676}
3677
Willy Tarreau5242ef82017-07-27 11:47:28 +02003678/* config parser for global "tune.h2.max-concurrent-streams" */
3679static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3680 struct proxy *defpx, const char *file, int line,
3681 char **err)
3682{
3683 if (too_many_args(1, args, err, NULL))
3684 return -1;
3685
3686 h2_settings_max_concurrent_streams = atoi(args[1]);
3687 if (h2_settings_max_concurrent_streams < 0) {
3688 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3689 return -1;
3690 }
3691 return 0;
3692}
3693
Willy Tarreau62f52692017-10-08 23:01:42 +02003694
3695/****************************************/
3696/* MUX initialization and instanciation */
3697/***************************************/
3698
3699/* The mux operations */
3700const struct mux_ops h2_ops = {
3701 .init = h2_init,
Willy Tarreau62f52692017-10-08 23:01:42 +02003702 .wake = h2_wake,
3703 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003704 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003705 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003706 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003707 .attach = h2_attach,
3708 .detach = h2_detach,
3709 .shutr = h2_shutr,
3710 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003711 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003712 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003713 .name = "H2",
3714};
3715
Christopher Faulet32f61c02018-04-10 14:33:41 +02003716/* PROTO selection : this mux registers PROTO token "h2" */
3717static struct mux_proto_list mux_proto_h2 =
3718 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003719
3720/* config keyword parsers */
3721static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003722 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003723 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003724 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003725 { 0, NULL, NULL }
3726}};
3727
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003728static void __h2_deinit(void)
3729{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003730 pool_destroy(pool_head_h2s);
3731 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003732}
3733
Willy Tarreau62f52692017-10-08 23:01:42 +02003734__attribute__((constructor))
3735static void __h2_init(void)
3736{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003737 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003738 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003739 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003740 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3741 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003742}