blob: ce36b1fe62cad27f7d6151106531374eb9d02d2e [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
186};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200187
Willy Tarreauc6405142017-09-21 20:23:50 +0200188/* descriptor for an h2 frame header */
189struct h2_fh {
190 uint32_t len; /* length, host order, 24 bits */
191 uint32_t sid; /* stream id, host order, 31 bits */
192 uint8_t ft; /* frame type */
193 uint8_t ff; /* frame flags */
194};
195
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200196/* a few settings from the global section */
197static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200198static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200199static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200200
Willy Tarreau2a856182017-05-16 15:20:39 +0200201/* a dmumy closed stream */
202static const struct h2s *h2_closed_stream = &(const struct h2s){
203 .cs = NULL,
204 .h2c = NULL,
205 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100206 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100207 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200208 .id = 0,
209};
210
211/* and a dummy idle stream for use with any unannounced stream */
212static const struct h2s *h2_idle_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200217 .id = 0,
218};
219
Olivier Houchard9f6af332018-05-25 14:04:04 +0200220static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200221static struct task *h2_send(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100222static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100223static int h2_frt_decode_headers(struct h2s *h2s);
224static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200226/*****************************************************/
227/* functions below are for dynamic buffer management */
228/*****************************************************/
229
Willy Tarreau315d8072017-12-10 22:17:57 +0100230/* indicates whether or not the we may call the h2_recv() function to attempt
231 * to receive data into the buffer and/or demux pending data. The condition is
232 * a bit complex due to some API limits for now. The rules are the following :
233 * - if an error or a shutdown was detected on the connection and the buffer
234 * is empty, we must not attempt to receive
235 * - if the demux buf failed to be allocated, we must not try to receive and
236 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100237 * - if no flag indicates a blocking condition, we may attempt to receive,
238 * regardless of whether the demux buffer is full or not, so that only
239 * de demux part decides whether or not to block. This is needed because
240 * the connection API indeed prevents us from re-enabling receipt that is
241 * already enabled in a polled state, so we must always immediately stop
242 * as soon as the demux can't proceed so as never to hit an end of read
243 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100244 * - otherwise must may not attempt
245 */
246static inline int h2_recv_allowed(const struct h2c *h2c)
247{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200248 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100249 (h2c->st0 >= H2_CS_ERROR ||
250 h2c->conn->flags & CO_FL_ERROR ||
251 conn_xprt_read0_pending(h2c->conn)))
252 return 0;
253
254 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100255 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100256 return 1;
257
258 return 0;
259}
260
Willy Tarreauf2101912018-07-19 10:11:38 +0200261/* returns true if the connection has too many conn_streams attached */
262static inline int h2_has_too_many_cs(const struct h2c *h2c)
263{
264 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
265}
266
Willy Tarreau44e973f2018-03-01 17:49:30 +0100267/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
268 * flags are used to figure what buffer was requested. It returns 1 if the
269 * allocation succeeds, in which case the connection is woken up, or 0 if it's
270 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200271 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100272static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200273{
274 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100275 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200278 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100279 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200280 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281 return 1;
282 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283
Willy Tarreau44e973f2018-03-01 17:49:30 +0100284 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
285 h2c->flags &= ~H2_CF_MUX_MALLOC;
286 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
287 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200288
289 if (h2c->flags & H2_CF_DEM_MROOM) {
290 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100291 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200292 conn_xprt_want_recv(h2c->conn);
293 }
Willy Tarreau14398122017-09-22 14:26:04 +0200294 return 1;
295 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100296
297 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
298 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
299 b_alloc_margin(&h2s->cs->rxbuf, 0)) {
300 h2c->flags &= ~H2_CF_DEM_SALLOC;
301 if (h2_recv_allowed(h2c))
302 conn_xprt_want_recv(h2c->conn);
303 return 1;
304 }
305
Willy Tarreau14398122017-09-22 14:26:04 +0200306 return 0;
307}
308
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200309static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200310{
311 struct buffer *buf = NULL;
312
Willy Tarreau44e973f2018-03-01 17:49:30 +0100313 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
314 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
315 h2c->buf_wait.target = h2c;
316 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100318 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200320 __conn_xprt_stop_recv(h2c->conn);
321 }
322 return buf;
323}
324
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200325static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200326{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200327 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200329 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200330 }
331}
332
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200333
Willy Tarreau62f52692017-10-08 23:01:42 +0200334/*****************************************************************/
335/* functions below are dedicated to the mux setup and management */
336/*****************************************************************/
337
Willy Tarreau32218eb2017-09-22 08:07:25 +0200338/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
339static int h2c_frt_init(struct connection *conn)
340{
341 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100342 struct task *t = NULL;
343 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200344
Willy Tarreaubafbe012017-11-24 17:34:44 +0100345 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200346 if (!h2c)
347 goto fail;
348
Willy Tarreau3f133572017-10-31 19:21:06 +0100349
Willy Tarreau599391a2017-11-24 10:16:00 +0100350 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
351 if (tick_isset(sess->fe->timeout.clientfin))
352 h2c->shut_timeout = sess->fe->timeout.clientfin;
353
Willy Tarreau33400292017-11-05 11:23:40 +0100354 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100355 if (tick_isset(h2c->timeout)) {
356 t = task_new(tid_bit);
357 if (!t)
358 goto fail;
359
360 h2c->task = t;
361 t->process = h2_timeout_task;
362 t->context = h2c;
363 t->expire = tick_add(now_ms, h2c->timeout);
364 }
Willy Tarreauea392822017-10-31 10:02:25 +0100365
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200366 h2c->wait_list.task = tasklet_new();
367 if (!h2c->wait_list.task)
368 goto fail;
369 h2c->wait_list.task->process = h2_send;
370 h2c->wait_list.task->context = conn;
371
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
373 if (!h2c->ddht)
374 goto fail;
375
376 /* Initialise the context. */
377 h2c->st0 = H2_CS_PREFACE;
378 h2c->conn = conn;
379 h2c->max_id = -1;
380 h2c->errcode = H2_ERR_NO_ERROR;
381 h2c->flags = H2_CF_NONE;
382 h2c->rcvd_c = 0;
383 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100384 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200385 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200387 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 h2c->dsi = -1;
389 h2c->msi = -1;
390 h2c->last_sid = -1;
391
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200392 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200393 h2c->miw = 65535; /* mux initial window size */
394 h2c->mws = 65535; /* mux window size */
395 h2c->mfs = 16384; /* initial max frame size */
396 h2c->streams_by_id = EB_ROOT_UNIQUE;
397 LIST_INIT(&h2c->send_list);
398 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100399 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200400 conn->mux_ctx = h2c;
401
Willy Tarreau3f133572017-10-31 19:21:06 +0100402 if (t)
403 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200405 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200406 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100407
Willy Tarreau32218eb2017-09-22 08:07:25 +0200408 /* mux->wake will be called soon to complete the operation */
409 return 0;
410 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100411 if (t)
412 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200413 if (h2c->wait_list.task)
414 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100415 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 return -1;
417}
418
Willy Tarreau62f52692017-10-08 23:01:42 +0200419/* Initialize the mux once it's attached. For outgoing connections, the context
420 * is already initialized before installing the mux, so we detect incoming
421 * connections from the fact that the context is still NULL. Returns < 0 on
422 * error.
423 */
424static int h2_init(struct connection *conn)
425{
426 if (conn->mux_ctx) {
427 /* we don't support outgoing connections for now */
428 return -1;
429 }
430
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200432}
433
Willy Tarreau2373acc2017-10-12 17:35:14 +0200434/* returns the stream associated with id <id> or NULL if not found */
435static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
436{
437 struct eb32_node *node;
438
Willy Tarreau2a856182017-05-16 15:20:39 +0200439 if (id > h2c->max_id)
440 return (struct h2s *)h2_idle_stream;
441
Willy Tarreau2373acc2017-10-12 17:35:14 +0200442 node = eb32_lookup(&h2c->streams_by_id, id);
443 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200444 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200445
446 return container_of(node, struct h2s, by_id);
447}
448
Willy Tarreau62f52692017-10-08 23:01:42 +0200449/* release function for a connection. This one should be called to free all
450 * resources allocated to the mux.
451 */
452static void h2_release(struct connection *conn)
453{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200454 struct h2c *h2c = conn->mux_ctx;
455
456 LIST_DEL(&conn->list);
457
458 if (h2c) {
459 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200460
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100462 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100463 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200464
Willy Tarreau44e973f2018-03-01 17:49:30 +0100465 h2_release_buf(h2c, &h2c->dbuf);
466 h2_release_buf(h2c, &h2c->mbuf);
467
Willy Tarreauea392822017-10-31 10:02:25 +0100468 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200469 h2c->task->context = NULL;
470 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100471 h2c->task = NULL;
472 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200473 if (h2c->wait_list.task)
474 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100475
Willy Tarreaubafbe012017-11-24 17:34:44 +0100476 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200477 }
478
479 conn->mux = NULL;
480 conn->mux_ctx = NULL;
481
482 conn_stop_tracking(conn);
483 conn_full_close(conn);
484 if (conn->destroy_cb)
485 conn->destroy_cb(conn);
486 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200487}
488
489
Willy Tarreau71681172017-10-23 14:39:06 +0200490/******************************************************/
491/* functions below are for the H2 protocol processing */
492/******************************************************/
493
494/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100495static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200496{
497 return h2s ? h2s->id : 0;
498}
499
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200500/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100501static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200502{
503 if (h2c->msi < 0)
504 return 0;
505
506 if (h2c->msi == h2s_id(h2s))
507 return 0;
508
509 return 1;
510}
511
Willy Tarreau741d6df2017-10-17 08:00:59 +0200512/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100513static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200514{
515 h2c->errcode = err;
516 h2c->st0 = H2_CS_ERROR;
517}
518
Willy Tarreau2e43f082017-10-17 08:03:59 +0200519/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100520static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200521{
522 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
523 h2s->errcode = err;
524 h2s->st = H2_SS_ERROR;
525 if (h2s->cs)
526 h2s->cs->flags |= CS_FL_ERROR;
527 }
528}
529
Willy Tarreaue4820742017-07-27 13:37:23 +0200530/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200532{
533 uint8_t *out = frame;
534
535 *out = len >> 16;
536 write_n16(out + 1, len);
537}
538
Willy Tarreau54c15062017-10-10 17:10:03 +0200539/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
540 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
541 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200542 * available in the buffer's input prior to calling this function. The buffer
543 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200544 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100545static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200546 const struct buffer *b, int o)
547{
Willy Tarreau591d4452018-06-15 17:21:00 +0200548 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200549}
550
Willy Tarreau1f094672017-11-20 21:27:45 +0100551static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200552{
Willy Tarreau591d4452018-06-15 17:21:00 +0200553 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200554}
555
Willy Tarreau1f094672017-11-20 21:27:45 +0100556static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200557{
Willy Tarreau591d4452018-06-15 17:21:00 +0200558 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200559}
560
Willy Tarreau1f094672017-11-20 21:27:45 +0100561static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200562{
Willy Tarreau591d4452018-06-15 17:21:00 +0200563 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200564}
565
566
Willy Tarreau715d5312017-07-11 15:20:24 +0200567/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
568 * is not obvious. It turns out that H2 headers are neither aligned nor do they
569 * use regular sizes. And to add to the trouble, the buffer may wrap so each
570 * byte read must be checked. The header is formed like this :
571 *
572 * b0 b1 b2 b3 b4 b5..b8
573 * +----------+---------+--------+----+----+----------------------+
574 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
575 * +----------+---------+--------+----+----+----------------------+
576 *
577 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
578 * we get the sid properly aligned and ordered, and 16 bits of len properly
579 * ordered as well. The type and flags can be extracted using bit shifts from
580 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200581 * Returns zero if some bytes are missing, otherwise non-zero on success. The
582 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200583 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100584static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200585{
586 uint64_t w;
587
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200588 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200589 return 0;
590
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200591 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200592 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200593 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
594 h->ff = w >> 32;
595 h->ft = w >> 40;
596 h->len += w >> 48;
597 return 1;
598}
599
600/* skip the next 9 bytes corresponding to the frame header possibly parsed by
601 * h2_peek_frame_hdr() above.
602 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100603static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200604{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200605 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200606}
607
608/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100609static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200610{
611 int ret;
612
613 ret = h2_peek_frame_hdr(b, h);
614 if (ret > 0)
615 h2_skip_frame_hdr(b);
616 return ret;
617}
618
Willy Tarreau00dd0782018-03-01 16:31:34 +0100619/* marks stream <h2s> as CLOSED and decrement the number of active streams for
620 * its connection if the stream was not yet closed. Please use this exclusively
621 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100622 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100623static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100624{
625 if (h2s->st != H2_SS_CLOSED)
626 h2s->h2c->nb_streams--;
627 h2s->st = H2_SS_CLOSED;
628}
629
Willy Tarreau71049cc2018-03-28 13:56:39 +0200630/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
631static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100632{
633 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200634 LIST_DEL(&h2s->list);
635 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100636 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100637 pool_free(pool_head_h2s, h2s);
638}
639
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200640/* creates a new stream <id> on the h2c connection and returns it, or NULL in
641 * case of memory allocation error.
642 */
643static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
644{
645 struct conn_stream *cs;
646 struct h2s *h2s;
647
Willy Tarreaubafbe012017-11-24 17:34:44 +0100648 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200649 if (!h2s)
650 goto out;
651
652 h2s->h2c = h2c;
653 h2s->mws = h2c->miw;
654 h2s->flags = H2_SF_NONE;
655 h2s->errcode = H2_ERR_NO_ERROR;
656 h2s->st = H2_SS_IDLE;
657 h1m_init(&h2s->req);
658 h1m_init(&h2s->res);
659 h2s->by_id.key = h2s->id = id;
660 h2c->max_id = id;
661 LIST_INIT(&h2s->list);
662
663 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100664 h2c->nb_streams++;
665 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
666 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200667
668 cs = cs_new(h2c->conn);
669 if (!cs)
670 goto out_close;
671
672 h2s->cs = cs;
673 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200674 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675
676 if (stream_create_from_cs(cs) < 0)
677 goto out_free_cs;
678
679 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200680 if (h2_has_too_many_cs(h2c))
681 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200682 return h2s;
683
684 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200685 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200686 cs_free(cs);
687 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200688 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200689 h2s = NULL;
690 out:
691 return h2s;
692}
693
Willy Tarreaube5b7152017-09-25 16:25:39 +0200694/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
695 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
696 * the various settings codes.
697 */
698static int h2c_snd_settings(struct h2c *h2c)
699{
700 struct buffer *res;
701 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200702 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200703 int ret;
704
705 if (h2c_mux_busy(h2c, NULL)) {
706 h2c->flags |= H2_CF_DEM_MBUSY;
707 return 0;
708 }
709
Willy Tarreau44e973f2018-03-01 17:49:30 +0100710 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200711 if (!res) {
712 h2c->flags |= H2_CF_MUX_MALLOC;
713 h2c->flags |= H2_CF_DEM_MROOM;
714 return 0;
715 }
716
717 chunk_init(&buf, buf_data, sizeof(buf_data));
718 chunk_memcpy(&buf,
719 "\x00\x00\x00" /* length : 0 for now */
720 "\x04\x00" /* type : 4 (settings), flags : 0 */
721 "\x00\x00\x00\x00", /* stream ID : 0 */
722 9);
723
724 if (h2_settings_header_table_size != 4096) {
725 char str[6] = "\x00\x01"; /* header_table_size */
726
727 write_n32(str + 2, h2_settings_header_table_size);
728 chunk_memcat(&buf, str, 6);
729 }
730
731 if (h2_settings_initial_window_size != 65535) {
732 char str[6] = "\x00\x04"; /* initial_window_size */
733
734 write_n32(str + 2, h2_settings_initial_window_size);
735 chunk_memcat(&buf, str, 6);
736 }
737
738 if (h2_settings_max_concurrent_streams != 0) {
739 char str[6] = "\x00\x03"; /* max_concurrent_streams */
740
741 /* Note: 0 means "unlimited" for haproxy's config but not for
742 * the protocol, so never send this value!
743 */
744 write_n32(str + 2, h2_settings_max_concurrent_streams);
745 chunk_memcat(&buf, str, 6);
746 }
747
748 if (global.tune.bufsize != 16384) {
749 char str[6] = "\x00\x05"; /* max_frame_size */
750
751 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
752 * match bufsize - rewrite size, but at the moment it seems
753 * that clients don't take care of it.
754 */
755 write_n32(str + 2, global.tune.bufsize);
756 chunk_memcat(&buf, str, 6);
757 }
758
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 h2_set_frame_size(buf.area, buf.data - 9);
760 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200761 if (unlikely(ret <= 0)) {
762 if (!ret) {
763 h2c->flags |= H2_CF_MUX_MFULL;
764 h2c->flags |= H2_CF_DEM_MROOM;
765 return 0;
766 }
767 else {
768 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
769 return 0;
770 }
771 }
772 return ret;
773}
774
Willy Tarreau52eed752017-09-22 15:05:09 +0200775/* Try to receive a connection preface, then upon success try to send our
776 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
777 * missing data. It may return an error in h2c.
778 */
779static int h2c_frt_recv_preface(struct h2c *h2c)
780{
781 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200782 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200783
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200784 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200785
786 if (unlikely(ret1 <= 0)) {
787 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
788 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
789 return 0;
790 }
791
Willy Tarreaube5b7152017-09-25 16:25:39 +0200792 ret2 = h2c_snd_settings(h2c);
793 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200794 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200795
Willy Tarreaube5b7152017-09-25 16:25:39 +0200796 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200797}
798
Willy Tarreau081d4722017-05-16 21:51:05 +0200799/* try to send a GOAWAY frame on the connection to report an error or a graceful
800 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
801 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
802 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
803 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
804 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
805 * on unrecoverable failure. It will not attempt to send one again in this last
806 * case so that it is safe to use h2c_error() to report such errors.
807 */
808static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
809{
810 struct buffer *res;
811 char str[17];
812 int ret;
813
814 if (h2c->flags & H2_CF_GOAWAY_FAILED)
815 return 1; // claim that it worked
816
817 if (h2c_mux_busy(h2c, h2s)) {
818 if (h2s)
819 h2s->flags |= H2_SF_BLK_MBUSY;
820 else
821 h2c->flags |= H2_CF_DEM_MBUSY;
822 return 0;
823 }
824
Willy Tarreau44e973f2018-03-01 17:49:30 +0100825 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200826 if (!res) {
827 h2c->flags |= H2_CF_MUX_MALLOC;
828 if (h2s)
829 h2s->flags |= H2_SF_BLK_MROOM;
830 else
831 h2c->flags |= H2_CF_DEM_MROOM;
832 return 0;
833 }
834
835 /* len: 8, type: 7, flags: none, sid: 0 */
836 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
837
838 if (h2c->last_sid < 0)
839 h2c->last_sid = h2c->max_id;
840
841 write_n32(str + 9, h2c->last_sid);
842 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200843 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200844 if (unlikely(ret <= 0)) {
845 if (!ret) {
846 h2c->flags |= H2_CF_MUX_MFULL;
847 if (h2s)
848 h2s->flags |= H2_SF_BLK_MROOM;
849 else
850 h2c->flags |= H2_CF_DEM_MROOM;
851 return 0;
852 }
853 else {
854 /* we cannot report this error using GOAWAY, so we mark
855 * it and claim a success.
856 */
857 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
858 h2c->flags |= H2_CF_GOAWAY_FAILED;
859 return 1;
860 }
861 }
862 h2c->flags |= H2_CF_GOAWAY_SENT;
863 return ret;
864}
865
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100866/* Try to send an RST_STREAM frame on the connection for the indicated stream
867 * during mux operations. This stream must be valid and cannot be closed
868 * already. h2s->id will be used for the stream ID and h2s->errcode will be
869 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
870 * not yet.
871 *
872 * Returns > 0 on success or zero if nothing was done. In case of lack of room
873 * to write the message, it subscribes the stream to future notifications.
874 */
875static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
876{
877 struct buffer *res;
878 char str[13];
879 int ret;
880
881 if (!h2s || h2s->st == H2_SS_CLOSED)
882 return 1;
883
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100884 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
885 * RST_STREAM in response to a RST_STREAM frame.
886 */
887 if (h2c->dft == H2_FT_RST_STREAM) {
888 ret = 1;
889 goto ignore;
890 }
891
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100892 if (h2c_mux_busy(h2c, h2s)) {
893 h2s->flags |= H2_SF_BLK_MBUSY;
894 return 0;
895 }
896
Willy Tarreau44e973f2018-03-01 17:49:30 +0100897 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100898 if (!res) {
899 h2c->flags |= H2_CF_MUX_MALLOC;
900 h2s->flags |= H2_SF_BLK_MROOM;
901 return 0;
902 }
903
904 /* len: 4, type: 3, flags: none */
905 memcpy(str, "\x00\x00\x04\x03\x00", 5);
906 write_n32(str + 5, h2s->id);
907 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200908 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100909
910 if (unlikely(ret <= 0)) {
911 if (!ret) {
912 h2c->flags |= H2_CF_MUX_MFULL;
913 h2s->flags |= H2_SF_BLK_MROOM;
914 return 0;
915 }
916 else {
917 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
918 return 0;
919 }
920 }
921
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100922 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100924 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100925 return ret;
926}
927
928/* Try to send an RST_STREAM frame on the connection for the stream being
929 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
930 * error code unless the stream's state already is IDLE or CLOSED in which
931 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
932 * it was not yet.
933 *
934 * Returns > 0 on success or zero if nothing was done. In case of lack of room
935 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100937 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200938 */
939static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
940{
941 struct buffer *res;
942 char str[13];
943 int ret;
944
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100945 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
946 * RST_STREAM in response to a RST_STREAM frame.
947 */
948 if (h2c->dft == H2_FT_RST_STREAM) {
949 ret = 1;
950 goto ignore;
951 }
952
Willy Tarreau27a84c92017-10-17 08:10:17 +0200953 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100954 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200955 return 0;
956 }
957
Willy Tarreau44e973f2018-03-01 17:49:30 +0100958 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200959 if (!res) {
960 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100961 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return 0;
963 }
964
965 /* len: 4, type: 3, flags: none */
966 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100967
Willy Tarreau27a84c92017-10-17 08:10:17 +0200968 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100969 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200970 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200971 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100972
Willy Tarreau27a84c92017-10-17 08:10:17 +0200973 if (unlikely(ret <= 0)) {
974 if (!ret) {
975 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100976 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 return 0;
978 }
979 else {
980 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
981 return 0;
982 }
983 }
984
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100985 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100986 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200987 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100988 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100989 }
990
Willy Tarreau27a84c92017-10-17 08:10:17 +0200991 return ret;
992}
993
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100994/* try to send an empty DATA frame with the ES flag set to notify about the
995 * end of stream and match a shutdown(write). If an ES was already sent as
996 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
997 * on success or zero if nothing was done. In case of lack of room to write the
998 * message, it subscribes the requesting stream to future notifications.
999 */
1000static int h2_send_empty_data_es(struct h2s *h2s)
1001{
1002 struct h2c *h2c = h2s->h2c;
1003 struct buffer *res;
1004 char str[9];
1005 int ret;
1006
Willy Tarreau721c9742017-11-07 11:05:42 +01001007 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001008 return 1;
1009
1010 if (h2c_mux_busy(h2c, h2s)) {
1011 h2s->flags |= H2_SF_BLK_MBUSY;
1012 return 0;
1013 }
1014
Willy Tarreau44e973f2018-03-01 17:49:30 +01001015 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001016 if (!res) {
1017 h2c->flags |= H2_CF_MUX_MALLOC;
1018 h2s->flags |= H2_SF_BLK_MROOM;
1019 return 0;
1020 }
1021
1022 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1023 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1024 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001025 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001026 if (likely(ret > 0)) {
1027 h2s->flags |= H2_SF_ES_SENT;
1028 }
1029 else if (!ret) {
1030 h2c->flags |= H2_CF_MUX_MFULL;
1031 h2s->flags |= H2_SF_BLK_MROOM;
1032 return 0;
1033 }
1034 else {
1035 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1036 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001037 }
1038 return ret;
1039}
1040
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001041/* wake the streams attached to the connection, whose id is greater than <last>,
1042 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1043 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1044 * stream's state is automatically updated accordingly.
1045 */
1046static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1047{
1048 struct eb32_node *node;
1049 struct h2s *h2s;
1050
1051 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1052 flags |= CS_FL_ERROR;
1053
1054 if (conn_xprt_read0_pending(h2c->conn))
1055 flags |= CS_FL_EOS;
1056
1057 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1058 while (node) {
1059 h2s = container_of(node, struct h2s, by_id);
1060 if (h2s->id <= last)
1061 break;
1062 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001063
1064 if (!h2s->cs) {
1065 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001066 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001067 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001068 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001069
1070 h2s->cs->flags |= flags;
1071 /* recv is used to force to detect CS_FL_EOS that wake()
1072 * doesn't handle in the stream int code.
1073 */
1074 h2s->cs->data_cb->recv(h2s->cs);
1075 h2s->cs->data_cb->wake(h2s->cs);
1076
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001077 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1078 h2s->st = H2_SS_ERROR;
1079 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1080 h2s->st = H2_SS_HREM;
1081 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001082 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001083 }
1084}
1085
Willy Tarreau3421aba2017-07-27 15:41:03 +02001086/* Increase all streams' outgoing window size by the difference passed in
1087 * argument. This is needed upon receipt of the settings frame if the initial
1088 * window size is different. The difference may be negative and the resulting
1089 * window size as well, for the time it takes to receive some window updates.
1090 */
1091static void h2c_update_all_ws(struct h2c *h2c, int diff)
1092{
1093 struct h2s *h2s;
1094 struct eb32_node *node;
1095
1096 if (!diff)
1097 return;
1098
1099 node = eb32_first(&h2c->streams_by_id);
1100 while (node) {
1101 h2s = container_of(node, struct h2s, by_id);
1102 h2s->mws += diff;
1103 node = eb32_next(node);
1104 }
1105}
1106
1107/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1108 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1109 * return an error in h2c. Described in RFC7540#6.5.
1110 */
1111static int h2c_handle_settings(struct h2c *h2c)
1112{
1113 unsigned int offset;
1114 int error;
1115
1116 if (h2c->dff & H2_F_SETTINGS_ACK) {
1117 if (h2c->dfl) {
1118 error = H2_ERR_FRAME_SIZE_ERROR;
1119 goto fail;
1120 }
1121 return 1;
1122 }
1123
1124 if (h2c->dsi != 0) {
1125 error = H2_ERR_PROTOCOL_ERROR;
1126 goto fail;
1127 }
1128
1129 if (h2c->dfl % 6) {
1130 error = H2_ERR_FRAME_SIZE_ERROR;
1131 goto fail;
1132 }
1133
1134 /* that's the limit we can process */
1135 if (h2c->dfl > global.tune.bufsize) {
1136 error = H2_ERR_FRAME_SIZE_ERROR;
1137 goto fail;
1138 }
1139
1140 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001141 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001142 return 0;
1143
1144 /* parse the frame */
1145 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001146 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1147 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001148
1149 switch (type) {
1150 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1151 /* we need to update all existing streams with the
1152 * difference from the previous iws.
1153 */
1154 if (arg < 0) { // RFC7540#6.5.2
1155 error = H2_ERR_FLOW_CONTROL_ERROR;
1156 goto fail;
1157 }
1158 h2c_update_all_ws(h2c, arg - h2c->miw);
1159 h2c->miw = arg;
1160 break;
1161 case H2_SETTINGS_MAX_FRAME_SIZE:
1162 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1163 error = H2_ERR_PROTOCOL_ERROR;
1164 goto fail;
1165 }
1166 h2c->mfs = arg;
1167 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001168 case H2_SETTINGS_ENABLE_PUSH:
1169 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1170 error = H2_ERR_PROTOCOL_ERROR;
1171 goto fail;
1172 }
1173 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001174 }
1175 }
1176
1177 /* need to ACK this frame now */
1178 h2c->st0 = H2_CS_FRAME_A;
1179 return 1;
1180 fail:
1181 h2c_error(h2c, error);
1182 return 0;
1183}
1184
1185/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1186 * success or one of the h2_status values.
1187 */
1188static int h2c_ack_settings(struct h2c *h2c)
1189{
1190 struct buffer *res;
1191 char str[9];
1192 int ret = -1;
1193
1194 if (h2c_mux_busy(h2c, NULL)) {
1195 h2c->flags |= H2_CF_DEM_MBUSY;
1196 return 0;
1197 }
1198
Willy Tarreau44e973f2018-03-01 17:49:30 +01001199 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001200 if (!res) {
1201 h2c->flags |= H2_CF_MUX_MALLOC;
1202 h2c->flags |= H2_CF_DEM_MROOM;
1203 return 0;
1204 }
1205
1206 memcpy(str,
1207 "\x00\x00\x00" /* length : 0 (no data) */
1208 "\x04" "\x01" /* type : 4, flags : ACK */
1209 "\x00\x00\x00\x00" /* stream ID */, 9);
1210
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001211 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001212 if (unlikely(ret <= 0)) {
1213 if (!ret) {
1214 h2c->flags |= H2_CF_MUX_MFULL;
1215 h2c->flags |= H2_CF_DEM_MROOM;
1216 return 0;
1217 }
1218 else {
1219 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1220 return 0;
1221 }
1222 }
1223 return ret;
1224}
1225
Willy Tarreaucf68c782017-10-10 17:11:41 +02001226/* processes a PING frame and schedules an ACK if needed. The caller must pass
1227 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1228 * missing data. It may return an error in h2c.
1229 */
1230static int h2c_handle_ping(struct h2c *h2c)
1231{
1232 /* frame length must be exactly 8 */
1233 if (h2c->dfl != 8) {
1234 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1235 return 0;
1236 }
1237
1238 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001239 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001240 h2c->st0 = H2_CS_FRAME_A;
1241 return 1;
1242}
1243
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001244/* Try to send a window update for stream id <sid> and value <increment>.
1245 * Returns > 0 on success or zero on missing room or failure. It may return an
1246 * error in h2c.
1247 */
1248static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1249{
1250 struct buffer *res;
1251 char str[13];
1252 int ret = -1;
1253
1254 if (h2c_mux_busy(h2c, NULL)) {
1255 h2c->flags |= H2_CF_DEM_MBUSY;
1256 return 0;
1257 }
1258
Willy Tarreau44e973f2018-03-01 17:49:30 +01001259 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001260 if (!res) {
1261 h2c->flags |= H2_CF_MUX_MALLOC;
1262 h2c->flags |= H2_CF_DEM_MROOM;
1263 return 0;
1264 }
1265
1266 /* length: 4, type: 8, flags: none */
1267 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1268 write_n32(str + 5, sid);
1269 write_n32(str + 9, increment);
1270
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001271 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001272
1273 if (unlikely(ret <= 0)) {
1274 if (!ret) {
1275 h2c->flags |= H2_CF_MUX_MFULL;
1276 h2c->flags |= H2_CF_DEM_MROOM;
1277 return 0;
1278 }
1279 else {
1280 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1281 return 0;
1282 }
1283 }
1284 return ret;
1285}
1286
1287/* try to send pending window update for the connection. It's safe to call it
1288 * with no pending updates. Returns > 0 on success or zero on missing room or
1289 * failure. It may return an error in h2c.
1290 */
1291static int h2c_send_conn_wu(struct h2c *h2c)
1292{
1293 int ret = 1;
1294
1295 if (h2c->rcvd_c <= 0)
1296 return 1;
1297
1298 /* send WU for the connection */
1299 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1300 if (ret > 0)
1301 h2c->rcvd_c = 0;
1302
1303 return ret;
1304}
1305
1306/* try to send pending window update for the current dmux stream. It's safe to
1307 * call it with no pending updates. Returns > 0 on success or zero on missing
1308 * room or failure. It may return an error in h2c.
1309 */
1310static int h2c_send_strm_wu(struct h2c *h2c)
1311{
1312 int ret = 1;
1313
1314 if (h2c->rcvd_s <= 0)
1315 return 1;
1316
1317 /* send WU for the stream */
1318 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1319 if (ret > 0)
1320 h2c->rcvd_s = 0;
1321
1322 return ret;
1323}
1324
Willy Tarreaucf68c782017-10-10 17:11:41 +02001325/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1326 * success, 0 on missing data or one of the h2_status values.
1327 */
1328static int h2c_ack_ping(struct h2c *h2c)
1329{
1330 struct buffer *res;
1331 char str[17];
1332 int ret = -1;
1333
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001334 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001335 return 0;
1336
1337 if (h2c_mux_busy(h2c, NULL)) {
1338 h2c->flags |= H2_CF_DEM_MBUSY;
1339 return 0;
1340 }
1341
Willy Tarreau44e973f2018-03-01 17:49:30 +01001342 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001343 if (!res) {
1344 h2c->flags |= H2_CF_MUX_MALLOC;
1345 h2c->flags |= H2_CF_DEM_MROOM;
1346 return 0;
1347 }
1348
1349 memcpy(str,
1350 "\x00\x00\x08" /* length : 8 (same payload) */
1351 "\x06" "\x01" /* type : 6, flags : ACK */
1352 "\x00\x00\x00\x00" /* stream ID */, 9);
1353
1354 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001355 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001356
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001357 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001358 if (unlikely(ret <= 0)) {
1359 if (!ret) {
1360 h2c->flags |= H2_CF_MUX_MFULL;
1361 h2c->flags |= H2_CF_DEM_MROOM;
1362 return 0;
1363 }
1364 else {
1365 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1366 return 0;
1367 }
1368 }
1369 return ret;
1370}
1371
Willy Tarreau26f95952017-07-27 17:18:30 +02001372/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1373 * Returns > 0 on success or zero on missing data. It may return an error in
1374 * h2c or h2s. Described in RFC7540#6.9.
1375 */
1376static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1377{
1378 int32_t inc;
1379 int error;
1380
1381 if (h2c->dfl != 4) {
1382 error = H2_ERR_FRAME_SIZE_ERROR;
1383 goto conn_err;
1384 }
1385
1386 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001387 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001388 return 0;
1389
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001390 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001391
1392 if (h2c->dsi != 0) {
1393 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001394
1395 /* it's not an error to receive WU on a closed stream */
1396 if (h2s->st == H2_SS_CLOSED)
1397 return 1;
1398
1399 if (!inc) {
1400 error = H2_ERR_PROTOCOL_ERROR;
1401 goto strm_err;
1402 }
1403
1404 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1405 error = H2_ERR_FLOW_CONTROL_ERROR;
1406 goto strm_err;
1407 }
1408
1409 h2s->mws += inc;
1410 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1411 h2s->flags &= ~H2_SF_BLK_SFCTL;
1412 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1413 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1414 /* This stream wanted to send but could not due to its
1415 * own flow control. We can put it back into the send
1416 * list now, it will be handled upon next send() call.
1417 */
1418 LIST_ADDQ(&h2c->send_list, &h2s->list);
1419 }
1420 }
1421 }
1422 else {
1423 /* connection window update */
1424 if (!inc) {
1425 error = H2_ERR_PROTOCOL_ERROR;
1426 goto conn_err;
1427 }
1428
1429 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1430 error = H2_ERR_FLOW_CONTROL_ERROR;
1431 goto conn_err;
1432 }
1433
1434 h2c->mws += inc;
1435 }
1436
1437 return 1;
1438
1439 conn_err:
1440 h2c_error(h2c, error);
1441 return 0;
1442
1443 strm_err:
1444 if (h2s) {
1445 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001446 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001447 }
1448 else
1449 h2c_error(h2c, error);
1450 return 0;
1451}
1452
Willy Tarreaue96b0922017-10-30 00:28:29 +01001453/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1454 * the last ID. Returns > 0 on success or zero on missing data. It may return
1455 * an error in h2c. Described in RFC7540#6.8.
1456 */
1457static int h2c_handle_goaway(struct h2c *h2c)
1458{
1459 int error;
1460 int last;
1461
1462 if (h2c->dsi != 0) {
1463 error = H2_ERR_PROTOCOL_ERROR;
1464 goto conn_err;
1465 }
1466
1467 if (h2c->dfl < 8) {
1468 error = H2_ERR_FRAME_SIZE_ERROR;
1469 goto conn_err;
1470 }
1471
1472 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001473 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001474 return 0;
1475
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001476 last = h2_get_n32(&h2c->dbuf, 0);
1477 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001478 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001479 if (h2c->last_sid < 0)
1480 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001481 return 1;
1482
1483 conn_err:
1484 h2c_error(h2c, error);
1485 return 0;
1486}
1487
Willy Tarreau92153fc2017-12-03 19:46:19 +01001488/* processes a PRIORITY frame, and either skips it or rejects if it is
1489 * invalid. Returns > 0 on success or zero on missing data. It may return
1490 * an error in h2c. Described in RFC7540#6.3.
1491 */
1492static int h2c_handle_priority(struct h2c *h2c)
1493{
1494 int error;
1495
1496 if (h2c->dsi == 0) {
1497 error = H2_ERR_PROTOCOL_ERROR;
1498 goto conn_err;
1499 }
1500
1501 if (h2c->dfl != 5) {
1502 error = H2_ERR_FRAME_SIZE_ERROR;
1503 goto conn_err;
1504 }
1505
1506 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001507 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001508 return 0;
1509
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001510 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001511 /* 7540#5.3 : can't depend on itself */
1512 error = H2_ERR_PROTOCOL_ERROR;
1513 goto conn_err;
1514 }
1515 return 1;
1516
1517 conn_err:
1518 h2c_error(h2c, error);
1519 return 0;
1520}
1521
Willy Tarreaucd234e92017-08-18 10:59:39 +02001522/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1523 * Returns > 0 on success or zero on missing data. It may return an error in
1524 * h2c. Described in RFC7540#6.4.
1525 */
1526static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1527{
1528 int error;
1529
1530 if (h2c->dsi == 0) {
1531 error = H2_ERR_PROTOCOL_ERROR;
1532 goto conn_err;
1533 }
1534
Willy Tarreaucd234e92017-08-18 10:59:39 +02001535 if (h2c->dfl != 4) {
1536 error = H2_ERR_FRAME_SIZE_ERROR;
1537 goto conn_err;
1538 }
1539
1540 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001541 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001542 return 0;
1543
1544 /* late RST, already handled */
1545 if (h2s->st == H2_SS_CLOSED)
1546 return 1;
1547
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001548 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001549 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001550
1551 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001552 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001553 /* recv is used to force to detect CS_FL_EOS that wake()
1554 * doesn't handle in the stream-int code.
1555 */
1556 h2s->cs->data_cb->recv(h2s->cs);
1557 h2s->cs->data_cb->wake(h2s->cs);
1558 }
1559
1560 h2s->flags |= H2_SF_RST_RCVD;
1561 return 1;
1562
1563 conn_err:
1564 h2c_error(h2c, error);
1565 return 0;
1566}
1567
Willy Tarreau13278b42017-10-13 19:23:14 +02001568/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1569 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1570 * 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 */
1573static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1574{
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 Tarreau13278b42017-10-13 19:23:14 +02001583 return 0; // empty buffer
1584
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001585 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001586 return 0; // incomplete frame
1587
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;
1615 }
1616
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001617 if (!h2_frt_decode_headers(h2s))
1618 return 0;
1619
Willy Tarreau13278b42017-10-13 19:23:14 +02001620 /* call the upper layers to process the frame, then let the upper layer
1621 * notify the stream about any change.
1622 */
1623 h2s->cs->data_cb->recv(h2s->cs);
1624
1625 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1626 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1627 error = H2_ERR_INTERNAL_ERROR;
1628 goto conn_err;
1629 }
1630
Willy Tarreau8f650c32017-11-21 19:36:21 +01001631 if (h2c->st0 >= H2_CS_ERROR)
1632 return 0;
1633
Willy Tarreau721c9742017-11-07 11:05:42 +01001634 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001635 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001636 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001637 }
1638 else {
1639 /* update the max stream ID if the request is being processed */
1640 if (h2s->id > h2c->max_id)
1641 h2c->max_id = h2s->id;
1642 }
1643
1644 return 1;
1645
1646 conn_err:
1647 h2c_error(h2c, error);
1648 return 0;
1649
1650 strm_err:
1651 if (h2s) {
1652 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001653 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001654 }
1655 else
1656 h2c_error(h2c, error);
1657 return 0;
1658}
1659
Willy Tarreau454f9052017-10-26 19:40:35 +02001660/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1661 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1662 */
1663static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1664{
1665 int error;
1666
1667 /* note that empty DATA frames are perfectly valid and sometimes used
1668 * to signal an end of stream (with the ES flag).
1669 */
1670
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001671 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001672 return 0; // empty buffer
1673
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001674 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001675 return 0; // incomplete frame
1676
1677 /* now either the frame is complete or the buffer is complete */
1678
1679 if (!h2c->dsi) {
1680 /* RFC7540#6.1 */
1681 error = H2_ERR_PROTOCOL_ERROR;
1682 goto conn_err;
1683 }
1684
1685 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1686 /* RFC7540#6.1 */
1687 error = H2_ERR_STREAM_CLOSED;
1688 goto strm_err;
1689 }
1690
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001691 if (!h2_frt_transfer_data(h2s))
1692 return 0;
1693
Willy Tarreau454f9052017-10-26 19:40:35 +02001694 /* call the upper layers to process the frame, then let the upper layer
1695 * notify the stream about any change.
1696 */
1697 if (!h2s->cs) {
1698 error = H2_ERR_STREAM_CLOSED;
1699 goto strm_err;
1700 }
1701
1702 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001703
Willy Tarreau454f9052017-10-26 19:40:35 +02001704 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1705 /* cs has just been destroyed, we have to kill h2s. */
1706 error = H2_ERR_STREAM_CLOSED;
1707 goto strm_err;
1708 }
1709
Willy Tarreau8f650c32017-11-21 19:36:21 +01001710 if (h2c->st0 >= H2_CS_ERROR)
1711 return 0;
1712
Willy Tarreau721c9742017-11-07 11:05:42 +01001713 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001714 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001715 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001716 }
1717
1718 /* check for completion : the callee will change this to FRAME_A or
1719 * FRAME_H once done.
1720 */
1721 if (h2c->st0 == H2_CS_FRAME_P)
1722 return 0;
1723
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001724
1725 /* last frame */
1726 if (h2c->dff & H2_F_DATA_END_STREAM) {
1727 h2s->st = H2_SS_HREM;
1728 h2s->flags |= H2_SF_ES_RCVD;
1729 }
1730
Willy Tarreau454f9052017-10-26 19:40:35 +02001731 return 1;
1732
1733 conn_err:
1734 h2c_error(h2c, error);
1735 return 0;
1736
1737 strm_err:
1738 if (h2s) {
1739 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001740 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001741 }
1742 else
1743 h2c_error(h2c, error);
1744 return 0;
1745}
1746
Willy Tarreaubc933932017-10-09 16:21:43 +02001747/* process Rx frames to be demultiplexed */
1748static void h2_process_demux(struct h2c *h2c)
1749{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001750 struct h2s *h2s;
1751
Willy Tarreau081d4722017-05-16 21:51:05 +02001752 if (h2c->st0 >= H2_CS_ERROR)
1753 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001754
1755 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1756 if (h2c->st0 == H2_CS_PREFACE) {
1757 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1758 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1759 if (h2c->st0 == H2_CS_ERROR)
1760 h2c->st0 = H2_CS_ERROR2;
1761 goto fail;
1762 }
1763
1764 h2c->max_id = 0;
1765 h2c->st0 = H2_CS_SETTINGS1;
1766 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001767
1768 if (h2c->st0 == H2_CS_SETTINGS1) {
1769 struct h2_fh hdr;
1770
1771 /* ensure that what is pending is a valid SETTINGS frame
1772 * without an ACK.
1773 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001774 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001775 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1776 if (h2c->st0 == H2_CS_ERROR)
1777 h2c->st0 = H2_CS_ERROR2;
1778 goto fail;
1779 }
1780
1781 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1782 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1783 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1784 h2c->st0 = H2_CS_ERROR2;
1785 goto fail;
1786 }
1787
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001788 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001789 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1790 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1791 h2c->st0 = H2_CS_ERROR2;
1792 goto fail;
1793 }
1794
1795 /* that's OK, switch to FRAME_P to process it */
1796 h2c->dfl = hdr.len;
1797 h2c->dsi = hdr.sid;
1798 h2c->dft = hdr.ft;
1799 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001800 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001801 h2c->st0 = H2_CS_FRAME_P;
1802 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001803 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001804
1805 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001806 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001807 int ret = 0;
1808
1809 if (h2c->st0 >= H2_CS_ERROR)
1810 break;
1811
1812 if (h2c->st0 == H2_CS_FRAME_H) {
1813 struct h2_fh hdr;
1814
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001815 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001816 break;
1817
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001818 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001819 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1820 h2c->st0 = H2_CS_ERROR;
1821 break;
1822 }
1823
1824 h2c->dfl = hdr.len;
1825 h2c->dsi = hdr.sid;
1826 h2c->dft = hdr.ft;
1827 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001828 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001829 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001830 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001831 }
1832
1833 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001834 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001835
Willy Tarreaud7901432017-12-29 11:34:40 +01001836 if (h2c->st0 == H2_CS_FRAME_E)
1837 goto strm_err;
1838
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001839 if (h2s->st == H2_SS_IDLE &&
1840 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1841 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1842 * this state MUST be treated as a connection error
1843 */
1844 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1845 h2c->st0 = H2_CS_ERROR;
1846 break;
1847 }
1848
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001849 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1850 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1851 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1852 * this state MUST be treated as a stream error
1853 */
1854 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001855 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001856 goto strm_err;
1857 }
1858
Willy Tarreauab837502017-12-27 15:07:30 +01001859 /* Below the management of frames received in closed state is a
1860 * bit hackish because the spec makes strong differences between
1861 * streams closed by receiving RST, sending RST, and seeing ES
1862 * in both directions. In addition to this, the creation of a
1863 * new stream reusing the identifier of a closed one will be
1864 * detected here. Given that we cannot keep track of all closed
1865 * streams forever, we consider that unknown closed streams were
1866 * closed on RST received, which allows us to respond with an
1867 * RST without breaking the connection (eg: to abort a transfer).
1868 * Some frames have to be silently ignored as well.
1869 */
1870 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1871 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1872 /* #5.1.1: The identifier of a newly
1873 * established stream MUST be numerically
1874 * greater than all streams that the initiating
1875 * endpoint has opened or reserved. This
1876 * governs streams that are opened using a
1877 * HEADERS frame and streams that are reserved
1878 * using PUSH_PROMISE. An endpoint that
1879 * receives an unexpected stream identifier
1880 * MUST respond with a connection error.
1881 */
1882 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1883 goto strm_err;
1884 }
1885
1886 if (h2s->flags & H2_SF_RST_RCVD) {
1887 /* RFC7540#5.1:closed: an endpoint that
1888 * receives any frame other than PRIORITY after
1889 * receiving a RST_STREAM MUST treat that as a
1890 * stream error of type STREAM_CLOSED.
1891 *
1892 * Note that old streams fall into this category
1893 * and will lead to an RST being sent.
1894 */
1895 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1896 h2c->st0 = H2_CS_FRAME_E;
1897 goto strm_err;
1898 }
1899
1900 /* RFC7540#5.1:closed: if this state is reached as a
1901 * result of sending a RST_STREAM frame, the peer that
1902 * receives the RST_STREAM might have already sent
1903 * frames on the stream that cannot be withdrawn. An
1904 * endpoint MUST ignore frames that it receives on
1905 * closed streams after it has sent a RST_STREAM
1906 * frame. An endpoint MAY choose to limit the period
1907 * over which it ignores frames and treat frames that
1908 * arrive after this time as being in error.
1909 */
1910 if (!(h2s->flags & H2_SF_RST_SENT)) {
1911 /* RFC7540#5.1:closed: any frame other than
1912 * PRIO/WU/RST in this state MUST be treated as
1913 * a connection error
1914 */
1915 if (h2c->dft != H2_FT_RST_STREAM &&
1916 h2c->dft != H2_FT_PRIORITY &&
1917 h2c->dft != H2_FT_WINDOW_UPDATE) {
1918 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1919 goto strm_err;
1920 }
1921 }
1922 }
1923
Willy Tarreauc0da1962017-10-30 18:38:00 +01001924#if 0
1925 // problem below: it is not possible to completely ignore such
1926 // streams as we need to maintain the compression state as well
1927 // and for this we need to completely process these frames (eg:
1928 // HEADERS frames) as well as counting DATA frames to emit
1929 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1930 // This is a typical case of layer violation where the
1931 // transported contents are critical to the connection's
1932 // validity and must be ignored at the same time :-(
1933
1934 /* graceful shutdown, ignore streams whose ID is higher than
1935 * the one advertised in GOAWAY. RFC7540#6.8.
1936 */
1937 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001938 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1939 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001940 h2c->dfl -= ret;
1941 ret = h2c->dfl == 0;
1942 goto strm_err;
1943 }
1944#endif
1945
Willy Tarreau7e98c052017-10-10 15:56:59 +02001946 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001947 case H2_FT_SETTINGS:
1948 if (h2c->st0 == H2_CS_FRAME_P)
1949 ret = h2c_handle_settings(h2c);
1950
1951 if (h2c->st0 == H2_CS_FRAME_A)
1952 ret = h2c_ack_settings(h2c);
1953 break;
1954
Willy Tarreaucf68c782017-10-10 17:11:41 +02001955 case H2_FT_PING:
1956 if (h2c->st0 == H2_CS_FRAME_P)
1957 ret = h2c_handle_ping(h2c);
1958
1959 if (h2c->st0 == H2_CS_FRAME_A)
1960 ret = h2c_ack_ping(h2c);
1961 break;
1962
Willy Tarreau26f95952017-07-27 17:18:30 +02001963 case H2_FT_WINDOW_UPDATE:
1964 if (h2c->st0 == H2_CS_FRAME_P)
1965 ret = h2c_handle_window_update(h2c, h2s);
1966 break;
1967
Willy Tarreau61290ec2017-10-17 08:19:21 +02001968 case H2_FT_CONTINUATION:
1969 /* we currently don't support CONTINUATION frames since
1970 * we have nowhere to store the partial HEADERS frame.
1971 * Let's abort the stream on an INTERNAL_ERROR here.
1972 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001973 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001974 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001975 h2c->st0 = H2_CS_FRAME_E;
1976 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001977 break;
1978
Willy Tarreau13278b42017-10-13 19:23:14 +02001979 case H2_FT_HEADERS:
1980 if (h2c->st0 == H2_CS_FRAME_P)
1981 ret = h2c_frt_handle_headers(h2c, h2s);
1982 break;
1983
Willy Tarreau454f9052017-10-26 19:40:35 +02001984 case H2_FT_DATA:
1985 if (h2c->st0 == H2_CS_FRAME_P)
1986 ret = h2c_frt_handle_data(h2c, h2s);
1987
1988 if (h2c->st0 == H2_CS_FRAME_A)
1989 ret = h2c_send_strm_wu(h2c);
1990 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001991
Willy Tarreau92153fc2017-12-03 19:46:19 +01001992 case H2_FT_PRIORITY:
1993 if (h2c->st0 == H2_CS_FRAME_P)
1994 ret = h2c_handle_priority(h2c);
1995 break;
1996
Willy Tarreaucd234e92017-08-18 10:59:39 +02001997 case H2_FT_RST_STREAM:
1998 if (h2c->st0 == H2_CS_FRAME_P)
1999 ret = h2c_handle_rst_stream(h2c, h2s);
2000 break;
2001
Willy Tarreaue96b0922017-10-30 00:28:29 +01002002 case H2_FT_GOAWAY:
2003 if (h2c->st0 == H2_CS_FRAME_P)
2004 ret = h2c_handle_goaway(h2c);
2005 break;
2006
Willy Tarreau1c661982017-10-30 13:52:01 +01002007 case H2_FT_PUSH_PROMISE:
2008 /* not permitted here, RFC7540#5.1 */
2009 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002010 break;
2011
2012 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002013 default:
2014 /* drop frames that we ignore. They may be larger than
2015 * the buffer so we drain all of their contents until
2016 * we reach the end.
2017 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002018 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2019 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002020 h2c->dfl -= ret;
2021 ret = h2c->dfl == 0;
2022 }
2023
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002024 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002025 /* We may have to send an RST if not done yet */
2026 if (h2s->st == H2_SS_ERROR)
2027 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002028
Willy Tarreaua20a5192017-12-27 11:02:06 +01002029 if (h2c->st0 == H2_CS_FRAME_E)
2030 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002031
Willy Tarreau7e98c052017-10-10 15:56:59 +02002032 /* error or missing data condition met above ? */
2033 if (ret <= 0)
2034 break;
2035
2036 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002037 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002038 h2c->st0 = H2_CS_FRAME_H;
2039 }
2040 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002041
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002042 if (h2c->rcvd_c > 0 &&
2043 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2044 h2c_send_conn_wu(h2c);
2045
Willy Tarreau52eed752017-09-22 15:05:09 +02002046 fail:
2047 /* we can go here on missing data, blocked response or error */
2048 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002049}
2050
2051/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2052 * the end.
2053 */
2054static int h2_process_mux(struct h2c *h2c)
2055{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002056 struct h2s *h2s, *h2s_back;
2057
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002058 /* start by sending possibly pending window updates */
2059 if (h2c->rcvd_c > 0 &&
2060 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2061 h2c_send_conn_wu(h2c) < 0)
2062 goto fail;
2063
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002064 /* First we always process the flow control list because the streams
2065 * waiting there were already elected for immediate emission but were
2066 * blocked just on this.
2067 */
2068
2069 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2070 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2071 h2c->st0 >= H2_CS_ERROR)
2072 break;
2073
2074 /* In theory it's possible that h2s->cs == NULL here :
2075 * - client sends crap that causes a parse error
2076 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2077 * - RST_STREAM cannot be emitted because mux is busy/full
2078 * - stream gets notified, detaches and quits
2079 * - mux buffer gets ready and wakes pending streams up
2080 * - bam!
2081 */
2082 h2s->flags &= ~H2_SF_BLK_ANY;
2083
2084 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002085 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002086 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002087 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002088 }
2089
2090 /* depending on callee's blocking reasons, we may queue in send
2091 * list or completely dequeue.
2092 */
2093 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2094 if (h2s->flags & H2_SF_BLK_ANY) {
2095 LIST_DEL(&h2s->list);
2096 LIST_ADDQ(&h2c->send_list, &h2s->list);
2097 }
2098 else {
2099 LIST_DEL(&h2s->list);
2100 LIST_INIT(&h2s->list);
2101 if (h2s->cs)
2102 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002103 else {
2104 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002105 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002106 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002107 }
2108 }
2109 }
2110
2111 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2112 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2113 break;
2114
2115 /* In theory it's possible that h2s->cs == NULL here :
2116 * - client sends crap that causes a parse error
2117 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2118 * - RST_STREAM cannot be emitted because mux is busy/full
2119 * - stream gets notified, detaches and quits
2120 * - mux buffer gets ready and wakes pending streams up
2121 * - bam!
2122 */
2123 h2s->flags &= ~H2_SF_BLK_ANY;
2124
2125 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002126 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002127 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002128 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002129 }
2130 /* depending on callee's blocking reasons, we may queue in fctl
2131 * list or completely dequeue.
2132 */
2133 if (h2s->flags & H2_SF_BLK_MFCTL) {
2134 /* stream hit the connection's flow control */
2135 LIST_DEL(&h2s->list);
2136 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2137 }
2138 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2139 LIST_DEL(&h2s->list);
2140 LIST_INIT(&h2s->list);
2141 if (h2s->cs)
2142 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002143 else {
2144 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002145 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002146 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002147 }
2148 }
2149
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002150 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002151 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002152 if (h2c->st0 == H2_CS_ERROR) {
2153 if (h2c->max_id >= 0) {
2154 h2c_send_goaway_error(h2c, NULL);
2155 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2156 return 0;
2157 }
2158
2159 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2160 }
2161 return 1;
2162 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002163 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002164}
2165
Willy Tarreau71681172017-10-23 14:39:06 +02002166
Willy Tarreau62f52692017-10-08 23:01:42 +02002167/*********************************************************/
2168/* functions below are I/O callbacks from the connection */
2169/*********************************************************/
2170
2171/* callback called on recv event by the connection handler */
2172static void h2_recv(struct connection *conn)
2173{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002174 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002175 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002176 int max;
2177
Willy Tarreau315d8072017-12-10 22:17:57 +01002178 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002179 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002180
Willy Tarreau44e973f2018-03-01 17:49:30 +01002181 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002182 if (!buf) {
2183 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002184 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002185 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002186
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002187 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002188 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002189 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002190
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002191 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002192 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002193 return;
2194 }
2195
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002196 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002197 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002198 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002199}
2200
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002201/* Try to send data if possible */
2202static struct task *h2_send(struct task *t, void *ctx, unsigned short state)
Willy Tarreau62f52692017-10-08 23:01:42 +02002203{
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002204 struct connection *conn = ctx;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002205 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002206 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002207
2208 if (conn->flags & CO_FL_ERROR)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002209 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002210
2211 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2212 /* a handshake was requested */
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002213 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002214 }
2215
Willy Tarreaubc933932017-10-09 16:21:43 +02002216 /* This loop is quite simple : it tries to fill as much as it can from
2217 * pending streams into the existing buffer until it's reportedly full
2218 * or the end of send requests is reached. Then it tries to send this
2219 * buffer's contents out, marks it not full if at least one byte could
2220 * be sent, and tries again.
2221 *
2222 * The snd_buf() function normally takes a "flags" argument which may
2223 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2224 * data immediately comes and CO_SFL_STREAMER to indicate that the
2225 * connection is streaming lots of data (used to increase TLS record
2226 * size at the expense of latency). The former can be sent any time
2227 * there's a buffer full flag, as it indicates at least one stream
2228 * attempted to send and failed so there are pending data. An
2229 * alternative would be to set it as long as there's an active stream
2230 * but that would be problematic for ACKs until we have an absolute
2231 * guarantee that all waiters have at least one byte to send. The
2232 * latter should possibly not be set for now.
2233 */
2234
2235 done = 0;
2236 while (!done) {
2237 unsigned int flags = 0;
2238
2239 /* fill as much as we can into the current buffer */
2240 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2241 done = h2_process_mux(h2c);
2242
2243 if (conn->flags & CO_FL_ERROR)
2244 break;
2245
2246 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2247 flags |= CO_SFL_MSG_MORE;
2248
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002249 if (b_data(&h2c->mbuf)) {
2250 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002251 if (!ret)
2252 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002253 b_del(&h2c->mbuf, ret);
2254 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002255 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002256
2257 /* wrote at least one byte, the buffer is not full anymore */
2258 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2259 }
2260
Willy Tarreaua2af5122017-10-09 11:56:46 +02002261 if (conn->flags & CO_FL_SOCK_WR_SH) {
2262 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002263 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002264 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002265 /* We're not full anymore, so we can wake any task that are waiting
2266 * for us.
2267 */
2268 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2269 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2270 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2271 struct wait_list *, list);
2272 LIST_DEL(&sw->list);
2273 LIST_INIT(&sw->list);
2274 tasklet_wakeup(sw->task);
2275 }
2276
2277 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002278 /* We're done, no more to send */
2279 if (!b_data(&h2c->mbuf))
2280 return NULL;
2281schedule:
2282 if (LIST_ISEMPTY(&h2c->wait_list.list))
2283 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2284 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002285}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002286
Willy Tarreau62f52692017-10-08 23:01:42 +02002287/* callback called on any event by the connection handler.
2288 * It applies changes and returns zero, or < 0 if it wants immediate
2289 * destruction of the connection (which normally doesn not happen in h2).
2290 */
2291static int h2_wake(struct connection *conn)
2292{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002293 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002294 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002295
Olivier Houchardf495fc42018-07-20 18:15:23 +02002296 h2_send(NULL, conn, 0);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002297 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002298 h2_process_demux(h2c);
2299
2300 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002301 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002302
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002303 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002304 h2c->flags &= ~H2_CF_DEM_DFULL;
2305 }
2306
Willy Tarreau8ec14062017-12-30 18:08:13 +01002307 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2308 /* frontend is stopping, reload likely in progress, let's try
2309 * to announce a graceful shutdown if not yet done. We don't
2310 * care if it fails, it will be tried again later.
2311 */
2312 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2313 if (h2c->last_sid < 0)
2314 h2c->last_sid = (1U << 31) - 1;
2315 h2c_send_goaway_error(h2c, NULL);
2316 }
2317 }
2318
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002319 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002320 * If we received early data, and the handshake is done, wake
2321 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002322 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002323 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2324 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2325 struct eb32_node *node;
2326 struct h2s *h2s;
2327
2328 h2c->flags |= H2_CF_WAIT_FOR_HS;
2329 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2330
2331 while (node) {
2332 h2s = container_of(node, struct h2s, by_id);
2333 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2334 h2s->cs->data_cb->wake(h2s->cs);
2335 node = eb32_next(node);
2336 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002337 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002338
Willy Tarreau26bd7612017-10-09 16:47:04 +02002339 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002340 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2341 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2342 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002343 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002344
2345 if (eb_is_empty(&h2c->streams_by_id)) {
2346 /* no more stream, kill the connection now */
2347 h2_release(conn);
2348 return -1;
2349 }
2350 else {
2351 /* some streams still there, we need to signal them all and
2352 * wait for their departure.
2353 */
2354 __conn_xprt_stop_recv(conn);
2355 __conn_xprt_stop_send(conn);
2356 return 0;
2357 }
2358 }
2359
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002360 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002361 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002362
2363 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002364 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002365 __conn_xprt_stop_recv(conn);
2366 }
2367 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002368 __conn_xprt_want_recv(conn);
2369 }
2370
2371 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002372 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2373 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002374 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002375 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2376 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002377 __conn_xprt_want_send(conn);
2378 }
2379 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002380 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002381 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002382 }
2383
Willy Tarreau3f133572017-10-31 19:21:06 +01002384 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002385 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002386 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002387 task_queue(h2c->task);
2388 }
2389 else
2390 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002391 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002392
Willy Tarreau62f52692017-10-08 23:01:42 +02002393 return 0;
2394}
2395
Willy Tarreauea392822017-10-31 10:02:25 +01002396/* Connection timeout management. The principle is that if there's no receipt
2397 * nor sending for a certain amount of time, the connection is closed. If the
2398 * MUX buffer still has lying data or is not allocatable, the connection is
2399 * immediately killed. If it's allocatable and empty, we attempt to send a
2400 * GOAWAY frame.
2401 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002402static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002403{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002404 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002405 int expired = tick_is_expired(t->expire, now_ms);
2406
Willy Tarreau0975f112018-03-29 15:22:59 +02002407 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002408 return t;
2409
Willy Tarreau0975f112018-03-29 15:22:59 +02002410 task_delete(t);
2411 task_free(t);
2412
2413 if (!h2c) {
2414 /* resources were already deleted */
2415 return NULL;
2416 }
2417
2418 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002419 h2c_error(h2c, H2_ERR_NO_ERROR);
2420 h2_wake_some_streams(h2c, 0, 0);
2421
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002422 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002423 /* don't even try to send a GOAWAY, the buffer is stuck */
2424 h2c->flags |= H2_CF_GOAWAY_FAILED;
2425 }
2426
2427 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002428 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002429 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2430 h2c->flags |= H2_CF_GOAWAY_FAILED;
2431
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002432 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2433 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002434 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002435 b_del(&h2c->mbuf, ret);
2436 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002437 }
2438 }
Willy Tarreauea392822017-10-31 10:02:25 +01002439
Willy Tarreau0975f112018-03-29 15:22:59 +02002440 /* either we can release everything now or it will be done later once
2441 * the last stream closes.
2442 */
2443 if (eb_is_empty(&h2c->streams_by_id))
2444 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002445
Willy Tarreauea392822017-10-31 10:02:25 +01002446 return NULL;
2447}
2448
2449
Willy Tarreau62f52692017-10-08 23:01:42 +02002450/*******************************************/
2451/* functions below are used by the streams */
2452/*******************************************/
2453
2454/*
2455 * Attach a new stream to a connection
2456 * (Used for outgoing connections)
2457 */
2458static struct conn_stream *h2_attach(struct connection *conn)
2459{
2460 return NULL;
2461}
2462
2463/* callback used to update the mux's polling flags after changing a cs' status.
2464 * The caller (cs_update_mux_polling) will take care of propagating any changes
2465 * to the transport layer.
2466 */
2467static void h2_update_poll(struct conn_stream *cs)
2468{
Willy Tarreau1d393222017-10-17 10:26:19 +02002469 struct h2s *h2s = cs->ctx;
2470
2471 if (!h2s)
2472 return;
2473
Willy Tarreaud7739c82017-10-30 15:38:23 +01002474 /* we may unblock a blocked read */
2475
Willy Tarreau315d8072017-12-10 22:17:57 +01002476 if (cs->flags & CS_FL_DATA_RD_ENA) {
2477 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002478 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002479 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002480 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002481 conn_xprt_want_send(cs->conn);
2482 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002483 }
2484
Willy Tarreau1d393222017-10-17 10:26:19 +02002485 /* Note: the stream and stream-int code doesn't allow us to perform a
2486 * synchronous send() here unfortunately, because this code is called
2487 * as si_update() from the process_stream() context. This means that
2488 * we have to queue the current cs and defer its processing after the
2489 * connection's cs list is processed anyway.
2490 */
2491
2492 if (cs->flags & CS_FL_DATA_WR_ENA) {
2493 if (LIST_ISEMPTY(&h2s->list)) {
2494 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002495 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002496 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2497 conn_xprt_want_send(cs->conn);
2498 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2499 }
2500 }
2501 else if (!LIST_ISEMPTY(&h2s->list)) {
2502 LIST_DEL(&h2s->list);
2503 LIST_INIT(&h2s->list);
2504 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2505 }
2506
2507 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002508 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002509 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002510}
2511
2512/*
2513 * Detach the stream from the connection and possibly release the connection.
2514 */
2515static void h2_detach(struct conn_stream *cs)
2516{
Willy Tarreau60935142017-10-16 18:11:19 +02002517 struct h2s *h2s = cs->ctx;
2518 struct h2c *h2c;
2519
2520 cs->ctx = NULL;
2521 if (!h2s)
2522 return;
2523
2524 h2c = h2s->h2c;
2525 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002526 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002527 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2528 !h2_has_too_many_cs(h2c)) {
2529 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2530 if (h2_recv_allowed(h2c)) {
2531 __conn_xprt_want_recv(h2c->conn);
2532 conn_xprt_want_send(h2c->conn);
2533 }
2534 }
Willy Tarreau60935142017-10-16 18:11:19 +02002535
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002536 /* this stream may be blocked waiting for some data to leave (possibly
2537 * an ES or RST frame), so orphan it in this case.
2538 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002539 if (!(cs->conn->flags & CO_FL_ERROR) &&
2540 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002541 return;
2542
Willy Tarreau45f752e2017-10-30 15:44:59 +01002543 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2544 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2545 /* unblock the connection if it was blocked on this
2546 * stream.
2547 */
2548 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2549 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2550 conn_xprt_want_recv(cs->conn);
2551 conn_xprt_want_send(cs->conn);
2552 }
2553
Willy Tarreau71049cc2018-03-28 13:56:39 +02002554 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002555
Willy Tarreaue323f342018-03-28 13:51:45 +02002556 /* We don't want to close right now unless we're removing the
2557 * last stream, and either the connection is in error, or it
2558 * reached the ID already specified in a GOAWAY frame received
2559 * or sent (as seen by last_sid >= 0).
2560 */
2561 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2562 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002563 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002564 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002565 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002566 (conn_xprt_read0_pending(h2c->conn) ||
2567 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2568 /* no more stream will come, kill it now */
2569 h2_release(h2c->conn);
2570 }
2571 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002572 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002573 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2574 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002575 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002576 else
2577 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002578 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002579}
2580
2581static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2582{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002583 struct h2s *h2s = cs->ctx;
2584
2585 if (!mode)
2586 return;
2587
Willy Tarreau721c9742017-11-07 11:05:42 +01002588 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002589 return;
2590
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002591 /* if no outgoing data was seen on this stream, it means it was
2592 * closed with a "tcp-request content" rule that is normally
2593 * used to kill the connection ASAP (eg: limit abuse). In this
2594 * case we send a goaway to close the connection.
2595 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002596 if (!(h2s->flags & H2_SF_RST_SENT) &&
2597 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002598 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002599
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002600 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2601 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2602 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002603 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002604
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002605 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002606 conn_xprt_want_send(cs->conn);
2607
Willy Tarreau00dd0782018-03-01 16:31:34 +01002608 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002609
2610 add_to_list:
2611 if (LIST_ISEMPTY(&h2s->list)) {
2612 if (h2s->flags & H2_SF_BLK_MFCTL)
2613 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2614 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2615 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2616 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002617}
2618
2619static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2620{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002621 struct h2s *h2s = cs->ctx;
2622
Willy Tarreau721c9742017-11-07 11:05:42 +01002623 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002624 return;
2625
Willy Tarreau67434202017-11-06 20:20:51 +01002626 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002627 /* we can cleanly close using an empty data frame only after headers */
2628
2629 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2630 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002631 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002632
2633 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002634 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002635 else
2636 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002637 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002638 /* if no outgoing data was seen on this stream, it means it was
2639 * closed with a "tcp-request content" rule that is normally
2640 * used to kill the connection ASAP (eg: limit abuse). In this
2641 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002642 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002643 if (!(h2s->flags & H2_SF_RST_SENT) &&
2644 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002645 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002646
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002647 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2648 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002649 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002650 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002651
Willy Tarreau00dd0782018-03-01 16:31:34 +01002652 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002653 }
2654
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002656 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002657
2658 add_to_list:
2659 if (LIST_ISEMPTY(&h2s->list)) {
2660 if (h2s->flags & H2_SF_BLK_MFCTL)
2661 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2662 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2663 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2664 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002665}
2666
Willy Tarreau13278b42017-10-13 19:23:14 +02002667/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2668 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2669 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002670 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002671 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002672static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002673{
2674 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002675 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002676 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002677 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002678 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002679 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002680 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002681 int flen = h2c->dfl;
2682 int outlen = 0;
2683 int wrap;
2684 int try;
2685
2686 if (!h2c->dfl) {
2687 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002688 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002689 return 0;
2690 }
2691
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002692 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002693 return 0; // incomplete input frame
2694
Willy Tarreau13278b42017-10-13 19:23:14 +02002695 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002696 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002697 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002698 copy = alloc_trash_chunk();
2699 if (!copy) {
2700 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2701 goto fail;
2702 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002703 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2704 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2705 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002706 }
2707
2708 /* The padlen is the first byte before data, and the padding appears
2709 * after data. padlen+data+padding are included in flen.
2710 */
2711 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002712 h2c->dpl = *hdrs;
2713 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002714 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2715 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002716 return 0;
2717 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002718 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002719 hdrs += 1; // skip Pad Length
2720 }
2721
2722 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2723 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002724 if (read_n32(hdrs) == h2s->id) {
2725 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2726 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2727 return 0;//goto fail_stream;
2728 }
2729
Willy Tarreau13278b42017-10-13 19:23:14 +02002730 hdrs += 5; // stream dep = 4, weight = 1
2731 flen -= 5;
2732 }
2733
2734 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2735 * don't support this for now and can't even decompress so we have to
2736 * break the connection.
2737 */
2738 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2739 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002740 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002741 }
2742
Willy Tarreau937f7602018-02-26 15:22:17 +01002743 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2744 if (!csbuf) {
2745 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002746 goto fail;
2747 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002748
Willy Tarreau937f7602018-02-26 15:22:17 +01002749 /* we can't retry a failed decompression operation so we must be very
2750 * careful not to take any risks. In practice the output buffer is
2751 * always empty except maybe for trailers, in which case we simply have
2752 * to wait for the upper layer to finish consuming what is available.
2753 */
2754 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002755 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002756
Willy Tarreau937f7602018-02-26 15:22:17 +01002757 csbuf->head = 0;
2758 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002759
2760 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2761 sizeof(list)/sizeof(list[0]), tmp);
2762 if (outlen < 0) {
2763 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2764 goto fail;
2765 }
2766
2767 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002768 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002769 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002770
2771 if (outlen < 0) {
2772 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2773 goto fail;
2774 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002775
Willy Tarreau174b06a2018-04-25 18:13:58 +02002776 if (msgf & H2_MSGF_BODY) {
2777 /* a payload is present */
2778 if (msgf & H2_MSGF_BODY_CL)
2779 h2s->flags |= H2_SF_DATA_CLEN;
2780 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2781 h2s->flags |= H2_SF_DATA_CHNK;
2782 }
2783
Willy Tarreau13278b42017-10-13 19:23:14 +02002784 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002785 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002786 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002787 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002788
Willy Tarreau937f7602018-02-26 15:22:17 +01002789 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002790 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau937f7602018-02-26 15:22:17 +01002791
Willy Tarreau68dd9852017-07-03 14:44:26 +02002792 leave:
2793 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002794 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002795 fail:
2796 outlen = 0;
2797 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002798}
2799
Willy Tarreau454f9052017-10-26 19:40:35 +02002800/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2801 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2802 * in use, a new chunk is emitted for each frame. This is supposed to fit
2803 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2804 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2805 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2806 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002807 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2808 * checked to know if some data remain pending (an empty DATA frame can return
2809 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2810 * connection errors in h2c->errcode. The caller must already have checked the
2811 * frame header and ensured that the frame was complete or the buffer full. It
2812 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002813 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002814static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002815{
2816 struct h2c *h2c = h2s->h2c;
2817 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002818 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002819 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002820 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002821
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002822 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002823
2824 /* The padlen is the first byte before data, and the padding appears
2825 * after data. padlen+data+padding are included in flen.
2826 */
Willy Tarreau79127812017-12-03 21:06:59 +01002827 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002828 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002829 return 0;
2830
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002831 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002832 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002833 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2834 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002835 return 0;
2836 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002837
2838 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002839 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002840 h2c->dfl--;
2841 h2c->rcvd_c++; h2c->rcvd_s++;
2842 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002843 }
2844
Willy Tarreaud755ea62018-02-26 15:44:54 +01002845 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2846 if (!csbuf) {
2847 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002848 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002849 }
2850
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002851 flen = h2c->dfl - h2c->dpl;
2852 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002853 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002854
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002855 if (flen > b_data(&h2c->dbuf)) {
2856 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002857 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002858 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002859 }
2860
2861 if (unlikely(b_space_wraps(csbuf))) {
2862 /* it doesn't fit and the buffer is fragmented,
2863 * so let's defragment it and try again.
2864 */
2865 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002866 }
2867
Willy Tarreaueba10f22018-04-25 20:44:22 +02002868 /* chunked-encoding requires more room */
2869 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002870 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002871 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2872 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2873 (chklen < 1048576) ? 4 : 8;
2874 chklen += 4; // CRLF, CRLF
2875 }
2876
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002877 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002878 if (flen + chklen > b_room(csbuf)) {
2879 if (chklen >= b_room(csbuf)) {
2880 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002881 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002882 }
2883 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002884 }
2885
2886 if (h2s->flags & H2_SF_DATA_CHNK) {
2887 /* emit the chunk size */
2888 unsigned int chksz = flen;
2889 char str[10];
2890 char *beg;
2891
2892 beg = str + sizeof(str);
2893 *--beg = '\n';
2894 *--beg = '\r';
2895 do {
2896 *--beg = hextab[chksz & 0xF];
2897 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002898 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002899 }
2900
Willy Tarreau454f9052017-10-26 19:40:35 +02002901 /* Block1 is the length of the first block before the buffer wraps,
2902 * block2 is the optional second block to reach the end of the frame.
2903 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002904 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002905 if (block1 > flen)
2906 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002907 block2 = flen - block1;
2908
2909 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002910 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002911
2912 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002913 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002914
Willy Tarreaueba10f22018-04-25 20:44:22 +02002915 if (h2s->flags & H2_SF_DATA_CHNK) {
2916 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002917 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002918 }
2919
Willy Tarreau454f9052017-10-26 19:40:35 +02002920 /* now mark the input data as consumed (will be deleted from the buffer
2921 * by the caller when seeing FRAME_A after sending the window update).
2922 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002923 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002924 h2c->dfl -= flen;
2925 h2c->rcvd_c += flen;
2926 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2927
2928 if (h2c->dfl > h2c->dpl) {
2929 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002930 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002931 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002932 }
2933
Willy Tarreau4a28da12018-01-04 14:41:00 +01002934 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002935 /* here we're done with the frame, all the payload (except padding) was
2936 * transferred.
2937 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002938
2939 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2940 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002941 if (b_room(csbuf) < 5) {
2942 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002943 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002944 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002945 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002946 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002947 }
2948
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002949 h2c->rcvd_c += h2c->dpl;
2950 h2c->rcvd_s += h2c->dpl;
2951 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002952 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2953
Willy Tarreaud755ea62018-02-26 15:44:54 +01002954 if (h2c->dff & H2_F_DATA_END_STREAM)
Willy Tarreau454f9052017-10-26 19:40:35 +02002955 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002956
Willy Tarreau454b57b2018-02-26 15:50:05 +01002957 return flen + chklen;
2958 fail:
2959 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002960}
2961
Willy Tarreau62f52692017-10-08 23:01:42 +02002962/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002963 * Called from the upper layer to get more data, up to <count> bytes. The
2964 * caller is responsible for never asking for more data than what is available
2965 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002966 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002967static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02002968{
Willy Tarreau13278b42017-10-13 19:23:14 +02002969 struct h2s *h2s = cs->ctx;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002970 struct buffer *csbuf = &cs->rxbuf;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002971 size_t ret;
Willy Tarreau454f9052017-10-26 19:40:35 +02002972
Willy Tarreau454b57b2018-02-26 15:50:05 +01002973 /* transfer possibly pending data to the upper layer */
2974 ret = b_xfer(buf, csbuf, count);
2975
2976 if (b_data(csbuf))
2977 cs->flags |= CS_FL_RCV_MORE;
2978 else {
2979 cs->flags &= ~CS_FL_RCV_MORE;
2980 if (h2s->flags & H2_SF_ES_RCVD)
2981 cs->flags |= CS_FL_EOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02002982 }
Willy Tarreau454b57b2018-02-26 15:50:05 +01002983
2984 cs_drop_rxbuf(cs);
Willy Tarreau13278b42017-10-13 19:23:14 +02002985 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002986}
2987
Willy Tarreau5dd17352018-06-14 13:33:30 +02002988/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2989 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2990 * number of bytes sent. The caller must check the stream's status to detect
2991 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002992 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002993static 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 +02002994{
2995 struct http_hdr list[MAX_HTTP_HDR];
2996 struct h2c *h2c = h2s->h2c;
2997 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002998 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002999 int es_now = 0;
3000 int ret = 0;
3001 int hdr;
3002
3003 if (h2c_mux_busy(h2c, h2s)) {
3004 h2s->flags |= H2_SF_BLK_MBUSY;
3005 return 0;
3006 }
3007
Willy Tarreau44e973f2018-03-01 17:49:30 +01003008 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003009 h2c->flags |= H2_CF_MUX_MALLOC;
3010 h2s->flags |= H2_SF_BLK_MROOM;
3011 return 0;
3012 }
3013
3014 /* First, try to parse the H1 response and index it into <list>.
3015 * NOTE! Since it comes from haproxy, we *know* that a response header
3016 * block does not wrap and we can safely read it this way without
3017 * having to realign the buffer.
3018 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003019 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003020 list, sizeof(list)/sizeof(list[0]), h1m);
3021 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003022 /* incomplete or invalid response, this is abnormal coming from
3023 * haproxy and may only result in a bad errorfile or bad Lua code
3024 * so that won't be fixed, raise an error now.
3025 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003026 * FIXME: we should instead add the ability to only return a
3027 * 502 bad gateway. But in theory this is not supposed to
3028 * happen.
3029 */
3030 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3031 ret = 0;
3032 goto end;
3033 }
3034
3035 chunk_reset(&outbuf);
3036
3037 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003038 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003039 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003041
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003042 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003043 break;
3044 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003045 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003046 }
3047
3048 if (outbuf.size < 9) {
3049 h2c->flags |= H2_CF_MUX_MFULL;
3050 h2s->flags |= H2_SF_BLK_MROOM;
3051 ret = 0;
3052 goto end;
3053 }
3054
3055 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003056 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3057 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3058 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003059
3060 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 if (outbuf.data < outbuf.size && h1m->status == 200)
3062 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3063 else if (outbuf.data < outbuf.size && h1m->status == 304)
3064 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003065 else if (unlikely(list[0].v.len != 3)) {
3066 /* this is an unparsable response */
3067 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3068 ret = 0;
3069 goto end;
3070 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003071 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003072 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003073 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3074 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3075 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3076 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3077 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003078 }
3079 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003080 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003081 goto realign_again;
3082
3083 h2c->flags |= H2_CF_MUX_MFULL;
3084 h2s->flags |= H2_SF_BLK_MROOM;
3085 ret = 0;
3086 goto end;
3087 }
3088
3089 /* encode all headers, stop at empty name */
3090 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003091 /* these ones do not exist in H2 and must be dropped. */
3092 if (isteq(list[hdr].n, ist("connection")) ||
3093 isteq(list[hdr].n, ist("proxy-connection")) ||
3094 isteq(list[hdr].n, ist("keep-alive")) ||
3095 isteq(list[hdr].n, ist("upgrade")) ||
3096 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003097 continue;
3098
3099 if (isteq(list[hdr].n, ist("")))
3100 break; // end
3101
3102 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3103 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003104 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003105 goto realign_again;
3106
3107 h2c->flags |= H2_CF_MUX_MFULL;
3108 h2s->flags |= H2_SF_BLK_MROOM;
3109 ret = 0;
3110 goto end;
3111 }
3112 }
3113
3114 /* we may need to add END_STREAM */
3115 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3116 es_now = 1;
3117
3118 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003119 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003120
3121 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003122 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003123
3124 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003125 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003126
3127 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003128 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003129 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130
3131 /* for now we don't implemented CONTINUATION, so we wait for a
3132 * body or directly end in TRL2.
3133 */
3134 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003135 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003136 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003137
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138 h1m->state = HTTP_MSG_DONE;
3139 h2s->flags |= H2_SF_ES_SENT;
3140 if (h2s->st == H2_SS_OPEN)
3141 h2s->st = H2_SS_HLOC;
3142 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003143 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003144 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003145 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003146 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003147 h1m->state = HTTP_MSG_RPBEFORE;
3148 h1m->status = 0;
3149 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003150 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003151 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003152 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003153 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003154
3155 end:
3156 //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));
3157 return ret;
3158}
3159
Willy Tarreau5dd17352018-06-14 13:33:30 +02003160/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3161 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3162 * the number of bytes sent. The caller must check the stream's status to
3163 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003164 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003165static 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 +02003166{
3167 struct h2c *h2c = h2s->h2c;
3168 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003169 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003170 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003171 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003172 int es_now = 0;
3173 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003174 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003175 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003176
3177 if (h2c_mux_busy(h2c, h2s)) {
3178 h2s->flags |= H2_SF_BLK_MBUSY;
3179 goto end;
3180 }
3181
Willy Tarreau44e973f2018-03-01 17:49:30 +01003182 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003183 h2c->flags |= H2_CF_MUX_MALLOC;
3184 h2s->flags |= H2_SF_BLK_MROOM;
3185 goto end;
3186 }
3187
3188 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003189 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003190 goto end;
3191
3192 chunk_reset(&outbuf);
3193
3194 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003195 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003196 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003198
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003199 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003200 break;
3201 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003203 }
3204
3205 if (outbuf.size < 9) {
3206 h2c->flags |= H2_CF_MUX_MFULL;
3207 h2s->flags |= H2_SF_BLK_MROOM;
3208 goto end;
3209 }
3210
3211 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003212 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3213 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3214 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003215
3216 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3217 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003218 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003219 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003220 break;
3221 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003222 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003223 if ((long long)size > h1m->curr_len)
3224 size = h1m->curr_len;
3225 break;
3226 default: /* te:chunked : parse chunks */
3227 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003228 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003229 if (!ret)
3230 goto end;
3231
3232 if (ret < 0) {
3233 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3234 h1m->err_pos = ret;
3235 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3236 goto end;
3237 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003238 max -= ret;
3239 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003240 total += ret;
3241 h1m->state = HTTP_MSG_CHUNK_SIZE;
3242 }
3243
3244 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3245 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003246 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003247 if (!ret)
3248 goto end;
3249
3250 if (ret < 0) {
3251 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3252 h1m->err_pos = ret;
3253 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3254 goto end;
3255 }
3256
3257 size = chunk;
3258 h1m->curr_len = chunk;
3259 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003260 max -= ret;
3261 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003262 total += ret;
3263 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3264 if (!size)
3265 goto send_empty;
3266 }
3267
3268 /* in MSG_DATA state, continue below */
3269 size = h1m->curr_len;
3270 break;
3271 }
3272
3273 /* we have in <size> the exact number of bytes we need to copy from
3274 * the H1 buffer. We need to check this against the connection's and
3275 * the stream's send windows, and to ensure that this fits in the max
3276 * frame size and in the buffer's available space minus 9 bytes (for
3277 * the frame header). The connection's flow control is applied last so
3278 * that we can use a separate list of streams which are immediately
3279 * unblocked on window opening. Note: we don't implement padding.
3280 */
3281
Willy Tarreau5dd17352018-06-14 13:33:30 +02003282 if (size > max)
3283 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003284
3285 if (size > h2s->mws)
3286 size = h2s->mws;
3287
3288 if (size <= 0) {
3289 h2s->flags |= H2_SF_BLK_SFCTL;
3290 goto end;
3291 }
3292
3293 if (h2c->mfs && size > h2c->mfs)
3294 size = h2c->mfs;
3295
3296 if (size + 9 > outbuf.size) {
3297 /* we have an opportunity for enlarging the too small
3298 * available space, let's try.
3299 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003300 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003301 goto realign_again;
3302 size = outbuf.size - 9;
3303 }
3304
3305 if (size <= 0) {
3306 h2c->flags |= H2_CF_MUX_MFULL;
3307 h2s->flags |= H2_SF_BLK_MROOM;
3308 goto end;
3309 }
3310
3311 if (size > h2c->mws)
3312 size = h2c->mws;
3313
3314 if (size <= 0) {
3315 h2s->flags |= H2_SF_BLK_MFCTL;
3316 goto end;
3317 }
3318
3319 /* copy whatever we can */
3320 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003321 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003322 if (ret == 1)
3323 len2 = 0;
3324
3325 if (!ret || len1 + len2 < size) {
3326 /* FIXME: must normally never happen */
3327 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3328 goto end;
3329 }
3330
3331 /* limit len1/len2 to size */
3332 if (len1 + len2 > size) {
3333 int sub = len1 + len2 - size;
3334
3335 if (len2 > sub)
3336 len2 -= sub;
3337 else {
3338 sub -= len2;
3339 len2 = 0;
3340 len1 -= sub;
3341 }
3342 }
3343
3344 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003345 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003346 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003347 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003348
3349 send_empty:
3350 /* we may need to add END_STREAM */
3351 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3352 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003353 *
3354 * FIXME: what we do here is not correct because we send end_stream
3355 * before knowing if we'll have to send a HEADERS frame for the
3356 * trailers. More importantly we're not consuming the trailing CRLF
3357 * after the end of trailers, so it will be left to the caller to
3358 * eat it. The right way to do it would be to measure trailers here
3359 * and to send ES only if there are no trailers.
3360 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003361 */
3362 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3363 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3364 es_now = 1;
3365
3366 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003368
3369 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003370 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003371
3372 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003373 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003374
3375 /* consume incoming H1 response */
3376 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003377 max -= size;
3378 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003379 total += size;
3380 h1m->curr_len -= size;
3381 h2s->mws -= size;
3382 h2c->mws -= size;
3383
3384 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3385 h1m->state = HTTP_MSG_CHUNK_CRLF;
3386 goto new_frame;
3387 }
3388 }
3389
3390 if (es_now) {
3391 if (h2s->st == H2_SS_OPEN)
3392 h2s->st = H2_SS_HLOC;
3393 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003394 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003395
Willy Tarreau35a62702018-02-27 15:37:25 +01003396 if (!(h1m->flags & H1_MF_CHNK)) {
3397 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003398 total += max;
3399 ofs += max;
3400 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003401
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003402 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003403 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003404
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405 h2s->flags |= H2_SF_ES_SENT;
3406 }
3407
3408 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003409 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 +02003410 return total;
3411}
3412
Olivier Houchard6ff20392018-07-17 18:46:31 +02003413/* Called from the upper layer, to subscribe to events, such as being able to send */
3414static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3415{
3416 struct wait_list *sw;
3417 struct h2s *h2s = cs->ctx;
3418
3419 switch (event_type) {
3420 case SUB_CAN_SEND:
3421 sw = param;
3422 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3423 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3424 return 0;
3425 default:
3426 break;
3427 }
3428 return -1;
3429
3430
3431}
3432
Willy Tarreau62f52692017-10-08 23:01:42 +02003433/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003434static size_t h2_snd_buf(struct conn_stream *cs, const struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003435{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003436 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003437 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003438 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003439
Willy Tarreau0bad0432018-06-14 16:54:01 +02003440 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003441 h2s->flags |= H2_SF_OUTGOING_DATA;
3442
Willy Tarreau0bad0432018-06-14 16:54:01 +02003443 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003444 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003445 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003446 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003447 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003448 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003449 }
3450 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3451 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003452 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003453
Willy Tarreau5dd17352018-06-14 13:33:30 +02003454 if (unlikely((int)ret <= 0)) {
3455 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003456 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3457 break;
3458 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003459 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003460 total += count;
3461 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003462 h2s->res.state = HTTP_MSG_DONE;
3463 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003464 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003466 cs->flags |= CS_FL_ERROR;
3467 break;
3468 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003469
3470 total += ret;
3471 count -= ret;
3472
3473 if (h2s->st >= H2_SS_ERROR)
3474 break;
3475
3476 if (h2s->flags & H2_SF_BLK_ANY)
3477 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003478 }
3479
Willy Tarreau00610962018-07-19 10:58:28 +02003480 if (h2s->st >= H2_SS_ERROR) {
3481 /* trim any possibly pending data after we close (extra CR-LF,
3482 * unprocessed trailers, abnormal extra data, ...)
3483 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003484 total += count;
3485 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003486 }
3487
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003488 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003489 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003490 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003491 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003492 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003493 }
3494
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003495 if (h2s->flags & H2_SF_BLK_SFCTL) {
3496 /* stream flow control, quit the list */
3497 LIST_DEL(&h2s->list);
3498 LIST_INIT(&h2s->list);
3499 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003500 else if (LIST_ISEMPTY(&h2s->list)) {
3501 if (h2s->flags & H2_SF_BLK_MFCTL)
3502 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003503 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003505 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003506}
3507
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003508/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003509static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003510{
3511 struct h2c *h2c = conn->mux_ctx;
3512 struct h2s *h2s;
3513 struct eb32_node *node;
3514 int fctl_cnt = 0;
3515 int send_cnt = 0;
3516 int tree_cnt = 0;
3517 int orph_cnt = 0;
3518
3519 if (!h2c)
3520 return;
3521
3522 list_for_each_entry(h2s, &h2c->fctl_list, list)
3523 fctl_cnt++;
3524
3525 list_for_each_entry(h2s, &h2c->send_list, list)
3526 send_cnt++;
3527
3528 node = eb32_first(&h2c->streams_by_id);
3529 while (node) {
3530 h2s = container_of(node, struct h2s, by_id);
3531 tree_cnt++;
3532 if (!h2s->cs)
3533 orph_cnt++;
3534 node = eb32_next(node);
3535 }
3536
Willy Tarreauc65edac2018-07-19 10:54:43 +02003537 chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003538 h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003539}
Willy Tarreau62f52692017-10-08 23:01:42 +02003540
3541/*******************************************************/
3542/* functions below are dedicated to the config parsers */
3543/*******************************************************/
3544
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003545/* config parser for global "tune.h2.header-table-size" */
3546static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3547 struct proxy *defpx, const char *file, int line,
3548 char **err)
3549{
3550 if (too_many_args(1, args, err, NULL))
3551 return -1;
3552
3553 h2_settings_header_table_size = atoi(args[1]);
3554 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3555 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3556 return -1;
3557 }
3558 return 0;
3559}
Willy Tarreau62f52692017-10-08 23:01:42 +02003560
Willy Tarreaue6baec02017-07-27 11:45:11 +02003561/* config parser for global "tune.h2.initial-window-size" */
3562static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3563 struct proxy *defpx, const char *file, int line,
3564 char **err)
3565{
3566 if (too_many_args(1, args, err, NULL))
3567 return -1;
3568
3569 h2_settings_initial_window_size = atoi(args[1]);
3570 if (h2_settings_initial_window_size < 0) {
3571 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3572 return -1;
3573 }
3574 return 0;
3575}
3576
Willy Tarreau5242ef82017-07-27 11:47:28 +02003577/* config parser for global "tune.h2.max-concurrent-streams" */
3578static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3579 struct proxy *defpx, const char *file, int line,
3580 char **err)
3581{
3582 if (too_many_args(1, args, err, NULL))
3583 return -1;
3584
3585 h2_settings_max_concurrent_streams = atoi(args[1]);
3586 if (h2_settings_max_concurrent_streams < 0) {
3587 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3588 return -1;
3589 }
3590 return 0;
3591}
3592
Willy Tarreau62f52692017-10-08 23:01:42 +02003593
3594/****************************************/
3595/* MUX initialization and instanciation */
3596/***************************************/
3597
3598/* The mux operations */
3599const struct mux_ops h2_ops = {
3600 .init = h2_init,
3601 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003602 .wake = h2_wake,
3603 .update_poll = h2_update_poll,
3604 .rcv_buf = h2_rcv_buf,
3605 .snd_buf = h2_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003606 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003607 .attach = h2_attach,
3608 .detach = h2_detach,
3609 .shutr = h2_shutr,
3610 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003611 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003612 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003613 .name = "H2",
3614};
3615
3616/* ALPN selection : this mux registers ALPN tolen "h2" */
3617static struct alpn_mux_list alpn_mux_h2 =
3618 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3619
3620/* config keyword parsers */
3621static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003622 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003623 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003624 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003625 { 0, NULL, NULL }
3626}};
3627
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003628static void __h2_deinit(void)
3629{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003630 pool_destroy(pool_head_h2s);
3631 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003632}
3633
Willy Tarreau62f52692017-10-08 23:01:42 +02003634__attribute__((constructor))
3635static void __h2_init(void)
3636{
3637 alpn_register_mux(&alpn_mux_h2);
3638 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003639 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003640 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3641 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003642}