blob: 7fc31312a209d76c67504ff3c9a0636fd7cce62d [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 Tarreau2a761dc2018-02-26 18:50:57 +01001568/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1569 * It may return an error in h2c or h2s. The caller must consider that the
1570 * return value is the new h2s in case one was allocated (most common case).
1571 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001572 * errors here are reported as connection errors since it's impossible to
1573 * recover from such errors after the compression context has been altered.
1574 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001575static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001576{
1577 int error;
1578
1579 if (!h2c->dfl) {
1580 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1581 goto strm_err;
1582 }
1583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001584 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001585 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001586
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001587 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001588 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001589
Willy Tarreauf2101912018-07-19 10:11:38 +02001590 if (h2c->flags & H2_CF_DEM_TOOMANY)
1591 return 0; // too many cs still present
1592
Willy Tarreau13278b42017-10-13 19:23:14 +02001593 /* now either the frame is complete or the buffer is complete */
1594 if (h2s->st != H2_SS_IDLE) {
1595 /* FIXME: stream already exists, this is only allowed for
1596 * trailers (not supported for now).
1597 */
1598 error = H2_ERR_PROTOCOL_ERROR;
1599 goto conn_err;
1600 }
1601 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1602 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1603 error = H2_ERR_PROTOCOL_ERROR;
1604 goto conn_err;
1605 }
1606
1607 h2s = h2c_stream_new(h2c, h2c->dsi);
1608 if (!h2s) {
1609 error = H2_ERR_INTERNAL_ERROR;
1610 goto conn_err;
1611 }
1612
1613 h2s->st = H2_SS_OPEN;
1614 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1615 h2s->st = H2_SS_HREM;
1616 h2s->flags |= H2_SF_ES_RCVD;
1617 }
1618
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001619 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001620 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001621
Willy Tarreau8f650c32017-11-21 19:36:21 +01001622 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001623 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001624
Willy Tarreau721c9742017-11-07 11:05:42 +01001625 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001626 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001627 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001628 }
1629 else {
1630 /* update the max stream ID if the request is being processed */
1631 if (h2s->id > h2c->max_id)
1632 h2c->max_id = h2s->id;
1633 }
1634
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001635 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001636
1637 conn_err:
1638 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001639 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001640
1641 strm_err:
1642 if (h2s) {
1643 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001644 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001645 }
1646 else
1647 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001648 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001649}
1650
Willy Tarreau454f9052017-10-26 19:40:35 +02001651/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1652 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1653 */
1654static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1655{
1656 int error;
1657
1658 /* note that empty DATA frames are perfectly valid and sometimes used
1659 * to signal an end of stream (with the ES flag).
1660 */
1661
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001662 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001663 return 0; // empty buffer
1664
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001665 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001666 return 0; // incomplete frame
1667
1668 /* now either the frame is complete or the buffer is complete */
1669
1670 if (!h2c->dsi) {
1671 /* RFC7540#6.1 */
1672 error = H2_ERR_PROTOCOL_ERROR;
1673 goto conn_err;
1674 }
1675
1676 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1677 /* RFC7540#6.1 */
1678 error = H2_ERR_STREAM_CLOSED;
1679 goto strm_err;
1680 }
1681
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001682 if (!h2_frt_transfer_data(h2s))
1683 return 0;
1684
Willy Tarreau454f9052017-10-26 19:40:35 +02001685 /* call the upper layers to process the frame, then let the upper layer
1686 * notify the stream about any change.
1687 */
1688 if (!h2s->cs) {
1689 error = H2_ERR_STREAM_CLOSED;
1690 goto strm_err;
1691 }
1692
Willy Tarreau8f650c32017-11-21 19:36:21 +01001693 if (h2c->st0 >= H2_CS_ERROR)
1694 return 0;
1695
Willy Tarreau721c9742017-11-07 11:05:42 +01001696 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001697 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001698 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001699 }
1700
1701 /* check for completion : the callee will change this to FRAME_A or
1702 * FRAME_H once done.
1703 */
1704 if (h2c->st0 == H2_CS_FRAME_P)
1705 return 0;
1706
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001707
1708 /* last frame */
1709 if (h2c->dff & H2_F_DATA_END_STREAM) {
1710 h2s->st = H2_SS_HREM;
1711 h2s->flags |= H2_SF_ES_RCVD;
1712 }
1713
Willy Tarreau454f9052017-10-26 19:40:35 +02001714 return 1;
1715
1716 conn_err:
1717 h2c_error(h2c, error);
1718 return 0;
1719
1720 strm_err:
1721 if (h2s) {
1722 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001723 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001724 }
1725 else
1726 h2c_error(h2c, error);
1727 return 0;
1728}
1729
Willy Tarreaubc933932017-10-09 16:21:43 +02001730/* process Rx frames to be demultiplexed */
1731static void h2_process_demux(struct h2c *h2c)
1732{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001733 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001734
Willy Tarreau081d4722017-05-16 21:51:05 +02001735 if (h2c->st0 >= H2_CS_ERROR)
1736 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001737
1738 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1739 if (h2c->st0 == H2_CS_PREFACE) {
1740 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1741 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1742 if (h2c->st0 == H2_CS_ERROR)
1743 h2c->st0 = H2_CS_ERROR2;
1744 goto fail;
1745 }
1746
1747 h2c->max_id = 0;
1748 h2c->st0 = H2_CS_SETTINGS1;
1749 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001750
1751 if (h2c->st0 == H2_CS_SETTINGS1) {
1752 struct h2_fh hdr;
1753
1754 /* ensure that what is pending is a valid SETTINGS frame
1755 * without an ACK.
1756 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001757 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001758 /* 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 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1765 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1766 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1767 h2c->st0 = H2_CS_ERROR2;
1768 goto fail;
1769 }
1770
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001771 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001772 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1773 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1774 h2c->st0 = H2_CS_ERROR2;
1775 goto fail;
1776 }
1777
1778 /* that's OK, switch to FRAME_P to process it */
1779 h2c->dfl = hdr.len;
1780 h2c->dsi = hdr.sid;
1781 h2c->dft = hdr.ft;
1782 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001783 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001784 h2c->st0 = H2_CS_FRAME_P;
1785 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001786 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001787
1788 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001789 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001790 int ret = 0;
1791
1792 if (h2c->st0 >= H2_CS_ERROR)
1793 break;
1794
1795 if (h2c->st0 == H2_CS_FRAME_H) {
1796 struct h2_fh hdr;
1797
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001798 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001799 break;
1800
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001801 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001802 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1803 h2c->st0 = H2_CS_ERROR;
1804 break;
1805 }
1806
1807 h2c->dfl = hdr.len;
1808 h2c->dsi = hdr.sid;
1809 h2c->dft = hdr.ft;
1810 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001811 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001812 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001813 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001814 }
1815
1816 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001817 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1818
1819 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->cs->rxbuf)) {
1820 /* we may have to signal the upper layers */
1821 h2s->cs->flags |= CS_FL_RCV_MORE;
1822 h2s->cs->data_cb->recv(h2s->cs);
1823 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1824 /* cs has just been destroyed, we have to kill h2s. */
1825 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1826 goto strm_err;
1827 }
1828
1829 if (h2c->st0 >= H2_CS_ERROR)
1830 goto strm_err;
1831
1832 if (h2s->st >= H2_SS_ERROR) {
1833 /* stream error : send RST_STREAM */
1834 h2c->st0 = H2_CS_FRAME_E;
1835 }
1836 }
1837 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001838
Willy Tarreaud7901432017-12-29 11:34:40 +01001839 if (h2c->st0 == H2_CS_FRAME_E)
1840 goto strm_err;
1841
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001842 if (h2s->st == H2_SS_IDLE &&
1843 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1844 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1845 * this state MUST be treated as a connection error
1846 */
1847 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1848 h2c->st0 = H2_CS_ERROR;
1849 break;
1850 }
1851
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001852 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1853 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1854 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1855 * this state MUST be treated as a stream error
1856 */
1857 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1858 goto strm_err;
1859 }
1860
Willy Tarreauab837502017-12-27 15:07:30 +01001861 /* Below the management of frames received in closed state is a
1862 * bit hackish because the spec makes strong differences between
1863 * streams closed by receiving RST, sending RST, and seeing ES
1864 * in both directions. In addition to this, the creation of a
1865 * new stream reusing the identifier of a closed one will be
1866 * detected here. Given that we cannot keep track of all closed
1867 * streams forever, we consider that unknown closed streams were
1868 * closed on RST received, which allows us to respond with an
1869 * RST without breaking the connection (eg: to abort a transfer).
1870 * Some frames have to be silently ignored as well.
1871 */
1872 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1873 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1874 /* #5.1.1: The identifier of a newly
1875 * established stream MUST be numerically
1876 * greater than all streams that the initiating
1877 * endpoint has opened or reserved. This
1878 * governs streams that are opened using a
1879 * HEADERS frame and streams that are reserved
1880 * using PUSH_PROMISE. An endpoint that
1881 * receives an unexpected stream identifier
1882 * MUST respond with a connection error.
1883 */
1884 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1885 goto strm_err;
1886 }
1887
1888 if (h2s->flags & H2_SF_RST_RCVD) {
1889 /* RFC7540#5.1:closed: an endpoint that
1890 * receives any frame other than PRIORITY after
1891 * receiving a RST_STREAM MUST treat that as a
1892 * stream error of type STREAM_CLOSED.
1893 *
1894 * Note that old streams fall into this category
1895 * and will lead to an RST being sent.
1896 */
1897 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1898 h2c->st0 = H2_CS_FRAME_E;
1899 goto strm_err;
1900 }
1901
1902 /* RFC7540#5.1:closed: if this state is reached as a
1903 * result of sending a RST_STREAM frame, the peer that
1904 * receives the RST_STREAM might have already sent
1905 * frames on the stream that cannot be withdrawn. An
1906 * endpoint MUST ignore frames that it receives on
1907 * closed streams after it has sent a RST_STREAM
1908 * frame. An endpoint MAY choose to limit the period
1909 * over which it ignores frames and treat frames that
1910 * arrive after this time as being in error.
1911 */
1912 if (!(h2s->flags & H2_SF_RST_SENT)) {
1913 /* RFC7540#5.1:closed: any frame other than
1914 * PRIO/WU/RST in this state MUST be treated as
1915 * a connection error
1916 */
1917 if (h2c->dft != H2_FT_RST_STREAM &&
1918 h2c->dft != H2_FT_PRIORITY &&
1919 h2c->dft != H2_FT_WINDOW_UPDATE) {
1920 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1921 goto strm_err;
1922 }
1923 }
1924 }
1925
Willy Tarreauc0da1962017-10-30 18:38:00 +01001926#if 0
1927 // problem below: it is not possible to completely ignore such
1928 // streams as we need to maintain the compression state as well
1929 // and for this we need to completely process these frames (eg:
1930 // HEADERS frames) as well as counting DATA frames to emit
1931 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1932 // This is a typical case of layer violation where the
1933 // transported contents are critical to the connection's
1934 // validity and must be ignored at the same time :-(
1935
1936 /* graceful shutdown, ignore streams whose ID is higher than
1937 * the one advertised in GOAWAY. RFC7540#6.8.
1938 */
1939 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001940 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1941 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001942 h2c->dfl -= ret;
1943 ret = h2c->dfl == 0;
1944 goto strm_err;
1945 }
1946#endif
1947
Willy Tarreau7e98c052017-10-10 15:56:59 +02001948 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001949 case H2_FT_SETTINGS:
1950 if (h2c->st0 == H2_CS_FRAME_P)
1951 ret = h2c_handle_settings(h2c);
1952
1953 if (h2c->st0 == H2_CS_FRAME_A)
1954 ret = h2c_ack_settings(h2c);
1955 break;
1956
Willy Tarreaucf68c782017-10-10 17:11:41 +02001957 case H2_FT_PING:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_handle_ping(h2c);
1960
1961 if (h2c->st0 == H2_CS_FRAME_A)
1962 ret = h2c_ack_ping(h2c);
1963 break;
1964
Willy Tarreau26f95952017-07-27 17:18:30 +02001965 case H2_FT_WINDOW_UPDATE:
1966 if (h2c->st0 == H2_CS_FRAME_P)
1967 ret = h2c_handle_window_update(h2c, h2s);
1968 break;
1969
Willy Tarreau61290ec2017-10-17 08:19:21 +02001970 case H2_FT_CONTINUATION:
1971 /* we currently don't support CONTINUATION frames since
1972 * we have nowhere to store the partial HEADERS frame.
1973 * Let's abort the stream on an INTERNAL_ERROR here.
1974 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001975 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001976 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001977 h2c->st0 = H2_CS_FRAME_E;
1978 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001979 break;
1980
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001982 if (h2c->st0 == H2_CS_FRAME_P) {
1983 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
1984 if (tmp_h2s) {
1985 h2s = tmp_h2s;
1986 ret = 1;
1987 }
1988 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 break;
1990
Willy Tarreau454f9052017-10-26 19:40:35 +02001991 case H2_FT_DATA:
1992 if (h2c->st0 == H2_CS_FRAME_P)
1993 ret = h2c_frt_handle_data(h2c, h2s);
1994
1995 if (h2c->st0 == H2_CS_FRAME_A)
1996 ret = h2c_send_strm_wu(h2c);
1997 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001998
Willy Tarreau92153fc2017-12-03 19:46:19 +01001999 case H2_FT_PRIORITY:
2000 if (h2c->st0 == H2_CS_FRAME_P)
2001 ret = h2c_handle_priority(h2c);
2002 break;
2003
Willy Tarreaucd234e92017-08-18 10:59:39 +02002004 case H2_FT_RST_STREAM:
2005 if (h2c->st0 == H2_CS_FRAME_P)
2006 ret = h2c_handle_rst_stream(h2c, h2s);
2007 break;
2008
Willy Tarreaue96b0922017-10-30 00:28:29 +01002009 case H2_FT_GOAWAY:
2010 if (h2c->st0 == H2_CS_FRAME_P)
2011 ret = h2c_handle_goaway(h2c);
2012 break;
2013
Willy Tarreau1c661982017-10-30 13:52:01 +01002014 case H2_FT_PUSH_PROMISE:
2015 /* not permitted here, RFC7540#5.1 */
2016 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002017 break;
2018
2019 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002020 default:
2021 /* drop frames that we ignore. They may be larger than
2022 * the buffer so we drain all of their contents until
2023 * we reach the end.
2024 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002025 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2026 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002027 h2c->dfl -= ret;
2028 ret = h2c->dfl == 0;
2029 }
2030
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002031 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002032 /* We may have to send an RST if not done yet */
2033 if (h2s->st == H2_SS_ERROR)
2034 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002035
Willy Tarreaua20a5192017-12-27 11:02:06 +01002036 if (h2c->st0 == H2_CS_FRAME_E)
2037 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002038
Willy Tarreau7e98c052017-10-10 15:56:59 +02002039 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002040 if (ret <= 0) {
2041 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002042 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002043 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002044
2045 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002046 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002047 h2c->st0 = H2_CS_FRAME_H;
2048 }
2049 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002050
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002051 if (h2c->rcvd_c > 0 &&
2052 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2053 h2c_send_conn_wu(h2c);
2054
Willy Tarreau52eed752017-09-22 15:05:09 +02002055 fail:
2056 /* we can go here on missing data, blocked response or error */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002057 if (h2s && h2s->cs && b_data(&h2s->cs->rxbuf)) {
2058 /* we may have to signal the upper layers */
2059 h2s->cs->flags |= CS_FL_RCV_MORE;
2060 h2s->cs->data_cb->recv(h2s->cs);
2061 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
2062 /* cs has just been destroyed, we have to kill h2s. */
2063 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2064 h2c_send_rst_stream(h2c, h2s);
2065 }
2066 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002067 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002068}
2069
2070/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2071 * the end.
2072 */
2073static int h2_process_mux(struct h2c *h2c)
2074{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002075 struct h2s *h2s, *h2s_back;
2076
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002077 /* start by sending possibly pending window updates */
2078 if (h2c->rcvd_c > 0 &&
2079 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2080 h2c_send_conn_wu(h2c) < 0)
2081 goto fail;
2082
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002083 /* First we always process the flow control list because the streams
2084 * waiting there were already elected for immediate emission but were
2085 * blocked just on this.
2086 */
2087
2088 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2089 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2090 h2c->st0 >= H2_CS_ERROR)
2091 break;
2092
2093 /* In theory it's possible that h2s->cs == NULL here :
2094 * - client sends crap that causes a parse error
2095 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2096 * - RST_STREAM cannot be emitted because mux is busy/full
2097 * - stream gets notified, detaches and quits
2098 * - mux buffer gets ready and wakes pending streams up
2099 * - bam!
2100 */
2101 h2s->flags &= ~H2_SF_BLK_ANY;
2102
2103 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002104 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002105 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002106 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002107 }
2108
2109 /* depending on callee's blocking reasons, we may queue in send
2110 * list or completely dequeue.
2111 */
2112 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2113 if (h2s->flags & H2_SF_BLK_ANY) {
2114 LIST_DEL(&h2s->list);
2115 LIST_ADDQ(&h2c->send_list, &h2s->list);
2116 }
2117 else {
2118 LIST_DEL(&h2s->list);
2119 LIST_INIT(&h2s->list);
2120 if (h2s->cs)
2121 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002122 else {
2123 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002124 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002125 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002126 }
2127 }
2128 }
2129
2130 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2131 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2132 break;
2133
2134 /* In theory it's possible that h2s->cs == NULL here :
2135 * - client sends crap that causes a parse error
2136 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2137 * - RST_STREAM cannot be emitted because mux is busy/full
2138 * - stream gets notified, detaches and quits
2139 * - mux buffer gets ready and wakes pending streams up
2140 * - bam!
2141 */
2142 h2s->flags &= ~H2_SF_BLK_ANY;
2143
2144 if (h2s->cs) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002145 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002146 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002147 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002148 }
2149 /* depending on callee's blocking reasons, we may queue in fctl
2150 * list or completely dequeue.
2151 */
2152 if (h2s->flags & H2_SF_BLK_MFCTL) {
2153 /* stream hit the connection's flow control */
2154 LIST_DEL(&h2s->list);
2155 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2156 }
2157 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2158 LIST_DEL(&h2s->list);
2159 LIST_INIT(&h2s->list);
2160 if (h2s->cs)
2161 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002162 else {
2163 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002164 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002165 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002166 }
2167 }
2168
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002169 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002170 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002171 if (h2c->st0 == H2_CS_ERROR) {
2172 if (h2c->max_id >= 0) {
2173 h2c_send_goaway_error(h2c, NULL);
2174 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2175 return 0;
2176 }
2177
2178 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2179 }
2180 return 1;
2181 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002182 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002183}
2184
Willy Tarreau71681172017-10-23 14:39:06 +02002185
Willy Tarreau62f52692017-10-08 23:01:42 +02002186/*********************************************************/
2187/* functions below are I/O callbacks from the connection */
2188/*********************************************************/
2189
2190/* callback called on recv event by the connection handler */
2191static void h2_recv(struct connection *conn)
2192{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002193 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002194 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002195 int max;
2196
Willy Tarreau315d8072017-12-10 22:17:57 +01002197 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002198 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002199
Willy Tarreau44e973f2018-03-01 17:49:30 +01002200 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002201 if (!buf) {
2202 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002203 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002204 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002205
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002206 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002207 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002208 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002209
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002210 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002211 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002212 return;
2213 }
2214
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002215 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002216 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002217 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002218}
2219
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002220/* Try to send data if possible */
2221static struct task *h2_send(struct task *t, void *ctx, unsigned short state)
Willy Tarreau62f52692017-10-08 23:01:42 +02002222{
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002223 struct connection *conn = ctx;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002224 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002225 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002226
2227 if (conn->flags & CO_FL_ERROR)
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002228 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002229
2230 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2231 /* a handshake was requested */
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002232 return NULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233 }
2234
Willy Tarreaubc933932017-10-09 16:21:43 +02002235 /* This loop is quite simple : it tries to fill as much as it can from
2236 * pending streams into the existing buffer until it's reportedly full
2237 * or the end of send requests is reached. Then it tries to send this
2238 * buffer's contents out, marks it not full if at least one byte could
2239 * be sent, and tries again.
2240 *
2241 * The snd_buf() function normally takes a "flags" argument which may
2242 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2243 * data immediately comes and CO_SFL_STREAMER to indicate that the
2244 * connection is streaming lots of data (used to increase TLS record
2245 * size at the expense of latency). The former can be sent any time
2246 * there's a buffer full flag, as it indicates at least one stream
2247 * attempted to send and failed so there are pending data. An
2248 * alternative would be to set it as long as there's an active stream
2249 * but that would be problematic for ACKs until we have an absolute
2250 * guarantee that all waiters have at least one byte to send. The
2251 * latter should possibly not be set for now.
2252 */
2253
2254 done = 0;
2255 while (!done) {
2256 unsigned int flags = 0;
2257
2258 /* fill as much as we can into the current buffer */
2259 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2260 done = h2_process_mux(h2c);
2261
2262 if (conn->flags & CO_FL_ERROR)
2263 break;
2264
2265 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2266 flags |= CO_SFL_MSG_MORE;
2267
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002268 if (b_data(&h2c->mbuf)) {
2269 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002270 if (!ret)
2271 break;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 b_del(&h2c->mbuf, ret);
2273 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002274 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002275
2276 /* wrote at least one byte, the buffer is not full anymore */
2277 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2278 }
2279
Willy Tarreaua2af5122017-10-09 11:56:46 +02002280 if (conn->flags & CO_FL_SOCK_WR_SH) {
2281 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002282 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002283 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002284 /* We're not full anymore, so we can wake any task that are waiting
2285 * for us.
2286 */
2287 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
2288 while (!LIST_ISEMPTY(&h2c->send_wait_list)) {
2289 struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n,
2290 struct wait_list *, list);
2291 LIST_DEL(&sw->list);
2292 LIST_INIT(&sw->list);
2293 tasklet_wakeup(sw->task);
2294 }
2295
2296 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002297 /* We're done, no more to send */
2298 if (!b_data(&h2c->mbuf))
2299 return NULL;
2300schedule:
2301 if (LIST_ISEMPTY(&h2c->wait_list.list))
2302 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2303 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002304}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002305
Willy Tarreau62f52692017-10-08 23:01:42 +02002306/* callback called on any event by the connection handler.
2307 * It applies changes and returns zero, or < 0 if it wants immediate
2308 * destruction of the connection (which normally doesn not happen in h2).
2309 */
2310static int h2_wake(struct connection *conn)
2311{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002312 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002313 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002314
Olivier Houchardf495fc42018-07-20 18:15:23 +02002315 h2_send(NULL, conn, 0);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002316 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002317 h2_process_demux(h2c);
2318
2319 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002321
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002322 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002323 h2c->flags &= ~H2_CF_DEM_DFULL;
2324 }
2325
Willy Tarreau8ec14062017-12-30 18:08:13 +01002326 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2327 /* frontend is stopping, reload likely in progress, let's try
2328 * to announce a graceful shutdown if not yet done. We don't
2329 * care if it fails, it will be tried again later.
2330 */
2331 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2332 if (h2c->last_sid < 0)
2333 h2c->last_sid = (1U << 31) - 1;
2334 h2c_send_goaway_error(h2c, NULL);
2335 }
2336 }
2337
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002338 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002339 * If we received early data, and the handshake is done, wake
2340 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002341 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002342 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2343 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2344 struct eb32_node *node;
2345 struct h2s *h2s;
2346
2347 h2c->flags |= H2_CF_WAIT_FOR_HS;
2348 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2349
2350 while (node) {
2351 h2s = container_of(node, struct h2s, by_id);
2352 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2353 h2s->cs->data_cb->wake(h2s->cs);
2354 node = eb32_next(node);
2355 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002356 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002357
Willy Tarreau26bd7612017-10-09 16:47:04 +02002358 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002359 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2360 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2361 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002362 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002363
2364 if (eb_is_empty(&h2c->streams_by_id)) {
2365 /* no more stream, kill the connection now */
2366 h2_release(conn);
2367 return -1;
2368 }
2369 else {
2370 /* some streams still there, we need to signal them all and
2371 * wait for their departure.
2372 */
2373 __conn_xprt_stop_recv(conn);
2374 __conn_xprt_stop_send(conn);
2375 return 0;
2376 }
2377 }
2378
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002379 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002380 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002381
2382 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002383 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002384 __conn_xprt_stop_recv(conn);
2385 }
2386 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002387 __conn_xprt_want_recv(conn);
2388 }
2389
2390 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002391 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2392 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002393 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002394 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2395 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002396 __conn_xprt_want_send(conn);
2397 }
2398 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002399 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002400 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002401 }
2402
Willy Tarreau3f133572017-10-31 19:21:06 +01002403 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002404 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002405 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002406 task_queue(h2c->task);
2407 }
2408 else
2409 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002410 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002411
Willy Tarreau62f52692017-10-08 23:01:42 +02002412 return 0;
2413}
2414
Willy Tarreauea392822017-10-31 10:02:25 +01002415/* Connection timeout management. The principle is that if there's no receipt
2416 * nor sending for a certain amount of time, the connection is closed. If the
2417 * MUX buffer still has lying data or is not allocatable, the connection is
2418 * immediately killed. If it's allocatable and empty, we attempt to send a
2419 * GOAWAY frame.
2420 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002421static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002422{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002423 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002424 int expired = tick_is_expired(t->expire, now_ms);
2425
Willy Tarreau0975f112018-03-29 15:22:59 +02002426 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002427 return t;
2428
Willy Tarreau0975f112018-03-29 15:22:59 +02002429 task_delete(t);
2430 task_free(t);
2431
2432 if (!h2c) {
2433 /* resources were already deleted */
2434 return NULL;
2435 }
2436
2437 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002438 h2c_error(h2c, H2_ERR_NO_ERROR);
2439 h2_wake_some_streams(h2c, 0, 0);
2440
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002441 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002442 /* don't even try to send a GOAWAY, the buffer is stuck */
2443 h2c->flags |= H2_CF_GOAWAY_FAILED;
2444 }
2445
2446 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002447 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002448 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2449 h2c->flags |= H2_CF_GOAWAY_FAILED;
2450
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002451 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2452 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002453 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 b_del(&h2c->mbuf, ret);
2455 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002456 }
2457 }
Willy Tarreauea392822017-10-31 10:02:25 +01002458
Willy Tarreau0975f112018-03-29 15:22:59 +02002459 /* either we can release everything now or it will be done later once
2460 * the last stream closes.
2461 */
2462 if (eb_is_empty(&h2c->streams_by_id))
2463 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002464
Willy Tarreauea392822017-10-31 10:02:25 +01002465 return NULL;
2466}
2467
2468
Willy Tarreau62f52692017-10-08 23:01:42 +02002469/*******************************************/
2470/* functions below are used by the streams */
2471/*******************************************/
2472
2473/*
2474 * Attach a new stream to a connection
2475 * (Used for outgoing connections)
2476 */
2477static struct conn_stream *h2_attach(struct connection *conn)
2478{
2479 return NULL;
2480}
2481
2482/* callback used to update the mux's polling flags after changing a cs' status.
2483 * The caller (cs_update_mux_polling) will take care of propagating any changes
2484 * to the transport layer.
2485 */
2486static void h2_update_poll(struct conn_stream *cs)
2487{
Willy Tarreau1d393222017-10-17 10:26:19 +02002488 struct h2s *h2s = cs->ctx;
2489
2490 if (!h2s)
2491 return;
2492
Willy Tarreaud7739c82017-10-30 15:38:23 +01002493 /* we may unblock a blocked read */
2494
Willy Tarreau315d8072017-12-10 22:17:57 +01002495 if (cs->flags & CS_FL_DATA_RD_ENA) {
2496 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002497 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002498 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002499 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002500 conn_xprt_want_send(cs->conn);
2501 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002502 }
2503
Willy Tarreau1d393222017-10-17 10:26:19 +02002504 /* Note: the stream and stream-int code doesn't allow us to perform a
2505 * synchronous send() here unfortunately, because this code is called
2506 * as si_update() from the process_stream() context. This means that
2507 * we have to queue the current cs and defer its processing after the
2508 * connection's cs list is processed anyway.
2509 */
2510
2511 if (cs->flags & CS_FL_DATA_WR_ENA) {
2512 if (LIST_ISEMPTY(&h2s->list)) {
2513 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002515 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2516 conn_xprt_want_send(cs->conn);
2517 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2518 }
2519 }
2520 else if (!LIST_ISEMPTY(&h2s->list)) {
2521 LIST_DEL(&h2s->list);
2522 LIST_INIT(&h2s->list);
2523 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2524 }
2525
2526 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002527 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002528 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002529}
2530
2531/*
2532 * Detach the stream from the connection and possibly release the connection.
2533 */
2534static void h2_detach(struct conn_stream *cs)
2535{
Willy Tarreau60935142017-10-16 18:11:19 +02002536 struct h2s *h2s = cs->ctx;
2537 struct h2c *h2c;
2538
2539 cs->ctx = NULL;
2540 if (!h2s)
2541 return;
2542
2543 h2c = h2s->h2c;
2544 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002545 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002546 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2547 !h2_has_too_many_cs(h2c)) {
2548 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2549 if (h2_recv_allowed(h2c)) {
2550 __conn_xprt_want_recv(h2c->conn);
2551 conn_xprt_want_send(h2c->conn);
2552 }
2553 }
Willy Tarreau60935142017-10-16 18:11:19 +02002554
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002555 /* this stream may be blocked waiting for some data to leave (possibly
2556 * an ES or RST frame), so orphan it in this case.
2557 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002558 if (!(cs->conn->flags & CO_FL_ERROR) &&
2559 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002560 return;
2561
Willy Tarreau45f752e2017-10-30 15:44:59 +01002562 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2563 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2564 /* unblock the connection if it was blocked on this
2565 * stream.
2566 */
2567 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2568 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2569 conn_xprt_want_recv(cs->conn);
2570 conn_xprt_want_send(cs->conn);
2571 }
2572
Willy Tarreau71049cc2018-03-28 13:56:39 +02002573 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002574
Willy Tarreaue323f342018-03-28 13:51:45 +02002575 /* We don't want to close right now unless we're removing the
2576 * last stream, and either the connection is in error, or it
2577 * reached the ID already specified in a GOAWAY frame received
2578 * or sent (as seen by last_sid >= 0).
2579 */
2580 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2581 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002582 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002583 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002584 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002585 (conn_xprt_read0_pending(h2c->conn) ||
2586 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2587 /* no more stream will come, kill it now */
2588 h2_release(h2c->conn);
2589 }
2590 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002591 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002592 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2593 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002594 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002595 else
2596 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002597 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002598}
2599
2600static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2601{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002602 struct h2s *h2s = cs->ctx;
2603
2604 if (!mode)
2605 return;
2606
Willy Tarreau721c9742017-11-07 11:05:42 +01002607 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002608 return;
2609
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002610 /* if no outgoing data was seen on this stream, it means it was
2611 * closed with a "tcp-request content" rule that is normally
2612 * used to kill the connection ASAP (eg: limit abuse). In this
2613 * case we send a goaway to close the connection.
2614 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002615 if (!(h2s->flags & H2_SF_RST_SENT) &&
2616 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002617 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002618
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002619 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2620 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2621 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002622 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002623
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002624 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002625 conn_xprt_want_send(cs->conn);
2626
Willy Tarreau00dd0782018-03-01 16:31:34 +01002627 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002628
2629 add_to_list:
2630 if (LIST_ISEMPTY(&h2s->list)) {
2631 if (h2s->flags & H2_SF_BLK_MFCTL)
2632 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2633 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2634 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2635 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002636}
2637
2638static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2639{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002640 struct h2s *h2s = cs->ctx;
2641
Willy Tarreau721c9742017-11-07 11:05:42 +01002642 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002643 return;
2644
Willy Tarreau67434202017-11-06 20:20:51 +01002645 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002646 /* we can cleanly close using an empty data frame only after headers */
2647
2648 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2649 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002650 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002651
2652 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002653 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002654 else
2655 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002656 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002657 /* if no outgoing data was seen on this stream, it means it was
2658 * closed with a "tcp-request content" rule that is normally
2659 * used to kill the connection ASAP (eg: limit abuse). In this
2660 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002661 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002662 if (!(h2s->flags & H2_SF_RST_SENT) &&
2663 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002664 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002665
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002666 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2667 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002668 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002669 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002670
Willy Tarreau00dd0782018-03-01 16:31:34 +01002671 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002672 }
2673
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002674 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002675 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002676
2677 add_to_list:
2678 if (LIST_ISEMPTY(&h2s->list)) {
2679 if (h2s->flags & H2_SF_BLK_MFCTL)
2680 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2681 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2682 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2683 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002684}
2685
Willy Tarreau13278b42017-10-13 19:23:14 +02002686/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2687 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2688 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002689 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002690 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002691static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002692{
2693 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002694 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002695 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002696 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002697 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002698 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002699 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002700 int flen = h2c->dfl;
2701 int outlen = 0;
2702 int wrap;
2703 int try;
2704
2705 if (!h2c->dfl) {
2706 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002707 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002708 return 0;
2709 }
2710
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002711 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002712 return 0; // incomplete input frame
2713
Willy Tarreau13278b42017-10-13 19:23:14 +02002714 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002715 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002716 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002717 copy = alloc_trash_chunk();
2718 if (!copy) {
2719 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2720 goto fail;
2721 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002722 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2723 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2724 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002725 }
2726
2727 /* The padlen is the first byte before data, and the padding appears
2728 * after data. padlen+data+padding are included in flen.
2729 */
2730 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002731 h2c->dpl = *hdrs;
2732 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002733 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2734 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002735 return 0;
2736 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002737 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002738 hdrs += 1; // skip Pad Length
2739 }
2740
2741 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2742 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002743 if (read_n32(hdrs) == h2s->id) {
2744 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2745 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2746 return 0;//goto fail_stream;
2747 }
2748
Willy Tarreau13278b42017-10-13 19:23:14 +02002749 hdrs += 5; // stream dep = 4, weight = 1
2750 flen -= 5;
2751 }
2752
2753 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2754 * don't support this for now and can't even decompress so we have to
2755 * break the connection.
2756 */
2757 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2758 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002759 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002760 }
2761
Willy Tarreau937f7602018-02-26 15:22:17 +01002762 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2763 if (!csbuf) {
2764 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002765 goto fail;
2766 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002767
Willy Tarreau937f7602018-02-26 15:22:17 +01002768 /* we can't retry a failed decompression operation so we must be very
2769 * careful not to take any risks. In practice the output buffer is
2770 * always empty except maybe for trailers, in which case we simply have
2771 * to wait for the upper layer to finish consuming what is available.
2772 */
2773 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002774 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002775
Willy Tarreau937f7602018-02-26 15:22:17 +01002776 csbuf->head = 0;
2777 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002778
2779 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2780 sizeof(list)/sizeof(list[0]), tmp);
2781 if (outlen < 0) {
2782 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2783 goto fail;
2784 }
2785
2786 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002787 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002788 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002789
2790 if (outlen < 0) {
2791 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2792 goto fail;
2793 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002794
Willy Tarreau174b06a2018-04-25 18:13:58 +02002795 if (msgf & H2_MSGF_BODY) {
2796 /* a payload is present */
2797 if (msgf & H2_MSGF_BODY_CL)
2798 h2s->flags |= H2_SF_DATA_CLEN;
2799 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2800 h2s->flags |= H2_SF_DATA_CHNK;
2801 }
2802
Willy Tarreau13278b42017-10-13 19:23:14 +02002803 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002804 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002805 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002806 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002807
Willy Tarreau937f7602018-02-26 15:22:17 +01002808 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002809 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau937f7602018-02-26 15:22:17 +01002810
Willy Tarreau68dd9852017-07-03 14:44:26 +02002811 leave:
2812 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002813 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002814 fail:
2815 outlen = 0;
2816 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002817}
2818
Willy Tarreau454f9052017-10-26 19:40:35 +02002819/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2820 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2821 * in use, a new chunk is emitted for each frame. This is supposed to fit
2822 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2823 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2824 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2825 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002826 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2827 * checked to know if some data remain pending (an empty DATA frame can return
2828 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2829 * connection errors in h2c->errcode. The caller must already have checked the
2830 * frame header and ensured that the frame was complete or the buffer full. It
2831 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002832 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002833static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002834{
2835 struct h2c *h2c = h2s->h2c;
2836 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002837 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002838 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002839 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002840
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002841 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002842
2843 /* The padlen is the first byte before data, and the padding appears
2844 * after data. padlen+data+padding are included in flen.
2845 */
Willy Tarreau79127812017-12-03 21:06:59 +01002846 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002848 return 0;
2849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002850 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002851 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002852 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2853 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002854 return 0;
2855 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002856
2857 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002859 h2c->dfl--;
2860 h2c->rcvd_c++; h2c->rcvd_s++;
2861 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002862 }
2863
Willy Tarreaud755ea62018-02-26 15:44:54 +01002864 csbuf = h2_get_buf(h2c, &h2s->cs->rxbuf);
2865 if (!csbuf) {
2866 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002867 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002868 }
2869
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002870 flen = h2c->dfl - h2c->dpl;
2871 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002872 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002873
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002874 if (flen > b_data(&h2c->dbuf)) {
2875 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002876 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002877 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002878 }
2879
2880 if (unlikely(b_space_wraps(csbuf))) {
2881 /* it doesn't fit and the buffer is fragmented,
2882 * so let's defragment it and try again.
2883 */
2884 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002885 }
2886
Willy Tarreaueba10f22018-04-25 20:44:22 +02002887 /* chunked-encoding requires more room */
2888 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002889 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002890 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2891 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2892 (chklen < 1048576) ? 4 : 8;
2893 chklen += 4; // CRLF, CRLF
2894 }
2895
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002896 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002897 if (flen + chklen > b_room(csbuf)) {
2898 if (chklen >= b_room(csbuf)) {
2899 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002900 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002901 }
2902 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002903 }
2904
2905 if (h2s->flags & H2_SF_DATA_CHNK) {
2906 /* emit the chunk size */
2907 unsigned int chksz = flen;
2908 char str[10];
2909 char *beg;
2910
2911 beg = str + sizeof(str);
2912 *--beg = '\n';
2913 *--beg = '\r';
2914 do {
2915 *--beg = hextab[chksz & 0xF];
2916 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002917 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002918 }
2919
Willy Tarreau454f9052017-10-26 19:40:35 +02002920 /* Block1 is the length of the first block before the buffer wraps,
2921 * block2 is the optional second block to reach the end of the frame.
2922 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002923 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002924 if (block1 > flen)
2925 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002926 block2 = flen - block1;
2927
2928 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002929 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002930
2931 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002932 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002933
Willy Tarreaueba10f22018-04-25 20:44:22 +02002934 if (h2s->flags & H2_SF_DATA_CHNK) {
2935 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002936 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002937 }
2938
Willy Tarreau454f9052017-10-26 19:40:35 +02002939 /* now mark the input data as consumed (will be deleted from the buffer
2940 * by the caller when seeing FRAME_A after sending the window update).
2941 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002942 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002943 h2c->dfl -= flen;
2944 h2c->rcvd_c += flen;
2945 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2946
2947 if (h2c->dfl > h2c->dpl) {
2948 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002949 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002950 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002951 }
2952
Willy Tarreau4a28da12018-01-04 14:41:00 +01002953 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002954 /* here we're done with the frame, all the payload (except padding) was
2955 * transferred.
2956 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002957
2958 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2959 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 if (b_room(csbuf) < 5) {
2961 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002962 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002963 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002964 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002965 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002966 }
2967
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002968 h2c->rcvd_c += h2c->dpl;
2969 h2c->rcvd_s += h2c->dpl;
2970 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002971 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2972
Willy Tarreaud755ea62018-02-26 15:44:54 +01002973 if (h2c->dff & H2_F_DATA_END_STREAM)
Willy Tarreau454f9052017-10-26 19:40:35 +02002974 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002975
Willy Tarreau454b57b2018-02-26 15:50:05 +01002976 return flen + chklen;
2977 fail:
2978 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002979}
2980
Willy Tarreau62f52692017-10-08 23:01:42 +02002981/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002982 * Called from the upper layer to get more data, up to <count> bytes. The
2983 * caller is responsible for never asking for more data than what is available
2984 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002985 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002986static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02002987{
Willy Tarreau13278b42017-10-13 19:23:14 +02002988 struct h2s *h2s = cs->ctx;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002989 struct buffer *csbuf = &cs->rxbuf;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002990 size_t ret;
Willy Tarreau454f9052017-10-26 19:40:35 +02002991
Willy Tarreau454b57b2018-02-26 15:50:05 +01002992 /* transfer possibly pending data to the upper layer */
2993 ret = b_xfer(buf, csbuf, count);
2994
2995 if (b_data(csbuf))
2996 cs->flags |= CS_FL_RCV_MORE;
2997 else {
2998 cs->flags &= ~CS_FL_RCV_MORE;
2999 if (h2s->flags & H2_SF_ES_RCVD)
3000 cs->flags |= CS_FL_EOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02003001 }
Willy Tarreau454b57b2018-02-26 15:50:05 +01003002
3003 cs_drop_rxbuf(cs);
Willy Tarreau13278b42017-10-13 19:23:14 +02003004 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02003005}
3006
Willy Tarreau5dd17352018-06-14 13:33:30 +02003007/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3008 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3009 * number of bytes sent. The caller must check the stream's status to detect
3010 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003011 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003012static 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 +02003013{
3014 struct http_hdr list[MAX_HTTP_HDR];
3015 struct h2c *h2c = h2s->h2c;
3016 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003017 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003018 int es_now = 0;
3019 int ret = 0;
3020 int hdr;
3021
3022 if (h2c_mux_busy(h2c, h2s)) {
3023 h2s->flags |= H2_SF_BLK_MBUSY;
3024 return 0;
3025 }
3026
Willy Tarreau44e973f2018-03-01 17:49:30 +01003027 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003028 h2c->flags |= H2_CF_MUX_MALLOC;
3029 h2s->flags |= H2_SF_BLK_MROOM;
3030 return 0;
3031 }
3032
3033 /* First, try to parse the H1 response and index it into <list>.
3034 * NOTE! Since it comes from haproxy, we *know* that a response header
3035 * block does not wrap and we can safely read it this way without
3036 * having to realign the buffer.
3037 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003038 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003039 list, sizeof(list)/sizeof(list[0]), h1m);
3040 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003041 /* incomplete or invalid response, this is abnormal coming from
3042 * haproxy and may only result in a bad errorfile or bad Lua code
3043 * so that won't be fixed, raise an error now.
3044 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003045 * FIXME: we should instead add the ability to only return a
3046 * 502 bad gateway. But in theory this is not supposed to
3047 * happen.
3048 */
3049 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3050 ret = 0;
3051 goto end;
3052 }
3053
3054 chunk_reset(&outbuf);
3055
3056 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003057 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003058 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003059 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003060
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003061 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003062 break;
3063 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003064 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003065 }
3066
3067 if (outbuf.size < 9) {
3068 h2c->flags |= H2_CF_MUX_MFULL;
3069 h2s->flags |= H2_SF_BLK_MROOM;
3070 ret = 0;
3071 goto end;
3072 }
3073
3074 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003075 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3076 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3077 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003078
3079 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003080 if (outbuf.data < outbuf.size && h1m->status == 200)
3081 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3082 else if (outbuf.data < outbuf.size && h1m->status == 304)
3083 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003084 else if (unlikely(list[0].v.len != 3)) {
3085 /* this is an unparsable response */
3086 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3087 ret = 0;
3088 goto end;
3089 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003090 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003091 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003092 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3093 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3094 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3095 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3096 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003097 }
3098 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003099 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100 goto realign_again;
3101
3102 h2c->flags |= H2_CF_MUX_MFULL;
3103 h2s->flags |= H2_SF_BLK_MROOM;
3104 ret = 0;
3105 goto end;
3106 }
3107
3108 /* encode all headers, stop at empty name */
3109 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003110 /* these ones do not exist in H2 and must be dropped. */
3111 if (isteq(list[hdr].n, ist("connection")) ||
3112 isteq(list[hdr].n, ist("proxy-connection")) ||
3113 isteq(list[hdr].n, ist("keep-alive")) ||
3114 isteq(list[hdr].n, ist("upgrade")) ||
3115 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003116 continue;
3117
3118 if (isteq(list[hdr].n, ist("")))
3119 break; // end
3120
3121 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3122 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003123 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003124 goto realign_again;
3125
3126 h2c->flags |= H2_CF_MUX_MFULL;
3127 h2s->flags |= H2_SF_BLK_MROOM;
3128 ret = 0;
3129 goto end;
3130 }
3131 }
3132
3133 /* we may need to add END_STREAM */
3134 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3135 es_now = 1;
3136
3137 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003139
3140 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003141 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003142
3143 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003144 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003145
3146 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003148 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003149
3150 /* for now we don't implemented CONTINUATION, so we wait for a
3151 * body or directly end in TRL2.
3152 */
3153 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003154 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003155 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003156
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003157 h1m->state = HTTP_MSG_DONE;
3158 h2s->flags |= H2_SF_ES_SENT;
3159 if (h2s->st == H2_SS_OPEN)
3160 h2s->st = H2_SS_HLOC;
3161 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003162 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003163 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003164 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003165 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003166 h1m->state = HTTP_MSG_RPBEFORE;
3167 h1m->status = 0;
3168 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003169 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003170 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003171 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003172 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003173
3174 end:
3175 //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));
3176 return ret;
3177}
3178
Willy Tarreau5dd17352018-06-14 13:33:30 +02003179/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3180 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3181 * the number of bytes sent. The caller must check the stream's status to
3182 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003183 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003184static 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 +02003185{
3186 struct h2c *h2c = h2s->h2c;
3187 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003188 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003189 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003190 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003191 int es_now = 0;
3192 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003193 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003194 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003195
3196 if (h2c_mux_busy(h2c, h2s)) {
3197 h2s->flags |= H2_SF_BLK_MBUSY;
3198 goto end;
3199 }
3200
Willy Tarreau44e973f2018-03-01 17:49:30 +01003201 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003202 h2c->flags |= H2_CF_MUX_MALLOC;
3203 h2s->flags |= H2_SF_BLK_MROOM;
3204 goto end;
3205 }
3206
3207 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003208 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003209 goto end;
3210
3211 chunk_reset(&outbuf);
3212
3213 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003214 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003215 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003216 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003217
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003218 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003219 break;
3220 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003222 }
3223
3224 if (outbuf.size < 9) {
3225 h2c->flags |= H2_CF_MUX_MFULL;
3226 h2s->flags |= H2_SF_BLK_MROOM;
3227 goto end;
3228 }
3229
3230 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003231 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3232 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3233 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003234
3235 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3236 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003237 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003238 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003239 break;
3240 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003241 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003242 if ((long long)size > h1m->curr_len)
3243 size = h1m->curr_len;
3244 break;
3245 default: /* te:chunked : parse chunks */
3246 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003247 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003248 if (!ret)
3249 goto end;
3250
3251 if (ret < 0) {
3252 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3253 h1m->err_pos = ret;
3254 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3255 goto end;
3256 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003257 max -= ret;
3258 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003259 total += ret;
3260 h1m->state = HTTP_MSG_CHUNK_SIZE;
3261 }
3262
3263 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3264 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003265 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003266 if (!ret)
3267 goto end;
3268
3269 if (ret < 0) {
3270 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3271 h1m->err_pos = ret;
3272 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3273 goto end;
3274 }
3275
3276 size = chunk;
3277 h1m->curr_len = chunk;
3278 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003279 max -= ret;
3280 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003281 total += ret;
3282 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3283 if (!size)
3284 goto send_empty;
3285 }
3286
3287 /* in MSG_DATA state, continue below */
3288 size = h1m->curr_len;
3289 break;
3290 }
3291
3292 /* we have in <size> the exact number of bytes we need to copy from
3293 * the H1 buffer. We need to check this against the connection's and
3294 * the stream's send windows, and to ensure that this fits in the max
3295 * frame size and in the buffer's available space minus 9 bytes (for
3296 * the frame header). The connection's flow control is applied last so
3297 * that we can use a separate list of streams which are immediately
3298 * unblocked on window opening. Note: we don't implement padding.
3299 */
3300
Willy Tarreau5dd17352018-06-14 13:33:30 +02003301 if (size > max)
3302 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003303
3304 if (size > h2s->mws)
3305 size = h2s->mws;
3306
3307 if (size <= 0) {
3308 h2s->flags |= H2_SF_BLK_SFCTL;
3309 goto end;
3310 }
3311
3312 if (h2c->mfs && size > h2c->mfs)
3313 size = h2c->mfs;
3314
3315 if (size + 9 > outbuf.size) {
3316 /* we have an opportunity for enlarging the too small
3317 * available space, let's try.
3318 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003319 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 goto realign_again;
3321 size = outbuf.size - 9;
3322 }
3323
3324 if (size <= 0) {
3325 h2c->flags |= H2_CF_MUX_MFULL;
3326 h2s->flags |= H2_SF_BLK_MROOM;
3327 goto end;
3328 }
3329
3330 if (size > h2c->mws)
3331 size = h2c->mws;
3332
3333 if (size <= 0) {
3334 h2s->flags |= H2_SF_BLK_MFCTL;
3335 goto end;
3336 }
3337
3338 /* copy whatever we can */
3339 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003340 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 if (ret == 1)
3342 len2 = 0;
3343
3344 if (!ret || len1 + len2 < size) {
3345 /* FIXME: must normally never happen */
3346 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3347 goto end;
3348 }
3349
3350 /* limit len1/len2 to size */
3351 if (len1 + len2 > size) {
3352 int sub = len1 + len2 - size;
3353
3354 if (len2 > sub)
3355 len2 -= sub;
3356 else {
3357 sub -= len2;
3358 len2 = 0;
3359 len1 -= sub;
3360 }
3361 }
3362
3363 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003364 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003365 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003366 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003367
3368 send_empty:
3369 /* we may need to add END_STREAM */
3370 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3371 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003372 *
3373 * FIXME: what we do here is not correct because we send end_stream
3374 * before knowing if we'll have to send a HEADERS frame for the
3375 * trailers. More importantly we're not consuming the trailing CRLF
3376 * after the end of trailers, so it will be left to the caller to
3377 * eat it. The right way to do it would be to measure trailers here
3378 * and to send ES only if there are no trailers.
3379 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003380 */
3381 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3382 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3383 es_now = 1;
3384
3385 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003386 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003387
3388 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003389 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003390
3391 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003392 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003393
3394 /* consume incoming H1 response */
3395 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003396 max -= size;
3397 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003398 total += size;
3399 h1m->curr_len -= size;
3400 h2s->mws -= size;
3401 h2c->mws -= size;
3402
3403 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3404 h1m->state = HTTP_MSG_CHUNK_CRLF;
3405 goto new_frame;
3406 }
3407 }
3408
3409 if (es_now) {
3410 if (h2s->st == H2_SS_OPEN)
3411 h2s->st = H2_SS_HLOC;
3412 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003413 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003414
Willy Tarreau35a62702018-02-27 15:37:25 +01003415 if (!(h1m->flags & H1_MF_CHNK)) {
3416 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003417 total += max;
3418 ofs += max;
3419 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003420
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003421 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003422 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003423
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003424 h2s->flags |= H2_SF_ES_SENT;
3425 }
3426
3427 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003428 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 +02003429 return total;
3430}
3431
Olivier Houchard6ff20392018-07-17 18:46:31 +02003432/* Called from the upper layer, to subscribe to events, such as being able to send */
3433static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3434{
3435 struct wait_list *sw;
3436 struct h2s *h2s = cs->ctx;
3437
3438 switch (event_type) {
3439 case SUB_CAN_SEND:
3440 sw = param;
3441 if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list))
3442 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
3443 return 0;
3444 default:
3445 break;
3446 }
3447 return -1;
3448
3449
3450}
3451
Willy Tarreau62f52692017-10-08 23:01:42 +02003452/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003453static 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 +02003454{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003455 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003456 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003457 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003458
Willy Tarreau0bad0432018-06-14 16:54:01 +02003459 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003460 h2s->flags |= H2_SF_OUTGOING_DATA;
3461
Willy Tarreau0bad0432018-06-14 16:54:01 +02003462 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003463 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003464 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003466 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003467 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003468 }
3469 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3470 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003471 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003472
Willy Tarreau5dd17352018-06-14 13:33:30 +02003473 if (unlikely((int)ret <= 0)) {
3474 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003475 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3476 break;
3477 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003478 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003479 total += count;
3480 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003481 h2s->res.state = HTTP_MSG_DONE;
3482 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003483 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003484 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003485 cs->flags |= CS_FL_ERROR;
3486 break;
3487 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003488
3489 total += ret;
3490 count -= ret;
3491
3492 if (h2s->st >= H2_SS_ERROR)
3493 break;
3494
3495 if (h2s->flags & H2_SF_BLK_ANY)
3496 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003497 }
3498
Willy Tarreau00610962018-07-19 10:58:28 +02003499 if (h2s->st >= H2_SS_ERROR) {
3500 /* trim any possibly pending data after we close (extra CR-LF,
3501 * unprocessed trailers, abnormal extra data, ...)
3502 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003503 total += count;
3504 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003505 }
3506
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003507 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003508 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003509 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003510 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003511 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003512 }
3513
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003514 if (h2s->flags & H2_SF_BLK_SFCTL) {
3515 /* stream flow control, quit the list */
3516 LIST_DEL(&h2s->list);
3517 LIST_INIT(&h2s->list);
3518 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003519 else if (LIST_ISEMPTY(&h2s->list)) {
3520 if (h2s->flags & H2_SF_BLK_MFCTL)
3521 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003522 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003524 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003525}
3526
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003527/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003528static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003529{
3530 struct h2c *h2c = conn->mux_ctx;
3531 struct h2s *h2s;
3532 struct eb32_node *node;
3533 int fctl_cnt = 0;
3534 int send_cnt = 0;
3535 int tree_cnt = 0;
3536 int orph_cnt = 0;
3537
3538 if (!h2c)
3539 return;
3540
3541 list_for_each_entry(h2s, &h2c->fctl_list, list)
3542 fctl_cnt++;
3543
3544 list_for_each_entry(h2s, &h2c->send_list, list)
3545 send_cnt++;
3546
3547 node = eb32_first(&h2c->streams_by_id);
3548 while (node) {
3549 h2s = container_of(node, struct h2s, by_id);
3550 tree_cnt++;
3551 if (!h2s->cs)
3552 orph_cnt++;
3553 node = eb32_next(node);
3554 }
3555
Willy Tarreauc65edac2018-07-19 10:54:43 +02003556 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 +02003557 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 +02003558}
Willy Tarreau62f52692017-10-08 23:01:42 +02003559
3560/*******************************************************/
3561/* functions below are dedicated to the config parsers */
3562/*******************************************************/
3563
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003564/* config parser for global "tune.h2.header-table-size" */
3565static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3566 struct proxy *defpx, const char *file, int line,
3567 char **err)
3568{
3569 if (too_many_args(1, args, err, NULL))
3570 return -1;
3571
3572 h2_settings_header_table_size = atoi(args[1]);
3573 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3574 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3575 return -1;
3576 }
3577 return 0;
3578}
Willy Tarreau62f52692017-10-08 23:01:42 +02003579
Willy Tarreaue6baec02017-07-27 11:45:11 +02003580/* config parser for global "tune.h2.initial-window-size" */
3581static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3582 struct proxy *defpx, const char *file, int line,
3583 char **err)
3584{
3585 if (too_many_args(1, args, err, NULL))
3586 return -1;
3587
3588 h2_settings_initial_window_size = atoi(args[1]);
3589 if (h2_settings_initial_window_size < 0) {
3590 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3591 return -1;
3592 }
3593 return 0;
3594}
3595
Willy Tarreau5242ef82017-07-27 11:47:28 +02003596/* config parser for global "tune.h2.max-concurrent-streams" */
3597static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3598 struct proxy *defpx, const char *file, int line,
3599 char **err)
3600{
3601 if (too_many_args(1, args, err, NULL))
3602 return -1;
3603
3604 h2_settings_max_concurrent_streams = atoi(args[1]);
3605 if (h2_settings_max_concurrent_streams < 0) {
3606 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3607 return -1;
3608 }
3609 return 0;
3610}
3611
Willy Tarreau62f52692017-10-08 23:01:42 +02003612
3613/****************************************/
3614/* MUX initialization and instanciation */
3615/***************************************/
3616
3617/* The mux operations */
3618const struct mux_ops h2_ops = {
3619 .init = h2_init,
3620 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003621 .wake = h2_wake,
3622 .update_poll = h2_update_poll,
3623 .rcv_buf = h2_rcv_buf,
3624 .snd_buf = h2_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003625 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003626 .attach = h2_attach,
3627 .detach = h2_detach,
3628 .shutr = h2_shutr,
3629 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003630 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003631 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003632 .name = "H2",
3633};
3634
3635/* ALPN selection : this mux registers ALPN tolen "h2" */
3636static struct alpn_mux_list alpn_mux_h2 =
3637 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3638
3639/* config keyword parsers */
3640static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003641 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003642 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003643 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003644 { 0, NULL, NULL }
3645}};
3646
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003647static void __h2_deinit(void)
3648{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003649 pool_destroy(pool_head_h2s);
3650 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003651}
3652
Willy Tarreau62f52692017-10-08 23:01:42 +02003653__attribute__((constructor))
3654static void __h2_init(void)
3655{
3656 alpn_register_mux(&alpn_mux_h2);
3657 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003658 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003659 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3660 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003661}