blob: a986b5a932af2e94b982865829e8abe8915915bd [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;
186};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200187
Willy Tarreauc6405142017-09-21 20:23:50 +0200188/* descriptor for an h2 frame header */
189struct h2_fh {
190 uint32_t len; /* length, host order, 24 bits */
191 uint32_t sid; /* stream id, host order, 31 bits */
192 uint8_t ft; /* frame type */
193 uint8_t ff; /* frame flags */
194};
195
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200196/* a few settings from the global section */
197static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200198static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200199static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200200
Willy Tarreau2a856182017-05-16 15:20:39 +0200201/* a dmumy closed stream */
202static const struct h2s *h2_closed_stream = &(const struct h2s){
203 .cs = NULL,
204 .h2c = NULL,
205 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100206 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100207 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200208 .id = 0,
209};
210
211/* and a dummy idle stream for use with any unannounced stream */
212static const struct h2s *h2_idle_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200217 .id = 0,
218};
219
Olivier Houchard9f6af332018-05-25 14:04:04 +0200220static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200221static struct task *h2_send(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100222static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100223static int h2_frt_decode_headers(struct h2s *h2s);
224static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200226/*****************************************************/
227/* functions below are for dynamic buffer management */
228/*****************************************************/
229
Willy Tarreau315d8072017-12-10 22:17:57 +0100230/* indicates whether or not the we may call the h2_recv() function to attempt
231 * to receive data into the buffer and/or demux pending data. The condition is
232 * a bit complex due to some API limits for now. The rules are the following :
233 * - if an error or a shutdown was detected on the connection and the buffer
234 * is empty, we must not attempt to receive
235 * - if the demux buf failed to be allocated, we must not try to receive and
236 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100237 * - if no flag indicates a blocking condition, we may attempt to receive,
238 * regardless of whether the demux buffer is full or not, so that only
239 * de demux part decides whether or not to block. This is needed because
240 * the connection API indeed prevents us from re-enabling receipt that is
241 * already enabled in a polled state, so we must always immediately stop
242 * as soon as the demux can't proceed so as never to hit an end of read
243 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100244 * - otherwise must may not attempt
245 */
246static inline int h2_recv_allowed(const struct h2c *h2c)
247{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200248 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100249 (h2c->st0 >= H2_CS_ERROR ||
250 h2c->conn->flags & CO_FL_ERROR ||
251 conn_xprt_read0_pending(h2c->conn)))
252 return 0;
253
254 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100255 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100256 return 1;
257
258 return 0;
259}
260
Willy Tarreauf2101912018-07-19 10:11:38 +0200261/* returns true if the connection has too many conn_streams attached */
262static inline int h2_has_too_many_cs(const struct h2c *h2c)
263{
264 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
265}
266
Willy Tarreau44e973f2018-03-01 17:49:30 +0100267/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
268 * flags are used to figure what buffer was requested. It returns 1 if the
269 * allocation succeeds, in which case the connection is woken up, or 0 if it's
270 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200271 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100272static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200273{
274 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100275 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200278 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100279 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200280 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281 return 1;
282 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283
Willy Tarreau44e973f2018-03-01 17:49:30 +0100284 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
285 h2c->flags &= ~H2_CF_MUX_MALLOC;
286 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
287 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200288
289 if (h2c->flags & H2_CF_DEM_MROOM) {
290 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100291 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200292 conn_xprt_want_recv(h2c->conn);
293 }
Willy Tarreau14398122017-09-22 14:26:04 +0200294 return 1;
295 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100296
297 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
298 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
299 b_alloc_margin(&h2s->cs->rxbuf, 0)) {
300 h2c->flags &= ~H2_CF_DEM_SALLOC;
301 if (h2_recv_allowed(h2c))
302 conn_xprt_want_recv(h2c->conn);
303 return 1;
304 }
305
Willy Tarreau14398122017-09-22 14:26:04 +0200306 return 0;
307}
308
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200309static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200310{
311 struct buffer *buf = NULL;
312
Willy Tarreau44e973f2018-03-01 17:49:30 +0100313 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
314 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
315 h2c->buf_wait.target = h2c;
316 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100318 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200320 __conn_xprt_stop_recv(h2c->conn);
321 }
322 return buf;
323}
324
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200325static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200326{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200327 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200329 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200330 }
331}
332
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200333
Willy Tarreau62f52692017-10-08 23:01:42 +0200334/*****************************************************************/
335/* functions below are dedicated to the mux setup and management */
336/*****************************************************************/
337
Willy Tarreau32218eb2017-09-22 08:07:25 +0200338/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
339static int h2c_frt_init(struct connection *conn)
340{
341 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100342 struct task *t = NULL;
343 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200344
Willy Tarreaubafbe012017-11-24 17:34:44 +0100345 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200346 if (!h2c)
347 goto fail;
348
Willy Tarreau3f133572017-10-31 19:21:06 +0100349
Willy Tarreau599391a2017-11-24 10:16:00 +0100350 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
351 if (tick_isset(sess->fe->timeout.clientfin))
352 h2c->shut_timeout = sess->fe->timeout.clientfin;
353
Willy Tarreau33400292017-11-05 11:23:40 +0100354 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100355 if (tick_isset(h2c->timeout)) {
356 t = task_new(tid_bit);
357 if (!t)
358 goto fail;
359
360 h2c->task = t;
361 t->process = h2_timeout_task;
362 t->context = h2c;
363 t->expire = tick_add(now_ms, h2c->timeout);
364 }
Willy Tarreauea392822017-10-31 10:02:25 +0100365
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200366 h2c->wait_list.task = tasklet_new();
367 if (!h2c->wait_list.task)
368 goto fail;
369 h2c->wait_list.task->process = h2_send;
370 h2c->wait_list.task->context = conn;
371
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
373 if (!h2c->ddht)
374 goto fail;
375
376 /* Initialise the context. */
377 h2c->st0 = H2_CS_PREFACE;
378 h2c->conn = conn;
379 h2c->max_id = -1;
380 h2c->errcode = H2_ERR_NO_ERROR;
381 h2c->flags = H2_CF_NONE;
382 h2c->rcvd_c = 0;
383 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100384 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200385 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200387 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 h2c->dsi = -1;
389 h2c->msi = -1;
390 h2c->last_sid = -1;
391
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200392 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200393 h2c->miw = 65535; /* mux initial window size */
394 h2c->mws = 65535; /* mux window size */
395 h2c->mfs = 16384; /* initial max frame size */
396 h2c->streams_by_id = EB_ROOT_UNIQUE;
397 LIST_INIT(&h2c->send_list);
398 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100399 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200400 conn->mux_ctx = h2c;
401
Willy Tarreau3f133572017-10-31 19:21:06 +0100402 if (t)
403 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200405 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200406 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100407
Willy Tarreau32218eb2017-09-22 08:07:25 +0200408 /* mux->wake will be called soon to complete the operation */
409 return 0;
410 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100411 if (t)
412 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200413 if (h2c->wait_list.task)
414 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100415 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 return -1;
417}
418
Willy Tarreau62f52692017-10-08 23:01:42 +0200419/* Initialize the mux once it's attached. For outgoing connections, the context
420 * is already initialized before installing the mux, so we detect incoming
421 * connections from the fact that the context is still NULL. Returns < 0 on
422 * error.
423 */
424static int h2_init(struct connection *conn)
425{
426 if (conn->mux_ctx) {
427 /* we don't support outgoing connections for now */
428 return -1;
429 }
430
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200432}
433
Willy Tarreau2373acc2017-10-12 17:35:14 +0200434/* returns the stream associated with id <id> or NULL if not found */
435static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
436{
437 struct eb32_node *node;
438
Willy Tarreau2a856182017-05-16 15:20:39 +0200439 if (id > h2c->max_id)
440 return (struct h2s *)h2_idle_stream;
441
Willy Tarreau2373acc2017-10-12 17:35:14 +0200442 node = eb32_lookup(&h2c->streams_by_id, id);
443 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200444 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200445
446 return container_of(node, struct h2s, by_id);
447}
448
Willy Tarreau62f52692017-10-08 23:01:42 +0200449/* release function for a connection. This one should be called to free all
450 * resources allocated to the mux.
451 */
452static void h2_release(struct connection *conn)
453{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200454 struct h2c *h2c = conn->mux_ctx;
455
456 LIST_DEL(&conn->list);
457
458 if (h2c) {
459 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200460
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100462 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100463 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200464
Willy Tarreau44e973f2018-03-01 17:49:30 +0100465 h2_release_buf(h2c, &h2c->dbuf);
466 h2_release_buf(h2c, &h2c->mbuf);
467
Willy Tarreauea392822017-10-31 10:02:25 +0100468 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200469 h2c->task->context = NULL;
470 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100471 h2c->task = NULL;
472 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200473 if (h2c->wait_list.task)
474 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100475
Willy Tarreaubafbe012017-11-24 17:34:44 +0100476 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200477 }
478
479 conn->mux = NULL;
480 conn->mux_ctx = NULL;
481
482 conn_stop_tracking(conn);
483 conn_full_close(conn);
484 if (conn->destroy_cb)
485 conn->destroy_cb(conn);
486 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200487}
488
489
Willy Tarreau71681172017-10-23 14:39:06 +0200490/******************************************************/
491/* functions below are for the H2 protocol processing */
492/******************************************************/
493
494/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100495static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200496{
497 return h2s ? h2s->id : 0;
498}
499
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200500/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100501static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200502{
503 if (h2c->msi < 0)
504 return 0;
505
506 if (h2c->msi == h2s_id(h2s))
507 return 0;
508
509 return 1;
510}
511
Willy Tarreau741d6df2017-10-17 08:00:59 +0200512/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200514{
515 h2c->errcode = err;
516 h2c->st0 = H2_CS_ERROR;
517}
518
Willy Tarreau2e43f082017-10-17 08:03:59 +0200519/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100520static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200521{
522 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
523 h2s->errcode = err;
524 h2s->st = H2_SS_ERROR;
525 if (h2s->cs)
526 h2s->cs->flags |= CS_FL_ERROR;
527 }
528}
529
Willy Tarreaue4820742017-07-27 13:37:23 +0200530/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200532{
533 uint8_t *out = frame;
534
535 *out = len >> 16;
536 write_n16(out + 1, len);
537}
538
Willy Tarreau54c15062017-10-10 17:10:03 +0200539/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
540 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
541 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200542 * available in the buffer's input prior to calling this function. The buffer
543 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200544 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100545static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200546 const struct buffer *b, int o)
547{
Willy Tarreau591d4452018-06-15 17:21:00 +0200548 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 +0200549}
550
Willy Tarreau1f094672017-11-20 21:27:45 +0100551static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200552{
Willy Tarreau591d4452018-06-15 17:21:00 +0200553 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200554}
555
Willy Tarreau1f094672017-11-20 21:27:45 +0100556static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200557{
Willy Tarreau591d4452018-06-15 17:21:00 +0200558 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200559}
560
Willy Tarreau1f094672017-11-20 21:27:45 +0100561static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200562{
Willy Tarreau591d4452018-06-15 17:21:00 +0200563 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200564}
565
566
Willy Tarreau715d5312017-07-11 15:20:24 +0200567/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
568 * is not obvious. It turns out that H2 headers are neither aligned nor do they
569 * use regular sizes. And to add to the trouble, the buffer may wrap so each
570 * byte read must be checked. The header is formed like this :
571 *
572 * b0 b1 b2 b3 b4 b5..b8
573 * +----------+---------+--------+----+----+----------------------+
574 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
575 * +----------+---------+--------+----+----+----------------------+
576 *
577 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
578 * we get the sid properly aligned and ordered, and 16 bits of len properly
579 * ordered as well. The type and flags can be extracted using bit shifts from
580 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200581 * Returns zero if some bytes are missing, otherwise non-zero on success. The
582 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200583 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100584static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200585{
586 uint64_t w;
587
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200588 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200589 return 0;
590
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200591 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200592 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200593 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
594 h->ff = w >> 32;
595 h->ft = w >> 40;
596 h->len += w >> 48;
597 return 1;
598}
599
600/* skip the next 9 bytes corresponding to the frame header possibly parsed by
601 * h2_peek_frame_hdr() above.
602 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100603static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200604{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200605 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200606}
607
608/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100609static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200610{
611 int ret;
612
613 ret = h2_peek_frame_hdr(b, h);
614 if (ret > 0)
615 h2_skip_frame_hdr(b);
616 return ret;
617}
618
Willy Tarreau00dd0782018-03-01 16:31:34 +0100619/* marks stream <h2s> as CLOSED and decrement the number of active streams for
620 * its connection if the stream was not yet closed. Please use this exclusively
621 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100622 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100623static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100624{
625 if (h2s->st != H2_SS_CLOSED)
626 h2s->h2c->nb_streams--;
627 h2s->st = H2_SS_CLOSED;
628}
629
Willy Tarreau71049cc2018-03-28 13:56:39 +0200630/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
631static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100632{
633 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200634 LIST_DEL(&h2s->list);
635 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100636 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100637 pool_free(pool_head_h2s, h2s);
638}
639
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200640/* creates a new stream <id> on the h2c connection and returns it, or NULL in
641 * case of memory allocation error.
642 */
643static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
644{
645 struct conn_stream *cs;
646 struct h2s *h2s;
647
Willy Tarreaubafbe012017-11-24 17:34:44 +0100648 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200649 if (!h2s)
650 goto out;
651
652 h2s->h2c = h2c;
653 h2s->mws = h2c->miw;
654 h2s->flags = H2_SF_NONE;
655 h2s->errcode = H2_ERR_NO_ERROR;
656 h2s->st = H2_SS_IDLE;
657 h1m_init(&h2s->req);
658 h1m_init(&h2s->res);
659 h2s->by_id.key = h2s->id = id;
660 h2c->max_id = id;
661 LIST_INIT(&h2s->list);
662
663 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100664 h2c->nb_streams++;
665 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
666 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200667
668 cs = cs_new(h2c->conn);
669 if (!cs)
670 goto out_close;
671
672 h2s->cs = cs;
673 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200674 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675
676 if (stream_create_from_cs(cs) < 0)
677 goto out_free_cs;
678
679 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200680 if (h2_has_too_many_cs(h2c))
681 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200682 return h2s;
683
684 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200685 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200686 cs_free(cs);
687 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200688 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200689 h2s = NULL;
690 out:
691 return h2s;
692}
693
Willy Tarreaube5b7152017-09-25 16:25:39 +0200694/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
695 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
696 * the various settings codes.
697 */
698static int h2c_snd_settings(struct h2c *h2c)
699{
700 struct buffer *res;
701 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200702 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200703 int ret;
704
705 if (h2c_mux_busy(h2c, NULL)) {
706 h2c->flags |= H2_CF_DEM_MBUSY;
707 return 0;
708 }
709
Willy Tarreau44e973f2018-03-01 17:49:30 +0100710 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200711 if (!res) {
712 h2c->flags |= H2_CF_MUX_MALLOC;
713 h2c->flags |= H2_CF_DEM_MROOM;
714 return 0;
715 }
716
717 chunk_init(&buf, buf_data, sizeof(buf_data));
718 chunk_memcpy(&buf,
719 "\x00\x00\x00" /* length : 0 for now */
720 "\x04\x00" /* type : 4 (settings), flags : 0 */
721 "\x00\x00\x00\x00", /* stream ID : 0 */
722 9);
723
724 if (h2_settings_header_table_size != 4096) {
725 char str[6] = "\x00\x01"; /* header_table_size */
726
727 write_n32(str + 2, h2_settings_header_table_size);
728 chunk_memcat(&buf, str, 6);
729 }
730
731 if (h2_settings_initial_window_size != 65535) {
732 char str[6] = "\x00\x04"; /* initial_window_size */
733
734 write_n32(str + 2, h2_settings_initial_window_size);
735 chunk_memcat(&buf, str, 6);
736 }
737
738 if (h2_settings_max_concurrent_streams != 0) {
739 char str[6] = "\x00\x03"; /* max_concurrent_streams */
740
741 /* Note: 0 means "unlimited" for haproxy's config but not for
742 * the protocol, so never send this value!
743 */
744 write_n32(str + 2, h2_settings_max_concurrent_streams);
745 chunk_memcat(&buf, str, 6);
746 }
747
748 if (global.tune.bufsize != 16384) {
749 char str[6] = "\x00\x05"; /* max_frame_size */
750
751 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
752 * match bufsize - rewrite size, but at the moment it seems
753 * that clients don't take care of it.
754 */
755 write_n32(str + 2, global.tune.bufsize);
756 chunk_memcat(&buf, str, 6);
757 }
758
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 h2_set_frame_size(buf.area, buf.data - 9);
760 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200761 if (unlikely(ret <= 0)) {
762 if (!ret) {
763 h2c->flags |= H2_CF_MUX_MFULL;
764 h2c->flags |= H2_CF_DEM_MROOM;
765 return 0;
766 }
767 else {
768 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
769 return 0;
770 }
771 }
772 return ret;
773}
774
Willy Tarreau52eed752017-09-22 15:05:09 +0200775/* Try to receive a connection preface, then upon success try to send our
776 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
777 * missing data. It may return an error in h2c.
778 */
779static int h2c_frt_recv_preface(struct h2c *h2c)
780{
781 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200782 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200783
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200784 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200785
786 if (unlikely(ret1 <= 0)) {
787 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
788 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
789 return 0;
790 }
791
Willy Tarreaube5b7152017-09-25 16:25:39 +0200792 ret2 = h2c_snd_settings(h2c);
793 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200794 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200795
Willy Tarreaube5b7152017-09-25 16:25:39 +0200796 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200797}
798
Willy Tarreau081d4722017-05-16 21:51:05 +0200799/* try to send a GOAWAY frame on the connection to report an error or a graceful
800 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
801 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
802 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
803 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
804 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
805 * on unrecoverable failure. It will not attempt to send one again in this last
806 * case so that it is safe to use h2c_error() to report such errors.
807 */
808static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
809{
810 struct buffer *res;
811 char str[17];
812 int ret;
813
814 if (h2c->flags & H2_CF_GOAWAY_FAILED)
815 return 1; // claim that it worked
816
817 if (h2c_mux_busy(h2c, h2s)) {
818 if (h2s)
819 h2s->flags |= H2_SF_BLK_MBUSY;
820 else
821 h2c->flags |= H2_CF_DEM_MBUSY;
822 return 0;
823 }
824
Willy Tarreau44e973f2018-03-01 17:49:30 +0100825 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200826 if (!res) {
827 h2c->flags |= H2_CF_MUX_MALLOC;
828 if (h2s)
829 h2s->flags |= H2_SF_BLK_MROOM;
830 else
831 h2c->flags |= H2_CF_DEM_MROOM;
832 return 0;
833 }
834
835 /* len: 8, type: 7, flags: none, sid: 0 */
836 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
837
838 if (h2c->last_sid < 0)
839 h2c->last_sid = h2c->max_id;
840
841 write_n32(str + 9, h2c->last_sid);
842 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200843 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200844 if (unlikely(ret <= 0)) {
845 if (!ret) {
846 h2c->flags |= H2_CF_MUX_MFULL;
847 if (h2s)
848 h2s->flags |= H2_SF_BLK_MROOM;
849 else
850 h2c->flags |= H2_CF_DEM_MROOM;
851 return 0;
852 }
853 else {
854 /* we cannot report this error using GOAWAY, so we mark
855 * it and claim a success.
856 */
857 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
858 h2c->flags |= H2_CF_GOAWAY_FAILED;
859 return 1;
860 }
861 }
862 h2c->flags |= H2_CF_GOAWAY_SENT;
863 return ret;
864}
865
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100866/* Try to send an RST_STREAM frame on the connection for the indicated stream
867 * during mux operations. This stream must be valid and cannot be closed
868 * already. h2s->id will be used for the stream ID and h2s->errcode will be
869 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
870 * not yet.
871 *
872 * Returns > 0 on success or zero if nothing was done. In case of lack of room
873 * to write the message, it subscribes the stream to future notifications.
874 */
875static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
876{
877 struct buffer *res;
878 char str[13];
879 int ret;
880
881 if (!h2s || h2s->st == H2_SS_CLOSED)
882 return 1;
883
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100884 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
885 * RST_STREAM in response to a RST_STREAM frame.
886 */
887 if (h2c->dft == H2_FT_RST_STREAM) {
888 ret = 1;
889 goto ignore;
890 }
891
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100892 if (h2c_mux_busy(h2c, h2s)) {
893 h2s->flags |= H2_SF_BLK_MBUSY;
894 return 0;
895 }
896
Willy Tarreau44e973f2018-03-01 17:49:30 +0100897 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100898 if (!res) {
899 h2c->flags |= H2_CF_MUX_MALLOC;
900 h2s->flags |= H2_SF_BLK_MROOM;
901 return 0;
902 }
903
904 /* len: 4, type: 3, flags: none */
905 memcpy(str, "\x00\x00\x04\x03\x00", 5);
906 write_n32(str + 5, h2s->id);
907 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200908 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100909
910 if (unlikely(ret <= 0)) {
911 if (!ret) {
912 h2c->flags |= H2_CF_MUX_MFULL;
913 h2s->flags |= H2_SF_BLK_MROOM;
914 return 0;
915 }
916 else {
917 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
918 return 0;
919 }
920 }
921
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100922 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100924 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100925 return ret;
926}
927
928/* Try to send an RST_STREAM frame on the connection for the stream being
929 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
930 * error code unless the stream's state already is IDLE or CLOSED in which
931 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
932 * it was not yet.
933 *
934 * Returns > 0 on success or zero if nothing was done. In case of lack of room
935 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100937 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200938 */
939static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
940{
941 struct buffer *res;
942 char str[13];
943 int ret;
944
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100945 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
946 * RST_STREAM in response to a RST_STREAM frame.
947 */
948 if (h2c->dft == H2_FT_RST_STREAM) {
949 ret = 1;
950 goto ignore;
951 }
952
Willy Tarreau27a84c92017-10-17 08:10:17 +0200953 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100954 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200955 return 0;
956 }
957
Willy Tarreau44e973f2018-03-01 17:49:30 +0100958 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200959 if (!res) {
960 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100961 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return 0;
963 }
964
965 /* len: 4, type: 3, flags: none */
966 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100967
Willy Tarreau27a84c92017-10-17 08:10:17 +0200968 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100969 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200970 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200971 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100972
Willy Tarreau27a84c92017-10-17 08:10:17 +0200973 if (unlikely(ret <= 0)) {
974 if (!ret) {
975 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100976 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 return 0;
978 }
979 else {
980 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
981 return 0;
982 }
983 }
984
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100985 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100986 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200987 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100988 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100989 }
990
Willy Tarreau27a84c92017-10-17 08:10:17 +0200991 return ret;
992}
993
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100994/* try to send an empty DATA frame with the ES flag set to notify about the
995 * end of stream and match a shutdown(write). If an ES was already sent as
996 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
997 * on success or zero if nothing was done. In case of lack of room to write the
998 * message, it subscribes the requesting stream to future notifications.
999 */
1000static int h2_send_empty_data_es(struct h2s *h2s)
1001{
1002 struct h2c *h2c = h2s->h2c;
1003 struct buffer *res;
1004 char str[9];
1005 int ret;
1006
Willy Tarreau721c9742017-11-07 11:05:42 +01001007 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001008 return 1;
1009
1010 if (h2c_mux_busy(h2c, h2s)) {
1011 h2s->flags |= H2_SF_BLK_MBUSY;
1012 return 0;
1013 }
1014
Willy Tarreau44e973f2018-03-01 17:49:30 +01001015 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001016 if (!res) {
1017 h2c->flags |= H2_CF_MUX_MALLOC;
1018 h2s->flags |= H2_SF_BLK_MROOM;
1019 return 0;
1020 }
1021
1022 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1023 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1024 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001025 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001026 if (likely(ret > 0)) {
1027 h2s->flags |= H2_SF_ES_SENT;
1028 }
1029 else if (!ret) {
1030 h2c->flags |= H2_CF_MUX_MFULL;
1031 h2s->flags |= H2_SF_BLK_MROOM;
1032 return 0;
1033 }
1034 else {
1035 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1036 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001037 }
1038 return ret;
1039}
1040
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001041/* wake the streams attached to the connection, whose id is greater than <last>,
1042 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1043 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1044 * stream's state is automatically updated accordingly.
1045 */
1046static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1047{
1048 struct eb32_node *node;
1049 struct h2s *h2s;
1050
1051 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1052 flags |= CS_FL_ERROR;
1053
1054 if (conn_xprt_read0_pending(h2c->conn))
1055 flags |= CS_FL_EOS;
1056
1057 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1058 while (node) {
1059 h2s = container_of(node, struct h2s, by_id);
1060 if (h2s->id <= last)
1061 break;
1062 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001063
1064 if (!h2s->cs) {
1065 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001066 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001067 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001068 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001069
1070 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001071 h2s->cs->data_cb->wake(h2s->cs);
1072
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001073 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1074 h2s->st = H2_SS_ERROR;
1075 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1076 h2s->st = H2_SS_HREM;
1077 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001078 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001079 }
1080}
1081
Willy Tarreau3421aba2017-07-27 15:41:03 +02001082/* Increase all streams' outgoing window size by the difference passed in
1083 * argument. This is needed upon receipt of the settings frame if the initial
1084 * window size is different. The difference may be negative and the resulting
1085 * window size as well, for the time it takes to receive some window updates.
1086 */
1087static void h2c_update_all_ws(struct h2c *h2c, int diff)
1088{
1089 struct h2s *h2s;
1090 struct eb32_node *node;
1091
1092 if (!diff)
1093 return;
1094
1095 node = eb32_first(&h2c->streams_by_id);
1096 while (node) {
1097 h2s = container_of(node, struct h2s, by_id);
1098 h2s->mws += diff;
1099 node = eb32_next(node);
1100 }
1101}
1102
1103/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1104 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1105 * return an error in h2c. Described in RFC7540#6.5.
1106 */
1107static int h2c_handle_settings(struct h2c *h2c)
1108{
1109 unsigned int offset;
1110 int error;
1111
1112 if (h2c->dff & H2_F_SETTINGS_ACK) {
1113 if (h2c->dfl) {
1114 error = H2_ERR_FRAME_SIZE_ERROR;
1115 goto fail;
1116 }
1117 return 1;
1118 }
1119
1120 if (h2c->dsi != 0) {
1121 error = H2_ERR_PROTOCOL_ERROR;
1122 goto fail;
1123 }
1124
1125 if (h2c->dfl % 6) {
1126 error = H2_ERR_FRAME_SIZE_ERROR;
1127 goto fail;
1128 }
1129
1130 /* that's the limit we can process */
1131 if (h2c->dfl > global.tune.bufsize) {
1132 error = H2_ERR_FRAME_SIZE_ERROR;
1133 goto fail;
1134 }
1135
1136 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001137 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001138 return 0;
1139
1140 /* parse the frame */
1141 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001142 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1143 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001144
1145 switch (type) {
1146 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1147 /* we need to update all existing streams with the
1148 * difference from the previous iws.
1149 */
1150 if (arg < 0) { // RFC7540#6.5.2
1151 error = H2_ERR_FLOW_CONTROL_ERROR;
1152 goto fail;
1153 }
1154 h2c_update_all_ws(h2c, arg - h2c->miw);
1155 h2c->miw = arg;
1156 break;
1157 case H2_SETTINGS_MAX_FRAME_SIZE:
1158 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1159 error = H2_ERR_PROTOCOL_ERROR;
1160 goto fail;
1161 }
1162 h2c->mfs = arg;
1163 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001164 case H2_SETTINGS_ENABLE_PUSH:
1165 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1166 error = H2_ERR_PROTOCOL_ERROR;
1167 goto fail;
1168 }
1169 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001170 }
1171 }
1172
1173 /* need to ACK this frame now */
1174 h2c->st0 = H2_CS_FRAME_A;
1175 return 1;
1176 fail:
1177 h2c_error(h2c, error);
1178 return 0;
1179}
1180
1181/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1182 * success or one of the h2_status values.
1183 */
1184static int h2c_ack_settings(struct h2c *h2c)
1185{
1186 struct buffer *res;
1187 char str[9];
1188 int ret = -1;
1189
1190 if (h2c_mux_busy(h2c, NULL)) {
1191 h2c->flags |= H2_CF_DEM_MBUSY;
1192 return 0;
1193 }
1194
Willy Tarreau44e973f2018-03-01 17:49:30 +01001195 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001196 if (!res) {
1197 h2c->flags |= H2_CF_MUX_MALLOC;
1198 h2c->flags |= H2_CF_DEM_MROOM;
1199 return 0;
1200 }
1201
1202 memcpy(str,
1203 "\x00\x00\x00" /* length : 0 (no data) */
1204 "\x04" "\x01" /* type : 4, flags : ACK */
1205 "\x00\x00\x00\x00" /* stream ID */, 9);
1206
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001207 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001208 if (unlikely(ret <= 0)) {
1209 if (!ret) {
1210 h2c->flags |= H2_CF_MUX_MFULL;
1211 h2c->flags |= H2_CF_DEM_MROOM;
1212 return 0;
1213 }
1214 else {
1215 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1216 return 0;
1217 }
1218 }
1219 return ret;
1220}
1221
Willy Tarreaucf68c782017-10-10 17:11:41 +02001222/* processes a PING frame and schedules an ACK if needed. The caller must pass
1223 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1224 * missing data. It may return an error in h2c.
1225 */
1226static int h2c_handle_ping(struct h2c *h2c)
1227{
1228 /* frame length must be exactly 8 */
1229 if (h2c->dfl != 8) {
1230 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1231 return 0;
1232 }
1233
1234 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001235 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001236 h2c->st0 = H2_CS_FRAME_A;
1237 return 1;
1238}
1239
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001240/* Try to send a window update for stream id <sid> and value <increment>.
1241 * Returns > 0 on success or zero on missing room or failure. It may return an
1242 * error in h2c.
1243 */
1244static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1245{
1246 struct buffer *res;
1247 char str[13];
1248 int ret = -1;
1249
1250 if (h2c_mux_busy(h2c, NULL)) {
1251 h2c->flags |= H2_CF_DEM_MBUSY;
1252 return 0;
1253 }
1254
Willy Tarreau44e973f2018-03-01 17:49:30 +01001255 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001256 if (!res) {
1257 h2c->flags |= H2_CF_MUX_MALLOC;
1258 h2c->flags |= H2_CF_DEM_MROOM;
1259 return 0;
1260 }
1261
1262 /* length: 4, type: 8, flags: none */
1263 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1264 write_n32(str + 5, sid);
1265 write_n32(str + 9, increment);
1266
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001267 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001268
1269 if (unlikely(ret <= 0)) {
1270 if (!ret) {
1271 h2c->flags |= H2_CF_MUX_MFULL;
1272 h2c->flags |= H2_CF_DEM_MROOM;
1273 return 0;
1274 }
1275 else {
1276 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1277 return 0;
1278 }
1279 }
1280 return ret;
1281}
1282
1283/* try to send pending window update for the connection. It's safe to call it
1284 * with no pending updates. Returns > 0 on success or zero on missing room or
1285 * failure. It may return an error in h2c.
1286 */
1287static int h2c_send_conn_wu(struct h2c *h2c)
1288{
1289 int ret = 1;
1290
1291 if (h2c->rcvd_c <= 0)
1292 return 1;
1293
1294 /* send WU for the connection */
1295 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1296 if (ret > 0)
1297 h2c->rcvd_c = 0;
1298
1299 return ret;
1300}
1301
1302/* try to send pending window update for the current dmux stream. It's safe to
1303 * call it with no pending updates. Returns > 0 on success or zero on missing
1304 * room or failure. It may return an error in h2c.
1305 */
1306static int h2c_send_strm_wu(struct h2c *h2c)
1307{
1308 int ret = 1;
1309
1310 if (h2c->rcvd_s <= 0)
1311 return 1;
1312
1313 /* send WU for the stream */
1314 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1315 if (ret > 0)
1316 h2c->rcvd_s = 0;
1317
1318 return ret;
1319}
1320
Willy Tarreaucf68c782017-10-10 17:11:41 +02001321/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1322 * success, 0 on missing data or one of the h2_status values.
1323 */
1324static int h2c_ack_ping(struct h2c *h2c)
1325{
1326 struct buffer *res;
1327 char str[17];
1328 int ret = -1;
1329
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001330 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001331 return 0;
1332
1333 if (h2c_mux_busy(h2c, NULL)) {
1334 h2c->flags |= H2_CF_DEM_MBUSY;
1335 return 0;
1336 }
1337
Willy Tarreau44e973f2018-03-01 17:49:30 +01001338 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001339 if (!res) {
1340 h2c->flags |= H2_CF_MUX_MALLOC;
1341 h2c->flags |= H2_CF_DEM_MROOM;
1342 return 0;
1343 }
1344
1345 memcpy(str,
1346 "\x00\x00\x08" /* length : 8 (same payload) */
1347 "\x06" "\x01" /* type : 6, flags : ACK */
1348 "\x00\x00\x00\x00" /* stream ID */, 9);
1349
1350 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001351 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001352
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001353 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001354 if (unlikely(ret <= 0)) {
1355 if (!ret) {
1356 h2c->flags |= H2_CF_MUX_MFULL;
1357 h2c->flags |= H2_CF_DEM_MROOM;
1358 return 0;
1359 }
1360 else {
1361 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1362 return 0;
1363 }
1364 }
1365 return ret;
1366}
1367
Willy Tarreau26f95952017-07-27 17:18:30 +02001368/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1369 * Returns > 0 on success or zero on missing data. It may return an error in
1370 * h2c or h2s. Described in RFC7540#6.9.
1371 */
1372static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1373{
1374 int32_t inc;
1375 int error;
1376
1377 if (h2c->dfl != 4) {
1378 error = H2_ERR_FRAME_SIZE_ERROR;
1379 goto conn_err;
1380 }
1381
1382 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001383 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001384 return 0;
1385
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001386 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001387
1388 if (h2c->dsi != 0) {
1389 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001390
1391 /* it's not an error to receive WU on a closed stream */
1392 if (h2s->st == H2_SS_CLOSED)
1393 return 1;
1394
1395 if (!inc) {
1396 error = H2_ERR_PROTOCOL_ERROR;
1397 goto strm_err;
1398 }
1399
1400 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1401 error = H2_ERR_FLOW_CONTROL_ERROR;
1402 goto strm_err;
1403 }
1404
1405 h2s->mws += inc;
1406 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1407 h2s->flags &= ~H2_SF_BLK_SFCTL;
1408 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1409 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1410 /* This stream wanted to send but could not due to its
1411 * own flow control. We can put it back into the send
1412 * list now, it will be handled upon next send() call.
1413 */
1414 LIST_ADDQ(&h2c->send_list, &h2s->list);
1415 }
1416 }
1417 }
1418 else {
1419 /* connection window update */
1420 if (!inc) {
1421 error = H2_ERR_PROTOCOL_ERROR;
1422 goto conn_err;
1423 }
1424
1425 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1426 error = H2_ERR_FLOW_CONTROL_ERROR;
1427 goto conn_err;
1428 }
1429
1430 h2c->mws += inc;
1431 }
1432
1433 return 1;
1434
1435 conn_err:
1436 h2c_error(h2c, error);
1437 return 0;
1438
1439 strm_err:
1440 if (h2s) {
1441 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001442 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001443 }
1444 else
1445 h2c_error(h2c, error);
1446 return 0;
1447}
1448
Willy Tarreaue96b0922017-10-30 00:28:29 +01001449/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1450 * the last ID. Returns > 0 on success or zero on missing data. It may return
1451 * an error in h2c. Described in RFC7540#6.8.
1452 */
1453static int h2c_handle_goaway(struct h2c *h2c)
1454{
1455 int error;
1456 int last;
1457
1458 if (h2c->dsi != 0) {
1459 error = H2_ERR_PROTOCOL_ERROR;
1460 goto conn_err;
1461 }
1462
1463 if (h2c->dfl < 8) {
1464 error = H2_ERR_FRAME_SIZE_ERROR;
1465 goto conn_err;
1466 }
1467
1468 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001469 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001470 return 0;
1471
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001472 last = h2_get_n32(&h2c->dbuf, 0);
1473 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001474 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001475 if (h2c->last_sid < 0)
1476 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001477 return 1;
1478
1479 conn_err:
1480 h2c_error(h2c, error);
1481 return 0;
1482}
1483
Willy Tarreau92153fc2017-12-03 19:46:19 +01001484/* processes a PRIORITY frame, and either skips it or rejects if it is
1485 * invalid. Returns > 0 on success or zero on missing data. It may return
1486 * an error in h2c. Described in RFC7540#6.3.
1487 */
1488static int h2c_handle_priority(struct h2c *h2c)
1489{
1490 int error;
1491
1492 if (h2c->dsi == 0) {
1493 error = H2_ERR_PROTOCOL_ERROR;
1494 goto conn_err;
1495 }
1496
1497 if (h2c->dfl != 5) {
1498 error = H2_ERR_FRAME_SIZE_ERROR;
1499 goto conn_err;
1500 }
1501
1502 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001503 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001504 return 0;
1505
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001506 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001507 /* 7540#5.3 : can't depend on itself */
1508 error = H2_ERR_PROTOCOL_ERROR;
1509 goto conn_err;
1510 }
1511 return 1;
1512
1513 conn_err:
1514 h2c_error(h2c, error);
1515 return 0;
1516}
1517
Willy Tarreaucd234e92017-08-18 10:59:39 +02001518/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1519 * Returns > 0 on success or zero on missing data. It may return an error in
1520 * h2c. Described in RFC7540#6.4.
1521 */
1522static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1523{
1524 int error;
1525
1526 if (h2c->dsi == 0) {
1527 error = H2_ERR_PROTOCOL_ERROR;
1528 goto conn_err;
1529 }
1530
Willy Tarreaucd234e92017-08-18 10:59:39 +02001531 if (h2c->dfl != 4) {
1532 error = H2_ERR_FRAME_SIZE_ERROR;
1533 goto conn_err;
1534 }
1535
1536 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001537 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001538 return 0;
1539
1540 /* late RST, already handled */
1541 if (h2s->st == H2_SS_CLOSED)
1542 return 1;
1543
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001544 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001545 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001546
1547 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001548 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001549 h2s->cs->data_cb->wake(h2s->cs);
1550 }
1551
1552 h2s->flags |= H2_SF_RST_RCVD;
1553 return 1;
1554
1555 conn_err:
1556 h2c_error(h2c, error);
1557 return 0;
1558}
1559
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001560/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1561 * It may return an error in h2c or h2s. The caller must consider that the
1562 * return value is the new h2s in case one was allocated (most common case).
1563 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001564 * errors here are reported as connection errors since it's impossible to
1565 * recover from such errors after the compression context has been altered.
1566 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001567static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001568{
1569 int error;
1570
1571 if (!h2c->dfl) {
1572 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1573 goto strm_err;
1574 }
1575
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001576 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001577 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001578
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001579 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001580 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001581
Willy Tarreauf2101912018-07-19 10:11:38 +02001582 if (h2c->flags & H2_CF_DEM_TOOMANY)
1583 return 0; // too many cs still present
1584
Willy Tarreau13278b42017-10-13 19:23:14 +02001585 /* now either the frame is complete or the buffer is complete */
1586 if (h2s->st != H2_SS_IDLE) {
1587 /* FIXME: stream already exists, this is only allowed for
1588 * trailers (not supported for now).
1589 */
1590 error = H2_ERR_PROTOCOL_ERROR;
1591 goto conn_err;
1592 }
1593 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1594 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1595 error = H2_ERR_PROTOCOL_ERROR;
1596 goto conn_err;
1597 }
1598
1599 h2s = h2c_stream_new(h2c, h2c->dsi);
1600 if (!h2s) {
1601 error = H2_ERR_INTERNAL_ERROR;
1602 goto conn_err;
1603 }
1604
1605 h2s->st = H2_SS_OPEN;
1606 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1607 h2s->st = H2_SS_HREM;
1608 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001609 /* note: cs cannot be null for now (just created above) */
1610 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001611 }
1612
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001613 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001614 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001615
Willy Tarreau8f650c32017-11-21 19:36:21 +01001616 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001617 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001618
Willy Tarreau721c9742017-11-07 11:05:42 +01001619 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001620 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001621 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001622 }
1623 else {
1624 /* update the max stream ID if the request is being processed */
1625 if (h2s->id > h2c->max_id)
1626 h2c->max_id = h2s->id;
1627 }
1628
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001629 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001630
1631 conn_err:
1632 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001633 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001634
1635 strm_err:
1636 if (h2s) {
1637 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001638 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001639 }
1640 else
1641 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001642 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001643}
1644
Willy Tarreau454f9052017-10-26 19:40:35 +02001645/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1646 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1647 */
1648static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1649{
1650 int error;
1651
1652 /* note that empty DATA frames are perfectly valid and sometimes used
1653 * to signal an end of stream (with the ES flag).
1654 */
1655
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001656 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001657 return 0; // empty buffer
1658
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001659 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001660 return 0; // incomplete frame
1661
1662 /* now either the frame is complete or the buffer is complete */
1663
1664 if (!h2c->dsi) {
1665 /* RFC7540#6.1 */
1666 error = H2_ERR_PROTOCOL_ERROR;
1667 goto conn_err;
1668 }
1669
1670 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1671 /* RFC7540#6.1 */
1672 error = H2_ERR_STREAM_CLOSED;
1673 goto strm_err;
1674 }
1675
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001676 if (!h2_frt_transfer_data(h2s))
1677 return 0;
1678
Willy Tarreau454f9052017-10-26 19:40:35 +02001679 /* call the upper layers to process the frame, then let the upper layer
1680 * notify the stream about any change.
1681 */
1682 if (!h2s->cs) {
1683 error = H2_ERR_STREAM_CLOSED;
1684 goto strm_err;
1685 }
1686
Willy Tarreau8f650c32017-11-21 19:36:21 +01001687 if (h2c->st0 >= H2_CS_ERROR)
1688 return 0;
1689
Willy Tarreau721c9742017-11-07 11:05:42 +01001690 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001691 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001692 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001693 }
1694
1695 /* check for completion : the callee will change this to FRAME_A or
1696 * FRAME_H once done.
1697 */
1698 if (h2c->st0 == H2_CS_FRAME_P)
1699 return 0;
1700
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001701
1702 /* last frame */
1703 if (h2c->dff & H2_F_DATA_END_STREAM) {
1704 h2s->st = H2_SS_HREM;
1705 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001706 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001707 }
1708
Willy Tarreau454f9052017-10-26 19:40:35 +02001709 return 1;
1710
1711 conn_err:
1712 h2c_error(h2c, error);
1713 return 0;
1714
1715 strm_err:
1716 if (h2s) {
1717 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001718 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001719 }
1720 else
1721 h2c_error(h2c, error);
1722 return 0;
1723}
1724
Willy Tarreaubc933932017-10-09 16:21:43 +02001725/* process Rx frames to be demultiplexed */
1726static void h2_process_demux(struct h2c *h2c)
1727{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001728 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001729
Willy Tarreau081d4722017-05-16 21:51:05 +02001730 if (h2c->st0 >= H2_CS_ERROR)
1731 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001732
1733 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1734 if (h2c->st0 == H2_CS_PREFACE) {
1735 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1736 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1737 if (h2c->st0 == H2_CS_ERROR)
1738 h2c->st0 = H2_CS_ERROR2;
1739 goto fail;
1740 }
1741
1742 h2c->max_id = 0;
1743 h2c->st0 = H2_CS_SETTINGS1;
1744 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001745
1746 if (h2c->st0 == H2_CS_SETTINGS1) {
1747 struct h2_fh hdr;
1748
1749 /* ensure that what is pending is a valid SETTINGS frame
1750 * without an ACK.
1751 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001752 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001753 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1754 if (h2c->st0 == H2_CS_ERROR)
1755 h2c->st0 = H2_CS_ERROR2;
1756 goto fail;
1757 }
1758
1759 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1760 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1761 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1762 h2c->st0 = H2_CS_ERROR2;
1763 goto fail;
1764 }
1765
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001766 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001767 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1768 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1769 h2c->st0 = H2_CS_ERROR2;
1770 goto fail;
1771 }
1772
1773 /* that's OK, switch to FRAME_P to process it */
1774 h2c->dfl = hdr.len;
1775 h2c->dsi = hdr.sid;
1776 h2c->dft = hdr.ft;
1777 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001778 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001779 h2c->st0 = H2_CS_FRAME_P;
1780 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001781 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001782
1783 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001784 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001785 int ret = 0;
1786
1787 if (h2c->st0 >= H2_CS_ERROR)
1788 break;
1789
1790 if (h2c->st0 == H2_CS_FRAME_H) {
1791 struct h2_fh hdr;
1792
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001793 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001794 break;
1795
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001796 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001797 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1798 h2c->st0 = H2_CS_ERROR;
1799 break;
1800 }
1801
1802 h2c->dfl = hdr.len;
1803 h2c->dsi = hdr.sid;
1804 h2c->dft = hdr.ft;
1805 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001806 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001807 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001808 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001809 }
1810
1811 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001812 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1813
1814 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->cs->rxbuf)) {
1815 /* we may have to signal the upper layers */
1816 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001817 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1818 /* cs has just been destroyed, we have to kill h2s. */
1819 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1820 goto strm_err;
1821 }
1822
1823 if (h2c->st0 >= H2_CS_ERROR)
1824 goto strm_err;
1825
1826 if (h2s->st >= H2_SS_ERROR) {
1827 /* stream error : send RST_STREAM */
1828 h2c->st0 = H2_CS_FRAME_E;
1829 }
1830 }
1831 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001832
Willy Tarreaud7901432017-12-29 11:34:40 +01001833 if (h2c->st0 == H2_CS_FRAME_E)
1834 goto strm_err;
1835
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001836 if (h2s->st == H2_SS_IDLE &&
1837 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1838 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1839 * this state MUST be treated as a connection error
1840 */
1841 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1842 h2c->st0 = H2_CS_ERROR;
1843 break;
1844 }
1845
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001846 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1847 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1848 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1849 * this state MUST be treated as a stream error
1850 */
1851 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1852 goto strm_err;
1853 }
1854
Willy Tarreauab837502017-12-27 15:07:30 +01001855 /* Below the management of frames received in closed state is a
1856 * bit hackish because the spec makes strong differences between
1857 * streams closed by receiving RST, sending RST, and seeing ES
1858 * in both directions. In addition to this, the creation of a
1859 * new stream reusing the identifier of a closed one will be
1860 * detected here. Given that we cannot keep track of all closed
1861 * streams forever, we consider that unknown closed streams were
1862 * closed on RST received, which allows us to respond with an
1863 * RST without breaking the connection (eg: to abort a transfer).
1864 * Some frames have to be silently ignored as well.
1865 */
1866 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1867 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1868 /* #5.1.1: The identifier of a newly
1869 * established stream MUST be numerically
1870 * greater than all streams that the initiating
1871 * endpoint has opened or reserved. This
1872 * governs streams that are opened using a
1873 * HEADERS frame and streams that are reserved
1874 * using PUSH_PROMISE. An endpoint that
1875 * receives an unexpected stream identifier
1876 * MUST respond with a connection error.
1877 */
1878 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1879 goto strm_err;
1880 }
1881
1882 if (h2s->flags & H2_SF_RST_RCVD) {
1883 /* RFC7540#5.1:closed: an endpoint that
1884 * receives any frame other than PRIORITY after
1885 * receiving a RST_STREAM MUST treat that as a
1886 * stream error of type STREAM_CLOSED.
1887 *
1888 * Note that old streams fall into this category
1889 * and will lead to an RST being sent.
1890 */
1891 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1892 h2c->st0 = H2_CS_FRAME_E;
1893 goto strm_err;
1894 }
1895
1896 /* RFC7540#5.1:closed: if this state is reached as a
1897 * result of sending a RST_STREAM frame, the peer that
1898 * receives the RST_STREAM might have already sent
1899 * frames on the stream that cannot be withdrawn. An
1900 * endpoint MUST ignore frames that it receives on
1901 * closed streams after it has sent a RST_STREAM
1902 * frame. An endpoint MAY choose to limit the period
1903 * over which it ignores frames and treat frames that
1904 * arrive after this time as being in error.
1905 */
1906 if (!(h2s->flags & H2_SF_RST_SENT)) {
1907 /* RFC7540#5.1:closed: any frame other than
1908 * PRIO/WU/RST in this state MUST be treated as
1909 * a connection error
1910 */
1911 if (h2c->dft != H2_FT_RST_STREAM &&
1912 h2c->dft != H2_FT_PRIORITY &&
1913 h2c->dft != H2_FT_WINDOW_UPDATE) {
1914 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1915 goto strm_err;
1916 }
1917 }
1918 }
1919
Willy Tarreauc0da1962017-10-30 18:38:00 +01001920#if 0
1921 // problem below: it is not possible to completely ignore such
1922 // streams as we need to maintain the compression state as well
1923 // and for this we need to completely process these frames (eg:
1924 // HEADERS frames) as well as counting DATA frames to emit
1925 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1926 // This is a typical case of layer violation where the
1927 // transported contents are critical to the connection's
1928 // validity and must be ignored at the same time :-(
1929
1930 /* graceful shutdown, ignore streams whose ID is higher than
1931 * the one advertised in GOAWAY. RFC7540#6.8.
1932 */
1933 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001934 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1935 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001936 h2c->dfl -= ret;
1937 ret = h2c->dfl == 0;
1938 goto strm_err;
1939 }
1940#endif
1941
Willy Tarreau7e98c052017-10-10 15:56:59 +02001942 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001943 case H2_FT_SETTINGS:
1944 if (h2c->st0 == H2_CS_FRAME_P)
1945 ret = h2c_handle_settings(h2c);
1946
1947 if (h2c->st0 == H2_CS_FRAME_A)
1948 ret = h2c_ack_settings(h2c);
1949 break;
1950
Willy Tarreaucf68c782017-10-10 17:11:41 +02001951 case H2_FT_PING:
1952 if (h2c->st0 == H2_CS_FRAME_P)
1953 ret = h2c_handle_ping(h2c);
1954
1955 if (h2c->st0 == H2_CS_FRAME_A)
1956 ret = h2c_ack_ping(h2c);
1957 break;
1958
Willy Tarreau26f95952017-07-27 17:18:30 +02001959 case H2_FT_WINDOW_UPDATE:
1960 if (h2c->st0 == H2_CS_FRAME_P)
1961 ret = h2c_handle_window_update(h2c, h2s);
1962 break;
1963
Willy Tarreau61290ec2017-10-17 08:19:21 +02001964 case H2_FT_CONTINUATION:
1965 /* we currently don't support CONTINUATION frames since
1966 * we have nowhere to store the partial HEADERS frame.
1967 * Let's abort the stream on an INTERNAL_ERROR here.
1968 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001969 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001970 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001971 h2c->st0 = H2_CS_FRAME_E;
1972 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001973 break;
1974
Willy Tarreau13278b42017-10-13 19:23:14 +02001975 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001976 if (h2c->st0 == H2_CS_FRAME_P) {
1977 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
1978 if (tmp_h2s) {
1979 h2s = tmp_h2s;
1980 ret = 1;
1981 }
1982 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001983 break;
1984
Willy Tarreau454f9052017-10-26 19:40:35 +02001985 case H2_FT_DATA:
1986 if (h2c->st0 == H2_CS_FRAME_P)
1987 ret = h2c_frt_handle_data(h2c, h2s);
1988
1989 if (h2c->st0 == H2_CS_FRAME_A)
1990 ret = h2c_send_strm_wu(h2c);
1991 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001992
Willy Tarreau92153fc2017-12-03 19:46:19 +01001993 case H2_FT_PRIORITY:
1994 if (h2c->st0 == H2_CS_FRAME_P)
1995 ret = h2c_handle_priority(h2c);
1996 break;
1997
Willy Tarreaucd234e92017-08-18 10:59:39 +02001998 case H2_FT_RST_STREAM:
1999 if (h2c->st0 == H2_CS_FRAME_P)
2000 ret = h2c_handle_rst_stream(h2c, h2s);
2001 break;
2002
Willy Tarreaue96b0922017-10-30 00:28:29 +01002003 case H2_FT_GOAWAY:
2004 if (h2c->st0 == H2_CS_FRAME_P)
2005 ret = h2c_handle_goaway(h2c);
2006 break;
2007
Willy Tarreau1c661982017-10-30 13:52:01 +01002008 case H2_FT_PUSH_PROMISE:
2009 /* not permitted here, RFC7540#5.1 */
2010 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002011 break;
2012
2013 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002014 default:
2015 /* drop frames that we ignore. They may be larger than
2016 * the buffer so we drain all of their contents until
2017 * we reach the end.
2018 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002019 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2020 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002021 h2c->dfl -= ret;
2022 ret = h2c->dfl == 0;
2023 }
2024
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002025 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002026 /* We may have to send an RST if not done yet */
2027 if (h2s->st == H2_SS_ERROR)
2028 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002029
Willy Tarreaua20a5192017-12-27 11:02:06 +01002030 if (h2c->st0 == H2_CS_FRAME_E)
2031 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002032
Willy Tarreau7e98c052017-10-10 15:56:59 +02002033 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002034 if (ret <= 0) {
2035 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002036 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002037 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002038
2039 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002041 h2c->st0 = H2_CS_FRAME_H;
2042 }
2043 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002044
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002045 if (h2c->rcvd_c > 0 &&
2046 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2047 h2c_send_conn_wu(h2c);
2048
Willy Tarreau52eed752017-09-22 15:05:09 +02002049 fail:
2050 /* we can go here on missing data, blocked response or error */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002051 if (h2s && h2s->cs && b_data(&h2s->cs->rxbuf)) {
2052 /* we may have to signal the upper layers */
2053 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002054 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2055 /* cs has just been destroyed, we have to kill h2s. */
2056 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2057 h2c_send_rst_stream(h2c, h2s);
2058 }
2059 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002060 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002061}
2062
2063/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2064 * the end.
2065 */
2066static int h2_process_mux(struct h2c *h2c)
2067{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002068 struct h2s *h2s, *h2s_back;
2069
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002070 /* start by sending possibly pending window updates */
2071 if (h2c->rcvd_c > 0 &&
2072 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2073 h2c_send_conn_wu(h2c) < 0)
2074 goto fail;
2075
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002076 /* First we always process the flow control list because the streams
2077 * waiting there were already elected for immediate emission but were
2078 * blocked just on this.
2079 */
2080
2081 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2082 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2083 h2c->st0 >= H2_CS_ERROR)
2084 break;
2085
2086 /* In theory it's possible that h2s->cs == NULL here :
2087 * - client sends crap that causes a parse error
2088 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2089 * - RST_STREAM cannot be emitted because mux is busy/full
2090 * - stream gets notified, detaches and quits
2091 * - mux buffer gets ready and wakes pending streams up
2092 * - bam!
2093 */
2094 h2s->flags &= ~H2_SF_BLK_ANY;
2095
2096 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002097 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002098 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002099 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002100 }
2101
2102 /* depending on callee's blocking reasons, we may queue in send
2103 * list or completely dequeue.
2104 */
2105 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2106 if (h2s->flags & H2_SF_BLK_ANY) {
2107 LIST_DEL(&h2s->list);
2108 LIST_ADDQ(&h2c->send_list, &h2s->list);
2109 }
2110 else {
2111 LIST_DEL(&h2s->list);
2112 LIST_INIT(&h2s->list);
2113 if (h2s->cs)
2114 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002115 else {
2116 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002117 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002118 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002119 }
2120 }
2121 }
2122
2123 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2124 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2125 break;
2126
2127 /* In theory it's possible that h2s->cs == NULL here :
2128 * - client sends crap that causes a parse error
2129 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2130 * - RST_STREAM cannot be emitted because mux is busy/full
2131 * - stream gets notified, detaches and quits
2132 * - mux buffer gets ready and wakes pending streams up
2133 * - bam!
2134 */
2135 h2s->flags &= ~H2_SF_BLK_ANY;
2136
2137 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002138 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002139 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002140 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002141 }
2142 /* depending on callee's blocking reasons, we may queue in fctl
2143 * list or completely dequeue.
2144 */
2145 if (h2s->flags & H2_SF_BLK_MFCTL) {
2146 /* stream hit the connection's flow control */
2147 LIST_DEL(&h2s->list);
2148 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2149 }
2150 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2151 LIST_DEL(&h2s->list);
2152 LIST_INIT(&h2s->list);
2153 if (h2s->cs)
2154 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002155 else {
2156 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002157 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002158 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002159 }
2160 }
2161
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002162 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002163 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002164 if (h2c->st0 == H2_CS_ERROR) {
2165 if (h2c->max_id >= 0) {
2166 h2c_send_goaway_error(h2c, NULL);
2167 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2168 return 0;
2169 }
2170
2171 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2172 }
2173 return 1;
2174 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002175 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002176}
2177
Willy Tarreau71681172017-10-23 14:39:06 +02002178
Willy Tarreau62f52692017-10-08 23:01:42 +02002179/*********************************************************/
2180/* functions below are I/O callbacks from the connection */
2181/*********************************************************/
2182
2183/* callback called on recv event by the connection handler */
2184static void h2_recv(struct connection *conn)
2185{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002186 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002187 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002188 int max;
2189
Willy Tarreau315d8072017-12-10 22:17:57 +01002190 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002191 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002192
Willy Tarreau44e973f2018-03-01 17:49:30 +01002193 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002194 if (!buf) {
2195 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002196 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002197 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002198
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002199 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002200 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002201 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002202
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002203 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002204 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002205 return;
2206 }
2207
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002208 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002209 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002210 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002211}
2212
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002213/* Try to send data if possible */
2214static struct task *h2_send(struct task *t, void *ctx, unsigned short state)
Willy Tarreau62f52692017-10-08 23:01:42 +02002215{
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002216 struct connection *conn = ctx;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002217 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002218 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002219
2220 if (conn->flags & CO_FL_ERROR)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002221 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002222
2223 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2224 /* a handshake was requested */
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002225 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226 }
2227
Willy Tarreaubc933932017-10-09 16:21:43 +02002228 /* This loop is quite simple : it tries to fill as much as it can from
2229 * pending streams into the existing buffer until it's reportedly full
2230 * or the end of send requests is reached. Then it tries to send this
2231 * buffer's contents out, marks it not full if at least one byte could
2232 * be sent, and tries again.
2233 *
2234 * The snd_buf() function normally takes a "flags" argument which may
2235 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2236 * data immediately comes and CO_SFL_STREAMER to indicate that the
2237 * connection is streaming lots of data (used to increase TLS record
2238 * size at the expense of latency). The former can be sent any time
2239 * there's a buffer full flag, as it indicates at least one stream
2240 * attempted to send and failed so there are pending data. An
2241 * alternative would be to set it as long as there's an active stream
2242 * but that would be problematic for ACKs until we have an absolute
2243 * guarantee that all waiters have at least one byte to send. The
2244 * latter should possibly not be set for now.
2245 */
2246
2247 done = 0;
2248 while (!done) {
2249 unsigned int flags = 0;
2250
2251 /* fill as much as we can into the current buffer */
2252 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2253 done = h2_process_mux(h2c);
2254
2255 if (conn->flags & CO_FL_ERROR)
2256 break;
2257
2258 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2259 flags |= CO_SFL_MSG_MORE;
2260
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002261 if (b_data(&h2c->mbuf)) {
2262 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002263 if (!ret)
2264 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002265 b_del(&h2c->mbuf, ret);
2266 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002267 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002268
2269 /* wrote at least one byte, the buffer is not full anymore */
2270 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2271 }
2272
Willy Tarreaua2af5122017-10-09 11:56:46 +02002273 if (conn->flags & CO_FL_SOCK_WR_SH) {
2274 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002275 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002276 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002277 /* We're not full anymore, so we can wake any task that are waiting
2278 * for us.
2279 */
2280 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2281 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2282 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2283 struct wait_list *, list);
2284 LIST_DEL(&sw->list);
2285 LIST_INIT(&sw->list);
2286 tasklet_wakeup(sw->task);
2287 }
2288
2289 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002290 /* We're done, no more to send */
2291 if (!b_data(&h2c->mbuf))
2292 return NULL;
2293schedule:
2294 if (LIST_ISEMPTY(&h2c->wait_list.list))
2295 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2296 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002297}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002298
Willy Tarreau62f52692017-10-08 23:01:42 +02002299/* callback called on any event by the connection handler.
2300 * It applies changes and returns zero, or < 0 if it wants immediate
2301 * destruction of the connection (which normally doesn not happen in h2).
2302 */
2303static int h2_wake(struct connection *conn)
2304{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002305 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002306 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002307
Olivier Houchardf495fc42018-07-20 18:15:23 +02002308 h2_send(NULL, conn, 0);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002309 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002310 h2_process_demux(h2c);
2311
2312 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002313 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002314
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002316 h2c->flags &= ~H2_CF_DEM_DFULL;
2317 }
2318
Willy Tarreau8ec14062017-12-30 18:08:13 +01002319 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2320 /* frontend is stopping, reload likely in progress, let's try
2321 * to announce a graceful shutdown if not yet done. We don't
2322 * care if it fails, it will be tried again later.
2323 */
2324 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2325 if (h2c->last_sid < 0)
2326 h2c->last_sid = (1U << 31) - 1;
2327 h2c_send_goaway_error(h2c, NULL);
2328 }
2329 }
2330
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002331 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002332 * If we received early data, and the handshake is done, wake
2333 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002334 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002335 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2336 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2337 struct eb32_node *node;
2338 struct h2s *h2s;
2339
2340 h2c->flags |= H2_CF_WAIT_FOR_HS;
2341 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2342
2343 while (node) {
2344 h2s = container_of(node, struct h2s, by_id);
2345 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2346 h2s->cs->data_cb->wake(h2s->cs);
2347 node = eb32_next(node);
2348 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002349 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002350
Willy Tarreau26bd7612017-10-09 16:47:04 +02002351 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002352 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2353 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2354 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002355 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002356
2357 if (eb_is_empty(&h2c->streams_by_id)) {
2358 /* no more stream, kill the connection now */
2359 h2_release(conn);
2360 return -1;
2361 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002362 }
2363
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002364 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002365 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002366
2367 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002368 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002369 __conn_xprt_stop_recv(conn);
2370 }
2371 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002372 __conn_xprt_want_recv(conn);
2373 }
2374
2375 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002376 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002377 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002378 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002379 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002380 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2381 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002382 __conn_xprt_want_send(conn);
2383 }
2384 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002385 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002386 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002387 }
2388
Willy Tarreau3f133572017-10-31 19:21:06 +01002389 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002390 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002391 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002392 task_queue(h2c->task);
2393 }
2394 else
2395 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002396 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002397
Willy Tarreau62f52692017-10-08 23:01:42 +02002398 return 0;
2399}
2400
Willy Tarreauea392822017-10-31 10:02:25 +01002401/* Connection timeout management. The principle is that if there's no receipt
2402 * nor sending for a certain amount of time, the connection is closed. If the
2403 * MUX buffer still has lying data or is not allocatable, the connection is
2404 * immediately killed. If it's allocatable and empty, we attempt to send a
2405 * GOAWAY frame.
2406 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002407static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002408{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002409 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002410 int expired = tick_is_expired(t->expire, now_ms);
2411
Willy Tarreau0975f112018-03-29 15:22:59 +02002412 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002413 return t;
2414
Willy Tarreau0975f112018-03-29 15:22:59 +02002415 task_delete(t);
2416 task_free(t);
2417
2418 if (!h2c) {
2419 /* resources were already deleted */
2420 return NULL;
2421 }
2422
2423 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002424 h2c_error(h2c, H2_ERR_NO_ERROR);
2425 h2_wake_some_streams(h2c, 0, 0);
2426
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002427 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002428 /* don't even try to send a GOAWAY, the buffer is stuck */
2429 h2c->flags |= H2_CF_GOAWAY_FAILED;
2430 }
2431
2432 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002433 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002434 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2435 h2c->flags |= H2_CF_GOAWAY_FAILED;
2436
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002437 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2438 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002439 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002440 b_del(&h2c->mbuf, ret);
2441 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002442 }
2443 }
Willy Tarreauea392822017-10-31 10:02:25 +01002444
Willy Tarreau0975f112018-03-29 15:22:59 +02002445 /* either we can release everything now or it will be done later once
2446 * the last stream closes.
2447 */
2448 if (eb_is_empty(&h2c->streams_by_id))
2449 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002450
Willy Tarreauea392822017-10-31 10:02:25 +01002451 return NULL;
2452}
2453
2454
Willy Tarreau62f52692017-10-08 23:01:42 +02002455/*******************************************/
2456/* functions below are used by the streams */
2457/*******************************************/
2458
2459/*
2460 * Attach a new stream to a connection
2461 * (Used for outgoing connections)
2462 */
2463static struct conn_stream *h2_attach(struct connection *conn)
2464{
2465 return NULL;
2466}
2467
2468/* callback used to update the mux's polling flags after changing a cs' status.
2469 * The caller (cs_update_mux_polling) will take care of propagating any changes
2470 * to the transport layer.
2471 */
2472static void h2_update_poll(struct conn_stream *cs)
2473{
Willy Tarreau1d393222017-10-17 10:26:19 +02002474 struct h2s *h2s = cs->ctx;
2475
2476 if (!h2s)
2477 return;
2478
Willy Tarreaud7739c82017-10-30 15:38:23 +01002479 /* we may unblock a blocked read */
2480
Willy Tarreau315d8072017-12-10 22:17:57 +01002481 if (cs->flags & CS_FL_DATA_RD_ENA) {
2482 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002483 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002484 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002485 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002486 conn_xprt_want_send(cs->conn);
2487 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002488 }
2489
Willy Tarreau1d393222017-10-17 10:26:19 +02002490 /* Note: the stream and stream-int code doesn't allow us to perform a
2491 * synchronous send() here unfortunately, because this code is called
2492 * as si_update() from the process_stream() context. This means that
2493 * we have to queue the current cs and defer its processing after the
2494 * connection's cs list is processed anyway.
2495 */
2496
2497 if (cs->flags & CS_FL_DATA_WR_ENA) {
2498 if (LIST_ISEMPTY(&h2s->list)) {
2499 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002500 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002501 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2502 conn_xprt_want_send(cs->conn);
2503 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2504 }
2505 }
2506 else if (!LIST_ISEMPTY(&h2s->list)) {
2507 LIST_DEL(&h2s->list);
2508 LIST_INIT(&h2s->list);
2509 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2510 }
2511
2512 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002513 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002514 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002515}
2516
2517/*
2518 * Detach the stream from the connection and possibly release the connection.
2519 */
2520static void h2_detach(struct conn_stream *cs)
2521{
Willy Tarreau60935142017-10-16 18:11:19 +02002522 struct h2s *h2s = cs->ctx;
2523 struct h2c *h2c;
2524
2525 cs->ctx = NULL;
2526 if (!h2s)
2527 return;
2528
2529 h2c = h2s->h2c;
2530 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002531 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002532 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2533 !h2_has_too_many_cs(h2c)) {
2534 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2535 if (h2_recv_allowed(h2c)) {
2536 __conn_xprt_want_recv(h2c->conn);
2537 conn_xprt_want_send(h2c->conn);
2538 }
2539 }
Willy Tarreau60935142017-10-16 18:11:19 +02002540
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002541 /* this stream may be blocked waiting for some data to leave (possibly
2542 * an ES or RST frame), so orphan it in this case.
2543 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002544 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002545 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002546 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002547 return;
2548
Willy Tarreau45f752e2017-10-30 15:44:59 +01002549 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2550 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2551 /* unblock the connection if it was blocked on this
2552 * stream.
2553 */
2554 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2555 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2556 conn_xprt_want_recv(cs->conn);
2557 conn_xprt_want_send(cs->conn);
2558 }
2559
Willy Tarreau71049cc2018-03-28 13:56:39 +02002560 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002561
Willy Tarreaue323f342018-03-28 13:51:45 +02002562 /* We don't want to close right now unless we're removing the
2563 * last stream, and either the connection is in error, or it
2564 * reached the ID already specified in a GOAWAY frame received
2565 * or sent (as seen by last_sid >= 0).
2566 */
2567 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2568 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002569 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002570 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002571 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002572 (conn_xprt_read0_pending(h2c->conn) ||
2573 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2574 /* no more stream will come, kill it now */
2575 h2_release(h2c->conn);
2576 }
2577 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002578 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002579 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2580 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002581 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002582 else
2583 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002584 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002585}
2586
2587static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2588{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002589 struct h2s *h2s = cs->ctx;
2590
2591 if (!mode)
2592 return;
2593
Willy Tarreau721c9742017-11-07 11:05:42 +01002594 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002595 return;
2596
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002597 /* if no outgoing data was seen on this stream, it means it was
2598 * closed with a "tcp-request content" rule that is normally
2599 * used to kill the connection ASAP (eg: limit abuse). In this
2600 * case we send a goaway to close the connection.
2601 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002602 if (!(h2s->flags & H2_SF_RST_SENT) &&
2603 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002604 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002605
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002606 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2607 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2608 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002609 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002610
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002611 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002612 conn_xprt_want_send(cs->conn);
2613
Willy Tarreau00dd0782018-03-01 16:31:34 +01002614 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002615
2616 add_to_list:
2617 if (LIST_ISEMPTY(&h2s->list)) {
2618 if (h2s->flags & H2_SF_BLK_MFCTL)
2619 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2620 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2621 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2622 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002623}
2624
2625static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2626{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002627 struct h2s *h2s = cs->ctx;
2628
Willy Tarreau721c9742017-11-07 11:05:42 +01002629 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002630 return;
2631
Willy Tarreau67434202017-11-06 20:20:51 +01002632 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002633 /* we can cleanly close using an empty data frame only after headers */
2634
2635 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2636 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002637 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002638
2639 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002640 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002641 else
2642 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002643 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002644 /* if no outgoing data was seen on this stream, it means it was
2645 * closed with a "tcp-request content" rule that is normally
2646 * used to kill the connection ASAP (eg: limit abuse). In this
2647 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002648 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002649 if (!(h2s->flags & H2_SF_RST_SENT) &&
2650 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002651 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002652
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002653 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2654 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002655 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002656 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002657
Willy Tarreau00dd0782018-03-01 16:31:34 +01002658 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002659 }
2660
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002661 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002662 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002663
2664 add_to_list:
2665 if (LIST_ISEMPTY(&h2s->list)) {
2666 if (h2s->flags & H2_SF_BLK_MFCTL)
2667 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2668 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2669 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2670 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002671}
2672
Willy Tarreau13278b42017-10-13 19:23:14 +02002673/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2674 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2675 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002676 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002677 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002678static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002679{
2680 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002681 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002682 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002683 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002684 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002685 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002686 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002687 int flen = h2c->dfl;
2688 int outlen = 0;
2689 int wrap;
2690 int try;
2691
2692 if (!h2c->dfl) {
2693 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002694 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002695 return 0;
2696 }
2697
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002698 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002699 return 0; // incomplete input frame
2700
Willy Tarreau13278b42017-10-13 19:23:14 +02002701 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002702 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002703 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002704 copy = alloc_trash_chunk();
2705 if (!copy) {
2706 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2707 goto fail;
2708 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002709 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2710 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2711 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002712 }
2713
2714 /* The padlen is the first byte before data, and the padding appears
2715 * after data. padlen+data+padding are included in flen.
2716 */
2717 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002718 h2c->dpl = *hdrs;
2719 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002720 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2721 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002722 return 0;
2723 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002724 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002725 hdrs += 1; // skip Pad Length
2726 }
2727
2728 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2729 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002730 if (read_n32(hdrs) == h2s->id) {
2731 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2732 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2733 return 0;//goto fail_stream;
2734 }
2735
Willy Tarreau13278b42017-10-13 19:23:14 +02002736 hdrs += 5; // stream dep = 4, weight = 1
2737 flen -= 5;
2738 }
2739
2740 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2741 * don't support this for now and can't even decompress so we have to
2742 * break the connection.
2743 */
2744 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2745 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002746 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002747 }
2748
Willy Tarreau937f7602018-02-26 15:22:17 +01002749 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2750 if (!csbuf) {
2751 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002752 goto fail;
2753 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002754
Willy Tarreau937f7602018-02-26 15:22:17 +01002755 /* we can't retry a failed decompression operation so we must be very
2756 * careful not to take any risks. In practice the output buffer is
2757 * always empty except maybe for trailers, in which case we simply have
2758 * to wait for the upper layer to finish consuming what is available.
2759 */
2760 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002761 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002762
Willy Tarreau937f7602018-02-26 15:22:17 +01002763 csbuf->head = 0;
2764 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002765
2766 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2767 sizeof(list)/sizeof(list[0]), tmp);
2768 if (outlen < 0) {
2769 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2770 goto fail;
2771 }
2772
2773 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002774 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002775 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002776
2777 if (outlen < 0) {
2778 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2779 goto fail;
2780 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002781
Willy Tarreau174b06a2018-04-25 18:13:58 +02002782 if (msgf & H2_MSGF_BODY) {
2783 /* a payload is present */
2784 if (msgf & H2_MSGF_BODY_CL)
2785 h2s->flags |= H2_SF_DATA_CLEN;
2786 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2787 h2s->flags |= H2_SF_DATA_CHNK;
2788 }
2789
Willy Tarreau13278b42017-10-13 19:23:14 +02002790 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002792 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002793 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002794
Willy Tarreau39d68502018-03-02 12:26:37 +01002795 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002796 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002797 h2s->cs->flags |= CS_FL_REOS;
2798 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002799
Willy Tarreau68dd9852017-07-03 14:44:26 +02002800 leave:
2801 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002802 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002803 fail:
2804 outlen = 0;
2805 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002806}
2807
Willy Tarreau454f9052017-10-26 19:40:35 +02002808/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2809 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2810 * in use, a new chunk is emitted for each frame. This is supposed to fit
2811 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2812 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2813 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2814 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002815 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2816 * checked to know if some data remain pending (an empty DATA frame can return
2817 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2818 * connection errors in h2c->errcode. The caller must already have checked the
2819 * frame header and ensured that the frame was complete or the buffer full. It
2820 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002821 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002822static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002823{
2824 struct h2c *h2c = h2s->h2c;
2825 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002826 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002827 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002828 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002829
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002830 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002831
2832 /* The padlen is the first byte before data, and the padding appears
2833 * after data. padlen+data+padding are included in flen.
2834 */
Willy Tarreau79127812017-12-03 21:06:59 +01002835 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002836 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002837 return 0;
2838
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002839 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002840 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002841 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2842 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002843 return 0;
2844 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002845
2846 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002848 h2c->dfl--;
2849 h2c->rcvd_c++; h2c->rcvd_s++;
2850 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002851 }
2852
Willy Tarreaud755ea62018-02-26 15:44:54 +01002853 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2854 if (!csbuf) {
2855 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002856 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002857 }
2858
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002859 flen = h2c->dfl - h2c->dpl;
2860 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002861 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002862
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002863 if (flen > b_data(&h2c->dbuf)) {
2864 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002865 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002866 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002867 }
2868
2869 if (unlikely(b_space_wraps(csbuf))) {
2870 /* it doesn't fit and the buffer is fragmented,
2871 * so let's defragment it and try again.
2872 */
2873 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002874 }
2875
Willy Tarreaueba10f22018-04-25 20:44:22 +02002876 /* chunked-encoding requires more room */
2877 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002878 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002879 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2880 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2881 (chklen < 1048576) ? 4 : 8;
2882 chklen += 4; // CRLF, CRLF
2883 }
2884
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002885 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002886 if (flen + chklen > b_room(csbuf)) {
2887 if (chklen >= b_room(csbuf)) {
2888 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002889 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002890 }
2891 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002892 }
2893
2894 if (h2s->flags & H2_SF_DATA_CHNK) {
2895 /* emit the chunk size */
2896 unsigned int chksz = flen;
2897 char str[10];
2898 char *beg;
2899
2900 beg = str + sizeof(str);
2901 *--beg = '\n';
2902 *--beg = '\r';
2903 do {
2904 *--beg = hextab[chksz & 0xF];
2905 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002906 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002907 }
2908
Willy Tarreau454f9052017-10-26 19:40:35 +02002909 /* Block1 is the length of the first block before the buffer wraps,
2910 * block2 is the optional second block to reach the end of the frame.
2911 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002912 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002913 if (block1 > flen)
2914 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002915 block2 = flen - block1;
2916
2917 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002918 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002919
2920 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002921 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002922
Willy Tarreaueba10f22018-04-25 20:44:22 +02002923 if (h2s->flags & H2_SF_DATA_CHNK) {
2924 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002925 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002926 }
2927
Willy Tarreau454f9052017-10-26 19:40:35 +02002928 /* now mark the input data as consumed (will be deleted from the buffer
2929 * by the caller when seeing FRAME_A after sending the window update).
2930 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002931 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002932 h2c->dfl -= flen;
2933 h2c->rcvd_c += flen;
2934 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2935
2936 if (h2c->dfl > h2c->dpl) {
2937 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002938 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002939 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002940 }
2941
Willy Tarreau4a28da12018-01-04 14:41:00 +01002942 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002943 /* here we're done with the frame, all the payload (except padding) was
2944 * transferred.
2945 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002946
2947 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2948 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002949 if (b_room(csbuf) < 5) {
2950 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002951 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002952 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002953 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002954 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002955 }
2956
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002957 h2c->rcvd_c += h2c->dpl;
2958 h2c->rcvd_s += h2c->dpl;
2959 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002960 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2961
Willy Tarreau39d68502018-03-02 12:26:37 +01002962 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002963 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002964 h2s->cs->flags |= CS_FL_REOS;
2965 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01002966
Willy Tarreau454b57b2018-02-26 15:50:05 +01002967 return flen + chklen;
2968 fail:
2969 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002970}
2971
Willy Tarreau5dd17352018-06-14 13:33:30 +02002972/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2973 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2974 * number of bytes sent. The caller must check the stream's status to detect
2975 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002976 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002977static 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 +02002978{
2979 struct http_hdr list[MAX_HTTP_HDR];
2980 struct h2c *h2c = h2s->h2c;
2981 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002982 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002983 int es_now = 0;
2984 int ret = 0;
2985 int hdr;
2986
2987 if (h2c_mux_busy(h2c, h2s)) {
2988 h2s->flags |= H2_SF_BLK_MBUSY;
2989 return 0;
2990 }
2991
Willy Tarreau44e973f2018-03-01 17:49:30 +01002992 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002993 h2c->flags |= H2_CF_MUX_MALLOC;
2994 h2s->flags |= H2_SF_BLK_MROOM;
2995 return 0;
2996 }
2997
2998 /* First, try to parse the H1 response and index it into <list>.
2999 * NOTE! Since it comes from haproxy, we *know* that a response header
3000 * block does not wrap and we can safely read it this way without
3001 * having to realign the buffer.
3002 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003003 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003004 list, sizeof(list)/sizeof(list[0]), h1m);
3005 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003006 /* incomplete or invalid response, this is abnormal coming from
3007 * haproxy and may only result in a bad errorfile or bad Lua code
3008 * so that won't be fixed, raise an error now.
3009 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003010 * FIXME: we should instead add the ability to only return a
3011 * 502 bad gateway. But in theory this is not supposed to
3012 * happen.
3013 */
3014 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3015 ret = 0;
3016 goto end;
3017 }
3018
3019 chunk_reset(&outbuf);
3020
3021 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003022 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003023 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003025
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003026 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003027 break;
3028 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003030 }
3031
3032 if (outbuf.size < 9) {
3033 h2c->flags |= H2_CF_MUX_MFULL;
3034 h2s->flags |= H2_SF_BLK_MROOM;
3035 ret = 0;
3036 goto end;
3037 }
3038
3039 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3041 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3042 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003043
3044 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003045 if (outbuf.data < outbuf.size && h1m->status == 200)
3046 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3047 else if (outbuf.data < outbuf.size && h1m->status == 304)
3048 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003049 else if (unlikely(list[0].v.len != 3)) {
3050 /* this is an unparsable response */
3051 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3052 ret = 0;
3053 goto end;
3054 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003055 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003056 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003057 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3058 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3059 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3060 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3061 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003062 }
3063 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003064 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003065 goto realign_again;
3066
3067 h2c->flags |= H2_CF_MUX_MFULL;
3068 h2s->flags |= H2_SF_BLK_MROOM;
3069 ret = 0;
3070 goto end;
3071 }
3072
3073 /* encode all headers, stop at empty name */
3074 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003075 /* these ones do not exist in H2 and must be dropped. */
3076 if (isteq(list[hdr].n, ist("connection")) ||
3077 isteq(list[hdr].n, ist("proxy-connection")) ||
3078 isteq(list[hdr].n, ist("keep-alive")) ||
3079 isteq(list[hdr].n, ist("upgrade")) ||
3080 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003081 continue;
3082
3083 if (isteq(list[hdr].n, ist("")))
3084 break; // end
3085
3086 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3087 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003088 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003089 goto realign_again;
3090
3091 h2c->flags |= H2_CF_MUX_MFULL;
3092 h2s->flags |= H2_SF_BLK_MROOM;
3093 ret = 0;
3094 goto end;
3095 }
3096 }
3097
3098 /* we may need to add END_STREAM */
3099 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3100 es_now = 1;
3101
3102 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003103 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003104
3105 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003106 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003107
3108 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003109 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110
3111 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003113 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003114
3115 /* for now we don't implemented CONTINUATION, so we wait for a
3116 * body or directly end in TRL2.
3117 */
3118 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003119 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003120 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003121
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003122 h1m->state = HTTP_MSG_DONE;
3123 h2s->flags |= H2_SF_ES_SENT;
3124 if (h2s->st == H2_SS_OPEN)
3125 h2s->st = H2_SS_HLOC;
3126 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003127 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003128 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003129 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003130 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003131 h1m->state = HTTP_MSG_RPBEFORE;
3132 h1m->status = 0;
3133 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003134 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003135 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003136 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003137 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138
3139 end:
3140 //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));
3141 return ret;
3142}
3143
Willy Tarreau5dd17352018-06-14 13:33:30 +02003144/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3145 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3146 * the number of bytes sent. The caller must check the stream's status to
3147 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003148 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003149static 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 +02003150{
3151 struct h2c *h2c = h2s->h2c;
3152 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003153 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003154 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003155 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003156 int es_now = 0;
3157 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003158 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003159 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003160
3161 if (h2c_mux_busy(h2c, h2s)) {
3162 h2s->flags |= H2_SF_BLK_MBUSY;
3163 goto end;
3164 }
3165
Willy Tarreau44e973f2018-03-01 17:49:30 +01003166 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003167 h2c->flags |= H2_CF_MUX_MALLOC;
3168 h2s->flags |= H2_SF_BLK_MROOM;
3169 goto end;
3170 }
3171
3172 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003173 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003174 goto end;
3175
3176 chunk_reset(&outbuf);
3177
3178 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003180 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003181 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003182
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003183 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003184 break;
3185 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003186 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003187 }
3188
3189 if (outbuf.size < 9) {
3190 h2c->flags |= H2_CF_MUX_MFULL;
3191 h2s->flags |= H2_SF_BLK_MROOM;
3192 goto end;
3193 }
3194
3195 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3197 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3198 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003199
3200 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3201 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003202 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003203 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003204 break;
3205 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003206 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003207 if ((long long)size > h1m->curr_len)
3208 size = h1m->curr_len;
3209 break;
3210 default: /* te:chunked : parse chunks */
3211 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003212 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003213 if (!ret)
3214 goto end;
3215
3216 if (ret < 0) {
3217 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3218 h1m->err_pos = ret;
3219 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3220 goto end;
3221 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003222 max -= ret;
3223 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003224 total += ret;
3225 h1m->state = HTTP_MSG_CHUNK_SIZE;
3226 }
3227
3228 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3229 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003230 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003231 if (!ret)
3232 goto end;
3233
3234 if (ret < 0) {
3235 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3236 h1m->err_pos = ret;
3237 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3238 goto end;
3239 }
3240
3241 size = chunk;
3242 h1m->curr_len = chunk;
3243 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003244 max -= ret;
3245 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003246 total += ret;
3247 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3248 if (!size)
3249 goto send_empty;
3250 }
3251
3252 /* in MSG_DATA state, continue below */
3253 size = h1m->curr_len;
3254 break;
3255 }
3256
3257 /* we have in <size> the exact number of bytes we need to copy from
3258 * the H1 buffer. We need to check this against the connection's and
3259 * the stream's send windows, and to ensure that this fits in the max
3260 * frame size and in the buffer's available space minus 9 bytes (for
3261 * the frame header). The connection's flow control is applied last so
3262 * that we can use a separate list of streams which are immediately
3263 * unblocked on window opening. Note: we don't implement padding.
3264 */
3265
Willy Tarreau5dd17352018-06-14 13:33:30 +02003266 if (size > max)
3267 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268
3269 if (size > h2s->mws)
3270 size = h2s->mws;
3271
3272 if (size <= 0) {
3273 h2s->flags |= H2_SF_BLK_SFCTL;
3274 goto end;
3275 }
3276
3277 if (h2c->mfs && size > h2c->mfs)
3278 size = h2c->mfs;
3279
3280 if (size + 9 > outbuf.size) {
3281 /* we have an opportunity for enlarging the too small
3282 * available space, let's try.
3283 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003284 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003285 goto realign_again;
3286 size = outbuf.size - 9;
3287 }
3288
3289 if (size <= 0) {
3290 h2c->flags |= H2_CF_MUX_MFULL;
3291 h2s->flags |= H2_SF_BLK_MROOM;
3292 goto end;
3293 }
3294
3295 if (size > h2c->mws)
3296 size = h2c->mws;
3297
3298 if (size <= 0) {
3299 h2s->flags |= H2_SF_BLK_MFCTL;
3300 goto end;
3301 }
3302
3303 /* copy whatever we can */
3304 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003305 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003306 if (ret == 1)
3307 len2 = 0;
3308
3309 if (!ret || len1 + len2 < size) {
3310 /* FIXME: must normally never happen */
3311 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3312 goto end;
3313 }
3314
3315 /* limit len1/len2 to size */
3316 if (len1 + len2 > size) {
3317 int sub = len1 + len2 - size;
3318
3319 if (len2 > sub)
3320 len2 -= sub;
3321 else {
3322 sub -= len2;
3323 len2 = 0;
3324 len1 -= sub;
3325 }
3326 }
3327
3328 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003329 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003330 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003331 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003332
3333 send_empty:
3334 /* we may need to add END_STREAM */
3335 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3336 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003337 *
3338 * FIXME: what we do here is not correct because we send end_stream
3339 * before knowing if we'll have to send a HEADERS frame for the
3340 * trailers. More importantly we're not consuming the trailing CRLF
3341 * after the end of trailers, so it will be left to the caller to
3342 * eat it. The right way to do it would be to measure trailers here
3343 * and to send ES only if there are no trailers.
3344 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003345 */
3346 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3347 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3348 es_now = 1;
3349
3350 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003352
3353 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003354 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003355
3356 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003357 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358
3359 /* consume incoming H1 response */
3360 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003361 max -= size;
3362 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363 total += size;
3364 h1m->curr_len -= size;
3365 h2s->mws -= size;
3366 h2c->mws -= size;
3367
3368 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3369 h1m->state = HTTP_MSG_CHUNK_CRLF;
3370 goto new_frame;
3371 }
3372 }
3373
3374 if (es_now) {
3375 if (h2s->st == H2_SS_OPEN)
3376 h2s->st = H2_SS_HLOC;
3377 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003378 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003379
Willy Tarreau35a62702018-02-27 15:37:25 +01003380 if (!(h1m->flags & H1_MF_CHNK)) {
3381 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003382 total += max;
3383 ofs += max;
3384 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003385
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003386 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003387 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003388
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003389 h2s->flags |= H2_SF_ES_SENT;
3390 }
3391
3392 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003393 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 +02003394 return total;
3395}
3396
Olivier Houchard6ff20392018-07-17 18:46:31 +02003397/* Called from the upper layer, to subscribe to events, such as being able to send */
3398static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3399{
3400 struct wait_list *sw;
3401 struct h2s *h2s = cs->ctx;
3402
3403 switch (event_type) {
3404 case SUB_CAN_SEND:
3405 sw = param;
3406 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3407 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3408 return 0;
3409 default:
3410 break;
3411 }
3412 return -1;
3413
3414
3415}
3416
Willy Tarreau62f52692017-10-08 23:01:42 +02003417/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003418static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003419{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003420 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003421 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003422 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003423
Willy Tarreau0bad0432018-06-14 16:54:01 +02003424 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003425 h2s->flags |= H2_SF_OUTGOING_DATA;
3426
Willy Tarreau0bad0432018-06-14 16:54:01 +02003427 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003428 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003429 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003430 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003432 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003433 }
3434 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3435 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003436 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003437
Willy Tarreau5dd17352018-06-14 13:33:30 +02003438 if (unlikely((int)ret <= 0)) {
3439 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003440 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3441 break;
3442 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003443 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003444 total += count;
3445 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003446 h2s->res.state = HTTP_MSG_DONE;
3447 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003448 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003449 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003450 cs->flags |= CS_FL_ERROR;
3451 break;
3452 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003453
3454 total += ret;
3455 count -= ret;
3456
3457 if (h2s->st >= H2_SS_ERROR)
3458 break;
3459
3460 if (h2s->flags & H2_SF_BLK_ANY)
3461 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003462 }
3463
Willy Tarreau00610962018-07-19 10:58:28 +02003464 if (h2s->st >= H2_SS_ERROR) {
3465 /* trim any possibly pending data after we close (extra CR-LF,
3466 * unprocessed trailers, abnormal extra data, ...)
3467 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003468 total += count;
3469 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003470 }
3471
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003472 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003473 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003474 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003475 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003476 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003477 }
3478
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003479 if (h2s->flags & H2_SF_BLK_SFCTL) {
3480 /* stream flow control, quit the list */
3481 LIST_DEL(&h2s->list);
3482 LIST_INIT(&h2s->list);
3483 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003484 else if (LIST_ISEMPTY(&h2s->list)) {
3485 if (h2s->flags & H2_SF_BLK_MFCTL)
3486 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003487 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003488
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003489 b_del(buf, total);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003490 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003491}
3492
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003493/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003494static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003495{
3496 struct h2c *h2c = conn->mux_ctx;
3497 struct h2s *h2s;
3498 struct eb32_node *node;
3499 int fctl_cnt = 0;
3500 int send_cnt = 0;
3501 int tree_cnt = 0;
3502 int orph_cnt = 0;
3503
3504 if (!h2c)
3505 return;
3506
3507 list_for_each_entry(h2s, &h2c->fctl_list, list)
3508 fctl_cnt++;
3509
3510 list_for_each_entry(h2s, &h2c->send_list, list)
3511 send_cnt++;
3512
3513 node = eb32_first(&h2c->streams_by_id);
3514 while (node) {
3515 h2s = container_of(node, struct h2s, by_id);
3516 tree_cnt++;
3517 if (!h2s->cs)
3518 orph_cnt++;
3519 node = eb32_next(node);
3520 }
3521
Willy Tarreau616ac812018-07-24 14:12:42 +02003522 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3523 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3524 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3525 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3526 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3527 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003528}
Willy Tarreau62f52692017-10-08 23:01:42 +02003529
3530/*******************************************************/
3531/* functions below are dedicated to the config parsers */
3532/*******************************************************/
3533
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003534/* config parser for global "tune.h2.header-table-size" */
3535static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3536 struct proxy *defpx, const char *file, int line,
3537 char **err)
3538{
3539 if (too_many_args(1, args, err, NULL))
3540 return -1;
3541
3542 h2_settings_header_table_size = atoi(args[1]);
3543 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3544 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3545 return -1;
3546 }
3547 return 0;
3548}
Willy Tarreau62f52692017-10-08 23:01:42 +02003549
Willy Tarreaue6baec02017-07-27 11:45:11 +02003550/* config parser for global "tune.h2.initial-window-size" */
3551static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3552 struct proxy *defpx, const char *file, int line,
3553 char **err)
3554{
3555 if (too_many_args(1, args, err, NULL))
3556 return -1;
3557
3558 h2_settings_initial_window_size = atoi(args[1]);
3559 if (h2_settings_initial_window_size < 0) {
3560 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3561 return -1;
3562 }
3563 return 0;
3564}
3565
Willy Tarreau5242ef82017-07-27 11:47:28 +02003566/* config parser for global "tune.h2.max-concurrent-streams" */
3567static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3568 struct proxy *defpx, const char *file, int line,
3569 char **err)
3570{
3571 if (too_many_args(1, args, err, NULL))
3572 return -1;
3573
3574 h2_settings_max_concurrent_streams = atoi(args[1]);
3575 if (h2_settings_max_concurrent_streams < 0) {
3576 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3577 return -1;
3578 }
3579 return 0;
3580}
3581
Willy Tarreau62f52692017-10-08 23:01:42 +02003582
3583/****************************************/
3584/* MUX initialization and instanciation */
3585/***************************************/
3586
3587/* The mux operations */
3588const struct mux_ops h2_ops = {
3589 .init = h2_init,
3590 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003591 .wake = h2_wake,
3592 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003593 .snd_buf = h2_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003594 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003595 .attach = h2_attach,
3596 .detach = h2_detach,
3597 .shutr = h2_shutr,
3598 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003599 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003600 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003601 .name = "H2",
3602};
3603
Christopher Faulet32f61c02018-04-10 14:33:41 +02003604/* PROTO selection : this mux registers PROTO token "h2" */
3605static struct mux_proto_list mux_proto_h2 =
3606 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003607
3608/* config keyword parsers */
3609static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003610 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003611 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003612 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003613 { 0, NULL, NULL }
3614}};
3615
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003616static void __h2_deinit(void)
3617{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003618 pool_destroy(pool_head_h2s);
3619 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003620}
3621
Willy Tarreau62f52692017-10-08 23:01:42 +02003622__attribute__((constructor))
3623static void __h2_init(void)
3624{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003625 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003626 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003627 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003628 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3629 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003630}