blob: 985d59f8f25ff75b8c83c8edbe1465cbda1277d2 [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 Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200147#define H2_SS_MASK(state) (1UL << (state))
148#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
149#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
150#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
151#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
152#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
153#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
154#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
155#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
156#define H2_SS_EOS_BITS (H2_SS_CLOSED_BIT|H2_SS_ERROR_BIT|H2_SS_HREM_BIT)
157
Willy Tarreau18312642017-10-11 07:57:07 +0200158/* HTTP/2 stream flags (32 bit), in h2s->flags */
159#define H2_SF_NONE 0x00000000
160#define H2_SF_ES_RCVD 0x00000001
161#define H2_SF_ES_SENT 0x00000002
162
163#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
164#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
165
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200166/* stream flags indicating the reason the stream is blocked */
167#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
168#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
169#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
170#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
171#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
172
Willy Tarreau454f9052017-10-26 19:40:35 +0200173/* stream flags indicating how data is supposed to be sent */
174#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
175#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
176
177/* step we're currently in when sending chunks. This is needed because we may
178 * have to transfer chunks as large as a full buffer so there's no room left
179 * for size nor crlf around.
180 */
181#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
182#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
183#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
184
185#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
186
Willy Tarreau67434202017-11-06 20:20:51 +0100187#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100188#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100189
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100190#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
191
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200192#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
193#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200194#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200195
196
Willy Tarreau18312642017-10-11 07:57:07 +0200197/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
198 * it is being processed in the internal HTTP representation (H1 for now).
199 */
200struct h2s {
201 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100202 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200203 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200204 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200205 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200206 int32_t id; /* stream ID */
207 uint32_t flags; /* H2_SF_* */
208 int mws; /* mux window size for this stream */
209 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
210 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200211 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100212 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200213 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200214 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100215 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
216 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200217 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100218 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200219};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200220
Willy Tarreauc6405142017-09-21 20:23:50 +0200221/* descriptor for an h2 frame header */
222struct h2_fh {
223 uint32_t len; /* length, host order, 24 bits */
224 uint32_t sid; /* stream id, host order, 31 bits */
225 uint8_t ft; /* frame type */
226 uint8_t ff; /* frame flags */
227};
228
Willy Tarreau8ceae722018-11-26 11:58:30 +0100229/* the h2c connection pool */
230DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
231
232/* the h2s stream pool */
233DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
234
Willy Tarreaudc572362018-12-12 08:08:05 +0100235/* The default connection window size is 65535, it may only be enlarged using
236 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
237 * we'll pretend we already received the difference between the two to send
238 * an equivalent window update to enlarge it to 2G-1.
239 */
240#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
241
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200242/* a few settings from the global section */
243static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200244static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100245static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100246static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200247
Willy Tarreau2a856182017-05-16 15:20:39 +0200248/* a dmumy closed stream */
249static const struct h2s *h2_closed_stream = &(const struct h2s){
250 .cs = NULL,
251 .h2c = NULL,
252 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100253 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100254 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200255 .id = 0,
256};
257
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100258/* a dmumy closed stream returning a PROTOCOL_ERROR error */
259static const struct h2s *h2_error_stream = &(const struct h2s){
260 .cs = NULL,
261 .h2c = NULL,
262 .st = H2_SS_CLOSED,
263 .errcode = H2_ERR_PROTOCOL_ERROR,
264 .flags = 0,
265 .id = 0,
266};
267
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100268/* a dmumy closed stream returning a REFUSED_STREAM error */
269static const struct h2s *h2_refused_stream = &(const struct h2s){
270 .cs = NULL,
271 .h2c = NULL,
272 .st = H2_SS_CLOSED,
273 .errcode = H2_ERR_REFUSED_STREAM,
274 .flags = 0,
275 .id = 0,
276};
277
Willy Tarreau2a856182017-05-16 15:20:39 +0200278/* and a dummy idle stream for use with any unannounced stream */
279static const struct h2s *h2_idle_stream = &(const struct h2s){
280 .cs = NULL,
281 .h2c = NULL,
282 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100283 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200284 .id = 0,
285};
286
Olivier Houchard9f6af332018-05-25 14:04:04 +0200287static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200288static int h2_send(struct h2c *h2c);
289static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200290static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200291static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100292static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100293static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100294static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200295static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100296static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100297static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200298
Olivier Houchard7a977432019-03-21 15:47:13 +0100299static __inline int
300h2c_is_dead(struct h2c *h2c)
301{
302 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
303 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
304 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
305 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
306 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
307 (conn_xprt_read0_pending(h2c->conn) ||
308 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
309 return 1;
310
311 return 0;
312
313}
314
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200315/*****************************************************/
316/* functions below are for dynamic buffer management */
317/*****************************************************/
318
Willy Tarreau315d8072017-12-10 22:17:57 +0100319/* indicates whether or not the we may call the h2_recv() function to attempt
320 * to receive data into the buffer and/or demux pending data. The condition is
321 * a bit complex due to some API limits for now. The rules are the following :
322 * - if an error or a shutdown was detected on the connection and the buffer
323 * is empty, we must not attempt to receive
324 * - if the demux buf failed to be allocated, we must not try to receive and
325 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100326 * - if no flag indicates a blocking condition, we may attempt to receive,
327 * regardless of whether the demux buffer is full or not, so that only
328 * de demux part decides whether or not to block. This is needed because
329 * the connection API indeed prevents us from re-enabling receipt that is
330 * already enabled in a polled state, so we must always immediately stop
331 * as soon as the demux can't proceed so as never to hit an end of read
332 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100333 * - otherwise must may not attempt
334 */
335static inline int h2_recv_allowed(const struct h2c *h2c)
336{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100338 (h2c->st0 >= H2_CS_ERROR ||
339 h2c->conn->flags & CO_FL_ERROR ||
340 conn_xprt_read0_pending(h2c->conn)))
341 return 0;
342
343 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100344 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100345 return 1;
346
347 return 0;
348}
349
Willy Tarreau47b515a2018-12-21 16:09:41 +0100350/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200351static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100352{
353 if (!h2_recv_allowed(h2c))
354 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200355 if ((!consider_buffer || !b_data(&h2c->dbuf))
356 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100357 return;
358 tasklet_wakeup(h2c->wait_event.task);
359}
360
361
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100362/* returns true if the front connection has too many conn_streams attached */
363static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200364{
Willy Tarreaua8754662018-12-23 20:43:58 +0100365 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200366}
367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
369 * flags are used to figure what buffer was requested. It returns 1 if the
370 * allocation succeeds, in which case the connection is woken up, or 0 if it's
371 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200372 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200374{
375 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100376 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200377
Willy Tarreau44e973f2018-03-01 17:49:30 +0100378 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200379 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200380 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200381 return 1;
382 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200383
Willy Tarreau44e973f2018-03-01 17:49:30 +0100384 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
385 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200386
387 if (h2c->flags & H2_CF_DEM_MROOM) {
388 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200389 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200390 }
Willy Tarreau14398122017-09-22 14:26:04 +0200391 return 1;
392 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100393
394 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
395 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200396 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100397 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200398 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100399 return 1;
400 }
401
Willy Tarreau14398122017-09-22 14:26:04 +0200402 return 0;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
407 struct buffer *buf = NULL;
408
Willy Tarreauc234ae32019-05-13 17:56:11 +0200409 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100410 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
411 h2c->buf_wait.target = h2c;
412 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100413 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100414 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100415 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200416 __conn_xprt_stop_recv(h2c->conn);
417 }
418 return buf;
419}
420
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200421static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200422{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200423 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100424 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200425 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200426 }
427}
428
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100429/* returns the number of allocatable outgoing streams for the connection taking
430 * the last_sid and the reserved ones into account.
431 */
432static inline int h2_streams_left(const struct h2c *h2c)
433{
434 int ret;
435
436 /* consider the number of outgoing streams we're allowed to create before
437 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
438 * nb_reserved is the number of streams which don't yet have an ID.
439 */
440 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
441 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
442 if (ret < 0)
443 ret = 0;
444 return ret;
445}
446
Willy Tarreau00f18a32019-01-26 12:19:01 +0100447/* returns the number of streams in use on a connection to figure if it's
448 * idle or not. We check nb_cs and not nb_streams as the caller will want
449 * to know if it was the last one after a detach().
450 */
451static int h2_used_streams(struct connection *conn)
452{
453 struct h2c *h2c = conn->ctx;
454
455 return h2c->nb_cs;
456}
457
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100458/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100459static int h2_avail_streams(struct connection *conn)
460{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100462 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100463 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100464
Willy Tarreau6afec462019-01-28 06:40:19 +0100465 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
466 * streams on the connection.
467 */
468 if (h2c->last_sid >= 0)
469 return 0;
470
Willy Tarreau86949782019-01-31 10:42:05 +0100471 /* note: may be negative if a SETTINGS frame changes the limit */
472 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100473
474 /* we must also consider the limit imposed by stream IDs */
475 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100476 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100477 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100478 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
479 ret1 = MIN(ret1, ret2);
480 }
481 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100482}
483
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200484
Willy Tarreau62f52692017-10-08 23:01:42 +0200485/*****************************************************************/
486/* functions below are dedicated to the mux setup and management */
487/*****************************************************************/
488
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200489/* Initialize the mux once it's attached. For outgoing connections, the context
490 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200491 * connections from the fact that the context is still NULL (even during mux
492 * upgrades). <input> is always used as Input buffer and may contain data. It is
493 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200494 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200495static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
496 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200497{
498 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100499 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200500
Willy Tarreaubafbe012017-11-24 17:34:44 +0100501 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200502 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200503 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200504
Christopher Faulete9b70722019-04-08 10:46:02 +0200505 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200506 h2c->flags = H2_CF_IS_BACK;
507 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
508 if (tick_isset(prx->timeout.serverfin))
509 h2c->shut_timeout = prx->timeout.serverfin;
510 } else {
511 h2c->flags = H2_CF_NONE;
512 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
513 if (tick_isset(prx->timeout.clientfin))
514 h2c->shut_timeout = prx->timeout.clientfin;
515 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100516
Willy Tarreau0b37d652018-10-03 10:33:02 +0200517 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100518 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100519 if (tick_isset(h2c->timeout)) {
520 t = task_new(tid_bit);
521 if (!t)
522 goto fail;
523
524 h2c->task = t;
525 t->process = h2_timeout_task;
526 t->context = h2c;
527 t->expire = tick_add(now_ms, h2c->timeout);
528 }
Willy Tarreauea392822017-10-31 10:02:25 +0100529
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200530 h2c->wait_event.task = tasklet_new();
531 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200532 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200533 h2c->wait_event.task->process = h2_io_cb;
534 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100535 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200536
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
538 if (!h2c->ddht)
539 goto fail;
540
541 /* Initialise the context. */
542 h2c->st0 = H2_CS_PREFACE;
543 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100544 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->max_id = -1;
546 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100547 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200548 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100549 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200550 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100551 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100552 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200554 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200555 h2c->dsi = -1;
556 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100557
Willy Tarreau32218eb2017-09-22 08:07:25 +0200558 h2c->last_sid = -1;
559
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200560 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200561 h2c->miw = 65535; /* mux initial window size */
562 h2c->mws = 65535; /* mux window size */
563 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200564 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200565 LIST_INIT(&h2c->send_list);
566 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200567 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100568 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200569
Willy Tarreau3f133572017-10-31 19:21:06 +0100570 if (t)
571 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100572
Willy Tarreau01b44822018-10-03 14:26:37 +0200573 if (h2c->flags & H2_CF_IS_BACK) {
574 /* FIXME: this is temporary, for outgoing connections we need
575 * to immediately allocate a stream until the code is modified
576 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100577 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200578 */
579 struct h2s *h2s;
580
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100581 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200582 if (!h2s)
583 goto fail_stream;
584 }
585
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100586 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200587
Willy Tarreau0f383582018-10-03 14:22:21 +0200588 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200589 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200590 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200591 fail_stream:
592 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200593 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200594 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200595 if (h2c->wait_event.task)
596 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100597 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200598 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200599 return -1;
600}
601
Willy Tarreau751f2d02018-10-05 09:35:00 +0200602/* returns the next allocatable outgoing stream ID for the H2 connection, or
603 * -1 if no more is allocatable.
604 */
605static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
606{
607 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100608
609 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200610 id = -1;
611 return id;
612}
613
Willy Tarreau2373acc2017-10-12 17:35:14 +0200614/* returns the stream associated with id <id> or NULL if not found */
615static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
616{
617 struct eb32_node *node;
618
Willy Tarreau751f2d02018-10-05 09:35:00 +0200619 if (id == 0)
620 return (struct h2s *)h2_closed_stream;
621
Willy Tarreau2a856182017-05-16 15:20:39 +0200622 if (id > h2c->max_id)
623 return (struct h2s *)h2_idle_stream;
624
Willy Tarreau2373acc2017-10-12 17:35:14 +0200625 node = eb32_lookup(&h2c->streams_by_id, id);
626 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200627 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200628
629 return container_of(node, struct h2s, by_id);
630}
631
Christopher Faulet73c12072019-04-08 11:23:22 +0200632/* release function. This one should be called to free all resources allocated
633 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200634 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200635static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200636{
Christopher Faulet61840e72019-04-15 09:33:32 +0200637 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200638
Willy Tarreau32218eb2017-09-22 08:07:25 +0200639 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200640 /* The connection must be aattached to this mux to be released */
641 if (h2c->conn && h2c->conn->ctx == h2c)
642 conn = h2c->conn;
643
Willy Tarreau32218eb2017-09-22 08:07:25 +0200644 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200645
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100646 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100647 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200649
Willy Tarreau44e973f2018-03-01 17:49:30 +0100650 h2_release_buf(h2c, &h2c->dbuf);
651 h2_release_buf(h2c, &h2c->mbuf);
652
Willy Tarreauea392822017-10-31 10:02:25 +0100653 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200654 h2c->task->context = NULL;
655 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100656 h2c->task = NULL;
657 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200658 if (h2c->wait_event.task)
659 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200660 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100661 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200662 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100663
Willy Tarreaubafbe012017-11-24 17:34:44 +0100664 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200665 }
666
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200667 if (conn) {
668 conn->mux = NULL;
669 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200670
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200671 conn_stop_tracking(conn);
672 conn_full_close(conn);
673 if (conn->destroy_cb)
674 conn->destroy_cb(conn);
675 conn_free(conn);
676 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200677}
678
679
Willy Tarreau71681172017-10-23 14:39:06 +0200680/******************************************************/
681/* functions below are for the H2 protocol processing */
682/******************************************************/
683
684/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100685static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200686{
687 return h2s ? h2s->id : 0;
688}
689
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200690/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100691static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200692{
693 if (h2c->msi < 0)
694 return 0;
695
696 if (h2c->msi == h2s_id(h2s))
697 return 0;
698
699 return 1;
700}
701
Willy Tarreau741d6df2017-10-17 08:00:59 +0200702/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100703static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200704{
705 h2c->errcode = err;
706 h2c->st0 = H2_CS_ERROR;
707}
708
Willy Tarreau175cebb2019-01-24 10:02:24 +0100709/* marks an error on the stream. It may also update an already closed stream
710 * (e.g. to report an error after an RST was received).
711 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100712static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200713{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100714 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200715 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100716 if (h2s->st < H2_SS_ERROR)
717 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100718 if (h2s->cs)
719 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200720 }
721}
722
Willy Tarreau7e094452018-12-19 18:08:52 +0100723/* attempt to notify the data layer of recv availability */
724static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
725{
726 struct wait_event *sw;
727
728 if (h2s->recv_wait) {
729 sw = h2s->recv_wait;
730 sw->events &= ~SUB_RETRY_RECV;
731 tasklet_wakeup(sw->task);
732 h2s->recv_wait = NULL;
733 }
734}
735
736/* attempt to notify the data layer of send availability */
737static void __maybe_unused h2s_notify_send(struct h2s *h2s)
738{
739 struct wait_event *sw;
740
Willy Tarreauc234ae32019-05-13 17:56:11 +0200741 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100742 sw = h2s->send_wait;
743 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100744 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100745 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100746 }
747}
748
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100749/* alerts the data layer, trying to wake it up by all means, following
750 * this sequence :
751 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
752 * - if its subscribed to send, then it's woken up for send
753 * - if it was subscribed to neither, its ->wake() callback is called
754 * It is safe to call this function with a closed stream which doesn't have a
755 * conn_stream anymore.
756 */
757static void __maybe_unused h2s_alert(struct h2s *h2s)
758{
759 if (h2s->recv_wait || h2s->send_wait) {
760 h2s_notify_recv(h2s);
761 h2s_notify_send(h2s);
762 }
763 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
764 h2s->cs->data_cb->wake(h2s->cs);
765}
766
Willy Tarreaue4820742017-07-27 13:37:23 +0200767/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100768static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200769{
770 uint8_t *out = frame;
771
772 *out = len >> 16;
773 write_n16(out + 1, len);
774}
775
Willy Tarreau54c15062017-10-10 17:10:03 +0200776/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
777 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
778 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200779 * available in the buffer's input prior to calling this function. The buffer
780 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200781 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200783 const struct buffer *b, int o)
784{
Willy Tarreau591d4452018-06-15 17:21:00 +0200785 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 +0200786}
787
Willy Tarreau1f094672017-11-20 21:27:45 +0100788static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200789{
Willy Tarreau591d4452018-06-15 17:21:00 +0200790 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200791}
792
Willy Tarreau1f094672017-11-20 21:27:45 +0100793static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200794{
Willy Tarreau591d4452018-06-15 17:21:00 +0200795 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200796}
797
Willy Tarreau1f094672017-11-20 21:27:45 +0100798static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200799{
Willy Tarreau591d4452018-06-15 17:21:00 +0200800 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200801}
802
803
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100804/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
805 * The algorithm is not obvious. It turns out that H2 headers are neither
806 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
807 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 *
809 * b0 b1 b2 b3 b4 b5..b8
810 * +----------+---------+--------+----+----+----------------------+
811 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
812 * +----------+---------+--------+----+----+----------------------+
813 *
814 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
815 * we get the sid properly aligned and ordered, and 16 bits of len properly
816 * ordered as well. The type and flags can be extracted using bit shifts from
817 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200818 * Returns zero if some bytes are missing, otherwise non-zero on success. The
819 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200820 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100821static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200822{
823 uint64_t w;
824
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100825 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200826 return 0;
827
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100828 w = h2_get_n64(b, o + 1);
829 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200830 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
831 h->ff = w >> 32;
832 h->ft = w >> 40;
833 h->len += w >> 48;
834 return 1;
835}
836
837/* skip the next 9 bytes corresponding to the frame header possibly parsed by
838 * h2_peek_frame_hdr() above.
839 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100840static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200841{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200842 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200843}
844
845/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100846static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200847{
848 int ret;
849
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100850 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200851 if (ret > 0)
852 h2_skip_frame_hdr(b);
853 return ret;
854}
855
Willy Tarreau00dd0782018-03-01 16:31:34 +0100856/* marks stream <h2s> as CLOSED and decrement the number of active streams for
857 * its connection if the stream was not yet closed. Please use this exclusively
858 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100859 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100860static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100861{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100862 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100863 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100864 if (!h2s->id)
865 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100866 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100867 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100868 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
869 h2s_notify_recv(h2s);
870 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100871 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100872 h2s->st = H2_SS_CLOSED;
873}
874
Willy Tarreau71049cc2018-03-28 13:56:39 +0200875/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
876static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100877{
878 h2s_close(h2s);
879 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200880 if (b_size(&h2s->rxbuf)) {
881 b_free(&h2s->rxbuf);
882 offer_buffers(NULL, tasks_run_queue);
883 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200884 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100885 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200886 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100887 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800888 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200889 * reference left would be in the h2c send_list/fctl_list, and if
890 * we're in it, we're getting out anyway
891 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100892 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200893 if (LIST_ADDED(&h2s->sending_list)) {
Olivier Houchard998410a2019-04-15 19:23:37 +0200894 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
895 LIST_DEL_INIT(&h2s->sending_list);
896 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200897 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100898 pool_free(pool_head_h2s, h2s);
899}
900
Willy Tarreaua8e49542018-10-03 18:53:55 +0200901/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
902 * stream tree. In case of error, nothing is added and NULL is returned. The
903 * causes of errors can be any failed memory allocation. The caller is
904 * responsible for checking if the connection may support an extra stream
905 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200906 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200907static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200908{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200909 struct h2s *h2s;
910
Willy Tarreaubafbe012017-11-24 17:34:44 +0100911 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200912 if (!h2s)
913 goto out;
914
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200915 h2s->wait_event.task = tasklet_new();
916 if (!h2s->wait_event.task) {
917 pool_free(pool_head_h2s, h2s);
918 goto out;
919 }
920 h2s->send_wait = NULL;
921 h2s->recv_wait = NULL;
922 h2s->wait_event.task->process = h2_deferred_shut;
923 h2s->wait_event.task->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100924 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200925 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100926 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200927 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200928 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200929 h2s->mws = h2c->miw;
930 h2s->flags = H2_SF_NONE;
931 h2s->errcode = H2_ERR_NO_ERROR;
932 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200933 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100934 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200935 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200936
937 if (h2c->flags & H2_CF_IS_BACK) {
938 h1m_init_req(&h2s->h1m);
939 h2s->h1m.err_pos = -1; // don't care about errors on the request path
940 h2s->h1m.flags |= H1_MF_TOLOWER;
941 } else {
942 h1m_init_res(&h2s->h1m);
943 h2s->h1m.err_pos = -1; // don't care about errors on the response path
944 h2s->h1m.flags |= H1_MF_TOLOWER;
945 }
946
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200947 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200948 if (id > 0)
949 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100950 else
951 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200952
953 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100954 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100955 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200956
957 return h2s;
958
959 out_free_h2s:
960 pool_free(pool_head_h2s, h2s);
961 out:
962 return NULL;
963}
964
965/* creates a new stream <id> on the h2c connection and returns it, or NULL in
966 * case of memory allocation error.
967 */
968static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
969{
970 struct session *sess = h2c->conn->owner;
971 struct conn_stream *cs;
972 struct h2s *h2s;
973
974 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
975 goto out;
976
977 h2s = h2s_new(h2c, id);
978 if (!h2s)
979 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200980
981 cs = cs_new(h2c->conn);
982 if (!cs)
983 goto out_close;
984
Olivier Houchard746fb772018-12-15 19:42:00 +0100985 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200986 h2s->cs = cs;
987 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200988 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200989
990 if (stream_create_from_cs(cs) < 0)
991 goto out_free_cs;
992
Willy Tarreau590a0512018-09-05 11:56:48 +0200993 /* We want the accept date presented to the next stream to be the one
994 * we have now, the handshake time to be null (since the next stream
995 * is not delayed by a handshake), and the idle time to count since
996 * right now.
997 */
998 sess->accept_date = date;
999 sess->tv_accept = now;
1000 sess->t_handshake = 0;
1001
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001002 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001003 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001004 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001005 return h2s;
1006
1007 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001008 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001009 cs_free(cs);
1010 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001011 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001012 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001013 sess_log(sess);
1014 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001015}
1016
Willy Tarreau751f2d02018-10-05 09:35:00 +02001017/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1018 * and returns it, or NULL in case of memory allocation error or if the highest
1019 * possible stream ID was reached.
1020 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001021static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001022{
1023 struct h2s *h2s = NULL;
1024
Willy Tarreau86949782019-01-31 10:42:05 +01001025 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001026 goto out;
1027
Willy Tarreaua80dca82019-01-24 17:08:28 +01001028 if (h2_streams_left(h2c) < 1)
1029 goto out;
1030
Willy Tarreau751f2d02018-10-05 09:35:00 +02001031 /* Defer choosing the ID until we send the first message to create the stream */
1032 h2s = h2s_new(h2c, 0);
1033 if (!h2s)
1034 goto out;
1035
1036 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001037 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001038 cs->ctx = h2s;
1039 h2c->nb_cs++;
1040
Willy Tarreau751f2d02018-10-05 09:35:00 +02001041 out:
1042 return h2s;
1043}
1044
Willy Tarreaube5b7152017-09-25 16:25:39 +02001045/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1046 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1047 * the various settings codes.
1048 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001049static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001050{
1051 struct buffer *res;
1052 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001053 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001054 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001055 int ret;
1056
1057 if (h2c_mux_busy(h2c, NULL)) {
1058 h2c->flags |= H2_CF_DEM_MBUSY;
1059 return 0;
1060 }
1061
Willy Tarreau44e973f2018-03-01 17:49:30 +01001062 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001063 if (!res) {
1064 h2c->flags |= H2_CF_MUX_MALLOC;
1065 h2c->flags |= H2_CF_DEM_MROOM;
1066 return 0;
1067 }
1068
1069 chunk_init(&buf, buf_data, sizeof(buf_data));
1070 chunk_memcpy(&buf,
1071 "\x00\x00\x00" /* length : 0 for now */
1072 "\x04\x00" /* type : 4 (settings), flags : 0 */
1073 "\x00\x00\x00\x00", /* stream ID : 0 */
1074 9);
1075
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001076 if (h2c->flags & H2_CF_IS_BACK) {
1077 /* send settings_enable_push=0 */
1078 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1079 }
1080
Willy Tarreaube5b7152017-09-25 16:25:39 +02001081 if (h2_settings_header_table_size != 4096) {
1082 char str[6] = "\x00\x01"; /* header_table_size */
1083
1084 write_n32(str + 2, h2_settings_header_table_size);
1085 chunk_memcat(&buf, str, 6);
1086 }
1087
1088 if (h2_settings_initial_window_size != 65535) {
1089 char str[6] = "\x00\x04"; /* initial_window_size */
1090
1091 write_n32(str + 2, h2_settings_initial_window_size);
1092 chunk_memcat(&buf, str, 6);
1093 }
1094
1095 if (h2_settings_max_concurrent_streams != 0) {
1096 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1097
1098 /* Note: 0 means "unlimited" for haproxy's config but not for
1099 * the protocol, so never send this value!
1100 */
1101 write_n32(str + 2, h2_settings_max_concurrent_streams);
1102 chunk_memcat(&buf, str, 6);
1103 }
1104
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001105 mfs = h2_settings_max_frame_size;
1106 if (mfs > global.tune.bufsize)
1107 mfs = global.tune.bufsize;
1108
1109 if (!mfs)
1110 mfs = global.tune.bufsize;
1111
1112 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001113 char str[6] = "\x00\x05"; /* max_frame_size */
1114
1115 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1116 * match bufsize - rewrite size, but at the moment it seems
1117 * that clients don't take care of it.
1118 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001119 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001120 chunk_memcat(&buf, str, 6);
1121 }
1122
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001123 h2_set_frame_size(buf.area, buf.data - 9);
1124 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001125 if (unlikely(ret <= 0)) {
1126 if (!ret) {
1127 h2c->flags |= H2_CF_MUX_MFULL;
1128 h2c->flags |= H2_CF_DEM_MROOM;
1129 return 0;
1130 }
1131 else {
1132 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1133 return 0;
1134 }
1135 }
1136 return ret;
1137}
1138
Willy Tarreau52eed752017-09-22 15:05:09 +02001139/* Try to receive a connection preface, then upon success try to send our
1140 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1141 * missing data. It may return an error in h2c.
1142 */
1143static int h2c_frt_recv_preface(struct h2c *h2c)
1144{
1145 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001146 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001147
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001148 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001149
1150 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001151 if (ret1 < 0)
1152 sess_log(h2c->conn->owner);
1153
Willy Tarreau52eed752017-09-22 15:05:09 +02001154 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1155 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1156 return 0;
1157 }
1158
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001159 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001160 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001161 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001162
Willy Tarreaube5b7152017-09-25 16:25:39 +02001163 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001164}
1165
Willy Tarreau01b44822018-10-03 14:26:37 +02001166/* Try to send a connection preface, then upon success try to send our
1167 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1168 * missing data. It may return an error in h2c.
1169 */
1170static int h2c_bck_send_preface(struct h2c *h2c)
1171{
1172 struct buffer *res;
1173
1174 if (h2c_mux_busy(h2c, NULL)) {
1175 h2c->flags |= H2_CF_DEM_MBUSY;
1176 return 0;
1177 }
1178
1179 res = h2_get_buf(h2c, &h2c->mbuf);
1180 if (!res) {
1181 h2c->flags |= H2_CF_MUX_MALLOC;
1182 h2c->flags |= H2_CF_DEM_MROOM;
1183 return 0;
1184 }
1185
1186 if (!b_data(res)) {
1187 /* preface not yet sent */
1188 b_istput(res, ist(H2_CONN_PREFACE));
1189 }
1190
1191 return h2c_send_settings(h2c);
1192}
1193
Willy Tarreau081d4722017-05-16 21:51:05 +02001194/* try to send a GOAWAY frame on the connection to report an error or a graceful
1195 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1196 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1197 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1198 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1199 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1200 * on unrecoverable failure. It will not attempt to send one again in this last
1201 * case so that it is safe to use h2c_error() to report such errors.
1202 */
1203static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1204{
1205 struct buffer *res;
1206 char str[17];
1207 int ret;
1208
1209 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1210 return 1; // claim that it worked
1211
1212 if (h2c_mux_busy(h2c, h2s)) {
1213 if (h2s)
1214 h2s->flags |= H2_SF_BLK_MBUSY;
1215 else
1216 h2c->flags |= H2_CF_DEM_MBUSY;
1217 return 0;
1218 }
1219
Willy Tarreau44e973f2018-03-01 17:49:30 +01001220 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001221 if (!res) {
1222 h2c->flags |= H2_CF_MUX_MALLOC;
1223 if (h2s)
1224 h2s->flags |= H2_SF_BLK_MROOM;
1225 else
1226 h2c->flags |= H2_CF_DEM_MROOM;
1227 return 0;
1228 }
1229
1230 /* len: 8, type: 7, flags: none, sid: 0 */
1231 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1232
1233 if (h2c->last_sid < 0)
1234 h2c->last_sid = h2c->max_id;
1235
1236 write_n32(str + 9, h2c->last_sid);
1237 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001238 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001239 if (unlikely(ret <= 0)) {
1240 if (!ret) {
1241 h2c->flags |= H2_CF_MUX_MFULL;
1242 if (h2s)
1243 h2s->flags |= H2_SF_BLK_MROOM;
1244 else
1245 h2c->flags |= H2_CF_DEM_MROOM;
1246 return 0;
1247 }
1248 else {
1249 /* we cannot report this error using GOAWAY, so we mark
1250 * it and claim a success.
1251 */
1252 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1253 h2c->flags |= H2_CF_GOAWAY_FAILED;
1254 return 1;
1255 }
1256 }
1257 h2c->flags |= H2_CF_GOAWAY_SENT;
1258 return ret;
1259}
1260
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001261/* Try to send an RST_STREAM frame on the connection for the indicated stream
1262 * during mux operations. This stream must be valid and cannot be closed
1263 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1264 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1265 * not yet.
1266 *
1267 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1268 * to write the message, it subscribes the stream to future notifications.
1269 */
1270static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1271{
1272 struct buffer *res;
1273 char str[13];
1274 int ret;
1275
1276 if (!h2s || h2s->st == H2_SS_CLOSED)
1277 return 1;
1278
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001279 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1280 * RST_STREAM in response to a RST_STREAM frame.
1281 */
1282 if (h2c->dft == H2_FT_RST_STREAM) {
1283 ret = 1;
1284 goto ignore;
1285 }
1286
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001287 if (h2c_mux_busy(h2c, h2s)) {
1288 h2s->flags |= H2_SF_BLK_MBUSY;
1289 return 0;
1290 }
1291
Willy Tarreau44e973f2018-03-01 17:49:30 +01001292 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001293 if (!res) {
1294 h2c->flags |= H2_CF_MUX_MALLOC;
1295 h2s->flags |= H2_SF_BLK_MROOM;
1296 return 0;
1297 }
1298
1299 /* len: 4, type: 3, flags: none */
1300 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1301 write_n32(str + 5, h2s->id);
1302 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001303 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304
1305 if (unlikely(ret <= 0)) {
1306 if (!ret) {
1307 h2c->flags |= H2_CF_MUX_MFULL;
1308 h2s->flags |= H2_SF_BLK_MROOM;
1309 return 0;
1310 }
1311 else {
1312 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1313 return 0;
1314 }
1315 }
1316
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001317 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001318 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001319 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001320 return ret;
1321}
1322
1323/* Try to send an RST_STREAM frame on the connection for the stream being
1324 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001325 * error code, even if the stream is one of the dummy ones, and will update
1326 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001327 *
1328 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1329 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001330 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001331 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 */
1333static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1334{
1335 struct buffer *res;
1336 char str[13];
1337 int ret;
1338
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001339 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1340 * RST_STREAM in response to a RST_STREAM frame.
1341 */
1342 if (h2c->dft == H2_FT_RST_STREAM) {
1343 ret = 1;
1344 goto ignore;
1345 }
1346
Willy Tarreau27a84c92017-10-17 08:10:17 +02001347 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001348 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001349 return 0;
1350 }
1351
Willy Tarreau44e973f2018-03-01 17:49:30 +01001352 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001353 if (!res) {
1354 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001356 return 0;
1357 }
1358
1359 /* len: 4, type: 3, flags: none */
1360 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001361
Willy Tarreau27a84c92017-10-17 08:10:17 +02001362 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001363 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001364 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001365
Willy Tarreau27a84c92017-10-17 08:10:17 +02001366 if (unlikely(ret <= 0)) {
1367 if (!ret) {
1368 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001369 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001370 return 0;
1371 }
1372 else {
1373 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1374 return 0;
1375 }
1376 }
1377
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001378 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001379 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001380 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001381 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001382 }
1383
Willy Tarreau27a84c92017-10-17 08:10:17 +02001384 return ret;
1385}
1386
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001387/* try to send an empty DATA frame with the ES flag set to notify about the
1388 * end of stream and match a shutdown(write). If an ES was already sent as
1389 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1390 * on success or zero if nothing was done. In case of lack of room to write the
1391 * message, it subscribes the requesting stream to future notifications.
1392 */
1393static int h2_send_empty_data_es(struct h2s *h2s)
1394{
1395 struct h2c *h2c = h2s->h2c;
1396 struct buffer *res;
1397 char str[9];
1398 int ret;
1399
Willy Tarreau721c9742017-11-07 11:05:42 +01001400 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001401 return 1;
1402
1403 if (h2c_mux_busy(h2c, h2s)) {
1404 h2s->flags |= H2_SF_BLK_MBUSY;
1405 return 0;
1406 }
1407
Willy Tarreau44e973f2018-03-01 17:49:30 +01001408 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001409 if (!res) {
1410 h2c->flags |= H2_CF_MUX_MALLOC;
1411 h2s->flags |= H2_SF_BLK_MROOM;
1412 return 0;
1413 }
1414
1415 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1416 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1417 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001418 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001419 if (likely(ret > 0)) {
1420 h2s->flags |= H2_SF_ES_SENT;
1421 }
1422 else if (!ret) {
1423 h2c->flags |= H2_CF_MUX_MFULL;
1424 h2s->flags |= H2_SF_BLK_MROOM;
1425 return 0;
1426 }
1427 else {
1428 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1429 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001430 }
1431 return ret;
1432}
1433
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001434/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
1435 * CS_FL_REOS, CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
1436 * is automatically updated accordingly. If the stream is orphaned, it is
1437 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001438 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001439static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001440{
1441 if (!h2s->cs) {
1442 /* this stream was already orphaned */
1443 h2s_destroy(h2s);
1444 return;
1445 }
1446
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001447 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
1448 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001449
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001450 if (h2s->st == H2_SS_OPEN)
1451 h2s->st = H2_SS_HREM;
1452 else if (h2s->st == H2_SS_HLOC)
1453 h2s_close(h2s);
1454 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001455
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001456 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1457 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1458 h2s->cs->flags |= CS_FL_ERR_PENDING;
1459 if (h2s->cs->flags & CS_FL_EOS)
1460 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001461
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001462 if (h2s->st < H2_SS_ERROR)
1463 h2s->st = H2_SS_ERROR;
1464 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001465
1466 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467}
1468
1469/* wake the streams attached to the connection, whose id is greater than <last>
1470 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001471 */
Willy Tarreau23482912019-05-07 15:23:14 +02001472static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001473{
1474 struct eb32_node *node;
1475 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001476
Christopher Fauletf02ca002019-03-07 16:21:34 +01001477 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001478 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1479 while (node) {
1480 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001481 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001482 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001483 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001484
Christopher Fauletf02ca002019-03-07 16:21:34 +01001485 /* Wake all streams with unassigned ID (ID == 0) */
1486 node = eb32_lookup(&h2c->streams_by_id, 0);
1487 while (node) {
1488 h2s = container_of(node, struct h2s, by_id);
1489 if (h2s->id > 0)
1490 break;
1491 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001492 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001493 }
1494}
1495
Willy Tarreau3421aba2017-07-27 15:41:03 +02001496/* Increase all streams' outgoing window size by the difference passed in
1497 * argument. This is needed upon receipt of the settings frame if the initial
1498 * window size is different. The difference may be negative and the resulting
1499 * window size as well, for the time it takes to receive some window updates.
1500 */
1501static void h2c_update_all_ws(struct h2c *h2c, int diff)
1502{
1503 struct h2s *h2s;
1504 struct eb32_node *node;
1505
1506 if (!diff)
1507 return;
1508
1509 node = eb32_first(&h2c->streams_by_id);
1510 while (node) {
1511 h2s = container_of(node, struct h2s, by_id);
1512 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001513
1514 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1515 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001516 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001517 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001518 }
1519
Willy Tarreau3421aba2017-07-27 15:41:03 +02001520 node = eb32_next(node);
1521 }
1522}
1523
1524/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1525 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001526 * return an error in h2c. The caller must have already verified frame length
1527 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 */
1529static int h2c_handle_settings(struct h2c *h2c)
1530{
1531 unsigned int offset;
1532 int error;
1533
1534 if (h2c->dff & H2_F_SETTINGS_ACK) {
1535 if (h2c->dfl) {
1536 error = H2_ERR_FRAME_SIZE_ERROR;
1537 goto fail;
1538 }
1539 return 1;
1540 }
1541
Willy Tarreau3421aba2017-07-27 15:41:03 +02001542 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001543 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001544 return 0;
1545
1546 /* parse the frame */
1547 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001548 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1549 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001550
1551 switch (type) {
1552 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1553 /* we need to update all existing streams with the
1554 * difference from the previous iws.
1555 */
1556 if (arg < 0) { // RFC7540#6.5.2
1557 error = H2_ERR_FLOW_CONTROL_ERROR;
1558 goto fail;
1559 }
1560 h2c_update_all_ws(h2c, arg - h2c->miw);
1561 h2c->miw = arg;
1562 break;
1563 case H2_SETTINGS_MAX_FRAME_SIZE:
1564 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1565 error = H2_ERR_PROTOCOL_ERROR;
1566 goto fail;
1567 }
1568 h2c->mfs = arg;
1569 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001570 case H2_SETTINGS_ENABLE_PUSH:
1571 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1572 error = H2_ERR_PROTOCOL_ERROR;
1573 goto fail;
1574 }
1575 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001576 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1577 if (h2c->flags & H2_CF_IS_BACK) {
1578 /* the limit is only for the backend; for the frontend it is our limit */
1579 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1580 arg = h2_settings_max_concurrent_streams;
1581 h2c->streams_limit = arg;
1582 }
1583 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001584 }
1585 }
1586
1587 /* need to ACK this frame now */
1588 h2c->st0 = H2_CS_FRAME_A;
1589 return 1;
1590 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001591 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001592 h2c_error(h2c, error);
1593 return 0;
1594}
1595
1596/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1597 * success or one of the h2_status values.
1598 */
1599static int h2c_ack_settings(struct h2c *h2c)
1600{
1601 struct buffer *res;
1602 char str[9];
1603 int ret = -1;
1604
1605 if (h2c_mux_busy(h2c, NULL)) {
1606 h2c->flags |= H2_CF_DEM_MBUSY;
1607 return 0;
1608 }
1609
Willy Tarreau44e973f2018-03-01 17:49:30 +01001610 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001611 if (!res) {
1612 h2c->flags |= H2_CF_MUX_MALLOC;
1613 h2c->flags |= H2_CF_DEM_MROOM;
1614 return 0;
1615 }
1616
1617 memcpy(str,
1618 "\x00\x00\x00" /* length : 0 (no data) */
1619 "\x04" "\x01" /* type : 4, flags : ACK */
1620 "\x00\x00\x00\x00" /* stream ID */, 9);
1621
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001622 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001623 if (unlikely(ret <= 0)) {
1624 if (!ret) {
1625 h2c->flags |= H2_CF_MUX_MFULL;
1626 h2c->flags |= H2_CF_DEM_MROOM;
1627 return 0;
1628 }
1629 else {
1630 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1631 return 0;
1632 }
1633 }
1634 return ret;
1635}
1636
Willy Tarreaucf68c782017-10-10 17:11:41 +02001637/* processes a PING frame and schedules an ACK if needed. The caller must pass
1638 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001639 * missing data. The caller must have already verified frame length
1640 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001641 */
1642static int h2c_handle_ping(struct h2c *h2c)
1643{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001644 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001645 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001646 h2c->st0 = H2_CS_FRAME_A;
1647 return 1;
1648}
1649
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001650/* Try to send a window update for stream id <sid> and value <increment>.
1651 * Returns > 0 on success or zero on missing room or failure. It may return an
1652 * error in h2c.
1653 */
1654static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1655{
1656 struct buffer *res;
1657 char str[13];
1658 int ret = -1;
1659
1660 if (h2c_mux_busy(h2c, NULL)) {
1661 h2c->flags |= H2_CF_DEM_MBUSY;
1662 return 0;
1663 }
1664
Willy Tarreau44e973f2018-03-01 17:49:30 +01001665 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001666 if (!res) {
1667 h2c->flags |= H2_CF_MUX_MALLOC;
1668 h2c->flags |= H2_CF_DEM_MROOM;
1669 return 0;
1670 }
1671
1672 /* length: 4, type: 8, flags: none */
1673 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1674 write_n32(str + 5, sid);
1675 write_n32(str + 9, increment);
1676
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001677 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001678
1679 if (unlikely(ret <= 0)) {
1680 if (!ret) {
1681 h2c->flags |= H2_CF_MUX_MFULL;
1682 h2c->flags |= H2_CF_DEM_MROOM;
1683 return 0;
1684 }
1685 else {
1686 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1687 return 0;
1688 }
1689 }
1690 return ret;
1691}
1692
1693/* try to send pending window update for the connection. It's safe to call it
1694 * with no pending updates. Returns > 0 on success or zero on missing room or
1695 * failure. It may return an error in h2c.
1696 */
1697static int h2c_send_conn_wu(struct h2c *h2c)
1698{
1699 int ret = 1;
1700
1701 if (h2c->rcvd_c <= 0)
1702 return 1;
1703
Willy Tarreau97aaa672018-12-23 09:49:04 +01001704 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1705 /* increase the advertised connection window to 2G on
1706 * first update.
1707 */
1708 h2c->flags |= H2_CF_WINDOW_OPENED;
1709 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1710 }
1711
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001712 /* send WU for the connection */
1713 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1714 if (ret > 0)
1715 h2c->rcvd_c = 0;
1716
1717 return ret;
1718}
1719
1720/* try to send pending window update for the current dmux stream. It's safe to
1721 * call it with no pending updates. Returns > 0 on success or zero on missing
1722 * room or failure. It may return an error in h2c.
1723 */
1724static int h2c_send_strm_wu(struct h2c *h2c)
1725{
1726 int ret = 1;
1727
1728 if (h2c->rcvd_s <= 0)
1729 return 1;
1730
1731 /* send WU for the stream */
1732 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1733 if (ret > 0)
1734 h2c->rcvd_s = 0;
1735
1736 return ret;
1737}
1738
Willy Tarreaucf68c782017-10-10 17:11:41 +02001739/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1740 * success, 0 on missing data or one of the h2_status values.
1741 */
1742static int h2c_ack_ping(struct h2c *h2c)
1743{
1744 struct buffer *res;
1745 char str[17];
1746 int ret = -1;
1747
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001748 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001749 return 0;
1750
1751 if (h2c_mux_busy(h2c, NULL)) {
1752 h2c->flags |= H2_CF_DEM_MBUSY;
1753 return 0;
1754 }
1755
Willy Tarreau44e973f2018-03-01 17:49:30 +01001756 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757 if (!res) {
1758 h2c->flags |= H2_CF_MUX_MALLOC;
1759 h2c->flags |= H2_CF_DEM_MROOM;
1760 return 0;
1761 }
1762
1763 memcpy(str,
1764 "\x00\x00\x08" /* length : 8 (same payload) */
1765 "\x06" "\x01" /* type : 6, flags : ACK */
1766 "\x00\x00\x00\x00" /* stream ID */, 9);
1767
1768 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001769 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001770
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001771 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001772 if (unlikely(ret <= 0)) {
1773 if (!ret) {
1774 h2c->flags |= H2_CF_MUX_MFULL;
1775 h2c->flags |= H2_CF_DEM_MROOM;
1776 return 0;
1777 }
1778 else {
1779 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1780 return 0;
1781 }
1782 }
1783 return ret;
1784}
1785
Willy Tarreau26f95952017-07-27 17:18:30 +02001786/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1787 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001788 * h2c or h2s. The caller must have already verified frame length and stream ID
1789 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001790 */
1791static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1792{
1793 int32_t inc;
1794 int error;
1795
Willy Tarreau26f95952017-07-27 17:18:30 +02001796 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001797 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001798 return 0;
1799
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001800 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001801
1802 if (h2c->dsi != 0) {
1803 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001804
1805 /* it's not an error to receive WU on a closed stream */
1806 if (h2s->st == H2_SS_CLOSED)
1807 return 1;
1808
1809 if (!inc) {
1810 error = H2_ERR_PROTOCOL_ERROR;
1811 goto strm_err;
1812 }
1813
1814 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1815 error = H2_ERR_FLOW_CONTROL_ERROR;
1816 goto strm_err;
1817 }
1818
1819 h2s->mws += inc;
1820 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1821 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001822 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001823 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001824 }
1825 }
1826 else {
1827 /* connection window update */
1828 if (!inc) {
1829 error = H2_ERR_PROTOCOL_ERROR;
1830 goto conn_err;
1831 }
1832
1833 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1834 error = H2_ERR_FLOW_CONTROL_ERROR;
1835 goto conn_err;
1836 }
1837
1838 h2c->mws += inc;
1839 }
1840
1841 return 1;
1842
1843 conn_err:
1844 h2c_error(h2c, error);
1845 return 0;
1846
1847 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001848 h2s_error(h2s, error);
1849 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001850 return 0;
1851}
1852
Willy Tarreaue96b0922017-10-30 00:28:29 +01001853/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001854 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1855 * have already verified frame length and stream ID validity. Described in
1856 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857 */
1858static int h2c_handle_goaway(struct h2c *h2c)
1859{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001860 int last;
1861
Willy Tarreaue96b0922017-10-30 00:28:29 +01001862 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001863 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001864 return 0;
1865
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001866 last = h2_get_n32(&h2c->dbuf, 0);
1867 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001868 if (h2c->last_sid < 0)
1869 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001870 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001871 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001872}
1873
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001875 * invalid. Returns > 0 on success or zero on missing data. It may return an
1876 * error in h2c. The caller must have already verified frame length and stream
1877 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001878 */
1879static int h2c_handle_priority(struct h2c *h2c)
1880{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001881 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001883 return 0;
1884
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001885 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001886 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001887 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1888 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001889 }
1890 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001891}
1892
Willy Tarreaucd234e92017-08-18 10:59:39 +02001893/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001894 * Returns > 0 on success or zero on missing data. The caller must have already
1895 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001896 */
1897static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1898{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001899 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001900 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001901 return 0;
1902
1903 /* late RST, already handled */
1904 if (h2s->st == H2_SS_CLOSED)
1905 return 1;
1906
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001907 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001908 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001909
1910 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001911 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001912 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001913 }
1914
1915 h2s->flags |= H2_SF_RST_RCVD;
1916 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001917}
1918
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001919/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1920 * It may return an error in h2c or h2s. The caller must consider that the
1921 * return value is the new h2s in case one was allocated (most common case).
1922 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001923 * errors here are reported as connection errors since it's impossible to
1924 * recover from such errors after the compression context has been altered.
1925 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001926static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001927{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001928 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001929 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001930 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001931 int error;
1932
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001933 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001934 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001936 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001937 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001938
1939 /* now either the frame is complete or the buffer is complete */
1940 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001941 /* The stream exists/existed, this must be a trailers frame */
1942 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001943 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1944 /* unrecoverable error ? */
1945 if (h2c->st0 >= H2_CS_ERROR)
1946 goto out;
1947
1948 if (error == 0)
1949 goto out; // missing data
1950
1951 if (error < 0) {
1952 /* Failed to decode this frame (e.g. too large request)
1953 * but the HPACK decompressor is still synchronized.
1954 */
1955 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1956 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001957 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001958 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001959 goto done;
1960 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001961 /* the connection was already killed by an RST, let's consume
1962 * the data and send another RST.
1963 */
1964 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1965 h2s = (struct h2s*)h2_error_stream;
1966 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001967 }
1968 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1969 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1970 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001971 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001972 goto conn_err;
1973 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001974 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1975 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001976
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001977 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001978
Willy Tarreau25919232019-01-03 14:48:18 +01001979 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001980 if (h2c->st0 >= H2_CS_ERROR)
1981 goto out;
1982
Willy Tarreau25919232019-01-03 14:48:18 +01001983 if (error <= 0) {
1984 if (error == 0)
1985 goto out; // missing data
1986
1987 /* Failed to decode this stream (e.g. too large request)
1988 * but the HPACK decompressor is still synchronized.
1989 */
1990 h2s = (struct h2s*)h2_error_stream;
1991 goto send_rst;
1992 }
1993
Willy Tarreau22de8d32018-09-05 19:55:58 +02001994 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001995 * positively from h2c_frt_stream_new(), the stream will report the error,
1996 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001997 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001998 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001999 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002000 h2s = (struct h2s*)h2_refused_stream;
2001 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002002 }
2003
2004 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002005 h2s->rxbuf = rxbuf;
2006 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002007 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002008
Willy Tarreau88d138e2019-01-02 19:38:14 +01002009 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002010 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002011 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002012
2013 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002014 if (h2s->cs)
2015 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002016 if (h2s->st == H2_SS_OPEN)
2017 h2s->st = H2_SS_HREM;
2018 else
2019 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002020 }
2021
Willy Tarreau3a429f02019-01-03 11:41:50 +01002022 /* update the max stream ID if the request is being processed */
2023 if (h2s->id > h2c->max_id)
2024 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002025
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002026 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002027
2028 conn_err:
2029 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002030 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002031
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002032 out:
2033 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002034 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002035
2036 send_rst:
2037 /* make the demux send an RST for the current stream. We may only
2038 * do this if we're certain that the HEADERS frame was properly
2039 * decompressed so that the HPACK decoder is still kept up to date.
2040 */
2041 h2_release_buf(h2c, &rxbuf);
2042 h2c->st0 = H2_CS_FRAME_E;
2043 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002044}
2045
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002046/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2047 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2048 * errors here are reported as connection errors since it's impossible to
2049 * recover from such errors after the compression context has been altered.
2050 */
2051static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2052{
2053 int error;
2054
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002055 if (!b_size(&h2c->dbuf))
2056 return NULL; // empty buffer
2057
2058 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2059 return NULL; // incomplete frame
2060
Willy Tarreau1915ca22019-01-24 11:49:37 +01002061 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002062
Willy Tarreau25919232019-01-03 14:48:18 +01002063 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002064 if (h2c->st0 >= H2_CS_ERROR)
2065 return NULL;
2066
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002067 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2068 /* RFC7540#5.1 */
2069 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2070 h2c->st0 = H2_CS_FRAME_E;
2071 return NULL;
2072 }
2073
Willy Tarreau25919232019-01-03 14:48:18 +01002074 if (error <= 0) {
2075 if (error == 0)
2076 return NULL; // missing data
2077
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002078 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002079 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002080 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002081 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002082 }
2083
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002084 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2085 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002086 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002087 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002088 }
2089
Willy Tarreau927b88b2019-03-04 08:03:25 +01002090 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002091 h2s->st = H2_SS_ERROR;
Willy Tarreauf8fe3d62019-05-14 11:31:00 +02002092 else if (h2s->cs && (h2s->cs->flags & CS_FL_EOI) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002093 h2s->st = H2_SS_HREM;
Willy Tarreauf8fe3d62019-05-14 11:31:00 +02002094 else if ((!h2s->cs || h2s->cs->flags & CS_FL_EOI) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002095 h2s_close(h2s);
2096
2097 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002098}
2099
Willy Tarreau454f9052017-10-26 19:40:35 +02002100/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2101 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2102 */
2103static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2104{
2105 int error;
2106
2107 /* note that empty DATA frames are perfectly valid and sometimes used
2108 * to signal an end of stream (with the ES flag).
2109 */
2110
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002111 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002112 return 0; // empty buffer
2113
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002114 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002115 return 0; // incomplete frame
2116
2117 /* now either the frame is complete or the buffer is complete */
2118
Willy Tarreau454f9052017-10-26 19:40:35 +02002119 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2120 /* RFC7540#6.1 */
2121 error = H2_ERR_STREAM_CLOSED;
2122 goto strm_err;
2123 }
2124
Willy Tarreau1915ca22019-01-24 11:49:37 +01002125 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2126 /* RFC7540#8.1.2 */
2127 error = H2_ERR_PROTOCOL_ERROR;
2128 goto strm_err;
2129 }
2130
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002131 if (!h2_frt_transfer_data(h2s))
2132 return 0;
2133
Willy Tarreau454f9052017-10-26 19:40:35 +02002134 /* call the upper layers to process the frame, then let the upper layer
2135 * notify the stream about any change.
2136 */
2137 if (!h2s->cs) {
2138 error = H2_ERR_STREAM_CLOSED;
2139 goto strm_err;
2140 }
2141
Willy Tarreau8f650c32017-11-21 19:36:21 +01002142 if (h2c->st0 >= H2_CS_ERROR)
2143 return 0;
2144
Willy Tarreau721c9742017-11-07 11:05:42 +01002145 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002146 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002147 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002148 }
2149
2150 /* check for completion : the callee will change this to FRAME_A or
2151 * FRAME_H once done.
2152 */
2153 if (h2c->st0 == H2_CS_FRAME_P)
2154 return 0;
2155
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002156 /* last frame */
2157 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002158 if (h2s->cs)
2159 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002160 if (h2s->st == H2_SS_OPEN)
2161 h2s->st = H2_SS_HREM;
2162 else
2163 h2s_close(h2s);
2164
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002165 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002166
2167 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2168 /* RFC7540#8.1.2 */
2169 error = H2_ERR_PROTOCOL_ERROR;
2170 goto strm_err;
2171 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002172 }
2173
Willy Tarreau454f9052017-10-26 19:40:35 +02002174 return 1;
2175
Willy Tarreau454f9052017-10-26 19:40:35 +02002176 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002177 h2s_error(h2s, error);
2178 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002179 return 0;
2180}
2181
Willy Tarreaubc933932017-10-09 16:21:43 +02002182/* process Rx frames to be demultiplexed */
2183static void h2_process_demux(struct h2c *h2c)
2184{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002185 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002186 struct h2_fh hdr;
2187 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002188
Willy Tarreau081d4722017-05-16 21:51:05 +02002189 if (h2c->st0 >= H2_CS_ERROR)
2190 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002191
2192 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2193 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002194 if (h2c->flags & H2_CF_IS_BACK)
2195 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002196 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2197 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002198 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002199 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002200 sess_log(h2c->conn->owner);
2201 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002202 goto fail;
2203 }
2204
2205 h2c->max_id = 0;
2206 h2c->st0 = H2_CS_SETTINGS1;
2207 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002208
2209 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002210 /* ensure that what is pending is a valid SETTINGS frame
2211 * without an ACK.
2212 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002213 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002215 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002216 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002217 sess_log(h2c->conn->owner);
2218 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002219 goto fail;
2220 }
2221
2222 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2223 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2224 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2225 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002226 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002227 goto fail;
2228 }
2229
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002230 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002231 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2232 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2233 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002234 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002235 goto fail;
2236 }
2237
Willy Tarreau3bf69182018-12-21 15:34:50 +01002238 /* that's OK, switch to FRAME_P to process it. This is
2239 * a SETTINGS frame whose header has already been
2240 * deleted above.
2241 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002242 padlen = 0;
2243 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002244 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002245 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246
2247 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002248 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002249 int ret = 0;
2250
2251 if (h2c->st0 >= H2_CS_ERROR)
2252 break;
2253
2254 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002255 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002256 break;
2257
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002258 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002259 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002260 if (!h2c->nb_streams) {
2261 /* only log if no other stream can report the error */
2262 sess_log(h2c->conn->owner);
2263 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002264 break;
2265 }
2266
Willy Tarreau3bf69182018-12-21 15:34:50 +01002267 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2268 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2269 * we read the pad length and drop it from the remaining
2270 * payload (one byte + the 9 remaining ones = 10 total
2271 * removed), so we have a frame payload starting after the
2272 * pad len. Flow controlled frames (DATA) also count the
2273 * padlen in the flow control, so it must be adjusted.
2274 */
2275 if (hdr.len < 1) {
2276 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2277 sess_log(h2c->conn->owner);
2278 goto fail;
2279 }
2280 hdr.len--;
2281
2282 if (b_data(&h2c->dbuf) < 10)
2283 break; // missing padlen
2284
2285 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2286
2287 if (padlen > hdr.len) {
2288 /* RFC7540#6.1 : pad length = length of
2289 * frame payload or greater => error.
2290 */
2291 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2292 sess_log(h2c->conn->owner);
2293 goto fail;
2294 }
2295
2296 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2297 h2c->rcvd_c++;
2298 h2c->rcvd_s++;
2299 }
2300 b_del(&h2c->dbuf, 1);
2301 }
2302 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002303
2304 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002305 h2c->dfl = hdr.len;
2306 h2c->dsi = hdr.sid;
2307 h2c->dft = hdr.ft;
2308 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002309 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002310 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002311
2312 /* check for minimum basic frame format validity */
2313 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2314 if (ret != H2_ERR_NO_ERROR) {
2315 h2c_error(h2c, ret);
2316 sess_log(h2c->conn->owner);
2317 goto fail;
2318 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319 }
2320
2321 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002322 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2323
Willy Tarreau567beb82018-12-18 16:52:44 +01002324 if (tmp_h2s != h2s && h2s && h2s->cs &&
2325 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002326 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002327 /* we may have to signal the upper layers */
2328 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002329 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002330 }
2331 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002332
Willy Tarreaud7901432017-12-29 11:34:40 +01002333 if (h2c->st0 == H2_CS_FRAME_E)
2334 goto strm_err;
2335
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002336 if (h2s->st == H2_SS_IDLE &&
2337 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2338 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2339 * this state MUST be treated as a connection error
2340 */
2341 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002342 if (!h2c->nb_streams) {
2343 /* only log if no other stream can report the error */
2344 sess_log(h2c->conn->owner);
2345 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002346 break;
2347 }
2348
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002349 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2350 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2351 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002352 * this state MUST be treated as a stream error.
2353 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2354 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002355 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002356 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2357 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2358 else
2359 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002360 goto strm_err;
2361 }
2362
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* Below the management of frames received in closed state is a
2364 * bit hackish because the spec makes strong differences between
2365 * streams closed by receiving RST, sending RST, and seeing ES
2366 * in both directions. In addition to this, the creation of a
2367 * new stream reusing the identifier of a closed one will be
2368 * detected here. Given that we cannot keep track of all closed
2369 * streams forever, we consider that unknown closed streams were
2370 * closed on RST received, which allows us to respond with an
2371 * RST without breaking the connection (eg: to abort a transfer).
2372 * Some frames have to be silently ignored as well.
2373 */
2374 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002375 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002376 /* #5.1.1: The identifier of a newly
2377 * established stream MUST be numerically
2378 * greater than all streams that the initiating
2379 * endpoint has opened or reserved. This
2380 * governs streams that are opened using a
2381 * HEADERS frame and streams that are reserved
2382 * using PUSH_PROMISE. An endpoint that
2383 * receives an unexpected stream identifier
2384 * MUST respond with a connection error.
2385 */
2386 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2387 goto strm_err;
2388 }
2389
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002390 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002391 /* RFC7540#5.1:closed: an endpoint that
2392 * receives any frame other than PRIORITY after
2393 * receiving a RST_STREAM MUST treat that as a
2394 * stream error of type STREAM_CLOSED.
2395 *
2396 * Note that old streams fall into this category
2397 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002398 *
2399 * However, we cannot generalize this to all frame types. Those
2400 * carrying compression state must still be processed before
2401 * being dropped or we'll desynchronize the decoder. This can
2402 * happen with request trailers received after sending an
2403 * RST_STREAM, or with header/trailers responses received after
2404 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002405 */
2406 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2407 h2c->st0 = H2_CS_FRAME_E;
2408 goto strm_err;
2409 }
2410
2411 /* RFC7540#5.1:closed: if this state is reached as a
2412 * result of sending a RST_STREAM frame, the peer that
2413 * receives the RST_STREAM might have already sent
2414 * frames on the stream that cannot be withdrawn. An
2415 * endpoint MUST ignore frames that it receives on
2416 * closed streams after it has sent a RST_STREAM
2417 * frame. An endpoint MAY choose to limit the period
2418 * over which it ignores frames and treat frames that
2419 * arrive after this time as being in error.
2420 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002421 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002422 /* RFC7540#5.1:closed: any frame other than
2423 * PRIO/WU/RST in this state MUST be treated as
2424 * a connection error
2425 */
2426 if (h2c->dft != H2_FT_RST_STREAM &&
2427 h2c->dft != H2_FT_PRIORITY &&
2428 h2c->dft != H2_FT_WINDOW_UPDATE) {
2429 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2430 goto strm_err;
2431 }
2432 }
2433 }
2434
Willy Tarreauc0da1962017-10-30 18:38:00 +01002435#if 0
2436 // problem below: it is not possible to completely ignore such
2437 // streams as we need to maintain the compression state as well
2438 // and for this we need to completely process these frames (eg:
2439 // HEADERS frames) as well as counting DATA frames to emit
2440 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2441 // This is a typical case of layer violation where the
2442 // transported contents are critical to the connection's
2443 // validity and must be ignored at the same time :-(
2444
2445 /* graceful shutdown, ignore streams whose ID is higher than
2446 * the one advertised in GOAWAY. RFC7540#6.8.
2447 */
2448 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002449 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2450 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002451 h2c->dfl -= ret;
2452 ret = h2c->dfl == 0;
2453 goto strm_err;
2454 }
2455#endif
2456
Willy Tarreau7e98c052017-10-10 15:56:59 +02002457 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002458 case H2_FT_SETTINGS:
2459 if (h2c->st0 == H2_CS_FRAME_P)
2460 ret = h2c_handle_settings(h2c);
2461
2462 if (h2c->st0 == H2_CS_FRAME_A)
2463 ret = h2c_ack_settings(h2c);
2464 break;
2465
Willy Tarreaucf68c782017-10-10 17:11:41 +02002466 case H2_FT_PING:
2467 if (h2c->st0 == H2_CS_FRAME_P)
2468 ret = h2c_handle_ping(h2c);
2469
2470 if (h2c->st0 == H2_CS_FRAME_A)
2471 ret = h2c_ack_ping(h2c);
2472 break;
2473
Willy Tarreau26f95952017-07-27 17:18:30 +02002474 case H2_FT_WINDOW_UPDATE:
2475 if (h2c->st0 == H2_CS_FRAME_P)
2476 ret = h2c_handle_window_update(h2c, h2s);
2477 break;
2478
Willy Tarreau61290ec2017-10-17 08:19:21 +02002479 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002480 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2481 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2482 * frames' parsers consume all following CONTINUATION
2483 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002484 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002485 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2486 sess_log(h2c->conn->owner);
2487 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002488
Willy Tarreau13278b42017-10-13 19:23:14 +02002489 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002490 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002491 if (h2c->flags & H2_CF_IS_BACK)
2492 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2493 else
2494 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002495 if (tmp_h2s) {
2496 h2s = tmp_h2s;
2497 ret = 1;
2498 }
2499 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002500 break;
2501
Willy Tarreau454f9052017-10-26 19:40:35 +02002502 case H2_FT_DATA:
2503 if (h2c->st0 == H2_CS_FRAME_P)
2504 ret = h2c_frt_handle_data(h2c, h2s);
2505
2506 if (h2c->st0 == H2_CS_FRAME_A)
2507 ret = h2c_send_strm_wu(h2c);
2508 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002509
Willy Tarreau92153fc2017-12-03 19:46:19 +01002510 case H2_FT_PRIORITY:
2511 if (h2c->st0 == H2_CS_FRAME_P)
2512 ret = h2c_handle_priority(h2c);
2513 break;
2514
Willy Tarreaucd234e92017-08-18 10:59:39 +02002515 case H2_FT_RST_STREAM:
2516 if (h2c->st0 == H2_CS_FRAME_P)
2517 ret = h2c_handle_rst_stream(h2c, h2s);
2518 break;
2519
Willy Tarreaue96b0922017-10-30 00:28:29 +01002520 case H2_FT_GOAWAY:
2521 if (h2c->st0 == H2_CS_FRAME_P)
2522 ret = h2c_handle_goaway(h2c);
2523 break;
2524
Willy Tarreau1c661982017-10-30 13:52:01 +01002525 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002526 default:
2527 /* drop frames that we ignore. They may be larger than
2528 * the buffer so we drain all of their contents until
2529 * we reach the end.
2530 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002531 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2532 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002533 h2c->dfl -= ret;
2534 ret = h2c->dfl == 0;
2535 }
2536
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002537 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002538 /* We may have to send an RST if not done yet */
2539 if (h2s->st == H2_SS_ERROR)
2540 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002541
Willy Tarreaua20a5192017-12-27 11:02:06 +01002542 if (h2c->st0 == H2_CS_FRAME_E)
2543 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002544
Willy Tarreau7e98c052017-10-10 15:56:59 +02002545 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002546 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002547 break;
2548
2549 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002550 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002551 h2c->st0 = H2_CS_FRAME_H;
2552 }
2553 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002554
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002555 if (h2c->rcvd_c > 0 &&
2556 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2557 h2c_send_conn_wu(h2c);
2558
Willy Tarreau52eed752017-09-22 15:05:09 +02002559 fail:
2560 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002561 if (h2s && h2s->cs &&
2562 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002563 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002564 /* we may have to signal the upper layers */
2565 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002566 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002567 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002568
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002569 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002570}
2571
2572/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2573 * the end.
2574 */
2575static int h2_process_mux(struct h2c *h2c)
2576{
Olivier Houchard998410a2019-04-15 19:23:37 +02002577 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002578
Willy Tarreau01b44822018-10-03 14:26:37 +02002579 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2580 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2581 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2582 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2583 if (h2c->st0 == H2_CS_ERROR) {
2584 h2c->st0 = H2_CS_ERROR2;
2585 sess_log(h2c->conn->owner);
2586 }
2587 goto fail;
2588 }
2589 h2c->st0 = H2_CS_SETTINGS1;
2590 }
2591 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002592 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002593 return 1;
2594 }
2595
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002596 /* start by sending possibly pending window updates */
2597 if (h2c->rcvd_c > 0 &&
2598 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2599 h2c_send_conn_wu(h2c) < 0)
2600 goto fail;
2601
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002602 /* First we always process the flow control list because the streams
2603 * waiting there were already elected for immediate emission but were
2604 * blocked just on this.
2605 */
2606
Olivier Houchard998410a2019-04-15 19:23:37 +02002607 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002608 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2609 h2c->st0 >= H2_CS_ERROR)
2610 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002611
Willy Tarreauc234ae32019-05-13 17:56:11 +02002612 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002613 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002614
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002615 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002616 /* For some reason, the upper layer failed to subsribe again,
2617 * so remove it from the send_list
2618 */
2619 if (!h2s->send_wait) {
2620 LIST_DEL_INIT(&h2s->list);
2621 continue;
2622 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002623 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002624 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002625 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002626 }
2627
Olivier Houchard998410a2019-04-15 19:23:37 +02002628 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002629 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2630 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002631
Willy Tarreauc234ae32019-05-13 17:56:11 +02002632 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002633 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002634
Olivier Houchard998410a2019-04-15 19:23:37 +02002635 /* For some reason, the upper layer failed to subsribe again,
2636 * so remove it from the send_list
2637 */
2638 if (!h2s->send_wait) {
2639 LIST_DEL_INIT(&h2s->list);
2640 continue;
2641 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002642 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002643 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002644 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002645 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002646 }
2647
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002648 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002649 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002650 if (h2c->st0 == H2_CS_ERROR) {
2651 if (h2c->max_id >= 0) {
2652 h2c_send_goaway_error(h2c, NULL);
2653 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2654 return 0;
2655 }
2656
2657 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2658 }
2659 return 1;
2660 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002661 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002662}
2663
Willy Tarreau62f52692017-10-08 23:01:42 +02002664
Willy Tarreau479998a2018-11-18 06:30:59 +01002665/* Attempt to read data, and subscribe if none available.
2666 * The function returns 1 if data has been received, otherwise zero.
2667 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002668static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002669{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002670 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002671 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002672 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002673 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002674
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002675 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002676 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002677
Willy Tarreau315d8072017-12-10 22:17:57 +01002678 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002679 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002680
Willy Tarreau44e973f2018-03-01 17:49:30 +01002681 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002682 if (!buf) {
2683 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002684 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002685 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002686
Olivier Houchard7505f942018-08-21 18:10:44 +02002687 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002688 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002689 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2690 /* HTX in use : try to pre-align the buffer like the
2691 * rxbufs will be to optimize memory copies. We'll make
2692 * sure that the frame header lands at the end of the
2693 * HTX block to alias it upon recv. We cannot use the
2694 * head because rcv_buf() will realign the buffer if
2695 * it's empty. Thus we cheat and pretend we already
2696 * have a few bytes there.
2697 */
2698 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002699 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002700 }
2701 else
2702 max = b_room(buf);
2703
Olivier Houchard7505f942018-08-21 18:10:44 +02002704 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002705 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002706 else
2707 ret = 0;
2708 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002709
Olivier Houchard53216e72018-10-10 15:46:36 +02002710 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002711 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002712
Olivier Houcharda1411e62018-08-17 18:42:48 +02002713 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002714 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002715 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002716 }
2717
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002718 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002719 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002720 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002721}
2722
Willy Tarreau479998a2018-11-18 06:30:59 +01002723/* Try to send data if possible.
2724 * The function returns 1 if data have been sent, otherwise zero.
2725 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002726static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002727{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002728 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002729 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002730 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731
2732 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002733 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002734
Olivier Houchard7505f942018-08-21 18:10:44 +02002735
Willy Tarreaua2af5122017-10-09 11:56:46 +02002736 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2737 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002738 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002739 }
2740
Willy Tarreaubc933932017-10-09 16:21:43 +02002741 /* This loop is quite simple : it tries to fill as much as it can from
2742 * pending streams into the existing buffer until it's reportedly full
2743 * or the end of send requests is reached. Then it tries to send this
2744 * buffer's contents out, marks it not full if at least one byte could
2745 * be sent, and tries again.
2746 *
2747 * The snd_buf() function normally takes a "flags" argument which may
2748 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2749 * data immediately comes and CO_SFL_STREAMER to indicate that the
2750 * connection is streaming lots of data (used to increase TLS record
2751 * size at the expense of latency). The former can be sent any time
2752 * there's a buffer full flag, as it indicates at least one stream
2753 * attempted to send and failed so there are pending data. An
2754 * alternative would be to set it as long as there's an active stream
2755 * but that would be problematic for ACKs until we have an absolute
2756 * guarantee that all waiters have at least one byte to send. The
2757 * latter should possibly not be set for now.
2758 */
2759
2760 done = 0;
2761 while (!done) {
2762 unsigned int flags = 0;
2763
2764 /* fill as much as we can into the current buffer */
2765 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2766 done = h2_process_mux(h2c);
2767
Olivier Houchard2b094432019-01-29 18:28:36 +01002768 if (h2c->flags & H2_CF_MUX_MALLOC)
2769 break;
2770
Willy Tarreaubc933932017-10-09 16:21:43 +02002771 if (conn->flags & CO_FL_ERROR)
2772 break;
2773
2774 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2775 flags |= CO_SFL_MSG_MORE;
2776
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002777 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002778 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002779 if (!ret)
2780 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002781 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002782 b_del(&h2c->mbuf, ret);
2783 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002784 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002785
2786 /* wrote at least one byte, the buffer is not full anymore */
2787 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2788 }
2789
Willy Tarreaua2af5122017-10-09 11:56:46 +02002790 if (conn->flags & CO_FL_SOCK_WR_SH) {
2791 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002792 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002794 /* We're not full anymore, so we can wake any task that are waiting
2795 * for us.
2796 */
2797 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002798 struct h2s *h2s;
2799
2800 list_for_each_entry(h2s, &h2c->send_list, list) {
2801 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2802 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002803
Willy Tarreauc234ae32019-05-13 17:56:11 +02002804 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002805 continue;
2806
Olivier Houchard998410a2019-04-15 19:23:37 +02002807 /* For some reason, the upper layer failed to subsribe again,
2808 * so remove it from the send_list
2809 */
2810 if (!h2s->send_wait) {
2811 LIST_DEL_INIT(&h2s->list);
2812 continue;
2813 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002814 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002815 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002816 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002817 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002818 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002819 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002820 /* We're done, no more to send */
2821 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002822 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002823schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002824 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002825 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002826 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002827}
2828
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002829/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002830static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2831{
2832 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002833 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002834
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002835 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002836 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002837 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002838 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002839 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002840 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002841 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002842}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002843
Willy Tarreau62f52692017-10-08 23:01:42 +02002844/* callback called on any event by the connection handler.
2845 * It applies changes and returns zero, or < 0 if it wants immediate
2846 * destruction of the connection (which normally doesn not happen in h2).
2847 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002848static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002849{
Olivier Houchard7505f942018-08-21 18:10:44 +02002850 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002851
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002852 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002853 h2_process_demux(h2c);
2854
2855 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002856 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002859 h2c->flags &= ~H2_CF_DEM_DFULL;
2860 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002861 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002862
Willy Tarreau0b37d652018-10-03 10:33:02 +02002863 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002864 /* frontend is stopping, reload likely in progress, let's try
2865 * to announce a graceful shutdown if not yet done. We don't
2866 * care if it fails, it will be tried again later.
2867 */
2868 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2869 if (h2c->last_sid < 0)
2870 h2c->last_sid = (1U << 31) - 1;
2871 h2c_send_goaway_error(h2c, NULL);
2872 }
2873 }
2874
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002875 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002876 * If we received early data, and the handshake is done, wake
2877 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002878 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002879 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2880 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2881 struct eb32_node *node;
2882 struct h2s *h2s;
2883
2884 h2c->flags |= H2_CF_WAIT_FOR_HS;
2885 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2886
2887 while (node) {
2888 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002889 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002890 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002891 node = eb32_next(node);
2892 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002893 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002894
Willy Tarreau26bd7612017-10-09 16:47:04 +02002895 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002896 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2897 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2898 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002899 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002900
2901 if (eb_is_empty(&h2c->streams_by_id)) {
2902 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002903 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002904 return -1;
2905 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002906 }
2907
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002908 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002909 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002910
Olivier Houchard53216e72018-10-10 15:46:36 +02002911 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2912 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2913 (h2c->st0 != H2_CS_ERROR &&
2914 !b_data(&h2c->mbuf) &&
2915 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2916 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002917 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002918
Willy Tarreau3f133572017-10-31 19:21:06 +01002919 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002920 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002921 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002922 task_queue(h2c->task);
2923 }
2924 else
2925 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002926 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002927
Olivier Houchard7505f942018-08-21 18:10:44 +02002928 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002929 return 0;
2930}
2931
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002932/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002933static int h2_wake(struct connection *conn)
2934{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002935 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002936
2937 return (h2_process(h2c));
2938}
2939
Willy Tarreauea392822017-10-31 10:02:25 +01002940/* Connection timeout management. The principle is that if there's no receipt
2941 * nor sending for a certain amount of time, the connection is closed. If the
2942 * MUX buffer still has lying data or is not allocatable, the connection is
2943 * immediately killed. If it's allocatable and empty, we attempt to send a
2944 * GOAWAY frame.
2945 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002946static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002947{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002948 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002949 int expired = tick_is_expired(t->expire, now_ms);
2950
Willy Tarreau0975f112018-03-29 15:22:59 +02002951 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002952 return t;
2953
Olivier Houchard3f795f72019-04-17 22:51:06 +02002954 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002955
2956 if (!h2c) {
2957 /* resources were already deleted */
2958 return NULL;
2959 }
2960
2961 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002962 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02002963 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01002964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002966 /* don't even try to send a GOAWAY, the buffer is stuck */
2967 h2c->flags |= H2_CF_GOAWAY_FAILED;
2968 }
2969
2970 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002971 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002972 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2973 h2c->flags |= H2_CF_GOAWAY_FAILED;
2974
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002975 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002976 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002977 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002978 b_del(&h2c->mbuf, ret);
2979 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002980 }
2981 }
Willy Tarreauea392822017-10-31 10:02:25 +01002982
Willy Tarreau0975f112018-03-29 15:22:59 +02002983 /* either we can release everything now or it will be done later once
2984 * the last stream closes.
2985 */
2986 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002987 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002988
Willy Tarreauea392822017-10-31 10:02:25 +01002989 return NULL;
2990}
2991
2992
Willy Tarreau62f52692017-10-08 23:01:42 +02002993/*******************************************/
2994/* functions below are used by the streams */
2995/*******************************************/
2996
2997/*
2998 * Attach a new stream to a connection
2999 * (Used for outgoing connections)
3000 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003001static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003002{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003003 struct conn_stream *cs;
3004 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003005 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003006
3007 cs = cs_new(conn);
3008 if (!cs)
3009 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003010 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003011 if (!h2s) {
3012 cs_free(cs);
3013 return NULL;
3014 }
3015 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003016}
3017
Willy Tarreaufafd3982018-11-18 21:29:20 +01003018/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3019 * We have to scan because we may have some orphan streams. It might be
3020 * beneficial to scan backwards from the end to reduce the likeliness to find
3021 * orphans.
3022 */
3023static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3024{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003025 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003026 struct h2s *h2s;
3027 struct eb32_node *node;
3028
3029 node = eb32_first(&h2c->streams_by_id);
3030 while (node) {
3031 h2s = container_of(node, struct h2s, by_id);
3032 if (h2s->cs)
3033 return h2s->cs;
3034 node = eb32_next(node);
3035 }
3036 return NULL;
3037}
3038
Willy Tarreau62f52692017-10-08 23:01:42 +02003039/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003040 * Destroy the mux and the associated connection, if it is no longer used
3041 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003042static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003043{
Christopher Faulet73c12072019-04-08 11:23:22 +02003044 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003045
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003046 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003047 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003048}
3049
3050/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003051 * Detach the stream from the connection and possibly release the connection.
3052 */
3053static void h2_detach(struct conn_stream *cs)
3054{
Willy Tarreau60935142017-10-16 18:11:19 +02003055 struct h2s *h2s = cs->ctx;
3056 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003057 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003058
3059 cs->ctx = NULL;
3060 if (!h2s)
3061 return;
3062
Olivier Houchard998410a2019-04-15 19:23:37 +02003063 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003064 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003065 h2s->send_wait != &h2s->wait_event) {
3066 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3067 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003068 /*
3069 * At this point, the stream_interface is supposed to have called
3070 * h2_unsubscribe(), so the only way there's still a
3071 * subscription that came from the stream_interface (as we
3072 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3073 * without the stream_interface involved) is that we subscribed
3074 * for sending, we woke the tasklet up and removed the
3075 * SUB_RETRY_SEND flag, so the stream_interface would not
3076 * know it has to unsubscribe for send, but the tasklet hasn't
3077 * run yet. Make sure to handle that by explicitely setting
3078 * send_wait to NULL, as nothing else will do it for us.
3079 */
3080 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003081 }
3082
Olivier Houchardf502aca2018-12-14 19:42:40 +01003083 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003084 h2c = h2s->h2c;
3085 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003086 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003087 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3088 !h2_frt_has_too_many_cs(h2c)) {
3089 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003090 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003091 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003092 }
Willy Tarreau60935142017-10-16 18:11:19 +02003093
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003094 /* this stream may be blocked waiting for some data to leave (possibly
3095 * an ES or RST frame), so orphan it in this case.
3096 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003097 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003098 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003099 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003100 return;
3101
Willy Tarreau45f752e2017-10-30 15:44:59 +01003102 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3103 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3104 /* unblock the connection if it was blocked on this
3105 * stream.
3106 */
3107 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3108 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003109 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003110 }
3111
Willy Tarreau71049cc2018-03-28 13:56:39 +02003112 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003113
Olivier Houchard8a786902018-12-15 16:05:40 +01003114 if (h2c->flags & H2_CF_IS_BACK &&
3115 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003116 if (!(h2c->conn->flags &
3117 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3118 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003119 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003120 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3121 h2c->conn->owner = NULL;
3122 if (eb_is_empty(&h2c->streams_by_id)) {
3123 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3124 /* The server doesn't want it, let's kill the connection right away */
3125 h2c->conn->mux->destroy(h2c->conn);
3126 return;
3127 }
3128 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003129 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003130 if (eb_is_empty(&h2c->streams_by_id)) {
3131 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3132 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3133 return;
3134 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003135 /* Never ever allow to reuse a connection from a non-reuse backend */
3136 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3137 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003138 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003139 struct server *srv = objt_server(h2c->conn->target);
3140
3141 if (srv) {
3142 if (h2c->conn->flags & CO_FL_PRIVATE)
3143 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3144 else
3145 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3146 }
3147
3148 }
3149 }
3150 }
3151
Willy Tarreaue323f342018-03-28 13:51:45 +02003152 /* We don't want to close right now unless we're removing the
3153 * last stream, and either the connection is in error, or it
3154 * reached the ID already specified in a GOAWAY frame received
3155 * or sent (as seen by last_sid >= 0).
3156 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003157 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003158 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003159 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003160 }
3161 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003162 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003163 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3164 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003165 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003166 else
3167 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003168 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003169}
3170
Willy Tarreau88bdba32019-05-13 18:17:53 +02003171/* Performs a synchronous or asynchronous shutr(). */
3172static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003173{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003174 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003175 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003176
Willy Tarreauf983d002019-05-14 10:40:21 +02003177 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003178 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003179
Willy Tarreau18059042019-01-31 19:12:48 +01003180 /* a connstream may require us to immediately kill the whole connection
3181 * for example because of a "tcp-request content reject" rule that is
3182 * normally used to limit abuse. In this case we schedule a goaway to
3183 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003184 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003185 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003186 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3187 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3188 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3189 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003190 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3191 /* Nothing was never sent for this stream, so reset with
3192 * REFUSED_STREAM error to let the client retry the
3193 * request.
3194 */
3195 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3196 }
Willy Tarreau18059042019-01-31 19:12:48 +01003197
Willy Tarreau90c32322017-11-24 08:00:30 +01003198 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003199 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003200 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003201
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003202 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003203 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003204 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003205 done:
3206 h2s->flags &= ~H2_SF_WANT_SHUTR;
3207 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003208add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003209 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003210 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003211 if (h2s->flags & H2_SF_BLK_MFCTL) {
3212 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3213 h2s->send_wait = sw;
3214 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3215 h2s->send_wait = sw;
3216 LIST_ADDQ(&h2c->send_list, &h2s->list);
3217 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003218 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003219 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003220 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003221 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003222}
3223
Willy Tarreau88bdba32019-05-13 18:17:53 +02003224/* Performs a synchronous or asynchronous shutw(). */
3225static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003226{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003227 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003228 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003229
Willy Tarreauf983d002019-05-14 10:40:21 +02003230 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003231 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003232
Willy Tarreauf983d002019-05-14 10:40:21 +02003233 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3234 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003235 /* we can cleanly close using an empty data frame only after headers */
3236
3237 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3238 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003239 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003240
3241 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003242 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003243 else
3244 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003245 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003246 /* a connstream may require us to immediately kill the whole connection
3247 * for example because of a "tcp-request content reject" rule that is
3248 * normally used to limit abuse. In this case we schedule a goaway to
3249 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003250 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003251 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003252 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3253 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3254 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3255 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003256 else {
3257 /* Nothing was never sent for this stream, so reset with
3258 * REFUSED_STREAM error to let the client retry the
3259 * request.
3260 */
3261 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3262 }
Willy Tarreau18059042019-01-31 19:12:48 +01003263
Willy Tarreau90c32322017-11-24 08:00:30 +01003264 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003265 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003266 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003267
Willy Tarreau00dd0782018-03-01 16:31:34 +01003268 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003269 }
3270
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003271 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003272 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003273 done:
3274 h2s->flags &= ~H2_SF_WANT_SHUTW;
3275 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003276
3277 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003278 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003279 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003280 if (h2s->flags & H2_SF_BLK_MFCTL) {
3281 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3282 h2s->send_wait = sw;
3283 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3284 h2s->send_wait = sw;
3285 LIST_ADDQ(&h2c->send_list, &h2s->list);
3286 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003287 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003288 /* let the handler know we want to shutw */
3289 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003290 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003291}
3292
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003293/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3294 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3295 * and prevented the last frame from being emitted.
3296 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003297static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3298{
3299 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003300 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003301
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003302 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003303 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003304 h2_do_shutw(h2s);
3305
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003306 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003307 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003308
Willy Tarreau88bdba32019-05-13 18:17:53 +02003309 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003310 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003311 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003312
Willy Tarreau88bdba32019-05-13 18:17:53 +02003313 if (!h2s->cs) {
3314 h2s_destroy(h2s);
3315 if (h2c_is_dead(h2c))
3316 h2_release(h2c);
3317 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003318 }
3319
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003320 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003321}
3322
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003323/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003324static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3325{
3326 struct h2s *h2s = cs->ctx;
3327
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003328 if (cs->flags & CS_FL_KILL_CONN)
3329 h2s->flags |= H2_SF_KILL_CONN;
3330
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003331 if (!mode)
3332 return;
3333
3334 h2_do_shutr(h2s);
3335}
3336
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003337/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003338static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3339{
3340 struct h2s *h2s = cs->ctx;
3341
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003342 if (cs->flags & CS_FL_KILL_CONN)
3343 h2s->flags |= H2_SF_KILL_CONN;
3344
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003345 h2_do_shutw(h2s);
3346}
3347
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003348/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003349 * HTX request or response depending on the connection's side. Returns a
3350 * positive value on success, a negative value on failure, or 0 if it couldn't
3351 * proceed. May report connection errors in h2c->errcode if the frame is
3352 * non-decodable and the connection unrecoverable. In absence of connection
3353 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003354 *
3355 * The function may fold CONTINUATION frames into the initial HEADERS frame
3356 * by removing padding and next frame header, then moving the CONTINUATION
3357 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3358 * leaving a hole between the main frame and the beginning of the next one.
3359 * The possibly remaining incomplete or next frame at the end may be moved
3360 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3361 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3362 *
3363 * A buffer at the beginning of processing may look like this :
3364 *
3365 * ,---.---------.-----.--------------.--------------.------.---.
3366 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3367 * `---^---------^-----^--------------^--------------^------^---'
3368 * | | <-----> | |
3369 * area | dpl | wrap
3370 * |<--------------> |
3371 * | dfl |
3372 * |<-------------------------------------------------->|
3373 * head data
3374 *
3375 * Padding is automatically overwritten when folding, participating to the
3376 * hole size after dfl :
3377 *
3378 * ,---.------------------------.-----.--------------.------.---.
3379 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3380 * `---^------------------------^-----^--------------^------^---'
3381 * | | <-----> | |
3382 * area | hole | wrap
3383 * |<-----------------------> |
3384 * | dfl |
3385 * |<-------------------------------------------------->|
3386 * head data
3387 *
3388 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3389 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3390 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003391 *
3392 * The <flags> field must point to either the stream's flags or to a copy of it
3393 * so that the function can update the following flags :
3394 * - H2_SF_DATA_CLEN when content-length is seen
3395 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3396 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003397 *
3398 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3399 * decoding, in order to detect if we're dealing with a headers or a trailers
3400 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003401 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003402static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02003403{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003404 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003405 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003406 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003407 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003408 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003409 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003410 int flen; // header frame len
3411 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003412 int ret = 0;
3413 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003414 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003415 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003416
Willy Tarreauea18f862018-12-22 20:19:26 +01003417next_frame:
3418 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3419 goto leave; // incomplete input frame
3420
3421 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3422 * this case, we'll try to paste it immediately after the initial
3423 * HEADERS frame payload and kill any possible padding. The initial
3424 * frame's length will be increased to represent the concatenation
3425 * of the two frames. The next frame is read from position <tlen>
3426 * and written at position <flen> (minus padding if some is present).
3427 */
3428 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3429 struct h2_fh hdr;
3430 int clen; // CONTINUATION frame's payload length
3431
3432 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3433 /* no more data, the buffer may be full, either due to
3434 * too large a frame or because of too large a hole that
3435 * we're going to compact at the end.
3436 */
3437 goto leave;
3438 }
3439
3440 if (hdr.ft != H2_FT_CONTINUATION) {
3441 /* RFC7540#6.10: frame of unexpected type */
3442 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3443 goto fail;
3444 }
3445
3446 if (hdr.sid != h2c->dsi) {
3447 /* RFC7540#6.10: frame of different stream */
3448 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3449 goto fail;
3450 }
3451
3452 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3453 /* RFC7540#4.2: invalid frame length */
3454 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3455 goto fail;
3456 }
3457
3458 /* detect when we must stop aggragating frames */
3459 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3460
3461 /* Take as much as we can of the CONTINUATION frame's payload */
3462 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3463 if (clen > hdr.len)
3464 clen = hdr.len;
3465
3466 /* Move the frame's payload over the padding, hole and frame
3467 * header. At least one of hole or dpl is null (see diagrams
3468 * above). The hole moves after the new aggragated frame.
3469 */
3470 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3471 h2c->dfl += clen - h2c->dpl;
3472 hole += h2c->dpl + 9;
3473 h2c->dpl = 0;
3474 goto next_frame;
3475 }
3476
3477 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003478
Willy Tarreau13278b42017-10-13 19:23:14 +02003479 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003480 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003481 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003482 copy = alloc_trash_chunk();
3483 if (!copy) {
3484 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3485 goto fail;
3486 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003487 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3488 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3489 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003490 }
3491
Willy Tarreau13278b42017-10-13 19:23:14 +02003492 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3493 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003494 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003495 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3496 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003497 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003498 }
3499
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003500 if (flen < 5) {
3501 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3502 goto fail;
3503 }
3504
Willy Tarreau13278b42017-10-13 19:23:14 +02003505 hdrs += 5; // stream dep = 4, weight = 1
3506 flen -= 5;
3507 }
3508
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003509 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003510 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003511 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003512 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003513
Willy Tarreau937f7602018-02-26 15:22:17 +01003514 /* we can't retry a failed decompression operation so we must be very
3515 * careful not to take any risks. In practice the output buffer is
3516 * always empty except maybe for trailers, in which case we simply have
3517 * to wait for the upper layer to finish consuming what is available.
3518 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003519
3520 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003521 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003522 if (!htx_is_empty(htx)) {
3523 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003524 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003525 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003526 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003527 if (b_data(rxbuf)) {
3528 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003529 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003530 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003531
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003532 rxbuf->head = 0;
3533 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003534 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003535
Willy Tarreau25919232019-01-03 14:48:18 +01003536 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003537 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3538 sizeof(list)/sizeof(list[0]), tmp);
3539 if (outlen < 0) {
3540 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3541 goto fail;
3542 }
3543
Willy Tarreau25919232019-01-03 14:48:18 +01003544 /* The PACK decompressor was updated, let's update the input buffer and
3545 * the parser's state to commit these changes and allow us to later
3546 * fail solely on the stream if needed.
3547 */
3548 b_del(&h2c->dbuf, h2c->dfl + hole);
3549 h2c->dfl = hole = 0;
3550 h2c->st0 = H2_CS_FRAME_H;
3551
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003552 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003553 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003554
Willy Tarreau88d138e2019-01-02 19:38:14 +01003555 if (*flags & H2_SF_HEADERS_RCVD)
3556 goto trailers;
3557
3558 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003559 if (htx) {
3560 /* HTX mode */
3561 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003562 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003563 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003564 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003565 } else {
3566 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003567 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003568 if (outlen > 0)
3569 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003570 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003571
3572 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003573 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003574 goto fail;
3575 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003576
Willy Tarreau174b06a2018-04-25 18:13:58 +02003577 if (msgf & H2_MSGF_BODY) {
3578 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003579 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003580 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003581 if (htx)
3582 htx->extra = *body_len;
3583 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003584 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003585 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003586 }
3587
Willy Tarreau88d138e2019-01-02 19:38:14 +01003588 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003589 /* indicate that a HEADERS frame was received for this stream, except
3590 * for 1xx responses. For 1xx responses, another HEADERS frame is
3591 * expected.
3592 */
3593 if (!(msgf & H2_MSGF_RSP_1XX))
3594 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003595
Christopher Faulet0b465482019-02-19 15:14:23 +01003596 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003597 /* Mark the end of message, either using EOM in HTX or with the
3598 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003599 * is not set during headers with END_STREAM. For HTX trailers,
3600 * we must not leave an HTX trailers block not followed by an
3601 * EOM block, the two must be atomic. Thus if we fail to emit
3602 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003603 */
3604 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003605 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3606 struct htx_blk *tail = htx_get_tail_blk(htx);
3607
3608 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3609 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003610 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003611 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003612 }
3613 else if (*flags & H2_SF_DATA_CHNK) {
3614 if (!b_putblk(rxbuf, "\r\n", 2))
3615 goto fail;
3616 }
3617 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003618
Willy Tarreau86277d42019-01-02 15:36:11 +01003619 /* success */
3620 ret = 1;
3621
Willy Tarreau68dd9852017-07-03 14:44:26 +02003622 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003623 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003624 * move the remaining data over it.
3625 */
3626 if (hole) {
3627 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3628 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3629 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3630 b_sub(&h2c->dbuf, hole);
3631 }
3632
Willy Tarreau97215ca2019-04-29 10:20:21 +02003633 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003634 /* too large frames */
3635 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003636 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003637 }
3638
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003639 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003640 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003641 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003642 return ret;
3643
Willy Tarreau68dd9852017-07-03 14:44:26 +02003644 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003645 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003646 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003647
3648 trailers:
3649 /* This is the last HEADERS frame hence a trailer */
3650
3651 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3652 /* It's a trailer but it's missing ES flag */
3653 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3654 goto fail;
3655 }
3656
3657 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3658 * block, and when using chunks we must send the 0 CRLF marker. For
3659 * other modes, the trailers are silently dropped.
3660 */
3661 if (htx) {
3662 if (!htx_add_endof(htx, HTX_BLK_EOD))
3663 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003664 if (h2_make_htx_trailers(list, htx) <= 0)
3665 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003666 }
3667 else if (*flags & H2_SF_DATA_CHNK) {
3668 /* Legacy mode with chunked encoding : we must finalize the
3669 * data block message emit the trailing CRLF */
3670 if (!b_putblk(rxbuf, "0\r\n", 3))
3671 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003672
3673 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3674 if (outlen > 0)
3675 b_add(rxbuf, outlen);
3676 else
3677 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003678 }
3679
3680 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003681}
3682
Willy Tarreau454f9052017-10-26 19:40:35 +02003683/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3684 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3685 * in use, a new chunk is emitted for each frame. This is supposed to fit
3686 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3687 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3688 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003689 * parser state is automatically updated. Returns > 0 if it could completely
3690 * send the current frame, 0 if it couldn't complete, in which case
3691 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3692 * DATA frame can return 0 as a valid result). Stream errors are reported in
3693 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3694 * have checked the frame header and ensured that the frame was complete or the
3695 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003696 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003697static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003698{
3699 struct h2c *h2c = h2s->h2c;
3700 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003701 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003702 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003703 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003704 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003705
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003706 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003707
Olivier Houchard638b7992018-08-16 15:41:52 +02003708 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003709 if (!csbuf) {
3710 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003711 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003712 }
3713
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003714try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003715 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003716 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3717 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003718 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003719 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003720
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003721 if (flen > b_data(&h2c->dbuf)) {
3722 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003723 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003724 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003725 }
3726
Willy Tarreaua9b77962019-01-31 07:23:00 +01003727 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003728 block1 = htx_free_data_space(htx);
3729 if (!block1) {
3730 h2c->flags |= H2_CF_DEM_SFULL;
3731 goto fail;
3732 }
3733 if (flen > block1)
3734 flen = block1;
3735
3736 /* here, flen is the max we can copy into the output buffer */
3737 block1 = b_contig_data(&h2c->dbuf, 0);
3738 if (flen > block1)
3739 flen = block1;
3740
3741 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3742 h2c->flags |= H2_CF_DEM_SFULL;
3743 goto fail;
3744 }
3745
3746 b_del(&h2c->dbuf, flen);
3747 h2c->dfl -= flen;
3748 h2c->rcvd_c += flen;
3749 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003750
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003751 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003752 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003753 htx->extra = h2s->body_len;
3754 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003755 goto try_again;
3756 }
3757 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 /* it doesn't fit and the buffer is fragmented,
3759 * so let's defragment it and try again.
3760 */
3761 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003762 }
3763
Willy Tarreaueba10f22018-04-25 20:44:22 +02003764 /* chunked-encoding requires more room */
3765 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003766 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003767 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3768 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3769 (chklen < 1048576) ? 4 : 8;
3770 chklen += 4; // CRLF, CRLF
3771 }
3772
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003773 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003774 if (flen + chklen > b_room(csbuf)) {
3775 if (chklen >= b_room(csbuf)) {
3776 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003777 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003778 }
3779 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003780 }
3781
3782 if (h2s->flags & H2_SF_DATA_CHNK) {
3783 /* emit the chunk size */
3784 unsigned int chksz = flen;
3785 char str[10];
3786 char *beg;
3787
3788 beg = str + sizeof(str);
3789 *--beg = '\n';
3790 *--beg = '\r';
3791 do {
3792 *--beg = hextab[chksz & 0xF];
3793 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003794 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003795 }
3796
Willy Tarreau454f9052017-10-26 19:40:35 +02003797 /* Block1 is the length of the first block before the buffer wraps,
3798 * block2 is the optional second block to reach the end of the frame.
3799 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003800 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003801 if (block1 > flen)
3802 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003803 block2 = flen - block1;
3804
3805 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003806 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003807
3808 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003809 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003810
Willy Tarreaueba10f22018-04-25 20:44:22 +02003811 if (h2s->flags & H2_SF_DATA_CHNK) {
3812 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003813 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003814 }
3815
Willy Tarreau454f9052017-10-26 19:40:35 +02003816 /* now mark the input data as consumed (will be deleted from the buffer
3817 * by the caller when seeing FRAME_A after sending the window update).
3818 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003819 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003820 h2c->dfl -= flen;
3821 h2c->rcvd_c += flen;
3822 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3823
Willy Tarreau1915ca22019-01-24 11:49:37 +01003824 if (h2s->flags & H2_SF_DATA_CLEN)
3825 h2s->body_len -= flen;
3826
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003827 if (h2c->dfl > h2c->dpl) {
3828 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003829 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003830 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003831 }
3832
Willy Tarreau4a28da12018-01-04 14:41:00 +01003833 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003834 /* here we're done with the frame, all the payload (except padding) was
3835 * transferred.
3836 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003837
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003838 if (h2c->dff & H2_F_DATA_END_STREAM) {
3839 if (htx) {
3840 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3841 h2c->flags |= H2_CF_DEM_SFULL;
3842 goto fail;
3843 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003844 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003845 else if (h2s->flags & H2_SF_DATA_CHNK) {
3846 /* emit the trailing 0 CRLF CRLF */
3847 if (b_room(csbuf) < 5) {
3848 h2c->flags |= H2_CF_DEM_SFULL;
3849 goto fail;
3850 }
3851 chklen += 5;
3852 b_putblk(csbuf, "0\r\n\r\n", 5);
3853 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003854 }
3855
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003856 h2c->rcvd_c += h2c->dpl;
3857 h2c->rcvd_s += h2c->dpl;
3858 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003859 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003860 if (htx)
3861 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003862 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003863 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003864 if (htx)
3865 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003866 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003867}
3868
Willy Tarreau5dd17352018-06-14 13:33:30 +02003869/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3870 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3871 * number of bytes sent. The caller must check the stream's status to detect
3872 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003874static 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 +02003875{
3876 struct http_hdr list[MAX_HTTP_HDR];
3877 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003878 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003879 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003880 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003881 int es_now = 0;
3882 int ret = 0;
3883 int hdr;
3884
3885 if (h2c_mux_busy(h2c, h2s)) {
3886 h2s->flags |= H2_SF_BLK_MBUSY;
3887 return 0;
3888 }
3889
Willy Tarreau44e973f2018-03-01 17:49:30 +01003890 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 h2c->flags |= H2_CF_MUX_MALLOC;
3892 h2s->flags |= H2_SF_BLK_MROOM;
3893 return 0;
3894 }
3895
3896 /* First, try to parse the H1 response and index it into <list>.
3897 * NOTE! Since it comes from haproxy, we *know* that a response header
3898 * block does not wrap and we can safely read it this way without
3899 * having to realign the buffer.
3900 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003901 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003902 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003904 /* incomplete or invalid response, this is abnormal coming from
3905 * haproxy and may only result in a bad errorfile or bad Lua code
3906 * so that won't be fixed, raise an error now.
3907 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003908 * FIXME: we should instead add the ability to only return a
3909 * 502 bad gateway. But in theory this is not supposed to
3910 * happen.
3911 */
3912 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3913 ret = 0;
3914 goto end;
3915 }
3916
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003917 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003918
3919 /* certain statuses have no body or an empty one, regardless of
3920 * what the headers say.
3921 */
3922 if (sl.st.status >= 100 && sl.st.status < 200) {
3923 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3924 h1m->curr_len = h1m->body_len = 0;
3925 }
3926 else if (sl.st.status == 204 || sl.st.status == 304) {
3927 /* no contents, claim c-len is present and set to zero */
3928 h1m->flags &= ~H1_MF_CHNK;
3929 h1m->flags |= H1_MF_CLEN;
3930 h1m->curr_len = h1m->body_len = 0;
3931 }
3932
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003933 chunk_reset(&outbuf);
3934
3935 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003936 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003937 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003938 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003939
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003940 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003941 break;
3942 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003943 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003944 }
3945
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003946 if (outbuf.size < 9)
3947 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003948
3949 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003950 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3951 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3952 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003953
3954 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003955 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003956 /* this is an unparsable response */
3957 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3958 ret = 0;
3959 goto end;
3960 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003961
3962 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003963 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003964 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003965 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003966 }
3967
3968 /* encode all headers, stop at empty name */
3969 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003970 /* these ones do not exist in H2 and must be dropped. */
3971 if (isteq(list[hdr].n, ist("connection")) ||
3972 isteq(list[hdr].n, ist("proxy-connection")) ||
3973 isteq(list[hdr].n, ist("keep-alive")) ||
3974 isteq(list[hdr].n, ist("upgrade")) ||
3975 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003976 continue;
3977
3978 if (isteq(list[hdr].n, ist("")))
3979 break; // end
3980
3981 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3982 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003983 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003984 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003985 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986 }
3987 }
3988
3989 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003990 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003991 es_now = 1;
3992
3993 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003994 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003995
3996 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003997 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003998
3999 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004000 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004001
4002 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004003 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004004 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004005
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004006 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004007 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004008 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004009
Willy Tarreau801250e2018-09-11 11:45:04 +02004010 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004011 h2s->flags |= H2_SF_ES_SENT;
4012 if (h2s->st == H2_SS_OPEN)
4013 h2s->st = H2_SS_HLOC;
4014 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004015 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004016 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004017 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004018 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004019 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004020 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004021 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004022 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004023 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004024
4025 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004026
4027 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004028 //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, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004029 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004030 full:
4031 h1m_init_res(h1m);
4032 h1m->err_pos = -1; // don't care about errors on the response path
4033 h2c->flags |= H2_CF_MUX_MFULL;
4034 h2s->flags |= H2_SF_BLK_MROOM;
4035 ret = 0;
4036 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004037}
4038
Willy Tarreau5dd17352018-06-14 13:33:30 +02004039/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4040 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4041 * the number of bytes sent. The caller must check the stream's status to
4042 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004044static 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 +02004045{
4046 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004047 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004048 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004050 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004051 int es_now = 0;
4052 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004053 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004054 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055
4056 if (h2c_mux_busy(h2c, h2s)) {
4057 h2s->flags |= H2_SF_BLK_MBUSY;
4058 goto end;
4059 }
4060
Willy Tarreau44e973f2018-03-01 17:49:30 +01004061 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 h2c->flags |= H2_CF_MUX_MALLOC;
4063 h2s->flags |= H2_SF_BLK_MROOM;
4064 goto end;
4065 }
4066
4067 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004068 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004069 goto end;
4070
4071 chunk_reset(&outbuf);
4072
4073 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004074 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004075 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004076 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004077
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004078 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004079 break;
4080 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004081 /* If there are pending data in the output buffer, and we have
4082 * less than 1/4 of the mbuf's size and everything fits, we'll
4083 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4084 * is full and wait, to save some slow realign calls.
4085 */
4086 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4087 h2c->flags |= H2_CF_MUX_MFULL;
4088 h2s->flags |= H2_SF_BLK_MROOM;
4089 goto end;
4090 }
4091
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004092 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004093 }
4094
4095 if (outbuf.size < 9) {
4096 h2c->flags |= H2_CF_MUX_MFULL;
4097 h2s->flags |= H2_SF_BLK_MROOM;
4098 goto end;
4099 }
4100
4101 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004102 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4103 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4104 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105
4106 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4107 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004108 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004109 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004110 break;
4111 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004112 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113 if ((long long)size > h1m->curr_len)
4114 size = h1m->curr_len;
4115 break;
4116 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004117 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004118 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004119 if (!ret)
4120 goto end;
4121
4122 if (ret < 0) {
4123 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004124 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004125 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4126 goto end;
4127 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004128 max -= ret;
4129 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004130 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004131 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004132 }
4133
Willy Tarreau801250e2018-09-11 11:45:04 +02004134 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004135 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004136 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004137 if (!ret)
4138 goto end;
4139
4140 if (ret < 0) {
4141 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004142 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004143 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4144 goto end;
4145 }
4146
4147 size = chunk;
4148 h1m->curr_len = chunk;
4149 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004150 max -= ret;
4151 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004152 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004153 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004154 if (!size)
4155 goto send_empty;
4156 }
4157
4158 /* in MSG_DATA state, continue below */
4159 size = h1m->curr_len;
4160 break;
4161 }
4162
4163 /* we have in <size> the exact number of bytes we need to copy from
4164 * the H1 buffer. We need to check this against the connection's and
4165 * the stream's send windows, and to ensure that this fits in the max
4166 * frame size and in the buffer's available space minus 9 bytes (for
4167 * the frame header). The connection's flow control is applied last so
4168 * that we can use a separate list of streams which are immediately
4169 * unblocked on window opening. Note: we don't implement padding.
4170 */
4171
Willy Tarreau5dd17352018-06-14 13:33:30 +02004172 if (size > max)
4173 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174
4175 if (size > h2s->mws)
4176 size = h2s->mws;
4177
4178 if (size <= 0) {
4179 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004180 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004181 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004182 goto end;
4183 }
4184
4185 if (h2c->mfs && size > h2c->mfs)
4186 size = h2c->mfs;
4187
4188 if (size + 9 > outbuf.size) {
4189 /* we have an opportunity for enlarging the too small
4190 * available space, let's try.
4191 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004192 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004193 goto realign_again;
4194 size = outbuf.size - 9;
4195 }
4196
4197 if (size <= 0) {
4198 h2c->flags |= H2_CF_MUX_MFULL;
4199 h2s->flags |= H2_SF_BLK_MROOM;
4200 goto end;
4201 }
4202
4203 if (size > h2c->mws)
4204 size = h2c->mws;
4205
4206 if (size <= 0) {
4207 h2s->flags |= H2_SF_BLK_MFCTL;
4208 goto end;
4209 }
4210
4211 /* copy whatever we can */
4212 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004213 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004214 if (ret == 1)
4215 len2 = 0;
4216
4217 if (!ret || len1 + len2 < size) {
4218 /* FIXME: must normally never happen */
4219 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4220 goto end;
4221 }
4222
4223 /* limit len1/len2 to size */
4224 if (len1 + len2 > size) {
4225 int sub = len1 + len2 - size;
4226
4227 if (len2 > sub)
4228 len2 -= sub;
4229 else {
4230 sub -= len2;
4231 len2 = 0;
4232 len1 -= sub;
4233 }
4234 }
4235
4236 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004237 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004238 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004239 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004240
4241 send_empty:
4242 /* we may need to add END_STREAM */
4243 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4244 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004245 *
4246 * FIXME: what we do here is not correct because we send end_stream
4247 * before knowing if we'll have to send a HEADERS frame for the
4248 * trailers. More importantly we're not consuming the trailing CRLF
4249 * after the end of trailers, so it will be left to the caller to
4250 * eat it. The right way to do it would be to measure trailers here
4251 * and to send ES only if there are no trailers.
4252 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004253 */
4254 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004255 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004256 es_now = 1;
4257
4258 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004259 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004260
4261 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004262 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004263
4264 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004265 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004266
4267 /* consume incoming H1 response */
4268 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004269 max -= size;
4270 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004271 total += size;
4272 h1m->curr_len -= size;
4273 h2s->mws -= size;
4274 h2c->mws -= size;
4275
4276 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004277 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004278 goto new_frame;
4279 }
4280 }
4281
4282 if (es_now) {
4283 if (h2s->st == H2_SS_OPEN)
4284 h2s->st = H2_SS_HLOC;
4285 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004286 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004287
Willy Tarreau35a62702018-02-27 15:37:25 +01004288 if (!(h1m->flags & H1_MF_CHNK)) {
4289 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004290 total += max;
4291 ofs += max;
4292 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004293
Willy Tarreau801250e2018-09-11 11:45:04 +02004294 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004295 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004296
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004297 h2s->flags |= H2_SF_ES_SENT;
4298 }
4299
4300 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004301 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, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004302 return total;
4303}
4304
Willy Tarreau115e83b2018-12-01 19:17:53 +01004305/* Try to send a HEADERS frame matching HTX response present in HTX message
4306 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4307 * must check the stream's status to detect any error which might have happened
4308 * subsequently to a successful send. The htx blocks are automatically removed
4309 * from the message. The htx message is assumed to be valid since produced from
4310 * the internal code, hence it contains a start line, an optional series of
4311 * header blocks and an end of header, otherwise an invalid frame could be
4312 * emitted and the resulting htx message could be left in an inconsistent state.
4313 */
4314static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4315{
4316 struct http_hdr list[MAX_HTTP_HDR];
4317 struct h2c *h2c = h2s->h2c;
4318 struct htx_blk *blk;
4319 struct htx_blk *blk_end;
4320 struct buffer outbuf;
4321 struct htx_sl *sl;
4322 enum htx_blk_type type;
4323 int es_now = 0;
4324 int ret = 0;
4325 int hdr;
4326 int idx;
4327
4328 if (h2c_mux_busy(h2c, h2s)) {
4329 h2s->flags |= H2_SF_BLK_MBUSY;
4330 return 0;
4331 }
4332
4333 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4334 h2c->flags |= H2_CF_MUX_MALLOC;
4335 h2s->flags |= H2_SF_BLK_MROOM;
4336 return 0;
4337 }
4338
4339 /* determine the first block which must not be deleted, blk_end may
4340 * be NULL if all blocks have to be deleted.
4341 */
4342 idx = htx_get_head(htx);
4343 blk_end = NULL;
4344 while (idx != -1) {
4345 type = htx_get_blk_type(htx_get_blk(htx, idx));
4346 idx = htx_get_next(htx, idx);
4347 if (type == HTX_BLK_EOH) {
4348 if (idx != -1)
4349 blk_end = htx_get_blk(htx, idx);
4350 break;
4351 }
4352 }
4353
4354 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004355 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004356 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004357 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004358 if (h2s->status < 100 || h2s->status > 999)
4359 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004360
4361 /* and the rest of the headers, that we dump starting at header 0 */
4362 hdr = 0;
4363
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004364 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004365 while ((idx = htx_get_next(htx, idx)) != -1) {
4366 blk = htx_get_blk(htx, idx);
4367 type = htx_get_blk_type(blk);
4368
4369 if (type == HTX_BLK_UNUSED)
4370 continue;
4371
4372 if (type != HTX_BLK_HDR)
4373 break;
4374
4375 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4376 goto fail;
4377
4378 list[hdr].n = htx_get_blk_name(htx, blk);
4379 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004380 hdr++;
4381 }
4382
4383 /* marker for end of headers */
4384 list[hdr].n = ist("");
4385
4386 if (h2s->status == 204 || h2s->status == 304) {
4387 /* no contents, claim c-len is present and set to zero */
4388 es_now = 1;
4389 }
4390
4391 chunk_reset(&outbuf);
4392
4393 while (1) {
4394 outbuf.area = b_tail(&h2c->mbuf);
4395 outbuf.size = b_contig_space(&h2c->mbuf);
4396 outbuf.data = 0;
4397
4398 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4399 break;
4400 realign_again:
4401 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4402 }
4403
4404 if (outbuf.size < 9)
4405 goto full;
4406
4407 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4408 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4409 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4410 outbuf.data = 9;
4411
4412 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004413 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004414 if (b_space_wraps(&h2c->mbuf))
4415 goto realign_again;
4416 goto full;
4417 }
4418
4419 /* encode all headers, stop at empty name */
4420 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4421 /* these ones do not exist in H2 and must be dropped. */
4422 if (isteq(list[hdr].n, ist("connection")) ||
4423 isteq(list[hdr].n, ist("proxy-connection")) ||
4424 isteq(list[hdr].n, ist("keep-alive")) ||
4425 isteq(list[hdr].n, ist("upgrade")) ||
4426 isteq(list[hdr].n, ist("transfer-encoding")))
4427 continue;
4428
4429 if (isteq(list[hdr].n, ist("")))
4430 break; // end
4431
4432 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4433 /* output full */
4434 if (b_space_wraps(&h2c->mbuf))
4435 goto realign_again;
4436 goto full;
4437 }
4438 }
4439
Christopher Faulet0b465482019-02-19 15:14:23 +01004440 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004441 * FIXME: we should also set it when we know for sure that the
4442 * content-length is zero as well as on 204/304
4443 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004444 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4445 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004446 es_now = 1;
4447
Willy Tarreau927b88b2019-03-04 08:03:25 +01004448 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004449 es_now = 1;
4450
4451 /* update the frame's size */
4452 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4453
4454 if (es_now)
4455 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4456
4457 /* commit the H2 response */
4458 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004459
4460 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4461 * 1xx responses, another HEADERS frame is expected.
4462 */
4463 if (h2s->status >= 200 || h2s->status == 101)
4464 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004465
Willy Tarreau115e83b2018-12-01 19:17:53 +01004466 if (es_now) {
4467 h2s->flags |= H2_SF_ES_SENT;
4468 if (h2s->st == H2_SS_OPEN)
4469 h2s->st = H2_SS_HLOC;
4470 else
4471 h2s_close(h2s);
4472 }
4473
4474 /* OK we could properly deliver the response */
4475
4476 /* remove all header blocks including the EOH and compute the
4477 * corresponding size.
4478 *
4479 * FIXME: We should remove everything when es_now is set.
4480 */
4481 ret = 0;
4482 idx = htx_get_head(htx);
4483 blk = htx_get_blk(htx, idx);
4484 while (blk != blk_end) {
4485 ret += htx_get_blksz(blk);
4486 blk = htx_remove_blk(htx, blk);
4487 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004488
4489 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4490 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004491 end:
4492 return ret;
4493 full:
4494 h2c->flags |= H2_CF_MUX_MFULL;
4495 h2s->flags |= H2_SF_BLK_MROOM;
4496 ret = 0;
4497 goto end;
4498 fail:
4499 /* unparsable HTX messages, too large ones to be produced in the local
4500 * list etc go here (unrecoverable errors).
4501 */
4502 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4503 ret = 0;
4504 goto end;
4505}
4506
Willy Tarreau80739692018-10-05 11:35:57 +02004507/* Try to send a HEADERS frame matching HTX request present in HTX message
4508 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4509 * must check the stream's status to detect any error which might have happened
4510 * subsequently to a successful send. The htx blocks are automatically removed
4511 * from the message. The htx message is assumed to be valid since produced from
4512 * the internal code, hence it contains a start line, an optional series of
4513 * header blocks and an end of header, otherwise an invalid frame could be
4514 * emitted and the resulting htx message could be left in an inconsistent state.
4515 */
4516static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4517{
4518 struct http_hdr list[MAX_HTTP_HDR];
4519 struct h2c *h2c = h2s->h2c;
4520 struct htx_blk *blk;
4521 struct htx_blk *blk_end;
4522 struct buffer outbuf;
4523 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004524 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004525 enum htx_blk_type type;
4526 int es_now = 0;
4527 int ret = 0;
4528 int hdr;
4529 int idx;
4530
4531 if (h2c_mux_busy(h2c, h2s)) {
4532 h2s->flags |= H2_SF_BLK_MBUSY;
4533 return 0;
4534 }
4535
4536 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4537 h2c->flags |= H2_CF_MUX_MALLOC;
4538 h2s->flags |= H2_SF_BLK_MROOM;
4539 return 0;
4540 }
4541
4542 /* determine the first block which must not be deleted, blk_end may
4543 * be NULL if all blocks have to be deleted.
4544 */
4545 idx = htx_get_head(htx);
4546 blk_end = NULL;
4547 while (idx != -1) {
4548 type = htx_get_blk_type(htx_get_blk(htx, idx));
4549 idx = htx_get_next(htx, idx);
4550 if (type == HTX_BLK_EOH) {
4551 if (idx != -1)
4552 blk_end = htx_get_blk(htx, idx);
4553 break;
4554 }
4555 }
4556
4557 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004558 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004559 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004560 meth = htx_sl_req_meth(sl);
4561 path = htx_sl_req_uri(sl);
4562
4563 /* and the rest of the headers, that we dump starting at header 0 */
4564 hdr = 0;
4565
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004566 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004567 while ((idx = htx_get_next(htx, idx)) != -1) {
4568 blk = htx_get_blk(htx, idx);
4569 type = htx_get_blk_type(blk);
4570
4571 if (type == HTX_BLK_UNUSED)
4572 continue;
4573
4574 if (type != HTX_BLK_HDR)
4575 break;
4576
4577 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4578 goto fail;
4579
4580 list[hdr].n = htx_get_blk_name(htx, blk);
4581 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004582 hdr++;
4583 }
4584
4585 /* marker for end of headers */
4586 list[hdr].n = ist("");
4587
4588 chunk_reset(&outbuf);
4589
4590 while (1) {
4591 outbuf.area = b_tail(&h2c->mbuf);
4592 outbuf.size = b_contig_space(&h2c->mbuf);
4593 outbuf.data = 0;
4594
4595 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4596 break;
4597 realign_again:
4598 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4599 }
4600
4601 if (outbuf.size < 9)
4602 goto full;
4603
4604 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4605 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4606 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4607 outbuf.data = 9;
4608
4609 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004610 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004611 if (b_space_wraps(&h2c->mbuf))
4612 goto realign_again;
4613 goto full;
4614 }
4615
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004616 /* RFC7540 #8.3: the CONNECT method must have :
4617 * - :authority set to the URI part (host:port)
4618 * - :method set to CONNECT
4619 * - :scheme and :path omitted
4620 */
4621 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4622 /* encode the scheme which is always "https" (or 0x86 for "http") */
4623 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4624 /* output full */
4625 if (b_space_wraps(&h2c->mbuf))
4626 goto realign_again;
4627 goto full;
4628 }
Willy Tarreau80739692018-10-05 11:35:57 +02004629
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004630 /* encode the path, which necessarily is the second one */
4631 if (!hpack_encode_path(&outbuf, path)) {
4632 /* output full */
4633 if (b_space_wraps(&h2c->mbuf))
4634 goto realign_again;
4635 goto full;
4636 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004637
4638 /* look for the Host header and place it in :authority */
4639 auth = ist2(NULL, 0);
4640 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4641 if (isteq(list[hdr].n, ist("")))
4642 break; // end
4643
4644 if (isteq(list[hdr].n, ist("host"))) {
4645 auth = list[hdr].v;
4646 break;
4647 }
4648 }
4649 }
4650 else {
4651 /* for CONNECT, :authority is taken from the path */
4652 auth = path;
4653 }
4654
4655 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4656 /* output full */
4657 if (b_space_wraps(&h2c->mbuf))
4658 goto realign_again;
4659 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004660 }
4661
4662 /* encode all headers, stop at empty name */
4663 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4664 /* these ones do not exist in H2 and must be dropped. */
4665 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004666 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004667 isteq(list[hdr].n, ist("proxy-connection")) ||
4668 isteq(list[hdr].n, ist("keep-alive")) ||
4669 isteq(list[hdr].n, ist("upgrade")) ||
4670 isteq(list[hdr].n, ist("transfer-encoding")))
4671 continue;
4672
4673 if (isteq(list[hdr].n, ist("")))
4674 break; // end
4675
4676 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4677 /* output full */
4678 if (b_space_wraps(&h2c->mbuf))
4679 goto realign_again;
4680 goto full;
4681 }
4682 }
4683
4684 /* we may need to add END_STREAM if we have no body :
4685 * - request already closed, or :
4686 * - no transfer-encoding, and :
4687 * - no content-length or content-length:0
4688 * Fixme: this doesn't take into account CONNECT requests.
4689 */
4690 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4691 es_now = 1;
4692
4693 if (sl->flags & HTX_SL_F_BODYLESS)
4694 es_now = 1;
4695
Willy Tarreau927b88b2019-03-04 08:03:25 +01004696 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004697 es_now = 1;
4698
4699 /* update the frame's size */
4700 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4701
4702 if (es_now)
4703 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4704
4705 /* commit the H2 response */
4706 b_add(&h2c->mbuf, outbuf.data);
4707 h2s->flags |= H2_SF_HEADERS_SENT;
4708 h2s->st = H2_SS_OPEN;
4709
Willy Tarreau80739692018-10-05 11:35:57 +02004710 if (es_now) {
4711 // trim any possibly pending data (eg: inconsistent content-length)
4712 h2s->flags |= H2_SF_ES_SENT;
4713 h2s->st = H2_SS_HLOC;
4714 }
4715
4716 /* remove all header blocks including the EOH and compute the
4717 * corresponding size.
4718 *
4719 * FIXME: We should remove everything when es_now is set.
4720 */
4721 ret = 0;
4722 idx = htx_get_head(htx);
4723 blk = htx_get_blk(htx, idx);
4724 while (blk != blk_end) {
4725 ret += htx_get_blksz(blk);
4726 blk = htx_remove_blk(htx, blk);
4727 }
4728
4729 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4730 htx_remove_blk(htx, blk_end);
4731
4732 end:
4733 return ret;
4734 full:
4735 h2c->flags |= H2_CF_MUX_MFULL;
4736 h2s->flags |= H2_SF_BLK_MROOM;
4737 ret = 0;
4738 goto end;
4739 fail:
4740 /* unparsable HTX messages, too large ones to be produced in the local
4741 * list etc go here (unrecoverable errors).
4742 */
4743 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4744 ret = 0;
4745 goto end;
4746}
4747
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004748/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004749 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4750 * caller must check the stream's status to detect any error which might have
4751 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004752 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4753 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004754static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004755{
4756 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004757 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004758 struct buffer outbuf;
4759 size_t total = 0;
4760 int es_now = 0;
4761 int bsize; /* htx block size */
4762 int fsize; /* h2 frame size */
4763 struct htx_blk *blk;
4764 enum htx_blk_type type;
4765 int idx;
4766
4767 if (h2c_mux_busy(h2c, h2s)) {
4768 h2s->flags |= H2_SF_BLK_MBUSY;
4769 goto end;
4770 }
4771
4772 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4773 h2c->flags |= H2_CF_MUX_MALLOC;
4774 h2s->flags |= H2_SF_BLK_MROOM;
4775 goto end;
4776 }
4777
Willy Tarreau98de12a2018-12-12 07:03:00 +01004778 htx = htx_from_buf(buf);
4779
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004780 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4781 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4782 * the caller to handle.
4783 */
4784
4785 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004786 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004787 goto end;
4788
4789 idx = htx_get_head(htx);
4790 blk = htx_get_blk(htx, idx);
4791 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4792 bsize = htx_get_blksz(blk);
4793 fsize = bsize;
4794
4795 if (type == HTX_BLK_EOD) {
4796 /* if we have an EOD, we're dealing with chunked data. We may
4797 * have a set of trailers after us that the caller will want to
4798 * deal with. Let's simply remove the EOD and return.
4799 */
4800 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004801 total++; // EOD counts as one byte
4802 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004803 goto end;
4804 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004805 else if (type == HTX_BLK_EOM) {
4806 if (h2s->flags & H2_SF_ES_SENT) {
4807 /* ES already sent */
4808 htx_remove_blk(htx, blk);
4809 total++; // EOM counts as one byte
4810 count--;
4811 goto end;
4812 }
4813 }
4814 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004815 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004816
4817 /* Perform some optimizations to reduce the number of buffer copies.
4818 * First, if the mux's buffer is empty and the htx area contains
4819 * exactly one data block of the same size as the requested count, and
4820 * this count fits within the frame size, the stream's window size, and
4821 * the connection's window size, then it's possible to simply swap the
4822 * caller's buffer with the mux's output buffer and adjust offsets and
4823 * length to match the entire DATA HTX block in the middle. In this
4824 * case we perform a true zero-copy operation from end-to-end. This is
4825 * the situation that happens all the time with large files. Second, if
4826 * this is not possible, but the mux's output buffer is empty, we still
4827 * have an opportunity to avoid the copy to the intermediary buffer, by
4828 * making the intermediary buffer's area point to the output buffer's
4829 * area. In this case we want to skip the HTX header to make sure that
4830 * copies remain aligned and that this operation remains possible all
4831 * the time. This goes for headers, data blocks and any data extracted
4832 * from the HTX blocks.
4833 */
4834 if (unlikely(fsize == count &&
4835 htx->used == 1 && type == HTX_BLK_DATA &&
4836 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4837 void *old_area = h2c->mbuf.area;
4838
4839 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004840 /* Too bad there are data left there. We're willing to memcpy/memmove
4841 * up to 1/4 of the buffer, which means that it's OK to copy a large
4842 * frame into a buffer containing few data if it needs to be realigned,
4843 * and that it's also OK to copy few data without realigning. Otherwise
4844 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004845 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004846 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4847 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4848 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004849 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004850
Willy Tarreau98de12a2018-12-12 07:03:00 +01004851 h2c->flags |= H2_CF_MUX_MFULL;
4852 h2s->flags |= H2_SF_BLK_MROOM;
4853 goto end;
4854 }
4855
4856 /* map an H2 frame to the HTX block so that we can put the
4857 * frame header there.
4858 */
4859 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004860 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004861 h2c->mbuf.data = fsize + 9;
4862 outbuf.area = b_head(&h2c->mbuf);
4863
4864 /* prepend an H2 DATA frame header just before the DATA block */
4865 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4866 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4867 h2_set_frame_size(outbuf.area, fsize);
4868
4869 /* update windows */
4870 h2s->mws -= fsize;
4871 h2c->mws -= fsize;
4872
4873 /* and exchange with our old area */
4874 buf->area = old_area;
4875 buf->data = buf->head = 0;
4876 total += fsize;
4877 goto end;
4878 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004879
Willy Tarreau98de12a2018-12-12 07:03:00 +01004880 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004881 /* for DATA and EOM we'll have to emit a frame, even if empty */
4882
4883 while (1) {
4884 outbuf.area = b_tail(&h2c->mbuf);
4885 outbuf.size = b_contig_space(&h2c->mbuf);
4886 outbuf.data = 0;
4887
4888 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4889 break;
4890 realign_again:
4891 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4892 }
4893
4894 if (outbuf.size < 9) {
4895 h2c->flags |= H2_CF_MUX_MFULL;
4896 h2s->flags |= H2_SF_BLK_MROOM;
4897 goto end;
4898 }
4899
4900 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4901 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4902 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4903 outbuf.data = 9;
4904
4905 /* we have in <fsize> the exact number of bytes we need to copy from
4906 * the HTX buffer. We need to check this against the connection's and
4907 * the stream's send windows, and to ensure that this fits in the max
4908 * frame size and in the buffer's available space minus 9 bytes (for
4909 * the frame header). The connection's flow control is applied last so
4910 * that we can use a separate list of streams which are immediately
4911 * unblocked on window opening. Note: we don't implement padding.
4912 */
4913
4914 /* EOM is presented with bsize==1 but would lead to the emission of an
4915 * empty frame, thus we force it to zero here.
4916 */
4917 if (type == HTX_BLK_EOM)
4918 bsize = fsize = 0;
4919
4920 if (!fsize)
4921 goto send_empty;
4922
4923 if (h2s->mws <= 0) {
4924 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004925 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004926 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004927 goto end;
4928 }
4929
Willy Tarreauee573762018-12-04 15:25:57 +01004930 if (fsize > count)
4931 fsize = count;
4932
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004933 if (fsize > h2s->mws)
4934 fsize = h2s->mws; // >0
4935
4936 if (h2c->mfs && fsize > h2c->mfs)
4937 fsize = h2c->mfs; // >0
4938
4939 if (fsize + 9 > outbuf.size) {
4940 /* we have an opportunity for enlarging the too small
4941 * available space, let's try.
4942 * FIXME: is this really interesting to do? Maybe we'll
4943 * spend lots of time realigning instead of using two
4944 * frames.
4945 */
4946 if (b_space_wraps(&h2c->mbuf))
4947 goto realign_again;
4948 fsize = outbuf.size - 9;
4949
4950 if (fsize <= 0) {
4951 /* no need to send an empty frame here */
4952 h2c->flags |= H2_CF_MUX_MFULL;
4953 h2s->flags |= H2_SF_BLK_MROOM;
4954 goto end;
4955 }
4956 }
4957
4958 if (h2c->mws <= 0) {
4959 h2s->flags |= H2_SF_BLK_MFCTL;
4960 goto end;
4961 }
4962
4963 if (fsize > h2c->mws)
4964 fsize = h2c->mws;
4965
4966 /* now let's copy this this into the output buffer */
4967 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004968 h2s->mws -= fsize;
4969 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004970 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004971
4972 send_empty:
4973 /* update the frame's size */
4974 h2_set_frame_size(outbuf.area, fsize);
4975
4976 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4977 * meeting EOM. We should optimize this later.
4978 */
4979 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004980 total++; // EOM counts as one byte
4981 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004982 es_now = 1;
4983 }
4984
4985 if (es_now)
4986 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4987
4988 /* commit the H2 response */
4989 b_add(&h2c->mbuf, fsize + 9);
4990
4991 /* consume incoming HTX block, including EOM */
4992 total += fsize;
4993 if (fsize == bsize) {
4994 htx_remove_blk(htx, blk);
4995 if (fsize)
4996 goto new_frame;
4997 } else {
4998 /* we've truncated this block */
4999 htx_cut_data_blk(htx, blk, fsize);
5000 }
5001
5002 if (es_now) {
5003 if (h2s->st == H2_SS_OPEN)
5004 h2s->st = H2_SS_HLOC;
5005 else
5006 h2s_close(h2s);
5007
5008 h2s->flags |= H2_SF_ES_SENT;
5009 }
5010
5011 end:
5012 return total;
5013}
5014
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005015/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5016 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5017 * processed. The caller must check the stream's status to detect any error
5018 * which might have happened subsequently to a successful send. The htx blocks
5019 * are automatically removed from the message. The htx message is assumed to be
5020 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005021 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005022 * as a single frame. The ES flag is always set.
5023 */
5024static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5025{
5026 struct http_hdr list[MAX_HTTP_HDR];
5027 struct h2c *h2c = h2s->h2c;
5028 struct htx_blk *blk;
5029 struct htx_blk *blk_end;
5030 struct buffer outbuf;
5031 struct h1m h1m;
5032 enum htx_blk_type type;
5033 uint32_t size;
5034 int ret = 0;
5035 int hdr;
5036 int idx;
5037 void *start;
5038
5039 if (h2c_mux_busy(h2c, h2s)) {
5040 h2s->flags |= H2_SF_BLK_MBUSY;
5041 goto end;
5042 }
5043
5044 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5045 h2c->flags |= H2_CF_MUX_MALLOC;
5046 h2s->flags |= H2_SF_BLK_MROOM;
5047 goto end;
5048 }
5049
5050 /* The principle is that we parse each and every trailers block using
5051 * the H1 headers parser, and append it to the list. We don't proceed
5052 * until EOM is met. blk_end will point to the EOM block.
5053 */
5054 hdr = 0;
5055 memset(list, 0, sizeof(list));
5056 blk_end = NULL;
5057
5058 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5059 blk = htx_get_blk(htx, idx);
5060 type = htx_get_blk_type(blk);
5061
5062 if (type == HTX_BLK_UNUSED)
5063 continue;
5064
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005065 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005066 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005067
5068 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5069 goto fail;
5070
5071 size = htx_get_blksz(blk);
5072 start = htx_get_blk_ptr(htx, blk);
5073
5074 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5075 h1m.err_pos = 0;
5076 ret = h1_headers_to_hdr_list(start, start + size,
5077 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5078 &h1m, NULL);
5079 if (ret < 0)
5080 goto fail;
5081
5082 /* ret == 0 if an incomplete trailers block was found (missing
5083 * empty line), or > 0 if it was found. We have to continue on
5084 * incomplete messages because the trailers block might be
5085 * incomplete.
5086 */
5087
5088 /* search the new end */
5089 while (hdr <= sizeof(list)/sizeof(list[0])) {
5090 if (!list[hdr].n.len)
5091 break;
5092 hdr++;
5093 }
5094 }
5095
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005096 if (list[hdr].n.len != 0)
5097 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005098
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005099 chunk_reset(&outbuf);
5100
5101 while (1) {
5102 outbuf.area = b_tail(&h2c->mbuf);
5103 outbuf.size = b_contig_space(&h2c->mbuf);
5104 outbuf.data = 0;
5105
5106 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5107 break;
5108 realign_again:
5109 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5110 }
5111
5112 if (outbuf.size < 9)
5113 goto full;
5114
5115 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5116 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5117 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5118 outbuf.data = 9;
5119
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005120 /* encode all headers */
5121 for (idx = 0; idx < hdr; idx++) {
5122 /* these ones do not exist in H2 or must not appear in
5123 * trailers and must be dropped.
5124 */
5125 if (isteq(list[idx].n, ist("host")) ||
5126 isteq(list[idx].n, ist("content-length")) ||
5127 isteq(list[idx].n, ist("connection")) ||
5128 isteq(list[idx].n, ist("proxy-connection")) ||
5129 isteq(list[idx].n, ist("keep-alive")) ||
5130 isteq(list[idx].n, ist("upgrade")) ||
5131 isteq(list[idx].n, ist("te")) ||
5132 isteq(list[idx].n, ist("transfer-encoding")))
5133 continue;
5134
5135 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5136 /* output full */
5137 if (b_space_wraps(&h2c->mbuf))
5138 goto realign_again;
5139 goto full;
5140 }
5141 }
5142
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005143 if (outbuf.data == 9) {
5144 /* here we have a problem, we have nothing to emit (either we
5145 * received an empty trailers block followed or we removed its
5146 * contents above). Because of this we can't send a HEADERS
5147 * frame, so we have to cheat and instead send an empty DATA
5148 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005149 */
5150 outbuf.area[3] = H2_FT_DATA;
5151 outbuf.area[4] = H2_F_DATA_END_STREAM;
5152 }
5153
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005154 /* update the frame's size */
5155 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5156
5157 /* commit the H2 response */
5158 b_add(&h2c->mbuf, outbuf.data);
5159 h2s->flags |= H2_SF_ES_SENT;
5160
5161 if (h2s->st == H2_SS_OPEN)
5162 h2s->st = H2_SS_HLOC;
5163 else
5164 h2s_close(h2s);
5165
5166 /* OK we could properly deliver the response */
5167 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005168 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005169 ret = 0;
5170 idx = htx_get_head(htx);
5171 blk = htx_get_blk(htx, idx);
5172 while (blk != blk_end) {
5173 ret += htx_get_blksz(blk);
5174 blk = htx_remove_blk(htx, blk);
5175 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005176 end:
5177 return ret;
5178 full:
5179 h2c->flags |= H2_CF_MUX_MFULL;
5180 h2s->flags |= H2_SF_BLK_MROOM;
5181 ret = 0;
5182 goto end;
5183 fail:
5184 /* unparsable HTX messages, too large ones to be produced in the local
5185 * list etc go here (unrecoverable errors).
5186 */
5187 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5188 ret = 0;
5189 goto end;
5190}
5191
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005192/* Called from the upper layer, to subscribe to events, such as being able to send.
5193 * The <param> argument here is supposed to be a pointer to a wait_event struct
5194 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5195 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5196 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5197 * returns 0 except for the error above.
5198 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005199static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5200{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005201 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005202 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005203 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005204
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005205 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005206 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005207 if (!(sw->events & SUB_RETRY_RECV)) {
5208 sw->events |= SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005209 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005210 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005211 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005212 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005213 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005214 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005215 if (!(sw->events & SUB_RETRY_SEND)) {
5216 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005217 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005218 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreauc234ae32019-05-13 17:56:11 +02005219 !LIST_ADDED(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005220 if (h2s->flags & H2_SF_BLK_MFCTL)
5221 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5222 else
5223 LIST_ADDQ(&h2c->send_list, &h2s->list);
5224 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005225 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005226 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005227 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005228 if (event_type != 0)
5229 return -1;
5230 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005231}
5232
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005233/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5234 * The <param> argument here is supposed to be a pointer to the same wait_event
5235 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5236 * It always returns zero.
5237 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005238static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5239{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005240 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005241 struct h2s *h2s = cs->ctx;
5242
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005243 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005244 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005245 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005246 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005247 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005248 }
5249 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005250 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005251 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005252 if (h2s->send_wait == sw) {
5253 LIST_DEL(&h2s->list);
5254 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005255 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005256 /* We were about to send, make sure it does not happen */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005257 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02005258 h2s->send_wait != &h2s->wait_event) {
5259 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5260 LIST_DEL_INIT(&h2s->sending_list);
5261 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005262 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005263
Olivier Houchardd846c262018-10-19 17:24:29 +02005264 }
5265 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005266 return 0;
5267}
5268
5269
Olivier Houchard511efea2018-08-16 15:30:32 +02005270/* Called from the upper layer, to receive data */
5271static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5272{
Olivier Houchard638b7992018-08-16 15:41:52 +02005273 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005274 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005275 struct htx *h2s_htx = NULL;
5276 struct htx *buf_htx = NULL;
5277 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005278 size_t ret = 0;
5279
5280 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005281 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5282 /* in HTX mode we ignore the count argument */
5283 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005284 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005285 /* Here htx_to_buf() will set buffer data to 0 because
5286 * the HTX is empty.
5287 */
5288 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005289 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005290 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005291
5292 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005293 count = htx_free_data_space(buf_htx);
5294 if (flags & CO_RFL_KEEP_RSV) {
5295 if (count <= global.tune.maxrewrite)
5296 goto end;
5297 count -= global.tune.maxrewrite;
5298 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005299
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005300 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005301
5302 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5303 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5304
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005305 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005306 htx_to_buf(buf_htx, buf);
5307 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005308 ret = htx_ret.ret;
5309 }
5310 else {
5311 ret = b_xfer(buf, &h2s->rxbuf, count);
5312 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005313
Christopher Faulet37070b22019-02-14 15:12:14 +01005314 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005315 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005316 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005317 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005318 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005319 if (cs->flags & CS_FL_REOS)
5320 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005321 if (cs->flags & CS_FL_ERR_PENDING)
5322 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005323 if (b_size(&h2s->rxbuf)) {
5324 b_free(&h2s->rxbuf);
5325 offer_buffers(NULL, tasks_run_queue);
5326 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005327 }
5328
Willy Tarreau082f5592018-11-25 08:03:32 +01005329 if (ret && h2c->dsi == h2s->id) {
5330 /* demux is blocking on this stream's buffer */
5331 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005332 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005333 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005334
Olivier Houchard511efea2018-08-16 15:30:32 +02005335 return ret;
5336}
5337
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005338/* stops all senders of this connection for example when the mux buffer is full.
5339 * They are moved from the sending_list to either fctl_list or send_list.
5340 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005341static void h2_stop_senders(struct h2c *h2c)
5342{
5343 struct h2s *h2s, *h2s_back;
5344
Olivier Houchardd360ac62019-03-22 17:37:16 +01005345 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5346 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005347 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005348 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005349 }
5350}
5351
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005352/* Called from the upper layer, to send data from buffer <buf> for no more than
5353 * <count> bytes. Returns the number of bytes effectively sent. Some status
5354 * flags may be updated on the conn_stream.
5355 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005356static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005357{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005358 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005359 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005360 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005361 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005362 struct htx *htx;
5363 struct htx_blk *blk;
5364 enum htx_blk_type btype;
5365 uint32_t bsize;
5366 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005367
Olivier Houchardd360ac62019-03-22 17:37:16 +01005368 /* If we were not just woken because we wanted to send but couldn't,
5369 * and there's somebody else that is waiting to send, do nothing,
5370 * we will subscribe later and be put at the end of the list
5371 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005372 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005373 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5374 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005375 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005376
Olivier Houchard998410a2019-04-15 19:23:37 +02005377 /* We couldn't set it to NULL before, because we needed it in case
5378 * we had to cancel the tasklet
5379 */
5380 h2s->send_wait = NULL;
5381
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005382 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5383 return 0;
5384
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005385 /* htx will be enough to decide if we're using HTX or legacy */
5386 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5387
Willy Tarreau0bad0432018-06-14 16:54:01 +02005388 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005389 h2s->flags |= H2_SF_OUTGOING_DATA;
5390
Willy Tarreau751f2d02018-10-05 09:35:00 +02005391 if (h2s->id == 0) {
5392 int32_t id = h2c_get_next_sid(h2s->h2c);
5393
5394 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005395 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005396 return 0;
5397 }
5398
5399 eb32_delete(&h2s->by_id);
5400 h2s->by_id.key = h2s->id = id;
5401 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005402 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005403 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5404 }
5405
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005406 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005407 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005408 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005409 idx = htx_get_head(htx);
5410 blk = htx_get_blk(htx, idx);
5411 btype = htx_get_blk_type(blk);
5412 bsize = htx_get_blksz(blk);
5413
5414 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005415 case HTX_BLK_REQ_SL:
5416 /* start-line before headers */
5417 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5418 if (ret > 0) {
5419 total += ret;
5420 count -= ret;
5421 if (ret < bsize)
5422 goto done;
5423 }
5424 break;
5425
Willy Tarreau115e83b2018-12-01 19:17:53 +01005426 case HTX_BLK_RES_SL:
5427 /* start-line before headers */
5428 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5429 if (ret > 0) {
5430 total += ret;
5431 count -= ret;
5432 if (ret < bsize)
5433 goto done;
5434 }
5435 break;
5436
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005437 case HTX_BLK_DATA:
5438 case HTX_BLK_EOD:
5439 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005440 /* all these cause the emission of a DATA frame (possibly empty).
5441 * This EOM necessarily is one before trailers, as the EOM following
5442 * trailers would have been consumed by the trailers parser.
5443 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005444 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005445 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005446 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005447 total += ret;
5448 count -= ret;
5449 if (ret < bsize)
5450 goto done;
5451 }
5452 break;
5453
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005454 case HTX_BLK_TLR:
5455 /* This is the first trailers block, all the subsequent ones AND
5456 * the EOM will be swallowed by the parser.
5457 */
5458 ret = h2s_htx_make_trailers(h2s, htx);
5459 if (ret > 0) {
5460 total += ret;
5461 count -= ret;
5462 if (ret < bsize)
5463 goto done;
5464 }
5465 break;
5466
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005467 default:
5468 htx_remove_blk(htx, blk);
5469 total += bsize;
5470 count -= bsize;
5471 break;
5472 }
5473 }
5474 goto done;
5475 }
5476
5477 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005478 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005479 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005480 if (h2s->h2c->flags & H2_CF_IS_BACK)
5481 ret = -1;
5482 else
5483 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005484 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005485 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005486 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005487 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005488 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005489 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005490 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005491
Willy Tarreau5dd17352018-06-14 13:33:30 +02005492 if (unlikely((int)ret <= 0)) {
5493 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005494 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5495 break;
5496 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005497 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005498 total += count;
5499 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005500 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005501 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005502 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005503 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005504 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005505 break;
5506 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005507
5508 total += ret;
5509 count -= ret;
5510
Willy Tarreau2b778482019-05-06 15:00:22 +02005511 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005512 break;
5513
5514 if (h2s->flags & H2_SF_BLK_ANY)
5515 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005516 }
5517
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005518 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005519 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005520 /* trim any possibly pending data after we close (extra CR-LF,
5521 * unprocessed trailers, abnormal extra data, ...)
5522 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005523 total += count;
5524 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005525 }
5526
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005527 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005528 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005529 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005530 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005531 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005532 }
5533
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005534 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005535 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005536 } else {
5537 b_del(buf, total);
5538 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005539
5540 /* The mux is full, cancel the pending tasks */
5541 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5542 (h2s->flags & H2_SF_BLK_MBUSY))
5543 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005544
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005545 /* If we're running HTX, and we read the whole buffer, then pretend
5546 * we read exactly what the caller specified, as with HTX the caller
5547 * will always give the buffer size, instead of the amount of data
5548 * available.
5549 */
5550 if (htx && !b_data(buf))
5551 total = orig_count;
5552
Olivier Houchard7505f942018-08-21 18:10:44 +02005553 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005554 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005555 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005556
Olivier Houchard7505f942018-08-21 18:10:44 +02005557 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005558 /* If we're waiting for flow control, and we got a shutr on the
5559 * connection, we will never be unlocked, so add an error on
5560 * the conn_stream.
5561 */
5562 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5563 !b_data(&h2s->h2c->dbuf) &&
5564 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5565 if (cs->flags & CS_FL_EOS)
5566 cs->flags |= CS_FL_ERROR;
5567 else
5568 cs->flags |= CS_FL_ERR_PENDING;
5569 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005570 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005571 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005572 LIST_DEL_INIT(&h2s->list);
5573 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005574 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005575}
5576
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005577/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005578static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005579{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005580 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005581 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005582 struct eb32_node *node;
5583 int fctl_cnt = 0;
5584 int send_cnt = 0;
5585 int tree_cnt = 0;
5586 int orph_cnt = 0;
5587
5588 if (!h2c)
5589 return;
5590
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005591 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005592 fctl_cnt++;
5593
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005594 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005595 send_cnt++;
5596
Willy Tarreau3af37712018-12-18 14:34:41 +01005597 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005598 node = eb32_first(&h2c->streams_by_id);
5599 while (node) {
5600 h2s = container_of(node, struct h2s, by_id);
5601 tree_cnt++;
5602 if (!h2s->cs)
5603 orph_cnt++;
5604 node = eb32_next(node);
5605 }
5606
Willy Tarreau987c0632018-12-18 10:32:05 +01005607 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5608 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5609 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005610 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5611 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005612 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005613 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5614 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5615 h2c->msi,
5616 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5617 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5618
5619 if (h2s) {
5620 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5621 h2s, h2s->id, h2s->flags,
5622 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5623 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5624 h2s->cs);
5625 if (h2s->cs)
5626 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5627 h2s->cs->flags, h2s->cs->data);
5628 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005629}
Willy Tarreau62f52692017-10-08 23:01:42 +02005630
5631/*******************************************************/
5632/* functions below are dedicated to the config parsers */
5633/*******************************************************/
5634
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005635/* config parser for global "tune.h2.header-table-size" */
5636static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5637 struct proxy *defpx, const char *file, int line,
5638 char **err)
5639{
5640 if (too_many_args(1, args, err, NULL))
5641 return -1;
5642
5643 h2_settings_header_table_size = atoi(args[1]);
5644 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5645 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5646 return -1;
5647 }
5648 return 0;
5649}
Willy Tarreau62f52692017-10-08 23:01:42 +02005650
Willy Tarreaue6baec02017-07-27 11:45:11 +02005651/* config parser for global "tune.h2.initial-window-size" */
5652static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5653 struct proxy *defpx, const char *file, int line,
5654 char **err)
5655{
5656 if (too_many_args(1, args, err, NULL))
5657 return -1;
5658
5659 h2_settings_initial_window_size = atoi(args[1]);
5660 if (h2_settings_initial_window_size < 0) {
5661 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5662 return -1;
5663 }
5664 return 0;
5665}
5666
Willy Tarreau5242ef82017-07-27 11:47:28 +02005667/* config parser for global "tune.h2.max-concurrent-streams" */
5668static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5669 struct proxy *defpx, const char *file, int line,
5670 char **err)
5671{
5672 if (too_many_args(1, args, err, NULL))
5673 return -1;
5674
5675 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005676 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005677 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5678 return -1;
5679 }
5680 return 0;
5681}
5682
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005683/* config parser for global "tune.h2.max-frame-size" */
5684static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5685 struct proxy *defpx, const char *file, int line,
5686 char **err)
5687{
5688 if (too_many_args(1, args, err, NULL))
5689 return -1;
5690
5691 h2_settings_max_frame_size = atoi(args[1]);
5692 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5693 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5694 return -1;
5695 }
5696 return 0;
5697}
5698
Willy Tarreau62f52692017-10-08 23:01:42 +02005699
5700/****************************************/
5701/* MUX initialization and instanciation */
5702/***************************************/
5703
5704/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005705static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005706 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005707 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005708 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005709 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005710 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005711 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005712 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005713 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005714 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005715 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005716 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005717 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005718 .shutr = h2_shutr,
5719 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005720 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005721 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005722 .name = "H2",
5723};
5724
Christopher Faulet32f61c02018-04-10 14:33:41 +02005725/* PROTO selection : this mux registers PROTO token "h2" */
5726static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005727 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005728
Willy Tarreau0108d902018-11-25 19:14:37 +01005729INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5730
Christopher Faulet9b579102019-04-11 22:52:25 +02005731
5732/* The mux operations */
5733static const struct mux_ops h2_htx_ops = {
5734 .init = h2_init,
5735 .wake = h2_wake,
5736 .snd_buf = h2_snd_buf,
5737 .rcv_buf = h2_rcv_buf,
5738 .subscribe = h2_subscribe,
5739 .unsubscribe = h2_unsubscribe,
5740 .attach = h2_attach,
5741 .get_first_cs = h2_get_first_cs,
5742 .detach = h2_detach,
5743 .destroy = h2_destroy,
5744 .avail_streams = h2_avail_streams,
5745 .used_streams = h2_used_streams,
5746 .shutr = h2_shutr,
5747 .shutw = h2_shutw,
5748 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005749 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005750 .name = "H2",
5751};
5752
Willy Tarreauf8957272018-10-03 10:25:20 +02005753static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005754 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005755
5756INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5757
Willy Tarreau62f52692017-10-08 23:01:42 +02005758/* config keyword parsers */
5759static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005760 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005761 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005762 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005763 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005764 { 0, NULL, NULL }
5765}};
5766
Willy Tarreau0108d902018-11-25 19:14:37 +01005767INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);