blob: 0ad4bac20d438e86bc269314f572edc1d0c1fd26 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
Olivier Houchard638b7992018-08-16 15:41:52 +0200186 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreau18312642017-10-11 07:57:07 +0200187};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200188
Willy Tarreauc6405142017-09-21 20:23:50 +0200189/* descriptor for an h2 frame header */
190struct h2_fh {
191 uint32_t len; /* length, host order, 24 bits */
192 uint32_t sid; /* stream id, host order, 31 bits */
193 uint8_t ft; /* frame type */
194 uint8_t ff; /* frame flags */
195};
196
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197/* a few settings from the global section */
198static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200199static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200200static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200201
Willy Tarreau2a856182017-05-16 15:20:39 +0200202/* a dmumy closed stream */
203static const struct h2s *h2_closed_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100208 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200209 .id = 0,
210};
211
212/* and a dummy idle stream for use with any unannounced stream */
213static const struct h2s *h2_idle_stream = &(const struct h2s){
214 .cs = NULL,
215 .h2c = NULL,
216 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100217 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
Olivier Houchard9f6af332018-05-25 14:04:04 +0200221static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200222static struct task *h2_send(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100223static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100224static int h2_frt_decode_headers(struct h2s *h2s);
225static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200227/*****************************************************/
228/* functions below are for dynamic buffer management */
229/*****************************************************/
230
Willy Tarreau315d8072017-12-10 22:17:57 +0100231/* indicates whether or not the we may call the h2_recv() function to attempt
232 * to receive data into the buffer and/or demux pending data. The condition is
233 * a bit complex due to some API limits for now. The rules are the following :
234 * - if an error or a shutdown was detected on the connection and the buffer
235 * is empty, we must not attempt to receive
236 * - if the demux buf failed to be allocated, we must not try to receive and
237 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100238 * - if no flag indicates a blocking condition, we may attempt to receive,
239 * regardless of whether the demux buffer is full or not, so that only
240 * de demux part decides whether or not to block. This is needed because
241 * the connection API indeed prevents us from re-enabling receipt that is
242 * already enabled in a polled state, so we must always immediately stop
243 * as soon as the demux can't proceed so as never to hit an end of read
244 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100245 * - otherwise must may not attempt
246 */
247static inline int h2_recv_allowed(const struct h2c *h2c)
248{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200249 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 (h2c->st0 >= H2_CS_ERROR ||
251 h2c->conn->flags & CO_FL_ERROR ||
252 conn_xprt_read0_pending(h2c->conn)))
253 return 0;
254
255 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100256 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100257 return 1;
258
259 return 0;
260}
261
Willy Tarreauf2101912018-07-19 10:11:38 +0200262/* returns true if the connection has too many conn_streams attached */
263static inline int h2_has_too_many_cs(const struct h2c *h2c)
264{
265 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
266}
267
Willy Tarreau44e973f2018-03-01 17:49:30 +0100268/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
269 * flags are used to figure what buffer was requested. It returns 1 if the
270 * allocation succeeds, in which case the connection is woken up, or 0 if it's
271 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200272 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100273static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200274{
275 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100276 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200279 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100280 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200281 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282 return 1;
283 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200284
Willy Tarreau44e973f2018-03-01 17:49:30 +0100285 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
286 h2c->flags &= ~H2_CF_MUX_MALLOC;
287 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
288 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200289
290 if (h2c->flags & H2_CF_DEM_MROOM) {
291 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100292 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 conn_xprt_want_recv(h2c->conn);
294 }
Willy Tarreau14398122017-09-22 14:26:04 +0200295 return 1;
296 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100297
298 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
299 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200300 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100301 h2c->flags &= ~H2_CF_DEM_SALLOC;
302 if (h2_recv_allowed(h2c))
303 conn_xprt_want_recv(h2c->conn);
304 return 1;
305 }
306
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 0;
308}
309
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200310static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200311{
312 struct buffer *buf = NULL;
313
Willy Tarreau44e973f2018-03-01 17:49:30 +0100314 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
315 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
316 h2c->buf_wait.target = h2c;
317 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100319 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100320 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200321 __conn_xprt_stop_recv(h2c->conn);
322 }
323 return buf;
324}
325
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200326static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200327{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200328 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100329 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200330 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200331 }
332}
333
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200334
Willy Tarreau62f52692017-10-08 23:01:42 +0200335/*****************************************************************/
336/* functions below are dedicated to the mux setup and management */
337/*****************************************************************/
338
Willy Tarreau32218eb2017-09-22 08:07:25 +0200339/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
340static int h2c_frt_init(struct connection *conn)
341{
342 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100343 struct task *t = NULL;
344 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200345
Willy Tarreaubafbe012017-11-24 17:34:44 +0100346 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200347 if (!h2c)
348 goto fail;
349
Willy Tarreau3f133572017-10-31 19:21:06 +0100350
Willy Tarreau599391a2017-11-24 10:16:00 +0100351 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
352 if (tick_isset(sess->fe->timeout.clientfin))
353 h2c->shut_timeout = sess->fe->timeout.clientfin;
354
Willy Tarreau33400292017-11-05 11:23:40 +0100355 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100356 if (tick_isset(h2c->timeout)) {
357 t = task_new(tid_bit);
358 if (!t)
359 goto fail;
360
361 h2c->task = t;
362 t->process = h2_timeout_task;
363 t->context = h2c;
364 t->expire = tick_add(now_ms, h2c->timeout);
365 }
Willy Tarreauea392822017-10-31 10:02:25 +0100366
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200367 h2c->wait_list.task = tasklet_new();
368 if (!h2c->wait_list.task)
369 goto fail;
370 h2c->wait_list.task->process = h2_send;
371 h2c->wait_list.task->context = conn;
372
Willy Tarreau32218eb2017-09-22 08:07:25 +0200373 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
374 if (!h2c->ddht)
375 goto fail;
376
377 /* Initialise the context. */
378 h2c->st0 = H2_CS_PREFACE;
379 h2c->conn = conn;
380 h2c->max_id = -1;
381 h2c->errcode = H2_ERR_NO_ERROR;
382 h2c->flags = H2_CF_NONE;
383 h2c->rcvd_c = 0;
384 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100385 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200386 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200387
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389 h2c->dsi = -1;
390 h2c->msi = -1;
391 h2c->last_sid = -1;
392
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200393 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200394 h2c->miw = 65535; /* mux initial window size */
395 h2c->mws = 65535; /* mux window size */
396 h2c->mfs = 16384; /* initial max frame size */
397 h2c->streams_by_id = EB_ROOT_UNIQUE;
398 LIST_INIT(&h2c->send_list);
399 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100400 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200401 conn->mux_ctx = h2c;
402
Willy Tarreau3f133572017-10-31 19:21:06 +0100403 if (t)
404 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200405 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200406 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200407 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100408
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 /* mux->wake will be called soon to complete the operation */
410 return 0;
411 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100412 if (t)
413 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200414 if (h2c->wait_list.task)
415 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100416 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417 return -1;
418}
419
Willy Tarreau62f52692017-10-08 23:01:42 +0200420/* Initialize the mux once it's attached. For outgoing connections, the context
421 * is already initialized before installing the mux, so we detect incoming
422 * connections from the fact that the context is still NULL. Returns < 0 on
423 * error.
424 */
425static int h2_init(struct connection *conn)
426{
427 if (conn->mux_ctx) {
428 /* we don't support outgoing connections for now */
429 return -1;
430 }
431
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200433}
434
Willy Tarreau2373acc2017-10-12 17:35:14 +0200435/* returns the stream associated with id <id> or NULL if not found */
436static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
437{
438 struct eb32_node *node;
439
Willy Tarreau2a856182017-05-16 15:20:39 +0200440 if (id > h2c->max_id)
441 return (struct h2s *)h2_idle_stream;
442
Willy Tarreau2373acc2017-10-12 17:35:14 +0200443 node = eb32_lookup(&h2c->streams_by_id, id);
444 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200445 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200446
447 return container_of(node, struct h2s, by_id);
448}
449
Willy Tarreau62f52692017-10-08 23:01:42 +0200450/* release function for a connection. This one should be called to free all
451 * resources allocated to the mux.
452 */
453static void h2_release(struct connection *conn)
454{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455 struct h2c *h2c = conn->mux_ctx;
456
457 LIST_DEL(&conn->list);
458
459 if (h2c) {
460 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200461
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100462 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100463 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100464 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200465
Willy Tarreau44e973f2018-03-01 17:49:30 +0100466 h2_release_buf(h2c, &h2c->dbuf);
467 h2_release_buf(h2c, &h2c->mbuf);
468
Willy Tarreauea392822017-10-31 10:02:25 +0100469 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200470 h2c->task->context = NULL;
471 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100472 h2c->task = NULL;
473 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200474 if (h2c->wait_list.task)
475 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100476
Willy Tarreaubafbe012017-11-24 17:34:44 +0100477 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200478 }
479
480 conn->mux = NULL;
481 conn->mux_ctx = NULL;
482
483 conn_stop_tracking(conn);
484 conn_full_close(conn);
485 if (conn->destroy_cb)
486 conn->destroy_cb(conn);
487 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200488}
489
490
Willy Tarreau71681172017-10-23 14:39:06 +0200491/******************************************************/
492/* functions below are for the H2 protocol processing */
493/******************************************************/
494
495/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100496static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200497{
498 return h2s ? h2s->id : 0;
499}
500
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200501/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100502static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200503{
504 if (h2c->msi < 0)
505 return 0;
506
507 if (h2c->msi == h2s_id(h2s))
508 return 0;
509
510 return 1;
511}
512
Willy Tarreau741d6df2017-10-17 08:00:59 +0200513/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100514static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200515{
516 h2c->errcode = err;
517 h2c->st0 = H2_CS_ERROR;
518}
519
Willy Tarreau2e43f082017-10-17 08:03:59 +0200520/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100521static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200522{
523 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
524 h2s->errcode = err;
525 h2s->st = H2_SS_ERROR;
526 if (h2s->cs)
527 h2s->cs->flags |= CS_FL_ERROR;
528 }
529}
530
Willy Tarreaue4820742017-07-27 13:37:23 +0200531/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200533{
534 uint8_t *out = frame;
535
536 *out = len >> 16;
537 write_n16(out + 1, len);
538}
539
Willy Tarreau54c15062017-10-10 17:10:03 +0200540/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
541 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
542 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200543 * available in the buffer's input prior to calling this function. The buffer
544 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200545 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100546static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200547 const struct buffer *b, int o)
548{
Willy Tarreau591d4452018-06-15 17:21:00 +0200549 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 +0200550}
551
Willy Tarreau1f094672017-11-20 21:27:45 +0100552static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200553{
Willy Tarreau591d4452018-06-15 17:21:00 +0200554 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200555}
556
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200558{
Willy Tarreau591d4452018-06-15 17:21:00 +0200559 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200560}
561
Willy Tarreau1f094672017-11-20 21:27:45 +0100562static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200563{
Willy Tarreau591d4452018-06-15 17:21:00 +0200564 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200565}
566
567
Willy Tarreau715d5312017-07-11 15:20:24 +0200568/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
569 * is not obvious. It turns out that H2 headers are neither aligned nor do they
570 * use regular sizes. And to add to the trouble, the buffer may wrap so each
571 * byte read must be checked. The header is formed like this :
572 *
573 * b0 b1 b2 b3 b4 b5..b8
574 * +----------+---------+--------+----+----+----------------------+
575 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
576 * +----------+---------+--------+----+----+----------------------+
577 *
578 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
579 * we get the sid properly aligned and ordered, and 16 bits of len properly
580 * ordered as well. The type and flags can be extracted using bit shifts from
581 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200582 * Returns zero if some bytes are missing, otherwise non-zero on success. The
583 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200584 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100585static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200586{
587 uint64_t w;
588
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200589 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200590 return 0;
591
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200592 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200593 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200594 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
595 h->ff = w >> 32;
596 h->ft = w >> 40;
597 h->len += w >> 48;
598 return 1;
599}
600
601/* skip the next 9 bytes corresponding to the frame header possibly parsed by
602 * h2_peek_frame_hdr() above.
603 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100604static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200605{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200606 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200607}
608
609/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100610static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200611{
612 int ret;
613
614 ret = h2_peek_frame_hdr(b, h);
615 if (ret > 0)
616 h2_skip_frame_hdr(b);
617 return ret;
618}
619
Willy Tarreau00dd0782018-03-01 16:31:34 +0100620/* marks stream <h2s> as CLOSED and decrement the number of active streams for
621 * its connection if the stream was not yet closed. Please use this exclusively
622 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100623 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100624static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100625{
626 if (h2s->st != H2_SS_CLOSED)
627 h2s->h2c->nb_streams--;
628 h2s->st = H2_SS_CLOSED;
629}
630
Willy Tarreau71049cc2018-03-28 13:56:39 +0200631/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
632static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100633{
634 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200635 LIST_DEL(&h2s->list);
636 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100637 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200638 if (b_size(&h2s->rxbuf)) {
639 b_free(&h2s->rxbuf);
640 offer_buffers(NULL, tasks_run_queue);
641 }
Willy Tarreau0a10de62018-03-01 16:27:53 +0100642 pool_free(pool_head_h2s, h2s);
643}
644
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200645/* creates a new stream <id> on the h2c connection and returns it, or NULL in
646 * case of memory allocation error.
647 */
648static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
649{
650 struct conn_stream *cs;
651 struct h2s *h2s;
652
Willy Tarreaubafbe012017-11-24 17:34:44 +0100653 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200654 if (!h2s)
655 goto out;
656
657 h2s->h2c = h2c;
658 h2s->mws = h2c->miw;
659 h2s->flags = H2_SF_NONE;
660 h2s->errcode = H2_ERR_NO_ERROR;
661 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200662 h2s->rxbuf = BUF_NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200663 h1m_init(&h2s->req);
664 h1m_init(&h2s->res);
665 h2s->by_id.key = h2s->id = id;
666 h2c->max_id = id;
667 LIST_INIT(&h2s->list);
668
669 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100670 h2c->nb_streams++;
671 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
672 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673
674 cs = cs_new(h2c->conn);
675 if (!cs)
676 goto out_close;
677
678 h2s->cs = cs;
679 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200680 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200681
682 if (stream_create_from_cs(cs) < 0)
683 goto out_free_cs;
684
685 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200686 if (h2_has_too_many_cs(h2c))
687 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200688 return h2s;
689
690 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200691 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200692 cs_free(cs);
693 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200694 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200695 h2s = NULL;
696 out:
697 return h2s;
698}
699
Willy Tarreaube5b7152017-09-25 16:25:39 +0200700/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
701 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
702 * the various settings codes.
703 */
704static int h2c_snd_settings(struct h2c *h2c)
705{
706 struct buffer *res;
707 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200708 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200709 int ret;
710
711 if (h2c_mux_busy(h2c, NULL)) {
712 h2c->flags |= H2_CF_DEM_MBUSY;
713 return 0;
714 }
715
Willy Tarreau44e973f2018-03-01 17:49:30 +0100716 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200717 if (!res) {
718 h2c->flags |= H2_CF_MUX_MALLOC;
719 h2c->flags |= H2_CF_DEM_MROOM;
720 return 0;
721 }
722
723 chunk_init(&buf, buf_data, sizeof(buf_data));
724 chunk_memcpy(&buf,
725 "\x00\x00\x00" /* length : 0 for now */
726 "\x04\x00" /* type : 4 (settings), flags : 0 */
727 "\x00\x00\x00\x00", /* stream ID : 0 */
728 9);
729
730 if (h2_settings_header_table_size != 4096) {
731 char str[6] = "\x00\x01"; /* header_table_size */
732
733 write_n32(str + 2, h2_settings_header_table_size);
734 chunk_memcat(&buf, str, 6);
735 }
736
737 if (h2_settings_initial_window_size != 65535) {
738 char str[6] = "\x00\x04"; /* initial_window_size */
739
740 write_n32(str + 2, h2_settings_initial_window_size);
741 chunk_memcat(&buf, str, 6);
742 }
743
744 if (h2_settings_max_concurrent_streams != 0) {
745 char str[6] = "\x00\x03"; /* max_concurrent_streams */
746
747 /* Note: 0 means "unlimited" for haproxy's config but not for
748 * the protocol, so never send this value!
749 */
750 write_n32(str + 2, h2_settings_max_concurrent_streams);
751 chunk_memcat(&buf, str, 6);
752 }
753
754 if (global.tune.bufsize != 16384) {
755 char str[6] = "\x00\x05"; /* max_frame_size */
756
757 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
758 * match bufsize - rewrite size, but at the moment it seems
759 * that clients don't take care of it.
760 */
761 write_n32(str + 2, global.tune.bufsize);
762 chunk_memcat(&buf, str, 6);
763 }
764
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200765 h2_set_frame_size(buf.area, buf.data - 9);
766 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767 if (unlikely(ret <= 0)) {
768 if (!ret) {
769 h2c->flags |= H2_CF_MUX_MFULL;
770 h2c->flags |= H2_CF_DEM_MROOM;
771 return 0;
772 }
773 else {
774 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
775 return 0;
776 }
777 }
778 return ret;
779}
780
Willy Tarreau52eed752017-09-22 15:05:09 +0200781/* Try to receive a connection preface, then upon success try to send our
782 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
783 * missing data. It may return an error in h2c.
784 */
785static int h2c_frt_recv_preface(struct h2c *h2c)
786{
787 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200788 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200789
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200790 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200791
792 if (unlikely(ret1 <= 0)) {
793 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
794 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
795 return 0;
796 }
797
Willy Tarreaube5b7152017-09-25 16:25:39 +0200798 ret2 = h2c_snd_settings(h2c);
799 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200800 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200801
Willy Tarreaube5b7152017-09-25 16:25:39 +0200802 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200803}
804
Willy Tarreau081d4722017-05-16 21:51:05 +0200805/* try to send a GOAWAY frame on the connection to report an error or a graceful
806 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
807 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
808 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
809 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
810 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
811 * on unrecoverable failure. It will not attempt to send one again in this last
812 * case so that it is safe to use h2c_error() to report such errors.
813 */
814static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
815{
816 struct buffer *res;
817 char str[17];
818 int ret;
819
820 if (h2c->flags & H2_CF_GOAWAY_FAILED)
821 return 1; // claim that it worked
822
823 if (h2c_mux_busy(h2c, h2s)) {
824 if (h2s)
825 h2s->flags |= H2_SF_BLK_MBUSY;
826 else
827 h2c->flags |= H2_CF_DEM_MBUSY;
828 return 0;
829 }
830
Willy Tarreau44e973f2018-03-01 17:49:30 +0100831 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200832 if (!res) {
833 h2c->flags |= H2_CF_MUX_MALLOC;
834 if (h2s)
835 h2s->flags |= H2_SF_BLK_MROOM;
836 else
837 h2c->flags |= H2_CF_DEM_MROOM;
838 return 0;
839 }
840
841 /* len: 8, type: 7, flags: none, sid: 0 */
842 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
843
844 if (h2c->last_sid < 0)
845 h2c->last_sid = h2c->max_id;
846
847 write_n32(str + 9, h2c->last_sid);
848 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200849 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200850 if (unlikely(ret <= 0)) {
851 if (!ret) {
852 h2c->flags |= H2_CF_MUX_MFULL;
853 if (h2s)
854 h2s->flags |= H2_SF_BLK_MROOM;
855 else
856 h2c->flags |= H2_CF_DEM_MROOM;
857 return 0;
858 }
859 else {
860 /* we cannot report this error using GOAWAY, so we mark
861 * it and claim a success.
862 */
863 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
864 h2c->flags |= H2_CF_GOAWAY_FAILED;
865 return 1;
866 }
867 }
868 h2c->flags |= H2_CF_GOAWAY_SENT;
869 return ret;
870}
871
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100872/* Try to send an RST_STREAM frame on the connection for the indicated stream
873 * during mux operations. This stream must be valid and cannot be closed
874 * already. h2s->id will be used for the stream ID and h2s->errcode will be
875 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
876 * not yet.
877 *
878 * Returns > 0 on success or zero if nothing was done. In case of lack of room
879 * to write the message, it subscribes the stream to future notifications.
880 */
881static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
882{
883 struct buffer *res;
884 char str[13];
885 int ret;
886
887 if (!h2s || h2s->st == H2_SS_CLOSED)
888 return 1;
889
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100890 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
891 * RST_STREAM in response to a RST_STREAM frame.
892 */
893 if (h2c->dft == H2_FT_RST_STREAM) {
894 ret = 1;
895 goto ignore;
896 }
897
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100898 if (h2c_mux_busy(h2c, h2s)) {
899 h2s->flags |= H2_SF_BLK_MBUSY;
900 return 0;
901 }
902
Willy Tarreau44e973f2018-03-01 17:49:30 +0100903 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100904 if (!res) {
905 h2c->flags |= H2_CF_MUX_MALLOC;
906 h2s->flags |= H2_SF_BLK_MROOM;
907 return 0;
908 }
909
910 /* len: 4, type: 3, flags: none */
911 memcpy(str, "\x00\x00\x04\x03\x00", 5);
912 write_n32(str + 5, h2s->id);
913 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200914 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100915
916 if (unlikely(ret <= 0)) {
917 if (!ret) {
918 h2c->flags |= H2_CF_MUX_MFULL;
919 h2s->flags |= H2_SF_BLK_MROOM;
920 return 0;
921 }
922 else {
923 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
924 return 0;
925 }
926 }
927
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100928 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100929 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100930 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931 return ret;
932}
933
934/* Try to send an RST_STREAM frame on the connection for the stream being
935 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
936 * error code unless the stream's state already is IDLE or CLOSED in which
937 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
938 * it was not yet.
939 *
940 * Returns > 0 on success or zero if nothing was done. In case of lack of room
941 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200942 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200944 */
945static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
946{
947 struct buffer *res;
948 char str[13];
949 int ret;
950
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100951 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
952 * RST_STREAM in response to a RST_STREAM frame.
953 */
954 if (h2c->dft == H2_FT_RST_STREAM) {
955 ret = 1;
956 goto ignore;
957 }
958
Willy Tarreau27a84c92017-10-17 08:10:17 +0200959 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100960 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200961 return 0;
962 }
963
Willy Tarreau44e973f2018-03-01 17:49:30 +0100964 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200965 if (!res) {
966 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100967 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200968 return 0;
969 }
970
971 /* len: 4, type: 3, flags: none */
972 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100973
Willy Tarreau27a84c92017-10-17 08:10:17 +0200974 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100975 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200976 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200977 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100978
Willy Tarreau27a84c92017-10-17 08:10:17 +0200979 if (unlikely(ret <= 0)) {
980 if (!ret) {
981 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100982 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200983 return 0;
984 }
985 else {
986 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
987 return 0;
988 }
989 }
990
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100991 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100992 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200993 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100994 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100995 }
996
Willy Tarreau27a84c92017-10-17 08:10:17 +0200997 return ret;
998}
999
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001000/* try to send an empty DATA frame with the ES flag set to notify about the
1001 * end of stream and match a shutdown(write). If an ES was already sent as
1002 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1003 * on success or zero if nothing was done. In case of lack of room to write the
1004 * message, it subscribes the requesting stream to future notifications.
1005 */
1006static int h2_send_empty_data_es(struct h2s *h2s)
1007{
1008 struct h2c *h2c = h2s->h2c;
1009 struct buffer *res;
1010 char str[9];
1011 int ret;
1012
Willy Tarreau721c9742017-11-07 11:05:42 +01001013 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001014 return 1;
1015
1016 if (h2c_mux_busy(h2c, h2s)) {
1017 h2s->flags |= H2_SF_BLK_MBUSY;
1018 return 0;
1019 }
1020
Willy Tarreau44e973f2018-03-01 17:49:30 +01001021 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001022 if (!res) {
1023 h2c->flags |= H2_CF_MUX_MALLOC;
1024 h2s->flags |= H2_SF_BLK_MROOM;
1025 return 0;
1026 }
1027
1028 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1029 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1030 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001031 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001032 if (likely(ret > 0)) {
1033 h2s->flags |= H2_SF_ES_SENT;
1034 }
1035 else if (!ret) {
1036 h2c->flags |= H2_CF_MUX_MFULL;
1037 h2s->flags |= H2_SF_BLK_MROOM;
1038 return 0;
1039 }
1040 else {
1041 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1042 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001043 }
1044 return ret;
1045}
1046
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001047/* wake the streams attached to the connection, whose id is greater than <last>,
1048 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1049 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1050 * stream's state is automatically updated accordingly.
1051 */
1052static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1053{
1054 struct eb32_node *node;
1055 struct h2s *h2s;
1056
1057 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1058 flags |= CS_FL_ERROR;
1059
1060 if (conn_xprt_read0_pending(h2c->conn))
1061 flags |= CS_FL_EOS;
1062
1063 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1064 while (node) {
1065 h2s = container_of(node, struct h2s, by_id);
1066 if (h2s->id <= last)
1067 break;
1068 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001069
1070 if (!h2s->cs) {
1071 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001072 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001073 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001074 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001075
1076 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001077 h2s->cs->data_cb->wake(h2s->cs);
1078
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001079 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1080 h2s->st = H2_SS_ERROR;
1081 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1082 h2s->st = H2_SS_HREM;
1083 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001084 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001085 }
1086}
1087
Willy Tarreau3421aba2017-07-27 15:41:03 +02001088/* Increase all streams' outgoing window size by the difference passed in
1089 * argument. This is needed upon receipt of the settings frame if the initial
1090 * window size is different. The difference may be negative and the resulting
1091 * window size as well, for the time it takes to receive some window updates.
1092 */
1093static void h2c_update_all_ws(struct h2c *h2c, int diff)
1094{
1095 struct h2s *h2s;
1096 struct eb32_node *node;
1097
1098 if (!diff)
1099 return;
1100
1101 node = eb32_first(&h2c->streams_by_id);
1102 while (node) {
1103 h2s = container_of(node, struct h2s, by_id);
1104 h2s->mws += diff;
1105 node = eb32_next(node);
1106 }
1107}
1108
1109/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1110 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1111 * return an error in h2c. Described in RFC7540#6.5.
1112 */
1113static int h2c_handle_settings(struct h2c *h2c)
1114{
1115 unsigned int offset;
1116 int error;
1117
1118 if (h2c->dff & H2_F_SETTINGS_ACK) {
1119 if (h2c->dfl) {
1120 error = H2_ERR_FRAME_SIZE_ERROR;
1121 goto fail;
1122 }
1123 return 1;
1124 }
1125
1126 if (h2c->dsi != 0) {
1127 error = H2_ERR_PROTOCOL_ERROR;
1128 goto fail;
1129 }
1130
1131 if (h2c->dfl % 6) {
1132 error = H2_ERR_FRAME_SIZE_ERROR;
1133 goto fail;
1134 }
1135
1136 /* that's the limit we can process */
1137 if (h2c->dfl > global.tune.bufsize) {
1138 error = H2_ERR_FRAME_SIZE_ERROR;
1139 goto fail;
1140 }
1141
1142 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001143 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001144 return 0;
1145
1146 /* parse the frame */
1147 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001148 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1149 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001150
1151 switch (type) {
1152 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1153 /* we need to update all existing streams with the
1154 * difference from the previous iws.
1155 */
1156 if (arg < 0) { // RFC7540#6.5.2
1157 error = H2_ERR_FLOW_CONTROL_ERROR;
1158 goto fail;
1159 }
1160 h2c_update_all_ws(h2c, arg - h2c->miw);
1161 h2c->miw = arg;
1162 break;
1163 case H2_SETTINGS_MAX_FRAME_SIZE:
1164 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1165 error = H2_ERR_PROTOCOL_ERROR;
1166 goto fail;
1167 }
1168 h2c->mfs = arg;
1169 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001170 case H2_SETTINGS_ENABLE_PUSH:
1171 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1172 error = H2_ERR_PROTOCOL_ERROR;
1173 goto fail;
1174 }
1175 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001176 }
1177 }
1178
1179 /* need to ACK this frame now */
1180 h2c->st0 = H2_CS_FRAME_A;
1181 return 1;
1182 fail:
1183 h2c_error(h2c, error);
1184 return 0;
1185}
1186
1187/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1188 * success or one of the h2_status values.
1189 */
1190static int h2c_ack_settings(struct h2c *h2c)
1191{
1192 struct buffer *res;
1193 char str[9];
1194 int ret = -1;
1195
1196 if (h2c_mux_busy(h2c, NULL)) {
1197 h2c->flags |= H2_CF_DEM_MBUSY;
1198 return 0;
1199 }
1200
Willy Tarreau44e973f2018-03-01 17:49:30 +01001201 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001202 if (!res) {
1203 h2c->flags |= H2_CF_MUX_MALLOC;
1204 h2c->flags |= H2_CF_DEM_MROOM;
1205 return 0;
1206 }
1207
1208 memcpy(str,
1209 "\x00\x00\x00" /* length : 0 (no data) */
1210 "\x04" "\x01" /* type : 4, flags : ACK */
1211 "\x00\x00\x00\x00" /* stream ID */, 9);
1212
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001213 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001214 if (unlikely(ret <= 0)) {
1215 if (!ret) {
1216 h2c->flags |= H2_CF_MUX_MFULL;
1217 h2c->flags |= H2_CF_DEM_MROOM;
1218 return 0;
1219 }
1220 else {
1221 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1222 return 0;
1223 }
1224 }
1225 return ret;
1226}
1227
Willy Tarreaucf68c782017-10-10 17:11:41 +02001228/* processes a PING frame and schedules an ACK if needed. The caller must pass
1229 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1230 * missing data. It may return an error in h2c.
1231 */
1232static int h2c_handle_ping(struct h2c *h2c)
1233{
1234 /* frame length must be exactly 8 */
1235 if (h2c->dfl != 8) {
1236 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1237 return 0;
1238 }
1239
1240 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001241 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001242 h2c->st0 = H2_CS_FRAME_A;
1243 return 1;
1244}
1245
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001246/* Try to send a window update for stream id <sid> and value <increment>.
1247 * Returns > 0 on success or zero on missing room or failure. It may return an
1248 * error in h2c.
1249 */
1250static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1251{
1252 struct buffer *res;
1253 char str[13];
1254 int ret = -1;
1255
1256 if (h2c_mux_busy(h2c, NULL)) {
1257 h2c->flags |= H2_CF_DEM_MBUSY;
1258 return 0;
1259 }
1260
Willy Tarreau44e973f2018-03-01 17:49:30 +01001261 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001262 if (!res) {
1263 h2c->flags |= H2_CF_MUX_MALLOC;
1264 h2c->flags |= H2_CF_DEM_MROOM;
1265 return 0;
1266 }
1267
1268 /* length: 4, type: 8, flags: none */
1269 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1270 write_n32(str + 5, sid);
1271 write_n32(str + 9, increment);
1272
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001273 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001274
1275 if (unlikely(ret <= 0)) {
1276 if (!ret) {
1277 h2c->flags |= H2_CF_MUX_MFULL;
1278 h2c->flags |= H2_CF_DEM_MROOM;
1279 return 0;
1280 }
1281 else {
1282 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1283 return 0;
1284 }
1285 }
1286 return ret;
1287}
1288
1289/* try to send pending window update for the connection. It's safe to call it
1290 * with no pending updates. Returns > 0 on success or zero on missing room or
1291 * failure. It may return an error in h2c.
1292 */
1293static int h2c_send_conn_wu(struct h2c *h2c)
1294{
1295 int ret = 1;
1296
1297 if (h2c->rcvd_c <= 0)
1298 return 1;
1299
1300 /* send WU for the connection */
1301 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1302 if (ret > 0)
1303 h2c->rcvd_c = 0;
1304
1305 return ret;
1306}
1307
1308/* try to send pending window update for the current dmux stream. It's safe to
1309 * call it with no pending updates. Returns > 0 on success or zero on missing
1310 * room or failure. It may return an error in h2c.
1311 */
1312static int h2c_send_strm_wu(struct h2c *h2c)
1313{
1314 int ret = 1;
1315
1316 if (h2c->rcvd_s <= 0)
1317 return 1;
1318
1319 /* send WU for the stream */
1320 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1321 if (ret > 0)
1322 h2c->rcvd_s = 0;
1323
1324 return ret;
1325}
1326
Willy Tarreaucf68c782017-10-10 17:11:41 +02001327/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1328 * success, 0 on missing data or one of the h2_status values.
1329 */
1330static int h2c_ack_ping(struct h2c *h2c)
1331{
1332 struct buffer *res;
1333 char str[17];
1334 int ret = -1;
1335
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001336 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001337 return 0;
1338
1339 if (h2c_mux_busy(h2c, NULL)) {
1340 h2c->flags |= H2_CF_DEM_MBUSY;
1341 return 0;
1342 }
1343
Willy Tarreau44e973f2018-03-01 17:49:30 +01001344 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001345 if (!res) {
1346 h2c->flags |= H2_CF_MUX_MALLOC;
1347 h2c->flags |= H2_CF_DEM_MROOM;
1348 return 0;
1349 }
1350
1351 memcpy(str,
1352 "\x00\x00\x08" /* length : 8 (same payload) */
1353 "\x06" "\x01" /* type : 6, flags : ACK */
1354 "\x00\x00\x00\x00" /* stream ID */, 9);
1355
1356 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001357 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001358
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001359 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001360 if (unlikely(ret <= 0)) {
1361 if (!ret) {
1362 h2c->flags |= H2_CF_MUX_MFULL;
1363 h2c->flags |= H2_CF_DEM_MROOM;
1364 return 0;
1365 }
1366 else {
1367 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1368 return 0;
1369 }
1370 }
1371 return ret;
1372}
1373
Willy Tarreau26f95952017-07-27 17:18:30 +02001374/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1375 * Returns > 0 on success or zero on missing data. It may return an error in
1376 * h2c or h2s. Described in RFC7540#6.9.
1377 */
1378static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1379{
1380 int32_t inc;
1381 int error;
1382
1383 if (h2c->dfl != 4) {
1384 error = H2_ERR_FRAME_SIZE_ERROR;
1385 goto conn_err;
1386 }
1387
1388 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001389 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001390 return 0;
1391
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001392 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001393
1394 if (h2c->dsi != 0) {
1395 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001396
1397 /* it's not an error to receive WU on a closed stream */
1398 if (h2s->st == H2_SS_CLOSED)
1399 return 1;
1400
1401 if (!inc) {
1402 error = H2_ERR_PROTOCOL_ERROR;
1403 goto strm_err;
1404 }
1405
1406 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1407 error = H2_ERR_FLOW_CONTROL_ERROR;
1408 goto strm_err;
1409 }
1410
1411 h2s->mws += inc;
1412 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1413 h2s->flags &= ~H2_SF_BLK_SFCTL;
1414 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1415 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1416 /* This stream wanted to send but could not due to its
1417 * own flow control. We can put it back into the send
1418 * list now, it will be handled upon next send() call.
1419 */
1420 LIST_ADDQ(&h2c->send_list, &h2s->list);
1421 }
1422 }
1423 }
1424 else {
1425 /* connection window update */
1426 if (!inc) {
1427 error = H2_ERR_PROTOCOL_ERROR;
1428 goto conn_err;
1429 }
1430
1431 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1432 error = H2_ERR_FLOW_CONTROL_ERROR;
1433 goto conn_err;
1434 }
1435
1436 h2c->mws += inc;
1437 }
1438
1439 return 1;
1440
1441 conn_err:
1442 h2c_error(h2c, error);
1443 return 0;
1444
1445 strm_err:
1446 if (h2s) {
1447 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001448 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001449 }
1450 else
1451 h2c_error(h2c, error);
1452 return 0;
1453}
1454
Willy Tarreaue96b0922017-10-30 00:28:29 +01001455/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1456 * the last ID. Returns > 0 on success or zero on missing data. It may return
1457 * an error in h2c. Described in RFC7540#6.8.
1458 */
1459static int h2c_handle_goaway(struct h2c *h2c)
1460{
1461 int error;
1462 int last;
1463
1464 if (h2c->dsi != 0) {
1465 error = H2_ERR_PROTOCOL_ERROR;
1466 goto conn_err;
1467 }
1468
1469 if (h2c->dfl < 8) {
1470 error = H2_ERR_FRAME_SIZE_ERROR;
1471 goto conn_err;
1472 }
1473
1474 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001475 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001476 return 0;
1477
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001478 last = h2_get_n32(&h2c->dbuf, 0);
1479 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001480 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001481 if (h2c->last_sid < 0)
1482 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001483 return 1;
1484
1485 conn_err:
1486 h2c_error(h2c, error);
1487 return 0;
1488}
1489
Willy Tarreau92153fc2017-12-03 19:46:19 +01001490/* processes a PRIORITY frame, and either skips it or rejects if it is
1491 * invalid. Returns > 0 on success or zero on missing data. It may return
1492 * an error in h2c. Described in RFC7540#6.3.
1493 */
1494static int h2c_handle_priority(struct h2c *h2c)
1495{
1496 int error;
1497
1498 if (h2c->dsi == 0) {
1499 error = H2_ERR_PROTOCOL_ERROR;
1500 goto conn_err;
1501 }
1502
1503 if (h2c->dfl != 5) {
1504 error = H2_ERR_FRAME_SIZE_ERROR;
1505 goto conn_err;
1506 }
1507
1508 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001509 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001510 return 0;
1511
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001512 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001513 /* 7540#5.3 : can't depend on itself */
1514 error = H2_ERR_PROTOCOL_ERROR;
1515 goto conn_err;
1516 }
1517 return 1;
1518
1519 conn_err:
1520 h2c_error(h2c, error);
1521 return 0;
1522}
1523
Willy Tarreaucd234e92017-08-18 10:59:39 +02001524/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1525 * Returns > 0 on success or zero on missing data. It may return an error in
1526 * h2c. Described in RFC7540#6.4.
1527 */
1528static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1529{
1530 int error;
1531
1532 if (h2c->dsi == 0) {
1533 error = H2_ERR_PROTOCOL_ERROR;
1534 goto conn_err;
1535 }
1536
Willy Tarreaucd234e92017-08-18 10:59:39 +02001537 if (h2c->dfl != 4) {
1538 error = H2_ERR_FRAME_SIZE_ERROR;
1539 goto conn_err;
1540 }
1541
1542 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001543 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001544 return 0;
1545
1546 /* late RST, already handled */
1547 if (h2s->st == H2_SS_CLOSED)
1548 return 1;
1549
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001550 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001551 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001552
1553 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001554 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001555 h2s->cs->data_cb->wake(h2s->cs);
1556 }
1557
1558 h2s->flags |= H2_SF_RST_RCVD;
1559 return 1;
1560
1561 conn_err:
1562 h2c_error(h2c, error);
1563 return 0;
1564}
1565
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001566/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1567 * It may return an error in h2c or h2s. The caller must consider that the
1568 * return value is the new h2s in case one was allocated (most common case).
1569 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001570 * errors here are reported as connection errors since it's impossible to
1571 * recover from such errors after the compression context has been altered.
1572 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001573static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001574{
1575 int error;
1576
1577 if (!h2c->dfl) {
1578 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1579 goto strm_err;
1580 }
1581
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001582 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001583 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001584
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001585 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001586 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001587
Willy Tarreauf2101912018-07-19 10:11:38 +02001588 if (h2c->flags & H2_CF_DEM_TOOMANY)
1589 return 0; // too many cs still present
1590
Willy Tarreau13278b42017-10-13 19:23:14 +02001591 /* now either the frame is complete or the buffer is complete */
1592 if (h2s->st != H2_SS_IDLE) {
1593 /* FIXME: stream already exists, this is only allowed for
1594 * trailers (not supported for now).
1595 */
1596 error = H2_ERR_PROTOCOL_ERROR;
1597 goto conn_err;
1598 }
1599 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1600 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1601 error = H2_ERR_PROTOCOL_ERROR;
1602 goto conn_err;
1603 }
1604
1605 h2s = h2c_stream_new(h2c, h2c->dsi);
1606 if (!h2s) {
1607 error = H2_ERR_INTERNAL_ERROR;
1608 goto conn_err;
1609 }
1610
1611 h2s->st = H2_SS_OPEN;
1612 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1613 h2s->st = H2_SS_HREM;
1614 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001615 /* note: cs cannot be null for now (just created above) */
1616 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001617 }
1618
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001619 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001620 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001621
Willy Tarreau8f650c32017-11-21 19:36:21 +01001622 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001623 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001624
Willy Tarreau721c9742017-11-07 11:05:42 +01001625 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001626 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001627 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001628 }
1629 else {
1630 /* update the max stream ID if the request is being processed */
1631 if (h2s->id > h2c->max_id)
1632 h2c->max_id = h2s->id;
1633 }
1634
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001635 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001636
1637 conn_err:
1638 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001639 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001640
1641 strm_err:
1642 if (h2s) {
1643 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001644 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001645 }
1646 else
1647 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001648 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001649}
1650
Willy Tarreau454f9052017-10-26 19:40:35 +02001651/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1652 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1653 */
1654static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1655{
1656 int error;
1657
1658 /* note that empty DATA frames are perfectly valid and sometimes used
1659 * to signal an end of stream (with the ES flag).
1660 */
1661
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001662 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001663 return 0; // empty buffer
1664
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001665 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001666 return 0; // incomplete frame
1667
1668 /* now either the frame is complete or the buffer is complete */
1669
1670 if (!h2c->dsi) {
1671 /* RFC7540#6.1 */
1672 error = H2_ERR_PROTOCOL_ERROR;
1673 goto conn_err;
1674 }
1675
1676 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1677 /* RFC7540#6.1 */
1678 error = H2_ERR_STREAM_CLOSED;
1679 goto strm_err;
1680 }
1681
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001682 if (!h2_frt_transfer_data(h2s))
1683 return 0;
1684
Willy Tarreau454f9052017-10-26 19:40:35 +02001685 /* call the upper layers to process the frame, then let the upper layer
1686 * notify the stream about any change.
1687 */
1688 if (!h2s->cs) {
1689 error = H2_ERR_STREAM_CLOSED;
1690 goto strm_err;
1691 }
1692
Willy Tarreau8f650c32017-11-21 19:36:21 +01001693 if (h2c->st0 >= H2_CS_ERROR)
1694 return 0;
1695
Willy Tarreau721c9742017-11-07 11:05:42 +01001696 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001697 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001698 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001699 }
1700
1701 /* check for completion : the callee will change this to FRAME_A or
1702 * FRAME_H once done.
1703 */
1704 if (h2c->st0 == H2_CS_FRAME_P)
1705 return 0;
1706
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001707
1708 /* last frame */
1709 if (h2c->dff & H2_F_DATA_END_STREAM) {
1710 h2s->st = H2_SS_HREM;
1711 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001712 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001713 }
1714
Willy Tarreau454f9052017-10-26 19:40:35 +02001715 return 1;
1716
1717 conn_err:
1718 h2c_error(h2c, error);
1719 return 0;
1720
1721 strm_err:
1722 if (h2s) {
1723 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001724 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001725 }
1726 else
1727 h2c_error(h2c, error);
1728 return 0;
1729}
1730
Willy Tarreaubc933932017-10-09 16:21:43 +02001731/* process Rx frames to be demultiplexed */
1732static void h2_process_demux(struct h2c *h2c)
1733{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001734 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001735
Willy Tarreau081d4722017-05-16 21:51:05 +02001736 if (h2c->st0 >= H2_CS_ERROR)
1737 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001738
1739 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1740 if (h2c->st0 == H2_CS_PREFACE) {
1741 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1742 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1743 if (h2c->st0 == H2_CS_ERROR)
1744 h2c->st0 = H2_CS_ERROR2;
1745 goto fail;
1746 }
1747
1748 h2c->max_id = 0;
1749 h2c->st0 = H2_CS_SETTINGS1;
1750 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001751
1752 if (h2c->st0 == H2_CS_SETTINGS1) {
1753 struct h2_fh hdr;
1754
1755 /* ensure that what is pending is a valid SETTINGS frame
1756 * without an ACK.
1757 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001758 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001759 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1760 if (h2c->st0 == H2_CS_ERROR)
1761 h2c->st0 = H2_CS_ERROR2;
1762 goto fail;
1763 }
1764
1765 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1766 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1767 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1768 h2c->st0 = H2_CS_ERROR2;
1769 goto fail;
1770 }
1771
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001772 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001773 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1774 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1775 h2c->st0 = H2_CS_ERROR2;
1776 goto fail;
1777 }
1778
1779 /* that's OK, switch to FRAME_P to process it */
1780 h2c->dfl = hdr.len;
1781 h2c->dsi = hdr.sid;
1782 h2c->dft = hdr.ft;
1783 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001784 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001785 h2c->st0 = H2_CS_FRAME_P;
1786 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001787 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001788
1789 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001790 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001791 int ret = 0;
1792
1793 if (h2c->st0 >= H2_CS_ERROR)
1794 break;
1795
1796 if (h2c->st0 == H2_CS_FRAME_H) {
1797 struct h2_fh hdr;
1798
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001799 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001800 break;
1801
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001802 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001803 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1804 h2c->st0 = H2_CS_ERROR;
1805 break;
1806 }
1807
1808 h2c->dfl = hdr.len;
1809 h2c->dsi = hdr.sid;
1810 h2c->dft = hdr.ft;
1811 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001812 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001813 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001814 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001815 }
1816
1817 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001818 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1819
Olivier Houchard638b7992018-08-16 15:41:52 +02001820 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001821 /* we may have to signal the upper layers */
1822 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001823 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1824 /* cs has just been destroyed, we have to kill h2s. */
1825 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1826 goto strm_err;
1827 }
1828
1829 if (h2c->st0 >= H2_CS_ERROR)
1830 goto strm_err;
1831
1832 if (h2s->st >= H2_SS_ERROR) {
1833 /* stream error : send RST_STREAM */
1834 h2c->st0 = H2_CS_FRAME_E;
1835 }
1836 }
1837 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001838
Willy Tarreaud7901432017-12-29 11:34:40 +01001839 if (h2c->st0 == H2_CS_FRAME_E)
1840 goto strm_err;
1841
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001842 if (h2s->st == H2_SS_IDLE &&
1843 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1844 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1845 * this state MUST be treated as a connection error
1846 */
1847 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1848 h2c->st0 = H2_CS_ERROR;
1849 break;
1850 }
1851
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001852 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1853 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1854 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1855 * this state MUST be treated as a stream error
1856 */
1857 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1858 goto strm_err;
1859 }
1860
Willy Tarreauab837502017-12-27 15:07:30 +01001861 /* Below the management of frames received in closed state is a
1862 * bit hackish because the spec makes strong differences between
1863 * streams closed by receiving RST, sending RST, and seeing ES
1864 * in both directions. In addition to this, the creation of a
1865 * new stream reusing the identifier of a closed one will be
1866 * detected here. Given that we cannot keep track of all closed
1867 * streams forever, we consider that unknown closed streams were
1868 * closed on RST received, which allows us to respond with an
1869 * RST without breaking the connection (eg: to abort a transfer).
1870 * Some frames have to be silently ignored as well.
1871 */
1872 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1873 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1874 /* #5.1.1: The identifier of a newly
1875 * established stream MUST be numerically
1876 * greater than all streams that the initiating
1877 * endpoint has opened or reserved. This
1878 * governs streams that are opened using a
1879 * HEADERS frame and streams that are reserved
1880 * using PUSH_PROMISE. An endpoint that
1881 * receives an unexpected stream identifier
1882 * MUST respond with a connection error.
1883 */
1884 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1885 goto strm_err;
1886 }
1887
1888 if (h2s->flags & H2_SF_RST_RCVD) {
1889 /* RFC7540#5.1:closed: an endpoint that
1890 * receives any frame other than PRIORITY after
1891 * receiving a RST_STREAM MUST treat that as a
1892 * stream error of type STREAM_CLOSED.
1893 *
1894 * Note that old streams fall into this category
1895 * and will lead to an RST being sent.
1896 */
1897 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1898 h2c->st0 = H2_CS_FRAME_E;
1899 goto strm_err;
1900 }
1901
1902 /* RFC7540#5.1:closed: if this state is reached as a
1903 * result of sending a RST_STREAM frame, the peer that
1904 * receives the RST_STREAM might have already sent
1905 * frames on the stream that cannot be withdrawn. An
1906 * endpoint MUST ignore frames that it receives on
1907 * closed streams after it has sent a RST_STREAM
1908 * frame. An endpoint MAY choose to limit the period
1909 * over which it ignores frames and treat frames that
1910 * arrive after this time as being in error.
1911 */
1912 if (!(h2s->flags & H2_SF_RST_SENT)) {
1913 /* RFC7540#5.1:closed: any frame other than
1914 * PRIO/WU/RST in this state MUST be treated as
1915 * a connection error
1916 */
1917 if (h2c->dft != H2_FT_RST_STREAM &&
1918 h2c->dft != H2_FT_PRIORITY &&
1919 h2c->dft != H2_FT_WINDOW_UPDATE) {
1920 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1921 goto strm_err;
1922 }
1923 }
1924 }
1925
Willy Tarreauc0da1962017-10-30 18:38:00 +01001926#if 0
1927 // problem below: it is not possible to completely ignore such
1928 // streams as we need to maintain the compression state as well
1929 // and for this we need to completely process these frames (eg:
1930 // HEADERS frames) as well as counting DATA frames to emit
1931 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1932 // This is a typical case of layer violation where the
1933 // transported contents are critical to the connection's
1934 // validity and must be ignored at the same time :-(
1935
1936 /* graceful shutdown, ignore streams whose ID is higher than
1937 * the one advertised in GOAWAY. RFC7540#6.8.
1938 */
1939 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001940 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1941 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001942 h2c->dfl -= ret;
1943 ret = h2c->dfl == 0;
1944 goto strm_err;
1945 }
1946#endif
1947
Willy Tarreau7e98c052017-10-10 15:56:59 +02001948 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001949 case H2_FT_SETTINGS:
1950 if (h2c->st0 == H2_CS_FRAME_P)
1951 ret = h2c_handle_settings(h2c);
1952
1953 if (h2c->st0 == H2_CS_FRAME_A)
1954 ret = h2c_ack_settings(h2c);
1955 break;
1956
Willy Tarreaucf68c782017-10-10 17:11:41 +02001957 case H2_FT_PING:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_handle_ping(h2c);
1960
1961 if (h2c->st0 == H2_CS_FRAME_A)
1962 ret = h2c_ack_ping(h2c);
1963 break;
1964
Willy Tarreau26f95952017-07-27 17:18:30 +02001965 case H2_FT_WINDOW_UPDATE:
1966 if (h2c->st0 == H2_CS_FRAME_P)
1967 ret = h2c_handle_window_update(h2c, h2s);
1968 break;
1969
Willy Tarreau61290ec2017-10-17 08:19:21 +02001970 case H2_FT_CONTINUATION:
1971 /* we currently don't support CONTINUATION frames since
1972 * we have nowhere to store the partial HEADERS frame.
1973 * Let's abort the stream on an INTERNAL_ERROR here.
1974 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001975 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001976 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001977 h2c->st0 = H2_CS_FRAME_E;
1978 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001979 break;
1980
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001982 if (h2c->st0 == H2_CS_FRAME_P) {
1983 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
1984 if (tmp_h2s) {
1985 h2s = tmp_h2s;
1986 ret = 1;
1987 }
1988 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 break;
1990
Willy Tarreau454f9052017-10-26 19:40:35 +02001991 case H2_FT_DATA:
1992 if (h2c->st0 == H2_CS_FRAME_P)
1993 ret = h2c_frt_handle_data(h2c, h2s);
1994
1995 if (h2c->st0 == H2_CS_FRAME_A)
1996 ret = h2c_send_strm_wu(h2c);
1997 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001998
Willy Tarreau92153fc2017-12-03 19:46:19 +01001999 case H2_FT_PRIORITY:
2000 if (h2c->st0 == H2_CS_FRAME_P)
2001 ret = h2c_handle_priority(h2c);
2002 break;
2003
Willy Tarreaucd234e92017-08-18 10:59:39 +02002004 case H2_FT_RST_STREAM:
2005 if (h2c->st0 == H2_CS_FRAME_P)
2006 ret = h2c_handle_rst_stream(h2c, h2s);
2007 break;
2008
Willy Tarreaue96b0922017-10-30 00:28:29 +01002009 case H2_FT_GOAWAY:
2010 if (h2c->st0 == H2_CS_FRAME_P)
2011 ret = h2c_handle_goaway(h2c);
2012 break;
2013
Willy Tarreau1c661982017-10-30 13:52:01 +01002014 case H2_FT_PUSH_PROMISE:
2015 /* not permitted here, RFC7540#5.1 */
2016 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002017 break;
2018
2019 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002020 default:
2021 /* drop frames that we ignore. They may be larger than
2022 * the buffer so we drain all of their contents until
2023 * we reach the end.
2024 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002025 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2026 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002027 h2c->dfl -= ret;
2028 ret = h2c->dfl == 0;
2029 }
2030
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002031 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002032 /* We may have to send an RST if not done yet */
2033 if (h2s->st == H2_SS_ERROR)
2034 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002035
Willy Tarreaua20a5192017-12-27 11:02:06 +01002036 if (h2c->st0 == H2_CS_FRAME_E)
2037 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002038
Willy Tarreau7e98c052017-10-10 15:56:59 +02002039 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002040 if (ret <= 0) {
2041 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002042 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002043 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002044
2045 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002046 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002047 h2c->st0 = H2_CS_FRAME_H;
2048 }
2049 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002050
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002051 if (h2c->rcvd_c > 0 &&
2052 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2053 h2c_send_conn_wu(h2c);
2054
Willy Tarreau52eed752017-09-22 15:05:09 +02002055 fail:
2056 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002057 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002058 /* we may have to signal the upper layers */
2059 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002060 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2061 /* cs has just been destroyed, we have to kill h2s. */
2062 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2063 h2c_send_rst_stream(h2c, h2s);
2064 }
2065 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002066 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002067}
2068
2069/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2070 * the end.
2071 */
2072static int h2_process_mux(struct h2c *h2c)
2073{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002074 struct h2s *h2s, *h2s_back;
2075
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002076 /* start by sending possibly pending window updates */
2077 if (h2c->rcvd_c > 0 &&
2078 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2079 h2c_send_conn_wu(h2c) < 0)
2080 goto fail;
2081
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002082 /* First we always process the flow control list because the streams
2083 * waiting there were already elected for immediate emission but were
2084 * blocked just on this.
2085 */
2086
2087 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2088 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2089 h2c->st0 >= H2_CS_ERROR)
2090 break;
2091
2092 /* In theory it's possible that h2s->cs == NULL here :
2093 * - client sends crap that causes a parse error
2094 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2095 * - RST_STREAM cannot be emitted because mux is busy/full
2096 * - stream gets notified, detaches and quits
2097 * - mux buffer gets ready and wakes pending streams up
2098 * - bam!
2099 */
2100 h2s->flags &= ~H2_SF_BLK_ANY;
2101
2102 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002103 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002104 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002105 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002106 }
2107
2108 /* depending on callee's blocking reasons, we may queue in send
2109 * list or completely dequeue.
2110 */
2111 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2112 if (h2s->flags & H2_SF_BLK_ANY) {
2113 LIST_DEL(&h2s->list);
2114 LIST_ADDQ(&h2c->send_list, &h2s->list);
2115 }
2116 else {
2117 LIST_DEL(&h2s->list);
2118 LIST_INIT(&h2s->list);
2119 if (h2s->cs)
2120 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002121 else {
2122 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002123 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002124 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002125 }
2126 }
2127 }
2128
2129 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2130 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2131 break;
2132
2133 /* In theory it's possible that h2s->cs == NULL here :
2134 * - client sends crap that causes a parse error
2135 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2136 * - RST_STREAM cannot be emitted because mux is busy/full
2137 * - stream gets notified, detaches and quits
2138 * - mux buffer gets ready and wakes pending streams up
2139 * - bam!
2140 */
2141 h2s->flags &= ~H2_SF_BLK_ANY;
2142
2143 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002144 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002145 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002146 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002147 }
2148 /* depending on callee's blocking reasons, we may queue in fctl
2149 * list or completely dequeue.
2150 */
2151 if (h2s->flags & H2_SF_BLK_MFCTL) {
2152 /* stream hit the connection's flow control */
2153 LIST_DEL(&h2s->list);
2154 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2155 }
2156 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2157 LIST_DEL(&h2s->list);
2158 LIST_INIT(&h2s->list);
2159 if (h2s->cs)
2160 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002161 else {
2162 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002163 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002164 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002165 }
2166 }
2167
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002168 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002169 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002170 if (h2c->st0 == H2_CS_ERROR) {
2171 if (h2c->max_id >= 0) {
2172 h2c_send_goaway_error(h2c, NULL);
2173 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2174 return 0;
2175 }
2176
2177 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2178 }
2179 return 1;
2180 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002181 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002182}
2183
Willy Tarreau71681172017-10-23 14:39:06 +02002184
Willy Tarreau62f52692017-10-08 23:01:42 +02002185/*********************************************************/
2186/* functions below are I/O callbacks from the connection */
2187/*********************************************************/
2188
2189/* callback called on recv event by the connection handler */
2190static void h2_recv(struct connection *conn)
2191{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002192 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002193 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002194 int max;
2195
Willy Tarreau315d8072017-12-10 22:17:57 +01002196 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002197 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002198
Willy Tarreau44e973f2018-03-01 17:49:30 +01002199 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002200 if (!buf) {
2201 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002202 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002203 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002204
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002205 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002206 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002207 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002208
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002209 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002210 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002211 return;
2212 }
2213
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002214 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002215 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002216 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002217}
2218
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002219/* Try to send data if possible */
2220static struct task *h2_send(struct task *t, void *ctx, unsigned short state)
Willy Tarreau62f52692017-10-08 23:01:42 +02002221{
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002222 struct connection *conn = ctx;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002223 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002224 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002225
2226 if (conn->flags & CO_FL_ERROR)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002227 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002228
2229 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2230 /* a handshake was requested */
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002231 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002232 }
2233
Willy Tarreaubc933932017-10-09 16:21:43 +02002234 /* This loop is quite simple : it tries to fill as much as it can from
2235 * pending streams into the existing buffer until it's reportedly full
2236 * or the end of send requests is reached. Then it tries to send this
2237 * buffer's contents out, marks it not full if at least one byte could
2238 * be sent, and tries again.
2239 *
2240 * The snd_buf() function normally takes a "flags" argument which may
2241 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2242 * data immediately comes and CO_SFL_STREAMER to indicate that the
2243 * connection is streaming lots of data (used to increase TLS record
2244 * size at the expense of latency). The former can be sent any time
2245 * there's a buffer full flag, as it indicates at least one stream
2246 * attempted to send and failed so there are pending data. An
2247 * alternative would be to set it as long as there's an active stream
2248 * but that would be problematic for ACKs until we have an absolute
2249 * guarantee that all waiters have at least one byte to send. The
2250 * latter should possibly not be set for now.
2251 */
2252
2253 done = 0;
2254 while (!done) {
2255 unsigned int flags = 0;
2256
2257 /* fill as much as we can into the current buffer */
2258 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2259 done = h2_process_mux(h2c);
2260
2261 if (conn->flags & CO_FL_ERROR)
2262 break;
2263
2264 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2265 flags |= CO_SFL_MSG_MORE;
2266
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002267 if (b_data(&h2c->mbuf)) {
2268 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002269 if (!ret)
2270 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002271 b_del(&h2c->mbuf, ret);
2272 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002273 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002274
2275 /* wrote at least one byte, the buffer is not full anymore */
2276 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2277 }
2278
Willy Tarreaua2af5122017-10-09 11:56:46 +02002279 if (conn->flags & CO_FL_SOCK_WR_SH) {
2280 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002281 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002282 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002283 /* We're not full anymore, so we can wake any task that are waiting
2284 * for us.
2285 */
2286 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2287 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2288 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2289 struct wait_list *, list);
2290 LIST_DEL(&sw->list);
2291 LIST_INIT(&sw->list);
2292 tasklet_wakeup(sw->task);
2293 }
2294
2295 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002296 /* We're done, no more to send */
2297 if (!b_data(&h2c->mbuf))
2298 return NULL;
2299schedule:
2300 if (LIST_ISEMPTY(&h2c->wait_list.list))
2301 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2302 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002303}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002304
Willy Tarreau62f52692017-10-08 23:01:42 +02002305/* callback called on any event by the connection handler.
2306 * It applies changes and returns zero, or < 0 if it wants immediate
2307 * destruction of the connection (which normally doesn not happen in h2).
2308 */
2309static int h2_wake(struct connection *conn)
2310{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002311 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002312 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002313
Olivier Houchardf495fc42018-07-20 18:15:23 +02002314 h2_send(NULL, conn, 0);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002316 h2_process_demux(h2c);
2317
2318 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002319 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002320
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002321 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002322 h2c->flags &= ~H2_CF_DEM_DFULL;
2323 }
2324
Willy Tarreau8ec14062017-12-30 18:08:13 +01002325 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2326 /* frontend is stopping, reload likely in progress, let's try
2327 * to announce a graceful shutdown if not yet done. We don't
2328 * care if it fails, it will be tried again later.
2329 */
2330 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2331 if (h2c->last_sid < 0)
2332 h2c->last_sid = (1U << 31) - 1;
2333 h2c_send_goaway_error(h2c, NULL);
2334 }
2335 }
2336
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002337 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002338 * If we received early data, and the handshake is done, wake
2339 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002340 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002341 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2342 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2343 struct eb32_node *node;
2344 struct h2s *h2s;
2345
2346 h2c->flags |= H2_CF_WAIT_FOR_HS;
2347 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2348
2349 while (node) {
2350 h2s = container_of(node, struct h2s, by_id);
2351 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2352 h2s->cs->data_cb->wake(h2s->cs);
2353 node = eb32_next(node);
2354 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002355 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002356
Willy Tarreau26bd7612017-10-09 16:47:04 +02002357 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002358 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2359 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2360 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002361 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002362
2363 if (eb_is_empty(&h2c->streams_by_id)) {
2364 /* no more stream, kill the connection now */
2365 h2_release(conn);
2366 return -1;
2367 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002368 }
2369
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002370 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002371 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002372
2373 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002374 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002375 __conn_xprt_stop_recv(conn);
2376 }
2377 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002378 __conn_xprt_want_recv(conn);
2379 }
2380
2381 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002382 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002383 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002384 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002385 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002386 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2387 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002388 __conn_xprt_want_send(conn);
2389 }
2390 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002391 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002392 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002393 }
2394
Willy Tarreau3f133572017-10-31 19:21:06 +01002395 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002396 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002397 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002398 task_queue(h2c->task);
2399 }
2400 else
2401 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002402 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002403
Willy Tarreau62f52692017-10-08 23:01:42 +02002404 return 0;
2405}
2406
Willy Tarreauea392822017-10-31 10:02:25 +01002407/* Connection timeout management. The principle is that if there's no receipt
2408 * nor sending for a certain amount of time, the connection is closed. If the
2409 * MUX buffer still has lying data or is not allocatable, the connection is
2410 * immediately killed. If it's allocatable and empty, we attempt to send a
2411 * GOAWAY frame.
2412 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002413static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002414{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002415 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002416 int expired = tick_is_expired(t->expire, now_ms);
2417
Willy Tarreau0975f112018-03-29 15:22:59 +02002418 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002419 return t;
2420
Willy Tarreau0975f112018-03-29 15:22:59 +02002421 task_delete(t);
2422 task_free(t);
2423
2424 if (!h2c) {
2425 /* resources were already deleted */
2426 return NULL;
2427 }
2428
2429 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002430 h2c_error(h2c, H2_ERR_NO_ERROR);
2431 h2_wake_some_streams(h2c, 0, 0);
2432
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002433 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002434 /* don't even try to send a GOAWAY, the buffer is stuck */
2435 h2c->flags |= H2_CF_GOAWAY_FAILED;
2436 }
2437
2438 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002439 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002440 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2441 h2c->flags |= H2_CF_GOAWAY_FAILED;
2442
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002443 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2444 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002445 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 b_del(&h2c->mbuf, ret);
2447 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002448 }
2449 }
Willy Tarreauea392822017-10-31 10:02:25 +01002450
Willy Tarreau0975f112018-03-29 15:22:59 +02002451 /* either we can release everything now or it will be done later once
2452 * the last stream closes.
2453 */
2454 if (eb_is_empty(&h2c->streams_by_id))
2455 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002456
Willy Tarreauea392822017-10-31 10:02:25 +01002457 return NULL;
2458}
2459
2460
Willy Tarreau62f52692017-10-08 23:01:42 +02002461/*******************************************/
2462/* functions below are used by the streams */
2463/*******************************************/
2464
2465/*
2466 * Attach a new stream to a connection
2467 * (Used for outgoing connections)
2468 */
2469static struct conn_stream *h2_attach(struct connection *conn)
2470{
2471 return NULL;
2472}
2473
2474/* callback used to update the mux's polling flags after changing a cs' status.
2475 * The caller (cs_update_mux_polling) will take care of propagating any changes
2476 * to the transport layer.
2477 */
2478static void h2_update_poll(struct conn_stream *cs)
2479{
Willy Tarreau1d393222017-10-17 10:26:19 +02002480 struct h2s *h2s = cs->ctx;
2481
2482 if (!h2s)
2483 return;
2484
Willy Tarreaud7739c82017-10-30 15:38:23 +01002485 /* we may unblock a blocked read */
2486
Willy Tarreau315d8072017-12-10 22:17:57 +01002487 if (cs->flags & CS_FL_DATA_RD_ENA) {
2488 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002489 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002490 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002491 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002492 conn_xprt_want_send(cs->conn);
2493 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002494 }
2495
Willy Tarreau1d393222017-10-17 10:26:19 +02002496 /* Note: the stream and stream-int code doesn't allow us to perform a
2497 * synchronous send() here unfortunately, because this code is called
2498 * as si_update() from the process_stream() context. This means that
2499 * we have to queue the current cs and defer its processing after the
2500 * connection's cs list is processed anyway.
2501 */
2502
2503 if (cs->flags & CS_FL_DATA_WR_ENA) {
2504 if (LIST_ISEMPTY(&h2s->list)) {
2505 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002506 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002507 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2508 conn_xprt_want_send(cs->conn);
2509 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2510 }
2511 }
2512 else if (!LIST_ISEMPTY(&h2s->list)) {
2513 LIST_DEL(&h2s->list);
2514 LIST_INIT(&h2s->list);
2515 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2516 }
2517
2518 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002519 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002520 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002521}
2522
2523/*
2524 * Detach the stream from the connection and possibly release the connection.
2525 */
2526static void h2_detach(struct conn_stream *cs)
2527{
Willy Tarreau60935142017-10-16 18:11:19 +02002528 struct h2s *h2s = cs->ctx;
2529 struct h2c *h2c;
2530
2531 cs->ctx = NULL;
2532 if (!h2s)
2533 return;
2534
2535 h2c = h2s->h2c;
2536 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002537 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002538 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2539 !h2_has_too_many_cs(h2c)) {
2540 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2541 if (h2_recv_allowed(h2c)) {
2542 __conn_xprt_want_recv(h2c->conn);
2543 conn_xprt_want_send(h2c->conn);
2544 }
2545 }
Willy Tarreau60935142017-10-16 18:11:19 +02002546
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002547 /* this stream may be blocked waiting for some data to leave (possibly
2548 * an ES or RST frame), so orphan it in this case.
2549 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002550 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002551 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002552 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002553 return;
2554
Willy Tarreau45f752e2017-10-30 15:44:59 +01002555 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2556 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2557 /* unblock the connection if it was blocked on this
2558 * stream.
2559 */
2560 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2561 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2562 conn_xprt_want_recv(cs->conn);
2563 conn_xprt_want_send(cs->conn);
2564 }
2565
Willy Tarreau71049cc2018-03-28 13:56:39 +02002566 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002567
Willy Tarreaue323f342018-03-28 13:51:45 +02002568 /* We don't want to close right now unless we're removing the
2569 * last stream, and either the connection is in error, or it
2570 * reached the ID already specified in a GOAWAY frame received
2571 * or sent (as seen by last_sid >= 0).
2572 */
2573 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2574 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002575 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002576 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002577 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002578 (conn_xprt_read0_pending(h2c->conn) ||
2579 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2580 /* no more stream will come, kill it now */
2581 h2_release(h2c->conn);
2582 }
2583 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002584 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002585 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2586 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002587 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002588 else
2589 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002590 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002591}
2592
2593static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2594{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002595 struct h2s *h2s = cs->ctx;
2596
2597 if (!mode)
2598 return;
2599
Willy Tarreau721c9742017-11-07 11:05:42 +01002600 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002601 return;
2602
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002603 /* if no outgoing data was seen on this stream, it means it was
2604 * closed with a "tcp-request content" rule that is normally
2605 * used to kill the connection ASAP (eg: limit abuse). In this
2606 * case we send a goaway to close the connection.
2607 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002608 if (!(h2s->flags & H2_SF_RST_SENT) &&
2609 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002610 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002611
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002612 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2613 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2614 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002615 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002616
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002617 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002618 conn_xprt_want_send(cs->conn);
2619
Willy Tarreau00dd0782018-03-01 16:31:34 +01002620 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002621
2622 add_to_list:
2623 if (LIST_ISEMPTY(&h2s->list)) {
2624 if (h2s->flags & H2_SF_BLK_MFCTL)
2625 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2626 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2627 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2628 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002629}
2630
2631static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2632{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002633 struct h2s *h2s = cs->ctx;
2634
Willy Tarreau721c9742017-11-07 11:05:42 +01002635 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002636 return;
2637
Willy Tarreau67434202017-11-06 20:20:51 +01002638 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002639 /* we can cleanly close using an empty data frame only after headers */
2640
2641 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2642 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002643 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002644
2645 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002646 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002647 else
2648 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002649 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002650 /* if no outgoing data was seen on this stream, it means it was
2651 * closed with a "tcp-request content" rule that is normally
2652 * used to kill the connection ASAP (eg: limit abuse). In this
2653 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002654 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002655 if (!(h2s->flags & H2_SF_RST_SENT) &&
2656 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002657 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002658
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002659 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2660 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002661 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002662 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002663
Willy Tarreau00dd0782018-03-01 16:31:34 +01002664 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002665 }
2666
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002667 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002668 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002669
2670 add_to_list:
2671 if (LIST_ISEMPTY(&h2s->list)) {
2672 if (h2s->flags & H2_SF_BLK_MFCTL)
2673 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2674 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2675 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2676 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002677}
2678
Willy Tarreau13278b42017-10-13 19:23:14 +02002679/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2680 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2681 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002682 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002683 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002684static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002685{
2686 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002687 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002688 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002689 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002690 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002691 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002692 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002693 int flen = h2c->dfl;
2694 int outlen = 0;
2695 int wrap;
2696 int try;
2697
2698 if (!h2c->dfl) {
2699 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002700 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002701 return 0;
2702 }
2703
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002704 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002705 return 0; // incomplete input frame
2706
Willy Tarreau13278b42017-10-13 19:23:14 +02002707 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002708 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002709 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002710 copy = alloc_trash_chunk();
2711 if (!copy) {
2712 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2713 goto fail;
2714 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002715 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2716 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2717 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002718 }
2719
2720 /* The padlen is the first byte before data, and the padding appears
2721 * after data. padlen+data+padding are included in flen.
2722 */
2723 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002724 h2c->dpl = *hdrs;
2725 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002726 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2727 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002728 return 0;
2729 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002730 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002731 hdrs += 1; // skip Pad Length
2732 }
2733
2734 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2735 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002736 if (read_n32(hdrs) == h2s->id) {
2737 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2738 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2739 return 0;//goto fail_stream;
2740 }
2741
Willy Tarreau13278b42017-10-13 19:23:14 +02002742 hdrs += 5; // stream dep = 4, weight = 1
2743 flen -= 5;
2744 }
2745
2746 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2747 * don't support this for now and can't even decompress so we have to
2748 * break the connection.
2749 */
2750 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2751 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002752 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002753 }
2754
Olivier Houchard638b7992018-08-16 15:41:52 +02002755 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002756 if (!csbuf) {
2757 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002758 goto fail;
2759 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002760
Willy Tarreau937f7602018-02-26 15:22:17 +01002761 /* we can't retry a failed decompression operation so we must be very
2762 * careful not to take any risks. In practice the output buffer is
2763 * always empty except maybe for trailers, in which case we simply have
2764 * to wait for the upper layer to finish consuming what is available.
2765 */
2766 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002767 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002768
Willy Tarreau937f7602018-02-26 15:22:17 +01002769 csbuf->head = 0;
2770 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002771
2772 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2773 sizeof(list)/sizeof(list[0]), tmp);
2774 if (outlen < 0) {
2775 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2776 goto fail;
2777 }
2778
2779 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002780 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002781 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002782
2783 if (outlen < 0) {
2784 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2785 goto fail;
2786 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002787
Willy Tarreau174b06a2018-04-25 18:13:58 +02002788 if (msgf & H2_MSGF_BODY) {
2789 /* a payload is present */
2790 if (msgf & H2_MSGF_BODY_CL)
2791 h2s->flags |= H2_SF_DATA_CLEN;
2792 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2793 h2s->flags |= H2_SF_DATA_CHNK;
2794 }
2795
Willy Tarreau13278b42017-10-13 19:23:14 +02002796 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002798 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002799 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002800
Willy Tarreau39d68502018-03-02 12:26:37 +01002801 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002802 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002803 h2s->cs->flags |= CS_FL_REOS;
2804 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002805
Willy Tarreau68dd9852017-07-03 14:44:26 +02002806 leave:
2807 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002808 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002809 fail:
2810 outlen = 0;
2811 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002812}
2813
Willy Tarreau454f9052017-10-26 19:40:35 +02002814/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2815 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2816 * in use, a new chunk is emitted for each frame. This is supposed to fit
2817 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2818 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2819 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2820 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002821 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2822 * checked to know if some data remain pending (an empty DATA frame can return
2823 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2824 * connection errors in h2c->errcode. The caller must already have checked the
2825 * frame header and ensured that the frame was complete or the buffer full. It
2826 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002827 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002828static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002829{
2830 struct h2c *h2c = h2s->h2c;
2831 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002832 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002833 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002834 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002835
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002836 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002837
2838 /* The padlen is the first byte before data, and the padding appears
2839 * after data. padlen+data+padding are included in flen.
2840 */
Willy Tarreau79127812017-12-03 21:06:59 +01002841 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002842 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002843 return 0;
2844
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002846 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002847 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2848 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002849 return 0;
2850 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002851
2852 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002854 h2c->dfl--;
2855 h2c->rcvd_c++; h2c->rcvd_s++;
2856 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002857 }
2858
Olivier Houchard638b7992018-08-16 15:41:52 +02002859 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002860 if (!csbuf) {
2861 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002862 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002863 }
2864
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002865 flen = h2c->dfl - h2c->dpl;
2866 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002867 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002868
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 if (flen > b_data(&h2c->dbuf)) {
2870 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002871 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002872 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002873 }
2874
2875 if (unlikely(b_space_wraps(csbuf))) {
2876 /* it doesn't fit and the buffer is fragmented,
2877 * so let's defragment it and try again.
2878 */
2879 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002880 }
2881
Willy Tarreaueba10f22018-04-25 20:44:22 +02002882 /* chunked-encoding requires more room */
2883 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002884 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002885 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2886 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2887 (chklen < 1048576) ? 4 : 8;
2888 chklen += 4; // CRLF, CRLF
2889 }
2890
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002891 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002892 if (flen + chklen > b_room(csbuf)) {
2893 if (chklen >= b_room(csbuf)) {
2894 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002895 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002896 }
2897 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002898 }
2899
2900 if (h2s->flags & H2_SF_DATA_CHNK) {
2901 /* emit the chunk size */
2902 unsigned int chksz = flen;
2903 char str[10];
2904 char *beg;
2905
2906 beg = str + sizeof(str);
2907 *--beg = '\n';
2908 *--beg = '\r';
2909 do {
2910 *--beg = hextab[chksz & 0xF];
2911 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002912 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002913 }
2914
Willy Tarreau454f9052017-10-26 19:40:35 +02002915 /* Block1 is the length of the first block before the buffer wraps,
2916 * block2 is the optional second block to reach the end of the frame.
2917 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002919 if (block1 > flen)
2920 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002921 block2 = flen - block1;
2922
2923 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002924 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002925
2926 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002927 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002928
Willy Tarreaueba10f22018-04-25 20:44:22 +02002929 if (h2s->flags & H2_SF_DATA_CHNK) {
2930 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002931 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002932 }
2933
Willy Tarreau454f9052017-10-26 19:40:35 +02002934 /* now mark the input data as consumed (will be deleted from the buffer
2935 * by the caller when seeing FRAME_A after sending the window update).
2936 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002937 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002938 h2c->dfl -= flen;
2939 h2c->rcvd_c += flen;
2940 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2941
2942 if (h2c->dfl > h2c->dpl) {
2943 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002944 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002945 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002946 }
2947
Willy Tarreau4a28da12018-01-04 14:41:00 +01002948 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002949 /* here we're done with the frame, all the payload (except padding) was
2950 * transferred.
2951 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002952
2953 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2954 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002955 if (b_room(csbuf) < 5) {
2956 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002957 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002958 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002959 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002961 }
2962
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002963 h2c->rcvd_c += h2c->dpl;
2964 h2c->rcvd_s += h2c->dpl;
2965 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002966 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2967
Willy Tarreau39d68502018-03-02 12:26:37 +01002968 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002969 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002970 h2s->cs->flags |= CS_FL_REOS;
2971 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01002972
Willy Tarreau454b57b2018-02-26 15:50:05 +01002973 return flen + chklen;
2974 fail:
2975 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002976}
2977
Willy Tarreau5dd17352018-06-14 13:33:30 +02002978/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2979 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2980 * number of bytes sent. The caller must check the stream's status to detect
2981 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002982 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002983static 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 +02002984{
2985 struct http_hdr list[MAX_HTTP_HDR];
2986 struct h2c *h2c = h2s->h2c;
2987 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002988 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002989 int es_now = 0;
2990 int ret = 0;
2991 int hdr;
2992
2993 if (h2c_mux_busy(h2c, h2s)) {
2994 h2s->flags |= H2_SF_BLK_MBUSY;
2995 return 0;
2996 }
2997
Willy Tarreau44e973f2018-03-01 17:49:30 +01002998 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002999 h2c->flags |= H2_CF_MUX_MALLOC;
3000 h2s->flags |= H2_SF_BLK_MROOM;
3001 return 0;
3002 }
3003
3004 /* First, try to parse the H1 response and index it into <list>.
3005 * NOTE! Since it comes from haproxy, we *know* that a response header
3006 * block does not wrap and we can safely read it this way without
3007 * having to realign the buffer.
3008 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003009 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003010 list, sizeof(list)/sizeof(list[0]), h1m);
3011 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003012 /* incomplete or invalid response, this is abnormal coming from
3013 * haproxy and may only result in a bad errorfile or bad Lua code
3014 * so that won't be fixed, raise an error now.
3015 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003016 * FIXME: we should instead add the ability to only return a
3017 * 502 bad gateway. But in theory this is not supposed to
3018 * happen.
3019 */
3020 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3021 ret = 0;
3022 goto end;
3023 }
3024
3025 chunk_reset(&outbuf);
3026
3027 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003028 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003031
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003032 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003033 break;
3034 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003035 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003036 }
3037
3038 if (outbuf.size < 9) {
3039 h2c->flags |= H2_CF_MUX_MFULL;
3040 h2s->flags |= H2_SF_BLK_MROOM;
3041 ret = 0;
3042 goto end;
3043 }
3044
3045 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003046 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3047 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3048 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003049
3050 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003051 if (outbuf.data < outbuf.size && h1m->status == 200)
3052 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3053 else if (outbuf.data < outbuf.size && h1m->status == 304)
3054 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003055 else if (unlikely(list[0].v.len != 3)) {
3056 /* this is an unparsable response */
3057 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3058 ret = 0;
3059 goto end;
3060 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003062 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003063 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3064 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3065 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3066 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3067 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003068 }
3069 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003070 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003071 goto realign_again;
3072
3073 h2c->flags |= H2_CF_MUX_MFULL;
3074 h2s->flags |= H2_SF_BLK_MROOM;
3075 ret = 0;
3076 goto end;
3077 }
3078
3079 /* encode all headers, stop at empty name */
3080 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003081 /* these ones do not exist in H2 and must be dropped. */
3082 if (isteq(list[hdr].n, ist("connection")) ||
3083 isteq(list[hdr].n, ist("proxy-connection")) ||
3084 isteq(list[hdr].n, ist("keep-alive")) ||
3085 isteq(list[hdr].n, ist("upgrade")) ||
3086 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003087 continue;
3088
3089 if (isteq(list[hdr].n, ist("")))
3090 break; // end
3091
3092 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3093 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003094 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003095 goto realign_again;
3096
3097 h2c->flags |= H2_CF_MUX_MFULL;
3098 h2s->flags |= H2_SF_BLK_MROOM;
3099 ret = 0;
3100 goto end;
3101 }
3102 }
3103
3104 /* we may need to add END_STREAM */
3105 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3106 es_now = 1;
3107
3108 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003109 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110
3111 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003113
3114 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003115 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003116
3117 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003118 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003119 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003120
3121 /* for now we don't implemented CONTINUATION, so we wait for a
3122 * body or directly end in TRL2.
3123 */
3124 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003125 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003126 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003127
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003128 h1m->state = HTTP_MSG_DONE;
3129 h2s->flags |= H2_SF_ES_SENT;
3130 if (h2s->st == H2_SS_OPEN)
3131 h2s->st = H2_SS_HLOC;
3132 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003133 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003134 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003135 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003136 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003137 h1m->state = HTTP_MSG_RPBEFORE;
3138 h1m->status = 0;
3139 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003140 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003141 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003142 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003143 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003144
3145 end:
3146 //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));
3147 return ret;
3148}
3149
Willy Tarreau5dd17352018-06-14 13:33:30 +02003150/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3151 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3152 * the number of bytes sent. The caller must check the stream's status to
3153 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003154 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003155static 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 +02003156{
3157 struct h2c *h2c = h2s->h2c;
3158 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003159 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003160 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003161 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003162 int es_now = 0;
3163 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003164 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003165 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003166
3167 if (h2c_mux_busy(h2c, h2s)) {
3168 h2s->flags |= H2_SF_BLK_MBUSY;
3169 goto end;
3170 }
3171
Willy Tarreau44e973f2018-03-01 17:49:30 +01003172 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003173 h2c->flags |= H2_CF_MUX_MALLOC;
3174 h2s->flags |= H2_SF_BLK_MROOM;
3175 goto end;
3176 }
3177
3178 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003179 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003180 goto end;
3181
3182 chunk_reset(&outbuf);
3183
3184 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003185 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003186 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003187 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003188
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003189 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003190 break;
3191 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003192 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003193 }
3194
3195 if (outbuf.size < 9) {
3196 h2c->flags |= H2_CF_MUX_MFULL;
3197 h2s->flags |= H2_SF_BLK_MROOM;
3198 goto end;
3199 }
3200
3201 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3203 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3204 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003205
3206 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3207 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003208 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003209 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003210 break;
3211 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003212 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003213 if ((long long)size > h1m->curr_len)
3214 size = h1m->curr_len;
3215 break;
3216 default: /* te:chunked : parse chunks */
3217 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003218 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003219 if (!ret)
3220 goto end;
3221
3222 if (ret < 0) {
3223 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3224 h1m->err_pos = ret;
3225 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3226 goto end;
3227 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003228 max -= ret;
3229 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003230 total += ret;
3231 h1m->state = HTTP_MSG_CHUNK_SIZE;
3232 }
3233
3234 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3235 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003236 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003237 if (!ret)
3238 goto end;
3239
3240 if (ret < 0) {
3241 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3242 h1m->err_pos = ret;
3243 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3244 goto end;
3245 }
3246
3247 size = chunk;
3248 h1m->curr_len = chunk;
3249 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003250 max -= ret;
3251 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003252 total += ret;
3253 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3254 if (!size)
3255 goto send_empty;
3256 }
3257
3258 /* in MSG_DATA state, continue below */
3259 size = h1m->curr_len;
3260 break;
3261 }
3262
3263 /* we have in <size> the exact number of bytes we need to copy from
3264 * the H1 buffer. We need to check this against the connection's and
3265 * the stream's send windows, and to ensure that this fits in the max
3266 * frame size and in the buffer's available space minus 9 bytes (for
3267 * the frame header). The connection's flow control is applied last so
3268 * that we can use a separate list of streams which are immediately
3269 * unblocked on window opening. Note: we don't implement padding.
3270 */
3271
Willy Tarreau5dd17352018-06-14 13:33:30 +02003272 if (size > max)
3273 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003274
3275 if (size > h2s->mws)
3276 size = h2s->mws;
3277
3278 if (size <= 0) {
3279 h2s->flags |= H2_SF_BLK_SFCTL;
3280 goto end;
3281 }
3282
3283 if (h2c->mfs && size > h2c->mfs)
3284 size = h2c->mfs;
3285
3286 if (size + 9 > outbuf.size) {
3287 /* we have an opportunity for enlarging the too small
3288 * available space, let's try.
3289 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003290 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003291 goto realign_again;
3292 size = outbuf.size - 9;
3293 }
3294
3295 if (size <= 0) {
3296 h2c->flags |= H2_CF_MUX_MFULL;
3297 h2s->flags |= H2_SF_BLK_MROOM;
3298 goto end;
3299 }
3300
3301 if (size > h2c->mws)
3302 size = h2c->mws;
3303
3304 if (size <= 0) {
3305 h2s->flags |= H2_SF_BLK_MFCTL;
3306 goto end;
3307 }
3308
3309 /* copy whatever we can */
3310 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003311 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003312 if (ret == 1)
3313 len2 = 0;
3314
3315 if (!ret || len1 + len2 < size) {
3316 /* FIXME: must normally never happen */
3317 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3318 goto end;
3319 }
3320
3321 /* limit len1/len2 to size */
3322 if (len1 + len2 > size) {
3323 int sub = len1 + len2 - size;
3324
3325 if (len2 > sub)
3326 len2 -= sub;
3327 else {
3328 sub -= len2;
3329 len2 = 0;
3330 len1 -= sub;
3331 }
3332 }
3333
3334 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003335 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003337 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338
3339 send_empty:
3340 /* we may need to add END_STREAM */
3341 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3342 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003343 *
3344 * FIXME: what we do here is not correct because we send end_stream
3345 * before knowing if we'll have to send a HEADERS frame for the
3346 * trailers. More importantly we're not consuming the trailing CRLF
3347 * after the end of trailers, so it will be left to the caller to
3348 * eat it. The right way to do it would be to measure trailers here
3349 * and to send ES only if there are no trailers.
3350 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003351 */
3352 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3353 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3354 es_now = 1;
3355
3356 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003357 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358
3359 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003360 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003361
3362 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003363 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003364
3365 /* consume incoming H1 response */
3366 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003367 max -= size;
3368 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369 total += size;
3370 h1m->curr_len -= size;
3371 h2s->mws -= size;
3372 h2c->mws -= size;
3373
3374 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3375 h1m->state = HTTP_MSG_CHUNK_CRLF;
3376 goto new_frame;
3377 }
3378 }
3379
3380 if (es_now) {
3381 if (h2s->st == H2_SS_OPEN)
3382 h2s->st = H2_SS_HLOC;
3383 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003384 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003385
Willy Tarreau35a62702018-02-27 15:37:25 +01003386 if (!(h1m->flags & H1_MF_CHNK)) {
3387 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003388 total += max;
3389 ofs += max;
3390 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003391
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003392 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003393 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003394
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003395 h2s->flags |= H2_SF_ES_SENT;
3396 }
3397
3398 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003399 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 +02003400 return total;
3401}
3402
Olivier Houchard6ff20392018-07-17 18:46:31 +02003403/* Called from the upper layer, to subscribe to events, such as being able to send */
3404static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3405{
3406 struct wait_list *sw;
3407 struct h2s *h2s = cs->ctx;
3408
3409 switch (event_type) {
3410 case SUB_CAN_SEND:
3411 sw = param;
3412 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3413 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3414 return 0;
3415 default:
3416 break;
3417 }
3418 return -1;
3419
3420
3421}
3422
Olivier Houchard511efea2018-08-16 15:30:32 +02003423/* Called from the upper layer, to receive data */
3424static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3425{
Olivier Houchard638b7992018-08-16 15:41:52 +02003426 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003427 size_t ret = 0;
3428
3429 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003430 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003431
Olivier Houchard638b7992018-08-16 15:41:52 +02003432 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003433 cs->flags |= CS_FL_RCV_MORE;
3434 else {
3435 cs->flags &= ~CS_FL_RCV_MORE;
3436 if (cs->flags & CS_FL_REOS)
3437 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003438 if (b_size(&h2s->rxbuf)) {
3439 b_free(&h2s->rxbuf);
3440 offer_buffers(NULL, tasks_run_queue);
3441 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003442 }
3443
3444 return ret;
3445}
3446
Willy Tarreau62f52692017-10-08 23:01:42 +02003447/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003448static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003449{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003450 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003451 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003452 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003453
Willy Tarreau0bad0432018-06-14 16:54:01 +02003454 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003455 h2s->flags |= H2_SF_OUTGOING_DATA;
3456
Willy Tarreau0bad0432018-06-14 16:54:01 +02003457 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003458 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003459 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003460 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003461 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003462 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003463 }
3464 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3465 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003466 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003467
Willy Tarreau5dd17352018-06-14 13:33:30 +02003468 if (unlikely((int)ret <= 0)) {
3469 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003470 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3471 break;
3472 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003473 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003474 total += count;
3475 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003476 h2s->res.state = HTTP_MSG_DONE;
3477 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003478 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003479 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003480 cs->flags |= CS_FL_ERROR;
3481 break;
3482 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003483
3484 total += ret;
3485 count -= ret;
3486
3487 if (h2s->st >= H2_SS_ERROR)
3488 break;
3489
3490 if (h2s->flags & H2_SF_BLK_ANY)
3491 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003492 }
3493
Willy Tarreau00610962018-07-19 10:58:28 +02003494 if (h2s->st >= H2_SS_ERROR) {
3495 /* trim any possibly pending data after we close (extra CR-LF,
3496 * unprocessed trailers, abnormal extra data, ...)
3497 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003498 total += count;
3499 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003500 }
3501
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003502 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003503 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003504 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003505 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003506 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003507 }
3508
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003509 if (h2s->flags & H2_SF_BLK_SFCTL) {
3510 /* stream flow control, quit the list */
3511 LIST_DEL(&h2s->list);
3512 LIST_INIT(&h2s->list);
3513 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003514 else if (LIST_ISEMPTY(&h2s->list)) {
3515 if (h2s->flags & H2_SF_BLK_MFCTL)
3516 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003517 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003518
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003519 b_del(buf, total);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003520 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003521}
3522
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003523/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003524static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003525{
3526 struct h2c *h2c = conn->mux_ctx;
3527 struct h2s *h2s;
3528 struct eb32_node *node;
3529 int fctl_cnt = 0;
3530 int send_cnt = 0;
3531 int tree_cnt = 0;
3532 int orph_cnt = 0;
3533
3534 if (!h2c)
3535 return;
3536
3537 list_for_each_entry(h2s, &h2c->fctl_list, list)
3538 fctl_cnt++;
3539
3540 list_for_each_entry(h2s, &h2c->send_list, list)
3541 send_cnt++;
3542
3543 node = eb32_first(&h2c->streams_by_id);
3544 while (node) {
3545 h2s = container_of(node, struct h2s, by_id);
3546 tree_cnt++;
3547 if (!h2s->cs)
3548 orph_cnt++;
3549 node = eb32_next(node);
3550 }
3551
Willy Tarreau616ac812018-07-24 14:12:42 +02003552 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3553 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3554 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3555 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3556 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3557 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003558}
Willy Tarreau62f52692017-10-08 23:01:42 +02003559
3560/*******************************************************/
3561/* functions below are dedicated to the config parsers */
3562/*******************************************************/
3563
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003564/* config parser for global "tune.h2.header-table-size" */
3565static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3566 struct proxy *defpx, const char *file, int line,
3567 char **err)
3568{
3569 if (too_many_args(1, args, err, NULL))
3570 return -1;
3571
3572 h2_settings_header_table_size = atoi(args[1]);
3573 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3574 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3575 return -1;
3576 }
3577 return 0;
3578}
Willy Tarreau62f52692017-10-08 23:01:42 +02003579
Willy Tarreaue6baec02017-07-27 11:45:11 +02003580/* config parser for global "tune.h2.initial-window-size" */
3581static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3582 struct proxy *defpx, const char *file, int line,
3583 char **err)
3584{
3585 if (too_many_args(1, args, err, NULL))
3586 return -1;
3587
3588 h2_settings_initial_window_size = atoi(args[1]);
3589 if (h2_settings_initial_window_size < 0) {
3590 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3591 return -1;
3592 }
3593 return 0;
3594}
3595
Willy Tarreau5242ef82017-07-27 11:47:28 +02003596/* config parser for global "tune.h2.max-concurrent-streams" */
3597static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3598 struct proxy *defpx, const char *file, int line,
3599 char **err)
3600{
3601 if (too_many_args(1, args, err, NULL))
3602 return -1;
3603
3604 h2_settings_max_concurrent_streams = atoi(args[1]);
3605 if (h2_settings_max_concurrent_streams < 0) {
3606 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3607 return -1;
3608 }
3609 return 0;
3610}
3611
Willy Tarreau62f52692017-10-08 23:01:42 +02003612
3613/****************************************/
3614/* MUX initialization and instanciation */
3615/***************************************/
3616
3617/* The mux operations */
3618const struct mux_ops h2_ops = {
3619 .init = h2_init,
3620 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003621 .wake = h2_wake,
3622 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003623 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003624 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003625 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003626 .attach = h2_attach,
3627 .detach = h2_detach,
3628 .shutr = h2_shutr,
3629 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003630 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003631 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003632 .name = "H2",
3633};
3634
Christopher Faulet32f61c02018-04-10 14:33:41 +02003635/* PROTO selection : this mux registers PROTO token "h2" */
3636static struct mux_proto_list mux_proto_h2 =
3637 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003638
3639/* config keyword parsers */
3640static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003641 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003642 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003643 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003644 { 0, NULL, NULL }
3645}};
3646
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003647static void __h2_deinit(void)
3648{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003649 pool_destroy(pool_head_h2s);
3650 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003651}
3652
Willy Tarreau62f52692017-10-08 23:01:42 +02003653__attribute__((constructor))
3654static void __h2_init(void)
3655{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003656 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003657 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003658 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003659 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3660 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003661}