blob: 538d21f07a787bb5ad0fb2e1f9c7d173541612e1 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard6ff20392018-07-17 18:46:31 +0200123 struct list send_wait_list; /* list of tasks to wake when we're ready to send */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200124 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200125};
126
Willy Tarreau18312642017-10-11 07:57:07 +0200127/* H2 stream state, in h2s->st */
128enum h2_ss {
129 H2_SS_IDLE = 0, // idle
130 H2_SS_RLOC, // reserved(local)
131 H2_SS_RREM, // reserved(remote)
132 H2_SS_OPEN, // open
133 H2_SS_HREM, // half-closed(remote)
134 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200135 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200136 H2_SS_CLOSED, // closed
137 H2_SS_ENTRIES // must be last
138} __attribute__((packed));
139
140/* HTTP/2 stream flags (32 bit), in h2s->flags */
141#define H2_SF_NONE 0x00000000
142#define H2_SF_ES_RCVD 0x00000001
143#define H2_SF_ES_SENT 0x00000002
144
145#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
146#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
147
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200148/* stream flags indicating the reason the stream is blocked */
149#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
150#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
151#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
152#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
153#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
154
Willy Tarreau454f9052017-10-26 19:40:35 +0200155/* stream flags indicating how data is supposed to be sent */
156#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
157#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
158
159/* step we're currently in when sending chunks. This is needed because we may
160 * have to transfer chunks as large as a full buffer so there's no room left
161 * for size nor crlf around.
162 */
163#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
164#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
165#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
166
167#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
168
Willy Tarreau67434202017-11-06 20:20:51 +0100169#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100170#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100171
Willy Tarreau18312642017-10-11 07:57:07 +0200172/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
173 * it is being processed in the internal HTTP representation (H1 for now).
174 */
175struct h2s {
176 struct conn_stream *cs;
177 struct h2c *h2c;
178 struct h1m req, res; /* request and response parser state for H1 */
179 struct eb32_node by_id; /* place in h2c's streams_by_id */
180 struct list list; /* position in active/blocked lists if blocked>0 */
181 int32_t id; /* stream ID */
182 uint32_t flags; /* H2_SF_* */
183 int mws; /* mux window size for this stream */
184 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
185 enum h2_ss st;
Olivier Houchard638b7992018-08-16 15:41:52 +0200186 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreau18312642017-10-11 07:57:07 +0200187};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200188
Willy Tarreauc6405142017-09-21 20:23:50 +0200189/* descriptor for an h2 frame header */
190struct h2_fh {
191 uint32_t len; /* length, host order, 24 bits */
192 uint32_t sid; /* stream id, host order, 31 bits */
193 uint8_t ft; /* frame type */
194 uint8_t ff; /* frame flags */
195};
196
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200197/* a few settings from the global section */
198static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200199static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200200static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200201
Willy Tarreau2a856182017-05-16 15:20:39 +0200202/* a dmumy closed stream */
203static const struct h2s *h2_closed_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100208 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200209 .id = 0,
210};
211
212/* and a dummy idle stream for use with any unannounced stream */
213static const struct h2s *h2_idle_stream = &(const struct h2s){
214 .cs = NULL,
215 .h2c = NULL,
216 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100217 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
Olivier Houchard9f6af332018-05-25 14:04:04 +0200221static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200222static struct task *h2_send(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100223static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100224static int h2_frt_decode_headers(struct h2s *h2s);
225static int h2_frt_transfer_data(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200227/*****************************************************/
228/* functions below are for dynamic buffer management */
229/*****************************************************/
230
Willy Tarreau315d8072017-12-10 22:17:57 +0100231/* indicates whether or not the we may call the h2_recv() function to attempt
232 * to receive data into the buffer and/or demux pending data. The condition is
233 * a bit complex due to some API limits for now. The rules are the following :
234 * - if an error or a shutdown was detected on the connection and the buffer
235 * is empty, we must not attempt to receive
236 * - if the demux buf failed to be allocated, we must not try to receive and
237 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100238 * - if no flag indicates a blocking condition, we may attempt to receive,
239 * regardless of whether the demux buffer is full or not, so that only
240 * de demux part decides whether or not to block. This is needed because
241 * the connection API indeed prevents us from re-enabling receipt that is
242 * already enabled in a polled state, so we must always immediately stop
243 * as soon as the demux can't proceed so as never to hit an end of read
244 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100245 * - otherwise must may not attempt
246 */
247static inline int h2_recv_allowed(const struct h2c *h2c)
248{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200249 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 (h2c->st0 >= H2_CS_ERROR ||
251 h2c->conn->flags & CO_FL_ERROR ||
252 conn_xprt_read0_pending(h2c->conn)))
253 return 0;
254
255 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100256 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100257 return 1;
258
259 return 0;
260}
261
Willy Tarreauf2101912018-07-19 10:11:38 +0200262/* returns true if the connection has too many conn_streams attached */
263static inline int h2_has_too_many_cs(const struct h2c *h2c)
264{
265 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
266}
267
Willy Tarreau44e973f2018-03-01 17:49:30 +0100268/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
269 * flags are used to figure what buffer was requested. It returns 1 if the
270 * allocation succeeds, in which case the connection is woken up, or 0 if it's
271 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200272 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100273static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200274{
275 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100276 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200279 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100280 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200281 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282 return 1;
283 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200284
Willy Tarreau44e973f2018-03-01 17:49:30 +0100285 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
286 h2c->flags &= ~H2_CF_MUX_MALLOC;
287 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
288 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200289
290 if (h2c->flags & H2_CF_DEM_MROOM) {
291 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100292 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 conn_xprt_want_recv(h2c->conn);
294 }
Willy Tarreau14398122017-09-22 14:26:04 +0200295 return 1;
296 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100297
298 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
299 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200300 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100301 h2c->flags &= ~H2_CF_DEM_SALLOC;
302 if (h2_recv_allowed(h2c))
303 conn_xprt_want_recv(h2c->conn);
304 return 1;
305 }
306
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 0;
308}
309
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200310static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200311{
312 struct buffer *buf = NULL;
313
Willy Tarreau44e973f2018-03-01 17:49:30 +0100314 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
315 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
316 h2c->buf_wait.target = h2c;
317 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100318 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100319 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100320 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200321 __conn_xprt_stop_recv(h2c->conn);
322 }
323 return buf;
324}
325
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200326static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200327{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200328 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100329 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200330 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200331 }
332}
333
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200334
Willy Tarreau62f52692017-10-08 23:01:42 +0200335/*****************************************************************/
336/* functions below are dedicated to the mux setup and management */
337/*****************************************************************/
338
Willy Tarreau32218eb2017-09-22 08:07:25 +0200339/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
340static int h2c_frt_init(struct connection *conn)
341{
342 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100343 struct task *t = NULL;
344 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200345
Willy Tarreaubafbe012017-11-24 17:34:44 +0100346 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200347 if (!h2c)
348 goto fail;
349
Willy Tarreau3f133572017-10-31 19:21:06 +0100350
Willy Tarreau599391a2017-11-24 10:16:00 +0100351 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
352 if (tick_isset(sess->fe->timeout.clientfin))
353 h2c->shut_timeout = sess->fe->timeout.clientfin;
354
Willy Tarreau33400292017-11-05 11:23:40 +0100355 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100356 if (tick_isset(h2c->timeout)) {
357 t = task_new(tid_bit);
358 if (!t)
359 goto fail;
360
361 h2c->task = t;
362 t->process = h2_timeout_task;
363 t->context = h2c;
364 t->expire = tick_add(now_ms, h2c->timeout);
365 }
Willy Tarreauea392822017-10-31 10:02:25 +0100366
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200367 h2c->wait_list.task = tasklet_new();
368 if (!h2c->wait_list.task)
369 goto fail;
370 h2c->wait_list.task->process = h2_send;
371 h2c->wait_list.task->context = conn;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200372 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200373
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
375 if (!h2c->ddht)
376 goto fail;
377
378 /* Initialise the context. */
379 h2c->st0 = H2_CS_PREFACE;
380 h2c->conn = conn;
381 h2c->max_id = -1;
382 h2c->errcode = H2_ERR_NO_ERROR;
383 h2c->flags = H2_CF_NONE;
384 h2c->rcvd_c = 0;
385 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100386 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200387 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200390 h2c->dsi = -1;
391 h2c->msi = -1;
392 h2c->last_sid = -1;
393
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200394 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200395 h2c->miw = 65535; /* mux initial window size */
396 h2c->mws = 65535; /* mux window size */
397 h2c->mfs = 16384; /* initial max frame size */
398 h2c->streams_by_id = EB_ROOT_UNIQUE;
399 LIST_INIT(&h2c->send_list);
400 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100401 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402 conn->mux_ctx = h2c;
403
Willy Tarreau3f133572017-10-31 19:21:06 +0100404 if (t)
405 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200406 conn_xprt_want_recv(conn);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200407 LIST_INIT(&h2c->send_wait_list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200408 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100409
Willy Tarreau32218eb2017-09-22 08:07:25 +0200410 /* mux->wake will be called soon to complete the operation */
411 return 0;
412 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100413 if (t)
414 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200415 if (h2c->wait_list.task)
416 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100417 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 return -1;
419}
420
Willy Tarreau62f52692017-10-08 23:01:42 +0200421/* Initialize the mux once it's attached. For outgoing connections, the context
422 * is already initialized before installing the mux, so we detect incoming
423 * connections from the fact that the context is still NULL. Returns < 0 on
424 * error.
425 */
426static int h2_init(struct connection *conn)
427{
428 if (conn->mux_ctx) {
429 /* we don't support outgoing connections for now */
430 return -1;
431 }
432
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200434}
435
Willy Tarreau2373acc2017-10-12 17:35:14 +0200436/* returns the stream associated with id <id> or NULL if not found */
437static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
438{
439 struct eb32_node *node;
440
Willy Tarreau2a856182017-05-16 15:20:39 +0200441 if (id > h2c->max_id)
442 return (struct h2s *)h2_idle_stream;
443
Willy Tarreau2373acc2017-10-12 17:35:14 +0200444 node = eb32_lookup(&h2c->streams_by_id, id);
445 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200446 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200447
448 return container_of(node, struct h2s, by_id);
449}
450
Willy Tarreau62f52692017-10-08 23:01:42 +0200451/* release function for a connection. This one should be called to free all
452 * resources allocated to the mux.
453 */
454static void h2_release(struct connection *conn)
455{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200456 struct h2c *h2c = conn->mux_ctx;
457
458 LIST_DEL(&conn->list);
459
460 if (h2c) {
461 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200462
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100463 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100464 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100465 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200466
Willy Tarreau44e973f2018-03-01 17:49:30 +0100467 h2_release_buf(h2c, &h2c->dbuf);
468 h2_release_buf(h2c, &h2c->mbuf);
469
Willy Tarreauea392822017-10-31 10:02:25 +0100470 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200471 h2c->task->context = NULL;
472 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100473 h2c->task = NULL;
474 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200475 if (h2c->wait_list.task)
476 tasklet_free(h2c->wait_list.task);
Willy Tarreauea392822017-10-31 10:02:25 +0100477
Willy Tarreaubafbe012017-11-24 17:34:44 +0100478 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200479 }
480
481 conn->mux = NULL;
482 conn->mux_ctx = NULL;
483
484 conn_stop_tracking(conn);
485 conn_full_close(conn);
486 if (conn->destroy_cb)
487 conn->destroy_cb(conn);
488 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200489}
490
491
Willy Tarreau71681172017-10-23 14:39:06 +0200492/******************************************************/
493/* functions below are for the H2 protocol processing */
494/******************************************************/
495
496/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100497static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200498{
499 return h2s ? h2s->id : 0;
500}
501
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200502/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100503static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200504{
505 if (h2c->msi < 0)
506 return 0;
507
508 if (h2c->msi == h2s_id(h2s))
509 return 0;
510
511 return 1;
512}
513
Willy Tarreau741d6df2017-10-17 08:00:59 +0200514/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100515static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200516{
517 h2c->errcode = err;
518 h2c->st0 = H2_CS_ERROR;
519}
520
Willy Tarreau2e43f082017-10-17 08:03:59 +0200521/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100522static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200523{
524 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
525 h2s->errcode = err;
526 h2s->st = H2_SS_ERROR;
527 if (h2s->cs)
528 h2s->cs->flags |= CS_FL_ERROR;
529 }
530}
531
Willy Tarreaue4820742017-07-27 13:37:23 +0200532/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100533static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200534{
535 uint8_t *out = frame;
536
537 *out = len >> 16;
538 write_n16(out + 1, len);
539}
540
Willy Tarreau54c15062017-10-10 17:10:03 +0200541/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
542 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
543 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200544 * available in the buffer's input prior to calling this function. The buffer
545 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200546 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100547static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200548 const struct buffer *b, int o)
549{
Willy Tarreau591d4452018-06-15 17:21:00 +0200550 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 +0200551}
552
Willy Tarreau1f094672017-11-20 21:27:45 +0100553static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200554{
Willy Tarreau591d4452018-06-15 17:21:00 +0200555 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200556}
557
Willy Tarreau1f094672017-11-20 21:27:45 +0100558static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200559{
Willy Tarreau591d4452018-06-15 17:21:00 +0200560 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200561}
562
Willy Tarreau1f094672017-11-20 21:27:45 +0100563static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200564{
Willy Tarreau591d4452018-06-15 17:21:00 +0200565 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200566}
567
568
Willy Tarreau715d5312017-07-11 15:20:24 +0200569/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
570 * is not obvious. It turns out that H2 headers are neither aligned nor do they
571 * use regular sizes. And to add to the trouble, the buffer may wrap so each
572 * byte read must be checked. The header is formed like this :
573 *
574 * b0 b1 b2 b3 b4 b5..b8
575 * +----------+---------+--------+----+----+----------------------+
576 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
577 * +----------+---------+--------+----+----+----------------------+
578 *
579 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
580 * we get the sid properly aligned and ordered, and 16 bits of len properly
581 * ordered as well. The type and flags can be extracted using bit shifts from
582 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200583 * Returns zero if some bytes are missing, otherwise non-zero on success. The
584 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200585 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100586static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200587{
588 uint64_t w;
589
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200590 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200591 return 0;
592
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200593 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200594 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200595 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
596 h->ff = w >> 32;
597 h->ft = w >> 40;
598 h->len += w >> 48;
599 return 1;
600}
601
602/* skip the next 9 bytes corresponding to the frame header possibly parsed by
603 * h2_peek_frame_hdr() above.
604 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100605static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200606{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200607 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200608}
609
610/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100611static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200612{
613 int ret;
614
615 ret = h2_peek_frame_hdr(b, h);
616 if (ret > 0)
617 h2_skip_frame_hdr(b);
618 return ret;
619}
620
Willy Tarreau00dd0782018-03-01 16:31:34 +0100621/* marks stream <h2s> as CLOSED and decrement the number of active streams for
622 * its connection if the stream was not yet closed. Please use this exclusively
623 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100624 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100625static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100626{
627 if (h2s->st != H2_SS_CLOSED)
628 h2s->h2c->nb_streams--;
629 h2s->st = H2_SS_CLOSED;
630}
631
Willy Tarreau71049cc2018-03-28 13:56:39 +0200632/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
633static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100634{
635 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200636 LIST_DEL(&h2s->list);
637 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100638 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200639 if (b_size(&h2s->rxbuf)) {
640 b_free(&h2s->rxbuf);
641 offer_buffers(NULL, tasks_run_queue);
642 }
Willy Tarreau0a10de62018-03-01 16:27:53 +0100643 pool_free(pool_head_h2s, h2s);
644}
645
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646/* creates a new stream <id> on the h2c connection and returns it, or NULL in
647 * case of memory allocation error.
648 */
649static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
650{
651 struct conn_stream *cs;
652 struct h2s *h2s;
653
Willy Tarreaubafbe012017-11-24 17:34:44 +0100654 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200655 if (!h2s)
656 goto out;
657
658 h2s->h2c = h2c;
659 h2s->mws = h2c->miw;
660 h2s->flags = H2_SF_NONE;
661 h2s->errcode = H2_ERR_NO_ERROR;
662 h2s->st = H2_SS_IDLE;
Olivier Houchard638b7992018-08-16 15:41:52 +0200663 h2s->rxbuf = BUF_NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200664 h1m_init(&h2s->req);
665 h1m_init(&h2s->res);
666 h2s->by_id.key = h2s->id = id;
667 h2c->max_id = id;
668 LIST_INIT(&h2s->list);
669
670 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100671 h2c->nb_streams++;
672 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
673 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674
675 cs = cs_new(h2c->conn);
676 if (!cs)
677 goto out_close;
678
679 h2s->cs = cs;
680 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200681 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200682
683 if (stream_create_from_cs(cs) < 0)
684 goto out_free_cs;
685
686 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200687 if (h2_has_too_many_cs(h2c))
688 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200689 return h2s;
690
691 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200692 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200693 cs_free(cs);
694 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200695 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200696 h2s = NULL;
697 out:
698 return h2s;
699}
700
Willy Tarreaube5b7152017-09-25 16:25:39 +0200701/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
702 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
703 * the various settings codes.
704 */
705static int h2c_snd_settings(struct h2c *h2c)
706{
707 struct buffer *res;
708 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200709 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200710 int ret;
711
712 if (h2c_mux_busy(h2c, NULL)) {
713 h2c->flags |= H2_CF_DEM_MBUSY;
714 return 0;
715 }
716
Willy Tarreau44e973f2018-03-01 17:49:30 +0100717 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200718 if (!res) {
719 h2c->flags |= H2_CF_MUX_MALLOC;
720 h2c->flags |= H2_CF_DEM_MROOM;
721 return 0;
722 }
723
724 chunk_init(&buf, buf_data, sizeof(buf_data));
725 chunk_memcpy(&buf,
726 "\x00\x00\x00" /* length : 0 for now */
727 "\x04\x00" /* type : 4 (settings), flags : 0 */
728 "\x00\x00\x00\x00", /* stream ID : 0 */
729 9);
730
731 if (h2_settings_header_table_size != 4096) {
732 char str[6] = "\x00\x01"; /* header_table_size */
733
734 write_n32(str + 2, h2_settings_header_table_size);
735 chunk_memcat(&buf, str, 6);
736 }
737
738 if (h2_settings_initial_window_size != 65535) {
739 char str[6] = "\x00\x04"; /* initial_window_size */
740
741 write_n32(str + 2, h2_settings_initial_window_size);
742 chunk_memcat(&buf, str, 6);
743 }
744
745 if (h2_settings_max_concurrent_streams != 0) {
746 char str[6] = "\x00\x03"; /* max_concurrent_streams */
747
748 /* Note: 0 means "unlimited" for haproxy's config but not for
749 * the protocol, so never send this value!
750 */
751 write_n32(str + 2, h2_settings_max_concurrent_streams);
752 chunk_memcat(&buf, str, 6);
753 }
754
755 if (global.tune.bufsize != 16384) {
756 char str[6] = "\x00\x05"; /* max_frame_size */
757
758 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
759 * match bufsize - rewrite size, but at the moment it seems
760 * that clients don't take care of it.
761 */
762 write_n32(str + 2, global.tune.bufsize);
763 chunk_memcat(&buf, str, 6);
764 }
765
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 h2_set_frame_size(buf.area, buf.data - 9);
767 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200768 if (unlikely(ret <= 0)) {
769 if (!ret) {
770 h2c->flags |= H2_CF_MUX_MFULL;
771 h2c->flags |= H2_CF_DEM_MROOM;
772 return 0;
773 }
774 else {
775 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
776 return 0;
777 }
778 }
779 return ret;
780}
781
Willy Tarreau52eed752017-09-22 15:05:09 +0200782/* Try to receive a connection preface, then upon success try to send our
783 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
784 * missing data. It may return an error in h2c.
785 */
786static int h2c_frt_recv_preface(struct h2c *h2c)
787{
788 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200789 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200790
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200791 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200792
793 if (unlikely(ret1 <= 0)) {
794 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
795 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
796 return 0;
797 }
798
Willy Tarreaube5b7152017-09-25 16:25:39 +0200799 ret2 = h2c_snd_settings(h2c);
800 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200801 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200802
Willy Tarreaube5b7152017-09-25 16:25:39 +0200803 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200804}
805
Willy Tarreau081d4722017-05-16 21:51:05 +0200806/* try to send a GOAWAY frame on the connection to report an error or a graceful
807 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
808 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
809 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
810 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
811 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
812 * on unrecoverable failure. It will not attempt to send one again in this last
813 * case so that it is safe to use h2c_error() to report such errors.
814 */
815static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
816{
817 struct buffer *res;
818 char str[17];
819 int ret;
820
821 if (h2c->flags & H2_CF_GOAWAY_FAILED)
822 return 1; // claim that it worked
823
824 if (h2c_mux_busy(h2c, h2s)) {
825 if (h2s)
826 h2s->flags |= H2_SF_BLK_MBUSY;
827 else
828 h2c->flags |= H2_CF_DEM_MBUSY;
829 return 0;
830 }
831
Willy Tarreau44e973f2018-03-01 17:49:30 +0100832 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200833 if (!res) {
834 h2c->flags |= H2_CF_MUX_MALLOC;
835 if (h2s)
836 h2s->flags |= H2_SF_BLK_MROOM;
837 else
838 h2c->flags |= H2_CF_DEM_MROOM;
839 return 0;
840 }
841
842 /* len: 8, type: 7, flags: none, sid: 0 */
843 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
844
845 if (h2c->last_sid < 0)
846 h2c->last_sid = h2c->max_id;
847
848 write_n32(str + 9, h2c->last_sid);
849 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200850 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200851 if (unlikely(ret <= 0)) {
852 if (!ret) {
853 h2c->flags |= H2_CF_MUX_MFULL;
854 if (h2s)
855 h2s->flags |= H2_SF_BLK_MROOM;
856 else
857 h2c->flags |= H2_CF_DEM_MROOM;
858 return 0;
859 }
860 else {
861 /* we cannot report this error using GOAWAY, so we mark
862 * it and claim a success.
863 */
864 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
865 h2c->flags |= H2_CF_GOAWAY_FAILED;
866 return 1;
867 }
868 }
869 h2c->flags |= H2_CF_GOAWAY_SENT;
870 return ret;
871}
872
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100873/* Try to send an RST_STREAM frame on the connection for the indicated stream
874 * during mux operations. This stream must be valid and cannot be closed
875 * already. h2s->id will be used for the stream ID and h2s->errcode will be
876 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
877 * not yet.
878 *
879 * Returns > 0 on success or zero if nothing was done. In case of lack of room
880 * to write the message, it subscribes the stream to future notifications.
881 */
882static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
883{
884 struct buffer *res;
885 char str[13];
886 int ret;
887
888 if (!h2s || h2s->st == H2_SS_CLOSED)
889 return 1;
890
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100891 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
892 * RST_STREAM in response to a RST_STREAM frame.
893 */
894 if (h2c->dft == H2_FT_RST_STREAM) {
895 ret = 1;
896 goto ignore;
897 }
898
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100899 if (h2c_mux_busy(h2c, h2s)) {
900 h2s->flags |= H2_SF_BLK_MBUSY;
901 return 0;
902 }
903
Willy Tarreau44e973f2018-03-01 17:49:30 +0100904 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100905 if (!res) {
906 h2c->flags |= H2_CF_MUX_MALLOC;
907 h2s->flags |= H2_SF_BLK_MROOM;
908 return 0;
909 }
910
911 /* len: 4, type: 3, flags: none */
912 memcpy(str, "\x00\x00\x04\x03\x00", 5);
913 write_n32(str + 5, h2s->id);
914 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200915 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100916
917 if (unlikely(ret <= 0)) {
918 if (!ret) {
919 h2c->flags |= H2_CF_MUX_MFULL;
920 h2s->flags |= H2_SF_BLK_MROOM;
921 return 0;
922 }
923 else {
924 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
925 return 0;
926 }
927 }
928
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100929 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100930 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100931 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100932 return ret;
933}
934
935/* Try to send an RST_STREAM frame on the connection for the stream being
936 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
937 * error code unless the stream's state already is IDLE or CLOSED in which
938 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
939 * it was not yet.
940 *
941 * Returns > 0 on success or zero if nothing was done. In case of lack of room
942 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200943 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100944 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200945 */
946static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
947{
948 struct buffer *res;
949 char str[13];
950 int ret;
951
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100952 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
953 * RST_STREAM in response to a RST_STREAM frame.
954 */
955 if (h2c->dft == H2_FT_RST_STREAM) {
956 ret = 1;
957 goto ignore;
958 }
959
Willy Tarreau27a84c92017-10-17 08:10:17 +0200960 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100961 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return 0;
963 }
964
Willy Tarreau44e973f2018-03-01 17:49:30 +0100965 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200966 if (!res) {
967 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100968 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200969 return 0;
970 }
971
972 /* len: 4, type: 3, flags: none */
973 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100974
Willy Tarreau27a84c92017-10-17 08:10:17 +0200975 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100976 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200978 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100979
Willy Tarreau27a84c92017-10-17 08:10:17 +0200980 if (unlikely(ret <= 0)) {
981 if (!ret) {
982 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100983 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200984 return 0;
985 }
986 else {
987 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
988 return 0;
989 }
990 }
991
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100992 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100993 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200994 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100995 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100996 }
997
Willy Tarreau27a84c92017-10-17 08:10:17 +0200998 return ret;
999}
1000
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001001/* try to send an empty DATA frame with the ES flag set to notify about the
1002 * end of stream and match a shutdown(write). If an ES was already sent as
1003 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1004 * on success or zero if nothing was done. In case of lack of room to write the
1005 * message, it subscribes the requesting stream to future notifications.
1006 */
1007static int h2_send_empty_data_es(struct h2s *h2s)
1008{
1009 struct h2c *h2c = h2s->h2c;
1010 struct buffer *res;
1011 char str[9];
1012 int ret;
1013
Willy Tarreau721c9742017-11-07 11:05:42 +01001014 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001015 return 1;
1016
1017 if (h2c_mux_busy(h2c, h2s)) {
1018 h2s->flags |= H2_SF_BLK_MBUSY;
1019 return 0;
1020 }
1021
Willy Tarreau44e973f2018-03-01 17:49:30 +01001022 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001023 if (!res) {
1024 h2c->flags |= H2_CF_MUX_MALLOC;
1025 h2s->flags |= H2_SF_BLK_MROOM;
1026 return 0;
1027 }
1028
1029 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1030 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1031 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001032 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001033 if (likely(ret > 0)) {
1034 h2s->flags |= H2_SF_ES_SENT;
1035 }
1036 else if (!ret) {
1037 h2c->flags |= H2_CF_MUX_MFULL;
1038 h2s->flags |= H2_SF_BLK_MROOM;
1039 return 0;
1040 }
1041 else {
1042 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1043 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001044 }
1045 return ret;
1046}
1047
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001048/* wake the streams attached to the connection, whose id is greater than <last>,
1049 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1050 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1051 * stream's state is automatically updated accordingly.
1052 */
1053static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1054{
1055 struct eb32_node *node;
1056 struct h2s *h2s;
1057
1058 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1059 flags |= CS_FL_ERROR;
1060
1061 if (conn_xprt_read0_pending(h2c->conn))
1062 flags |= CS_FL_EOS;
1063
1064 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1065 while (node) {
1066 h2s = container_of(node, struct h2s, by_id);
1067 if (h2s->id <= last)
1068 break;
1069 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001070
1071 if (!h2s->cs) {
1072 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001073 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001074 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001075 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001076
1077 h2s->cs->flags |= flags;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001078 h2s->cs->data_cb->wake(h2s->cs);
1079
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001080 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1081 h2s->st = H2_SS_ERROR;
1082 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1083 h2s->st = H2_SS_HREM;
1084 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001085 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001086 }
1087}
1088
Willy Tarreau3421aba2017-07-27 15:41:03 +02001089/* Increase all streams' outgoing window size by the difference passed in
1090 * argument. This is needed upon receipt of the settings frame if the initial
1091 * window size is different. The difference may be negative and the resulting
1092 * window size as well, for the time it takes to receive some window updates.
1093 */
1094static void h2c_update_all_ws(struct h2c *h2c, int diff)
1095{
1096 struct h2s *h2s;
1097 struct eb32_node *node;
1098
1099 if (!diff)
1100 return;
1101
1102 node = eb32_first(&h2c->streams_by_id);
1103 while (node) {
1104 h2s = container_of(node, struct h2s, by_id);
1105 h2s->mws += diff;
1106 node = eb32_next(node);
1107 }
1108}
1109
1110/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1111 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1112 * return an error in h2c. Described in RFC7540#6.5.
1113 */
1114static int h2c_handle_settings(struct h2c *h2c)
1115{
1116 unsigned int offset;
1117 int error;
1118
1119 if (h2c->dff & H2_F_SETTINGS_ACK) {
1120 if (h2c->dfl) {
1121 error = H2_ERR_FRAME_SIZE_ERROR;
1122 goto fail;
1123 }
1124 return 1;
1125 }
1126
1127 if (h2c->dsi != 0) {
1128 error = H2_ERR_PROTOCOL_ERROR;
1129 goto fail;
1130 }
1131
1132 if (h2c->dfl % 6) {
1133 error = H2_ERR_FRAME_SIZE_ERROR;
1134 goto fail;
1135 }
1136
1137 /* that's the limit we can process */
1138 if (h2c->dfl > global.tune.bufsize) {
1139 error = H2_ERR_FRAME_SIZE_ERROR;
1140 goto fail;
1141 }
1142
1143 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001144 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001145 return 0;
1146
1147 /* parse the frame */
1148 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001149 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1150 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001151
1152 switch (type) {
1153 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1154 /* we need to update all existing streams with the
1155 * difference from the previous iws.
1156 */
1157 if (arg < 0) { // RFC7540#6.5.2
1158 error = H2_ERR_FLOW_CONTROL_ERROR;
1159 goto fail;
1160 }
1161 h2c_update_all_ws(h2c, arg - h2c->miw);
1162 h2c->miw = arg;
1163 break;
1164 case H2_SETTINGS_MAX_FRAME_SIZE:
1165 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1166 error = H2_ERR_PROTOCOL_ERROR;
1167 goto fail;
1168 }
1169 h2c->mfs = arg;
1170 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001171 case H2_SETTINGS_ENABLE_PUSH:
1172 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1173 error = H2_ERR_PROTOCOL_ERROR;
1174 goto fail;
1175 }
1176 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001177 }
1178 }
1179
1180 /* need to ACK this frame now */
1181 h2c->st0 = H2_CS_FRAME_A;
1182 return 1;
1183 fail:
1184 h2c_error(h2c, error);
1185 return 0;
1186}
1187
1188/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1189 * success or one of the h2_status values.
1190 */
1191static int h2c_ack_settings(struct h2c *h2c)
1192{
1193 struct buffer *res;
1194 char str[9];
1195 int ret = -1;
1196
1197 if (h2c_mux_busy(h2c, NULL)) {
1198 h2c->flags |= H2_CF_DEM_MBUSY;
1199 return 0;
1200 }
1201
Willy Tarreau44e973f2018-03-01 17:49:30 +01001202 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001203 if (!res) {
1204 h2c->flags |= H2_CF_MUX_MALLOC;
1205 h2c->flags |= H2_CF_DEM_MROOM;
1206 return 0;
1207 }
1208
1209 memcpy(str,
1210 "\x00\x00\x00" /* length : 0 (no data) */
1211 "\x04" "\x01" /* type : 4, flags : ACK */
1212 "\x00\x00\x00\x00" /* stream ID */, 9);
1213
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001214 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001215 if (unlikely(ret <= 0)) {
1216 if (!ret) {
1217 h2c->flags |= H2_CF_MUX_MFULL;
1218 h2c->flags |= H2_CF_DEM_MROOM;
1219 return 0;
1220 }
1221 else {
1222 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1223 return 0;
1224 }
1225 }
1226 return ret;
1227}
1228
Willy Tarreaucf68c782017-10-10 17:11:41 +02001229/* processes a PING frame and schedules an ACK if needed. The caller must pass
1230 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1231 * missing data. It may return an error in h2c.
1232 */
1233static int h2c_handle_ping(struct h2c *h2c)
1234{
1235 /* frame length must be exactly 8 */
1236 if (h2c->dfl != 8) {
1237 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1238 return 0;
1239 }
1240
1241 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001242 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001243 h2c->st0 = H2_CS_FRAME_A;
1244 return 1;
1245}
1246
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001247/* Try to send a window update for stream id <sid> and value <increment>.
1248 * Returns > 0 on success or zero on missing room or failure. It may return an
1249 * error in h2c.
1250 */
1251static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1252{
1253 struct buffer *res;
1254 char str[13];
1255 int ret = -1;
1256
1257 if (h2c_mux_busy(h2c, NULL)) {
1258 h2c->flags |= H2_CF_DEM_MBUSY;
1259 return 0;
1260 }
1261
Willy Tarreau44e973f2018-03-01 17:49:30 +01001262 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001263 if (!res) {
1264 h2c->flags |= H2_CF_MUX_MALLOC;
1265 h2c->flags |= H2_CF_DEM_MROOM;
1266 return 0;
1267 }
1268
1269 /* length: 4, type: 8, flags: none */
1270 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1271 write_n32(str + 5, sid);
1272 write_n32(str + 9, increment);
1273
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001274 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001275
1276 if (unlikely(ret <= 0)) {
1277 if (!ret) {
1278 h2c->flags |= H2_CF_MUX_MFULL;
1279 h2c->flags |= H2_CF_DEM_MROOM;
1280 return 0;
1281 }
1282 else {
1283 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1284 return 0;
1285 }
1286 }
1287 return ret;
1288}
1289
1290/* try to send pending window update for the connection. It's safe to call it
1291 * with no pending updates. Returns > 0 on success or zero on missing room or
1292 * failure. It may return an error in h2c.
1293 */
1294static int h2c_send_conn_wu(struct h2c *h2c)
1295{
1296 int ret = 1;
1297
1298 if (h2c->rcvd_c <= 0)
1299 return 1;
1300
1301 /* send WU for the connection */
1302 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1303 if (ret > 0)
1304 h2c->rcvd_c = 0;
1305
1306 return ret;
1307}
1308
1309/* try to send pending window update for the current dmux stream. It's safe to
1310 * call it with no pending updates. Returns > 0 on success or zero on missing
1311 * room or failure. It may return an error in h2c.
1312 */
1313static int h2c_send_strm_wu(struct h2c *h2c)
1314{
1315 int ret = 1;
1316
1317 if (h2c->rcvd_s <= 0)
1318 return 1;
1319
1320 /* send WU for the stream */
1321 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1322 if (ret > 0)
1323 h2c->rcvd_s = 0;
1324
1325 return ret;
1326}
1327
Willy Tarreaucf68c782017-10-10 17:11:41 +02001328/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1329 * success, 0 on missing data or one of the h2_status values.
1330 */
1331static int h2c_ack_ping(struct h2c *h2c)
1332{
1333 struct buffer *res;
1334 char str[17];
1335 int ret = -1;
1336
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001337 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001338 return 0;
1339
1340 if (h2c_mux_busy(h2c, NULL)) {
1341 h2c->flags |= H2_CF_DEM_MBUSY;
1342 return 0;
1343 }
1344
Willy Tarreau44e973f2018-03-01 17:49:30 +01001345 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001346 if (!res) {
1347 h2c->flags |= H2_CF_MUX_MALLOC;
1348 h2c->flags |= H2_CF_DEM_MROOM;
1349 return 0;
1350 }
1351
1352 memcpy(str,
1353 "\x00\x00\x08" /* length : 8 (same payload) */
1354 "\x06" "\x01" /* type : 6, flags : ACK */
1355 "\x00\x00\x00\x00" /* stream ID */, 9);
1356
1357 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001358 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001359
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001360 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001361 if (unlikely(ret <= 0)) {
1362 if (!ret) {
1363 h2c->flags |= H2_CF_MUX_MFULL;
1364 h2c->flags |= H2_CF_DEM_MROOM;
1365 return 0;
1366 }
1367 else {
1368 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1369 return 0;
1370 }
1371 }
1372 return ret;
1373}
1374
Willy Tarreau26f95952017-07-27 17:18:30 +02001375/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1376 * Returns > 0 on success or zero on missing data. It may return an error in
1377 * h2c or h2s. Described in RFC7540#6.9.
1378 */
1379static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1380{
1381 int32_t inc;
1382 int error;
1383
1384 if (h2c->dfl != 4) {
1385 error = H2_ERR_FRAME_SIZE_ERROR;
1386 goto conn_err;
1387 }
1388
1389 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001390 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001391 return 0;
1392
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001393 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001394
1395 if (h2c->dsi != 0) {
1396 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001397
1398 /* it's not an error to receive WU on a closed stream */
1399 if (h2s->st == H2_SS_CLOSED)
1400 return 1;
1401
1402 if (!inc) {
1403 error = H2_ERR_PROTOCOL_ERROR;
1404 goto strm_err;
1405 }
1406
1407 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1408 error = H2_ERR_FLOW_CONTROL_ERROR;
1409 goto strm_err;
1410 }
1411
1412 h2s->mws += inc;
1413 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1414 h2s->flags &= ~H2_SF_BLK_SFCTL;
1415 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1416 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1417 /* This stream wanted to send but could not due to its
1418 * own flow control. We can put it back into the send
1419 * list now, it will be handled upon next send() call.
1420 */
1421 LIST_ADDQ(&h2c->send_list, &h2s->list);
1422 }
1423 }
1424 }
1425 else {
1426 /* connection window update */
1427 if (!inc) {
1428 error = H2_ERR_PROTOCOL_ERROR;
1429 goto conn_err;
1430 }
1431
1432 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1433 error = H2_ERR_FLOW_CONTROL_ERROR;
1434 goto conn_err;
1435 }
1436
1437 h2c->mws += inc;
1438 }
1439
1440 return 1;
1441
1442 conn_err:
1443 h2c_error(h2c, error);
1444 return 0;
1445
1446 strm_err:
1447 if (h2s) {
1448 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001449 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001450 }
1451 else
1452 h2c_error(h2c, error);
1453 return 0;
1454}
1455
Willy Tarreaue96b0922017-10-30 00:28:29 +01001456/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1457 * the last ID. Returns > 0 on success or zero on missing data. It may return
1458 * an error in h2c. Described in RFC7540#6.8.
1459 */
1460static int h2c_handle_goaway(struct h2c *h2c)
1461{
1462 int error;
1463 int last;
1464
1465 if (h2c->dsi != 0) {
1466 error = H2_ERR_PROTOCOL_ERROR;
1467 goto conn_err;
1468 }
1469
1470 if (h2c->dfl < 8) {
1471 error = H2_ERR_FRAME_SIZE_ERROR;
1472 goto conn_err;
1473 }
1474
1475 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001476 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001477 return 0;
1478
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001479 last = h2_get_n32(&h2c->dbuf, 0);
1480 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001481 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001482 if (h2c->last_sid < 0)
1483 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001484 return 1;
1485
1486 conn_err:
1487 h2c_error(h2c, error);
1488 return 0;
1489}
1490
Willy Tarreau92153fc2017-12-03 19:46:19 +01001491/* processes a PRIORITY frame, and either skips it or rejects if it is
1492 * invalid. Returns > 0 on success or zero on missing data. It may return
1493 * an error in h2c. Described in RFC7540#6.3.
1494 */
1495static int h2c_handle_priority(struct h2c *h2c)
1496{
1497 int error;
1498
1499 if (h2c->dsi == 0) {
1500 error = H2_ERR_PROTOCOL_ERROR;
1501 goto conn_err;
1502 }
1503
1504 if (h2c->dfl != 5) {
1505 error = H2_ERR_FRAME_SIZE_ERROR;
1506 goto conn_err;
1507 }
1508
1509 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001510 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001511 return 0;
1512
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001513 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001514 /* 7540#5.3 : can't depend on itself */
1515 error = H2_ERR_PROTOCOL_ERROR;
1516 goto conn_err;
1517 }
1518 return 1;
1519
1520 conn_err:
1521 h2c_error(h2c, error);
1522 return 0;
1523}
1524
Willy Tarreaucd234e92017-08-18 10:59:39 +02001525/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1526 * Returns > 0 on success or zero on missing data. It may return an error in
1527 * h2c. Described in RFC7540#6.4.
1528 */
1529static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1530{
1531 int error;
1532
1533 if (h2c->dsi == 0) {
1534 error = H2_ERR_PROTOCOL_ERROR;
1535 goto conn_err;
1536 }
1537
Willy Tarreaucd234e92017-08-18 10:59:39 +02001538 if (h2c->dfl != 4) {
1539 error = H2_ERR_FRAME_SIZE_ERROR;
1540 goto conn_err;
1541 }
1542
1543 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001544 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001545 return 0;
1546
1547 /* late RST, already handled */
1548 if (h2s->st == H2_SS_CLOSED)
1549 return 1;
1550
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001551 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001552 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001553
1554 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001555 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001556 h2s->cs->data_cb->wake(h2s->cs);
1557 }
1558
1559 h2s->flags |= H2_SF_RST_RCVD;
1560 return 1;
1561
1562 conn_err:
1563 h2c_error(h2c, error);
1564 return 0;
1565}
1566
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001567/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1568 * It may return an error in h2c or h2s. The caller must consider that the
1569 * return value is the new h2s in case one was allocated (most common case).
1570 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001571 * errors here are reported as connection errors since it's impossible to
1572 * recover from such errors after the compression context has been altered.
1573 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001574static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001575{
1576 int error;
1577
1578 if (!h2c->dfl) {
1579 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1580 goto strm_err;
1581 }
1582
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001583 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001584 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001585
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001586 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001587 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001588
Willy Tarreauf2101912018-07-19 10:11:38 +02001589 if (h2c->flags & H2_CF_DEM_TOOMANY)
1590 return 0; // too many cs still present
1591
Willy Tarreau13278b42017-10-13 19:23:14 +02001592 /* now either the frame is complete or the buffer is complete */
1593 if (h2s->st != H2_SS_IDLE) {
1594 /* FIXME: stream already exists, this is only allowed for
1595 * trailers (not supported for now).
1596 */
1597 error = H2_ERR_PROTOCOL_ERROR;
1598 goto conn_err;
1599 }
1600 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1601 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1602 error = H2_ERR_PROTOCOL_ERROR;
1603 goto conn_err;
1604 }
1605
1606 h2s = h2c_stream_new(h2c, h2c->dsi);
1607 if (!h2s) {
1608 error = H2_ERR_INTERNAL_ERROR;
1609 goto conn_err;
1610 }
1611
1612 h2s->st = H2_SS_OPEN;
1613 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1614 h2s->st = H2_SS_HREM;
1615 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001616 /* note: cs cannot be null for now (just created above) */
1617 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001618 }
1619
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001620 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001621 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001622
Willy Tarreau8f650c32017-11-21 19:36:21 +01001623 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001624 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001625
Willy Tarreau721c9742017-11-07 11:05:42 +01001626 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001627 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001628 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001629 }
1630 else {
1631 /* update the max stream ID if the request is being processed */
1632 if (h2s->id > h2c->max_id)
1633 h2c->max_id = h2s->id;
1634 }
1635
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001636 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001637
1638 conn_err:
1639 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001640 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001641
1642 strm_err:
1643 if (h2s) {
1644 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001645 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001646 }
1647 else
1648 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001649 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001650}
1651
Willy Tarreau454f9052017-10-26 19:40:35 +02001652/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1653 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1654 */
1655static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1656{
1657 int error;
1658
1659 /* note that empty DATA frames are perfectly valid and sometimes used
1660 * to signal an end of stream (with the ES flag).
1661 */
1662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001664 return 0; // empty buffer
1665
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001666 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001667 return 0; // incomplete frame
1668
1669 /* now either the frame is complete or the buffer is complete */
1670
1671 if (!h2c->dsi) {
1672 /* RFC7540#6.1 */
1673 error = H2_ERR_PROTOCOL_ERROR;
1674 goto conn_err;
1675 }
1676
1677 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1678 /* RFC7540#6.1 */
1679 error = H2_ERR_STREAM_CLOSED;
1680 goto strm_err;
1681 }
1682
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001683 if (!h2_frt_transfer_data(h2s))
1684 return 0;
1685
Willy Tarreau454f9052017-10-26 19:40:35 +02001686 /* call the upper layers to process the frame, then let the upper layer
1687 * notify the stream about any change.
1688 */
1689 if (!h2s->cs) {
1690 error = H2_ERR_STREAM_CLOSED;
1691 goto strm_err;
1692 }
1693
Willy Tarreau8f650c32017-11-21 19:36:21 +01001694 if (h2c->st0 >= H2_CS_ERROR)
1695 return 0;
1696
Willy Tarreau721c9742017-11-07 11:05:42 +01001697 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001698 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001699 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001700 }
1701
1702 /* check for completion : the callee will change this to FRAME_A or
1703 * FRAME_H once done.
1704 */
1705 if (h2c->st0 == H2_CS_FRAME_P)
1706 return 0;
1707
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001708
1709 /* last frame */
1710 if (h2c->dff & H2_F_DATA_END_STREAM) {
1711 h2s->st = H2_SS_HREM;
1712 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001713 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001714 }
1715
Willy Tarreau454f9052017-10-26 19:40:35 +02001716 return 1;
1717
1718 conn_err:
1719 h2c_error(h2c, error);
1720 return 0;
1721
1722 strm_err:
1723 if (h2s) {
1724 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001725 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001726 }
1727 else
1728 h2c_error(h2c, error);
1729 return 0;
1730}
1731
Willy Tarreaubc933932017-10-09 16:21:43 +02001732/* process Rx frames to be demultiplexed */
1733static void h2_process_demux(struct h2c *h2c)
1734{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001735 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001736
Willy Tarreau081d4722017-05-16 21:51:05 +02001737 if (h2c->st0 >= H2_CS_ERROR)
1738 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001739
1740 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1741 if (h2c->st0 == H2_CS_PREFACE) {
1742 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1743 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1744 if (h2c->st0 == H2_CS_ERROR)
1745 h2c->st0 = H2_CS_ERROR2;
1746 goto fail;
1747 }
1748
1749 h2c->max_id = 0;
1750 h2c->st0 = H2_CS_SETTINGS1;
1751 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001752
1753 if (h2c->st0 == H2_CS_SETTINGS1) {
1754 struct h2_fh hdr;
1755
1756 /* ensure that what is pending is a valid SETTINGS frame
1757 * without an ACK.
1758 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001759 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001760 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1761 if (h2c->st0 == H2_CS_ERROR)
1762 h2c->st0 = H2_CS_ERROR2;
1763 goto fail;
1764 }
1765
1766 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1767 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1768 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1769 h2c->st0 = H2_CS_ERROR2;
1770 goto fail;
1771 }
1772
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001773 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001774 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1775 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1776 h2c->st0 = H2_CS_ERROR2;
1777 goto fail;
1778 }
1779
1780 /* that's OK, switch to FRAME_P to process it */
1781 h2c->dfl = hdr.len;
1782 h2c->dsi = hdr.sid;
1783 h2c->dft = hdr.ft;
1784 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001785 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001786 h2c->st0 = H2_CS_FRAME_P;
1787 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001788 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001789
1790 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001791 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001792 int ret = 0;
1793
1794 if (h2c->st0 >= H2_CS_ERROR)
1795 break;
1796
1797 if (h2c->st0 == H2_CS_FRAME_H) {
1798 struct h2_fh hdr;
1799
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001800 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001801 break;
1802
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001803 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001804 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1805 h2c->st0 = H2_CS_ERROR;
1806 break;
1807 }
1808
1809 h2c->dfl = hdr.len;
1810 h2c->dsi = hdr.sid;
1811 h2c->dft = hdr.ft;
1812 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001813 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001814 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001815 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001816 }
1817
1818 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001819 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1820
Olivier Houchard638b7992018-08-16 15:41:52 +02001821 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001822 /* we may have to signal the upper layers */
1823 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001824 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1825 /* cs has just been destroyed, we have to kill h2s. */
1826 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1827 goto strm_err;
1828 }
1829
1830 if (h2c->st0 >= H2_CS_ERROR)
1831 goto strm_err;
1832
1833 if (h2s->st >= H2_SS_ERROR) {
1834 /* stream error : send RST_STREAM */
1835 h2c->st0 = H2_CS_FRAME_E;
1836 }
1837 }
1838 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001839
Willy Tarreaud7901432017-12-29 11:34:40 +01001840 if (h2c->st0 == H2_CS_FRAME_E)
1841 goto strm_err;
1842
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001843 if (h2s->st == H2_SS_IDLE &&
1844 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1845 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1846 * this state MUST be treated as a connection error
1847 */
1848 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1849 h2c->st0 = H2_CS_ERROR;
1850 break;
1851 }
1852
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001853 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1854 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1855 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1856 * this state MUST be treated as a stream error
1857 */
1858 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1859 goto strm_err;
1860 }
1861
Willy Tarreauab837502017-12-27 15:07:30 +01001862 /* Below the management of frames received in closed state is a
1863 * bit hackish because the spec makes strong differences between
1864 * streams closed by receiving RST, sending RST, and seeing ES
1865 * in both directions. In addition to this, the creation of a
1866 * new stream reusing the identifier of a closed one will be
1867 * detected here. Given that we cannot keep track of all closed
1868 * streams forever, we consider that unknown closed streams were
1869 * closed on RST received, which allows us to respond with an
1870 * RST without breaking the connection (eg: to abort a transfer).
1871 * Some frames have to be silently ignored as well.
1872 */
1873 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1874 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1875 /* #5.1.1: The identifier of a newly
1876 * established stream MUST be numerically
1877 * greater than all streams that the initiating
1878 * endpoint has opened or reserved. This
1879 * governs streams that are opened using a
1880 * HEADERS frame and streams that are reserved
1881 * using PUSH_PROMISE. An endpoint that
1882 * receives an unexpected stream identifier
1883 * MUST respond with a connection error.
1884 */
1885 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1886 goto strm_err;
1887 }
1888
1889 if (h2s->flags & H2_SF_RST_RCVD) {
1890 /* RFC7540#5.1:closed: an endpoint that
1891 * receives any frame other than PRIORITY after
1892 * receiving a RST_STREAM MUST treat that as a
1893 * stream error of type STREAM_CLOSED.
1894 *
1895 * Note that old streams fall into this category
1896 * and will lead to an RST being sent.
1897 */
1898 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1899 h2c->st0 = H2_CS_FRAME_E;
1900 goto strm_err;
1901 }
1902
1903 /* RFC7540#5.1:closed: if this state is reached as a
1904 * result of sending a RST_STREAM frame, the peer that
1905 * receives the RST_STREAM might have already sent
1906 * frames on the stream that cannot be withdrawn. An
1907 * endpoint MUST ignore frames that it receives on
1908 * closed streams after it has sent a RST_STREAM
1909 * frame. An endpoint MAY choose to limit the period
1910 * over which it ignores frames and treat frames that
1911 * arrive after this time as being in error.
1912 */
1913 if (!(h2s->flags & H2_SF_RST_SENT)) {
1914 /* RFC7540#5.1:closed: any frame other than
1915 * PRIO/WU/RST in this state MUST be treated as
1916 * a connection error
1917 */
1918 if (h2c->dft != H2_FT_RST_STREAM &&
1919 h2c->dft != H2_FT_PRIORITY &&
1920 h2c->dft != H2_FT_WINDOW_UPDATE) {
1921 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1922 goto strm_err;
1923 }
1924 }
1925 }
1926
Willy Tarreauc0da1962017-10-30 18:38:00 +01001927#if 0
1928 // problem below: it is not possible to completely ignore such
1929 // streams as we need to maintain the compression state as well
1930 // and for this we need to completely process these frames (eg:
1931 // HEADERS frames) as well as counting DATA frames to emit
1932 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1933 // This is a typical case of layer violation where the
1934 // transported contents are critical to the connection's
1935 // validity and must be ignored at the same time :-(
1936
1937 /* graceful shutdown, ignore streams whose ID is higher than
1938 * the one advertised in GOAWAY. RFC7540#6.8.
1939 */
1940 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001941 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
1942 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001943 h2c->dfl -= ret;
1944 ret = h2c->dfl == 0;
1945 goto strm_err;
1946 }
1947#endif
1948
Willy Tarreau7e98c052017-10-10 15:56:59 +02001949 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001950 case H2_FT_SETTINGS:
1951 if (h2c->st0 == H2_CS_FRAME_P)
1952 ret = h2c_handle_settings(h2c);
1953
1954 if (h2c->st0 == H2_CS_FRAME_A)
1955 ret = h2c_ack_settings(h2c);
1956 break;
1957
Willy Tarreaucf68c782017-10-10 17:11:41 +02001958 case H2_FT_PING:
1959 if (h2c->st0 == H2_CS_FRAME_P)
1960 ret = h2c_handle_ping(h2c);
1961
1962 if (h2c->st0 == H2_CS_FRAME_A)
1963 ret = h2c_ack_ping(h2c);
1964 break;
1965
Willy Tarreau26f95952017-07-27 17:18:30 +02001966 case H2_FT_WINDOW_UPDATE:
1967 if (h2c->st0 == H2_CS_FRAME_P)
1968 ret = h2c_handle_window_update(h2c, h2s);
1969 break;
1970
Willy Tarreau61290ec2017-10-17 08:19:21 +02001971 case H2_FT_CONTINUATION:
1972 /* we currently don't support CONTINUATION frames since
1973 * we have nowhere to store the partial HEADERS frame.
1974 * Let's abort the stream on an INTERNAL_ERROR here.
1975 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001976 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001977 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001978 h2c->st0 = H2_CS_FRAME_E;
1979 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001980 break;
1981
Willy Tarreau13278b42017-10-13 19:23:14 +02001982 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001983 if (h2c->st0 == H2_CS_FRAME_P) {
1984 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
1985 if (tmp_h2s) {
1986 h2s = tmp_h2s;
1987 ret = 1;
1988 }
1989 }
Willy Tarreau13278b42017-10-13 19:23:14 +02001990 break;
1991
Willy Tarreau454f9052017-10-26 19:40:35 +02001992 case H2_FT_DATA:
1993 if (h2c->st0 == H2_CS_FRAME_P)
1994 ret = h2c_frt_handle_data(h2c, h2s);
1995
1996 if (h2c->st0 == H2_CS_FRAME_A)
1997 ret = h2c_send_strm_wu(h2c);
1998 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001999
Willy Tarreau92153fc2017-12-03 19:46:19 +01002000 case H2_FT_PRIORITY:
2001 if (h2c->st0 == H2_CS_FRAME_P)
2002 ret = h2c_handle_priority(h2c);
2003 break;
2004
Willy Tarreaucd234e92017-08-18 10:59:39 +02002005 case H2_FT_RST_STREAM:
2006 if (h2c->st0 == H2_CS_FRAME_P)
2007 ret = h2c_handle_rst_stream(h2c, h2s);
2008 break;
2009
Willy Tarreaue96b0922017-10-30 00:28:29 +01002010 case H2_FT_GOAWAY:
2011 if (h2c->st0 == H2_CS_FRAME_P)
2012 ret = h2c_handle_goaway(h2c);
2013 break;
2014
Willy Tarreau1c661982017-10-30 13:52:01 +01002015 case H2_FT_PUSH_PROMISE:
2016 /* not permitted here, RFC7540#5.1 */
2017 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01002018 break;
2019
2020 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002021 default:
2022 /* drop frames that we ignore. They may be larger than
2023 * the buffer so we drain all of their contents until
2024 * we reach the end.
2025 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002026 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2027 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002028 h2c->dfl -= ret;
2029 ret = h2c->dfl == 0;
2030 }
2031
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002032 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002033 /* We may have to send an RST if not done yet */
2034 if (h2s->st == H2_SS_ERROR)
2035 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002036
Willy Tarreaua20a5192017-12-27 11:02:06 +01002037 if (h2c->st0 == H2_CS_FRAME_E)
2038 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002039
Willy Tarreau7e98c052017-10-10 15:56:59 +02002040 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002041 if (ret <= 0) {
2042 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002043 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002044 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002045
2046 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002047 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002048 h2c->st0 = H2_CS_FRAME_H;
2049 }
2050 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002051
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002052 if (h2c->rcvd_c > 0 &&
2053 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2054 h2c_send_conn_wu(h2c);
2055
Willy Tarreau52eed752017-09-22 15:05:09 +02002056 fail:
2057 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002058 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002059 /* we may have to signal the upper layers */
2060 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002061 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);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02002293 sw->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02002294 tasklet_wakeup(sw->task);
2295 }
2296
2297 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002298 /* We're done, no more to send */
2299 if (!b_data(&h2c->mbuf))
2300 return NULL;
2301schedule:
2302 if (LIST_ISEMPTY(&h2c->wait_list.list))
2303 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
2304 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002305}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002306
Willy Tarreau62f52692017-10-08 23:01:42 +02002307/* callback called on any event by the connection handler.
2308 * It applies changes and returns zero, or < 0 if it wants immediate
2309 * destruction of the connection (which normally doesn not happen in h2).
2310 */
2311static int h2_wake(struct connection *conn)
2312{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002313 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002314 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002315
Olivier Houchardf495fc42018-07-20 18:15:23 +02002316 h2_send(NULL, conn, 0);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002317 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002318 h2_process_demux(h2c);
2319
2320 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002321 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002322
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002323 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002324 h2c->flags &= ~H2_CF_DEM_DFULL;
2325 }
2326
Willy Tarreau8ec14062017-12-30 18:08:13 +01002327 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2328 /* frontend is stopping, reload likely in progress, let's try
2329 * to announce a graceful shutdown if not yet done. We don't
2330 * care if it fails, it will be tried again later.
2331 */
2332 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2333 if (h2c->last_sid < 0)
2334 h2c->last_sid = (1U << 31) - 1;
2335 h2c_send_goaway_error(h2c, NULL);
2336 }
2337 }
2338
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002339 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002340 * If we received early data, and the handshake is done, wake
2341 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002342 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002343 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2344 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2345 struct eb32_node *node;
2346 struct h2s *h2s;
2347
2348 h2c->flags |= H2_CF_WAIT_FOR_HS;
2349 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2350
2351 while (node) {
2352 h2s = container_of(node, struct h2s, by_id);
2353 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2354 h2s->cs->data_cb->wake(h2s->cs);
2355 node = eb32_next(node);
2356 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002357 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002358
Willy Tarreau26bd7612017-10-09 16:47:04 +02002359 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002360 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2361 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2362 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002363 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002364
2365 if (eb_is_empty(&h2c->streams_by_id)) {
2366 /* no more stream, kill the connection now */
2367 h2_release(conn);
2368 return -1;
2369 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002370 }
2371
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002372 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002373 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002374
2375 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002376 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002377 __conn_xprt_stop_recv(conn);
2378 }
2379 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002380 __conn_xprt_want_recv(conn);
2381 }
2382
2383 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002384 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002385 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002386 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002387 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002388 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2389 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002390 __conn_xprt_want_send(conn);
2391 }
2392 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002393 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002394 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002395 }
2396
Willy Tarreau3f133572017-10-31 19:21:06 +01002397 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002398 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002399 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002400 task_queue(h2c->task);
2401 }
2402 else
2403 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002404 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002405
Willy Tarreau62f52692017-10-08 23:01:42 +02002406 return 0;
2407}
2408
Willy Tarreauea392822017-10-31 10:02:25 +01002409/* Connection timeout management. The principle is that if there's no receipt
2410 * nor sending for a certain amount of time, the connection is closed. If the
2411 * MUX buffer still has lying data or is not allocatable, the connection is
2412 * immediately killed. If it's allocatable and empty, we attempt to send a
2413 * GOAWAY frame.
2414 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002415static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002416{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002417 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002418 int expired = tick_is_expired(t->expire, now_ms);
2419
Willy Tarreau0975f112018-03-29 15:22:59 +02002420 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002421 return t;
2422
Willy Tarreau0975f112018-03-29 15:22:59 +02002423 task_delete(t);
2424 task_free(t);
2425
2426 if (!h2c) {
2427 /* resources were already deleted */
2428 return NULL;
2429 }
2430
2431 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002432 h2c_error(h2c, H2_ERR_NO_ERROR);
2433 h2_wake_some_streams(h2c, 0, 0);
2434
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002435 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002436 /* don't even try to send a GOAWAY, the buffer is stuck */
2437 h2c->flags |= H2_CF_GOAWAY_FAILED;
2438 }
2439
2440 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002441 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002442 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2443 h2c->flags |= H2_CF_GOAWAY_FAILED;
2444
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002445 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2446 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002447 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002448 b_del(&h2c->mbuf, ret);
2449 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002450 }
2451 }
Willy Tarreauea392822017-10-31 10:02:25 +01002452
Willy Tarreau0975f112018-03-29 15:22:59 +02002453 /* either we can release everything now or it will be done later once
2454 * the last stream closes.
2455 */
2456 if (eb_is_empty(&h2c->streams_by_id))
2457 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002458
Willy Tarreauea392822017-10-31 10:02:25 +01002459 return NULL;
2460}
2461
2462
Willy Tarreau62f52692017-10-08 23:01:42 +02002463/*******************************************/
2464/* functions below are used by the streams */
2465/*******************************************/
2466
2467/*
2468 * Attach a new stream to a connection
2469 * (Used for outgoing connections)
2470 */
2471static struct conn_stream *h2_attach(struct connection *conn)
2472{
2473 return NULL;
2474}
2475
2476/* callback used to update the mux's polling flags after changing a cs' status.
2477 * The caller (cs_update_mux_polling) will take care of propagating any changes
2478 * to the transport layer.
2479 */
2480static void h2_update_poll(struct conn_stream *cs)
2481{
Willy Tarreau1d393222017-10-17 10:26:19 +02002482 struct h2s *h2s = cs->ctx;
2483
2484 if (!h2s)
2485 return;
2486
Willy Tarreaud7739c82017-10-30 15:38:23 +01002487 /* we may unblock a blocked read */
2488
Willy Tarreau315d8072017-12-10 22:17:57 +01002489 if (cs->flags & CS_FL_DATA_RD_ENA) {
2490 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002491 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002492 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002493 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002494 conn_xprt_want_send(cs->conn);
2495 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002496 }
2497
Willy Tarreau1d393222017-10-17 10:26:19 +02002498 /* Note: the stream and stream-int code doesn't allow us to perform a
2499 * synchronous send() here unfortunately, because this code is called
2500 * as si_update() from the process_stream() context. This means that
2501 * we have to queue the current cs and defer its processing after the
2502 * connection's cs list is processed anyway.
2503 */
2504
2505 if (cs->flags & CS_FL_DATA_WR_ENA) {
2506 if (LIST_ISEMPTY(&h2s->list)) {
2507 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002508 !b_data(&h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002509 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2510 conn_xprt_want_send(cs->conn);
2511 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2512 }
2513 }
2514 else if (!LIST_ISEMPTY(&h2s->list)) {
2515 LIST_DEL(&h2s->list);
2516 LIST_INIT(&h2s->list);
2517 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2518 }
2519
2520 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002521 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002522 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002523}
2524
2525/*
2526 * Detach the stream from the connection and possibly release the connection.
2527 */
2528static void h2_detach(struct conn_stream *cs)
2529{
Willy Tarreau60935142017-10-16 18:11:19 +02002530 struct h2s *h2s = cs->ctx;
2531 struct h2c *h2c;
2532
2533 cs->ctx = NULL;
2534 if (!h2s)
2535 return;
2536
2537 h2c = h2s->h2c;
2538 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002539 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002540 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2541 !h2_has_too_many_cs(h2c)) {
2542 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2543 if (h2_recv_allowed(h2c)) {
2544 __conn_xprt_want_recv(h2c->conn);
2545 conn_xprt_want_send(h2c->conn);
2546 }
2547 }
Willy Tarreau60935142017-10-16 18:11:19 +02002548
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002549 /* this stream may be blocked waiting for some data to leave (possibly
2550 * an ES or RST frame), so orphan it in this case.
2551 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002552 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002553 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002554 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002555 return;
2556
Willy Tarreau45f752e2017-10-30 15:44:59 +01002557 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2558 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2559 /* unblock the connection if it was blocked on this
2560 * stream.
2561 */
2562 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2563 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2564 conn_xprt_want_recv(cs->conn);
2565 conn_xprt_want_send(cs->conn);
2566 }
2567
Willy Tarreau71049cc2018-03-28 13:56:39 +02002568 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002569
Willy Tarreaue323f342018-03-28 13:51:45 +02002570 /* We don't want to close right now unless we're removing the
2571 * last stream, and either the connection is in error, or it
2572 * reached the ID already specified in a GOAWAY frame received
2573 * or sent (as seen by last_sid >= 0).
2574 */
2575 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2576 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002577 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002578 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002579 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002580 (conn_xprt_read0_pending(h2c->conn) ||
2581 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2582 /* no more stream will come, kill it now */
2583 h2_release(h2c->conn);
2584 }
2585 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002586 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002587 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2588 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002589 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002590 else
2591 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002592 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002593}
2594
2595static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2596{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002597 struct h2s *h2s = cs->ctx;
2598
2599 if (!mode)
2600 return;
2601
Willy Tarreau721c9742017-11-07 11:05:42 +01002602 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002603 return;
2604
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002605 /* if no outgoing data was seen on this stream, it means it was
2606 * closed with a "tcp-request content" rule that is normally
2607 * used to kill the connection ASAP (eg: limit abuse). In this
2608 * case we send a goaway to close the connection.
2609 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002610 if (!(h2s->flags & H2_SF_RST_SENT) &&
2611 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002612 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002613
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002614 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2615 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2616 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002617 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002618
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002619 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002620 conn_xprt_want_send(cs->conn);
2621
Willy Tarreau00dd0782018-03-01 16:31:34 +01002622 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002623
2624 add_to_list:
2625 if (LIST_ISEMPTY(&h2s->list)) {
2626 if (h2s->flags & H2_SF_BLK_MFCTL)
2627 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2628 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2629 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2630 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002631}
2632
2633static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2634{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002635 struct h2s *h2s = cs->ctx;
2636
Willy Tarreau721c9742017-11-07 11:05:42 +01002637 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002638 return;
2639
Willy Tarreau67434202017-11-06 20:20:51 +01002640 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002641 /* we can cleanly close using an empty data frame only after headers */
2642
2643 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2644 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002645 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002646
2647 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002648 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002649 else
2650 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002651 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002652 /* if no outgoing data was seen on this stream, it means it was
2653 * closed with a "tcp-request content" rule that is normally
2654 * used to kill the connection ASAP (eg: limit abuse). In this
2655 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002656 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002657 if (!(h2s->flags & H2_SF_RST_SENT) &&
2658 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002659 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002660
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002661 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2662 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002663 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002664 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002665
Willy Tarreau00dd0782018-03-01 16:31:34 +01002666 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002667 }
2668
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002669 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002670 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002671
2672 add_to_list:
2673 if (LIST_ISEMPTY(&h2s->list)) {
2674 if (h2s->flags & H2_SF_BLK_MFCTL)
2675 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2676 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2677 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2678 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002679}
2680
Willy Tarreau13278b42017-10-13 19:23:14 +02002681/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2682 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2683 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002684 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002685 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002686static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002687{
2688 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002689 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002690 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002691 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002692 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002693 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002694 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002695 int flen = h2c->dfl;
2696 int outlen = 0;
2697 int wrap;
2698 int try;
2699
2700 if (!h2c->dfl) {
2701 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002702 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002703 return 0;
2704 }
2705
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002706 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002707 return 0; // incomplete input frame
2708
Willy Tarreau13278b42017-10-13 19:23:14 +02002709 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002710 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002711 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002712 copy = alloc_trash_chunk();
2713 if (!copy) {
2714 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2715 goto fail;
2716 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2718 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2719 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002720 }
2721
2722 /* The padlen is the first byte before data, and the padding appears
2723 * after data. padlen+data+padding are included in flen.
2724 */
2725 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002726 h2c->dpl = *hdrs;
2727 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002728 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2729 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002730 return 0;
2731 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002732 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002733 hdrs += 1; // skip Pad Length
2734 }
2735
2736 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2737 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002738 if (read_n32(hdrs) == h2s->id) {
2739 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2740 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2741 return 0;//goto fail_stream;
2742 }
2743
Willy Tarreau13278b42017-10-13 19:23:14 +02002744 hdrs += 5; // stream dep = 4, weight = 1
2745 flen -= 5;
2746 }
2747
2748 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2749 * don't support this for now and can't even decompress so we have to
2750 * break the connection.
2751 */
2752 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2753 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002754 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002755 }
2756
Olivier Houchard638b7992018-08-16 15:41:52 +02002757 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002758 if (!csbuf) {
2759 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002760 goto fail;
2761 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002762
Willy Tarreau937f7602018-02-26 15:22:17 +01002763 /* we can't retry a failed decompression operation so we must be very
2764 * careful not to take any risks. In practice the output buffer is
2765 * always empty except maybe for trailers, in which case we simply have
2766 * to wait for the upper layer to finish consuming what is available.
2767 */
2768 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002769 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002770
Willy Tarreau937f7602018-02-26 15:22:17 +01002771 csbuf->head = 0;
2772 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002773
2774 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2775 sizeof(list)/sizeof(list[0]), tmp);
2776 if (outlen < 0) {
2777 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2778 goto fail;
2779 }
2780
2781 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002782 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002783 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002784
2785 if (outlen < 0) {
2786 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2787 goto fail;
2788 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002789
Willy Tarreau174b06a2018-04-25 18:13:58 +02002790 if (msgf & H2_MSGF_BODY) {
2791 /* a payload is present */
2792 if (msgf & H2_MSGF_BODY_CL)
2793 h2s->flags |= H2_SF_DATA_CLEN;
2794 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2795 h2s->flags |= H2_SF_DATA_CHNK;
2796 }
2797
Willy Tarreau13278b42017-10-13 19:23:14 +02002798 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002799 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002800 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002801 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002802
Willy Tarreau39d68502018-03-02 12:26:37 +01002803 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002804 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002805 h2s->cs->flags |= CS_FL_REOS;
2806 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002807
Willy Tarreau68dd9852017-07-03 14:44:26 +02002808 leave:
2809 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002810 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002811 fail:
2812 outlen = 0;
2813 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002814}
2815
Willy Tarreau454f9052017-10-26 19:40:35 +02002816/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2817 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2818 * in use, a new chunk is emitted for each frame. This is supposed to fit
2819 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2820 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2821 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2822 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002823 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2824 * checked to know if some data remain pending (an empty DATA frame can return
2825 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2826 * connection errors in h2c->errcode. The caller must already have checked the
2827 * frame header and ensured that the frame was complete or the buffer full. It
2828 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002829 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002830static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002831{
2832 struct h2c *h2c = h2s->h2c;
2833 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002834 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002835 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002836 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002837
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002838 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002839
2840 /* The padlen is the first byte before data, and the padding appears
2841 * after data. padlen+data+padding are included in flen.
2842 */
Willy Tarreau79127812017-12-03 21:06:59 +01002843 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002844 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002845 return 0;
2846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002848 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002849 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2850 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002851 return 0;
2852 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002853
2854 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002855 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002856 h2c->dfl--;
2857 h2c->rcvd_c++; h2c->rcvd_s++;
2858 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002859 }
2860
Olivier Houchard638b7992018-08-16 15:41:52 +02002861 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002862 if (!csbuf) {
2863 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002864 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002865 }
2866
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002867 flen = h2c->dfl - h2c->dpl;
2868 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002869 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002870
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871 if (flen > b_data(&h2c->dbuf)) {
2872 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002873 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002874 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002875 }
2876
2877 if (unlikely(b_space_wraps(csbuf))) {
2878 /* it doesn't fit and the buffer is fragmented,
2879 * so let's defragment it and try again.
2880 */
2881 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002882 }
2883
Willy Tarreaueba10f22018-04-25 20:44:22 +02002884 /* chunked-encoding requires more room */
2885 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002886 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002887 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2888 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2889 (chklen < 1048576) ? 4 : 8;
2890 chklen += 4; // CRLF, CRLF
2891 }
2892
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002893 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002894 if (flen + chklen > b_room(csbuf)) {
2895 if (chklen >= b_room(csbuf)) {
2896 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002897 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002898 }
2899 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002900 }
2901
2902 if (h2s->flags & H2_SF_DATA_CHNK) {
2903 /* emit the chunk size */
2904 unsigned int chksz = flen;
2905 char str[10];
2906 char *beg;
2907
2908 beg = str + sizeof(str);
2909 *--beg = '\n';
2910 *--beg = '\r';
2911 do {
2912 *--beg = hextab[chksz & 0xF];
2913 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002914 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002915 }
2916
Willy Tarreau454f9052017-10-26 19:40:35 +02002917 /* Block1 is the length of the first block before the buffer wraps,
2918 * block2 is the optional second block to reach the end of the frame.
2919 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002920 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002921 if (block1 > flen)
2922 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002923 block2 = flen - block1;
2924
2925 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002926 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002927
2928 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01002929 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002930
Willy Tarreaueba10f22018-04-25 20:44:22 +02002931 if (h2s->flags & H2_SF_DATA_CHNK) {
2932 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002933 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002934 }
2935
Willy Tarreau454f9052017-10-26 19:40:35 +02002936 /* now mark the input data as consumed (will be deleted from the buffer
2937 * by the caller when seeing FRAME_A after sending the window update).
2938 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002940 h2c->dfl -= flen;
2941 h2c->rcvd_c += flen;
2942 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2943
2944 if (h2c->dfl > h2c->dpl) {
2945 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002946 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002947 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002948 }
2949
Willy Tarreau4a28da12018-01-04 14:41:00 +01002950 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002951 /* here we're done with the frame, all the payload (except padding) was
2952 * transferred.
2953 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002954
2955 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2956 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002957 if (b_room(csbuf) < 5) {
2958 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002959 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002960 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02002961 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002962 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02002963 }
2964
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002965 h2c->rcvd_c += h2c->dpl;
2966 h2c->rcvd_s += h2c->dpl;
2967 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002968 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2969
Willy Tarreau39d68502018-03-02 12:26:37 +01002970 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002971 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002972 h2s->cs->flags |= CS_FL_REOS;
2973 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01002974
Willy Tarreau454b57b2018-02-26 15:50:05 +01002975 return flen + chklen;
2976 fail:
2977 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002978}
2979
Willy Tarreau5dd17352018-06-14 13:33:30 +02002980/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2981 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2982 * number of bytes sent. The caller must check the stream's status to detect
2983 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002984 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002985static 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 +02002986{
2987 struct http_hdr list[MAX_HTTP_HDR];
2988 struct h2c *h2c = h2s->h2c;
2989 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02002990 struct buffer outbuf;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002991 int es_now = 0;
2992 int ret = 0;
2993 int hdr;
2994
2995 if (h2c_mux_busy(h2c, h2s)) {
2996 h2s->flags |= H2_SF_BLK_MBUSY;
2997 return 0;
2998 }
2999
Willy Tarreau44e973f2018-03-01 17:49:30 +01003000 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003001 h2c->flags |= H2_CF_MUX_MALLOC;
3002 h2s->flags |= H2_SF_BLK_MROOM;
3003 return 0;
3004 }
3005
3006 /* First, try to parse the H1 response and index it into <list>.
3007 * NOTE! Since it comes from haproxy, we *know* that a response header
3008 * block does not wrap and we can safely read it this way without
3009 * having to realign the buffer.
3010 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003011 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003012 list, sizeof(list)/sizeof(list[0]), h1m);
3013 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003014 /* incomplete or invalid response, this is abnormal coming from
3015 * haproxy and may only result in a bad errorfile or bad Lua code
3016 * so that won't be fixed, raise an error now.
3017 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003018 * FIXME: we should instead add the ability to only return a
3019 * 502 bad gateway. But in theory this is not supposed to
3020 * happen.
3021 */
3022 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3023 ret = 0;
3024 goto end;
3025 }
3026
3027 chunk_reset(&outbuf);
3028
3029 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003031 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003032 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003033
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003035 break;
3036 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003037 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003038 }
3039
3040 if (outbuf.size < 9) {
3041 h2c->flags |= H2_CF_MUX_MFULL;
3042 h2s->flags |= H2_SF_BLK_MROOM;
3043 ret = 0;
3044 goto end;
3045 }
3046
3047 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003048 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3049 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3050 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003051
3052 /* encode status, which necessarily is the first one */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003053 if (outbuf.data < outbuf.size && h1m->status == 200)
3054 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3055 else if (outbuf.data < outbuf.size && h1m->status == 304)
3056 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003057 else if (unlikely(list[0].v.len != 3)) {
3058 /* this is an unparsable response */
3059 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3060 ret = 0;
3061 goto end;
3062 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003063 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003064 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003065 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3066 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3067 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3068 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3069 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003070 }
3071 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003072 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003073 goto realign_again;
3074
3075 h2c->flags |= H2_CF_MUX_MFULL;
3076 h2s->flags |= H2_SF_BLK_MROOM;
3077 ret = 0;
3078 goto end;
3079 }
3080
3081 /* encode all headers, stop at empty name */
3082 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003083 /* these ones do not exist in H2 and must be dropped. */
3084 if (isteq(list[hdr].n, ist("connection")) ||
3085 isteq(list[hdr].n, ist("proxy-connection")) ||
3086 isteq(list[hdr].n, ist("keep-alive")) ||
3087 isteq(list[hdr].n, ist("upgrade")) ||
3088 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003089 continue;
3090
3091 if (isteq(list[hdr].n, ist("")))
3092 break; // end
3093
3094 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3095 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003096 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003097 goto realign_again;
3098
3099 h2c->flags |= H2_CF_MUX_MFULL;
3100 h2s->flags |= H2_SF_BLK_MROOM;
3101 ret = 0;
3102 goto end;
3103 }
3104 }
3105
3106 /* we may need to add END_STREAM */
3107 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3108 es_now = 1;
3109
3110 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003111 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003112
3113 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003114 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003115
3116 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003117 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003118
3119 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003121 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003122
3123 /* for now we don't implemented CONTINUATION, so we wait for a
3124 * body or directly end in TRL2.
3125 */
3126 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003127 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003128 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003129
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130 h1m->state = HTTP_MSG_DONE;
3131 h2s->flags |= H2_SF_ES_SENT;
3132 if (h2s->st == H2_SS_OPEN)
3133 h2s->st = H2_SS_HLOC;
3134 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003135 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003136 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003137 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003138 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003139 h1m->state = HTTP_MSG_RPBEFORE;
3140 h1m->status = 0;
3141 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003142 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003143 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003144 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003145 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146
3147 end:
3148 //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));
3149 return ret;
3150}
3151
Willy Tarreau5dd17352018-06-14 13:33:30 +02003152/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3153 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3154 * the number of bytes sent. The caller must check the stream's status to
3155 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003156 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003157static 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 +02003158{
3159 struct h2c *h2c = h2s->h2c;
3160 struct h1m *h1m = &h2s->res;
Willy Tarreau83061a82018-07-13 11:56:34 +02003161 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003162 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003163 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003164 int es_now = 0;
3165 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003166 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003167 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003168
3169 if (h2c_mux_busy(h2c, h2s)) {
3170 h2s->flags |= H2_SF_BLK_MBUSY;
3171 goto end;
3172 }
3173
Willy Tarreau44e973f2018-03-01 17:49:30 +01003174 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003175 h2c->flags |= H2_CF_MUX_MALLOC;
3176 h2s->flags |= H2_SF_BLK_MROOM;
3177 goto end;
3178 }
3179
3180 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003181 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003182 goto end;
3183
3184 chunk_reset(&outbuf);
3185
3186 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003187 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003188 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003189 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003190
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003191 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003192 break;
3193 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003194 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003195 }
3196
3197 if (outbuf.size < 9) {
3198 h2c->flags |= H2_CF_MUX_MFULL;
3199 h2s->flags |= H2_SF_BLK_MROOM;
3200 goto end;
3201 }
3202
3203 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003204 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3205 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3206 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003207
3208 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3209 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003210 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003211 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003212 break;
3213 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003214 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003215 if ((long long)size > h1m->curr_len)
3216 size = h1m->curr_len;
3217 break;
3218 default: /* te:chunked : parse chunks */
3219 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003220 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003221 if (!ret)
3222 goto end;
3223
3224 if (ret < 0) {
3225 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3226 h1m->err_pos = ret;
3227 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3228 goto end;
3229 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003230 max -= ret;
3231 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003232 total += ret;
3233 h1m->state = HTTP_MSG_CHUNK_SIZE;
3234 }
3235
3236 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3237 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003238 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003239 if (!ret)
3240 goto end;
3241
3242 if (ret < 0) {
3243 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3244 h1m->err_pos = ret;
3245 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3246 goto end;
3247 }
3248
3249 size = chunk;
3250 h1m->curr_len = chunk;
3251 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003252 max -= ret;
3253 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003254 total += ret;
3255 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3256 if (!size)
3257 goto send_empty;
3258 }
3259
3260 /* in MSG_DATA state, continue below */
3261 size = h1m->curr_len;
3262 break;
3263 }
3264
3265 /* we have in <size> the exact number of bytes we need to copy from
3266 * the H1 buffer. We need to check this against the connection's and
3267 * the stream's send windows, and to ensure that this fits in the max
3268 * frame size and in the buffer's available space minus 9 bytes (for
3269 * the frame header). The connection's flow control is applied last so
3270 * that we can use a separate list of streams which are immediately
3271 * unblocked on window opening. Note: we don't implement padding.
3272 */
3273
Willy Tarreau5dd17352018-06-14 13:33:30 +02003274 if (size > max)
3275 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003276
3277 if (size > h2s->mws)
3278 size = h2s->mws;
3279
3280 if (size <= 0) {
3281 h2s->flags |= H2_SF_BLK_SFCTL;
3282 goto end;
3283 }
3284
3285 if (h2c->mfs && size > h2c->mfs)
3286 size = h2c->mfs;
3287
3288 if (size + 9 > outbuf.size) {
3289 /* we have an opportunity for enlarging the too small
3290 * available space, let's try.
3291 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003292 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 goto realign_again;
3294 size = outbuf.size - 9;
3295 }
3296
3297 if (size <= 0) {
3298 h2c->flags |= H2_CF_MUX_MFULL;
3299 h2s->flags |= H2_SF_BLK_MROOM;
3300 goto end;
3301 }
3302
3303 if (size > h2c->mws)
3304 size = h2c->mws;
3305
3306 if (size <= 0) {
3307 h2s->flags |= H2_SF_BLK_MFCTL;
3308 goto end;
3309 }
3310
3311 /* copy whatever we can */
3312 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003313 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 if (ret == 1)
3315 len2 = 0;
3316
3317 if (!ret || len1 + len2 < size) {
3318 /* FIXME: must normally never happen */
3319 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3320 goto end;
3321 }
3322
3323 /* limit len1/len2 to size */
3324 if (len1 + len2 > size) {
3325 int sub = len1 + len2 - size;
3326
3327 if (len2 > sub)
3328 len2 -= sub;
3329 else {
3330 sub -= len2;
3331 len2 = 0;
3332 len1 -= sub;
3333 }
3334 }
3335
3336 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003337 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003339 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003340
3341 send_empty:
3342 /* we may need to add END_STREAM */
3343 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3344 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003345 *
3346 * FIXME: what we do here is not correct because we send end_stream
3347 * before knowing if we'll have to send a HEADERS frame for the
3348 * trailers. More importantly we're not consuming the trailing CRLF
3349 * after the end of trailers, so it will be left to the caller to
3350 * eat it. The right way to do it would be to measure trailers here
3351 * and to send ES only if there are no trailers.
3352 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003353 */
3354 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3355 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3356 es_now = 1;
3357
3358 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003359 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003360
3361 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363
3364 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003365 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003366
3367 /* consume incoming H1 response */
3368 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003369 max -= size;
3370 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003371 total += size;
3372 h1m->curr_len -= size;
3373 h2s->mws -= size;
3374 h2c->mws -= size;
3375
3376 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3377 h1m->state = HTTP_MSG_CHUNK_CRLF;
3378 goto new_frame;
3379 }
3380 }
3381
3382 if (es_now) {
3383 if (h2s->st == H2_SS_OPEN)
3384 h2s->st = H2_SS_HLOC;
3385 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003386 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003387
Willy Tarreau35a62702018-02-27 15:37:25 +01003388 if (!(h1m->flags & H1_MF_CHNK)) {
3389 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003390 total += max;
3391 ofs += max;
3392 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003393
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003394 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003395 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003396
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003397 h2s->flags |= H2_SF_ES_SENT;
3398 }
3399
3400 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003401 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 +02003402 return total;
3403}
3404
Olivier Houchard6ff20392018-07-17 18:46:31 +02003405/* Called from the upper layer, to subscribe to events, such as being able to send */
3406static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3407{
3408 struct wait_list *sw;
3409 struct h2s *h2s = cs->ctx;
3410
3411 switch (event_type) {
3412 case SUB_CAN_SEND:
3413 sw = param;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003414 if (LIST_ISEMPTY(&h2s->list) &&
3415 !(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003416 LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003417 sw->wait_reason |= SUB_CAN_SEND;
3418 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003419 return 0;
3420 default:
3421 break;
3422 }
3423 return -1;
3424
3425
3426}
3427
Olivier Houchard511efea2018-08-16 15:30:32 +02003428/* Called from the upper layer, to receive data */
3429static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3430{
Olivier Houchard638b7992018-08-16 15:41:52 +02003431 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003432 size_t ret = 0;
3433
3434 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003435 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003436
Olivier Houchard638b7992018-08-16 15:41:52 +02003437 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003438 cs->flags |= CS_FL_RCV_MORE;
3439 else {
3440 cs->flags &= ~CS_FL_RCV_MORE;
3441 if (cs->flags & CS_FL_REOS)
3442 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003443 if (b_size(&h2s->rxbuf)) {
3444 b_free(&h2s->rxbuf);
3445 offer_buffers(NULL, tasks_run_queue);
3446 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003447 }
3448
3449 return ret;
3450}
3451
Willy Tarreau62f52692017-10-08 23:01:42 +02003452/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003453static size_t h2_snd_buf(struct conn_stream *cs, 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
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003524 b_del(buf, total);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003525 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003526}
3527
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003528/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003529static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003530{
3531 struct h2c *h2c = conn->mux_ctx;
3532 struct h2s *h2s;
3533 struct eb32_node *node;
3534 int fctl_cnt = 0;
3535 int send_cnt = 0;
3536 int tree_cnt = 0;
3537 int orph_cnt = 0;
3538
3539 if (!h2c)
3540 return;
3541
3542 list_for_each_entry(h2s, &h2c->fctl_list, list)
3543 fctl_cnt++;
3544
3545 list_for_each_entry(h2s, &h2c->send_list, list)
3546 send_cnt++;
3547
3548 node = eb32_first(&h2c->streams_by_id);
3549 while (node) {
3550 h2s = container_of(node, struct h2s, by_id);
3551 tree_cnt++;
3552 if (!h2s->cs)
3553 orph_cnt++;
3554 node = eb32_next(node);
3555 }
3556
Willy Tarreau616ac812018-07-24 14:12:42 +02003557 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3558 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3559 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3560 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3561 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3562 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003563}
Willy Tarreau62f52692017-10-08 23:01:42 +02003564
3565/*******************************************************/
3566/* functions below are dedicated to the config parsers */
3567/*******************************************************/
3568
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003569/* config parser for global "tune.h2.header-table-size" */
3570static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3571 struct proxy *defpx, const char *file, int line,
3572 char **err)
3573{
3574 if (too_many_args(1, args, err, NULL))
3575 return -1;
3576
3577 h2_settings_header_table_size = atoi(args[1]);
3578 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3579 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3580 return -1;
3581 }
3582 return 0;
3583}
Willy Tarreau62f52692017-10-08 23:01:42 +02003584
Willy Tarreaue6baec02017-07-27 11:45:11 +02003585/* config parser for global "tune.h2.initial-window-size" */
3586static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3587 struct proxy *defpx, const char *file, int line,
3588 char **err)
3589{
3590 if (too_many_args(1, args, err, NULL))
3591 return -1;
3592
3593 h2_settings_initial_window_size = atoi(args[1]);
3594 if (h2_settings_initial_window_size < 0) {
3595 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3596 return -1;
3597 }
3598 return 0;
3599}
3600
Willy Tarreau5242ef82017-07-27 11:47:28 +02003601/* config parser for global "tune.h2.max-concurrent-streams" */
3602static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3603 struct proxy *defpx, const char *file, int line,
3604 char **err)
3605{
3606 if (too_many_args(1, args, err, NULL))
3607 return -1;
3608
3609 h2_settings_max_concurrent_streams = atoi(args[1]);
3610 if (h2_settings_max_concurrent_streams < 0) {
3611 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3612 return -1;
3613 }
3614 return 0;
3615}
3616
Willy Tarreau62f52692017-10-08 23:01:42 +02003617
3618/****************************************/
3619/* MUX initialization and instanciation */
3620/***************************************/
3621
3622/* The mux operations */
3623const struct mux_ops h2_ops = {
3624 .init = h2_init,
3625 .recv = h2_recv,
Willy Tarreau62f52692017-10-08 23:01:42 +02003626 .wake = h2_wake,
3627 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003628 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003629 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003630 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003631 .attach = h2_attach,
3632 .detach = h2_detach,
3633 .shutr = h2_shutr,
3634 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003635 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003636 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003637 .name = "H2",
3638};
3639
Christopher Faulet32f61c02018-04-10 14:33:41 +02003640/* PROTO selection : this mux registers PROTO token "h2" */
3641static struct mux_proto_list mux_proto_h2 =
3642 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003643
3644/* config keyword parsers */
3645static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003646 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003647 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003648 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003649 { 0, NULL, NULL }
3650}};
3651
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003652static void __h2_deinit(void)
3653{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003654 pool_destroy(pool_head_h2s);
3655 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003656}
3657
Willy Tarreau62f52692017-10-08 23:01:42 +02003658__attribute__((constructor))
3659static void __h2_init(void)
3660{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003661 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003662 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003663 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003664 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3665 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003666}