blob: fe4d0928e4e85209b774397aadf9ef87f7b9289a [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
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
199 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
200 struct wait_event *send_wait; /* The streeam is waiting for flow control */
201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200202};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200203
Willy Tarreauc6405142017-09-21 20:23:50 +0200204/* descriptor for an h2 frame header */
205struct h2_fh {
206 uint32_t len; /* length, host order, 24 bits */
207 uint32_t sid; /* stream id, host order, 31 bits */
208 uint8_t ft; /* frame type */
209 uint8_t ff; /* frame flags */
210};
211
Willy Tarreau8ceae722018-11-26 11:58:30 +0100212/* the h2c connection pool */
213DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
214
215/* the h2s stream pool */
216DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
217
Willy Tarreaudc572362018-12-12 08:08:05 +0100218/* The default connection window size is 65535, it may only be enlarged using
219 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
220 * we'll pretend we already received the difference between the two to send
221 * an equivalent window update to enlarge it to 2G-1.
222 */
223#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
224
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225/* a few settings from the global section */
226static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200227static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100228static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100229static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200230
Willy Tarreau2a856182017-05-16 15:20:39 +0200231/* a dmumy closed stream */
232static const struct h2s *h2_closed_stream = &(const struct h2s){
233 .cs = NULL,
234 .h2c = NULL,
235 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100236 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100237 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200238 .id = 0,
239};
240
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100241/* a dmumy closed stream returning a PROTOCOL_ERROR error */
242static const struct h2s *h2_error_stream = &(const struct h2s){
243 .cs = NULL,
244 .h2c = NULL,
245 .st = H2_SS_CLOSED,
246 .errcode = H2_ERR_PROTOCOL_ERROR,
247 .flags = 0,
248 .id = 0,
249};
250
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100251/* a dmumy closed stream returning a REFUSED_STREAM error */
252static const struct h2s *h2_refused_stream = &(const struct h2s){
253 .cs = NULL,
254 .h2c = NULL,
255 .st = H2_SS_CLOSED,
256 .errcode = H2_ERR_REFUSED_STREAM,
257 .flags = 0,
258 .id = 0,
259};
260
Willy Tarreau2a856182017-05-16 15:20:39 +0200261/* and a dummy idle stream for use with any unannounced stream */
262static const struct h2s *h2_idle_stream = &(const struct h2s){
263 .cs = NULL,
264 .h2c = NULL,
265 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100266 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200267 .id = 0,
268};
269
Olivier Houchard9f6af332018-05-25 14:04:04 +0200270static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200271static int h2_send(struct h2c *h2c);
272static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200273static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200274static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100275static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100276static 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 +0100277static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200278static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100279static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100280static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200281
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282/*****************************************************/
283/* functions below are for dynamic buffer management */
284/*****************************************************/
285
Willy Tarreau315d8072017-12-10 22:17:57 +0100286/* indicates whether or not the we may call the h2_recv() function to attempt
287 * to receive data into the buffer and/or demux pending data. The condition is
288 * a bit complex due to some API limits for now. The rules are the following :
289 * - if an error or a shutdown was detected on the connection and the buffer
290 * is empty, we must not attempt to receive
291 * - if the demux buf failed to be allocated, we must not try to receive and
292 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100293 * - if no flag indicates a blocking condition, we may attempt to receive,
294 * regardless of whether the demux buffer is full or not, so that only
295 * de demux part decides whether or not to block. This is needed because
296 * the connection API indeed prevents us from re-enabling receipt that is
297 * already enabled in a polled state, so we must always immediately stop
298 * as soon as the demux can't proceed so as never to hit an end of read
299 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100300 * - otherwise must may not attempt
301 */
302static inline int h2_recv_allowed(const struct h2c *h2c)
303{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200304 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100305 (h2c->st0 >= H2_CS_ERROR ||
306 h2c->conn->flags & CO_FL_ERROR ||
307 conn_xprt_read0_pending(h2c->conn)))
308 return 0;
309
310 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100311 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100312 return 1;
313
314 return 0;
315}
316
Willy Tarreau47b515a2018-12-21 16:09:41 +0100317/* restarts reading on the connection if it was not enabled */
318static inline void h2c_restart_reading(const struct h2c *h2c)
319{
320 if (!h2_recv_allowed(h2c))
321 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100322 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100323 return;
324 tasklet_wakeup(h2c->wait_event.task);
325}
326
327
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100328/* returns true if the front connection has too many conn_streams attached */
329static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200330{
Willy Tarreaua8754662018-12-23 20:43:58 +0100331 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200332}
333
Willy Tarreau44e973f2018-03-01 17:49:30 +0100334/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
335 * flags are used to figure what buffer was requested. It returns 1 if the
336 * allocation succeeds, in which case the connection is woken up, or 0 if it's
337 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200338 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100339static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200340{
341 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100342 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200343
Willy Tarreau44e973f2018-03-01 17:49:30 +0100344 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200345 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100346 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200347 return 1;
348 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200349
Willy Tarreau44e973f2018-03-01 17:49:30 +0100350 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
351 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200352
353 if (h2c->flags & H2_CF_DEM_MROOM) {
354 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100355 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200356 }
Willy Tarreau14398122017-09-22 14:26:04 +0200357 return 1;
358 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100359
360 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
361 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200362 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100363 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100364 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100365 return 1;
366 }
367
Willy Tarreau14398122017-09-22 14:26:04 +0200368 return 0;
369}
370
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200372{
373 struct buffer *buf = NULL;
374
Willy Tarreau44e973f2018-03-01 17:49:30 +0100375 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
376 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
377 h2c->buf_wait.target = h2c;
378 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100379 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100380 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100381 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200382 __conn_xprt_stop_recv(h2c->conn);
383 }
384 return buf;
385}
386
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200387static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200388{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100390 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200391 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200392 }
393}
394
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100395/* returns the number of allocatable outgoing streams for the connection taking
396 * the last_sid and the reserved ones into account.
397 */
398static inline int h2_streams_left(const struct h2c *h2c)
399{
400 int ret;
401
402 /* consider the number of outgoing streams we're allowed to create before
403 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
404 * nb_reserved is the number of streams which don't yet have an ID.
405 */
406 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
407 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
408 if (ret < 0)
409 ret = 0;
410 return ret;
411}
412
Willy Tarreau00f18a32019-01-26 12:19:01 +0100413/* returns the number of streams in use on a connection to figure if it's
414 * idle or not. We check nb_cs and not nb_streams as the caller will want
415 * to know if it was the last one after a detach().
416 */
417static int h2_used_streams(struct connection *conn)
418{
419 struct h2c *h2c = conn->ctx;
420
421 return h2c->nb_cs;
422}
423
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100424/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100425static int h2_avail_streams(struct connection *conn)
426{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100427 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100428 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100429 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100430
Willy Tarreau6afec462019-01-28 06:40:19 +0100431 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
432 * streams on the connection.
433 */
434 if (h2c->last_sid >= 0)
435 return 0;
436
Willy Tarreau86949782019-01-31 10:42:05 +0100437 /* note: may be negative if a SETTINGS frame changes the limit */
438 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100439
440 /* we must also consider the limit imposed by stream IDs */
441 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100442 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100443 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
445 ret1 = MIN(ret1, ret2);
446 }
447 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448}
449
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200450
Willy Tarreau62f52692017-10-08 23:01:42 +0200451/*****************************************************************/
452/* functions below are dedicated to the mux setup and management */
453/*****************************************************************/
454
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200455/* Initialize the mux once it's attached. For outgoing connections, the context
456 * is already initialized before installing the mux, so we detect incoming
457 * connections from the fact that the context is still NULL. Returns < 0 on
458 * error.
459 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100460static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200461{
462 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100463 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200464
Willy Tarreaubafbe012017-11-24 17:34:44 +0100465 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200467 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100469 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200470 h2c->flags = H2_CF_IS_BACK;
471 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
472 if (tick_isset(prx->timeout.serverfin))
473 h2c->shut_timeout = prx->timeout.serverfin;
474 } else {
475 h2c->flags = H2_CF_NONE;
476 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
477 if (tick_isset(prx->timeout.clientfin))
478 h2c->shut_timeout = prx->timeout.clientfin;
479 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100480
Willy Tarreau0b37d652018-10-03 10:33:02 +0200481 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100482 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100483 if (tick_isset(h2c->timeout)) {
484 t = task_new(tid_bit);
485 if (!t)
486 goto fail;
487
488 h2c->task = t;
489 t->process = h2_timeout_task;
490 t->context = h2c;
491 t->expire = tick_add(now_ms, h2c->timeout);
492 }
Willy Tarreauea392822017-10-31 10:02:25 +0100493
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200494 h2c->wait_event.task = tasklet_new();
495 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200496 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200497 h2c->wait_event.task->process = h2_io_cb;
498 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100499 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200500
Willy Tarreau32218eb2017-09-22 08:07:25 +0200501 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
502 if (!h2c->ddht)
503 goto fail;
504
505 /* Initialise the context. */
506 h2c->st0 = H2_CS_PREFACE;
507 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100508 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200509 h2c->max_id = -1;
510 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100511 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200512 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100513 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200514 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100515 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100516 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200517
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200518 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519 h2c->dsi = -1;
520 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100521
Willy Tarreau32218eb2017-09-22 08:07:25 +0200522 h2c->last_sid = -1;
523
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200524 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200525 h2c->miw = 65535; /* mux initial window size */
526 h2c->mws = 65535; /* mux window size */
527 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200528 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 LIST_INIT(&h2c->send_list);
530 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200531 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100532 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200533
Willy Tarreau3f133572017-10-31 19:21:06 +0100534 if (t)
535 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100536
Willy Tarreau01b44822018-10-03 14:26:37 +0200537 if (h2c->flags & H2_CF_IS_BACK) {
538 /* FIXME: this is temporary, for outgoing connections we need
539 * to immediately allocate a stream until the code is modified
540 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100541 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200542 */
543 struct h2s *h2s;
544
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100545 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200546 if (!h2s)
547 goto fail_stream;
548 }
549
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100550 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200551
Willy Tarreau0f383582018-10-03 14:22:21 +0200552 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100553 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200554 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200555 fail_stream:
556 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200557 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100558 if (t)
559 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200560 if (h2c->wait_event.task)
561 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100562 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200563 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200564 return -1;
565}
566
Willy Tarreau751f2d02018-10-05 09:35:00 +0200567/* returns the next allocatable outgoing stream ID for the H2 connection, or
568 * -1 if no more is allocatable.
569 */
570static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
571{
572 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100573
574 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200575 id = -1;
576 return id;
577}
578
Willy Tarreau2373acc2017-10-12 17:35:14 +0200579/* returns the stream associated with id <id> or NULL if not found */
580static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
581{
582 struct eb32_node *node;
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584 if (id == 0)
585 return (struct h2s *)h2_closed_stream;
586
Willy Tarreau2a856182017-05-16 15:20:39 +0200587 if (id > h2c->max_id)
588 return (struct h2s *)h2_idle_stream;
589
Willy Tarreau2373acc2017-10-12 17:35:14 +0200590 node = eb32_lookup(&h2c->streams_by_id, id);
591 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200592 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200593
594 return container_of(node, struct h2s, by_id);
595}
596
Willy Tarreau62f52692017-10-08 23:01:42 +0200597/* release function for a connection. This one should be called to free all
598 * resources allocated to the mux.
599 */
600static void h2_release(struct connection *conn)
601{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100602 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200603
Willy Tarreau32218eb2017-09-22 08:07:25 +0200604 if (h2c) {
605 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200606
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100607 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100608 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100609 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200610
Willy Tarreau44e973f2018-03-01 17:49:30 +0100611 h2_release_buf(h2c, &h2c->dbuf);
612 h2_release_buf(h2c, &h2c->mbuf);
613
Willy Tarreauea392822017-10-31 10:02:25 +0100614 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200615 h2c->task->context = NULL;
616 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100617 h2c->task = NULL;
618 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200619 if (h2c->wait_event.task)
620 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100621 if (h2c->wait_event.events != 0)
622 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200623 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100624
Willy Tarreaubafbe012017-11-24 17:34:44 +0100625 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200626 }
627
628 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100629 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200630
631 conn_stop_tracking(conn);
632 conn_full_close(conn);
633 if (conn->destroy_cb)
634 conn->destroy_cb(conn);
635 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200636}
637
638
Willy Tarreau71681172017-10-23 14:39:06 +0200639/******************************************************/
640/* functions below are for the H2 protocol processing */
641/******************************************************/
642
643/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100644static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200645{
646 return h2s ? h2s->id : 0;
647}
648
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200649/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100650static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200651{
652 if (h2c->msi < 0)
653 return 0;
654
655 if (h2c->msi == h2s_id(h2s))
656 return 0;
657
658 return 1;
659}
660
Willy Tarreau741d6df2017-10-17 08:00:59 +0200661/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100662static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200663{
664 h2c->errcode = err;
665 h2c->st0 = H2_CS_ERROR;
666}
667
Willy Tarreau175cebb2019-01-24 10:02:24 +0100668/* marks an error on the stream. It may also update an already closed stream
669 * (e.g. to report an error after an RST was received).
670 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100671static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200672{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100673 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200674 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100675 if (h2s->st < H2_SS_ERROR)
676 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100677 if (h2s->cs)
678 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200679 }
680}
681
Willy Tarreau7e094452018-12-19 18:08:52 +0100682/* attempt to notify the data layer of recv availability */
683static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
684{
685 struct wait_event *sw;
686
687 if (h2s->recv_wait) {
688 sw = h2s->recv_wait;
689 sw->events &= ~SUB_RETRY_RECV;
690 tasklet_wakeup(sw->task);
691 h2s->recv_wait = NULL;
692 }
693}
694
695/* attempt to notify the data layer of send availability */
696static void __maybe_unused h2s_notify_send(struct h2s *h2s)
697{
698 struct wait_event *sw;
699
700 if (h2s->send_wait) {
701 sw = h2s->send_wait;
702 sw->events &= ~SUB_RETRY_SEND;
703 tasklet_wakeup(sw->task);
704 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100705 LIST_DEL(&h2s->list);
706 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100707 }
708}
709
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100710/* alerts the data layer, trying to wake it up by all means, following
711 * this sequence :
712 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
713 * - if its subscribed to send, then it's woken up for send
714 * - if it was subscribed to neither, its ->wake() callback is called
715 * It is safe to call this function with a closed stream which doesn't have a
716 * conn_stream anymore.
717 */
718static void __maybe_unused h2s_alert(struct h2s *h2s)
719{
720 if (h2s->recv_wait || h2s->send_wait) {
721 h2s_notify_recv(h2s);
722 h2s_notify_send(h2s);
723 }
724 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
725 h2s->cs->data_cb->wake(h2s->cs);
726}
727
Willy Tarreaue4820742017-07-27 13:37:23 +0200728/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100729static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200730{
731 uint8_t *out = frame;
732
733 *out = len >> 16;
734 write_n16(out + 1, len);
735}
736
Willy Tarreau54c15062017-10-10 17:10:03 +0200737/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
738 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
739 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200740 * available in the buffer's input prior to calling this function. The buffer
741 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200742 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100743static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200744 const struct buffer *b, int o)
745{
Willy Tarreau591d4452018-06-15 17:21:00 +0200746 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 +0200747}
748
Willy Tarreau1f094672017-11-20 21:27:45 +0100749static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200750{
Willy Tarreau591d4452018-06-15 17:21:00 +0200751 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200752}
753
Willy Tarreau1f094672017-11-20 21:27:45 +0100754static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200755{
Willy Tarreau591d4452018-06-15 17:21:00 +0200756 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200757}
758
Willy Tarreau1f094672017-11-20 21:27:45 +0100759static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200760{
Willy Tarreau591d4452018-06-15 17:21:00 +0200761 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200762}
763
764
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100765/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
766 * The algorithm is not obvious. It turns out that H2 headers are neither
767 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
768 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200769 *
770 * b0 b1 b2 b3 b4 b5..b8
771 * +----------+---------+--------+----+----+----------------------+
772 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
773 * +----------+---------+--------+----+----+----------------------+
774 *
775 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
776 * we get the sid properly aligned and ordered, and 16 bits of len properly
777 * ordered as well. The type and flags can be extracted using bit shifts from
778 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200779 * Returns zero if some bytes are missing, otherwise non-zero on success. The
780 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200781 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100782static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200783{
784 uint64_t w;
785
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100786 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200787 return 0;
788
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100789 w = h2_get_n64(b, o + 1);
790 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200791 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
792 h->ff = w >> 32;
793 h->ft = w >> 40;
794 h->len += w >> 48;
795 return 1;
796}
797
798/* skip the next 9 bytes corresponding to the frame header possibly parsed by
799 * h2_peek_frame_hdr() above.
800 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100801static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200802{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200803 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200804}
805
806/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100807static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200808{
809 int ret;
810
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100811 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200812 if (ret > 0)
813 h2_skip_frame_hdr(b);
814 return ret;
815}
816
Willy Tarreau00dd0782018-03-01 16:31:34 +0100817/* marks stream <h2s> as CLOSED and decrement the number of active streams for
818 * its connection if the stream was not yet closed. Please use this exclusively
819 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100820 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100821static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100822{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100823 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100824 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100825 if (!h2s->id)
826 h2s->h2c->nb_reserved--;
827 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100828 h2s->st = H2_SS_CLOSED;
829}
830
Willy Tarreau71049cc2018-03-28 13:56:39 +0200831/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
832static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100833{
834 h2s_close(h2s);
835 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200836 if (b_size(&h2s->rxbuf)) {
837 b_free(&h2s->rxbuf);
838 offer_buffers(NULL, tasks_run_queue);
839 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200840 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100841 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200842 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100843 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800844 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200845 * reference left would be in the h2c send_list/fctl_list, and if
846 * we're in it, we're getting out anyway
847 */
848 LIST_DEL(&h2s->list);
849 LIST_INIT(&h2s->list);
850 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100851 pool_free(pool_head_h2s, h2s);
852}
853
Willy Tarreaua8e49542018-10-03 18:53:55 +0200854/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
855 * stream tree. In case of error, nothing is added and NULL is returned. The
856 * causes of errors can be any failed memory allocation. The caller is
857 * responsible for checking if the connection may support an extra stream
858 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200859 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200860static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200862 struct h2s *h2s;
863
Willy Tarreaubafbe012017-11-24 17:34:44 +0100864 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200865 if (!h2s)
866 goto out;
867
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 h2s->wait_event.task = tasklet_new();
869 if (!h2s->wait_event.task) {
870 pool_free(pool_head_h2s, h2s);
871 goto out;
872 }
873 h2s->send_wait = NULL;
874 h2s->recv_wait = NULL;
875 h2s->wait_event.task->process = h2_deferred_shut;
876 h2s->wait_event.task->context = h2s;
877 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100878 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200879 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200881 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200882 h2s->mws = h2c->miw;
883 h2s->flags = H2_SF_NONE;
884 h2s->errcode = H2_ERR_NO_ERROR;
885 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200886 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100887 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200888 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200889
890 if (h2c->flags & H2_CF_IS_BACK) {
891 h1m_init_req(&h2s->h1m);
892 h2s->h1m.err_pos = -1; // don't care about errors on the request path
893 h2s->h1m.flags |= H1_MF_TOLOWER;
894 } else {
895 h1m_init_res(&h2s->h1m);
896 h2s->h1m.err_pos = -1; // don't care about errors on the response path
897 h2s->h1m.flags |= H1_MF_TOLOWER;
898 }
899
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200900 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200901 if (id > 0)
902 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100903 else
904 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200905
906 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100907 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100908 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200909
910 return h2s;
911
912 out_free_h2s:
913 pool_free(pool_head_h2s, h2s);
914 out:
915 return NULL;
916}
917
918/* creates a new stream <id> on the h2c connection and returns it, or NULL in
919 * case of memory allocation error.
920 */
921static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
922{
923 struct session *sess = h2c->conn->owner;
924 struct conn_stream *cs;
925 struct h2s *h2s;
926
927 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
928 goto out;
929
930 h2s = h2s_new(h2c, id);
931 if (!h2s)
932 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933
934 cs = cs_new(h2c->conn);
935 if (!cs)
936 goto out_close;
937
Olivier Houchard746fb772018-12-15 19:42:00 +0100938 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200939 h2s->cs = cs;
940 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200941 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200942
943 if (stream_create_from_cs(cs) < 0)
944 goto out_free_cs;
945
Willy Tarreau590a0512018-09-05 11:56:48 +0200946 /* We want the accept date presented to the next stream to be the one
947 * we have now, the handshake time to be null (since the next stream
948 * is not delayed by a handshake), and the idle time to count since
949 * right now.
950 */
951 sess->accept_date = date;
952 sess->tv_accept = now;
953 sess->t_handshake = 0;
954
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200955 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100956 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200957 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 return h2s;
959
960 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200961 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962 cs_free(cs);
963 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200964 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200966 sess_log(sess);
967 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200968}
969
Willy Tarreau751f2d02018-10-05 09:35:00 +0200970/* allocates a new stream associated to conn_stream <cs> on the h2c connection
971 * and returns it, or NULL in case of memory allocation error or if the highest
972 * possible stream ID was reached.
973 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100974static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200975{
976 struct h2s *h2s = NULL;
977
Willy Tarreau86949782019-01-31 10:42:05 +0100978 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200979 goto out;
980
Willy Tarreaua80dca82019-01-24 17:08:28 +0100981 if (h2_streams_left(h2c) < 1)
982 goto out;
983
Willy Tarreau751f2d02018-10-05 09:35:00 +0200984 /* Defer choosing the ID until we send the first message to create the stream */
985 h2s = h2s_new(h2c, 0);
986 if (!h2s)
987 goto out;
988
989 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100990 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200991 cs->ctx = h2s;
992 h2c->nb_cs++;
993
Willy Tarreau751f2d02018-10-05 09:35:00 +0200994 out:
995 return h2s;
996}
997
Willy Tarreaube5b7152017-09-25 16:25:39 +0200998/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
999 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1000 * the various settings codes.
1001 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001002static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001003{
1004 struct buffer *res;
1005 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001006 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001007 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001008 int ret;
1009
1010 if (h2c_mux_busy(h2c, NULL)) {
1011 h2c->flags |= H2_CF_DEM_MBUSY;
1012 return 0;
1013 }
1014
Willy Tarreau44e973f2018-03-01 17:49:30 +01001015 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001016 if (!res) {
1017 h2c->flags |= H2_CF_MUX_MALLOC;
1018 h2c->flags |= H2_CF_DEM_MROOM;
1019 return 0;
1020 }
1021
1022 chunk_init(&buf, buf_data, sizeof(buf_data));
1023 chunk_memcpy(&buf,
1024 "\x00\x00\x00" /* length : 0 for now */
1025 "\x04\x00" /* type : 4 (settings), flags : 0 */
1026 "\x00\x00\x00\x00", /* stream ID : 0 */
1027 9);
1028
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001029 if (h2c->flags & H2_CF_IS_BACK) {
1030 /* send settings_enable_push=0 */
1031 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1032 }
1033
Willy Tarreaube5b7152017-09-25 16:25:39 +02001034 if (h2_settings_header_table_size != 4096) {
1035 char str[6] = "\x00\x01"; /* header_table_size */
1036
1037 write_n32(str + 2, h2_settings_header_table_size);
1038 chunk_memcat(&buf, str, 6);
1039 }
1040
1041 if (h2_settings_initial_window_size != 65535) {
1042 char str[6] = "\x00\x04"; /* initial_window_size */
1043
1044 write_n32(str + 2, h2_settings_initial_window_size);
1045 chunk_memcat(&buf, str, 6);
1046 }
1047
1048 if (h2_settings_max_concurrent_streams != 0) {
1049 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1050
1051 /* Note: 0 means "unlimited" for haproxy's config but not for
1052 * the protocol, so never send this value!
1053 */
1054 write_n32(str + 2, h2_settings_max_concurrent_streams);
1055 chunk_memcat(&buf, str, 6);
1056 }
1057
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001058 mfs = h2_settings_max_frame_size;
1059 if (mfs > global.tune.bufsize)
1060 mfs = global.tune.bufsize;
1061
1062 if (!mfs)
1063 mfs = global.tune.bufsize;
1064
1065 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066 char str[6] = "\x00\x05"; /* max_frame_size */
1067
1068 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1069 * match bufsize - rewrite size, but at the moment it seems
1070 * that clients don't take care of it.
1071 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001072 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001073 chunk_memcat(&buf, str, 6);
1074 }
1075
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001076 h2_set_frame_size(buf.area, buf.data - 9);
1077 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001078 if (unlikely(ret <= 0)) {
1079 if (!ret) {
1080 h2c->flags |= H2_CF_MUX_MFULL;
1081 h2c->flags |= H2_CF_DEM_MROOM;
1082 return 0;
1083 }
1084 else {
1085 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1086 return 0;
1087 }
1088 }
1089 return ret;
1090}
1091
Willy Tarreau52eed752017-09-22 15:05:09 +02001092/* Try to receive a connection preface, then upon success try to send our
1093 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1094 * missing data. It may return an error in h2c.
1095 */
1096static int h2c_frt_recv_preface(struct h2c *h2c)
1097{
1098 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001100
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001101 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001102
1103 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001104 if (ret1 < 0)
1105 sess_log(h2c->conn->owner);
1106
Willy Tarreau52eed752017-09-22 15:05:09 +02001107 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1108 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1109 return 0;
1110 }
1111
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001112 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001113 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001114 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001115
Willy Tarreaube5b7152017-09-25 16:25:39 +02001116 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001117}
1118
Willy Tarreau01b44822018-10-03 14:26:37 +02001119/* Try to send a connection preface, then upon success try to send our
1120 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1121 * missing data. It may return an error in h2c.
1122 */
1123static int h2c_bck_send_preface(struct h2c *h2c)
1124{
1125 struct buffer *res;
1126
1127 if (h2c_mux_busy(h2c, NULL)) {
1128 h2c->flags |= H2_CF_DEM_MBUSY;
1129 return 0;
1130 }
1131
1132 res = h2_get_buf(h2c, &h2c->mbuf);
1133 if (!res) {
1134 h2c->flags |= H2_CF_MUX_MALLOC;
1135 h2c->flags |= H2_CF_DEM_MROOM;
1136 return 0;
1137 }
1138
1139 if (!b_data(res)) {
1140 /* preface not yet sent */
1141 b_istput(res, ist(H2_CONN_PREFACE));
1142 }
1143
1144 return h2c_send_settings(h2c);
1145}
1146
Willy Tarreau081d4722017-05-16 21:51:05 +02001147/* try to send a GOAWAY frame on the connection to report an error or a graceful
1148 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1149 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1150 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1151 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1152 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1153 * on unrecoverable failure. It will not attempt to send one again in this last
1154 * case so that it is safe to use h2c_error() to report such errors.
1155 */
1156static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1157{
1158 struct buffer *res;
1159 char str[17];
1160 int ret;
1161
1162 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1163 return 1; // claim that it worked
1164
1165 if (h2c_mux_busy(h2c, h2s)) {
1166 if (h2s)
1167 h2s->flags |= H2_SF_BLK_MBUSY;
1168 else
1169 h2c->flags |= H2_CF_DEM_MBUSY;
1170 return 0;
1171 }
1172
Willy Tarreau44e973f2018-03-01 17:49:30 +01001173 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001174 if (!res) {
1175 h2c->flags |= H2_CF_MUX_MALLOC;
1176 if (h2s)
1177 h2s->flags |= H2_SF_BLK_MROOM;
1178 else
1179 h2c->flags |= H2_CF_DEM_MROOM;
1180 return 0;
1181 }
1182
1183 /* len: 8, type: 7, flags: none, sid: 0 */
1184 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1185
1186 if (h2c->last_sid < 0)
1187 h2c->last_sid = h2c->max_id;
1188
1189 write_n32(str + 9, h2c->last_sid);
1190 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001191 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001192 if (unlikely(ret <= 0)) {
1193 if (!ret) {
1194 h2c->flags |= H2_CF_MUX_MFULL;
1195 if (h2s)
1196 h2s->flags |= H2_SF_BLK_MROOM;
1197 else
1198 h2c->flags |= H2_CF_DEM_MROOM;
1199 return 0;
1200 }
1201 else {
1202 /* we cannot report this error using GOAWAY, so we mark
1203 * it and claim a success.
1204 */
1205 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1206 h2c->flags |= H2_CF_GOAWAY_FAILED;
1207 return 1;
1208 }
1209 }
1210 h2c->flags |= H2_CF_GOAWAY_SENT;
1211 return ret;
1212}
1213
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001214/* Try to send an RST_STREAM frame on the connection for the indicated stream
1215 * during mux operations. This stream must be valid and cannot be closed
1216 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1217 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1218 * not yet.
1219 *
1220 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1221 * to write the message, it subscribes the stream to future notifications.
1222 */
1223static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1224{
1225 struct buffer *res;
1226 char str[13];
1227 int ret;
1228
1229 if (!h2s || h2s->st == H2_SS_CLOSED)
1230 return 1;
1231
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001232 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1233 * RST_STREAM in response to a RST_STREAM frame.
1234 */
1235 if (h2c->dft == H2_FT_RST_STREAM) {
1236 ret = 1;
1237 goto ignore;
1238 }
1239
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001240 if (h2c_mux_busy(h2c, h2s)) {
1241 h2s->flags |= H2_SF_BLK_MBUSY;
1242 return 0;
1243 }
1244
Willy Tarreau44e973f2018-03-01 17:49:30 +01001245 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246 if (!res) {
1247 h2c->flags |= H2_CF_MUX_MALLOC;
1248 h2s->flags |= H2_SF_BLK_MROOM;
1249 return 0;
1250 }
1251
1252 /* len: 4, type: 3, flags: none */
1253 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1254 write_n32(str + 5, h2s->id);
1255 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001256 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001257
1258 if (unlikely(ret <= 0)) {
1259 if (!ret) {
1260 h2c->flags |= H2_CF_MUX_MFULL;
1261 h2s->flags |= H2_SF_BLK_MROOM;
1262 return 0;
1263 }
1264 else {
1265 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1266 return 0;
1267 }
1268 }
1269
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001270 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001271 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001272 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001273 return ret;
1274}
1275
1276/* Try to send an RST_STREAM frame on the connection for the stream being
1277 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001278 * error code, even if the stream is one of the dummy ones, and will update
1279 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001280 *
1281 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1282 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001283 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001284 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001285 */
1286static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1287{
1288 struct buffer *res;
1289 char str[13];
1290 int ret;
1291
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001292 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1293 * RST_STREAM in response to a RST_STREAM frame.
1294 */
1295 if (h2c->dft == H2_FT_RST_STREAM) {
1296 ret = 1;
1297 goto ignore;
1298 }
1299
Willy Tarreau27a84c92017-10-17 08:10:17 +02001300 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001301 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001302 return 0;
1303 }
1304
Willy Tarreau44e973f2018-03-01 17:49:30 +01001305 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001306 if (!res) {
1307 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001308 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001309 return 0;
1310 }
1311
1312 /* len: 4, type: 3, flags: none */
1313 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001314
Willy Tarreau27a84c92017-10-17 08:10:17 +02001315 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001316 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001317 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001318
Willy Tarreau27a84c92017-10-17 08:10:17 +02001319 if (unlikely(ret <= 0)) {
1320 if (!ret) {
1321 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001322 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001323 return 0;
1324 }
1325 else {
1326 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1327 return 0;
1328 }
1329 }
1330
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001331 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001332 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001334 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001335 }
1336
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 return ret;
1338}
1339
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001340/* try to send an empty DATA frame with the ES flag set to notify about the
1341 * end of stream and match a shutdown(write). If an ES was already sent as
1342 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1343 * on success or zero if nothing was done. In case of lack of room to write the
1344 * message, it subscribes the requesting stream to future notifications.
1345 */
1346static int h2_send_empty_data_es(struct h2s *h2s)
1347{
1348 struct h2c *h2c = h2s->h2c;
1349 struct buffer *res;
1350 char str[9];
1351 int ret;
1352
Willy Tarreau721c9742017-11-07 11:05:42 +01001353 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001354 return 1;
1355
1356 if (h2c_mux_busy(h2c, h2s)) {
1357 h2s->flags |= H2_SF_BLK_MBUSY;
1358 return 0;
1359 }
1360
Willy Tarreau44e973f2018-03-01 17:49:30 +01001361 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001362 if (!res) {
1363 h2c->flags |= H2_CF_MUX_MALLOC;
1364 h2s->flags |= H2_SF_BLK_MROOM;
1365 return 0;
1366 }
1367
1368 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1369 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1370 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001371 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001372 if (likely(ret > 0)) {
1373 h2s->flags |= H2_SF_ES_SENT;
1374 }
1375 else if (!ret) {
1376 h2c->flags |= H2_CF_MUX_MFULL;
1377 h2s->flags |= H2_SF_BLK_MROOM;
1378 return 0;
1379 }
1380 else {
1381 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1382 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001383 }
1384 return ret;
1385}
1386
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001387/* wake the streams attached to the connection, whose id is greater than <last>,
1388 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001389 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1390 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001391 */
1392static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1393{
1394 struct eb32_node *node;
1395 struct h2s *h2s;
1396
1397 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001398 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001399
1400 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001401 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001402
1403 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1404 while (node) {
1405 h2s = container_of(node, struct h2s, by_id);
1406 if (h2s->id <= last)
1407 break;
1408 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001409
1410 if (!h2s->cs) {
1411 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001412 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001413 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001414 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001415
1416 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001417 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1418 h2s->cs->flags |= CS_FL_ERROR;
1419
Willy Tarreauf830f012018-12-19 17:44:55 +01001420 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001421
Willy Tarreaua8519352018-12-18 16:44:28 +01001422 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001423 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001424 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001425 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001426 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001427 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001428 }
1429}
1430
Willy Tarreau3421aba2017-07-27 15:41:03 +02001431/* Increase all streams' outgoing window size by the difference passed in
1432 * argument. This is needed upon receipt of the settings frame if the initial
1433 * window size is different. The difference may be negative and the resulting
1434 * window size as well, for the time it takes to receive some window updates.
1435 */
1436static void h2c_update_all_ws(struct h2c *h2c, int diff)
1437{
1438 struct h2s *h2s;
1439 struct eb32_node *node;
1440
1441 if (!diff)
1442 return;
1443
1444 node = eb32_first(&h2c->streams_by_id);
1445 while (node) {
1446 h2s = container_of(node, struct h2s, by_id);
1447 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001448
1449 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1450 h2s->flags &= ~H2_SF_BLK_SFCTL;
1451 if (h2s->send_wait)
1452 LIST_ADDQ(&h2c->send_list, &h2s->list);
1453
1454 }
1455
Willy Tarreau3421aba2017-07-27 15:41:03 +02001456 node = eb32_next(node);
1457 }
1458}
1459
1460/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1461 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001462 * return an error in h2c. The caller must have already verified frame length
1463 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001464 */
1465static int h2c_handle_settings(struct h2c *h2c)
1466{
1467 unsigned int offset;
1468 int error;
1469
1470 if (h2c->dff & H2_F_SETTINGS_ACK) {
1471 if (h2c->dfl) {
1472 error = H2_ERR_FRAME_SIZE_ERROR;
1473 goto fail;
1474 }
1475 return 1;
1476 }
1477
Willy Tarreau3421aba2017-07-27 15:41:03 +02001478 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001479 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001480 return 0;
1481
1482 /* parse the frame */
1483 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001484 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1485 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001486
1487 switch (type) {
1488 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1489 /* we need to update all existing streams with the
1490 * difference from the previous iws.
1491 */
1492 if (arg < 0) { // RFC7540#6.5.2
1493 error = H2_ERR_FLOW_CONTROL_ERROR;
1494 goto fail;
1495 }
1496 h2c_update_all_ws(h2c, arg - h2c->miw);
1497 h2c->miw = arg;
1498 break;
1499 case H2_SETTINGS_MAX_FRAME_SIZE:
1500 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1501 error = H2_ERR_PROTOCOL_ERROR;
1502 goto fail;
1503 }
1504 h2c->mfs = arg;
1505 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001506 case H2_SETTINGS_ENABLE_PUSH:
1507 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1508 error = H2_ERR_PROTOCOL_ERROR;
1509 goto fail;
1510 }
1511 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001512 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1513 if (h2c->flags & H2_CF_IS_BACK) {
1514 /* the limit is only for the backend; for the frontend it is our limit */
1515 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1516 arg = h2_settings_max_concurrent_streams;
1517 h2c->streams_limit = arg;
1518 }
1519 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001520 }
1521 }
1522
1523 /* need to ACK this frame now */
1524 h2c->st0 = H2_CS_FRAME_A;
1525 return 1;
1526 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001527 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 h2c_error(h2c, error);
1529 return 0;
1530}
1531
1532/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1533 * success or one of the h2_status values.
1534 */
1535static int h2c_ack_settings(struct h2c *h2c)
1536{
1537 struct buffer *res;
1538 char str[9];
1539 int ret = -1;
1540
1541 if (h2c_mux_busy(h2c, NULL)) {
1542 h2c->flags |= H2_CF_DEM_MBUSY;
1543 return 0;
1544 }
1545
Willy Tarreau44e973f2018-03-01 17:49:30 +01001546 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001547 if (!res) {
1548 h2c->flags |= H2_CF_MUX_MALLOC;
1549 h2c->flags |= H2_CF_DEM_MROOM;
1550 return 0;
1551 }
1552
1553 memcpy(str,
1554 "\x00\x00\x00" /* length : 0 (no data) */
1555 "\x04" "\x01" /* type : 4, flags : ACK */
1556 "\x00\x00\x00\x00" /* stream ID */, 9);
1557
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001558 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001559 if (unlikely(ret <= 0)) {
1560 if (!ret) {
1561 h2c->flags |= H2_CF_MUX_MFULL;
1562 h2c->flags |= H2_CF_DEM_MROOM;
1563 return 0;
1564 }
1565 else {
1566 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1567 return 0;
1568 }
1569 }
1570 return ret;
1571}
1572
Willy Tarreaucf68c782017-10-10 17:11:41 +02001573/* processes a PING frame and schedules an ACK if needed. The caller must pass
1574 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001575 * missing data. The caller must have already verified frame length
1576 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001577 */
1578static int h2c_handle_ping(struct h2c *h2c)
1579{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001580 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001581 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001582 h2c->st0 = H2_CS_FRAME_A;
1583 return 1;
1584}
1585
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001586/* Try to send a window update for stream id <sid> and value <increment>.
1587 * Returns > 0 on success or zero on missing room or failure. It may return an
1588 * error in h2c.
1589 */
1590static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1591{
1592 struct buffer *res;
1593 char str[13];
1594 int ret = -1;
1595
1596 if (h2c_mux_busy(h2c, NULL)) {
1597 h2c->flags |= H2_CF_DEM_MBUSY;
1598 return 0;
1599 }
1600
Willy Tarreau44e973f2018-03-01 17:49:30 +01001601 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001602 if (!res) {
1603 h2c->flags |= H2_CF_MUX_MALLOC;
1604 h2c->flags |= H2_CF_DEM_MROOM;
1605 return 0;
1606 }
1607
1608 /* length: 4, type: 8, flags: none */
1609 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1610 write_n32(str + 5, sid);
1611 write_n32(str + 9, increment);
1612
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001613 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001614
1615 if (unlikely(ret <= 0)) {
1616 if (!ret) {
1617 h2c->flags |= H2_CF_MUX_MFULL;
1618 h2c->flags |= H2_CF_DEM_MROOM;
1619 return 0;
1620 }
1621 else {
1622 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1623 return 0;
1624 }
1625 }
1626 return ret;
1627}
1628
1629/* try to send pending window update for the connection. It's safe to call it
1630 * with no pending updates. Returns > 0 on success or zero on missing room or
1631 * failure. It may return an error in h2c.
1632 */
1633static int h2c_send_conn_wu(struct h2c *h2c)
1634{
1635 int ret = 1;
1636
1637 if (h2c->rcvd_c <= 0)
1638 return 1;
1639
Willy Tarreau97aaa672018-12-23 09:49:04 +01001640 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1641 /* increase the advertised connection window to 2G on
1642 * first update.
1643 */
1644 h2c->flags |= H2_CF_WINDOW_OPENED;
1645 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1646 }
1647
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001648 /* send WU for the connection */
1649 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1650 if (ret > 0)
1651 h2c->rcvd_c = 0;
1652
1653 return ret;
1654}
1655
1656/* try to send pending window update for the current dmux stream. It's safe to
1657 * call it with no pending updates. Returns > 0 on success or zero on missing
1658 * room or failure. It may return an error in h2c.
1659 */
1660static int h2c_send_strm_wu(struct h2c *h2c)
1661{
1662 int ret = 1;
1663
1664 if (h2c->rcvd_s <= 0)
1665 return 1;
1666
1667 /* send WU for the stream */
1668 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1669 if (ret > 0)
1670 h2c->rcvd_s = 0;
1671
1672 return ret;
1673}
1674
Willy Tarreaucf68c782017-10-10 17:11:41 +02001675/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1676 * success, 0 on missing data or one of the h2_status values.
1677 */
1678static int h2c_ack_ping(struct h2c *h2c)
1679{
1680 struct buffer *res;
1681 char str[17];
1682 int ret = -1;
1683
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001684 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001685 return 0;
1686
1687 if (h2c_mux_busy(h2c, NULL)) {
1688 h2c->flags |= H2_CF_DEM_MBUSY;
1689 return 0;
1690 }
1691
Willy Tarreau44e973f2018-03-01 17:49:30 +01001692 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001693 if (!res) {
1694 h2c->flags |= H2_CF_MUX_MALLOC;
1695 h2c->flags |= H2_CF_DEM_MROOM;
1696 return 0;
1697 }
1698
1699 memcpy(str,
1700 "\x00\x00\x08" /* length : 8 (same payload) */
1701 "\x06" "\x01" /* type : 6, flags : ACK */
1702 "\x00\x00\x00\x00" /* stream ID */, 9);
1703
1704 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001705 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001706
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001707 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001708 if (unlikely(ret <= 0)) {
1709 if (!ret) {
1710 h2c->flags |= H2_CF_MUX_MFULL;
1711 h2c->flags |= H2_CF_DEM_MROOM;
1712 return 0;
1713 }
1714 else {
1715 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1716 return 0;
1717 }
1718 }
1719 return ret;
1720}
1721
Willy Tarreau26f95952017-07-27 17:18:30 +02001722/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1723 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001724 * h2c or h2s. The caller must have already verified frame length and stream ID
1725 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001726 */
1727static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1728{
1729 int32_t inc;
1730 int error;
1731
Willy Tarreau26f95952017-07-27 17:18:30 +02001732 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001734 return 0;
1735
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001736 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001737
1738 if (h2c->dsi != 0) {
1739 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001740
1741 /* it's not an error to receive WU on a closed stream */
1742 if (h2s->st == H2_SS_CLOSED)
1743 return 1;
1744
1745 if (!inc) {
1746 error = H2_ERR_PROTOCOL_ERROR;
1747 goto strm_err;
1748 }
1749
1750 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1751 error = H2_ERR_FLOW_CONTROL_ERROR;
1752 goto strm_err;
1753 }
1754
1755 h2s->mws += inc;
1756 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1757 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001758 if (h2s->send_wait)
1759 LIST_ADDQ(&h2c->send_list, &h2s->list);
1760
Willy Tarreau26f95952017-07-27 17:18:30 +02001761 }
1762 }
1763 else {
1764 /* connection window update */
1765 if (!inc) {
1766 error = H2_ERR_PROTOCOL_ERROR;
1767 goto conn_err;
1768 }
1769
1770 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1771 error = H2_ERR_FLOW_CONTROL_ERROR;
1772 goto conn_err;
1773 }
1774
1775 h2c->mws += inc;
1776 }
1777
1778 return 1;
1779
1780 conn_err:
1781 h2c_error(h2c, error);
1782 return 0;
1783
1784 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001785 h2s_error(h2s, error);
1786 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001787 return 0;
1788}
1789
Willy Tarreaue96b0922017-10-30 00:28:29 +01001790/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001791 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1792 * have already verified frame length and stream ID validity. Described in
1793 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001794 */
1795static int h2c_handle_goaway(struct h2c *h2c)
1796{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001797 int last;
1798
Willy Tarreaue96b0922017-10-30 00:28:29 +01001799 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001800 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001801 return 0;
1802
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001803 last = h2_get_n32(&h2c->dbuf, 0);
1804 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001805 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001806 if (h2c->last_sid < 0)
1807 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001808 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001809}
1810
Willy Tarreau92153fc2017-12-03 19:46:19 +01001811/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001812 * invalid. Returns > 0 on success or zero on missing data. It may return an
1813 * error in h2c. The caller must have already verified frame length and stream
1814 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001815 */
1816static int h2c_handle_priority(struct h2c *h2c)
1817{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001818 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001819 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001820 return 0;
1821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001822 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001823 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001824 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1825 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001826 }
1827 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001828}
1829
Willy Tarreaucd234e92017-08-18 10:59:39 +02001830/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001831 * Returns > 0 on success or zero on missing data. The caller must have already
1832 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001833 */
1834static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1835{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001836 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001837 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001838 return 0;
1839
1840 /* late RST, already handled */
1841 if (h2s->st == H2_SS_CLOSED)
1842 return 1;
1843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001845 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001846
1847 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001848 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001849 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001850 }
1851
1852 h2s->flags |= H2_SF_RST_RCVD;
1853 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001854}
1855
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001856/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1857 * It may return an error in h2c or h2s. The caller must consider that the
1858 * return value is the new h2s in case one was allocated (most common case).
1859 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001860 * errors here are reported as connection errors since it's impossible to
1861 * recover from such errors after the compression context has been altered.
1862 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001863static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001864{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001865 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001866 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001867 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001868 int error;
1869
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001871 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001874 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001875
1876 /* now either the frame is complete or the buffer is complete */
1877 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001878 /* The stream exists/existed, this must be a trailers frame */
1879 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001880 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001881 goto out;
1882 goto done;
1883 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001884 /* the connection was already killed by an RST, let's consume
1885 * the data and send another RST.
1886 */
1887 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1888 h2s = (struct h2s*)h2_error_stream;
1889 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001890 }
1891 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1892 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1893 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001894 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001895 goto conn_err;
1896 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001897 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1898 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001899
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001900 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001901
Willy Tarreau25919232019-01-03 14:48:18 +01001902 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001903 if (h2c->st0 >= H2_CS_ERROR)
1904 goto out;
1905
Willy Tarreau25919232019-01-03 14:48:18 +01001906 if (error <= 0) {
1907 if (error == 0)
1908 goto out; // missing data
1909
1910 /* Failed to decode this stream (e.g. too large request)
1911 * but the HPACK decompressor is still synchronized.
1912 */
1913 h2s = (struct h2s*)h2_error_stream;
1914 goto send_rst;
1915 }
1916
Willy Tarreau22de8d32018-09-05 19:55:58 +02001917 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001918 * positively from h2c_frt_stream_new(), the stream will report the error,
1919 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001920 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001921 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001922 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001923 h2s = (struct h2s*)h2_refused_stream;
1924 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001925 }
1926
1927 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001928 h2s->rxbuf = rxbuf;
1929 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001930 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001931
Willy Tarreau88d138e2019-01-02 19:38:14 +01001932 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001933 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001934 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001935
1936 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001937 if (h2s->st == H2_SS_OPEN)
1938 h2s->st = H2_SS_HREM;
1939 else
1940 h2s_close(h2s);
Willy Tarreau927b88b2019-03-04 08:03:25 +01001941 if (h2s->cs)
1942 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001943 }
1944
Willy Tarreau3a429f02019-01-03 11:41:50 +01001945 /* update the max stream ID if the request is being processed */
1946 if (h2s->id > h2c->max_id)
1947 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001948
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001949 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001950
1951 conn_err:
1952 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001953 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001954
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001955 out:
1956 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001957 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001958
1959 send_rst:
1960 /* make the demux send an RST for the current stream. We may only
1961 * do this if we're certain that the HEADERS frame was properly
1962 * decompressed so that the HPACK decoder is still kept up to date.
1963 */
1964 h2_release_buf(h2c, &rxbuf);
1965 h2c->st0 = H2_CS_FRAME_E;
1966 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001967}
1968
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001969/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1970 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1971 * errors here are reported as connection errors since it's impossible to
1972 * recover from such errors after the compression context has been altered.
1973 */
1974static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1975{
1976 int error;
1977
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001978 if (!b_size(&h2c->dbuf))
1979 return NULL; // empty buffer
1980
1981 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1982 return NULL; // incomplete frame
1983
Willy Tarreau1915ca22019-01-24 11:49:37 +01001984 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001985
Willy Tarreau25919232019-01-03 14:48:18 +01001986 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001987 if (h2c->st0 >= H2_CS_ERROR)
1988 return NULL;
1989
Willy Tarreau08bb1d62019-01-30 16:55:48 +01001990 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1991 /* RFC7540#5.1 */
1992 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1993 h2c->st0 = H2_CS_FRAME_E;
1994 return NULL;
1995 }
1996
Willy Tarreau25919232019-01-03 14:48:18 +01001997 if (error <= 0) {
1998 if (error == 0)
1999 return NULL; // missing data
2000
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002001 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002002 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002003 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002004 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002005 }
2006
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002007 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2008 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002009 if (h2s->cs)
2010 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002011 }
2012
Willy Tarreau927b88b2019-03-04 08:03:25 +01002013 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002014 h2s->st = H2_SS_ERROR;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002015 else if (h2s->cs && h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002016 h2s->st = H2_SS_HREM;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002017 else if ((!h2s || h2s->cs->flags & CS_FL_REOS) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002018 h2s_close(h2s);
2019
2020 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002021}
2022
Willy Tarreau454f9052017-10-26 19:40:35 +02002023/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2024 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2025 */
2026static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2027{
2028 int error;
2029
2030 /* note that empty DATA frames are perfectly valid and sometimes used
2031 * to signal an end of stream (with the ES flag).
2032 */
2033
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002035 return 0; // empty buffer
2036
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002037 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002038 return 0; // incomplete frame
2039
2040 /* now either the frame is complete or the buffer is complete */
2041
Willy Tarreau454f9052017-10-26 19:40:35 +02002042 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2043 /* RFC7540#6.1 */
2044 error = H2_ERR_STREAM_CLOSED;
2045 goto strm_err;
2046 }
2047
Willy Tarreau1915ca22019-01-24 11:49:37 +01002048 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2049 /* RFC7540#8.1.2 */
2050 error = H2_ERR_PROTOCOL_ERROR;
2051 goto strm_err;
2052 }
2053
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002054 if (!h2_frt_transfer_data(h2s))
2055 return 0;
2056
Willy Tarreau454f9052017-10-26 19:40:35 +02002057 /* call the upper layers to process the frame, then let the upper layer
2058 * notify the stream about any change.
2059 */
2060 if (!h2s->cs) {
2061 error = H2_ERR_STREAM_CLOSED;
2062 goto strm_err;
2063 }
2064
Willy Tarreau8f650c32017-11-21 19:36:21 +01002065 if (h2c->st0 >= H2_CS_ERROR)
2066 return 0;
2067
Willy Tarreau721c9742017-11-07 11:05:42 +01002068 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002069 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002070 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002071 }
2072
2073 /* check for completion : the callee will change this to FRAME_A or
2074 * FRAME_H once done.
2075 */
2076 if (h2c->st0 == H2_CS_FRAME_P)
2077 return 0;
2078
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002079 /* last frame */
2080 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002081 if (h2s->st == H2_SS_OPEN)
2082 h2s->st = H2_SS_HREM;
2083 else
2084 h2s_close(h2s);
2085
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002086 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002087 if (h2s->cs)
2088 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002089
2090 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2091 /* RFC7540#8.1.2 */
2092 error = H2_ERR_PROTOCOL_ERROR;
2093 goto strm_err;
2094 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002095 }
2096
Willy Tarreau454f9052017-10-26 19:40:35 +02002097 return 1;
2098
Willy Tarreau454f9052017-10-26 19:40:35 +02002099 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002100 h2s_error(h2s, error);
2101 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002102 return 0;
2103}
2104
Willy Tarreaubc933932017-10-09 16:21:43 +02002105/* process Rx frames to be demultiplexed */
2106static void h2_process_demux(struct h2c *h2c)
2107{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002108 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002109 struct h2_fh hdr;
2110 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002111
Willy Tarreau081d4722017-05-16 21:51:05 +02002112 if (h2c->st0 >= H2_CS_ERROR)
2113 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002114
2115 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2116 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002117 if (h2c->flags & H2_CF_IS_BACK)
2118 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002119 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2120 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002121 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002122 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002123 sess_log(h2c->conn->owner);
2124 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002125 goto fail;
2126 }
2127
2128 h2c->max_id = 0;
2129 h2c->st0 = H2_CS_SETTINGS1;
2130 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002131
2132 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002133 /* ensure that what is pending is a valid SETTINGS frame
2134 * without an ACK.
2135 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002136 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002137 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002138 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002139 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002140 sess_log(h2c->conn->owner);
2141 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002142 goto fail;
2143 }
2144
2145 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2146 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2147 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2148 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002149 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002150 goto fail;
2151 }
2152
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002153 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002154 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2155 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2156 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002157 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002158 goto fail;
2159 }
2160
Willy Tarreau3bf69182018-12-21 15:34:50 +01002161 /* that's OK, switch to FRAME_P to process it. This is
2162 * a SETTINGS frame whose header has already been
2163 * deleted above.
2164 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002165 padlen = 0;
2166 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002167 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002168 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002169
2170 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002171 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002172 int ret = 0;
2173
2174 if (h2c->st0 >= H2_CS_ERROR)
2175 break;
2176
2177 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002178 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002179 break;
2180
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002181 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002182 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002183 if (!h2c->nb_streams) {
2184 /* only log if no other stream can report the error */
2185 sess_log(h2c->conn->owner);
2186 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002187 break;
2188 }
2189
Willy Tarreau3bf69182018-12-21 15:34:50 +01002190 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2191 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2192 * we read the pad length and drop it from the remaining
2193 * payload (one byte + the 9 remaining ones = 10 total
2194 * removed), so we have a frame payload starting after the
2195 * pad len. Flow controlled frames (DATA) also count the
2196 * padlen in the flow control, so it must be adjusted.
2197 */
2198 if (hdr.len < 1) {
2199 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2200 sess_log(h2c->conn->owner);
2201 goto fail;
2202 }
2203 hdr.len--;
2204
2205 if (b_data(&h2c->dbuf) < 10)
2206 break; // missing padlen
2207
2208 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2209
2210 if (padlen > hdr.len) {
2211 /* RFC7540#6.1 : pad length = length of
2212 * frame payload or greater => error.
2213 */
2214 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2215 sess_log(h2c->conn->owner);
2216 goto fail;
2217 }
2218
2219 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2220 h2c->rcvd_c++;
2221 h2c->rcvd_s++;
2222 }
2223 b_del(&h2c->dbuf, 1);
2224 }
2225 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002226
2227 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002228 h2c->dfl = hdr.len;
2229 h2c->dsi = hdr.sid;
2230 h2c->dft = hdr.ft;
2231 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002232 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002234
2235 /* check for minimum basic frame format validity */
2236 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2237 if (ret != H2_ERR_NO_ERROR) {
2238 h2c_error(h2c, ret);
2239 sess_log(h2c->conn->owner);
2240 goto fail;
2241 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002242 }
2243
2244 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002245 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2246
Willy Tarreau567beb82018-12-18 16:52:44 +01002247 if (tmp_h2s != h2s && h2s && h2s->cs &&
2248 (b_data(&h2s->rxbuf) ||
2249 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002250 /* we may have to signal the upper layers */
2251 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002252 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002253 }
2254 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002255
Willy Tarreaud7901432017-12-29 11:34:40 +01002256 if (h2c->st0 == H2_CS_FRAME_E)
2257 goto strm_err;
2258
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002259 if (h2s->st == H2_SS_IDLE &&
2260 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2261 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2262 * this state MUST be treated as a connection error
2263 */
2264 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002265 if (!h2c->nb_streams) {
2266 /* only log if no other stream can report the error */
2267 sess_log(h2c->conn->owner);
2268 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002269 break;
2270 }
2271
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002272 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2273 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2274 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002275 * this state MUST be treated as a stream error.
2276 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2277 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002278 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002279 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2280 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2281 else
2282 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002283 goto strm_err;
2284 }
2285
Willy Tarreauab837502017-12-27 15:07:30 +01002286 /* Below the management of frames received in closed state is a
2287 * bit hackish because the spec makes strong differences between
2288 * streams closed by receiving RST, sending RST, and seeing ES
2289 * in both directions. In addition to this, the creation of a
2290 * new stream reusing the identifier of a closed one will be
2291 * detected here. Given that we cannot keep track of all closed
2292 * streams forever, we consider that unknown closed streams were
2293 * closed on RST received, which allows us to respond with an
2294 * RST without breaking the connection (eg: to abort a transfer).
2295 * Some frames have to be silently ignored as well.
2296 */
2297 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002298 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002299 /* #5.1.1: The identifier of a newly
2300 * established stream MUST be numerically
2301 * greater than all streams that the initiating
2302 * endpoint has opened or reserved. This
2303 * governs streams that are opened using a
2304 * HEADERS frame and streams that are reserved
2305 * using PUSH_PROMISE. An endpoint that
2306 * receives an unexpected stream identifier
2307 * MUST respond with a connection error.
2308 */
2309 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2310 goto strm_err;
2311 }
2312
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002313 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002314 /* RFC7540#5.1:closed: an endpoint that
2315 * receives any frame other than PRIORITY after
2316 * receiving a RST_STREAM MUST treat that as a
2317 * stream error of type STREAM_CLOSED.
2318 *
2319 * Note that old streams fall into this category
2320 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002321 *
2322 * However, we cannot generalize this to all frame types. Those
2323 * carrying compression state must still be processed before
2324 * being dropped or we'll desynchronize the decoder. This can
2325 * happen with request trailers received after sending an
2326 * RST_STREAM, or with header/trailers responses received after
2327 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002328 */
2329 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2330 h2c->st0 = H2_CS_FRAME_E;
2331 goto strm_err;
2332 }
2333
2334 /* RFC7540#5.1:closed: if this state is reached as a
2335 * result of sending a RST_STREAM frame, the peer that
2336 * receives the RST_STREAM might have already sent
2337 * frames on the stream that cannot be withdrawn. An
2338 * endpoint MUST ignore frames that it receives on
2339 * closed streams after it has sent a RST_STREAM
2340 * frame. An endpoint MAY choose to limit the period
2341 * over which it ignores frames and treat frames that
2342 * arrive after this time as being in error.
2343 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002344 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002345 /* RFC7540#5.1:closed: any frame other than
2346 * PRIO/WU/RST in this state MUST be treated as
2347 * a connection error
2348 */
2349 if (h2c->dft != H2_FT_RST_STREAM &&
2350 h2c->dft != H2_FT_PRIORITY &&
2351 h2c->dft != H2_FT_WINDOW_UPDATE) {
2352 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2353 goto strm_err;
2354 }
2355 }
2356 }
2357
Willy Tarreauc0da1962017-10-30 18:38:00 +01002358#if 0
2359 // problem below: it is not possible to completely ignore such
2360 // streams as we need to maintain the compression state as well
2361 // and for this we need to completely process these frames (eg:
2362 // HEADERS frames) as well as counting DATA frames to emit
2363 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2364 // This is a typical case of layer violation where the
2365 // transported contents are critical to the connection's
2366 // validity and must be ignored at the same time :-(
2367
2368 /* graceful shutdown, ignore streams whose ID is higher than
2369 * the one advertised in GOAWAY. RFC7540#6.8.
2370 */
2371 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002372 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2373 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002374 h2c->dfl -= ret;
2375 ret = h2c->dfl == 0;
2376 goto strm_err;
2377 }
2378#endif
2379
Willy Tarreau7e98c052017-10-10 15:56:59 +02002380 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002381 case H2_FT_SETTINGS:
2382 if (h2c->st0 == H2_CS_FRAME_P)
2383 ret = h2c_handle_settings(h2c);
2384
2385 if (h2c->st0 == H2_CS_FRAME_A)
2386 ret = h2c_ack_settings(h2c);
2387 break;
2388
Willy Tarreaucf68c782017-10-10 17:11:41 +02002389 case H2_FT_PING:
2390 if (h2c->st0 == H2_CS_FRAME_P)
2391 ret = h2c_handle_ping(h2c);
2392
2393 if (h2c->st0 == H2_CS_FRAME_A)
2394 ret = h2c_ack_ping(h2c);
2395 break;
2396
Willy Tarreau26f95952017-07-27 17:18:30 +02002397 case H2_FT_WINDOW_UPDATE:
2398 if (h2c->st0 == H2_CS_FRAME_P)
2399 ret = h2c_handle_window_update(h2c, h2s);
2400 break;
2401
Willy Tarreau61290ec2017-10-17 08:19:21 +02002402 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002403 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2404 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2405 * frames' parsers consume all following CONTINUATION
2406 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002407 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002408 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2409 sess_log(h2c->conn->owner);
2410 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002411
Willy Tarreau13278b42017-10-13 19:23:14 +02002412 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002413 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002414 if (h2c->flags & H2_CF_IS_BACK)
2415 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2416 else
2417 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002418 if (tmp_h2s) {
2419 h2s = tmp_h2s;
2420 ret = 1;
2421 }
2422 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002423 break;
2424
Willy Tarreau454f9052017-10-26 19:40:35 +02002425 case H2_FT_DATA:
2426 if (h2c->st0 == H2_CS_FRAME_P)
2427 ret = h2c_frt_handle_data(h2c, h2s);
2428
2429 if (h2c->st0 == H2_CS_FRAME_A)
2430 ret = h2c_send_strm_wu(h2c);
2431 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002432
Willy Tarreau92153fc2017-12-03 19:46:19 +01002433 case H2_FT_PRIORITY:
2434 if (h2c->st0 == H2_CS_FRAME_P)
2435 ret = h2c_handle_priority(h2c);
2436 break;
2437
Willy Tarreaucd234e92017-08-18 10:59:39 +02002438 case H2_FT_RST_STREAM:
2439 if (h2c->st0 == H2_CS_FRAME_P)
2440 ret = h2c_handle_rst_stream(h2c, h2s);
2441 break;
2442
Willy Tarreaue96b0922017-10-30 00:28:29 +01002443 case H2_FT_GOAWAY:
2444 if (h2c->st0 == H2_CS_FRAME_P)
2445 ret = h2c_handle_goaway(h2c);
2446 break;
2447
Willy Tarreau1c661982017-10-30 13:52:01 +01002448 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002449 default:
2450 /* drop frames that we ignore. They may be larger than
2451 * the buffer so we drain all of their contents until
2452 * we reach the end.
2453 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2455 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002456 h2c->dfl -= ret;
2457 ret = h2c->dfl == 0;
2458 }
2459
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002460 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002461 /* We may have to send an RST if not done yet */
2462 if (h2s->st == H2_SS_ERROR)
2463 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002464
Willy Tarreaua20a5192017-12-27 11:02:06 +01002465 if (h2c->st0 == H2_CS_FRAME_E)
2466 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002467
Willy Tarreau7e98c052017-10-10 15:56:59 +02002468 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002469 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002470 break;
2471
2472 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002473 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002474 h2c->st0 = H2_CS_FRAME_H;
2475 }
2476 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002477
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002478 if (h2c->rcvd_c > 0 &&
2479 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2480 h2c_send_conn_wu(h2c);
2481
Willy Tarreau52eed752017-09-22 15:05:09 +02002482 fail:
2483 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002484 if (h2s && h2s->cs &&
2485 (b_data(&h2s->rxbuf) ||
2486 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002487 /* we may have to signal the upper layers */
2488 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002489 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002490 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002491
Willy Tarreau47b515a2018-12-21 16:09:41 +01002492 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002493}
2494
2495/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2496 * the end.
2497 */
2498static int h2_process_mux(struct h2c *h2c)
2499{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002500 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002501
Willy Tarreau01b44822018-10-03 14:26:37 +02002502 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2503 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2504 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2505 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2506 if (h2c->st0 == H2_CS_ERROR) {
2507 h2c->st0 = H2_CS_ERROR2;
2508 sess_log(h2c->conn->owner);
2509 }
2510 goto fail;
2511 }
2512 h2c->st0 = H2_CS_SETTINGS1;
2513 }
2514 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002515 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002516 return 1;
2517 }
2518
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002519 /* start by sending possibly pending window updates */
2520 if (h2c->rcvd_c > 0 &&
2521 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2522 h2c_send_conn_wu(h2c) < 0)
2523 goto fail;
2524
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002525 /* First we always process the flow control list because the streams
2526 * waiting there were already elected for immediate emission but were
2527 * blocked just on this.
2528 */
2529
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002530 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002531 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2532 h2c->st0 >= H2_CS_ERROR)
2533 break;
2534
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002535 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002536 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2537 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002539 LIST_DEL(&h2s->list);
2540 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002541 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002542 }
2543
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002544 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002545 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2546 break;
2547
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002548 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002549 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2550 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002551 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002552 LIST_DEL(&h2s->list);
2553 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002554 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002555 }
2556
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002557 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002558 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002559 if (h2c->st0 == H2_CS_ERROR) {
2560 if (h2c->max_id >= 0) {
2561 h2c_send_goaway_error(h2c, NULL);
2562 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2563 return 0;
2564 }
2565
2566 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2567 }
2568 return 1;
2569 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002570 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002571}
2572
Willy Tarreau62f52692017-10-08 23:01:42 +02002573
Willy Tarreau479998a2018-11-18 06:30:59 +01002574/* Attempt to read data, and subscribe if none available.
2575 * The function returns 1 if data has been received, otherwise zero.
2576 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002577static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002578{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002579 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002580 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002581 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002582 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002583
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002584 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002585 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002586
Willy Tarreau315d8072017-12-10 22:17:57 +01002587 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002588 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002589
Willy Tarreau44e973f2018-03-01 17:49:30 +01002590 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002591 if (!buf) {
2592 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002593 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002594 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002595
Olivier Houchard7505f942018-08-21 18:10:44 +02002596 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002597 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002598 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2599 /* HTX in use : try to pre-align the buffer like the
2600 * rxbufs will be to optimize memory copies. We'll make
2601 * sure that the frame header lands at the end of the
2602 * HTX block to alias it upon recv. We cannot use the
2603 * head because rcv_buf() will realign the buffer if
2604 * it's empty. Thus we cheat and pretend we already
2605 * have a few bytes there.
2606 */
2607 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002608 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002609 }
2610 else
2611 max = b_room(buf);
2612
Olivier Houchard7505f942018-08-21 18:10:44 +02002613 if (max)
2614 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2615 else
2616 ret = 0;
2617 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002618
Olivier Houchard53216e72018-10-10 15:46:36 +02002619 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002620 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002621
Olivier Houcharda1411e62018-08-17 18:42:48 +02002622 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002623 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002624 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002625 }
2626
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002627 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002628 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002629 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002630}
2631
Willy Tarreau479998a2018-11-18 06:30:59 +01002632/* Try to send data if possible.
2633 * The function returns 1 if data have been sent, otherwise zero.
2634 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002635static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002636{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002637 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002638 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002639 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002640
2641 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002642 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002643
Olivier Houchard7505f942018-08-21 18:10:44 +02002644
Willy Tarreaua2af5122017-10-09 11:56:46 +02002645 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2646 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002647 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002648 }
2649
Willy Tarreaubc933932017-10-09 16:21:43 +02002650 /* This loop is quite simple : it tries to fill as much as it can from
2651 * pending streams into the existing buffer until it's reportedly full
2652 * or the end of send requests is reached. Then it tries to send this
2653 * buffer's contents out, marks it not full if at least one byte could
2654 * be sent, and tries again.
2655 *
2656 * The snd_buf() function normally takes a "flags" argument which may
2657 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2658 * data immediately comes and CO_SFL_STREAMER to indicate that the
2659 * connection is streaming lots of data (used to increase TLS record
2660 * size at the expense of latency). The former can be sent any time
2661 * there's a buffer full flag, as it indicates at least one stream
2662 * attempted to send and failed so there are pending data. An
2663 * alternative would be to set it as long as there's an active stream
2664 * but that would be problematic for ACKs until we have an absolute
2665 * guarantee that all waiters have at least one byte to send. The
2666 * latter should possibly not be set for now.
2667 */
2668
2669 done = 0;
2670 while (!done) {
2671 unsigned int flags = 0;
2672
2673 /* fill as much as we can into the current buffer */
2674 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2675 done = h2_process_mux(h2c);
2676
Olivier Houchard2b094432019-01-29 18:28:36 +01002677 if (h2c->flags & H2_CF_MUX_MALLOC)
2678 break;
2679
Willy Tarreaubc933932017-10-09 16:21:43 +02002680 if (conn->flags & CO_FL_ERROR)
2681 break;
2682
2683 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2684 flags |= CO_SFL_MSG_MORE;
2685
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002686 if (b_data(&h2c->mbuf)) {
2687 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002688 if (!ret)
2689 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002690 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002691 b_del(&h2c->mbuf, ret);
2692 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002693 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002694
2695 /* wrote at least one byte, the buffer is not full anymore */
2696 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2697 }
2698
Willy Tarreaua2af5122017-10-09 11:56:46 +02002699 if (conn->flags & CO_FL_SOCK_WR_SH) {
2700 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002701 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002702 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002703 /* We're not full anymore, so we can wake any task that are waiting
2704 * for us.
2705 */
2706 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002707 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002708 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2709 struct h2s *, list);
2710 LIST_DEL(&h2s->list);
2711 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002712 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002713 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2714 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002715 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002716 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002717 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002718 /* We're done, no more to send */
2719 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002720 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002721schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002722 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2723 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002724 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002725}
2726
2727static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2728{
2729 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002730 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002731
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002732 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002733 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002734 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002735 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002736 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002737 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002738 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002739}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002740
Willy Tarreau62f52692017-10-08 23:01:42 +02002741/* callback called on any event by the connection handler.
2742 * It applies changes and returns zero, or < 0 if it wants immediate
2743 * destruction of the connection (which normally doesn not happen in h2).
2744 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002745static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002746{
Olivier Houchard7505f942018-08-21 18:10:44 +02002747 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002748
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002749 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002750 h2_process_demux(h2c);
2751
2752 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002753 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002754
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002755 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002756 h2c->flags &= ~H2_CF_DEM_DFULL;
2757 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002758 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002759
Willy Tarreau0b37d652018-10-03 10:33:02 +02002760 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002761 /* frontend is stopping, reload likely in progress, let's try
2762 * to announce a graceful shutdown if not yet done. We don't
2763 * care if it fails, it will be tried again later.
2764 */
2765 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2766 if (h2c->last_sid < 0)
2767 h2c->last_sid = (1U << 31) - 1;
2768 h2c_send_goaway_error(h2c, NULL);
2769 }
2770 }
2771
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002772 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002773 * If we received early data, and the handshake is done, wake
2774 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002775 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002776 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2777 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2778 struct eb32_node *node;
2779 struct h2s *h2s;
2780
2781 h2c->flags |= H2_CF_WAIT_FOR_HS;
2782 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2783
2784 while (node) {
2785 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002786 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002787 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002788 node = eb32_next(node);
2789 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002790 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002791
Willy Tarreau26bd7612017-10-09 16:47:04 +02002792 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002793 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2794 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2795 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002796 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002797
2798 if (eb_is_empty(&h2c->streams_by_id)) {
2799 /* no more stream, kill the connection now */
2800 h2_release(conn);
2801 return -1;
2802 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002803 }
2804
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002805 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002806 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002807
Olivier Houchard53216e72018-10-10 15:46:36 +02002808 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2809 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2810 (h2c->st0 != H2_CS_ERROR &&
2811 !b_data(&h2c->mbuf) &&
2812 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2813 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002814 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002815
Willy Tarreau3f133572017-10-31 19:21:06 +01002816 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002817 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002818 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002819 task_queue(h2c->task);
2820 }
2821 else
2822 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002823 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002824
Olivier Houchard7505f942018-08-21 18:10:44 +02002825 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002826 return 0;
2827}
2828
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002829static int h2_wake(struct connection *conn)
2830{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002831 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002832
2833 return (h2_process(h2c));
2834}
2835
Willy Tarreauea392822017-10-31 10:02:25 +01002836/* Connection timeout management. The principle is that if there's no receipt
2837 * nor sending for a certain amount of time, the connection is closed. If the
2838 * MUX buffer still has lying data or is not allocatable, the connection is
2839 * immediately killed. If it's allocatable and empty, we attempt to send a
2840 * GOAWAY frame.
2841 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002842static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002843{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002844 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002845 int expired = tick_is_expired(t->expire, now_ms);
2846
Willy Tarreau0975f112018-03-29 15:22:59 +02002847 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002848 return t;
2849
Willy Tarreau0975f112018-03-29 15:22:59 +02002850 task_delete(t);
2851 task_free(t);
2852
2853 if (!h2c) {
2854 /* resources were already deleted */
2855 return NULL;
2856 }
2857
2858 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002859 h2c_error(h2c, H2_ERR_NO_ERROR);
2860 h2_wake_some_streams(h2c, 0, 0);
2861
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002862 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002863 /* don't even try to send a GOAWAY, the buffer is stuck */
2864 h2c->flags |= H2_CF_GOAWAY_FAILED;
2865 }
2866
2867 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002868 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002869 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2870 h2c->flags |= H2_CF_GOAWAY_FAILED;
2871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2873 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002874 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002875 b_del(&h2c->mbuf, ret);
2876 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002877 }
2878 }
Willy Tarreauea392822017-10-31 10:02:25 +01002879
Willy Tarreau0975f112018-03-29 15:22:59 +02002880 /* either we can release everything now or it will be done later once
2881 * the last stream closes.
2882 */
2883 if (eb_is_empty(&h2c->streams_by_id))
2884 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002885
Willy Tarreauea392822017-10-31 10:02:25 +01002886 return NULL;
2887}
2888
2889
Willy Tarreau62f52692017-10-08 23:01:42 +02002890/*******************************************/
2891/* functions below are used by the streams */
2892/*******************************************/
2893
2894/*
2895 * Attach a new stream to a connection
2896 * (Used for outgoing connections)
2897 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002898static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002899{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002900 struct conn_stream *cs;
2901 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002902 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002903
2904 cs = cs_new(conn);
2905 if (!cs)
2906 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002907 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002908 if (!h2s) {
2909 cs_free(cs);
2910 return NULL;
2911 }
2912 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002913}
2914
Willy Tarreaufafd3982018-11-18 21:29:20 +01002915/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2916 * We have to scan because we may have some orphan streams. It might be
2917 * beneficial to scan backwards from the end to reduce the likeliness to find
2918 * orphans.
2919 */
2920static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2921{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002922 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002923 struct h2s *h2s;
2924 struct eb32_node *node;
2925
2926 node = eb32_first(&h2c->streams_by_id);
2927 while (node) {
2928 h2s = container_of(node, struct h2s, by_id);
2929 if (h2s->cs)
2930 return h2s->cs;
2931 node = eb32_next(node);
2932 }
2933 return NULL;
2934}
2935
Willy Tarreau62f52692017-10-08 23:01:42 +02002936/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002937 * Destroy the mux and the associated connection, if it is no longer used
2938 */
2939static void h2_destroy(struct connection *conn)
2940{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002941 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002942
2943 if (eb_is_empty(&h2c->streams_by_id))
2944 h2_release(h2c->conn);
2945}
2946
2947/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002948 * Detach the stream from the connection and possibly release the connection.
2949 */
2950static void h2_detach(struct conn_stream *cs)
2951{
Willy Tarreau60935142017-10-16 18:11:19 +02002952 struct h2s *h2s = cs->ctx;
2953 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002954 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002955
2956 cs->ctx = NULL;
2957 if (!h2s)
2958 return;
2959
Olivier Houchardf502aca2018-12-14 19:42:40 +01002960 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002961 h2c = h2s->h2c;
2962 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002963 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01002964 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
2965 !h2_frt_has_too_many_cs(h2c)) {
2966 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02002967 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002968 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002969 }
Willy Tarreau60935142017-10-16 18:11:19 +02002970
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002971 /* this stream may be blocked waiting for some data to leave (possibly
2972 * an ES or RST frame), so orphan it in this case.
2973 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002974 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002975 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002976 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002977 return;
2978
Willy Tarreau45f752e2017-10-30 15:44:59 +01002979 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2980 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2981 /* unblock the connection if it was blocked on this
2982 * stream.
2983 */
2984 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2985 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002986 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002987 }
2988
Willy Tarreau71049cc2018-03-28 13:56:39 +02002989 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002990
Olivier Houchard8a786902018-12-15 16:05:40 +01002991 if (h2c->flags & H2_CF_IS_BACK &&
2992 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002993 if (!(h2c->conn->flags &
2994 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2995 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002996 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002997 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2998 h2c->conn->owner = NULL;
2999 if (eb_is_empty(&h2c->streams_by_id)) {
3000 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3001 /* The server doesn't want it, let's kill the connection right away */
3002 h2c->conn->mux->destroy(h2c->conn);
3003 return;
3004 }
3005 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003006 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003007 if (eb_is_empty(&h2c->streams_by_id)) {
3008 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3009 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3010 return;
3011 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003012 /* Never ever allow to reuse a connection from a non-reuse backend */
3013 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3014 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003015 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003016 struct server *srv = objt_server(h2c->conn->target);
3017
3018 if (srv) {
3019 if (h2c->conn->flags & CO_FL_PRIVATE)
3020 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3021 else
3022 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3023 }
3024
3025 }
3026 }
3027 }
3028
Willy Tarreaue323f342018-03-28 13:51:45 +02003029 /* We don't want to close right now unless we're removing the
3030 * last stream, and either the connection is in error, or it
3031 * reached the ID already specified in a GOAWAY frame received
3032 * or sent (as seen by last_sid >= 0).
3033 */
3034 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3035 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003036 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard93c88522018-11-30 15:39:16 +01003037 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003038 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003039 (conn_xprt_read0_pending(h2c->conn) ||
3040 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3041 /* no more stream will come, kill it now */
3042 h2_release(h2c->conn);
3043 }
3044 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003045 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003046 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3047 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003048 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003049 else
3050 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003051 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003052}
3053
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003054static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003055{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003056 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003057 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003058
Willy Tarreau721c9742017-11-07 11:05:42 +01003059 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003060 return;
3061
Willy Tarreau18059042019-01-31 19:12:48 +01003062 /* a connstream may require us to immediately kill the whole connection
3063 * for example because of a "tcp-request content reject" rule that is
3064 * normally used to limit abuse. In this case we schedule a goaway to
3065 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003066 */
Willy Tarreau18059042019-01-31 19:12:48 +01003067 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3068 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3069 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3070 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3071 }
3072
Willy Tarreau90c32322017-11-24 08:00:30 +01003073 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003074 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003075 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003076
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003077 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003078 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003079 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003080
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003081 return;
3082add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003083 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003084 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003085 if (h2s->flags & H2_SF_BLK_MFCTL) {
3086 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3087 h2s->send_wait = sw;
3088 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3089 h2s->send_wait = sw;
3090 LIST_ADDQ(&h2c->send_list, &h2s->list);
3091 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003092 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003093 /* Let the handler know we want shutr */
3094 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003095}
3096
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003097static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003098{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003099 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003100 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003101
Willy Tarreau721c9742017-11-07 11:05:42 +01003102 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003103 return;
3104
Willy Tarreau67434202017-11-06 20:20:51 +01003105 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003106 /* we can cleanly close using an empty data frame only after headers */
3107
3108 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3109 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003110 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003111
3112 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003113 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003114 else
3115 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003116 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003117 /* a connstream may require us to immediately kill the whole connection
3118 * for example because of a "tcp-request content reject" rule that is
3119 * normally used to limit abuse. In this case we schedule a goaway to
3120 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003121 */
Willy Tarreau18059042019-01-31 19:12:48 +01003122 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3123 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3124 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3125 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3126 }
3127
Willy Tarreau90c32322017-11-24 08:00:30 +01003128 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003129 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003130 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003131
Willy Tarreau00dd0782018-03-01 16:31:34 +01003132 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003133 }
3134
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003135 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003136 tasklet_wakeup(h2c->wait_event.task);
3137 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003138
3139 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003140 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003141 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 if (h2s->flags & H2_SF_BLK_MFCTL) {
3143 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3144 h2s->send_wait = sw;
3145 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3146 h2s->send_wait = sw;
3147 LIST_ADDQ(&h2c->send_list, &h2s->list);
3148 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003149 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003150 /* let the handler know we want to shutw */
3151 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003152}
3153
3154static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3155{
3156 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003157 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003158
Olivier Houchard2c68a462018-12-15 22:42:20 +01003159 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003160 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003161 h2s->send_wait = NULL;
3162 LIST_DEL(&h2s->list);
3163 LIST_INIT(&h2s->list);
3164 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003165 if (reason & 2)
3166 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003167 if (reason & 1)
3168 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003169
Olivier Houchard2c68a462018-12-15 22:42:20 +01003170 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003171 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003172 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003173 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003174}
3175
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003176static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3177{
3178 struct h2s *h2s = cs->ctx;
3179
3180 if (!mode)
3181 return;
3182
3183 h2_do_shutr(h2s);
3184}
3185
3186static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3187{
3188 struct h2s *h2s = cs->ctx;
3189
3190 h2_do_shutw(h2s);
3191}
3192
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003193/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003194 * HTX request or response depending on the connection's side. Returns a
3195 * positive value on success, a negative value on failure, or 0 if it couldn't
3196 * proceed. May report connection errors in h2c->errcode if the frame is
3197 * non-decodable and the connection unrecoverable. In absence of connection
3198 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003199 *
3200 * The function may fold CONTINUATION frames into the initial HEADERS frame
3201 * by removing padding and next frame header, then moving the CONTINUATION
3202 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3203 * leaving a hole between the main frame and the beginning of the next one.
3204 * The possibly remaining incomplete or next frame at the end may be moved
3205 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3206 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3207 *
3208 * A buffer at the beginning of processing may look like this :
3209 *
3210 * ,---.---------.-----.--------------.--------------.------.---.
3211 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3212 * `---^---------^-----^--------------^--------------^------^---'
3213 * | | <-----> | |
3214 * area | dpl | wrap
3215 * |<--------------> |
3216 * | dfl |
3217 * |<-------------------------------------------------->|
3218 * head data
3219 *
3220 * Padding is automatically overwritten when folding, participating to the
3221 * hole size after dfl :
3222 *
3223 * ,---.------------------------.-----.--------------.------.---.
3224 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3225 * `---^------------------------^-----^--------------^------^---'
3226 * | | <-----> | |
3227 * area | hole | wrap
3228 * |<-----------------------> |
3229 * | dfl |
3230 * |<-------------------------------------------------->|
3231 * head data
3232 *
3233 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3234 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3235 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003236 *
3237 * The <flags> field must point to either the stream's flags or to a copy of it
3238 * so that the function can update the following flags :
3239 * - H2_SF_DATA_CLEN when content-length is seen
3240 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3241 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003242 *
3243 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3244 * decoding, in order to detect if we're dealing with a headers or a trailers
3245 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003246 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003247static 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 +02003248{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003249 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003250 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003251 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003252 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003253 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003254 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003255 int flen; // header frame len
3256 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003257 int ret = 0;
3258 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003259 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003260 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003261
Willy Tarreauea18f862018-12-22 20:19:26 +01003262next_frame:
3263 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3264 goto leave; // incomplete input frame
3265
3266 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3267 * this case, we'll try to paste it immediately after the initial
3268 * HEADERS frame payload and kill any possible padding. The initial
3269 * frame's length will be increased to represent the concatenation
3270 * of the two frames. The next frame is read from position <tlen>
3271 * and written at position <flen> (minus padding if some is present).
3272 */
3273 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3274 struct h2_fh hdr;
3275 int clen; // CONTINUATION frame's payload length
3276
3277 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3278 /* no more data, the buffer may be full, either due to
3279 * too large a frame or because of too large a hole that
3280 * we're going to compact at the end.
3281 */
3282 goto leave;
3283 }
3284
3285 if (hdr.ft != H2_FT_CONTINUATION) {
3286 /* RFC7540#6.10: frame of unexpected type */
3287 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3288 goto fail;
3289 }
3290
3291 if (hdr.sid != h2c->dsi) {
3292 /* RFC7540#6.10: frame of different stream */
3293 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3294 goto fail;
3295 }
3296
3297 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3298 /* RFC7540#4.2: invalid frame length */
3299 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3300 goto fail;
3301 }
3302
3303 /* detect when we must stop aggragating frames */
3304 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3305
3306 /* Take as much as we can of the CONTINUATION frame's payload */
3307 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3308 if (clen > hdr.len)
3309 clen = hdr.len;
3310
3311 /* Move the frame's payload over the padding, hole and frame
3312 * header. At least one of hole or dpl is null (see diagrams
3313 * above). The hole moves after the new aggragated frame.
3314 */
3315 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3316 h2c->dfl += clen - h2c->dpl;
3317 hole += h2c->dpl + 9;
3318 h2c->dpl = 0;
3319 goto next_frame;
3320 }
3321
3322 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003323
Willy Tarreau13278b42017-10-13 19:23:14 +02003324 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003325 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003326 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003327 copy = alloc_trash_chunk();
3328 if (!copy) {
3329 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3330 goto fail;
3331 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003332 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3333 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3334 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003335 }
3336
Willy Tarreau13278b42017-10-13 19:23:14 +02003337 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3338 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003339 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003340 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3341 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003342 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003343 }
3344
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003345 if (flen < 5) {
3346 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3347 goto fail;
3348 }
3349
Willy Tarreau13278b42017-10-13 19:23:14 +02003350 hdrs += 5; // stream dep = 4, weight = 1
3351 flen -= 5;
3352 }
3353
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003354 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003355 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003356 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003357 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003358
Willy Tarreau937f7602018-02-26 15:22:17 +01003359 /* we can't retry a failed decompression operation so we must be very
3360 * careful not to take any risks. In practice the output buffer is
3361 * always empty except maybe for trailers, in which case we simply have
3362 * to wait for the upper layer to finish consuming what is available.
3363 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003364
3365 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003366 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003367 if (!htx_is_empty(htx)) {
3368 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003369 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003370 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003371 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003372 if (b_data(rxbuf)) {
3373 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003374 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003375 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003376
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003377 rxbuf->head = 0;
3378 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003379 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003380
Willy Tarreau25919232019-01-03 14:48:18 +01003381 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003382 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3383 sizeof(list)/sizeof(list[0]), tmp);
3384 if (outlen < 0) {
3385 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3386 goto fail;
3387 }
3388
Willy Tarreau25919232019-01-03 14:48:18 +01003389 /* The PACK decompressor was updated, let's update the input buffer and
3390 * the parser's state to commit these changes and allow us to later
3391 * fail solely on the stream if needed.
3392 */
3393 b_del(&h2c->dbuf, h2c->dfl + hole);
3394 h2c->dfl = hole = 0;
3395 h2c->st0 = H2_CS_FRAME_H;
3396
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003397 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003398 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003399
Willy Tarreau88d138e2019-01-02 19:38:14 +01003400 if (*flags & H2_SF_HEADERS_RCVD)
3401 goto trailers;
3402
3403 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003404 if (htx) {
3405 /* HTX mode */
3406 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003407 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003408 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003409 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003410 } else {
3411 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003412 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003413 if (outlen > 0)
3414 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003415 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003416
3417 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003418 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003419 goto fail;
3420 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003421
Willy Tarreau174b06a2018-04-25 18:13:58 +02003422 if (msgf & H2_MSGF_BODY) {
3423 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003424 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003425 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003426 if (htx)
3427 htx->extra = *body_len;
3428 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003429 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003430 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003431 }
3432
Willy Tarreau88d138e2019-01-02 19:38:14 +01003433 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003434 /* indicate that a HEADERS frame was received for this stream, except
3435 * for 1xx responses. For 1xx responses, another HEADERS frame is
3436 * expected.
3437 */
3438 if (!(msgf & H2_MSGF_RSP_1XX))
3439 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003440
Christopher Faulet0b465482019-02-19 15:14:23 +01003441 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003442 /* Mark the end of message, either using EOM in HTX or with the
3443 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3444 * is not set during headers with END_STREAM.
3445 */
3446 if (htx) {
3447 if (!htx_add_endof(htx, HTX_BLK_EOM))
3448 goto fail;
3449 }
3450 else if (*flags & H2_SF_DATA_CHNK) {
3451 if (!b_putblk(rxbuf, "\r\n", 2))
3452 goto fail;
3453 }
3454 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003455
Willy Tarreau86277d42019-01-02 15:36:11 +01003456 /* success */
3457 ret = 1;
3458
Willy Tarreau68dd9852017-07-03 14:44:26 +02003459 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003460 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003461 * move the remaining data over it.
3462 */
3463 if (hole) {
3464 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3465 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3466 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3467 b_sub(&h2c->dbuf, hole);
3468 }
3469
3470 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3471 /* too large frames */
3472 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003473 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003474 }
3475
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003476 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003477 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003478 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003479 return ret;
3480
Willy Tarreau68dd9852017-07-03 14:44:26 +02003481 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003482 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003483 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003484
3485 trailers:
3486 /* This is the last HEADERS frame hence a trailer */
3487
3488 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3489 /* It's a trailer but it's missing ES flag */
3490 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3491 goto fail;
3492 }
3493
3494 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3495 * block, and when using chunks we must send the 0 CRLF marker. For
3496 * other modes, the trailers are silently dropped.
3497 */
3498 if (htx) {
3499 if (!htx_add_endof(htx, HTX_BLK_EOD))
3500 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003501 if (h2_make_htx_trailers(list, htx) <= 0)
3502 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003503 }
3504 else if (*flags & H2_SF_DATA_CHNK) {
3505 /* Legacy mode with chunked encoding : we must finalize the
3506 * data block message emit the trailing CRLF */
3507 if (!b_putblk(rxbuf, "0\r\n", 3))
3508 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003509
3510 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3511 if (outlen > 0)
3512 b_add(rxbuf, outlen);
3513 else
3514 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003515 }
3516
3517 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003518}
3519
Willy Tarreau454f9052017-10-26 19:40:35 +02003520/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3521 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3522 * in use, a new chunk is emitted for each frame. This is supposed to fit
3523 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3524 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3525 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003526 * parser state is automatically updated. Returns > 0 if it could completely
3527 * send the current frame, 0 if it couldn't complete, in which case
3528 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3529 * DATA frame can return 0 as a valid result). Stream errors are reported in
3530 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3531 * have checked the frame header and ensured that the frame was complete or the
3532 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003533 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003534static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003535{
3536 struct h2c *h2c = h2s->h2c;
3537 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003538 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003539 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003540 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003541 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003542
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003543 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003544
Olivier Houchard638b7992018-08-16 15:41:52 +02003545 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003546 if (!csbuf) {
3547 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003548 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003549 }
3550
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003551try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003552 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003553 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3554 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003555 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003556 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003557
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003558 if (flen > b_data(&h2c->dbuf)) {
3559 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003560 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003561 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003562 }
3563
Willy Tarreaua9b77962019-01-31 07:23:00 +01003564 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003565 block1 = htx_free_data_space(htx);
3566 if (!block1) {
3567 h2c->flags |= H2_CF_DEM_SFULL;
3568 goto fail;
3569 }
3570 if (flen > block1)
3571 flen = block1;
3572
3573 /* here, flen is the max we can copy into the output buffer */
3574 block1 = b_contig_data(&h2c->dbuf, 0);
3575 if (flen > block1)
3576 flen = block1;
3577
3578 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3579 h2c->flags |= H2_CF_DEM_SFULL;
3580 goto fail;
3581 }
3582
3583 b_del(&h2c->dbuf, flen);
3584 h2c->dfl -= flen;
3585 h2c->rcvd_c += flen;
3586 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003587
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003588 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003589 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003590 htx->extra = h2s->body_len;
3591 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003592 goto try_again;
3593 }
3594 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003595 /* it doesn't fit and the buffer is fragmented,
3596 * so let's defragment it and try again.
3597 */
3598 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003599 }
3600
Willy Tarreaueba10f22018-04-25 20:44:22 +02003601 /* chunked-encoding requires more room */
3602 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003603 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003604 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3605 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3606 (chklen < 1048576) ? 4 : 8;
3607 chklen += 4; // CRLF, CRLF
3608 }
3609
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003610 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003611 if (flen + chklen > b_room(csbuf)) {
3612 if (chklen >= b_room(csbuf)) {
3613 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003614 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003615 }
3616 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003617 }
3618
3619 if (h2s->flags & H2_SF_DATA_CHNK) {
3620 /* emit the chunk size */
3621 unsigned int chksz = flen;
3622 char str[10];
3623 char *beg;
3624
3625 beg = str + sizeof(str);
3626 *--beg = '\n';
3627 *--beg = '\r';
3628 do {
3629 *--beg = hextab[chksz & 0xF];
3630 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003631 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003632 }
3633
Willy Tarreau454f9052017-10-26 19:40:35 +02003634 /* Block1 is the length of the first block before the buffer wraps,
3635 * block2 is the optional second block to reach the end of the frame.
3636 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003637 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003638 if (block1 > flen)
3639 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003640 block2 = flen - block1;
3641
3642 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003643 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003644
3645 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003646 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003647
Willy Tarreaueba10f22018-04-25 20:44:22 +02003648 if (h2s->flags & H2_SF_DATA_CHNK) {
3649 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003650 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003651 }
3652
Willy Tarreau454f9052017-10-26 19:40:35 +02003653 /* now mark the input data as consumed (will be deleted from the buffer
3654 * by the caller when seeing FRAME_A after sending the window update).
3655 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003656 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003657 h2c->dfl -= flen;
3658 h2c->rcvd_c += flen;
3659 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3660
Willy Tarreau1915ca22019-01-24 11:49:37 +01003661 if (h2s->flags & H2_SF_DATA_CLEN)
3662 h2s->body_len -= flen;
3663
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003664 if (h2c->dfl > h2c->dpl) {
3665 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003666 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003667 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003668 }
3669
Willy Tarreau4a28da12018-01-04 14:41:00 +01003670 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003671 /* here we're done with the frame, all the payload (except padding) was
3672 * transferred.
3673 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003674
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003675 if (h2c->dff & H2_F_DATA_END_STREAM) {
3676 if (htx) {
3677 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3678 h2c->flags |= H2_CF_DEM_SFULL;
3679 goto fail;
3680 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003681 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003682 else if (h2s->flags & H2_SF_DATA_CHNK) {
3683 /* emit the trailing 0 CRLF CRLF */
3684 if (b_room(csbuf) < 5) {
3685 h2c->flags |= H2_CF_DEM_SFULL;
3686 goto fail;
3687 }
3688 chklen += 5;
3689 b_putblk(csbuf, "0\r\n\r\n", 5);
3690 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003691 }
3692
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003693 h2c->rcvd_c += h2c->dpl;
3694 h2c->rcvd_s += h2c->dpl;
3695 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003696 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3697
Willy Tarreau39d68502018-03-02 12:26:37 +01003698 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003699 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01003700 if (h2s->cs)
3701 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau39d68502018-03-02 12:26:37 +01003702 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003703 if (htx)
3704 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003705 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003706 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003707 if (htx)
3708 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003709 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003710}
3711
Willy Tarreau5dd17352018-06-14 13:33:30 +02003712/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3713 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3714 * number of bytes sent. The caller must check the stream's status to detect
3715 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003716 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003717static 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 +02003718{
3719 struct http_hdr list[MAX_HTTP_HDR];
3720 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003721 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003722 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003723 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003724 int es_now = 0;
3725 int ret = 0;
3726 int hdr;
3727
3728 if (h2c_mux_busy(h2c, h2s)) {
3729 h2s->flags |= H2_SF_BLK_MBUSY;
3730 return 0;
3731 }
3732
Willy Tarreau44e973f2018-03-01 17:49:30 +01003733 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003734 h2c->flags |= H2_CF_MUX_MALLOC;
3735 h2s->flags |= H2_SF_BLK_MROOM;
3736 return 0;
3737 }
3738
3739 /* First, try to parse the H1 response and index it into <list>.
3740 * NOTE! Since it comes from haproxy, we *know* that a response header
3741 * block does not wrap and we can safely read it this way without
3742 * having to realign the buffer.
3743 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003744 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003745 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003746 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003747 /* incomplete or invalid response, this is abnormal coming from
3748 * haproxy and may only result in a bad errorfile or bad Lua code
3749 * so that won't be fixed, raise an error now.
3750 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003751 * FIXME: we should instead add the ability to only return a
3752 * 502 bad gateway. But in theory this is not supposed to
3753 * happen.
3754 */
3755 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3756 ret = 0;
3757 goto end;
3758 }
3759
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003760 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003761
3762 /* certain statuses have no body or an empty one, regardless of
3763 * what the headers say.
3764 */
3765 if (sl.st.status >= 100 && sl.st.status < 200) {
3766 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3767 h1m->curr_len = h1m->body_len = 0;
3768 }
3769 else if (sl.st.status == 204 || sl.st.status == 304) {
3770 /* no contents, claim c-len is present and set to zero */
3771 h1m->flags &= ~H1_MF_CHNK;
3772 h1m->flags |= H1_MF_CLEN;
3773 h1m->curr_len = h1m->body_len = 0;
3774 }
3775
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003776 chunk_reset(&outbuf);
3777
3778 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003779 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003780 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003781 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003782
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003783 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003784 break;
3785 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003786 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787 }
3788
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003789 if (outbuf.size < 9)
3790 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003791
3792 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003793 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3794 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3795 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003796
3797 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003798 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003799 /* this is an unparsable response */
3800 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3801 ret = 0;
3802 goto end;
3803 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003804
3805 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003806 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003807 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003808 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003809 }
3810
3811 /* encode all headers, stop at empty name */
3812 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003813 /* these ones do not exist in H2 and must be dropped. */
3814 if (isteq(list[hdr].n, ist("connection")) ||
3815 isteq(list[hdr].n, ist("proxy-connection")) ||
3816 isteq(list[hdr].n, ist("keep-alive")) ||
3817 isteq(list[hdr].n, ist("upgrade")) ||
3818 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003819 continue;
3820
3821 if (isteq(list[hdr].n, ist("")))
3822 break; // end
3823
3824 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3825 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003826 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003827 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003828 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003829 }
3830 }
3831
3832 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003833 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003834 es_now = 1;
3835
3836 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003837 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003838
3839 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003840 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003841
3842 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003843 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003844
3845 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003847 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003848
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003849 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003850 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003851 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003852
Willy Tarreau801250e2018-09-11 11:45:04 +02003853 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003854 h2s->flags |= H2_SF_ES_SENT;
3855 if (h2s->st == H2_SS_OPEN)
3856 h2s->st = H2_SS_HLOC;
3857 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003858 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003860 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003861 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003862 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003863 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003864 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003865 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003866 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003867
3868 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003869
3870 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003871 //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 +02003872 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003873 full:
3874 h1m_init_res(h1m);
3875 h1m->err_pos = -1; // don't care about errors on the response path
3876 h2c->flags |= H2_CF_MUX_MFULL;
3877 h2s->flags |= H2_SF_BLK_MROOM;
3878 ret = 0;
3879 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003880}
3881
Willy Tarreau5dd17352018-06-14 13:33:30 +02003882/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3883 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3884 * the number of bytes sent. The caller must check the stream's status to
3885 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003886 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003887static 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 +02003888{
3889 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003890 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003891 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003892 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003893 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003894 int es_now = 0;
3895 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003896 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003897 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003898
3899 if (h2c_mux_busy(h2c, h2s)) {
3900 h2s->flags |= H2_SF_BLK_MBUSY;
3901 goto end;
3902 }
3903
Willy Tarreau44e973f2018-03-01 17:49:30 +01003904 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003905 h2c->flags |= H2_CF_MUX_MALLOC;
3906 h2s->flags |= H2_SF_BLK_MROOM;
3907 goto end;
3908 }
3909
3910 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003911 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003912 goto end;
3913
3914 chunk_reset(&outbuf);
3915
3916 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003917 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003918 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003921 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003922 break;
3923 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003924 /* If there are pending data in the output buffer, and we have
3925 * less than 1/4 of the mbuf's size and everything fits, we'll
3926 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3927 * is full and wait, to save some slow realign calls.
3928 */
3929 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3930 h2c->flags |= H2_CF_MUX_MFULL;
3931 h2s->flags |= H2_SF_BLK_MROOM;
3932 goto end;
3933 }
3934
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003935 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003936 }
3937
3938 if (outbuf.size < 9) {
3939 h2c->flags |= H2_CF_MUX_MFULL;
3940 h2s->flags |= H2_SF_BLK_MROOM;
3941 goto end;
3942 }
3943
3944 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003945 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3946 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3947 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003948
3949 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3950 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003951 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003952 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003953 break;
3954 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003955 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003956 if ((long long)size > h1m->curr_len)
3957 size = h1m->curr_len;
3958 break;
3959 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003960 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003961 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003962 if (!ret)
3963 goto end;
3964
3965 if (ret < 0) {
3966 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003967 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3969 goto end;
3970 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003971 max -= ret;
3972 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003973 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003974 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003975 }
3976
Willy Tarreau801250e2018-09-11 11:45:04 +02003977 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003978 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003979 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980 if (!ret)
3981 goto end;
3982
3983 if (ret < 0) {
3984 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003985 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3987 goto end;
3988 }
3989
3990 size = chunk;
3991 h1m->curr_len = chunk;
3992 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003993 max -= ret;
3994 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003995 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003996 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003997 if (!size)
3998 goto send_empty;
3999 }
4000
4001 /* in MSG_DATA state, continue below */
4002 size = h1m->curr_len;
4003 break;
4004 }
4005
4006 /* we have in <size> the exact number of bytes we need to copy from
4007 * the H1 buffer. We need to check this against the connection's and
4008 * the stream's send windows, and to ensure that this fits in the max
4009 * frame size and in the buffer's available space minus 9 bytes (for
4010 * the frame header). The connection's flow control is applied last so
4011 * that we can use a separate list of streams which are immediately
4012 * unblocked on window opening. Note: we don't implement padding.
4013 */
4014
Willy Tarreau5dd17352018-06-14 13:33:30 +02004015 if (size > max)
4016 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004017
4018 if (size > h2s->mws)
4019 size = h2s->mws;
4020
4021 if (size <= 0) {
4022 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004023 if (h2s->send_wait) {
4024 LIST_DEL(&h2s->list);
4025 LIST_INIT(&h2s->list);
4026 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 goto end;
4028 }
4029
4030 if (h2c->mfs && size > h2c->mfs)
4031 size = h2c->mfs;
4032
4033 if (size + 9 > outbuf.size) {
4034 /* we have an opportunity for enlarging the too small
4035 * available space, let's try.
4036 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004037 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038 goto realign_again;
4039 size = outbuf.size - 9;
4040 }
4041
4042 if (size <= 0) {
4043 h2c->flags |= H2_CF_MUX_MFULL;
4044 h2s->flags |= H2_SF_BLK_MROOM;
4045 goto end;
4046 }
4047
4048 if (size > h2c->mws)
4049 size = h2c->mws;
4050
4051 if (size <= 0) {
4052 h2s->flags |= H2_SF_BLK_MFCTL;
4053 goto end;
4054 }
4055
4056 /* copy whatever we can */
4057 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004058 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 if (ret == 1)
4060 len2 = 0;
4061
4062 if (!ret || len1 + len2 < size) {
4063 /* FIXME: must normally never happen */
4064 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4065 goto end;
4066 }
4067
4068 /* limit len1/len2 to size */
4069 if (len1 + len2 > size) {
4070 int sub = len1 + len2 - size;
4071
4072 if (len2 > sub)
4073 len2 -= sub;
4074 else {
4075 sub -= len2;
4076 len2 = 0;
4077 len1 -= sub;
4078 }
4079 }
4080
4081 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004082 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004083 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004084 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004085
4086 send_empty:
4087 /* we may need to add END_STREAM */
4088 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4089 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004090 *
4091 * FIXME: what we do here is not correct because we send end_stream
4092 * before knowing if we'll have to send a HEADERS frame for the
4093 * trailers. More importantly we're not consuming the trailing CRLF
4094 * after the end of trailers, so it will be left to the caller to
4095 * eat it. The right way to do it would be to measure trailers here
4096 * and to send ES only if there are no trailers.
4097 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004098 */
4099 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004100 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 es_now = 1;
4102
4103 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004104 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105
4106 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004107 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004108
4109 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004110 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004111
4112 /* consume incoming H1 response */
4113 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004114 max -= size;
4115 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004116 total += size;
4117 h1m->curr_len -= size;
4118 h2s->mws -= size;
4119 h2c->mws -= size;
4120
4121 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004122 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004123 goto new_frame;
4124 }
4125 }
4126
4127 if (es_now) {
4128 if (h2s->st == H2_SS_OPEN)
4129 h2s->st = H2_SS_HLOC;
4130 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004131 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004132
Willy Tarreau35a62702018-02-27 15:37:25 +01004133 if (!(h1m->flags & H1_MF_CHNK)) {
4134 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004135 total += max;
4136 ofs += max;
4137 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004138
Willy Tarreau801250e2018-09-11 11:45:04 +02004139 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004140 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004141
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004142 h2s->flags |= H2_SF_ES_SENT;
4143 }
4144
4145 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004146 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 +02004147 return total;
4148}
4149
Willy Tarreau115e83b2018-12-01 19:17:53 +01004150/* Try to send a HEADERS frame matching HTX response present in HTX message
4151 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4152 * must check the stream's status to detect any error which might have happened
4153 * subsequently to a successful send. The htx blocks are automatically removed
4154 * from the message. The htx message is assumed to be valid since produced from
4155 * the internal code, hence it contains a start line, an optional series of
4156 * header blocks and an end of header, otherwise an invalid frame could be
4157 * emitted and the resulting htx message could be left in an inconsistent state.
4158 */
4159static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4160{
4161 struct http_hdr list[MAX_HTTP_HDR];
4162 struct h2c *h2c = h2s->h2c;
4163 struct htx_blk *blk;
4164 struct htx_blk *blk_end;
4165 struct buffer outbuf;
4166 struct htx_sl *sl;
4167 enum htx_blk_type type;
4168 int es_now = 0;
4169 int ret = 0;
4170 int hdr;
4171 int idx;
4172
4173 if (h2c_mux_busy(h2c, h2s)) {
4174 h2s->flags |= H2_SF_BLK_MBUSY;
4175 return 0;
4176 }
4177
4178 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4179 h2c->flags |= H2_CF_MUX_MALLOC;
4180 h2s->flags |= H2_SF_BLK_MROOM;
4181 return 0;
4182 }
4183
4184 /* determine the first block which must not be deleted, blk_end may
4185 * be NULL if all blocks have to be deleted.
4186 */
4187 idx = htx_get_head(htx);
4188 blk_end = NULL;
4189 while (idx != -1) {
4190 type = htx_get_blk_type(htx_get_blk(htx, idx));
4191 idx = htx_get_next(htx, idx);
4192 if (type == HTX_BLK_EOH) {
4193 if (idx != -1)
4194 blk_end = htx_get_blk(htx, idx);
4195 break;
4196 }
4197 }
4198
4199 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004200 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004201 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004202 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004203 if (h2s->status < 100 || h2s->status > 999)
4204 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004205
4206 /* and the rest of the headers, that we dump starting at header 0 */
4207 hdr = 0;
4208
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004209 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004210 while ((idx = htx_get_next(htx, idx)) != -1) {
4211 blk = htx_get_blk(htx, idx);
4212 type = htx_get_blk_type(blk);
4213
4214 if (type == HTX_BLK_UNUSED)
4215 continue;
4216
4217 if (type != HTX_BLK_HDR)
4218 break;
4219
4220 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4221 goto fail;
4222
4223 list[hdr].n = htx_get_blk_name(htx, blk);
4224 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004225 hdr++;
4226 }
4227
4228 /* marker for end of headers */
4229 list[hdr].n = ist("");
4230
4231 if (h2s->status == 204 || h2s->status == 304) {
4232 /* no contents, claim c-len is present and set to zero */
4233 es_now = 1;
4234 }
4235
4236 chunk_reset(&outbuf);
4237
4238 while (1) {
4239 outbuf.area = b_tail(&h2c->mbuf);
4240 outbuf.size = b_contig_space(&h2c->mbuf);
4241 outbuf.data = 0;
4242
4243 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4244 break;
4245 realign_again:
4246 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4247 }
4248
4249 if (outbuf.size < 9)
4250 goto full;
4251
4252 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4253 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4254 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4255 outbuf.data = 9;
4256
4257 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004258 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004259 if (b_space_wraps(&h2c->mbuf))
4260 goto realign_again;
4261 goto full;
4262 }
4263
4264 /* encode all headers, stop at empty name */
4265 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4266 /* these ones do not exist in H2 and must be dropped. */
4267 if (isteq(list[hdr].n, ist("connection")) ||
4268 isteq(list[hdr].n, ist("proxy-connection")) ||
4269 isteq(list[hdr].n, ist("keep-alive")) ||
4270 isteq(list[hdr].n, ist("upgrade")) ||
4271 isteq(list[hdr].n, ist("transfer-encoding")))
4272 continue;
4273
4274 if (isteq(list[hdr].n, ist("")))
4275 break; // end
4276
4277 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4278 /* output full */
4279 if (b_space_wraps(&h2c->mbuf))
4280 goto realign_again;
4281 goto full;
4282 }
4283 }
4284
Christopher Faulet0b465482019-02-19 15:14:23 +01004285 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004286 * FIXME: we should also set it when we know for sure that the
4287 * content-length is zero as well as on 204/304
4288 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004289 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4290 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004291 es_now = 1;
4292
Willy Tarreau927b88b2019-03-04 08:03:25 +01004293 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004294 es_now = 1;
4295
4296 /* update the frame's size */
4297 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4298
4299 if (es_now)
4300 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4301
4302 /* commit the H2 response */
4303 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004304
4305 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4306 * 1xx responses, another HEADERS frame is expected.
4307 */
4308 if (h2s->status >= 200 || h2s->status == 101)
4309 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004310
Willy Tarreau115e83b2018-12-01 19:17:53 +01004311 if (es_now) {
4312 h2s->flags |= H2_SF_ES_SENT;
4313 if (h2s->st == H2_SS_OPEN)
4314 h2s->st = H2_SS_HLOC;
4315 else
4316 h2s_close(h2s);
4317 }
4318
4319 /* OK we could properly deliver the response */
4320
4321 /* remove all header blocks including the EOH and compute the
4322 * corresponding size.
4323 *
4324 * FIXME: We should remove everything when es_now is set.
4325 */
4326 ret = 0;
4327 idx = htx_get_head(htx);
4328 blk = htx_get_blk(htx, idx);
4329 while (blk != blk_end) {
4330 ret += htx_get_blksz(blk);
4331 blk = htx_remove_blk(htx, blk);
4332 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004333
4334 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4335 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004336 end:
4337 return ret;
4338 full:
4339 h2c->flags |= H2_CF_MUX_MFULL;
4340 h2s->flags |= H2_SF_BLK_MROOM;
4341 ret = 0;
4342 goto end;
4343 fail:
4344 /* unparsable HTX messages, too large ones to be produced in the local
4345 * list etc go here (unrecoverable errors).
4346 */
4347 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4348 ret = 0;
4349 goto end;
4350}
4351
Willy Tarreau80739692018-10-05 11:35:57 +02004352/* Try to send a HEADERS frame matching HTX request present in HTX message
4353 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4354 * must check the stream's status to detect any error which might have happened
4355 * subsequently to a successful send. The htx blocks are automatically removed
4356 * from the message. The htx message is assumed to be valid since produced from
4357 * the internal code, hence it contains a start line, an optional series of
4358 * header blocks and an end of header, otherwise an invalid frame could be
4359 * emitted and the resulting htx message could be left in an inconsistent state.
4360 */
4361static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4362{
4363 struct http_hdr list[MAX_HTTP_HDR];
4364 struct h2c *h2c = h2s->h2c;
4365 struct htx_blk *blk;
4366 struct htx_blk *blk_end;
4367 struct buffer outbuf;
4368 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004369 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004370 enum htx_blk_type type;
4371 int es_now = 0;
4372 int ret = 0;
4373 int hdr;
4374 int idx;
4375
4376 if (h2c_mux_busy(h2c, h2s)) {
4377 h2s->flags |= H2_SF_BLK_MBUSY;
4378 return 0;
4379 }
4380
4381 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4382 h2c->flags |= H2_CF_MUX_MALLOC;
4383 h2s->flags |= H2_SF_BLK_MROOM;
4384 return 0;
4385 }
4386
4387 /* determine the first block which must not be deleted, blk_end may
4388 * be NULL if all blocks have to be deleted.
4389 */
4390 idx = htx_get_head(htx);
4391 blk_end = NULL;
4392 while (idx != -1) {
4393 type = htx_get_blk_type(htx_get_blk(htx, idx));
4394 idx = htx_get_next(htx, idx);
4395 if (type == HTX_BLK_EOH) {
4396 if (idx != -1)
4397 blk_end = htx_get_blk(htx, idx);
4398 break;
4399 }
4400 }
4401
4402 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004403 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004404 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004405 meth = htx_sl_req_meth(sl);
4406 path = htx_sl_req_uri(sl);
4407
4408 /* and the rest of the headers, that we dump starting at header 0 */
4409 hdr = 0;
4410
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004411 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004412 while ((idx = htx_get_next(htx, idx)) != -1) {
4413 blk = htx_get_blk(htx, idx);
4414 type = htx_get_blk_type(blk);
4415
4416 if (type == HTX_BLK_UNUSED)
4417 continue;
4418
4419 if (type != HTX_BLK_HDR)
4420 break;
4421
4422 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4423 goto fail;
4424
4425 list[hdr].n = htx_get_blk_name(htx, blk);
4426 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004427 hdr++;
4428 }
4429
4430 /* marker for end of headers */
4431 list[hdr].n = ist("");
4432
4433 chunk_reset(&outbuf);
4434
4435 while (1) {
4436 outbuf.area = b_tail(&h2c->mbuf);
4437 outbuf.size = b_contig_space(&h2c->mbuf);
4438 outbuf.data = 0;
4439
4440 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4441 break;
4442 realign_again:
4443 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4444 }
4445
4446 if (outbuf.size < 9)
4447 goto full;
4448
4449 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4450 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4451 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4452 outbuf.data = 9;
4453
4454 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004455 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004456 if (b_space_wraps(&h2c->mbuf))
4457 goto realign_again;
4458 goto full;
4459 }
4460
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004461 /* RFC7540 #8.3: the CONNECT method must have :
4462 * - :authority set to the URI part (host:port)
4463 * - :method set to CONNECT
4464 * - :scheme and :path omitted
4465 */
4466 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4467 /* encode the scheme which is always "https" (or 0x86 for "http") */
4468 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4469 /* output full */
4470 if (b_space_wraps(&h2c->mbuf))
4471 goto realign_again;
4472 goto full;
4473 }
Willy Tarreau80739692018-10-05 11:35:57 +02004474
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004475 /* encode the path, which necessarily is the second one */
4476 if (!hpack_encode_path(&outbuf, path)) {
4477 /* output full */
4478 if (b_space_wraps(&h2c->mbuf))
4479 goto realign_again;
4480 goto full;
4481 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004482
4483 /* look for the Host header and place it in :authority */
4484 auth = ist2(NULL, 0);
4485 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4486 if (isteq(list[hdr].n, ist("")))
4487 break; // end
4488
4489 if (isteq(list[hdr].n, ist("host"))) {
4490 auth = list[hdr].v;
4491 break;
4492 }
4493 }
4494 }
4495 else {
4496 /* for CONNECT, :authority is taken from the path */
4497 auth = path;
4498 }
4499
4500 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4501 /* output full */
4502 if (b_space_wraps(&h2c->mbuf))
4503 goto realign_again;
4504 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004505 }
4506
4507 /* encode all headers, stop at empty name */
4508 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4509 /* these ones do not exist in H2 and must be dropped. */
4510 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004511 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004512 isteq(list[hdr].n, ist("proxy-connection")) ||
4513 isteq(list[hdr].n, ist("keep-alive")) ||
4514 isteq(list[hdr].n, ist("upgrade")) ||
4515 isteq(list[hdr].n, ist("transfer-encoding")))
4516 continue;
4517
4518 if (isteq(list[hdr].n, ist("")))
4519 break; // end
4520
4521 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4522 /* output full */
4523 if (b_space_wraps(&h2c->mbuf))
4524 goto realign_again;
4525 goto full;
4526 }
4527 }
4528
4529 /* we may need to add END_STREAM if we have no body :
4530 * - request already closed, or :
4531 * - no transfer-encoding, and :
4532 * - no content-length or content-length:0
4533 * Fixme: this doesn't take into account CONNECT requests.
4534 */
4535 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4536 es_now = 1;
4537
4538 if (sl->flags & HTX_SL_F_BODYLESS)
4539 es_now = 1;
4540
Willy Tarreau927b88b2019-03-04 08:03:25 +01004541 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004542 es_now = 1;
4543
4544 /* update the frame's size */
4545 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4546
4547 if (es_now)
4548 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4549
4550 /* commit the H2 response */
4551 b_add(&h2c->mbuf, outbuf.data);
4552 h2s->flags |= H2_SF_HEADERS_SENT;
4553 h2s->st = H2_SS_OPEN;
4554
Willy Tarreau80739692018-10-05 11:35:57 +02004555 if (es_now) {
4556 // trim any possibly pending data (eg: inconsistent content-length)
4557 h2s->flags |= H2_SF_ES_SENT;
4558 h2s->st = H2_SS_HLOC;
4559 }
4560
4561 /* remove all header blocks including the EOH and compute the
4562 * corresponding size.
4563 *
4564 * FIXME: We should remove everything when es_now is set.
4565 */
4566 ret = 0;
4567 idx = htx_get_head(htx);
4568 blk = htx_get_blk(htx, idx);
4569 while (blk != blk_end) {
4570 ret += htx_get_blksz(blk);
4571 blk = htx_remove_blk(htx, blk);
4572 }
4573
4574 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4575 htx_remove_blk(htx, blk_end);
4576
4577 end:
4578 return ret;
4579 full:
4580 h2c->flags |= H2_CF_MUX_MFULL;
4581 h2s->flags |= H2_SF_BLK_MROOM;
4582 ret = 0;
4583 goto end;
4584 fail:
4585 /* unparsable HTX messages, too large ones to be produced in the local
4586 * list etc go here (unrecoverable errors).
4587 */
4588 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4589 ret = 0;
4590 goto end;
4591}
4592
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004593/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004594 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4595 * caller must check the stream's status to detect any error which might have
4596 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004597 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4598 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004599static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004600{
4601 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004602 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004603 struct buffer outbuf;
4604 size_t total = 0;
4605 int es_now = 0;
4606 int bsize; /* htx block size */
4607 int fsize; /* h2 frame size */
4608 struct htx_blk *blk;
4609 enum htx_blk_type type;
4610 int idx;
4611
4612 if (h2c_mux_busy(h2c, h2s)) {
4613 h2s->flags |= H2_SF_BLK_MBUSY;
4614 goto end;
4615 }
4616
4617 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4618 h2c->flags |= H2_CF_MUX_MALLOC;
4619 h2s->flags |= H2_SF_BLK_MROOM;
4620 goto end;
4621 }
4622
Willy Tarreau98de12a2018-12-12 07:03:00 +01004623 htx = htx_from_buf(buf);
4624
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004625 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4626 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4627 * the caller to handle.
4628 */
4629
4630 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004631 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004632 goto end;
4633
4634 idx = htx_get_head(htx);
4635 blk = htx_get_blk(htx, idx);
4636 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4637 bsize = htx_get_blksz(blk);
4638 fsize = bsize;
4639
4640 if (type == HTX_BLK_EOD) {
4641 /* if we have an EOD, we're dealing with chunked data. We may
4642 * have a set of trailers after us that the caller will want to
4643 * deal with. Let's simply remove the EOD and return.
4644 */
4645 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004646 total++; // EOD counts as one byte
4647 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004648 goto end;
4649 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004650 else if (type == HTX_BLK_EOM) {
4651 if (h2s->flags & H2_SF_ES_SENT) {
4652 /* ES already sent */
4653 htx_remove_blk(htx, blk);
4654 total++; // EOM counts as one byte
4655 count--;
4656 goto end;
4657 }
4658 }
4659 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004660 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004661
4662 /* Perform some optimizations to reduce the number of buffer copies.
4663 * First, if the mux's buffer is empty and the htx area contains
4664 * exactly one data block of the same size as the requested count, and
4665 * this count fits within the frame size, the stream's window size, and
4666 * the connection's window size, then it's possible to simply swap the
4667 * caller's buffer with the mux's output buffer and adjust offsets and
4668 * length to match the entire DATA HTX block in the middle. In this
4669 * case we perform a true zero-copy operation from end-to-end. This is
4670 * the situation that happens all the time with large files. Second, if
4671 * this is not possible, but the mux's output buffer is empty, we still
4672 * have an opportunity to avoid the copy to the intermediary buffer, by
4673 * making the intermediary buffer's area point to the output buffer's
4674 * area. In this case we want to skip the HTX header to make sure that
4675 * copies remain aligned and that this operation remains possible all
4676 * the time. This goes for headers, data blocks and any data extracted
4677 * from the HTX blocks.
4678 */
4679 if (unlikely(fsize == count &&
4680 htx->used == 1 && type == HTX_BLK_DATA &&
4681 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4682 void *old_area = h2c->mbuf.area;
4683
4684 if (b_data(&h2c->mbuf)) {
4685 /* too bad there are data left there. If we have less
4686 * than 1/4 of the mbuf's size and everything fits,
4687 * we'll perform a copy anyway. Otherwise we'll pretend
4688 * the mbuf is full and wait.
4689 */
4690 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4691 goto copy;
4692 h2c->flags |= H2_CF_MUX_MFULL;
4693 h2s->flags |= H2_SF_BLK_MROOM;
4694 goto end;
4695 }
4696
4697 /* map an H2 frame to the HTX block so that we can put the
4698 * frame header there.
4699 */
4700 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004701 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004702 h2c->mbuf.data = fsize + 9;
4703 outbuf.area = b_head(&h2c->mbuf);
4704
4705 /* prepend an H2 DATA frame header just before the DATA block */
4706 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4707 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4708 h2_set_frame_size(outbuf.area, fsize);
4709
4710 /* update windows */
4711 h2s->mws -= fsize;
4712 h2c->mws -= fsize;
4713
4714 /* and exchange with our old area */
4715 buf->area = old_area;
4716 buf->data = buf->head = 0;
4717 total += fsize;
4718 goto end;
4719 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004720
Willy Tarreau98de12a2018-12-12 07:03:00 +01004721 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004722 /* for DATA and EOM we'll have to emit a frame, even if empty */
4723
4724 while (1) {
4725 outbuf.area = b_tail(&h2c->mbuf);
4726 outbuf.size = b_contig_space(&h2c->mbuf);
4727 outbuf.data = 0;
4728
4729 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4730 break;
4731 realign_again:
4732 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4733 }
4734
4735 if (outbuf.size < 9) {
4736 h2c->flags |= H2_CF_MUX_MFULL;
4737 h2s->flags |= H2_SF_BLK_MROOM;
4738 goto end;
4739 }
4740
4741 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4742 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4743 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4744 outbuf.data = 9;
4745
4746 /* we have in <fsize> the exact number of bytes we need to copy from
4747 * the HTX buffer. We need to check this against the connection's and
4748 * the stream's send windows, and to ensure that this fits in the max
4749 * frame size and in the buffer's available space minus 9 bytes (for
4750 * the frame header). The connection's flow control is applied last so
4751 * that we can use a separate list of streams which are immediately
4752 * unblocked on window opening. Note: we don't implement padding.
4753 */
4754
4755 /* EOM is presented with bsize==1 but would lead to the emission of an
4756 * empty frame, thus we force it to zero here.
4757 */
4758 if (type == HTX_BLK_EOM)
4759 bsize = fsize = 0;
4760
4761 if (!fsize)
4762 goto send_empty;
4763
4764 if (h2s->mws <= 0) {
4765 h2s->flags |= H2_SF_BLK_SFCTL;
4766 if (h2s->send_wait) {
4767 LIST_DEL(&h2s->list);
4768 LIST_INIT(&h2s->list);
4769 }
4770 goto end;
4771 }
4772
Willy Tarreauee573762018-12-04 15:25:57 +01004773 if (fsize > count)
4774 fsize = count;
4775
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004776 if (fsize > h2s->mws)
4777 fsize = h2s->mws; // >0
4778
4779 if (h2c->mfs && fsize > h2c->mfs)
4780 fsize = h2c->mfs; // >0
4781
4782 if (fsize + 9 > outbuf.size) {
4783 /* we have an opportunity for enlarging the too small
4784 * available space, let's try.
4785 * FIXME: is this really interesting to do? Maybe we'll
4786 * spend lots of time realigning instead of using two
4787 * frames.
4788 */
4789 if (b_space_wraps(&h2c->mbuf))
4790 goto realign_again;
4791 fsize = outbuf.size - 9;
4792
4793 if (fsize <= 0) {
4794 /* no need to send an empty frame here */
4795 h2c->flags |= H2_CF_MUX_MFULL;
4796 h2s->flags |= H2_SF_BLK_MROOM;
4797 goto end;
4798 }
4799 }
4800
4801 if (h2c->mws <= 0) {
4802 h2s->flags |= H2_SF_BLK_MFCTL;
4803 goto end;
4804 }
4805
4806 if (fsize > h2c->mws)
4807 fsize = h2c->mws;
4808
4809 /* now let's copy this this into the output buffer */
4810 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004811 h2s->mws -= fsize;
4812 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004813 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004814
4815 send_empty:
4816 /* update the frame's size */
4817 h2_set_frame_size(outbuf.area, fsize);
4818
4819 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4820 * meeting EOM. We should optimize this later.
4821 */
4822 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004823 total++; // EOM counts as one byte
4824 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004825 es_now = 1;
4826 }
4827
4828 if (es_now)
4829 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4830
4831 /* commit the H2 response */
4832 b_add(&h2c->mbuf, fsize + 9);
4833
4834 /* consume incoming HTX block, including EOM */
4835 total += fsize;
4836 if (fsize == bsize) {
4837 htx_remove_blk(htx, blk);
4838 if (fsize)
4839 goto new_frame;
4840 } else {
4841 /* we've truncated this block */
4842 htx_cut_data_blk(htx, blk, fsize);
4843 }
4844
4845 if (es_now) {
4846 if (h2s->st == H2_SS_OPEN)
4847 h2s->st = H2_SS_HLOC;
4848 else
4849 h2s_close(h2s);
4850
4851 h2s->flags |= H2_SF_ES_SENT;
4852 }
4853
4854 end:
4855 return total;
4856}
4857
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004858/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4859 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4860 * processed. The caller must check the stream's status to detect any error
4861 * which might have happened subsequently to a successful send. The htx blocks
4862 * are automatically removed from the message. The htx message is assumed to be
4863 * valid since produced from the internal code. Processing stops when meeting
4864 * the EOM, which is also removed. All trailers are processed at once and sent
4865 * as a single frame. The ES flag is always set.
4866 */
4867static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4868{
4869 struct http_hdr list[MAX_HTTP_HDR];
4870 struct h2c *h2c = h2s->h2c;
4871 struct htx_blk *blk;
4872 struct htx_blk *blk_end;
4873 struct buffer outbuf;
4874 struct h1m h1m;
4875 enum htx_blk_type type;
4876 uint32_t size;
4877 int ret = 0;
4878 int hdr;
4879 int idx;
4880 void *start;
4881
4882 if (h2c_mux_busy(h2c, h2s)) {
4883 h2s->flags |= H2_SF_BLK_MBUSY;
4884 goto end;
4885 }
4886
4887 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4888 h2c->flags |= H2_CF_MUX_MALLOC;
4889 h2s->flags |= H2_SF_BLK_MROOM;
4890 goto end;
4891 }
4892
4893 /* The principle is that we parse each and every trailers block using
4894 * the H1 headers parser, and append it to the list. We don't proceed
4895 * until EOM is met. blk_end will point to the EOM block.
4896 */
4897 hdr = 0;
4898 memset(list, 0, sizeof(list));
4899 blk_end = NULL;
4900
4901 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4902 blk = htx_get_blk(htx, idx);
4903 type = htx_get_blk_type(blk);
4904
4905 if (type == HTX_BLK_UNUSED)
4906 continue;
4907
4908 if (type != HTX_BLK_TLR) {
4909 if (type == HTX_BLK_EOM)
4910 blk_end = blk;
4911 break;
4912 }
4913
4914 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4915 goto fail;
4916
4917 size = htx_get_blksz(blk);
4918 start = htx_get_blk_ptr(htx, blk);
4919
4920 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4921 h1m.err_pos = 0;
4922 ret = h1_headers_to_hdr_list(start, start + size,
4923 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4924 &h1m, NULL);
4925 if (ret < 0)
4926 goto fail;
4927
4928 /* ret == 0 if an incomplete trailers block was found (missing
4929 * empty line), or > 0 if it was found. We have to continue on
4930 * incomplete messages because the trailers block might be
4931 * incomplete.
4932 */
4933
4934 /* search the new end */
4935 while (hdr <= sizeof(list)/sizeof(list[0])) {
4936 if (!list[hdr].n.len)
4937 break;
4938 hdr++;
4939 }
4940 }
4941
4942 if (!blk_end)
4943 goto end; // end not found yet
4944
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004945 chunk_reset(&outbuf);
4946
4947 while (1) {
4948 outbuf.area = b_tail(&h2c->mbuf);
4949 outbuf.size = b_contig_space(&h2c->mbuf);
4950 outbuf.data = 0;
4951
4952 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4953 break;
4954 realign_again:
4955 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4956 }
4957
4958 if (outbuf.size < 9)
4959 goto full;
4960
4961 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4962 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4963 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4964 outbuf.data = 9;
4965
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004966 /* encode all headers */
4967 for (idx = 0; idx < hdr; idx++) {
4968 /* these ones do not exist in H2 or must not appear in
4969 * trailers and must be dropped.
4970 */
4971 if (isteq(list[idx].n, ist("host")) ||
4972 isteq(list[idx].n, ist("content-length")) ||
4973 isteq(list[idx].n, ist("connection")) ||
4974 isteq(list[idx].n, ist("proxy-connection")) ||
4975 isteq(list[idx].n, ist("keep-alive")) ||
4976 isteq(list[idx].n, ist("upgrade")) ||
4977 isteq(list[idx].n, ist("te")) ||
4978 isteq(list[idx].n, ist("transfer-encoding")))
4979 continue;
4980
4981 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4982 /* output full */
4983 if (b_space_wraps(&h2c->mbuf))
4984 goto realign_again;
4985 goto full;
4986 }
4987 }
4988
Willy Tarreau67b8cae2019-02-21 18:16:35 +01004989 if (!hdr) {
4990 /* here we have a problem, we've received an empty trailers
4991 * block followed by an EOM. Because of this we can't send a
4992 * HEADERS frame, so we have to cheat and instead send an empty
4993 * DATA frame conveying the ES flag.
4994 */
4995 outbuf.area[3] = H2_FT_DATA;
4996 outbuf.area[4] = H2_F_DATA_END_STREAM;
4997 }
4998
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004999 /* update the frame's size */
5000 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5001
5002 /* commit the H2 response */
5003 b_add(&h2c->mbuf, outbuf.data);
5004 h2s->flags |= H2_SF_ES_SENT;
5005
5006 if (h2s->st == H2_SS_OPEN)
5007 h2s->st = H2_SS_HLOC;
5008 else
5009 h2s_close(h2s);
5010
5011 /* OK we could properly deliver the response */
5012 done:
5013 /* remove all header blocks including EOM and compute the corresponding size. */
5014 ret = 0;
5015 idx = htx_get_head(htx);
5016 blk = htx_get_blk(htx, idx);
5017 while (blk != blk_end) {
5018 ret += htx_get_blksz(blk);
5019 blk = htx_remove_blk(htx, blk);
5020 }
5021 blk = htx_remove_blk(htx, blk);
5022 end:
5023 return ret;
5024 full:
5025 h2c->flags |= H2_CF_MUX_MFULL;
5026 h2s->flags |= H2_SF_BLK_MROOM;
5027 ret = 0;
5028 goto end;
5029 fail:
5030 /* unparsable HTX messages, too large ones to be produced in the local
5031 * list etc go here (unrecoverable errors).
5032 */
5033 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5034 ret = 0;
5035 goto end;
5036}
5037
Olivier Houchard6ff20392018-07-17 18:46:31 +02005038/* Called from the upper layer, to subscribe to events, such as being able to send */
5039static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5040{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005041 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005042 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005043 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005044
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005045 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005046 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005047 if (!(sw->events & SUB_RETRY_RECV)) {
5048 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005049 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005050 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005051 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005052 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005053 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005054 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005055 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005056 if (!(sw->events & SUB_RETRY_SEND)) {
5057 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005058 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005059 h2s->send_wait = sw;
5060 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5061 if (h2s->flags & H2_SF_BLK_MFCTL)
5062 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5063 else
5064 LIST_ADDQ(&h2c->send_list, &h2s->list);
5065 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005066 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005067 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005068 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005069 if (event_type != 0)
5070 return -1;
5071 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005072
5073
5074}
5075
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005076static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5077{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005078 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005079 struct h2s *h2s = cs->ctx;
5080
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005081 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005082 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005083 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005084 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005085 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005086 }
5087 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005088 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005089 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005090 if (h2s->send_wait == sw) {
5091 LIST_DEL(&h2s->list);
5092 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005093 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005094 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005095 }
5096 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005097 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5098 sw = param;
5099 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005100 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005101 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005102 LIST_DEL(&h2s->list);
5103 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005104 }
5105 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005106 return 0;
5107}
5108
5109
Olivier Houchard511efea2018-08-16 15:30:32 +02005110/* Called from the upper layer, to receive data */
5111static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5112{
Olivier Houchard638b7992018-08-16 15:41:52 +02005113 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005114 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005115 struct htx *h2s_htx = NULL;
5116 struct htx *buf_htx = NULL;
5117 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005118 size_t ret = 0;
5119
5120 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005121 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5122 /* in HTX mode we ignore the count argument */
5123 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005124 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005125 /* Here htx_to_buf() will set buffer data to 0 because
5126 * the HTX is empty.
5127 */
5128 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005129 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005130 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005131
5132 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005133 count = htx_free_data_space(buf_htx);
5134 if (flags & CO_RFL_KEEP_RSV) {
5135 if (count <= global.tune.maxrewrite)
5136 goto end;
5137 count -= global.tune.maxrewrite;
5138 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005139
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005140 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005141
5142 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5143 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5144
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005145 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005146 htx_to_buf(buf_htx, buf);
5147 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005148 ret = htx_ret.ret;
5149 }
5150 else {
5151 ret = b_xfer(buf, &h2s->rxbuf, count);
5152 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005153
Christopher Faulet37070b22019-02-14 15:12:14 +01005154 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005155 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005156 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005157 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005158 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005159 if (cs->flags & CS_FL_REOS)
5160 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005161 if (cs->flags & CS_FL_ERR_PENDING)
5162 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005163 if (b_size(&h2s->rxbuf)) {
5164 b_free(&h2s->rxbuf);
5165 offer_buffers(NULL, tasks_run_queue);
5166 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005167 }
5168
Willy Tarreau082f5592018-11-25 08:03:32 +01005169 if (ret && h2c->dsi == h2s->id) {
5170 /* demux is blocking on this stream's buffer */
5171 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005172 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005173 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005174
Olivier Houchard511efea2018-08-16 15:30:32 +02005175 return ret;
5176}
5177
Olivier Houchardd846c262018-10-19 17:24:29 +02005178static void h2_stop_senders(struct h2c *h2c)
5179{
5180 struct h2s *h2s, *h2s_back;
5181
5182 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5183 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5184 if (h2c->msi == h2s_id(h2s))
5185 continue;
5186 LIST_DEL(&h2s->list);
5187 LIST_INIT(&h2s->list);
5188 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005189 h2s->send_wait->events |= SUB_RETRY_SEND;
5190 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005191 LIST_ADD(&h2c->send_list, &h2s->list);
5192 }
5193}
5194
Willy Tarreau62f52692017-10-08 23:01:42 +02005195/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005196static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005197{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005198 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005199 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005200 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005201 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005202 struct htx *htx;
5203 struct htx_blk *blk;
5204 enum htx_blk_type btype;
5205 uint32_t bsize;
5206 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005207
Olivier Houchardd846c262018-10-19 17:24:29 +02005208 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005209 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005210 h2s->send_wait = NULL;
5211 LIST_DEL(&h2s->list);
5212 LIST_INIT(&h2s->list);
5213 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005214 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5215 return 0;
5216
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005217 /* htx will be enough to decide if we're using HTX or legacy */
5218 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5219
Willy Tarreau0bad0432018-06-14 16:54:01 +02005220 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005221 h2s->flags |= H2_SF_OUTGOING_DATA;
5222
Willy Tarreau751f2d02018-10-05 09:35:00 +02005223 if (h2s->id == 0) {
5224 int32_t id = h2c_get_next_sid(h2s->h2c);
5225
5226 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005227 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005228 return 0;
5229 }
5230
5231 eb32_delete(&h2s->by_id);
5232 h2s->by_id.key = h2s->id = id;
5233 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005234 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005235 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5236 }
5237
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005238 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005239 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5240 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005241 idx = htx_get_head(htx);
5242 blk = htx_get_blk(htx, idx);
5243 btype = htx_get_blk_type(blk);
5244 bsize = htx_get_blksz(blk);
5245
5246 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005247 case HTX_BLK_REQ_SL:
5248 /* start-line before headers */
5249 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5250 if (ret > 0) {
5251 total += ret;
5252 count -= ret;
5253 if (ret < bsize)
5254 goto done;
5255 }
5256 break;
5257
Willy Tarreau115e83b2018-12-01 19:17:53 +01005258 case HTX_BLK_RES_SL:
5259 /* start-line before headers */
5260 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5261 if (ret > 0) {
5262 total += ret;
5263 count -= ret;
5264 if (ret < bsize)
5265 goto done;
5266 }
5267 break;
5268
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005269 case HTX_BLK_DATA:
5270 case HTX_BLK_EOD:
5271 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005272 /* all these cause the emission of a DATA frame (possibly empty).
5273 * This EOM necessarily is one before trailers, as the EOM following
5274 * trailers would have been consumed by the trailers parser.
5275 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005276 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005277 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005278 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005279 total += ret;
5280 count -= ret;
5281 if (ret < bsize)
5282 goto done;
5283 }
5284 break;
5285
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005286 case HTX_BLK_TLR:
5287 /* This is the first trailers block, all the subsequent ones AND
5288 * the EOM will be swallowed by the parser.
5289 */
5290 ret = h2s_htx_make_trailers(h2s, htx);
5291 if (ret > 0) {
5292 total += ret;
5293 count -= ret;
5294 if (ret < bsize)
5295 goto done;
5296 }
5297 break;
5298
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005299 default:
5300 htx_remove_blk(htx, blk);
5301 total += bsize;
5302 count -= bsize;
5303 break;
5304 }
5305 }
5306 goto done;
5307 }
5308
5309 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005310 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005311 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005312 if (h2s->h2c->flags & H2_CF_IS_BACK)
5313 ret = -1;
5314 else
5315 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005316 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005317 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005318 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005319 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005320 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005321 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005322 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005323
Willy Tarreau5dd17352018-06-14 13:33:30 +02005324 if (unlikely((int)ret <= 0)) {
5325 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005326 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5327 break;
5328 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005329 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005330 total += count;
5331 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005332 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005333 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005334 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005335 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005336 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005337 break;
5338 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005339
5340 total += ret;
5341 count -= ret;
5342
5343 if (h2s->st >= H2_SS_ERROR)
5344 break;
5345
5346 if (h2s->flags & H2_SF_BLK_ANY)
5347 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005348 }
5349
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005350 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005351 if (h2s->st >= H2_SS_ERROR) {
5352 /* trim any possibly pending data after we close (extra CR-LF,
5353 * unprocessed trailers, abnormal extra data, ...)
5354 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005355 total += count;
5356 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005357 }
5358
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005359 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005360 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005361 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005362 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005363 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005364 }
5365
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005366 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005367 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005368 } else {
5369 b_del(buf, total);
5370 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005371
5372 /* The mux is full, cancel the pending tasks */
5373 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5374 (h2s->flags & H2_SF_BLK_MBUSY))
5375 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005376
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005377 /* If we're running HTX, and we read the whole buffer, then pretend
5378 * we read exactly what the caller specified, as with HTX the caller
5379 * will always give the buffer size, instead of the amount of data
5380 * available.
5381 */
5382 if (htx && !b_data(buf))
5383 total = orig_count;
5384
Olivier Houchard7505f942018-08-21 18:10:44 +02005385 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005386 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005387 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005388
Olivier Houchard7505f942018-08-21 18:10:44 +02005389 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005390 /* If we're waiting for flow control, and we got a shutr on the
5391 * connection, we will never be unlocked, so add an error on
5392 * the conn_stream.
5393 */
5394 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5395 !b_data(&h2s->h2c->dbuf) &&
5396 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5397 if (cs->flags & CS_FL_EOS)
5398 cs->flags |= CS_FL_ERROR;
5399 else
5400 cs->flags |= CS_FL_ERR_PENDING;
5401 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005402 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005403}
5404
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005405/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005406static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005407{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005408 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005409 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005410 struct eb32_node *node;
5411 int fctl_cnt = 0;
5412 int send_cnt = 0;
5413 int tree_cnt = 0;
5414 int orph_cnt = 0;
5415
5416 if (!h2c)
5417 return;
5418
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005419 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005420 fctl_cnt++;
5421
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005422 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005423 send_cnt++;
5424
Willy Tarreau3af37712018-12-18 14:34:41 +01005425 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005426 node = eb32_first(&h2c->streams_by_id);
5427 while (node) {
5428 h2s = container_of(node, struct h2s, by_id);
5429 tree_cnt++;
5430 if (!h2s->cs)
5431 orph_cnt++;
5432 node = eb32_next(node);
5433 }
5434
Willy Tarreau987c0632018-12-18 10:32:05 +01005435 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5436 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5437 " .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 +02005438 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5439 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005440 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005441 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5442 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5443 h2c->msi,
5444 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5445 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5446
5447 if (h2s) {
5448 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5449 h2s, h2s->id, h2s->flags,
5450 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5451 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5452 h2s->cs);
5453 if (h2s->cs)
5454 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5455 h2s->cs->flags, h2s->cs->data);
5456 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005457}
Willy Tarreau62f52692017-10-08 23:01:42 +02005458
5459/*******************************************************/
5460/* functions below are dedicated to the config parsers */
5461/*******************************************************/
5462
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005463/* config parser for global "tune.h2.header-table-size" */
5464static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5465 struct proxy *defpx, const char *file, int line,
5466 char **err)
5467{
5468 if (too_many_args(1, args, err, NULL))
5469 return -1;
5470
5471 h2_settings_header_table_size = atoi(args[1]);
5472 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5473 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5474 return -1;
5475 }
5476 return 0;
5477}
Willy Tarreau62f52692017-10-08 23:01:42 +02005478
Willy Tarreaue6baec02017-07-27 11:45:11 +02005479/* config parser for global "tune.h2.initial-window-size" */
5480static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5481 struct proxy *defpx, const char *file, int line,
5482 char **err)
5483{
5484 if (too_many_args(1, args, err, NULL))
5485 return -1;
5486
5487 h2_settings_initial_window_size = atoi(args[1]);
5488 if (h2_settings_initial_window_size < 0) {
5489 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5490 return -1;
5491 }
5492 return 0;
5493}
5494
Willy Tarreau5242ef82017-07-27 11:47:28 +02005495/* config parser for global "tune.h2.max-concurrent-streams" */
5496static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5497 struct proxy *defpx, const char *file, int line,
5498 char **err)
5499{
5500 if (too_many_args(1, args, err, NULL))
5501 return -1;
5502
5503 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005504 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005505 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5506 return -1;
5507 }
5508 return 0;
5509}
5510
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005511/* config parser for global "tune.h2.max-frame-size" */
5512static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5513 struct proxy *defpx, const char *file, int line,
5514 char **err)
5515{
5516 if (too_many_args(1, args, err, NULL))
5517 return -1;
5518
5519 h2_settings_max_frame_size = atoi(args[1]);
5520 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5521 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5522 return -1;
5523 }
5524 return 0;
5525}
5526
Willy Tarreau62f52692017-10-08 23:01:42 +02005527
5528/****************************************/
5529/* MUX initialization and instanciation */
5530/***************************************/
5531
5532/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005533static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005534 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005535 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005536 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005537 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005538 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005539 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005540 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005541 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005542 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005543 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005544 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005545 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005546 .shutr = h2_shutr,
5547 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005548 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005549 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005550 .name = "H2",
5551};
5552
Christopher Faulet32f61c02018-04-10 14:33:41 +02005553/* PROTO selection : this mux registers PROTO token "h2" */
5554static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005555 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005556
Willy Tarreau0108d902018-11-25 19:14:37 +01005557INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5558
Willy Tarreauf8957272018-10-03 10:25:20 +02005559static struct mux_proto_list mux_proto_h2_htx =
5560 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5561
5562INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5563
Willy Tarreau62f52692017-10-08 23:01:42 +02005564/* config keyword parsers */
5565static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005566 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005567 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005568 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005569 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005570 { 0, NULL, NULL }
5571}};
5572
Willy Tarreau0108d902018-11-25 19:14:37 +01005573INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);