blob: f54b67d2021bd4bc1f8e20e1e20863fc63c8dfbf [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
Christopher Fauletf02ca002019-03-07 16:21:34 +01001387/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1388 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1389 * connection. The stream's state is automatically updated accordingly. If the
1390 * stream is orphaned, it is destroyed.
1391 */
1392static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1393{
1394 if (!h2s->cs) {
1395 /* this stream was already orphaned */
1396 h2s_destroy(h2s);
1397 return;
1398 }
1399
1400 h2s->cs->flags |= flags;
1401 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1402 h2s->cs->flags |= CS_FL_ERROR;
1403
1404 h2s_alert(h2s);
1405
1406 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1407 h2s->st = H2_SS_ERROR;
1408 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1409 h2s->st = H2_SS_HREM;
1410 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1411 h2s_close(h2s);
1412}
1413
1414/* wake the streams attached to the connection, whose id is greater than <last>
1415 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001416 */
1417static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1418{
1419 struct eb32_node *node;
1420 struct h2s *h2s;
1421
1422 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001423 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001424
1425 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet203b2b02019-03-08 09:23:46 +01001426 flags |= (CS_FL_REOS|CS_FL_READ_NULL);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001427
Christopher Fauletf02ca002019-03-07 16:21:34 +01001428 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001429 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1430 while (node) {
1431 h2s = container_of(node, struct h2s, by_id);
1432 if (h2s->id <= last)
1433 break;
1434 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001435 h2s_wake_one_stream(h2s, flags);
1436 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001437
Christopher Fauletf02ca002019-03-07 16:21:34 +01001438 /* Wake all streams with unassigned ID (ID == 0) */
1439 node = eb32_lookup(&h2c->streams_by_id, 0);
1440 while (node) {
1441 h2s = container_of(node, struct h2s, by_id);
1442 if (h2s->id > 0)
1443 break;
1444 node = eb32_next(node);
1445 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001446 }
1447}
1448
Willy Tarreau3421aba2017-07-27 15:41:03 +02001449/* Increase all streams' outgoing window size by the difference passed in
1450 * argument. This is needed upon receipt of the settings frame if the initial
1451 * window size is different. The difference may be negative and the resulting
1452 * window size as well, for the time it takes to receive some window updates.
1453 */
1454static void h2c_update_all_ws(struct h2c *h2c, int diff)
1455{
1456 struct h2s *h2s;
1457 struct eb32_node *node;
1458
1459 if (!diff)
1460 return;
1461
1462 node = eb32_first(&h2c->streams_by_id);
1463 while (node) {
1464 h2s = container_of(node, struct h2s, by_id);
1465 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001466
1467 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1468 h2s->flags &= ~H2_SF_BLK_SFCTL;
1469 if (h2s->send_wait)
1470 LIST_ADDQ(&h2c->send_list, &h2s->list);
1471
1472 }
1473
Willy Tarreau3421aba2017-07-27 15:41:03 +02001474 node = eb32_next(node);
1475 }
1476}
1477
1478/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1479 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001480 * return an error in h2c. The caller must have already verified frame length
1481 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482 */
1483static int h2c_handle_settings(struct h2c *h2c)
1484{
1485 unsigned int offset;
1486 int error;
1487
1488 if (h2c->dff & H2_F_SETTINGS_ACK) {
1489 if (h2c->dfl) {
1490 error = H2_ERR_FRAME_SIZE_ERROR;
1491 goto fail;
1492 }
1493 return 1;
1494 }
1495
Willy Tarreau3421aba2017-07-27 15:41:03 +02001496 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001497 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001498 return 0;
1499
1500 /* parse the frame */
1501 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001502 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1503 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001504
1505 switch (type) {
1506 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1507 /* we need to update all existing streams with the
1508 * difference from the previous iws.
1509 */
1510 if (arg < 0) { // RFC7540#6.5.2
1511 error = H2_ERR_FLOW_CONTROL_ERROR;
1512 goto fail;
1513 }
1514 h2c_update_all_ws(h2c, arg - h2c->miw);
1515 h2c->miw = arg;
1516 break;
1517 case H2_SETTINGS_MAX_FRAME_SIZE:
1518 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1519 error = H2_ERR_PROTOCOL_ERROR;
1520 goto fail;
1521 }
1522 h2c->mfs = arg;
1523 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001524 case H2_SETTINGS_ENABLE_PUSH:
1525 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1526 error = H2_ERR_PROTOCOL_ERROR;
1527 goto fail;
1528 }
1529 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001530 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1531 if (h2c->flags & H2_CF_IS_BACK) {
1532 /* the limit is only for the backend; for the frontend it is our limit */
1533 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1534 arg = h2_settings_max_concurrent_streams;
1535 h2c->streams_limit = arg;
1536 }
1537 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001538 }
1539 }
1540
1541 /* need to ACK this frame now */
1542 h2c->st0 = H2_CS_FRAME_A;
1543 return 1;
1544 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001545 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001546 h2c_error(h2c, error);
1547 return 0;
1548}
1549
1550/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1551 * success or one of the h2_status values.
1552 */
1553static int h2c_ack_settings(struct h2c *h2c)
1554{
1555 struct buffer *res;
1556 char str[9];
1557 int ret = -1;
1558
1559 if (h2c_mux_busy(h2c, NULL)) {
1560 h2c->flags |= H2_CF_DEM_MBUSY;
1561 return 0;
1562 }
1563
Willy Tarreau44e973f2018-03-01 17:49:30 +01001564 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001565 if (!res) {
1566 h2c->flags |= H2_CF_MUX_MALLOC;
1567 h2c->flags |= H2_CF_DEM_MROOM;
1568 return 0;
1569 }
1570
1571 memcpy(str,
1572 "\x00\x00\x00" /* length : 0 (no data) */
1573 "\x04" "\x01" /* type : 4, flags : ACK */
1574 "\x00\x00\x00\x00" /* stream ID */, 9);
1575
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001576 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001577 if (unlikely(ret <= 0)) {
1578 if (!ret) {
1579 h2c->flags |= H2_CF_MUX_MFULL;
1580 h2c->flags |= H2_CF_DEM_MROOM;
1581 return 0;
1582 }
1583 else {
1584 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1585 return 0;
1586 }
1587 }
1588 return ret;
1589}
1590
Willy Tarreaucf68c782017-10-10 17:11:41 +02001591/* processes a PING frame and schedules an ACK if needed. The caller must pass
1592 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001593 * missing data. The caller must have already verified frame length
1594 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001595 */
1596static int h2c_handle_ping(struct h2c *h2c)
1597{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001598 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001599 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001600 h2c->st0 = H2_CS_FRAME_A;
1601 return 1;
1602}
1603
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001604/* Try to send a window update for stream id <sid> and value <increment>.
1605 * Returns > 0 on success or zero on missing room or failure. It may return an
1606 * error in h2c.
1607 */
1608static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1609{
1610 struct buffer *res;
1611 char str[13];
1612 int ret = -1;
1613
1614 if (h2c_mux_busy(h2c, NULL)) {
1615 h2c->flags |= H2_CF_DEM_MBUSY;
1616 return 0;
1617 }
1618
Willy Tarreau44e973f2018-03-01 17:49:30 +01001619 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001620 if (!res) {
1621 h2c->flags |= H2_CF_MUX_MALLOC;
1622 h2c->flags |= H2_CF_DEM_MROOM;
1623 return 0;
1624 }
1625
1626 /* length: 4, type: 8, flags: none */
1627 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1628 write_n32(str + 5, sid);
1629 write_n32(str + 9, increment);
1630
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001631 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001632
1633 if (unlikely(ret <= 0)) {
1634 if (!ret) {
1635 h2c->flags |= H2_CF_MUX_MFULL;
1636 h2c->flags |= H2_CF_DEM_MROOM;
1637 return 0;
1638 }
1639 else {
1640 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1641 return 0;
1642 }
1643 }
1644 return ret;
1645}
1646
1647/* try to send pending window update for the connection. It's safe to call it
1648 * with no pending updates. Returns > 0 on success or zero on missing room or
1649 * failure. It may return an error in h2c.
1650 */
1651static int h2c_send_conn_wu(struct h2c *h2c)
1652{
1653 int ret = 1;
1654
1655 if (h2c->rcvd_c <= 0)
1656 return 1;
1657
Willy Tarreau97aaa672018-12-23 09:49:04 +01001658 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1659 /* increase the advertised connection window to 2G on
1660 * first update.
1661 */
1662 h2c->flags |= H2_CF_WINDOW_OPENED;
1663 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1664 }
1665
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001666 /* send WU for the connection */
1667 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1668 if (ret > 0)
1669 h2c->rcvd_c = 0;
1670
1671 return ret;
1672}
1673
1674/* try to send pending window update for the current dmux stream. It's safe to
1675 * call it with no pending updates. Returns > 0 on success or zero on missing
1676 * room or failure. It may return an error in h2c.
1677 */
1678static int h2c_send_strm_wu(struct h2c *h2c)
1679{
1680 int ret = 1;
1681
1682 if (h2c->rcvd_s <= 0)
1683 return 1;
1684
1685 /* send WU for the stream */
1686 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1687 if (ret > 0)
1688 h2c->rcvd_s = 0;
1689
1690 return ret;
1691}
1692
Willy Tarreaucf68c782017-10-10 17:11:41 +02001693/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1694 * success, 0 on missing data or one of the h2_status values.
1695 */
1696static int h2c_ack_ping(struct h2c *h2c)
1697{
1698 struct buffer *res;
1699 char str[17];
1700 int ret = -1;
1701
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001702 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001703 return 0;
1704
1705 if (h2c_mux_busy(h2c, NULL)) {
1706 h2c->flags |= H2_CF_DEM_MBUSY;
1707 return 0;
1708 }
1709
Willy Tarreau44e973f2018-03-01 17:49:30 +01001710 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001711 if (!res) {
1712 h2c->flags |= H2_CF_MUX_MALLOC;
1713 h2c->flags |= H2_CF_DEM_MROOM;
1714 return 0;
1715 }
1716
1717 memcpy(str,
1718 "\x00\x00\x08" /* length : 8 (same payload) */
1719 "\x06" "\x01" /* type : 6, flags : ACK */
1720 "\x00\x00\x00\x00" /* stream ID */, 9);
1721
1722 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001723 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001724
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001725 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726 if (unlikely(ret <= 0)) {
1727 if (!ret) {
1728 h2c->flags |= H2_CF_MUX_MFULL;
1729 h2c->flags |= H2_CF_DEM_MROOM;
1730 return 0;
1731 }
1732 else {
1733 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1734 return 0;
1735 }
1736 }
1737 return ret;
1738}
1739
Willy Tarreau26f95952017-07-27 17:18:30 +02001740/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1741 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001742 * h2c or h2s. The caller must have already verified frame length and stream ID
1743 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001744 */
1745static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1746{
1747 int32_t inc;
1748 int error;
1749
Willy Tarreau26f95952017-07-27 17:18:30 +02001750 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001751 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001752 return 0;
1753
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001754 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001755
1756 if (h2c->dsi != 0) {
1757 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001758
1759 /* it's not an error to receive WU on a closed stream */
1760 if (h2s->st == H2_SS_CLOSED)
1761 return 1;
1762
1763 if (!inc) {
1764 error = H2_ERR_PROTOCOL_ERROR;
1765 goto strm_err;
1766 }
1767
1768 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1769 error = H2_ERR_FLOW_CONTROL_ERROR;
1770 goto strm_err;
1771 }
1772
1773 h2s->mws += inc;
1774 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1775 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001776 if (h2s->send_wait)
1777 LIST_ADDQ(&h2c->send_list, &h2s->list);
1778
Willy Tarreau26f95952017-07-27 17:18:30 +02001779 }
1780 }
1781 else {
1782 /* connection window update */
1783 if (!inc) {
1784 error = H2_ERR_PROTOCOL_ERROR;
1785 goto conn_err;
1786 }
1787
1788 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1789 error = H2_ERR_FLOW_CONTROL_ERROR;
1790 goto conn_err;
1791 }
1792
1793 h2c->mws += inc;
1794 }
1795
1796 return 1;
1797
1798 conn_err:
1799 h2c_error(h2c, error);
1800 return 0;
1801
1802 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001803 h2s_error(h2s, error);
1804 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001805 return 0;
1806}
1807
Willy Tarreaue96b0922017-10-30 00:28:29 +01001808/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001809 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1810 * have already verified frame length and stream ID validity. Described in
1811 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001812 */
1813static int h2c_handle_goaway(struct h2c *h2c)
1814{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001815 int last;
1816
Willy Tarreaue96b0922017-10-30 00:28:29 +01001817 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001818 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001819 return 0;
1820
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001821 last = h2_get_n32(&h2c->dbuf, 0);
1822 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001823 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001824 if (h2c->last_sid < 0)
1825 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001826 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001827}
1828
Willy Tarreau92153fc2017-12-03 19:46:19 +01001829/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001830 * invalid. Returns > 0 on success or zero on missing data. It may return an
1831 * error in h2c. The caller must have already verified frame length and stream
1832 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001833 */
1834static int h2c_handle_priority(struct h2c *h2c)
1835{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001836 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001837 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001838 return 0;
1839
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001840 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001841 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001842 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1843 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001844 }
1845 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001846}
1847
Willy Tarreaucd234e92017-08-18 10:59:39 +02001848/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001849 * Returns > 0 on success or zero on missing data. The caller must have already
1850 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001851 */
1852static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1853{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001854 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001855 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001856 return 0;
1857
1858 /* late RST, already handled */
1859 if (h2s->st == H2_SS_CLOSED)
1860 return 1;
1861
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001862 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001863 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001864
1865 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001866 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001867 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001868 }
1869
1870 h2s->flags |= H2_SF_RST_RCVD;
1871 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001872}
1873
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001874/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1875 * It may return an error in h2c or h2s. The caller must consider that the
1876 * return value is the new h2s in case one was allocated (most common case).
1877 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001878 * errors here are reported as connection errors since it's impossible to
1879 * recover from such errors after the compression context has been altered.
1880 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001881static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001882{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001883 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001884 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001885 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001886 int error;
1887
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001888 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001889 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001890
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001891 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001892 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001893
1894 /* now either the frame is complete or the buffer is complete */
1895 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001896 /* The stream exists/existed, this must be a trailers frame */
1897 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001898 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001899 goto out;
1900 goto done;
1901 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001902 /* the connection was already killed by an RST, let's consume
1903 * the data and send another RST.
1904 */
1905 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1906 h2s = (struct h2s*)h2_error_stream;
1907 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001908 }
1909 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1910 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1911 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001912 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001913 goto conn_err;
1914 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001915 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1916 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001917
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001918 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001919
Willy Tarreau25919232019-01-03 14:48:18 +01001920 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001921 if (h2c->st0 >= H2_CS_ERROR)
1922 goto out;
1923
Willy Tarreau25919232019-01-03 14:48:18 +01001924 if (error <= 0) {
1925 if (error == 0)
1926 goto out; // missing data
1927
1928 /* Failed to decode this stream (e.g. too large request)
1929 * but the HPACK decompressor is still synchronized.
1930 */
1931 h2s = (struct h2s*)h2_error_stream;
1932 goto send_rst;
1933 }
1934
Willy Tarreau22de8d32018-09-05 19:55:58 +02001935 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001936 * positively from h2c_frt_stream_new(), the stream will report the error,
1937 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001938 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001939 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001940 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001941 h2s = (struct h2s*)h2_refused_stream;
1942 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001943 }
1944
1945 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001946 h2s->rxbuf = rxbuf;
1947 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001948 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001949
Willy Tarreau88d138e2019-01-02 19:38:14 +01001950 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001951 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001952 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001953
1954 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001955 if (h2s->st == H2_SS_OPEN)
1956 h2s->st = H2_SS_HREM;
1957 else
1958 h2s_close(h2s);
Willy Tarreau927b88b2019-03-04 08:03:25 +01001959 if (h2s->cs)
1960 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001961 }
1962
Willy Tarreau3a429f02019-01-03 11:41:50 +01001963 /* update the max stream ID if the request is being processed */
1964 if (h2s->id > h2c->max_id)
1965 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001966
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001967 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001968
1969 conn_err:
1970 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001972
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001973 out:
1974 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001975 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001976
1977 send_rst:
1978 /* make the demux send an RST for the current stream. We may only
1979 * do this if we're certain that the HEADERS frame was properly
1980 * decompressed so that the HPACK decoder is still kept up to date.
1981 */
1982 h2_release_buf(h2c, &rxbuf);
1983 h2c->st0 = H2_CS_FRAME_E;
1984 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001985}
1986
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001987/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1988 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1989 * errors here are reported as connection errors since it's impossible to
1990 * recover from such errors after the compression context has been altered.
1991 */
1992static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1993{
1994 int error;
1995
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001996 if (!b_size(&h2c->dbuf))
1997 return NULL; // empty buffer
1998
1999 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2000 return NULL; // incomplete frame
2001
Willy Tarreau1915ca22019-01-24 11:49:37 +01002002 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002003
Willy Tarreau25919232019-01-03 14:48:18 +01002004 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002005 if (h2c->st0 >= H2_CS_ERROR)
2006 return NULL;
2007
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002008 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2009 /* RFC7540#5.1 */
2010 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2011 h2c->st0 = H2_CS_FRAME_E;
2012 return NULL;
2013 }
2014
Willy Tarreau25919232019-01-03 14:48:18 +01002015 if (error <= 0) {
2016 if (error == 0)
2017 return NULL; // missing data
2018
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002019 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002020 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002021 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002022 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002023 }
2024
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002025 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2026 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002027 if (h2s->cs)
2028 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002029 }
2030
Willy Tarreau927b88b2019-03-04 08:03:25 +01002031 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002032 h2s->st = H2_SS_ERROR;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002033 else if (h2s->cs && h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002034 h2s->st = H2_SS_HREM;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002035 else if ((!h2s || h2s->cs->flags & CS_FL_REOS) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002036 h2s_close(h2s);
2037
2038 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002039}
2040
Willy Tarreau454f9052017-10-26 19:40:35 +02002041/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2042 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2043 */
2044static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2045{
2046 int error;
2047
2048 /* note that empty DATA frames are perfectly valid and sometimes used
2049 * to signal an end of stream (with the ES flag).
2050 */
2051
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002052 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002053 return 0; // empty buffer
2054
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002055 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002056 return 0; // incomplete frame
2057
2058 /* now either the frame is complete or the buffer is complete */
2059
Willy Tarreau454f9052017-10-26 19:40:35 +02002060 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2061 /* RFC7540#6.1 */
2062 error = H2_ERR_STREAM_CLOSED;
2063 goto strm_err;
2064 }
2065
Willy Tarreau1915ca22019-01-24 11:49:37 +01002066 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2067 /* RFC7540#8.1.2 */
2068 error = H2_ERR_PROTOCOL_ERROR;
2069 goto strm_err;
2070 }
2071
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002072 if (!h2_frt_transfer_data(h2s))
2073 return 0;
2074
Willy Tarreau454f9052017-10-26 19:40:35 +02002075 /* call the upper layers to process the frame, then let the upper layer
2076 * notify the stream about any change.
2077 */
2078 if (!h2s->cs) {
2079 error = H2_ERR_STREAM_CLOSED;
2080 goto strm_err;
2081 }
2082
Willy Tarreau8f650c32017-11-21 19:36:21 +01002083 if (h2c->st0 >= H2_CS_ERROR)
2084 return 0;
2085
Willy Tarreau721c9742017-11-07 11:05:42 +01002086 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002087 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002088 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 }
2090
2091 /* check for completion : the callee will change this to FRAME_A or
2092 * FRAME_H once done.
2093 */
2094 if (h2c->st0 == H2_CS_FRAME_P)
2095 return 0;
2096
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002097 /* last frame */
2098 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002099 if (h2s->st == H2_SS_OPEN)
2100 h2s->st = H2_SS_HREM;
2101 else
2102 h2s_close(h2s);
2103
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002104 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002105 if (h2s->cs)
2106 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002107
2108 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2109 /* RFC7540#8.1.2 */
2110 error = H2_ERR_PROTOCOL_ERROR;
2111 goto strm_err;
2112 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002113 }
2114
Willy Tarreau454f9052017-10-26 19:40:35 +02002115 return 1;
2116
Willy Tarreau454f9052017-10-26 19:40:35 +02002117 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002118 h2s_error(h2s, error);
2119 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002120 return 0;
2121}
2122
Willy Tarreaubc933932017-10-09 16:21:43 +02002123/* process Rx frames to be demultiplexed */
2124static void h2_process_demux(struct h2c *h2c)
2125{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002126 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002127 struct h2_fh hdr;
2128 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002129
Willy Tarreau081d4722017-05-16 21:51:05 +02002130 if (h2c->st0 >= H2_CS_ERROR)
2131 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002132
2133 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2134 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002135 if (h2c->flags & H2_CF_IS_BACK)
2136 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002137 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2138 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002139 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002140 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002141 sess_log(h2c->conn->owner);
2142 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002143 goto fail;
2144 }
2145
2146 h2c->max_id = 0;
2147 h2c->st0 = H2_CS_SETTINGS1;
2148 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002149
2150 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002151 /* ensure that what is pending is a valid SETTINGS frame
2152 * without an ACK.
2153 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002154 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002155 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002156 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002157 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002158 sess_log(h2c->conn->owner);
2159 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002160 goto fail;
2161 }
2162
2163 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2164 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2165 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2166 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002167 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002168 goto fail;
2169 }
2170
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002171 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002172 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2173 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2174 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002175 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002176 goto fail;
2177 }
2178
Willy Tarreau3bf69182018-12-21 15:34:50 +01002179 /* that's OK, switch to FRAME_P to process it. This is
2180 * a SETTINGS frame whose header has already been
2181 * deleted above.
2182 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002183 padlen = 0;
2184 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002185 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002186 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002187
2188 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002189 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002190 int ret = 0;
2191
2192 if (h2c->st0 >= H2_CS_ERROR)
2193 break;
2194
2195 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002196 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002197 break;
2198
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002199 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002200 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002201 if (!h2c->nb_streams) {
2202 /* only log if no other stream can report the error */
2203 sess_log(h2c->conn->owner);
2204 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002205 break;
2206 }
2207
Willy Tarreau3bf69182018-12-21 15:34:50 +01002208 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2209 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2210 * we read the pad length and drop it from the remaining
2211 * payload (one byte + the 9 remaining ones = 10 total
2212 * removed), so we have a frame payload starting after the
2213 * pad len. Flow controlled frames (DATA) also count the
2214 * padlen in the flow control, so it must be adjusted.
2215 */
2216 if (hdr.len < 1) {
2217 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2218 sess_log(h2c->conn->owner);
2219 goto fail;
2220 }
2221 hdr.len--;
2222
2223 if (b_data(&h2c->dbuf) < 10)
2224 break; // missing padlen
2225
2226 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2227
2228 if (padlen > hdr.len) {
2229 /* RFC7540#6.1 : pad length = length of
2230 * frame payload or greater => error.
2231 */
2232 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2233 sess_log(h2c->conn->owner);
2234 goto fail;
2235 }
2236
2237 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2238 h2c->rcvd_c++;
2239 h2c->rcvd_s++;
2240 }
2241 b_del(&h2c->dbuf, 1);
2242 }
2243 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002244
2245 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246 h2c->dfl = hdr.len;
2247 h2c->dsi = hdr.sid;
2248 h2c->dft = hdr.ft;
2249 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002250 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002251 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002252
2253 /* check for minimum basic frame format validity */
2254 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2255 if (ret != H2_ERR_NO_ERROR) {
2256 h2c_error(h2c, ret);
2257 sess_log(h2c->conn->owner);
2258 goto fail;
2259 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002260 }
2261
2262 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002263 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2264
Willy Tarreau567beb82018-12-18 16:52:44 +01002265 if (tmp_h2s != h2s && h2s && h2s->cs &&
2266 (b_data(&h2s->rxbuf) ||
2267 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002268 /* we may have to signal the upper layers */
2269 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002270 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002271 }
2272 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002273
Willy Tarreaud7901432017-12-29 11:34:40 +01002274 if (h2c->st0 == H2_CS_FRAME_E)
2275 goto strm_err;
2276
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002277 if (h2s->st == H2_SS_IDLE &&
2278 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2279 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2280 * this state MUST be treated as a connection error
2281 */
2282 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002283 if (!h2c->nb_streams) {
2284 /* only log if no other stream can report the error */
2285 sess_log(h2c->conn->owner);
2286 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002287 break;
2288 }
2289
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002290 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2291 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2292 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002293 * this state MUST be treated as a stream error.
2294 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2295 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002296 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002297 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2298 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2299 else
2300 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002301 goto strm_err;
2302 }
2303
Willy Tarreauab837502017-12-27 15:07:30 +01002304 /* Below the management of frames received in closed state is a
2305 * bit hackish because the spec makes strong differences between
2306 * streams closed by receiving RST, sending RST, and seeing ES
2307 * in both directions. In addition to this, the creation of a
2308 * new stream reusing the identifier of a closed one will be
2309 * detected here. Given that we cannot keep track of all closed
2310 * streams forever, we consider that unknown closed streams were
2311 * closed on RST received, which allows us to respond with an
2312 * RST without breaking the connection (eg: to abort a transfer).
2313 * Some frames have to be silently ignored as well.
2314 */
2315 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002316 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002317 /* #5.1.1: The identifier of a newly
2318 * established stream MUST be numerically
2319 * greater than all streams that the initiating
2320 * endpoint has opened or reserved. This
2321 * governs streams that are opened using a
2322 * HEADERS frame and streams that are reserved
2323 * using PUSH_PROMISE. An endpoint that
2324 * receives an unexpected stream identifier
2325 * MUST respond with a connection error.
2326 */
2327 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2328 goto strm_err;
2329 }
2330
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002331 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002332 /* RFC7540#5.1:closed: an endpoint that
2333 * receives any frame other than PRIORITY after
2334 * receiving a RST_STREAM MUST treat that as a
2335 * stream error of type STREAM_CLOSED.
2336 *
2337 * Note that old streams fall into this category
2338 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002339 *
2340 * However, we cannot generalize this to all frame types. Those
2341 * carrying compression state must still be processed before
2342 * being dropped or we'll desynchronize the decoder. This can
2343 * happen with request trailers received after sending an
2344 * RST_STREAM, or with header/trailers responses received after
2345 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002346 */
2347 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2348 h2c->st0 = H2_CS_FRAME_E;
2349 goto strm_err;
2350 }
2351
2352 /* RFC7540#5.1:closed: if this state is reached as a
2353 * result of sending a RST_STREAM frame, the peer that
2354 * receives the RST_STREAM might have already sent
2355 * frames on the stream that cannot be withdrawn. An
2356 * endpoint MUST ignore frames that it receives on
2357 * closed streams after it has sent a RST_STREAM
2358 * frame. An endpoint MAY choose to limit the period
2359 * over which it ignores frames and treat frames that
2360 * arrive after this time as being in error.
2361 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002362 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* RFC7540#5.1:closed: any frame other than
2364 * PRIO/WU/RST in this state MUST be treated as
2365 * a connection error
2366 */
2367 if (h2c->dft != H2_FT_RST_STREAM &&
2368 h2c->dft != H2_FT_PRIORITY &&
2369 h2c->dft != H2_FT_WINDOW_UPDATE) {
2370 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2371 goto strm_err;
2372 }
2373 }
2374 }
2375
Willy Tarreauc0da1962017-10-30 18:38:00 +01002376#if 0
2377 // problem below: it is not possible to completely ignore such
2378 // streams as we need to maintain the compression state as well
2379 // and for this we need to completely process these frames (eg:
2380 // HEADERS frames) as well as counting DATA frames to emit
2381 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2382 // This is a typical case of layer violation where the
2383 // transported contents are critical to the connection's
2384 // validity and must be ignored at the same time :-(
2385
2386 /* graceful shutdown, ignore streams whose ID is higher than
2387 * the one advertised in GOAWAY. RFC7540#6.8.
2388 */
2389 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002390 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2391 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002392 h2c->dfl -= ret;
2393 ret = h2c->dfl == 0;
2394 goto strm_err;
2395 }
2396#endif
2397
Willy Tarreau7e98c052017-10-10 15:56:59 +02002398 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002399 case H2_FT_SETTINGS:
2400 if (h2c->st0 == H2_CS_FRAME_P)
2401 ret = h2c_handle_settings(h2c);
2402
2403 if (h2c->st0 == H2_CS_FRAME_A)
2404 ret = h2c_ack_settings(h2c);
2405 break;
2406
Willy Tarreaucf68c782017-10-10 17:11:41 +02002407 case H2_FT_PING:
2408 if (h2c->st0 == H2_CS_FRAME_P)
2409 ret = h2c_handle_ping(h2c);
2410
2411 if (h2c->st0 == H2_CS_FRAME_A)
2412 ret = h2c_ack_ping(h2c);
2413 break;
2414
Willy Tarreau26f95952017-07-27 17:18:30 +02002415 case H2_FT_WINDOW_UPDATE:
2416 if (h2c->st0 == H2_CS_FRAME_P)
2417 ret = h2c_handle_window_update(h2c, h2s);
2418 break;
2419
Willy Tarreau61290ec2017-10-17 08:19:21 +02002420 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002421 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2422 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2423 * frames' parsers consume all following CONTINUATION
2424 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002425 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002426 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2427 sess_log(h2c->conn->owner);
2428 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002429
Willy Tarreau13278b42017-10-13 19:23:14 +02002430 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002431 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002432 if (h2c->flags & H2_CF_IS_BACK)
2433 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2434 else
2435 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002436 if (tmp_h2s) {
2437 h2s = tmp_h2s;
2438 ret = 1;
2439 }
2440 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002441 break;
2442
Willy Tarreau454f9052017-10-26 19:40:35 +02002443 case H2_FT_DATA:
2444 if (h2c->st0 == H2_CS_FRAME_P)
2445 ret = h2c_frt_handle_data(h2c, h2s);
2446
2447 if (h2c->st0 == H2_CS_FRAME_A)
2448 ret = h2c_send_strm_wu(h2c);
2449 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002450
Willy Tarreau92153fc2017-12-03 19:46:19 +01002451 case H2_FT_PRIORITY:
2452 if (h2c->st0 == H2_CS_FRAME_P)
2453 ret = h2c_handle_priority(h2c);
2454 break;
2455
Willy Tarreaucd234e92017-08-18 10:59:39 +02002456 case H2_FT_RST_STREAM:
2457 if (h2c->st0 == H2_CS_FRAME_P)
2458 ret = h2c_handle_rst_stream(h2c, h2s);
2459 break;
2460
Willy Tarreaue96b0922017-10-30 00:28:29 +01002461 case H2_FT_GOAWAY:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_handle_goaway(h2c);
2464 break;
2465
Willy Tarreau1c661982017-10-30 13:52:01 +01002466 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002467 default:
2468 /* drop frames that we ignore. They may be larger than
2469 * the buffer so we drain all of their contents until
2470 * we reach the end.
2471 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002472 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2473 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002474 h2c->dfl -= ret;
2475 ret = h2c->dfl == 0;
2476 }
2477
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002478 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002479 /* We may have to send an RST if not done yet */
2480 if (h2s->st == H2_SS_ERROR)
2481 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002482
Willy Tarreaua20a5192017-12-27 11:02:06 +01002483 if (h2c->st0 == H2_CS_FRAME_E)
2484 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002485
Willy Tarreau7e98c052017-10-10 15:56:59 +02002486 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002487 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002488 break;
2489
2490 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002491 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002492 h2c->st0 = H2_CS_FRAME_H;
2493 }
2494 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002495
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002496 if (h2c->rcvd_c > 0 &&
2497 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2498 h2c_send_conn_wu(h2c);
2499
Willy Tarreau52eed752017-09-22 15:05:09 +02002500 fail:
2501 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002502 if (h2s && h2s->cs &&
2503 (b_data(&h2s->rxbuf) ||
2504 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002505 /* we may have to signal the upper layers */
2506 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002507 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002508 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002509
Willy Tarreau47b515a2018-12-21 16:09:41 +01002510 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002511}
2512
2513/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2514 * the end.
2515 */
2516static int h2_process_mux(struct h2c *h2c)
2517{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002518 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002519
Willy Tarreau01b44822018-10-03 14:26:37 +02002520 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2521 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2522 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2523 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2524 if (h2c->st0 == H2_CS_ERROR) {
2525 h2c->st0 = H2_CS_ERROR2;
2526 sess_log(h2c->conn->owner);
2527 }
2528 goto fail;
2529 }
2530 h2c->st0 = H2_CS_SETTINGS1;
2531 }
2532 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002533 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002534 return 1;
2535 }
2536
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002537 /* start by sending possibly pending window updates */
2538 if (h2c->rcvd_c > 0 &&
2539 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2540 h2c_send_conn_wu(h2c) < 0)
2541 goto fail;
2542
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002543 /* First we always process the flow control list because the streams
2544 * waiting there were already elected for immediate emission but were
2545 * blocked just on this.
2546 */
2547
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002548 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002549 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2550 h2c->st0 >= H2_CS_ERROR)
2551 break;
2552
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002553 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002554 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2555 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002556 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002557 LIST_DEL(&h2s->list);
2558 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002559 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002560 }
2561
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002562 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002563 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2564 break;
2565
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002566 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002567 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2568 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002569 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002570 LIST_DEL(&h2s->list);
2571 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002572 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002573 }
2574
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002575 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002576 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002577 if (h2c->st0 == H2_CS_ERROR) {
2578 if (h2c->max_id >= 0) {
2579 h2c_send_goaway_error(h2c, NULL);
2580 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2581 return 0;
2582 }
2583
2584 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2585 }
2586 return 1;
2587 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002588 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002589}
2590
Willy Tarreau62f52692017-10-08 23:01:42 +02002591
Willy Tarreau479998a2018-11-18 06:30:59 +01002592/* Attempt to read data, and subscribe if none available.
2593 * The function returns 1 if data has been received, otherwise zero.
2594 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002595static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002596{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002597 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002598 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002599 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002600 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002601
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002602 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002603 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002604
Willy Tarreau315d8072017-12-10 22:17:57 +01002605 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002606 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002607
Willy Tarreau44e973f2018-03-01 17:49:30 +01002608 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002609 if (!buf) {
2610 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002611 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002612 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002613
Olivier Houchard7505f942018-08-21 18:10:44 +02002614 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002615 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002616 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2617 /* HTX in use : try to pre-align the buffer like the
2618 * rxbufs will be to optimize memory copies. We'll make
2619 * sure that the frame header lands at the end of the
2620 * HTX block to alias it upon recv. We cannot use the
2621 * head because rcv_buf() will realign the buffer if
2622 * it's empty. Thus we cheat and pretend we already
2623 * have a few bytes there.
2624 */
2625 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002626 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002627 }
2628 else
2629 max = b_room(buf);
2630
Olivier Houchard7505f942018-08-21 18:10:44 +02002631 if (max)
2632 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2633 else
2634 ret = 0;
2635 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002636
Olivier Houchard53216e72018-10-10 15:46:36 +02002637 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002638 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002639
Olivier Houcharda1411e62018-08-17 18:42:48 +02002640 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002641 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002642 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002643 }
2644
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002645 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002646 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002647 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002648}
2649
Willy Tarreau479998a2018-11-18 06:30:59 +01002650/* Try to send data if possible.
2651 * The function returns 1 if data have been sent, otherwise zero.
2652 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002653static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002654{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002655 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002656 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002657 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002658
2659 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002660 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661
Olivier Houchard7505f942018-08-21 18:10:44 +02002662
Willy Tarreaua2af5122017-10-09 11:56:46 +02002663 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2664 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002665 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666 }
2667
Willy Tarreaubc933932017-10-09 16:21:43 +02002668 /* This loop is quite simple : it tries to fill as much as it can from
2669 * pending streams into the existing buffer until it's reportedly full
2670 * or the end of send requests is reached. Then it tries to send this
2671 * buffer's contents out, marks it not full if at least one byte could
2672 * be sent, and tries again.
2673 *
2674 * The snd_buf() function normally takes a "flags" argument which may
2675 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2676 * data immediately comes and CO_SFL_STREAMER to indicate that the
2677 * connection is streaming lots of data (used to increase TLS record
2678 * size at the expense of latency). The former can be sent any time
2679 * there's a buffer full flag, as it indicates at least one stream
2680 * attempted to send and failed so there are pending data. An
2681 * alternative would be to set it as long as there's an active stream
2682 * but that would be problematic for ACKs until we have an absolute
2683 * guarantee that all waiters have at least one byte to send. The
2684 * latter should possibly not be set for now.
2685 */
2686
2687 done = 0;
2688 while (!done) {
2689 unsigned int flags = 0;
2690
2691 /* fill as much as we can into the current buffer */
2692 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2693 done = h2_process_mux(h2c);
2694
Olivier Houchard2b094432019-01-29 18:28:36 +01002695 if (h2c->flags & H2_CF_MUX_MALLOC)
2696 break;
2697
Willy Tarreaubc933932017-10-09 16:21:43 +02002698 if (conn->flags & CO_FL_ERROR)
2699 break;
2700
2701 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2702 flags |= CO_SFL_MSG_MORE;
2703
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002704 if (b_data(&h2c->mbuf)) {
2705 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002706 if (!ret)
2707 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002708 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002709 b_del(&h2c->mbuf, ret);
2710 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002711 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002712
2713 /* wrote at least one byte, the buffer is not full anymore */
2714 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2715 }
2716
Willy Tarreaua2af5122017-10-09 11:56:46 +02002717 if (conn->flags & CO_FL_SOCK_WR_SH) {
2718 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002719 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002720 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002721 /* We're not full anymore, so we can wake any task that are waiting
2722 * for us.
2723 */
2724 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002725 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002726 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2727 struct h2s *, list);
2728 LIST_DEL(&h2s->list);
2729 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002730 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002731 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2732 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002733 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002734 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002735 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002736 /* We're done, no more to send */
2737 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002738 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002739schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002740 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2741 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002742 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002743}
2744
2745static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2746{
2747 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002748 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002749
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002750 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002751 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002752 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002753 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002754 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002755 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002756 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002757}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002758
Willy Tarreau62f52692017-10-08 23:01:42 +02002759/* callback called on any event by the connection handler.
2760 * It applies changes and returns zero, or < 0 if it wants immediate
2761 * destruction of the connection (which normally doesn not happen in h2).
2762 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002763static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002764{
Olivier Houchard7505f942018-08-21 18:10:44 +02002765 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002766
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002767 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002768 h2_process_demux(h2c);
2769
2770 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002771 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002772
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002773 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002774 h2c->flags &= ~H2_CF_DEM_DFULL;
2775 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002776 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002777
Willy Tarreau0b37d652018-10-03 10:33:02 +02002778 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002779 /* frontend is stopping, reload likely in progress, let's try
2780 * to announce a graceful shutdown if not yet done. We don't
2781 * care if it fails, it will be tried again later.
2782 */
2783 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2784 if (h2c->last_sid < 0)
2785 h2c->last_sid = (1U << 31) - 1;
2786 h2c_send_goaway_error(h2c, NULL);
2787 }
2788 }
2789
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002790 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002791 * If we received early data, and the handshake is done, wake
2792 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002793 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002794 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2795 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2796 struct eb32_node *node;
2797 struct h2s *h2s;
2798
2799 h2c->flags |= H2_CF_WAIT_FOR_HS;
2800 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2801
2802 while (node) {
2803 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002804 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002805 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002806 node = eb32_next(node);
2807 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002808 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002809
Willy Tarreau26bd7612017-10-09 16:47:04 +02002810 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002811 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2812 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2813 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002814 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002815
2816 if (eb_is_empty(&h2c->streams_by_id)) {
2817 /* no more stream, kill the connection now */
2818 h2_release(conn);
2819 return -1;
2820 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002821 }
2822
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002823 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002824 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002825
Olivier Houchard53216e72018-10-10 15:46:36 +02002826 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2827 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2828 (h2c->st0 != H2_CS_ERROR &&
2829 !b_data(&h2c->mbuf) &&
2830 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2831 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002832 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002833
Willy Tarreau3f133572017-10-31 19:21:06 +01002834 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002835 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002836 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002837 task_queue(h2c->task);
2838 }
2839 else
2840 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002841 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002842
Olivier Houchard7505f942018-08-21 18:10:44 +02002843 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002844 return 0;
2845}
2846
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002847static int h2_wake(struct connection *conn)
2848{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002849 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002850
2851 return (h2_process(h2c));
2852}
2853
Willy Tarreauea392822017-10-31 10:02:25 +01002854/* Connection timeout management. The principle is that if there's no receipt
2855 * nor sending for a certain amount of time, the connection is closed. If the
2856 * MUX buffer still has lying data or is not allocatable, the connection is
2857 * immediately killed. If it's allocatable and empty, we attempt to send a
2858 * GOAWAY frame.
2859 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002860static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002861{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002862 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002863 int expired = tick_is_expired(t->expire, now_ms);
2864
Willy Tarreau0975f112018-03-29 15:22:59 +02002865 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002866 return t;
2867
Willy Tarreau0975f112018-03-29 15:22:59 +02002868 task_delete(t);
2869 task_free(t);
2870
2871 if (!h2c) {
2872 /* resources were already deleted */
2873 return NULL;
2874 }
2875
2876 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002877 h2c_error(h2c, H2_ERR_NO_ERROR);
2878 h2_wake_some_streams(h2c, 0, 0);
2879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002881 /* don't even try to send a GOAWAY, the buffer is stuck */
2882 h2c->flags |= H2_CF_GOAWAY_FAILED;
2883 }
2884
2885 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002886 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002887 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2888 h2c->flags |= H2_CF_GOAWAY_FAILED;
2889
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2891 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002892 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 b_del(&h2c->mbuf, ret);
2894 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002895 }
2896 }
Willy Tarreauea392822017-10-31 10:02:25 +01002897
Willy Tarreau0975f112018-03-29 15:22:59 +02002898 /* either we can release everything now or it will be done later once
2899 * the last stream closes.
2900 */
2901 if (eb_is_empty(&h2c->streams_by_id))
2902 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002903
Willy Tarreauea392822017-10-31 10:02:25 +01002904 return NULL;
2905}
2906
2907
Willy Tarreau62f52692017-10-08 23:01:42 +02002908/*******************************************/
2909/* functions below are used by the streams */
2910/*******************************************/
2911
2912/*
2913 * Attach a new stream to a connection
2914 * (Used for outgoing connections)
2915 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002916static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002917{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002918 struct conn_stream *cs;
2919 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002920 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002921
2922 cs = cs_new(conn);
2923 if (!cs)
2924 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002925 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002926 if (!h2s) {
2927 cs_free(cs);
2928 return NULL;
2929 }
2930 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002931}
2932
Willy Tarreaufafd3982018-11-18 21:29:20 +01002933/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2934 * We have to scan because we may have some orphan streams. It might be
2935 * beneficial to scan backwards from the end to reduce the likeliness to find
2936 * orphans.
2937 */
2938static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2939{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002940 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002941 struct h2s *h2s;
2942 struct eb32_node *node;
2943
2944 node = eb32_first(&h2c->streams_by_id);
2945 while (node) {
2946 h2s = container_of(node, struct h2s, by_id);
2947 if (h2s->cs)
2948 return h2s->cs;
2949 node = eb32_next(node);
2950 }
2951 return NULL;
2952}
2953
Willy Tarreau62f52692017-10-08 23:01:42 +02002954/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002955 * Destroy the mux and the associated connection, if it is no longer used
2956 */
2957static void h2_destroy(struct connection *conn)
2958{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002959 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002960
2961 if (eb_is_empty(&h2c->streams_by_id))
2962 h2_release(h2c->conn);
2963}
2964
2965/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002966 * Detach the stream from the connection and possibly release the connection.
2967 */
2968static void h2_detach(struct conn_stream *cs)
2969{
Willy Tarreau60935142017-10-16 18:11:19 +02002970 struct h2s *h2s = cs->ctx;
2971 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002972 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002973
2974 cs->ctx = NULL;
2975 if (!h2s)
2976 return;
2977
Olivier Houchardf502aca2018-12-14 19:42:40 +01002978 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002979 h2c = h2s->h2c;
2980 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002981 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01002982 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
2983 !h2_frt_has_too_many_cs(h2c)) {
2984 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02002985 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002986 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002987 }
Willy Tarreau60935142017-10-16 18:11:19 +02002988
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002989 /* this stream may be blocked waiting for some data to leave (possibly
2990 * an ES or RST frame), so orphan it in this case.
2991 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002992 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002993 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002994 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002995 return;
2996
Willy Tarreau45f752e2017-10-30 15:44:59 +01002997 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2998 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2999 /* unblock the connection if it was blocked on this
3000 * stream.
3001 */
3002 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3003 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003004 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003005 }
3006
Willy Tarreau71049cc2018-03-28 13:56:39 +02003007 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003008
Olivier Houchard8a786902018-12-15 16:05:40 +01003009 if (h2c->flags & H2_CF_IS_BACK &&
3010 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003011 if (!(h2c->conn->flags &
3012 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3013 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003014 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003015 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3016 h2c->conn->owner = NULL;
3017 if (eb_is_empty(&h2c->streams_by_id)) {
3018 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3019 /* The server doesn't want it, let's kill the connection right away */
3020 h2c->conn->mux->destroy(h2c->conn);
3021 return;
3022 }
3023 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003024 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003025 if (eb_is_empty(&h2c->streams_by_id)) {
3026 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3027 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3028 return;
3029 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003030 /* Never ever allow to reuse a connection from a non-reuse backend */
3031 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3032 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003033 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003034 struct server *srv = objt_server(h2c->conn->target);
3035
3036 if (srv) {
3037 if (h2c->conn->flags & CO_FL_PRIVATE)
3038 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3039 else
3040 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3041 }
3042
3043 }
3044 }
3045 }
3046
Willy Tarreaue323f342018-03-28 13:51:45 +02003047 /* We don't want to close right now unless we're removing the
3048 * last stream, and either the connection is in error, or it
3049 * reached the ID already specified in a GOAWAY frame received
3050 * or sent (as seen by last_sid >= 0).
3051 */
3052 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3053 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003054 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard93c88522018-11-30 15:39:16 +01003055 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003056 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003057 (conn_xprt_read0_pending(h2c->conn) ||
3058 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3059 /* no more stream will come, kill it now */
3060 h2_release(h2c->conn);
3061 }
3062 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003063 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003064 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3065 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003066 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003067 else
3068 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003069 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003070}
3071
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003072static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003073{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003074 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003075 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003076
Willy Tarreau721c9742017-11-07 11:05:42 +01003077 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003078 return;
3079
Willy Tarreau18059042019-01-31 19:12:48 +01003080 /* a connstream may require us to immediately kill the whole connection
3081 * for example because of a "tcp-request content reject" rule that is
3082 * normally used to limit abuse. In this case we schedule a goaway to
3083 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003084 */
Willy Tarreau18059042019-01-31 19:12:48 +01003085 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3086 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3087 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3088 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3089 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003090 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3091 /* Nothing was never sent for this stream, so reset with
3092 * REFUSED_STREAM error to let the client retry the
3093 * request.
3094 */
3095 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3096 }
Willy Tarreau18059042019-01-31 19:12:48 +01003097
Willy Tarreau90c32322017-11-24 08:00:30 +01003098 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003099 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003100 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003101
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003102 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003103 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003104 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003105
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003106 return;
3107add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003108 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003109 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003110 if (h2s->flags & H2_SF_BLK_MFCTL) {
3111 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3112 h2s->send_wait = sw;
3113 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3114 h2s->send_wait = sw;
3115 LIST_ADDQ(&h2c->send_list, &h2s->list);
3116 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003117 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003118 /* Let the handler know we want shutr */
3119 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003120}
3121
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003122static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003123{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003124 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003125 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003126
Willy Tarreau721c9742017-11-07 11:05:42 +01003127 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003128 return;
3129
Willy Tarreau67434202017-11-06 20:20:51 +01003130 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003131 /* we can cleanly close using an empty data frame only after headers */
3132
3133 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3134 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003135 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003136
3137 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003138 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003139 else
3140 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003141 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003142 /* a connstream may require us to immediately kill the whole connection
3143 * for example because of a "tcp-request content reject" rule that is
3144 * normally used to limit abuse. In this case we schedule a goaway to
3145 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003146 */
Willy Tarreau18059042019-01-31 19:12:48 +01003147 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3148 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3149 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3150 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3151 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003152 else {
3153 /* Nothing was never sent for this stream, so reset with
3154 * REFUSED_STREAM error to let the client retry the
3155 * request.
3156 */
3157 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3158 }
Willy Tarreau18059042019-01-31 19:12:48 +01003159
Willy Tarreau90c32322017-11-24 08:00:30 +01003160 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003161 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003162 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003163
Willy Tarreau00dd0782018-03-01 16:31:34 +01003164 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003165 }
3166
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003167 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003168 tasklet_wakeup(h2c->wait_event.task);
3169 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003170
3171 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003172 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003173 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003174 if (h2s->flags & H2_SF_BLK_MFCTL) {
3175 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3176 h2s->send_wait = sw;
3177 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3178 h2s->send_wait = sw;
3179 LIST_ADDQ(&h2c->send_list, &h2s->list);
3180 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003181 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003182 /* let the handler know we want to shutw */
3183 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003184}
3185
3186static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3187{
3188 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003189 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003190
Olivier Houchard2c68a462018-12-15 22:42:20 +01003191 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003192 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003193 h2s->send_wait = NULL;
3194 LIST_DEL(&h2s->list);
3195 LIST_INIT(&h2s->list);
3196 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003197 if (reason & 2)
3198 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003199 if (reason & 1)
3200 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003201
Olivier Houchard2c68a462018-12-15 22:42:20 +01003202 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003203 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003204 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003205 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003206}
3207
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003208static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3209{
3210 struct h2s *h2s = cs->ctx;
3211
3212 if (!mode)
3213 return;
3214
3215 h2_do_shutr(h2s);
3216}
3217
3218static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3219{
3220 struct h2s *h2s = cs->ctx;
3221
3222 h2_do_shutw(h2s);
3223}
3224
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003225/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003226 * HTX request or response depending on the connection's side. Returns a
3227 * positive value on success, a negative value on failure, or 0 if it couldn't
3228 * proceed. May report connection errors in h2c->errcode if the frame is
3229 * non-decodable and the connection unrecoverable. In absence of connection
3230 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003231 *
3232 * The function may fold CONTINUATION frames into the initial HEADERS frame
3233 * by removing padding and next frame header, then moving the CONTINUATION
3234 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3235 * leaving a hole between the main frame and the beginning of the next one.
3236 * The possibly remaining incomplete or next frame at the end may be moved
3237 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3238 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3239 *
3240 * A buffer at the beginning of processing may look like this :
3241 *
3242 * ,---.---------.-----.--------------.--------------.------.---.
3243 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3244 * `---^---------^-----^--------------^--------------^------^---'
3245 * | | <-----> | |
3246 * area | dpl | wrap
3247 * |<--------------> |
3248 * | dfl |
3249 * |<-------------------------------------------------->|
3250 * head data
3251 *
3252 * Padding is automatically overwritten when folding, participating to the
3253 * hole size after dfl :
3254 *
3255 * ,---.------------------------.-----.--------------.------.---.
3256 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3257 * `---^------------------------^-----^--------------^------^---'
3258 * | | <-----> | |
3259 * area | hole | wrap
3260 * |<-----------------------> |
3261 * | dfl |
3262 * |<-------------------------------------------------->|
3263 * head data
3264 *
3265 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3266 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3267 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003268 *
3269 * The <flags> field must point to either the stream's flags or to a copy of it
3270 * so that the function can update the following flags :
3271 * - H2_SF_DATA_CLEN when content-length is seen
3272 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3273 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003274 *
3275 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3276 * decoding, in order to detect if we're dealing with a headers or a trailers
3277 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003278 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003279static 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 +02003280{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003281 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003282 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003283 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003284 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003285 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003286 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003287 int flen; // header frame len
3288 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003289 int ret = 0;
3290 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003291 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003292 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003293
Willy Tarreauea18f862018-12-22 20:19:26 +01003294next_frame:
3295 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3296 goto leave; // incomplete input frame
3297
3298 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3299 * this case, we'll try to paste it immediately after the initial
3300 * HEADERS frame payload and kill any possible padding. The initial
3301 * frame's length will be increased to represent the concatenation
3302 * of the two frames. The next frame is read from position <tlen>
3303 * and written at position <flen> (minus padding if some is present).
3304 */
3305 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3306 struct h2_fh hdr;
3307 int clen; // CONTINUATION frame's payload length
3308
3309 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3310 /* no more data, the buffer may be full, either due to
3311 * too large a frame or because of too large a hole that
3312 * we're going to compact at the end.
3313 */
3314 goto leave;
3315 }
3316
3317 if (hdr.ft != H2_FT_CONTINUATION) {
3318 /* RFC7540#6.10: frame of unexpected type */
3319 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3320 goto fail;
3321 }
3322
3323 if (hdr.sid != h2c->dsi) {
3324 /* RFC7540#6.10: frame of different stream */
3325 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3326 goto fail;
3327 }
3328
3329 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3330 /* RFC7540#4.2: invalid frame length */
3331 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3332 goto fail;
3333 }
3334
3335 /* detect when we must stop aggragating frames */
3336 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3337
3338 /* Take as much as we can of the CONTINUATION frame's payload */
3339 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3340 if (clen > hdr.len)
3341 clen = hdr.len;
3342
3343 /* Move the frame's payload over the padding, hole and frame
3344 * header. At least one of hole or dpl is null (see diagrams
3345 * above). The hole moves after the new aggragated frame.
3346 */
3347 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3348 h2c->dfl += clen - h2c->dpl;
3349 hole += h2c->dpl + 9;
3350 h2c->dpl = 0;
3351 goto next_frame;
3352 }
3353
3354 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003355
Willy Tarreau13278b42017-10-13 19:23:14 +02003356 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003357 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003358 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003359 copy = alloc_trash_chunk();
3360 if (!copy) {
3361 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3362 goto fail;
3363 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003364 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3365 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3366 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003367 }
3368
Willy Tarreau13278b42017-10-13 19:23:14 +02003369 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3370 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003371 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003372 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3373 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003374 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003375 }
3376
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003377 if (flen < 5) {
3378 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3379 goto fail;
3380 }
3381
Willy Tarreau13278b42017-10-13 19:23:14 +02003382 hdrs += 5; // stream dep = 4, weight = 1
3383 flen -= 5;
3384 }
3385
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003386 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003387 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003388 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003389 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003390
Willy Tarreau937f7602018-02-26 15:22:17 +01003391 /* we can't retry a failed decompression operation so we must be very
3392 * careful not to take any risks. In practice the output buffer is
3393 * always empty except maybe for trailers, in which case we simply have
3394 * to wait for the upper layer to finish consuming what is available.
3395 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003396
3397 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003398 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003399 if (!htx_is_empty(htx)) {
3400 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003401 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003402 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003403 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003404 if (b_data(rxbuf)) {
3405 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003406 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003407 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003408
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003409 rxbuf->head = 0;
3410 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003411 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003412
Willy Tarreau25919232019-01-03 14:48:18 +01003413 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003414 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3415 sizeof(list)/sizeof(list[0]), tmp);
3416 if (outlen < 0) {
3417 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3418 goto fail;
3419 }
3420
Willy Tarreau25919232019-01-03 14:48:18 +01003421 /* The PACK decompressor was updated, let's update the input buffer and
3422 * the parser's state to commit these changes and allow us to later
3423 * fail solely on the stream if needed.
3424 */
3425 b_del(&h2c->dbuf, h2c->dfl + hole);
3426 h2c->dfl = hole = 0;
3427 h2c->st0 = H2_CS_FRAME_H;
3428
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003429 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003430 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003431
Willy Tarreau88d138e2019-01-02 19:38:14 +01003432 if (*flags & H2_SF_HEADERS_RCVD)
3433 goto trailers;
3434
3435 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003436 if (htx) {
3437 /* HTX mode */
3438 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003439 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003440 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003441 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003442 } else {
3443 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003444 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003445 if (outlen > 0)
3446 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003447 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003448
3449 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003450 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003451 goto fail;
3452 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003453
Willy Tarreau174b06a2018-04-25 18:13:58 +02003454 if (msgf & H2_MSGF_BODY) {
3455 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003456 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003457 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003458 if (htx)
3459 htx->extra = *body_len;
3460 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003461 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003462 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003463 }
3464
Willy Tarreau88d138e2019-01-02 19:38:14 +01003465 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003466 /* indicate that a HEADERS frame was received for this stream, except
3467 * for 1xx responses. For 1xx responses, another HEADERS frame is
3468 * expected.
3469 */
3470 if (!(msgf & H2_MSGF_RSP_1XX))
3471 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003472
Christopher Faulet0b465482019-02-19 15:14:23 +01003473 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003474 /* Mark the end of message, either using EOM in HTX or with the
3475 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3476 * is not set during headers with END_STREAM.
3477 */
3478 if (htx) {
3479 if (!htx_add_endof(htx, HTX_BLK_EOM))
3480 goto fail;
3481 }
3482 else if (*flags & H2_SF_DATA_CHNK) {
3483 if (!b_putblk(rxbuf, "\r\n", 2))
3484 goto fail;
3485 }
3486 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003487
Willy Tarreau86277d42019-01-02 15:36:11 +01003488 /* success */
3489 ret = 1;
3490
Willy Tarreau68dd9852017-07-03 14:44:26 +02003491 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003492 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003493 * move the remaining data over it.
3494 */
3495 if (hole) {
3496 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3497 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3498 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3499 b_sub(&h2c->dbuf, hole);
3500 }
3501
3502 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3503 /* too large frames */
3504 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003505 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003506 }
3507
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003508 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003509 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003510 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003511 return ret;
3512
Willy Tarreau68dd9852017-07-03 14:44:26 +02003513 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003514 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003515 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003516
3517 trailers:
3518 /* This is the last HEADERS frame hence a trailer */
3519
3520 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3521 /* It's a trailer but it's missing ES flag */
3522 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3523 goto fail;
3524 }
3525
3526 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3527 * block, and when using chunks we must send the 0 CRLF marker. For
3528 * other modes, the trailers are silently dropped.
3529 */
3530 if (htx) {
3531 if (!htx_add_endof(htx, HTX_BLK_EOD))
3532 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003533 if (h2_make_htx_trailers(list, htx) <= 0)
3534 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003535 }
3536 else if (*flags & H2_SF_DATA_CHNK) {
3537 /* Legacy mode with chunked encoding : we must finalize the
3538 * data block message emit the trailing CRLF */
3539 if (!b_putblk(rxbuf, "0\r\n", 3))
3540 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003541
3542 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3543 if (outlen > 0)
3544 b_add(rxbuf, outlen);
3545 else
3546 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003547 }
3548
3549 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003550}
3551
Willy Tarreau454f9052017-10-26 19:40:35 +02003552/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3553 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3554 * in use, a new chunk is emitted for each frame. This is supposed to fit
3555 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3556 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3557 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003558 * parser state is automatically updated. Returns > 0 if it could completely
3559 * send the current frame, 0 if it couldn't complete, in which case
3560 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3561 * DATA frame can return 0 as a valid result). Stream errors are reported in
3562 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3563 * have checked the frame header and ensured that the frame was complete or the
3564 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003565 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003566static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003567{
3568 struct h2c *h2c = h2s->h2c;
3569 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003570 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003571 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003572 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003573 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003574
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003575 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003576
Olivier Houchard638b7992018-08-16 15:41:52 +02003577 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003578 if (!csbuf) {
3579 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003580 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003581 }
3582
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003583try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003584 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003585 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3586 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003587 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003588 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003589
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003590 if (flen > b_data(&h2c->dbuf)) {
3591 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003592 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003593 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003594 }
3595
Willy Tarreaua9b77962019-01-31 07:23:00 +01003596 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003597 block1 = htx_free_data_space(htx);
3598 if (!block1) {
3599 h2c->flags |= H2_CF_DEM_SFULL;
3600 goto fail;
3601 }
3602 if (flen > block1)
3603 flen = block1;
3604
3605 /* here, flen is the max we can copy into the output buffer */
3606 block1 = b_contig_data(&h2c->dbuf, 0);
3607 if (flen > block1)
3608 flen = block1;
3609
3610 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3611 h2c->flags |= H2_CF_DEM_SFULL;
3612 goto fail;
3613 }
3614
3615 b_del(&h2c->dbuf, flen);
3616 h2c->dfl -= flen;
3617 h2c->rcvd_c += flen;
3618 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003619
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003620 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003621 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003622 htx->extra = h2s->body_len;
3623 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003624 goto try_again;
3625 }
3626 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003627 /* it doesn't fit and the buffer is fragmented,
3628 * so let's defragment it and try again.
3629 */
3630 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003631 }
3632
Willy Tarreaueba10f22018-04-25 20:44:22 +02003633 /* chunked-encoding requires more room */
3634 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003635 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003636 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3637 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3638 (chklen < 1048576) ? 4 : 8;
3639 chklen += 4; // CRLF, CRLF
3640 }
3641
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003642 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003643 if (flen + chklen > b_room(csbuf)) {
3644 if (chklen >= b_room(csbuf)) {
3645 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003646 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003647 }
3648 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003649 }
3650
3651 if (h2s->flags & H2_SF_DATA_CHNK) {
3652 /* emit the chunk size */
3653 unsigned int chksz = flen;
3654 char str[10];
3655 char *beg;
3656
3657 beg = str + sizeof(str);
3658 *--beg = '\n';
3659 *--beg = '\r';
3660 do {
3661 *--beg = hextab[chksz & 0xF];
3662 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003663 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003664 }
3665
Willy Tarreau454f9052017-10-26 19:40:35 +02003666 /* Block1 is the length of the first block before the buffer wraps,
3667 * block2 is the optional second block to reach the end of the frame.
3668 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003669 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003670 if (block1 > flen)
3671 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003672 block2 = flen - block1;
3673
3674 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003675 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003676
3677 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003678 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003679
Willy Tarreaueba10f22018-04-25 20:44:22 +02003680 if (h2s->flags & H2_SF_DATA_CHNK) {
3681 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003682 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003683 }
3684
Willy Tarreau454f9052017-10-26 19:40:35 +02003685 /* now mark the input data as consumed (will be deleted from the buffer
3686 * by the caller when seeing FRAME_A after sending the window update).
3687 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003688 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003689 h2c->dfl -= flen;
3690 h2c->rcvd_c += flen;
3691 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3692
Willy Tarreau1915ca22019-01-24 11:49:37 +01003693 if (h2s->flags & H2_SF_DATA_CLEN)
3694 h2s->body_len -= flen;
3695
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003696 if (h2c->dfl > h2c->dpl) {
3697 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003698 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003699 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003700 }
3701
Willy Tarreau4a28da12018-01-04 14:41:00 +01003702 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003703 /* here we're done with the frame, all the payload (except padding) was
3704 * transferred.
3705 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003706
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003707 if (h2c->dff & H2_F_DATA_END_STREAM) {
3708 if (htx) {
3709 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3710 h2c->flags |= H2_CF_DEM_SFULL;
3711 goto fail;
3712 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003713 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003714 else if (h2s->flags & H2_SF_DATA_CHNK) {
3715 /* emit the trailing 0 CRLF CRLF */
3716 if (b_room(csbuf) < 5) {
3717 h2c->flags |= H2_CF_DEM_SFULL;
3718 goto fail;
3719 }
3720 chklen += 5;
3721 b_putblk(csbuf, "0\r\n\r\n", 5);
3722 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003723 }
3724
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003725 h2c->rcvd_c += h2c->dpl;
3726 h2c->rcvd_s += h2c->dpl;
3727 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003728 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3729
Willy Tarreau39d68502018-03-02 12:26:37 +01003730 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003731 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01003732 if (h2s->cs)
3733 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau39d68502018-03-02 12:26:37 +01003734 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003735 if (htx)
3736 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003737 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003738 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003739 if (htx)
3740 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003741 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003742}
3743
Willy Tarreau5dd17352018-06-14 13:33:30 +02003744/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3745 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3746 * number of bytes sent. The caller must check the stream's status to detect
3747 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003748 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003749static 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 +02003750{
3751 struct http_hdr list[MAX_HTTP_HDR];
3752 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003753 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003754 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003755 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003756 int es_now = 0;
3757 int ret = 0;
3758 int hdr;
3759
3760 if (h2c_mux_busy(h2c, h2s)) {
3761 h2s->flags |= H2_SF_BLK_MBUSY;
3762 return 0;
3763 }
3764
Willy Tarreau44e973f2018-03-01 17:49:30 +01003765 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003766 h2c->flags |= H2_CF_MUX_MALLOC;
3767 h2s->flags |= H2_SF_BLK_MROOM;
3768 return 0;
3769 }
3770
3771 /* First, try to parse the H1 response and index it into <list>.
3772 * NOTE! Since it comes from haproxy, we *know* that a response header
3773 * block does not wrap and we can safely read it this way without
3774 * having to realign the buffer.
3775 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003776 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003777 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003778 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003779 /* incomplete or invalid response, this is abnormal coming from
3780 * haproxy and may only result in a bad errorfile or bad Lua code
3781 * so that won't be fixed, raise an error now.
3782 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003783 * FIXME: we should instead add the ability to only return a
3784 * 502 bad gateway. But in theory this is not supposed to
3785 * happen.
3786 */
3787 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3788 ret = 0;
3789 goto end;
3790 }
3791
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003792 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003793
3794 /* certain statuses have no body or an empty one, regardless of
3795 * what the headers say.
3796 */
3797 if (sl.st.status >= 100 && sl.st.status < 200) {
3798 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3799 h1m->curr_len = h1m->body_len = 0;
3800 }
3801 else if (sl.st.status == 204 || sl.st.status == 304) {
3802 /* no contents, claim c-len is present and set to zero */
3803 h1m->flags &= ~H1_MF_CHNK;
3804 h1m->flags |= H1_MF_CLEN;
3805 h1m->curr_len = h1m->body_len = 0;
3806 }
3807
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003808 chunk_reset(&outbuf);
3809
3810 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003811 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003812 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003813 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003814
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003815 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003816 break;
3817 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003818 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003819 }
3820
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003821 if (outbuf.size < 9)
3822 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003823
3824 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003825 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3826 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3827 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003828
3829 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003830 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003831 /* this is an unparsable response */
3832 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3833 ret = 0;
3834 goto end;
3835 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003836
3837 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003838 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003839 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003840 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003841 }
3842
3843 /* encode all headers, stop at empty name */
3844 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003845 /* these ones do not exist in H2 and must be dropped. */
3846 if (isteq(list[hdr].n, ist("connection")) ||
3847 isteq(list[hdr].n, ist("proxy-connection")) ||
3848 isteq(list[hdr].n, ist("keep-alive")) ||
3849 isteq(list[hdr].n, ist("upgrade")) ||
3850 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003851 continue;
3852
3853 if (isteq(list[hdr].n, ist("")))
3854 break; // end
3855
3856 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3857 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003858 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003860 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 }
3862 }
3863
3864 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003865 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003866 es_now = 1;
3867
3868 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003869 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870
3871 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003872 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003873
3874 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003875 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003876
3877 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003878 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003879 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003880
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003881 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003882 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003883 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003884
Willy Tarreau801250e2018-09-11 11:45:04 +02003885 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 h2s->flags |= H2_SF_ES_SENT;
3887 if (h2s->st == H2_SS_OPEN)
3888 h2s->st = H2_SS_HLOC;
3889 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003890 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003892 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003893 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003894 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003895 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003896 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003897 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003898 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003899
3900 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003901
3902 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003903 //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 +02003904 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003905 full:
3906 h1m_init_res(h1m);
3907 h1m->err_pos = -1; // don't care about errors on the response path
3908 h2c->flags |= H2_CF_MUX_MFULL;
3909 h2s->flags |= H2_SF_BLK_MROOM;
3910 ret = 0;
3911 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003912}
3913
Willy Tarreau5dd17352018-06-14 13:33:30 +02003914/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3915 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3916 * the number of bytes sent. The caller must check the stream's status to
3917 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003918 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003919static 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 +02003920{
3921 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003922 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003923 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003924 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003925 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003926 int es_now = 0;
3927 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003928 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003929 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003930
3931 if (h2c_mux_busy(h2c, h2s)) {
3932 h2s->flags |= H2_SF_BLK_MBUSY;
3933 goto end;
3934 }
3935
Willy Tarreau44e973f2018-03-01 17:49:30 +01003936 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003937 h2c->flags |= H2_CF_MUX_MALLOC;
3938 h2s->flags |= H2_SF_BLK_MROOM;
3939 goto end;
3940 }
3941
3942 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003943 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944 goto end;
3945
3946 chunk_reset(&outbuf);
3947
3948 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003949 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003950 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003951 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003952
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003953 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003954 break;
3955 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003956 /* If there are pending data in the output buffer, and we have
3957 * less than 1/4 of the mbuf's size and everything fits, we'll
3958 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3959 * is full and wait, to save some slow realign calls.
3960 */
3961 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3962 h2c->flags |= H2_CF_MUX_MFULL;
3963 h2s->flags |= H2_SF_BLK_MROOM;
3964 goto end;
3965 }
3966
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003968 }
3969
3970 if (outbuf.size < 9) {
3971 h2c->flags |= H2_CF_MUX_MFULL;
3972 h2s->flags |= H2_SF_BLK_MROOM;
3973 goto end;
3974 }
3975
3976 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3978 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3979 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980
3981 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3982 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003983 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003984 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985 break;
3986 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003987 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003988 if ((long long)size > h1m->curr_len)
3989 size = h1m->curr_len;
3990 break;
3991 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003992 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003993 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003994 if (!ret)
3995 goto end;
3996
3997 if (ret < 0) {
3998 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003999 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004000 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4001 goto end;
4002 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004003 max -= ret;
4004 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004005 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004006 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004007 }
4008
Willy Tarreau801250e2018-09-11 11:45:04 +02004009 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004010 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004011 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004012 if (!ret)
4013 goto end;
4014
4015 if (ret < 0) {
4016 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004017 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4019 goto end;
4020 }
4021
4022 size = chunk;
4023 h1m->curr_len = chunk;
4024 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004025 max -= ret;
4026 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004028 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 if (!size)
4030 goto send_empty;
4031 }
4032
4033 /* in MSG_DATA state, continue below */
4034 size = h1m->curr_len;
4035 break;
4036 }
4037
4038 /* we have in <size> the exact number of bytes we need to copy from
4039 * the H1 buffer. We need to check this against the connection's and
4040 * the stream's send windows, and to ensure that this fits in the max
4041 * frame size and in the buffer's available space minus 9 bytes (for
4042 * the frame header). The connection's flow control is applied last so
4043 * that we can use a separate list of streams which are immediately
4044 * unblocked on window opening. Note: we don't implement padding.
4045 */
4046
Willy Tarreau5dd17352018-06-14 13:33:30 +02004047 if (size > max)
4048 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049
4050 if (size > h2s->mws)
4051 size = h2s->mws;
4052
4053 if (size <= 0) {
4054 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004055 if (h2s->send_wait) {
4056 LIST_DEL(&h2s->list);
4057 LIST_INIT(&h2s->list);
4058 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 goto end;
4060 }
4061
4062 if (h2c->mfs && size > h2c->mfs)
4063 size = h2c->mfs;
4064
4065 if (size + 9 > outbuf.size) {
4066 /* we have an opportunity for enlarging the too small
4067 * available space, let's try.
4068 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004069 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004070 goto realign_again;
4071 size = outbuf.size - 9;
4072 }
4073
4074 if (size <= 0) {
4075 h2c->flags |= H2_CF_MUX_MFULL;
4076 h2s->flags |= H2_SF_BLK_MROOM;
4077 goto end;
4078 }
4079
4080 if (size > h2c->mws)
4081 size = h2c->mws;
4082
4083 if (size <= 0) {
4084 h2s->flags |= H2_SF_BLK_MFCTL;
4085 goto end;
4086 }
4087
4088 /* copy whatever we can */
4089 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004090 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004091 if (ret == 1)
4092 len2 = 0;
4093
4094 if (!ret || len1 + len2 < size) {
4095 /* FIXME: must normally never happen */
4096 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4097 goto end;
4098 }
4099
4100 /* limit len1/len2 to size */
4101 if (len1 + len2 > size) {
4102 int sub = len1 + len2 - size;
4103
4104 if (len2 > sub)
4105 len2 -= sub;
4106 else {
4107 sub -= len2;
4108 len2 = 0;
4109 len1 -= sub;
4110 }
4111 }
4112
4113 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004114 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004116 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004117
4118 send_empty:
4119 /* we may need to add END_STREAM */
4120 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4121 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004122 *
4123 * FIXME: what we do here is not correct because we send end_stream
4124 * before knowing if we'll have to send a HEADERS frame for the
4125 * trailers. More importantly we're not consuming the trailing CRLF
4126 * after the end of trailers, so it will be left to the caller to
4127 * eat it. The right way to do it would be to measure trailers here
4128 * and to send ES only if there are no trailers.
4129 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004130 */
4131 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004132 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 es_now = 1;
4134
4135 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004136 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004137
4138 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004139 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004140
4141 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004142 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004143
4144 /* consume incoming H1 response */
4145 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004146 max -= size;
4147 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004148 total += size;
4149 h1m->curr_len -= size;
4150 h2s->mws -= size;
4151 h2c->mws -= size;
4152
4153 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004154 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004155 goto new_frame;
4156 }
4157 }
4158
4159 if (es_now) {
4160 if (h2s->st == H2_SS_OPEN)
4161 h2s->st = H2_SS_HLOC;
4162 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004163 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004164
Willy Tarreau35a62702018-02-27 15:37:25 +01004165 if (!(h1m->flags & H1_MF_CHNK)) {
4166 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004167 total += max;
4168 ofs += max;
4169 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004170
Willy Tarreau801250e2018-09-11 11:45:04 +02004171 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004172 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004173
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174 h2s->flags |= H2_SF_ES_SENT;
4175 }
4176
4177 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004178 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 +02004179 return total;
4180}
4181
Willy Tarreau115e83b2018-12-01 19:17:53 +01004182/* Try to send a HEADERS frame matching HTX response present in HTX message
4183 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4184 * must check the stream's status to detect any error which might have happened
4185 * subsequently to a successful send. The htx blocks are automatically removed
4186 * from the message. The htx message is assumed to be valid since produced from
4187 * the internal code, hence it contains a start line, an optional series of
4188 * header blocks and an end of header, otherwise an invalid frame could be
4189 * emitted and the resulting htx message could be left in an inconsistent state.
4190 */
4191static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4192{
4193 struct http_hdr list[MAX_HTTP_HDR];
4194 struct h2c *h2c = h2s->h2c;
4195 struct htx_blk *blk;
4196 struct htx_blk *blk_end;
4197 struct buffer outbuf;
4198 struct htx_sl *sl;
4199 enum htx_blk_type type;
4200 int es_now = 0;
4201 int ret = 0;
4202 int hdr;
4203 int idx;
4204
4205 if (h2c_mux_busy(h2c, h2s)) {
4206 h2s->flags |= H2_SF_BLK_MBUSY;
4207 return 0;
4208 }
4209
4210 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4211 h2c->flags |= H2_CF_MUX_MALLOC;
4212 h2s->flags |= H2_SF_BLK_MROOM;
4213 return 0;
4214 }
4215
4216 /* determine the first block which must not be deleted, blk_end may
4217 * be NULL if all blocks have to be deleted.
4218 */
4219 idx = htx_get_head(htx);
4220 blk_end = NULL;
4221 while (idx != -1) {
4222 type = htx_get_blk_type(htx_get_blk(htx, idx));
4223 idx = htx_get_next(htx, idx);
4224 if (type == HTX_BLK_EOH) {
4225 if (idx != -1)
4226 blk_end = htx_get_blk(htx, idx);
4227 break;
4228 }
4229 }
4230
4231 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004232 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004233 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004234 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004235 if (h2s->status < 100 || h2s->status > 999)
4236 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004237
4238 /* and the rest of the headers, that we dump starting at header 0 */
4239 hdr = 0;
4240
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004241 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004242 while ((idx = htx_get_next(htx, idx)) != -1) {
4243 blk = htx_get_blk(htx, idx);
4244 type = htx_get_blk_type(blk);
4245
4246 if (type == HTX_BLK_UNUSED)
4247 continue;
4248
4249 if (type != HTX_BLK_HDR)
4250 break;
4251
4252 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4253 goto fail;
4254
4255 list[hdr].n = htx_get_blk_name(htx, blk);
4256 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004257 hdr++;
4258 }
4259
4260 /* marker for end of headers */
4261 list[hdr].n = ist("");
4262
4263 if (h2s->status == 204 || h2s->status == 304) {
4264 /* no contents, claim c-len is present and set to zero */
4265 es_now = 1;
4266 }
4267
4268 chunk_reset(&outbuf);
4269
4270 while (1) {
4271 outbuf.area = b_tail(&h2c->mbuf);
4272 outbuf.size = b_contig_space(&h2c->mbuf);
4273 outbuf.data = 0;
4274
4275 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4276 break;
4277 realign_again:
4278 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4279 }
4280
4281 if (outbuf.size < 9)
4282 goto full;
4283
4284 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4285 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4286 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4287 outbuf.data = 9;
4288
4289 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004290 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004291 if (b_space_wraps(&h2c->mbuf))
4292 goto realign_again;
4293 goto full;
4294 }
4295
4296 /* encode all headers, stop at empty name */
4297 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4298 /* these ones do not exist in H2 and must be dropped. */
4299 if (isteq(list[hdr].n, ist("connection")) ||
4300 isteq(list[hdr].n, ist("proxy-connection")) ||
4301 isteq(list[hdr].n, ist("keep-alive")) ||
4302 isteq(list[hdr].n, ist("upgrade")) ||
4303 isteq(list[hdr].n, ist("transfer-encoding")))
4304 continue;
4305
4306 if (isteq(list[hdr].n, ist("")))
4307 break; // end
4308
4309 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4310 /* output full */
4311 if (b_space_wraps(&h2c->mbuf))
4312 goto realign_again;
4313 goto full;
4314 }
4315 }
4316
Christopher Faulet0b465482019-02-19 15:14:23 +01004317 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004318 * FIXME: we should also set it when we know for sure that the
4319 * content-length is zero as well as on 204/304
4320 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004321 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4322 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004323 es_now = 1;
4324
Willy Tarreau927b88b2019-03-04 08:03:25 +01004325 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004326 es_now = 1;
4327
4328 /* update the frame's size */
4329 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4330
4331 if (es_now)
4332 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4333
4334 /* commit the H2 response */
4335 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004336
4337 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4338 * 1xx responses, another HEADERS frame is expected.
4339 */
4340 if (h2s->status >= 200 || h2s->status == 101)
4341 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004342
Willy Tarreau115e83b2018-12-01 19:17:53 +01004343 if (es_now) {
4344 h2s->flags |= H2_SF_ES_SENT;
4345 if (h2s->st == H2_SS_OPEN)
4346 h2s->st = H2_SS_HLOC;
4347 else
4348 h2s_close(h2s);
4349 }
4350
4351 /* OK we could properly deliver the response */
4352
4353 /* remove all header blocks including the EOH and compute the
4354 * corresponding size.
4355 *
4356 * FIXME: We should remove everything when es_now is set.
4357 */
4358 ret = 0;
4359 idx = htx_get_head(htx);
4360 blk = htx_get_blk(htx, idx);
4361 while (blk != blk_end) {
4362 ret += htx_get_blksz(blk);
4363 blk = htx_remove_blk(htx, blk);
4364 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004365
4366 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4367 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004368 end:
4369 return ret;
4370 full:
4371 h2c->flags |= H2_CF_MUX_MFULL;
4372 h2s->flags |= H2_SF_BLK_MROOM;
4373 ret = 0;
4374 goto end;
4375 fail:
4376 /* unparsable HTX messages, too large ones to be produced in the local
4377 * list etc go here (unrecoverable errors).
4378 */
4379 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4380 ret = 0;
4381 goto end;
4382}
4383
Willy Tarreau80739692018-10-05 11:35:57 +02004384/* Try to send a HEADERS frame matching HTX request present in HTX message
4385 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4386 * must check the stream's status to detect any error which might have happened
4387 * subsequently to a successful send. The htx blocks are automatically removed
4388 * from the message. The htx message is assumed to be valid since produced from
4389 * the internal code, hence it contains a start line, an optional series of
4390 * header blocks and an end of header, otherwise an invalid frame could be
4391 * emitted and the resulting htx message could be left in an inconsistent state.
4392 */
4393static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4394{
4395 struct http_hdr list[MAX_HTTP_HDR];
4396 struct h2c *h2c = h2s->h2c;
4397 struct htx_blk *blk;
4398 struct htx_blk *blk_end;
4399 struct buffer outbuf;
4400 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004401 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004402 enum htx_blk_type type;
4403 int es_now = 0;
4404 int ret = 0;
4405 int hdr;
4406 int idx;
4407
4408 if (h2c_mux_busy(h2c, h2s)) {
4409 h2s->flags |= H2_SF_BLK_MBUSY;
4410 return 0;
4411 }
4412
4413 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4414 h2c->flags |= H2_CF_MUX_MALLOC;
4415 h2s->flags |= H2_SF_BLK_MROOM;
4416 return 0;
4417 }
4418
4419 /* determine the first block which must not be deleted, blk_end may
4420 * be NULL if all blocks have to be deleted.
4421 */
4422 idx = htx_get_head(htx);
4423 blk_end = NULL;
4424 while (idx != -1) {
4425 type = htx_get_blk_type(htx_get_blk(htx, idx));
4426 idx = htx_get_next(htx, idx);
4427 if (type == HTX_BLK_EOH) {
4428 if (idx != -1)
4429 blk_end = htx_get_blk(htx, idx);
4430 break;
4431 }
4432 }
4433
4434 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004435 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004436 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004437 meth = htx_sl_req_meth(sl);
4438 path = htx_sl_req_uri(sl);
4439
4440 /* and the rest of the headers, that we dump starting at header 0 */
4441 hdr = 0;
4442
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004443 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004444 while ((idx = htx_get_next(htx, idx)) != -1) {
4445 blk = htx_get_blk(htx, idx);
4446 type = htx_get_blk_type(blk);
4447
4448 if (type == HTX_BLK_UNUSED)
4449 continue;
4450
4451 if (type != HTX_BLK_HDR)
4452 break;
4453
4454 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4455 goto fail;
4456
4457 list[hdr].n = htx_get_blk_name(htx, blk);
4458 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004459 hdr++;
4460 }
4461
4462 /* marker for end of headers */
4463 list[hdr].n = ist("");
4464
4465 chunk_reset(&outbuf);
4466
4467 while (1) {
4468 outbuf.area = b_tail(&h2c->mbuf);
4469 outbuf.size = b_contig_space(&h2c->mbuf);
4470 outbuf.data = 0;
4471
4472 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4473 break;
4474 realign_again:
4475 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4476 }
4477
4478 if (outbuf.size < 9)
4479 goto full;
4480
4481 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4482 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4483 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4484 outbuf.data = 9;
4485
4486 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004487 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004488 if (b_space_wraps(&h2c->mbuf))
4489 goto realign_again;
4490 goto full;
4491 }
4492
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004493 /* RFC7540 #8.3: the CONNECT method must have :
4494 * - :authority set to the URI part (host:port)
4495 * - :method set to CONNECT
4496 * - :scheme and :path omitted
4497 */
4498 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4499 /* encode the scheme which is always "https" (or 0x86 for "http") */
4500 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4501 /* output full */
4502 if (b_space_wraps(&h2c->mbuf))
4503 goto realign_again;
4504 goto full;
4505 }
Willy Tarreau80739692018-10-05 11:35:57 +02004506
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004507 /* encode the path, which necessarily is the second one */
4508 if (!hpack_encode_path(&outbuf, path)) {
4509 /* output full */
4510 if (b_space_wraps(&h2c->mbuf))
4511 goto realign_again;
4512 goto full;
4513 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004514
4515 /* look for the Host header and place it in :authority */
4516 auth = ist2(NULL, 0);
4517 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4518 if (isteq(list[hdr].n, ist("")))
4519 break; // end
4520
4521 if (isteq(list[hdr].n, ist("host"))) {
4522 auth = list[hdr].v;
4523 break;
4524 }
4525 }
4526 }
4527 else {
4528 /* for CONNECT, :authority is taken from the path */
4529 auth = path;
4530 }
4531
4532 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4533 /* output full */
4534 if (b_space_wraps(&h2c->mbuf))
4535 goto realign_again;
4536 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004537 }
4538
4539 /* encode all headers, stop at empty name */
4540 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4541 /* these ones do not exist in H2 and must be dropped. */
4542 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004543 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004544 isteq(list[hdr].n, ist("proxy-connection")) ||
4545 isteq(list[hdr].n, ist("keep-alive")) ||
4546 isteq(list[hdr].n, ist("upgrade")) ||
4547 isteq(list[hdr].n, ist("transfer-encoding")))
4548 continue;
4549
4550 if (isteq(list[hdr].n, ist("")))
4551 break; // end
4552
4553 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4554 /* output full */
4555 if (b_space_wraps(&h2c->mbuf))
4556 goto realign_again;
4557 goto full;
4558 }
4559 }
4560
4561 /* we may need to add END_STREAM if we have no body :
4562 * - request already closed, or :
4563 * - no transfer-encoding, and :
4564 * - no content-length or content-length:0
4565 * Fixme: this doesn't take into account CONNECT requests.
4566 */
4567 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4568 es_now = 1;
4569
4570 if (sl->flags & HTX_SL_F_BODYLESS)
4571 es_now = 1;
4572
Willy Tarreau927b88b2019-03-04 08:03:25 +01004573 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004574 es_now = 1;
4575
4576 /* update the frame's size */
4577 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4578
4579 if (es_now)
4580 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4581
4582 /* commit the H2 response */
4583 b_add(&h2c->mbuf, outbuf.data);
4584 h2s->flags |= H2_SF_HEADERS_SENT;
4585 h2s->st = H2_SS_OPEN;
4586
Willy Tarreau80739692018-10-05 11:35:57 +02004587 if (es_now) {
4588 // trim any possibly pending data (eg: inconsistent content-length)
4589 h2s->flags |= H2_SF_ES_SENT;
4590 h2s->st = H2_SS_HLOC;
4591 }
4592
4593 /* remove all header blocks including the EOH and compute the
4594 * corresponding size.
4595 *
4596 * FIXME: We should remove everything when es_now is set.
4597 */
4598 ret = 0;
4599 idx = htx_get_head(htx);
4600 blk = htx_get_blk(htx, idx);
4601 while (blk != blk_end) {
4602 ret += htx_get_blksz(blk);
4603 blk = htx_remove_blk(htx, blk);
4604 }
4605
4606 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4607 htx_remove_blk(htx, blk_end);
4608
4609 end:
4610 return ret;
4611 full:
4612 h2c->flags |= H2_CF_MUX_MFULL;
4613 h2s->flags |= H2_SF_BLK_MROOM;
4614 ret = 0;
4615 goto end;
4616 fail:
4617 /* unparsable HTX messages, too large ones to be produced in the local
4618 * list etc go here (unrecoverable errors).
4619 */
4620 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4621 ret = 0;
4622 goto end;
4623}
4624
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004625/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004626 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4627 * caller must check the stream's status to detect any error which might have
4628 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004629 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4630 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004631static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004632{
4633 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004634 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004635 struct buffer outbuf;
4636 size_t total = 0;
4637 int es_now = 0;
4638 int bsize; /* htx block size */
4639 int fsize; /* h2 frame size */
4640 struct htx_blk *blk;
4641 enum htx_blk_type type;
4642 int idx;
4643
4644 if (h2c_mux_busy(h2c, h2s)) {
4645 h2s->flags |= H2_SF_BLK_MBUSY;
4646 goto end;
4647 }
4648
4649 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4650 h2c->flags |= H2_CF_MUX_MALLOC;
4651 h2s->flags |= H2_SF_BLK_MROOM;
4652 goto end;
4653 }
4654
Willy Tarreau98de12a2018-12-12 07:03:00 +01004655 htx = htx_from_buf(buf);
4656
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004657 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4658 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4659 * the caller to handle.
4660 */
4661
4662 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004663 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004664 goto end;
4665
4666 idx = htx_get_head(htx);
4667 blk = htx_get_blk(htx, idx);
4668 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4669 bsize = htx_get_blksz(blk);
4670 fsize = bsize;
4671
4672 if (type == HTX_BLK_EOD) {
4673 /* if we have an EOD, we're dealing with chunked data. We may
4674 * have a set of trailers after us that the caller will want to
4675 * deal with. Let's simply remove the EOD and return.
4676 */
4677 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004678 total++; // EOD counts as one byte
4679 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004680 goto end;
4681 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004682 else if (type == HTX_BLK_EOM) {
4683 if (h2s->flags & H2_SF_ES_SENT) {
4684 /* ES already sent */
4685 htx_remove_blk(htx, blk);
4686 total++; // EOM counts as one byte
4687 count--;
4688 goto end;
4689 }
4690 }
4691 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004692 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004693
4694 /* Perform some optimizations to reduce the number of buffer copies.
4695 * First, if the mux's buffer is empty and the htx area contains
4696 * exactly one data block of the same size as the requested count, and
4697 * this count fits within the frame size, the stream's window size, and
4698 * the connection's window size, then it's possible to simply swap the
4699 * caller's buffer with the mux's output buffer and adjust offsets and
4700 * length to match the entire DATA HTX block in the middle. In this
4701 * case we perform a true zero-copy operation from end-to-end. This is
4702 * the situation that happens all the time with large files. Second, if
4703 * this is not possible, but the mux's output buffer is empty, we still
4704 * have an opportunity to avoid the copy to the intermediary buffer, by
4705 * making the intermediary buffer's area point to the output buffer's
4706 * area. In this case we want to skip the HTX header to make sure that
4707 * copies remain aligned and that this operation remains possible all
4708 * the time. This goes for headers, data blocks and any data extracted
4709 * from the HTX blocks.
4710 */
4711 if (unlikely(fsize == count &&
4712 htx->used == 1 && type == HTX_BLK_DATA &&
4713 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4714 void *old_area = h2c->mbuf.area;
4715
4716 if (b_data(&h2c->mbuf)) {
4717 /* too bad there are data left there. If we have less
4718 * than 1/4 of the mbuf's size and everything fits,
4719 * we'll perform a copy anyway. Otherwise we'll pretend
4720 * the mbuf is full and wait.
4721 */
4722 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4723 goto copy;
4724 h2c->flags |= H2_CF_MUX_MFULL;
4725 h2s->flags |= H2_SF_BLK_MROOM;
4726 goto end;
4727 }
4728
4729 /* map an H2 frame to the HTX block so that we can put the
4730 * frame header there.
4731 */
4732 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004733 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004734 h2c->mbuf.data = fsize + 9;
4735 outbuf.area = b_head(&h2c->mbuf);
4736
4737 /* prepend an H2 DATA frame header just before the DATA block */
4738 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4739 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4740 h2_set_frame_size(outbuf.area, fsize);
4741
4742 /* update windows */
4743 h2s->mws -= fsize;
4744 h2c->mws -= fsize;
4745
4746 /* and exchange with our old area */
4747 buf->area = old_area;
4748 buf->data = buf->head = 0;
4749 total += fsize;
4750 goto end;
4751 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004752
Willy Tarreau98de12a2018-12-12 07:03:00 +01004753 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004754 /* for DATA and EOM we'll have to emit a frame, even if empty */
4755
4756 while (1) {
4757 outbuf.area = b_tail(&h2c->mbuf);
4758 outbuf.size = b_contig_space(&h2c->mbuf);
4759 outbuf.data = 0;
4760
4761 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4762 break;
4763 realign_again:
4764 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4765 }
4766
4767 if (outbuf.size < 9) {
4768 h2c->flags |= H2_CF_MUX_MFULL;
4769 h2s->flags |= H2_SF_BLK_MROOM;
4770 goto end;
4771 }
4772
4773 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4774 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4775 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4776 outbuf.data = 9;
4777
4778 /* we have in <fsize> the exact number of bytes we need to copy from
4779 * the HTX buffer. We need to check this against the connection's and
4780 * the stream's send windows, and to ensure that this fits in the max
4781 * frame size and in the buffer's available space minus 9 bytes (for
4782 * the frame header). The connection's flow control is applied last so
4783 * that we can use a separate list of streams which are immediately
4784 * unblocked on window opening. Note: we don't implement padding.
4785 */
4786
4787 /* EOM is presented with bsize==1 but would lead to the emission of an
4788 * empty frame, thus we force it to zero here.
4789 */
4790 if (type == HTX_BLK_EOM)
4791 bsize = fsize = 0;
4792
4793 if (!fsize)
4794 goto send_empty;
4795
4796 if (h2s->mws <= 0) {
4797 h2s->flags |= H2_SF_BLK_SFCTL;
4798 if (h2s->send_wait) {
4799 LIST_DEL(&h2s->list);
4800 LIST_INIT(&h2s->list);
4801 }
4802 goto end;
4803 }
4804
Willy Tarreauee573762018-12-04 15:25:57 +01004805 if (fsize > count)
4806 fsize = count;
4807
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004808 if (fsize > h2s->mws)
4809 fsize = h2s->mws; // >0
4810
4811 if (h2c->mfs && fsize > h2c->mfs)
4812 fsize = h2c->mfs; // >0
4813
4814 if (fsize + 9 > outbuf.size) {
4815 /* we have an opportunity for enlarging the too small
4816 * available space, let's try.
4817 * FIXME: is this really interesting to do? Maybe we'll
4818 * spend lots of time realigning instead of using two
4819 * frames.
4820 */
4821 if (b_space_wraps(&h2c->mbuf))
4822 goto realign_again;
4823 fsize = outbuf.size - 9;
4824
4825 if (fsize <= 0) {
4826 /* no need to send an empty frame here */
4827 h2c->flags |= H2_CF_MUX_MFULL;
4828 h2s->flags |= H2_SF_BLK_MROOM;
4829 goto end;
4830 }
4831 }
4832
4833 if (h2c->mws <= 0) {
4834 h2s->flags |= H2_SF_BLK_MFCTL;
4835 goto end;
4836 }
4837
4838 if (fsize > h2c->mws)
4839 fsize = h2c->mws;
4840
4841 /* now let's copy this this into the output buffer */
4842 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004843 h2s->mws -= fsize;
4844 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004845 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004846
4847 send_empty:
4848 /* update the frame's size */
4849 h2_set_frame_size(outbuf.area, fsize);
4850
4851 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4852 * meeting EOM. We should optimize this later.
4853 */
4854 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004855 total++; // EOM counts as one byte
4856 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004857 es_now = 1;
4858 }
4859
4860 if (es_now)
4861 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4862
4863 /* commit the H2 response */
4864 b_add(&h2c->mbuf, fsize + 9);
4865
4866 /* consume incoming HTX block, including EOM */
4867 total += fsize;
4868 if (fsize == bsize) {
4869 htx_remove_blk(htx, blk);
4870 if (fsize)
4871 goto new_frame;
4872 } else {
4873 /* we've truncated this block */
4874 htx_cut_data_blk(htx, blk, fsize);
4875 }
4876
4877 if (es_now) {
4878 if (h2s->st == H2_SS_OPEN)
4879 h2s->st = H2_SS_HLOC;
4880 else
4881 h2s_close(h2s);
4882
4883 h2s->flags |= H2_SF_ES_SENT;
4884 }
4885
4886 end:
4887 return total;
4888}
4889
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004890/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4891 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4892 * processed. The caller must check the stream's status to detect any error
4893 * which might have happened subsequently to a successful send. The htx blocks
4894 * are automatically removed from the message. The htx message is assumed to be
4895 * valid since produced from the internal code. Processing stops when meeting
4896 * the EOM, which is also removed. All trailers are processed at once and sent
4897 * as a single frame. The ES flag is always set.
4898 */
4899static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4900{
4901 struct http_hdr list[MAX_HTTP_HDR];
4902 struct h2c *h2c = h2s->h2c;
4903 struct htx_blk *blk;
4904 struct htx_blk *blk_end;
4905 struct buffer outbuf;
4906 struct h1m h1m;
4907 enum htx_blk_type type;
4908 uint32_t size;
4909 int ret = 0;
4910 int hdr;
4911 int idx;
4912 void *start;
4913
4914 if (h2c_mux_busy(h2c, h2s)) {
4915 h2s->flags |= H2_SF_BLK_MBUSY;
4916 goto end;
4917 }
4918
4919 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4920 h2c->flags |= H2_CF_MUX_MALLOC;
4921 h2s->flags |= H2_SF_BLK_MROOM;
4922 goto end;
4923 }
4924
4925 /* The principle is that we parse each and every trailers block using
4926 * the H1 headers parser, and append it to the list. We don't proceed
4927 * until EOM is met. blk_end will point to the EOM block.
4928 */
4929 hdr = 0;
4930 memset(list, 0, sizeof(list));
4931 blk_end = NULL;
4932
4933 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4934 blk = htx_get_blk(htx, idx);
4935 type = htx_get_blk_type(blk);
4936
4937 if (type == HTX_BLK_UNUSED)
4938 continue;
4939
4940 if (type != HTX_BLK_TLR) {
4941 if (type == HTX_BLK_EOM)
4942 blk_end = blk;
4943 break;
4944 }
4945
4946 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4947 goto fail;
4948
4949 size = htx_get_blksz(blk);
4950 start = htx_get_blk_ptr(htx, blk);
4951
4952 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4953 h1m.err_pos = 0;
4954 ret = h1_headers_to_hdr_list(start, start + size,
4955 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4956 &h1m, NULL);
4957 if (ret < 0)
4958 goto fail;
4959
4960 /* ret == 0 if an incomplete trailers block was found (missing
4961 * empty line), or > 0 if it was found. We have to continue on
4962 * incomplete messages because the trailers block might be
4963 * incomplete.
4964 */
4965
4966 /* search the new end */
4967 while (hdr <= sizeof(list)/sizeof(list[0])) {
4968 if (!list[hdr].n.len)
4969 break;
4970 hdr++;
4971 }
4972 }
4973
4974 if (!blk_end)
4975 goto end; // end not found yet
4976
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004977 chunk_reset(&outbuf);
4978
4979 while (1) {
4980 outbuf.area = b_tail(&h2c->mbuf);
4981 outbuf.size = b_contig_space(&h2c->mbuf);
4982 outbuf.data = 0;
4983
4984 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4985 break;
4986 realign_again:
4987 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4988 }
4989
4990 if (outbuf.size < 9)
4991 goto full;
4992
4993 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4994 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4995 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4996 outbuf.data = 9;
4997
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004998 /* encode all headers */
4999 for (idx = 0; idx < hdr; idx++) {
5000 /* these ones do not exist in H2 or must not appear in
5001 * trailers and must be dropped.
5002 */
5003 if (isteq(list[idx].n, ist("host")) ||
5004 isteq(list[idx].n, ist("content-length")) ||
5005 isteq(list[idx].n, ist("connection")) ||
5006 isteq(list[idx].n, ist("proxy-connection")) ||
5007 isteq(list[idx].n, ist("keep-alive")) ||
5008 isteq(list[idx].n, ist("upgrade")) ||
5009 isteq(list[idx].n, ist("te")) ||
5010 isteq(list[idx].n, ist("transfer-encoding")))
5011 continue;
5012
5013 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5014 /* output full */
5015 if (b_space_wraps(&h2c->mbuf))
5016 goto realign_again;
5017 goto full;
5018 }
5019 }
5020
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005021 if (!hdr) {
5022 /* here we have a problem, we've received an empty trailers
5023 * block followed by an EOM. Because of this we can't send a
5024 * HEADERS frame, so we have to cheat and instead send an empty
5025 * DATA frame conveying the ES flag.
5026 */
5027 outbuf.area[3] = H2_FT_DATA;
5028 outbuf.area[4] = H2_F_DATA_END_STREAM;
5029 }
5030
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005031 /* update the frame's size */
5032 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5033
5034 /* commit the H2 response */
5035 b_add(&h2c->mbuf, outbuf.data);
5036 h2s->flags |= H2_SF_ES_SENT;
5037
5038 if (h2s->st == H2_SS_OPEN)
5039 h2s->st = H2_SS_HLOC;
5040 else
5041 h2s_close(h2s);
5042
5043 /* OK we could properly deliver the response */
5044 done:
5045 /* remove all header blocks including EOM and compute the corresponding size. */
5046 ret = 0;
5047 idx = htx_get_head(htx);
5048 blk = htx_get_blk(htx, idx);
5049 while (blk != blk_end) {
5050 ret += htx_get_blksz(blk);
5051 blk = htx_remove_blk(htx, blk);
5052 }
5053 blk = htx_remove_blk(htx, blk);
5054 end:
5055 return ret;
5056 full:
5057 h2c->flags |= H2_CF_MUX_MFULL;
5058 h2s->flags |= H2_SF_BLK_MROOM;
5059 ret = 0;
5060 goto end;
5061 fail:
5062 /* unparsable HTX messages, too large ones to be produced in the local
5063 * list etc go here (unrecoverable errors).
5064 */
5065 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5066 ret = 0;
5067 goto end;
5068}
5069
Olivier Houchard6ff20392018-07-17 18:46:31 +02005070/* Called from the upper layer, to subscribe to events, such as being able to send */
5071static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5072{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005073 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005074 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005075 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005076
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005077 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005078 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005079 if (!(sw->events & SUB_RETRY_RECV)) {
5080 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005081 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005082 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005083 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005084 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005085 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005086 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005087 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005088 if (!(sw->events & SUB_RETRY_SEND)) {
5089 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005090 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005091 h2s->send_wait = sw;
5092 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5093 if (h2s->flags & H2_SF_BLK_MFCTL)
5094 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5095 else
5096 LIST_ADDQ(&h2c->send_list, &h2s->list);
5097 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005098 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005099 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005100 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005101 if (event_type != 0)
5102 return -1;
5103 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005104
5105
5106}
5107
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005108static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5109{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005110 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005111 struct h2s *h2s = cs->ctx;
5112
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005113 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005114 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005115 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005116 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005117 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005118 }
5119 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005120 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005121 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005122 if (h2s->send_wait == sw) {
5123 LIST_DEL(&h2s->list);
5124 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005125 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005126 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005127 }
5128 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005129 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5130 sw = param;
5131 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005132 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005133 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005134 LIST_DEL(&h2s->list);
5135 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005136 }
5137 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005138 return 0;
5139}
5140
5141
Olivier Houchard511efea2018-08-16 15:30:32 +02005142/* Called from the upper layer, to receive data */
5143static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5144{
Olivier Houchard638b7992018-08-16 15:41:52 +02005145 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005146 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005147 struct htx *h2s_htx = NULL;
5148 struct htx *buf_htx = NULL;
5149 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005150 size_t ret = 0;
5151
5152 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005153 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5154 /* in HTX mode we ignore the count argument */
5155 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005156 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005157 /* Here htx_to_buf() will set buffer data to 0 because
5158 * the HTX is empty.
5159 */
5160 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005161 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005162 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005163
5164 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005165 count = htx_free_data_space(buf_htx);
5166 if (flags & CO_RFL_KEEP_RSV) {
5167 if (count <= global.tune.maxrewrite)
5168 goto end;
5169 count -= global.tune.maxrewrite;
5170 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005171
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005172 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005173
5174 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5175 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5176
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005177 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005178 htx_to_buf(buf_htx, buf);
5179 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005180 ret = htx_ret.ret;
5181 }
5182 else {
5183 ret = b_xfer(buf, &h2s->rxbuf, count);
5184 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005185
Christopher Faulet37070b22019-02-14 15:12:14 +01005186 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005187 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005188 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005189 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005190 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005191 if (cs->flags & CS_FL_REOS)
5192 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005193 if (cs->flags & CS_FL_ERR_PENDING)
5194 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005195 if (b_size(&h2s->rxbuf)) {
5196 b_free(&h2s->rxbuf);
5197 offer_buffers(NULL, tasks_run_queue);
5198 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005199 }
5200
Willy Tarreau082f5592018-11-25 08:03:32 +01005201 if (ret && h2c->dsi == h2s->id) {
5202 /* demux is blocking on this stream's buffer */
5203 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005204 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005205 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005206
Olivier Houchard511efea2018-08-16 15:30:32 +02005207 return ret;
5208}
5209
Olivier Houchardd846c262018-10-19 17:24:29 +02005210static void h2_stop_senders(struct h2c *h2c)
5211{
5212 struct h2s *h2s, *h2s_back;
5213
5214 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5215 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5216 if (h2c->msi == h2s_id(h2s))
5217 continue;
5218 LIST_DEL(&h2s->list);
5219 LIST_INIT(&h2s->list);
5220 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005221 h2s->send_wait->events |= SUB_RETRY_SEND;
5222 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005223 LIST_ADD(&h2c->send_list, &h2s->list);
5224 }
5225}
5226
Willy Tarreau62f52692017-10-08 23:01:42 +02005227/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005228static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005229{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005230 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005231 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005232 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005233 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005234 struct htx *htx;
5235 struct htx_blk *blk;
5236 enum htx_blk_type btype;
5237 uint32_t bsize;
5238 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005239
Olivier Houchardd846c262018-10-19 17:24:29 +02005240 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005241 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005242 h2s->send_wait = NULL;
5243 LIST_DEL(&h2s->list);
5244 LIST_INIT(&h2s->list);
5245 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005246 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5247 return 0;
5248
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005249 /* htx will be enough to decide if we're using HTX or legacy */
5250 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5251
Willy Tarreau0bad0432018-06-14 16:54:01 +02005252 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005253 h2s->flags |= H2_SF_OUTGOING_DATA;
5254
Willy Tarreau751f2d02018-10-05 09:35:00 +02005255 if (h2s->id == 0) {
5256 int32_t id = h2c_get_next_sid(h2s->h2c);
5257
5258 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005259 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005260 return 0;
5261 }
5262
5263 eb32_delete(&h2s->by_id);
5264 h2s->by_id.key = h2s->id = id;
5265 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005266 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005267 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5268 }
5269
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005270 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005271 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5272 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005273 idx = htx_get_head(htx);
5274 blk = htx_get_blk(htx, idx);
5275 btype = htx_get_blk_type(blk);
5276 bsize = htx_get_blksz(blk);
5277
5278 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005279 case HTX_BLK_REQ_SL:
5280 /* start-line before headers */
5281 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5282 if (ret > 0) {
5283 total += ret;
5284 count -= ret;
5285 if (ret < bsize)
5286 goto done;
5287 }
5288 break;
5289
Willy Tarreau115e83b2018-12-01 19:17:53 +01005290 case HTX_BLK_RES_SL:
5291 /* start-line before headers */
5292 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5293 if (ret > 0) {
5294 total += ret;
5295 count -= ret;
5296 if (ret < bsize)
5297 goto done;
5298 }
5299 break;
5300
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005301 case HTX_BLK_DATA:
5302 case HTX_BLK_EOD:
5303 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005304 /* all these cause the emission of a DATA frame (possibly empty).
5305 * This EOM necessarily is one before trailers, as the EOM following
5306 * trailers would have been consumed by the trailers parser.
5307 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005308 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005309 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005310 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005311 total += ret;
5312 count -= ret;
5313 if (ret < bsize)
5314 goto done;
5315 }
5316 break;
5317
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005318 case HTX_BLK_TLR:
5319 /* This is the first trailers block, all the subsequent ones AND
5320 * the EOM will be swallowed by the parser.
5321 */
5322 ret = h2s_htx_make_trailers(h2s, htx);
5323 if (ret > 0) {
5324 total += ret;
5325 count -= ret;
5326 if (ret < bsize)
5327 goto done;
5328 }
5329 break;
5330
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005331 default:
5332 htx_remove_blk(htx, blk);
5333 total += bsize;
5334 count -= bsize;
5335 break;
5336 }
5337 }
5338 goto done;
5339 }
5340
5341 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005342 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005343 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005344 if (h2s->h2c->flags & H2_CF_IS_BACK)
5345 ret = -1;
5346 else
5347 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005348 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005349 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005350 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005351 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005352 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005353 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005354 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005355
Willy Tarreau5dd17352018-06-14 13:33:30 +02005356 if (unlikely((int)ret <= 0)) {
5357 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005358 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5359 break;
5360 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005361 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005362 total += count;
5363 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005364 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005365 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005366 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005367 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005368 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005369 break;
5370 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005371
5372 total += ret;
5373 count -= ret;
5374
5375 if (h2s->st >= H2_SS_ERROR)
5376 break;
5377
5378 if (h2s->flags & H2_SF_BLK_ANY)
5379 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005380 }
5381
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005382 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005383 if (h2s->st >= H2_SS_ERROR) {
5384 /* trim any possibly pending data after we close (extra CR-LF,
5385 * unprocessed trailers, abnormal extra data, ...)
5386 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005387 total += count;
5388 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005389 }
5390
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005391 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005392 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005393 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005394 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005395 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005396 }
5397
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005398 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005399 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005400 } else {
5401 b_del(buf, total);
5402 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005403
5404 /* The mux is full, cancel the pending tasks */
5405 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5406 (h2s->flags & H2_SF_BLK_MBUSY))
5407 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005408
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005409 /* If we're running HTX, and we read the whole buffer, then pretend
5410 * we read exactly what the caller specified, as with HTX the caller
5411 * will always give the buffer size, instead of the amount of data
5412 * available.
5413 */
5414 if (htx && !b_data(buf))
5415 total = orig_count;
5416
Olivier Houchard7505f942018-08-21 18:10:44 +02005417 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005418 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005419 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005420
Olivier Houchard7505f942018-08-21 18:10:44 +02005421 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005422 /* If we're waiting for flow control, and we got a shutr on the
5423 * connection, we will never be unlocked, so add an error on
5424 * the conn_stream.
5425 */
5426 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5427 !b_data(&h2s->h2c->dbuf) &&
5428 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5429 if (cs->flags & CS_FL_EOS)
5430 cs->flags |= CS_FL_ERROR;
5431 else
5432 cs->flags |= CS_FL_ERR_PENDING;
5433 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005434 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005435}
5436
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005437/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005438static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005439{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005440 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005441 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005442 struct eb32_node *node;
5443 int fctl_cnt = 0;
5444 int send_cnt = 0;
5445 int tree_cnt = 0;
5446 int orph_cnt = 0;
5447
5448 if (!h2c)
5449 return;
5450
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005451 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005452 fctl_cnt++;
5453
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005454 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005455 send_cnt++;
5456
Willy Tarreau3af37712018-12-18 14:34:41 +01005457 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005458 node = eb32_first(&h2c->streams_by_id);
5459 while (node) {
5460 h2s = container_of(node, struct h2s, by_id);
5461 tree_cnt++;
5462 if (!h2s->cs)
5463 orph_cnt++;
5464 node = eb32_next(node);
5465 }
5466
Willy Tarreau987c0632018-12-18 10:32:05 +01005467 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5468 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5469 " .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 +02005470 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5471 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005472 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005473 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5474 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5475 h2c->msi,
5476 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5477 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5478
5479 if (h2s) {
5480 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5481 h2s, h2s->id, h2s->flags,
5482 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5483 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5484 h2s->cs);
5485 if (h2s->cs)
5486 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5487 h2s->cs->flags, h2s->cs->data);
5488 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005489}
Willy Tarreau62f52692017-10-08 23:01:42 +02005490
5491/*******************************************************/
5492/* functions below are dedicated to the config parsers */
5493/*******************************************************/
5494
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005495/* config parser for global "tune.h2.header-table-size" */
5496static int h2_parse_header_table_size(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_header_table_size = atoi(args[1]);
5504 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5505 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5506 return -1;
5507 }
5508 return 0;
5509}
Willy Tarreau62f52692017-10-08 23:01:42 +02005510
Willy Tarreaue6baec02017-07-27 11:45:11 +02005511/* config parser for global "tune.h2.initial-window-size" */
5512static int h2_parse_initial_window_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_initial_window_size = atoi(args[1]);
5520 if (h2_settings_initial_window_size < 0) {
5521 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5522 return -1;
5523 }
5524 return 0;
5525}
5526
Willy Tarreau5242ef82017-07-27 11:47:28 +02005527/* config parser for global "tune.h2.max-concurrent-streams" */
5528static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5529 struct proxy *defpx, const char *file, int line,
5530 char **err)
5531{
5532 if (too_many_args(1, args, err, NULL))
5533 return -1;
5534
5535 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005536 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005537 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5538 return -1;
5539 }
5540 return 0;
5541}
5542
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005543/* config parser for global "tune.h2.max-frame-size" */
5544static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5545 struct proxy *defpx, const char *file, int line,
5546 char **err)
5547{
5548 if (too_many_args(1, args, err, NULL))
5549 return -1;
5550
5551 h2_settings_max_frame_size = atoi(args[1]);
5552 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5553 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5554 return -1;
5555 }
5556 return 0;
5557}
5558
Willy Tarreau62f52692017-10-08 23:01:42 +02005559
5560/****************************************/
5561/* MUX initialization and instanciation */
5562/***************************************/
5563
5564/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005565static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005566 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005567 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005568 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005569 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005570 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005571 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005572 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005573 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005574 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005575 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005576 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005577 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005578 .shutr = h2_shutr,
5579 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005580 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005581 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005582 .name = "H2",
5583};
5584
Christopher Faulet32f61c02018-04-10 14:33:41 +02005585/* PROTO selection : this mux registers PROTO token "h2" */
5586static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005587 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005588
Willy Tarreau0108d902018-11-25 19:14:37 +01005589INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5590
Willy Tarreauf8957272018-10-03 10:25:20 +02005591static struct mux_proto_list mux_proto_h2_htx =
5592 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5593
5594INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5595
Willy Tarreau62f52692017-10-08 23:01:42 +02005596/* config keyword parsers */
5597static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005598 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005599 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005600 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005601 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005602 { 0, NULL, NULL }
5603}};
5604
Willy Tarreau0108d902018-11-25 19:14:37 +01005605INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);