blob: 6d13b1011aebdc12395e40f3273a0fc270f3aa2f [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))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001426 flags |= CS_FL_REOS;
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 }
3090
Willy Tarreau90c32322017-11-24 08:00:30 +01003091 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003092 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003093 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003094
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003095 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003096 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003097 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003098
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003099 return;
3100add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003101 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003102 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003103 if (h2s->flags & H2_SF_BLK_MFCTL) {
3104 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3105 h2s->send_wait = sw;
3106 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3107 h2s->send_wait = sw;
3108 LIST_ADDQ(&h2c->send_list, &h2s->list);
3109 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003110 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003111 /* Let the handler know we want shutr */
3112 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003113}
3114
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003115static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003116{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003117 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003118 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003119
Willy Tarreau721c9742017-11-07 11:05:42 +01003120 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003121 return;
3122
Willy Tarreau67434202017-11-06 20:20:51 +01003123 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003124 /* we can cleanly close using an empty data frame only after headers */
3125
3126 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3127 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003128 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003129
3130 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003131 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003132 else
3133 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003134 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003135 /* a connstream may require us to immediately kill the whole connection
3136 * for example because of a "tcp-request content reject" rule that is
3137 * normally used to limit abuse. In this case we schedule a goaway to
3138 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003139 */
Willy Tarreau18059042019-01-31 19:12:48 +01003140 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3141 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3142 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3143 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3144 }
3145
Willy Tarreau90c32322017-11-24 08:00:30 +01003146 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003147 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003148 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003149
Willy Tarreau00dd0782018-03-01 16:31:34 +01003150 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003151 }
3152
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003153 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003154 tasklet_wakeup(h2c->wait_event.task);
3155 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003156
3157 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003158 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003159 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003160 if (h2s->flags & H2_SF_BLK_MFCTL) {
3161 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3162 h2s->send_wait = sw;
3163 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3164 h2s->send_wait = sw;
3165 LIST_ADDQ(&h2c->send_list, &h2s->list);
3166 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003167 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003168 /* let the handler know we want to shutw */
3169 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170}
3171
3172static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3173{
3174 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003175 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003176
Olivier Houchard2c68a462018-12-15 22:42:20 +01003177 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003178 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003179 h2s->send_wait = NULL;
3180 LIST_DEL(&h2s->list);
3181 LIST_INIT(&h2s->list);
3182 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003183 if (reason & 2)
3184 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003185 if (reason & 1)
3186 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003187
Olivier Houchard2c68a462018-12-15 22:42:20 +01003188 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003189 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003190 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003191 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003192}
3193
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3195{
3196 struct h2s *h2s = cs->ctx;
3197
3198 if (!mode)
3199 return;
3200
3201 h2_do_shutr(h2s);
3202}
3203
3204static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3205{
3206 struct h2s *h2s = cs->ctx;
3207
3208 h2_do_shutw(h2s);
3209}
3210
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003211/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003212 * HTX request or response depending on the connection's side. Returns a
3213 * positive value on success, a negative value on failure, or 0 if it couldn't
3214 * proceed. May report connection errors in h2c->errcode if the frame is
3215 * non-decodable and the connection unrecoverable. In absence of connection
3216 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003217 *
3218 * The function may fold CONTINUATION frames into the initial HEADERS frame
3219 * by removing padding and next frame header, then moving the CONTINUATION
3220 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3221 * leaving a hole between the main frame and the beginning of the next one.
3222 * The possibly remaining incomplete or next frame at the end may be moved
3223 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3224 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3225 *
3226 * A buffer at the beginning of processing may look like this :
3227 *
3228 * ,---.---------.-----.--------------.--------------.------.---.
3229 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3230 * `---^---------^-----^--------------^--------------^------^---'
3231 * | | <-----> | |
3232 * area | dpl | wrap
3233 * |<--------------> |
3234 * | dfl |
3235 * |<-------------------------------------------------->|
3236 * head data
3237 *
3238 * Padding is automatically overwritten when folding, participating to the
3239 * hole size after dfl :
3240 *
3241 * ,---.------------------------.-----.--------------.------.---.
3242 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3243 * `---^------------------------^-----^--------------^------^---'
3244 * | | <-----> | |
3245 * area | hole | wrap
3246 * |<-----------------------> |
3247 * | dfl |
3248 * |<-------------------------------------------------->|
3249 * head data
3250 *
3251 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3252 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3253 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003254 *
3255 * The <flags> field must point to either the stream's flags or to a copy of it
3256 * so that the function can update the following flags :
3257 * - H2_SF_DATA_CLEN when content-length is seen
3258 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3259 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003260 *
3261 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3262 * decoding, in order to detect if we're dealing with a headers or a trailers
3263 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003264 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003265static 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 +02003266{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003267 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003268 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003269 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003270 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003271 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003272 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003273 int flen; // header frame len
3274 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003275 int ret = 0;
3276 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003277 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003278 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003279
Willy Tarreauea18f862018-12-22 20:19:26 +01003280next_frame:
3281 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3282 goto leave; // incomplete input frame
3283
3284 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3285 * this case, we'll try to paste it immediately after the initial
3286 * HEADERS frame payload and kill any possible padding. The initial
3287 * frame's length will be increased to represent the concatenation
3288 * of the two frames. The next frame is read from position <tlen>
3289 * and written at position <flen> (minus padding if some is present).
3290 */
3291 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3292 struct h2_fh hdr;
3293 int clen; // CONTINUATION frame's payload length
3294
3295 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3296 /* no more data, the buffer may be full, either due to
3297 * too large a frame or because of too large a hole that
3298 * we're going to compact at the end.
3299 */
3300 goto leave;
3301 }
3302
3303 if (hdr.ft != H2_FT_CONTINUATION) {
3304 /* RFC7540#6.10: frame of unexpected type */
3305 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3306 goto fail;
3307 }
3308
3309 if (hdr.sid != h2c->dsi) {
3310 /* RFC7540#6.10: frame of different stream */
3311 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3312 goto fail;
3313 }
3314
3315 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3316 /* RFC7540#4.2: invalid frame length */
3317 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3318 goto fail;
3319 }
3320
3321 /* detect when we must stop aggragating frames */
3322 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3323
3324 /* Take as much as we can of the CONTINUATION frame's payload */
3325 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3326 if (clen > hdr.len)
3327 clen = hdr.len;
3328
3329 /* Move the frame's payload over the padding, hole and frame
3330 * header. At least one of hole or dpl is null (see diagrams
3331 * above). The hole moves after the new aggragated frame.
3332 */
3333 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3334 h2c->dfl += clen - h2c->dpl;
3335 hole += h2c->dpl + 9;
3336 h2c->dpl = 0;
3337 goto next_frame;
3338 }
3339
3340 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003341
Willy Tarreau13278b42017-10-13 19:23:14 +02003342 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003343 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003344 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003345 copy = alloc_trash_chunk();
3346 if (!copy) {
3347 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3348 goto fail;
3349 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003350 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3351 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3352 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003353 }
3354
Willy Tarreau13278b42017-10-13 19:23:14 +02003355 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3356 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003357 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003358 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3359 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003360 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003361 }
3362
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003363 if (flen < 5) {
3364 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3365 goto fail;
3366 }
3367
Willy Tarreau13278b42017-10-13 19:23:14 +02003368 hdrs += 5; // stream dep = 4, weight = 1
3369 flen -= 5;
3370 }
3371
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003372 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003373 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003374 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003375 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003376
Willy Tarreau937f7602018-02-26 15:22:17 +01003377 /* we can't retry a failed decompression operation so we must be very
3378 * careful not to take any risks. In practice the output buffer is
3379 * always empty except maybe for trailers, in which case we simply have
3380 * to wait for the upper layer to finish consuming what is available.
3381 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003382
3383 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003384 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003385 if (!htx_is_empty(htx)) {
3386 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003387 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003388 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003389 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003390 if (b_data(rxbuf)) {
3391 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003392 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003393 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003394
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003395 rxbuf->head = 0;
3396 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003397 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003398
Willy Tarreau25919232019-01-03 14:48:18 +01003399 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003400 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3401 sizeof(list)/sizeof(list[0]), tmp);
3402 if (outlen < 0) {
3403 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3404 goto fail;
3405 }
3406
Willy Tarreau25919232019-01-03 14:48:18 +01003407 /* The PACK decompressor was updated, let's update the input buffer and
3408 * the parser's state to commit these changes and allow us to later
3409 * fail solely on the stream if needed.
3410 */
3411 b_del(&h2c->dbuf, h2c->dfl + hole);
3412 h2c->dfl = hole = 0;
3413 h2c->st0 = H2_CS_FRAME_H;
3414
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003415 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003416 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003417
Willy Tarreau88d138e2019-01-02 19:38:14 +01003418 if (*flags & H2_SF_HEADERS_RCVD)
3419 goto trailers;
3420
3421 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003422 if (htx) {
3423 /* HTX mode */
3424 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003425 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003426 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003427 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003428 } else {
3429 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003430 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003431 if (outlen > 0)
3432 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003433 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003434
3435 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003436 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003437 goto fail;
3438 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003439
Willy Tarreau174b06a2018-04-25 18:13:58 +02003440 if (msgf & H2_MSGF_BODY) {
3441 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003442 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003443 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003444 if (htx)
3445 htx->extra = *body_len;
3446 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003447 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003448 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003449 }
3450
Willy Tarreau88d138e2019-01-02 19:38:14 +01003451 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003452 /* indicate that a HEADERS frame was received for this stream, except
3453 * for 1xx responses. For 1xx responses, another HEADERS frame is
3454 * expected.
3455 */
3456 if (!(msgf & H2_MSGF_RSP_1XX))
3457 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003458
Christopher Faulet0b465482019-02-19 15:14:23 +01003459 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003460 /* Mark the end of message, either using EOM in HTX or with the
3461 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3462 * is not set during headers with END_STREAM.
3463 */
3464 if (htx) {
3465 if (!htx_add_endof(htx, HTX_BLK_EOM))
3466 goto fail;
3467 }
3468 else if (*flags & H2_SF_DATA_CHNK) {
3469 if (!b_putblk(rxbuf, "\r\n", 2))
3470 goto fail;
3471 }
3472 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003473
Willy Tarreau86277d42019-01-02 15:36:11 +01003474 /* success */
3475 ret = 1;
3476
Willy Tarreau68dd9852017-07-03 14:44:26 +02003477 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003478 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003479 * move the remaining data over it.
3480 */
3481 if (hole) {
3482 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3483 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3484 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3485 b_sub(&h2c->dbuf, hole);
3486 }
3487
3488 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3489 /* too large frames */
3490 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003491 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003492 }
3493
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003494 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003495 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003496 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003497 return ret;
3498
Willy Tarreau68dd9852017-07-03 14:44:26 +02003499 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003500 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003501 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003502
3503 trailers:
3504 /* This is the last HEADERS frame hence a trailer */
3505
3506 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3507 /* It's a trailer but it's missing ES flag */
3508 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3509 goto fail;
3510 }
3511
3512 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3513 * block, and when using chunks we must send the 0 CRLF marker. For
3514 * other modes, the trailers are silently dropped.
3515 */
3516 if (htx) {
3517 if (!htx_add_endof(htx, HTX_BLK_EOD))
3518 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003519 if (h2_make_htx_trailers(list, htx) <= 0)
3520 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003521 }
3522 else if (*flags & H2_SF_DATA_CHNK) {
3523 /* Legacy mode with chunked encoding : we must finalize the
3524 * data block message emit the trailing CRLF */
3525 if (!b_putblk(rxbuf, "0\r\n", 3))
3526 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003527
3528 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3529 if (outlen > 0)
3530 b_add(rxbuf, outlen);
3531 else
3532 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003533 }
3534
3535 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003536}
3537
Willy Tarreau454f9052017-10-26 19:40:35 +02003538/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3539 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3540 * in use, a new chunk is emitted for each frame. This is supposed to fit
3541 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3542 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3543 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003544 * parser state is automatically updated. Returns > 0 if it could completely
3545 * send the current frame, 0 if it couldn't complete, in which case
3546 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3547 * DATA frame can return 0 as a valid result). Stream errors are reported in
3548 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3549 * have checked the frame header and ensured that the frame was complete or the
3550 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003551 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003552static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003553{
3554 struct h2c *h2c = h2s->h2c;
3555 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003556 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003557 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003558 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003559 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003560
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003561 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003562
Olivier Houchard638b7992018-08-16 15:41:52 +02003563 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003564 if (!csbuf) {
3565 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003566 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003567 }
3568
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003569try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003570 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003571 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3572 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003573 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003574 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003575
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003576 if (flen > b_data(&h2c->dbuf)) {
3577 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003578 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003579 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003580 }
3581
Willy Tarreaua9b77962019-01-31 07:23:00 +01003582 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003583 block1 = htx_free_data_space(htx);
3584 if (!block1) {
3585 h2c->flags |= H2_CF_DEM_SFULL;
3586 goto fail;
3587 }
3588 if (flen > block1)
3589 flen = block1;
3590
3591 /* here, flen is the max we can copy into the output buffer */
3592 block1 = b_contig_data(&h2c->dbuf, 0);
3593 if (flen > block1)
3594 flen = block1;
3595
3596 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3597 h2c->flags |= H2_CF_DEM_SFULL;
3598 goto fail;
3599 }
3600
3601 b_del(&h2c->dbuf, flen);
3602 h2c->dfl -= flen;
3603 h2c->rcvd_c += flen;
3604 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003605
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003606 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003607 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003608 htx->extra = h2s->body_len;
3609 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003610 goto try_again;
3611 }
3612 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003613 /* it doesn't fit and the buffer is fragmented,
3614 * so let's defragment it and try again.
3615 */
3616 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003617 }
3618
Willy Tarreaueba10f22018-04-25 20:44:22 +02003619 /* chunked-encoding requires more room */
3620 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003621 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003622 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3623 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3624 (chklen < 1048576) ? 4 : 8;
3625 chklen += 4; // CRLF, CRLF
3626 }
3627
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003628 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003629 if (flen + chklen > b_room(csbuf)) {
3630 if (chklen >= b_room(csbuf)) {
3631 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003632 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003633 }
3634 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003635 }
3636
3637 if (h2s->flags & H2_SF_DATA_CHNK) {
3638 /* emit the chunk size */
3639 unsigned int chksz = flen;
3640 char str[10];
3641 char *beg;
3642
3643 beg = str + sizeof(str);
3644 *--beg = '\n';
3645 *--beg = '\r';
3646 do {
3647 *--beg = hextab[chksz & 0xF];
3648 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003649 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003650 }
3651
Willy Tarreau454f9052017-10-26 19:40:35 +02003652 /* Block1 is the length of the first block before the buffer wraps,
3653 * block2 is the optional second block to reach the end of the frame.
3654 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003655 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003656 if (block1 > flen)
3657 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003658 block2 = flen - block1;
3659
3660 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003661 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003662
3663 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003664 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003665
Willy Tarreaueba10f22018-04-25 20:44:22 +02003666 if (h2s->flags & H2_SF_DATA_CHNK) {
3667 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003668 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003669 }
3670
Willy Tarreau454f9052017-10-26 19:40:35 +02003671 /* now mark the input data as consumed (will be deleted from the buffer
3672 * by the caller when seeing FRAME_A after sending the window update).
3673 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003674 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003675 h2c->dfl -= flen;
3676 h2c->rcvd_c += flen;
3677 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3678
Willy Tarreau1915ca22019-01-24 11:49:37 +01003679 if (h2s->flags & H2_SF_DATA_CLEN)
3680 h2s->body_len -= flen;
3681
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003682 if (h2c->dfl > h2c->dpl) {
3683 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003685 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003686 }
3687
Willy Tarreau4a28da12018-01-04 14:41:00 +01003688 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003689 /* here we're done with the frame, all the payload (except padding) was
3690 * transferred.
3691 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003692
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003693 if (h2c->dff & H2_F_DATA_END_STREAM) {
3694 if (htx) {
3695 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3696 h2c->flags |= H2_CF_DEM_SFULL;
3697 goto fail;
3698 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003699 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003700 else if (h2s->flags & H2_SF_DATA_CHNK) {
3701 /* emit the trailing 0 CRLF CRLF */
3702 if (b_room(csbuf) < 5) {
3703 h2c->flags |= H2_CF_DEM_SFULL;
3704 goto fail;
3705 }
3706 chklen += 5;
3707 b_putblk(csbuf, "0\r\n\r\n", 5);
3708 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003709 }
3710
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003711 h2c->rcvd_c += h2c->dpl;
3712 h2c->rcvd_s += h2c->dpl;
3713 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003714 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3715
Willy Tarreau39d68502018-03-02 12:26:37 +01003716 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003717 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01003718 if (h2s->cs)
3719 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau39d68502018-03-02 12:26:37 +01003720 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003721 if (htx)
3722 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003723 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003724 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003725 if (htx)
3726 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003727 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003728}
3729
Willy Tarreau5dd17352018-06-14 13:33:30 +02003730/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3731 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3732 * number of bytes sent. The caller must check the stream's status to detect
3733 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003734 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003735static 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 +02003736{
3737 struct http_hdr list[MAX_HTTP_HDR];
3738 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003739 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003740 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003741 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003742 int es_now = 0;
3743 int ret = 0;
3744 int hdr;
3745
3746 if (h2c_mux_busy(h2c, h2s)) {
3747 h2s->flags |= H2_SF_BLK_MBUSY;
3748 return 0;
3749 }
3750
Willy Tarreau44e973f2018-03-01 17:49:30 +01003751 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003752 h2c->flags |= H2_CF_MUX_MALLOC;
3753 h2s->flags |= H2_SF_BLK_MROOM;
3754 return 0;
3755 }
3756
3757 /* First, try to parse the H1 response and index it into <list>.
3758 * NOTE! Since it comes from haproxy, we *know* that a response header
3759 * block does not wrap and we can safely read it this way without
3760 * having to realign the buffer.
3761 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003762 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003763 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003764 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003765 /* incomplete or invalid response, this is abnormal coming from
3766 * haproxy and may only result in a bad errorfile or bad Lua code
3767 * so that won't be fixed, raise an error now.
3768 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003769 * FIXME: we should instead add the ability to only return a
3770 * 502 bad gateway. But in theory this is not supposed to
3771 * happen.
3772 */
3773 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3774 ret = 0;
3775 goto end;
3776 }
3777
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003778 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003779
3780 /* certain statuses have no body or an empty one, regardless of
3781 * what the headers say.
3782 */
3783 if (sl.st.status >= 100 && sl.st.status < 200) {
3784 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3785 h1m->curr_len = h1m->body_len = 0;
3786 }
3787 else if (sl.st.status == 204 || sl.st.status == 304) {
3788 /* no contents, claim c-len is present and set to zero */
3789 h1m->flags &= ~H1_MF_CHNK;
3790 h1m->flags |= H1_MF_CLEN;
3791 h1m->curr_len = h1m->body_len = 0;
3792 }
3793
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003794 chunk_reset(&outbuf);
3795
3796 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003797 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003798 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003799 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003800
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003801 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003802 break;
3803 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003804 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003805 }
3806
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003807 if (outbuf.size < 9)
3808 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003809
3810 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003811 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3812 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3813 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003814
3815 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003816 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003817 /* this is an unparsable response */
3818 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3819 ret = 0;
3820 goto end;
3821 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003822
3823 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003824 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003825 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003826 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003827 }
3828
3829 /* encode all headers, stop at empty name */
3830 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003831 /* these ones do not exist in H2 and must be dropped. */
3832 if (isteq(list[hdr].n, ist("connection")) ||
3833 isteq(list[hdr].n, ist("proxy-connection")) ||
3834 isteq(list[hdr].n, ist("keep-alive")) ||
3835 isteq(list[hdr].n, ist("upgrade")) ||
3836 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003837 continue;
3838
3839 if (isteq(list[hdr].n, ist("")))
3840 break; // end
3841
3842 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3843 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003844 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003845 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003846 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003847 }
3848 }
3849
3850 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003851 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003852 es_now = 1;
3853
3854 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003855 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003856
3857 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003858 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003859
3860 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003861 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003862
3863 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003864 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003865 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003866
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003867 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003868 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003869 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003870
Willy Tarreau801250e2018-09-11 11:45:04 +02003871 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003872 h2s->flags |= H2_SF_ES_SENT;
3873 if (h2s->st == H2_SS_OPEN)
3874 h2s->st = H2_SS_HLOC;
3875 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003876 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003877 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003878 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003879 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003880 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003881 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003882 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003883 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003884 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003885
3886 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003887
3888 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003889 //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 +02003890 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003891 full:
3892 h1m_init_res(h1m);
3893 h1m->err_pos = -1; // don't care about errors on the response path
3894 h2c->flags |= H2_CF_MUX_MFULL;
3895 h2s->flags |= H2_SF_BLK_MROOM;
3896 ret = 0;
3897 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003898}
3899
Willy Tarreau5dd17352018-06-14 13:33:30 +02003900/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3901 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3902 * the number of bytes sent. The caller must check the stream's status to
3903 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003904 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003905static 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 +02003906{
3907 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003908 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003909 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003910 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003911 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003912 int es_now = 0;
3913 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003914 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003915 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003916
3917 if (h2c_mux_busy(h2c, h2s)) {
3918 h2s->flags |= H2_SF_BLK_MBUSY;
3919 goto end;
3920 }
3921
Willy Tarreau44e973f2018-03-01 17:49:30 +01003922 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003923 h2c->flags |= H2_CF_MUX_MALLOC;
3924 h2s->flags |= H2_SF_BLK_MROOM;
3925 goto end;
3926 }
3927
3928 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003929 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003930 goto end;
3931
3932 chunk_reset(&outbuf);
3933
3934 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003935 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003936 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003937 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003938
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003939 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003940 break;
3941 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003942 /* If there are pending data in the output buffer, and we have
3943 * less than 1/4 of the mbuf's size and everything fits, we'll
3944 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3945 * is full and wait, to save some slow realign calls.
3946 */
3947 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3948 h2c->flags |= H2_CF_MUX_MFULL;
3949 h2s->flags |= H2_SF_BLK_MROOM;
3950 goto end;
3951 }
3952
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003953 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003954 }
3955
3956 if (outbuf.size < 9) {
3957 h2c->flags |= H2_CF_MUX_MFULL;
3958 h2s->flags |= H2_SF_BLK_MROOM;
3959 goto end;
3960 }
3961
3962 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003963 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3964 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3965 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003966
3967 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3968 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003969 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003970 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003971 break;
3972 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003973 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003974 if ((long long)size > h1m->curr_len)
3975 size = h1m->curr_len;
3976 break;
3977 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003978 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003979 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003980 if (!ret)
3981 goto end;
3982
3983 if (ret < 0) {
3984 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003985 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3987 goto end;
3988 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003989 max -= ret;
3990 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003991 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003992 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003993 }
3994
Willy Tarreau801250e2018-09-11 11:45:04 +02003995 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003996 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003997 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003998 if (!ret)
3999 goto end;
4000
4001 if (ret < 0) {
4002 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004003 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004004 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4005 goto end;
4006 }
4007
4008 size = chunk;
4009 h1m->curr_len = chunk;
4010 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004011 max -= ret;
4012 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004013 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004014 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004015 if (!size)
4016 goto send_empty;
4017 }
4018
4019 /* in MSG_DATA state, continue below */
4020 size = h1m->curr_len;
4021 break;
4022 }
4023
4024 /* we have in <size> the exact number of bytes we need to copy from
4025 * the H1 buffer. We need to check this against the connection's and
4026 * the stream's send windows, and to ensure that this fits in the max
4027 * frame size and in the buffer's available space minus 9 bytes (for
4028 * the frame header). The connection's flow control is applied last so
4029 * that we can use a separate list of streams which are immediately
4030 * unblocked on window opening. Note: we don't implement padding.
4031 */
4032
Willy Tarreau5dd17352018-06-14 13:33:30 +02004033 if (size > max)
4034 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004035
4036 if (size > h2s->mws)
4037 size = h2s->mws;
4038
4039 if (size <= 0) {
4040 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004041 if (h2s->send_wait) {
4042 LIST_DEL(&h2s->list);
4043 LIST_INIT(&h2s->list);
4044 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 goto end;
4046 }
4047
4048 if (h2c->mfs && size > h2c->mfs)
4049 size = h2c->mfs;
4050
4051 if (size + 9 > outbuf.size) {
4052 /* we have an opportunity for enlarging the too small
4053 * available space, let's try.
4054 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004055 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004056 goto realign_again;
4057 size = outbuf.size - 9;
4058 }
4059
4060 if (size <= 0) {
4061 h2c->flags |= H2_CF_MUX_MFULL;
4062 h2s->flags |= H2_SF_BLK_MROOM;
4063 goto end;
4064 }
4065
4066 if (size > h2c->mws)
4067 size = h2c->mws;
4068
4069 if (size <= 0) {
4070 h2s->flags |= H2_SF_BLK_MFCTL;
4071 goto end;
4072 }
4073
4074 /* copy whatever we can */
4075 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004076 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004077 if (ret == 1)
4078 len2 = 0;
4079
4080 if (!ret || len1 + len2 < size) {
4081 /* FIXME: must normally never happen */
4082 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4083 goto end;
4084 }
4085
4086 /* limit len1/len2 to size */
4087 if (len1 + len2 > size) {
4088 int sub = len1 + len2 - size;
4089
4090 if (len2 > sub)
4091 len2 -= sub;
4092 else {
4093 sub -= len2;
4094 len2 = 0;
4095 len1 -= sub;
4096 }
4097 }
4098
4099 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004100 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004102 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103
4104 send_empty:
4105 /* we may need to add END_STREAM */
4106 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4107 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004108 *
4109 * FIXME: what we do here is not correct because we send end_stream
4110 * before knowing if we'll have to send a HEADERS frame for the
4111 * trailers. More importantly we're not consuming the trailing CRLF
4112 * after the end of trailers, so it will be left to the caller to
4113 * eat it. The right way to do it would be to measure trailers here
4114 * and to send ES only if there are no trailers.
4115 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004116 */
4117 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004118 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004119 es_now = 1;
4120
4121 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004122 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004123
4124 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004125 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004126
4127 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004128 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004129
4130 /* consume incoming H1 response */
4131 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004132 max -= size;
4133 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004134 total += size;
4135 h1m->curr_len -= size;
4136 h2s->mws -= size;
4137 h2c->mws -= size;
4138
4139 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004140 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004141 goto new_frame;
4142 }
4143 }
4144
4145 if (es_now) {
4146 if (h2s->st == H2_SS_OPEN)
4147 h2s->st = H2_SS_HLOC;
4148 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004149 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004150
Willy Tarreau35a62702018-02-27 15:37:25 +01004151 if (!(h1m->flags & H1_MF_CHNK)) {
4152 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004153 total += max;
4154 ofs += max;
4155 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004156
Willy Tarreau801250e2018-09-11 11:45:04 +02004157 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004158 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004159
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004160 h2s->flags |= H2_SF_ES_SENT;
4161 }
4162
4163 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004164 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 +02004165 return total;
4166}
4167
Willy Tarreau115e83b2018-12-01 19:17:53 +01004168/* Try to send a HEADERS frame matching HTX response present in HTX message
4169 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4170 * must check the stream's status to detect any error which might have happened
4171 * subsequently to a successful send. The htx blocks are automatically removed
4172 * from the message. The htx message is assumed to be valid since produced from
4173 * the internal code, hence it contains a start line, an optional series of
4174 * header blocks and an end of header, otherwise an invalid frame could be
4175 * emitted and the resulting htx message could be left in an inconsistent state.
4176 */
4177static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4178{
4179 struct http_hdr list[MAX_HTTP_HDR];
4180 struct h2c *h2c = h2s->h2c;
4181 struct htx_blk *blk;
4182 struct htx_blk *blk_end;
4183 struct buffer outbuf;
4184 struct htx_sl *sl;
4185 enum htx_blk_type type;
4186 int es_now = 0;
4187 int ret = 0;
4188 int hdr;
4189 int idx;
4190
4191 if (h2c_mux_busy(h2c, h2s)) {
4192 h2s->flags |= H2_SF_BLK_MBUSY;
4193 return 0;
4194 }
4195
4196 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4197 h2c->flags |= H2_CF_MUX_MALLOC;
4198 h2s->flags |= H2_SF_BLK_MROOM;
4199 return 0;
4200 }
4201
4202 /* determine the first block which must not be deleted, blk_end may
4203 * be NULL if all blocks have to be deleted.
4204 */
4205 idx = htx_get_head(htx);
4206 blk_end = NULL;
4207 while (idx != -1) {
4208 type = htx_get_blk_type(htx_get_blk(htx, idx));
4209 idx = htx_get_next(htx, idx);
4210 if (type == HTX_BLK_EOH) {
4211 if (idx != -1)
4212 blk_end = htx_get_blk(htx, idx);
4213 break;
4214 }
4215 }
4216
4217 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004218 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004219 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004220 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004221 if (h2s->status < 100 || h2s->status > 999)
4222 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004223
4224 /* and the rest of the headers, that we dump starting at header 0 */
4225 hdr = 0;
4226
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004227 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004228 while ((idx = htx_get_next(htx, idx)) != -1) {
4229 blk = htx_get_blk(htx, idx);
4230 type = htx_get_blk_type(blk);
4231
4232 if (type == HTX_BLK_UNUSED)
4233 continue;
4234
4235 if (type != HTX_BLK_HDR)
4236 break;
4237
4238 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4239 goto fail;
4240
4241 list[hdr].n = htx_get_blk_name(htx, blk);
4242 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004243 hdr++;
4244 }
4245
4246 /* marker for end of headers */
4247 list[hdr].n = ist("");
4248
4249 if (h2s->status == 204 || h2s->status == 304) {
4250 /* no contents, claim c-len is present and set to zero */
4251 es_now = 1;
4252 }
4253
4254 chunk_reset(&outbuf);
4255
4256 while (1) {
4257 outbuf.area = b_tail(&h2c->mbuf);
4258 outbuf.size = b_contig_space(&h2c->mbuf);
4259 outbuf.data = 0;
4260
4261 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4262 break;
4263 realign_again:
4264 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4265 }
4266
4267 if (outbuf.size < 9)
4268 goto full;
4269
4270 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4271 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4272 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4273 outbuf.data = 9;
4274
4275 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004276 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004277 if (b_space_wraps(&h2c->mbuf))
4278 goto realign_again;
4279 goto full;
4280 }
4281
4282 /* encode all headers, stop at empty name */
4283 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4284 /* these ones do not exist in H2 and must be dropped. */
4285 if (isteq(list[hdr].n, ist("connection")) ||
4286 isteq(list[hdr].n, ist("proxy-connection")) ||
4287 isteq(list[hdr].n, ist("keep-alive")) ||
4288 isteq(list[hdr].n, ist("upgrade")) ||
4289 isteq(list[hdr].n, ist("transfer-encoding")))
4290 continue;
4291
4292 if (isteq(list[hdr].n, ist("")))
4293 break; // end
4294
4295 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4296 /* output full */
4297 if (b_space_wraps(&h2c->mbuf))
4298 goto realign_again;
4299 goto full;
4300 }
4301 }
4302
Christopher Faulet0b465482019-02-19 15:14:23 +01004303 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004304 * FIXME: we should also set it when we know for sure that the
4305 * content-length is zero as well as on 204/304
4306 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004307 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4308 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004309 es_now = 1;
4310
Willy Tarreau927b88b2019-03-04 08:03:25 +01004311 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004312 es_now = 1;
4313
4314 /* update the frame's size */
4315 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4316
4317 if (es_now)
4318 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4319
4320 /* commit the H2 response */
4321 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004322
4323 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4324 * 1xx responses, another HEADERS frame is expected.
4325 */
4326 if (h2s->status >= 200 || h2s->status == 101)
4327 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004328
Willy Tarreau115e83b2018-12-01 19:17:53 +01004329 if (es_now) {
4330 h2s->flags |= H2_SF_ES_SENT;
4331 if (h2s->st == H2_SS_OPEN)
4332 h2s->st = H2_SS_HLOC;
4333 else
4334 h2s_close(h2s);
4335 }
4336
4337 /* OK we could properly deliver the response */
4338
4339 /* remove all header blocks including the EOH and compute the
4340 * corresponding size.
4341 *
4342 * FIXME: We should remove everything when es_now is set.
4343 */
4344 ret = 0;
4345 idx = htx_get_head(htx);
4346 blk = htx_get_blk(htx, idx);
4347 while (blk != blk_end) {
4348 ret += htx_get_blksz(blk);
4349 blk = htx_remove_blk(htx, blk);
4350 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004351
4352 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4353 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004354 end:
4355 return ret;
4356 full:
4357 h2c->flags |= H2_CF_MUX_MFULL;
4358 h2s->flags |= H2_SF_BLK_MROOM;
4359 ret = 0;
4360 goto end;
4361 fail:
4362 /* unparsable HTX messages, too large ones to be produced in the local
4363 * list etc go here (unrecoverable errors).
4364 */
4365 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4366 ret = 0;
4367 goto end;
4368}
4369
Willy Tarreau80739692018-10-05 11:35:57 +02004370/* Try to send a HEADERS frame matching HTX request present in HTX message
4371 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4372 * must check the stream's status to detect any error which might have happened
4373 * subsequently to a successful send. The htx blocks are automatically removed
4374 * from the message. The htx message is assumed to be valid since produced from
4375 * the internal code, hence it contains a start line, an optional series of
4376 * header blocks and an end of header, otherwise an invalid frame could be
4377 * emitted and the resulting htx message could be left in an inconsistent state.
4378 */
4379static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4380{
4381 struct http_hdr list[MAX_HTTP_HDR];
4382 struct h2c *h2c = h2s->h2c;
4383 struct htx_blk *blk;
4384 struct htx_blk *blk_end;
4385 struct buffer outbuf;
4386 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004387 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004388 enum htx_blk_type type;
4389 int es_now = 0;
4390 int ret = 0;
4391 int hdr;
4392 int idx;
4393
4394 if (h2c_mux_busy(h2c, h2s)) {
4395 h2s->flags |= H2_SF_BLK_MBUSY;
4396 return 0;
4397 }
4398
4399 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4400 h2c->flags |= H2_CF_MUX_MALLOC;
4401 h2s->flags |= H2_SF_BLK_MROOM;
4402 return 0;
4403 }
4404
4405 /* determine the first block which must not be deleted, blk_end may
4406 * be NULL if all blocks have to be deleted.
4407 */
4408 idx = htx_get_head(htx);
4409 blk_end = NULL;
4410 while (idx != -1) {
4411 type = htx_get_blk_type(htx_get_blk(htx, idx));
4412 idx = htx_get_next(htx, idx);
4413 if (type == HTX_BLK_EOH) {
4414 if (idx != -1)
4415 blk_end = htx_get_blk(htx, idx);
4416 break;
4417 }
4418 }
4419
4420 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004421 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004422 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004423 meth = htx_sl_req_meth(sl);
4424 path = htx_sl_req_uri(sl);
4425
4426 /* and the rest of the headers, that we dump starting at header 0 */
4427 hdr = 0;
4428
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004429 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004430 while ((idx = htx_get_next(htx, idx)) != -1) {
4431 blk = htx_get_blk(htx, idx);
4432 type = htx_get_blk_type(blk);
4433
4434 if (type == HTX_BLK_UNUSED)
4435 continue;
4436
4437 if (type != HTX_BLK_HDR)
4438 break;
4439
4440 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4441 goto fail;
4442
4443 list[hdr].n = htx_get_blk_name(htx, blk);
4444 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004445 hdr++;
4446 }
4447
4448 /* marker for end of headers */
4449 list[hdr].n = ist("");
4450
4451 chunk_reset(&outbuf);
4452
4453 while (1) {
4454 outbuf.area = b_tail(&h2c->mbuf);
4455 outbuf.size = b_contig_space(&h2c->mbuf);
4456 outbuf.data = 0;
4457
4458 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4459 break;
4460 realign_again:
4461 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4462 }
4463
4464 if (outbuf.size < 9)
4465 goto full;
4466
4467 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4468 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4469 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4470 outbuf.data = 9;
4471
4472 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004473 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004474 if (b_space_wraps(&h2c->mbuf))
4475 goto realign_again;
4476 goto full;
4477 }
4478
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004479 /* RFC7540 #8.3: the CONNECT method must have :
4480 * - :authority set to the URI part (host:port)
4481 * - :method set to CONNECT
4482 * - :scheme and :path omitted
4483 */
4484 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4485 /* encode the scheme which is always "https" (or 0x86 for "http") */
4486 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4487 /* output full */
4488 if (b_space_wraps(&h2c->mbuf))
4489 goto realign_again;
4490 goto full;
4491 }
Willy Tarreau80739692018-10-05 11:35:57 +02004492
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004493 /* encode the path, which necessarily is the second one */
4494 if (!hpack_encode_path(&outbuf, path)) {
4495 /* output full */
4496 if (b_space_wraps(&h2c->mbuf))
4497 goto realign_again;
4498 goto full;
4499 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004500
4501 /* look for the Host header and place it in :authority */
4502 auth = ist2(NULL, 0);
4503 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4504 if (isteq(list[hdr].n, ist("")))
4505 break; // end
4506
4507 if (isteq(list[hdr].n, ist("host"))) {
4508 auth = list[hdr].v;
4509 break;
4510 }
4511 }
4512 }
4513 else {
4514 /* for CONNECT, :authority is taken from the path */
4515 auth = path;
4516 }
4517
4518 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4519 /* output full */
4520 if (b_space_wraps(&h2c->mbuf))
4521 goto realign_again;
4522 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004523 }
4524
4525 /* encode all headers, stop at empty name */
4526 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4527 /* these ones do not exist in H2 and must be dropped. */
4528 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004529 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004530 isteq(list[hdr].n, ist("proxy-connection")) ||
4531 isteq(list[hdr].n, ist("keep-alive")) ||
4532 isteq(list[hdr].n, ist("upgrade")) ||
4533 isteq(list[hdr].n, ist("transfer-encoding")))
4534 continue;
4535
4536 if (isteq(list[hdr].n, ist("")))
4537 break; // end
4538
4539 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4540 /* output full */
4541 if (b_space_wraps(&h2c->mbuf))
4542 goto realign_again;
4543 goto full;
4544 }
4545 }
4546
4547 /* we may need to add END_STREAM if we have no body :
4548 * - request already closed, or :
4549 * - no transfer-encoding, and :
4550 * - no content-length or content-length:0
4551 * Fixme: this doesn't take into account CONNECT requests.
4552 */
4553 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4554 es_now = 1;
4555
4556 if (sl->flags & HTX_SL_F_BODYLESS)
4557 es_now = 1;
4558
Willy Tarreau927b88b2019-03-04 08:03:25 +01004559 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004560 es_now = 1;
4561
4562 /* update the frame's size */
4563 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4564
4565 if (es_now)
4566 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4567
4568 /* commit the H2 response */
4569 b_add(&h2c->mbuf, outbuf.data);
4570 h2s->flags |= H2_SF_HEADERS_SENT;
4571 h2s->st = H2_SS_OPEN;
4572
Willy Tarreau80739692018-10-05 11:35:57 +02004573 if (es_now) {
4574 // trim any possibly pending data (eg: inconsistent content-length)
4575 h2s->flags |= H2_SF_ES_SENT;
4576 h2s->st = H2_SS_HLOC;
4577 }
4578
4579 /* remove all header blocks including the EOH and compute the
4580 * corresponding size.
4581 *
4582 * FIXME: We should remove everything when es_now is set.
4583 */
4584 ret = 0;
4585 idx = htx_get_head(htx);
4586 blk = htx_get_blk(htx, idx);
4587 while (blk != blk_end) {
4588 ret += htx_get_blksz(blk);
4589 blk = htx_remove_blk(htx, blk);
4590 }
4591
4592 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4593 htx_remove_blk(htx, blk_end);
4594
4595 end:
4596 return ret;
4597 full:
4598 h2c->flags |= H2_CF_MUX_MFULL;
4599 h2s->flags |= H2_SF_BLK_MROOM;
4600 ret = 0;
4601 goto end;
4602 fail:
4603 /* unparsable HTX messages, too large ones to be produced in the local
4604 * list etc go here (unrecoverable errors).
4605 */
4606 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4607 ret = 0;
4608 goto end;
4609}
4610
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004611/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004612 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4613 * caller must check the stream's status to detect any error which might have
4614 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004615 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4616 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004617static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004618{
4619 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004620 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004621 struct buffer outbuf;
4622 size_t total = 0;
4623 int es_now = 0;
4624 int bsize; /* htx block size */
4625 int fsize; /* h2 frame size */
4626 struct htx_blk *blk;
4627 enum htx_blk_type type;
4628 int idx;
4629
4630 if (h2c_mux_busy(h2c, h2s)) {
4631 h2s->flags |= H2_SF_BLK_MBUSY;
4632 goto end;
4633 }
4634
4635 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4636 h2c->flags |= H2_CF_MUX_MALLOC;
4637 h2s->flags |= H2_SF_BLK_MROOM;
4638 goto end;
4639 }
4640
Willy Tarreau98de12a2018-12-12 07:03:00 +01004641 htx = htx_from_buf(buf);
4642
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004643 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4644 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4645 * the caller to handle.
4646 */
4647
4648 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004649 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004650 goto end;
4651
4652 idx = htx_get_head(htx);
4653 blk = htx_get_blk(htx, idx);
4654 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4655 bsize = htx_get_blksz(blk);
4656 fsize = bsize;
4657
4658 if (type == HTX_BLK_EOD) {
4659 /* if we have an EOD, we're dealing with chunked data. We may
4660 * have a set of trailers after us that the caller will want to
4661 * deal with. Let's simply remove the EOD and return.
4662 */
4663 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004664 total++; // EOD counts as one byte
4665 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004666 goto end;
4667 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004668 else if (type == HTX_BLK_EOM) {
4669 if (h2s->flags & H2_SF_ES_SENT) {
4670 /* ES already sent */
4671 htx_remove_blk(htx, blk);
4672 total++; // EOM counts as one byte
4673 count--;
4674 goto end;
4675 }
4676 }
4677 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004678 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004679
4680 /* Perform some optimizations to reduce the number of buffer copies.
4681 * First, if the mux's buffer is empty and the htx area contains
4682 * exactly one data block of the same size as the requested count, and
4683 * this count fits within the frame size, the stream's window size, and
4684 * the connection's window size, then it's possible to simply swap the
4685 * caller's buffer with the mux's output buffer and adjust offsets and
4686 * length to match the entire DATA HTX block in the middle. In this
4687 * case we perform a true zero-copy operation from end-to-end. This is
4688 * the situation that happens all the time with large files. Second, if
4689 * this is not possible, but the mux's output buffer is empty, we still
4690 * have an opportunity to avoid the copy to the intermediary buffer, by
4691 * making the intermediary buffer's area point to the output buffer's
4692 * area. In this case we want to skip the HTX header to make sure that
4693 * copies remain aligned and that this operation remains possible all
4694 * the time. This goes for headers, data blocks and any data extracted
4695 * from the HTX blocks.
4696 */
4697 if (unlikely(fsize == count &&
4698 htx->used == 1 && type == HTX_BLK_DATA &&
4699 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4700 void *old_area = h2c->mbuf.area;
4701
4702 if (b_data(&h2c->mbuf)) {
4703 /* too bad there are data left there. If we have less
4704 * than 1/4 of the mbuf's size and everything fits,
4705 * we'll perform a copy anyway. Otherwise we'll pretend
4706 * the mbuf is full and wait.
4707 */
4708 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4709 goto copy;
4710 h2c->flags |= H2_CF_MUX_MFULL;
4711 h2s->flags |= H2_SF_BLK_MROOM;
4712 goto end;
4713 }
4714
4715 /* map an H2 frame to the HTX block so that we can put the
4716 * frame header there.
4717 */
4718 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004719 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004720 h2c->mbuf.data = fsize + 9;
4721 outbuf.area = b_head(&h2c->mbuf);
4722
4723 /* prepend an H2 DATA frame header just before the DATA block */
4724 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4725 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4726 h2_set_frame_size(outbuf.area, fsize);
4727
4728 /* update windows */
4729 h2s->mws -= fsize;
4730 h2c->mws -= fsize;
4731
4732 /* and exchange with our old area */
4733 buf->area = old_area;
4734 buf->data = buf->head = 0;
4735 total += fsize;
4736 goto end;
4737 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004738
Willy Tarreau98de12a2018-12-12 07:03:00 +01004739 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004740 /* for DATA and EOM we'll have to emit a frame, even if empty */
4741
4742 while (1) {
4743 outbuf.area = b_tail(&h2c->mbuf);
4744 outbuf.size = b_contig_space(&h2c->mbuf);
4745 outbuf.data = 0;
4746
4747 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4748 break;
4749 realign_again:
4750 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4751 }
4752
4753 if (outbuf.size < 9) {
4754 h2c->flags |= H2_CF_MUX_MFULL;
4755 h2s->flags |= H2_SF_BLK_MROOM;
4756 goto end;
4757 }
4758
4759 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4760 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4761 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4762 outbuf.data = 9;
4763
4764 /* we have in <fsize> the exact number of bytes we need to copy from
4765 * the HTX buffer. We need to check this against the connection's and
4766 * the stream's send windows, and to ensure that this fits in the max
4767 * frame size and in the buffer's available space minus 9 bytes (for
4768 * the frame header). The connection's flow control is applied last so
4769 * that we can use a separate list of streams which are immediately
4770 * unblocked on window opening. Note: we don't implement padding.
4771 */
4772
4773 /* EOM is presented with bsize==1 but would lead to the emission of an
4774 * empty frame, thus we force it to zero here.
4775 */
4776 if (type == HTX_BLK_EOM)
4777 bsize = fsize = 0;
4778
4779 if (!fsize)
4780 goto send_empty;
4781
4782 if (h2s->mws <= 0) {
4783 h2s->flags |= H2_SF_BLK_SFCTL;
4784 if (h2s->send_wait) {
4785 LIST_DEL(&h2s->list);
4786 LIST_INIT(&h2s->list);
4787 }
4788 goto end;
4789 }
4790
Willy Tarreauee573762018-12-04 15:25:57 +01004791 if (fsize > count)
4792 fsize = count;
4793
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004794 if (fsize > h2s->mws)
4795 fsize = h2s->mws; // >0
4796
4797 if (h2c->mfs && fsize > h2c->mfs)
4798 fsize = h2c->mfs; // >0
4799
4800 if (fsize + 9 > outbuf.size) {
4801 /* we have an opportunity for enlarging the too small
4802 * available space, let's try.
4803 * FIXME: is this really interesting to do? Maybe we'll
4804 * spend lots of time realigning instead of using two
4805 * frames.
4806 */
4807 if (b_space_wraps(&h2c->mbuf))
4808 goto realign_again;
4809 fsize = outbuf.size - 9;
4810
4811 if (fsize <= 0) {
4812 /* no need to send an empty frame here */
4813 h2c->flags |= H2_CF_MUX_MFULL;
4814 h2s->flags |= H2_SF_BLK_MROOM;
4815 goto end;
4816 }
4817 }
4818
4819 if (h2c->mws <= 0) {
4820 h2s->flags |= H2_SF_BLK_MFCTL;
4821 goto end;
4822 }
4823
4824 if (fsize > h2c->mws)
4825 fsize = h2c->mws;
4826
4827 /* now let's copy this this into the output buffer */
4828 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004829 h2s->mws -= fsize;
4830 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004831 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004832
4833 send_empty:
4834 /* update the frame's size */
4835 h2_set_frame_size(outbuf.area, fsize);
4836
4837 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4838 * meeting EOM. We should optimize this later.
4839 */
4840 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004841 total++; // EOM counts as one byte
4842 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004843 es_now = 1;
4844 }
4845
4846 if (es_now)
4847 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4848
4849 /* commit the H2 response */
4850 b_add(&h2c->mbuf, fsize + 9);
4851
4852 /* consume incoming HTX block, including EOM */
4853 total += fsize;
4854 if (fsize == bsize) {
4855 htx_remove_blk(htx, blk);
4856 if (fsize)
4857 goto new_frame;
4858 } else {
4859 /* we've truncated this block */
4860 htx_cut_data_blk(htx, blk, fsize);
4861 }
4862
4863 if (es_now) {
4864 if (h2s->st == H2_SS_OPEN)
4865 h2s->st = H2_SS_HLOC;
4866 else
4867 h2s_close(h2s);
4868
4869 h2s->flags |= H2_SF_ES_SENT;
4870 }
4871
4872 end:
4873 return total;
4874}
4875
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004876/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4877 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4878 * processed. The caller must check the stream's status to detect any error
4879 * which might have happened subsequently to a successful send. The htx blocks
4880 * are automatically removed from the message. The htx message is assumed to be
4881 * valid since produced from the internal code. Processing stops when meeting
4882 * the EOM, which is also removed. All trailers are processed at once and sent
4883 * as a single frame. The ES flag is always set.
4884 */
4885static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4886{
4887 struct http_hdr list[MAX_HTTP_HDR];
4888 struct h2c *h2c = h2s->h2c;
4889 struct htx_blk *blk;
4890 struct htx_blk *blk_end;
4891 struct buffer outbuf;
4892 struct h1m h1m;
4893 enum htx_blk_type type;
4894 uint32_t size;
4895 int ret = 0;
4896 int hdr;
4897 int idx;
4898 void *start;
4899
4900 if (h2c_mux_busy(h2c, h2s)) {
4901 h2s->flags |= H2_SF_BLK_MBUSY;
4902 goto end;
4903 }
4904
4905 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4906 h2c->flags |= H2_CF_MUX_MALLOC;
4907 h2s->flags |= H2_SF_BLK_MROOM;
4908 goto end;
4909 }
4910
4911 /* The principle is that we parse each and every trailers block using
4912 * the H1 headers parser, and append it to the list. We don't proceed
4913 * until EOM is met. blk_end will point to the EOM block.
4914 */
4915 hdr = 0;
4916 memset(list, 0, sizeof(list));
4917 blk_end = NULL;
4918
4919 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4920 blk = htx_get_blk(htx, idx);
4921 type = htx_get_blk_type(blk);
4922
4923 if (type == HTX_BLK_UNUSED)
4924 continue;
4925
4926 if (type != HTX_BLK_TLR) {
4927 if (type == HTX_BLK_EOM)
4928 blk_end = blk;
4929 break;
4930 }
4931
4932 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4933 goto fail;
4934
4935 size = htx_get_blksz(blk);
4936 start = htx_get_blk_ptr(htx, blk);
4937
4938 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4939 h1m.err_pos = 0;
4940 ret = h1_headers_to_hdr_list(start, start + size,
4941 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4942 &h1m, NULL);
4943 if (ret < 0)
4944 goto fail;
4945
4946 /* ret == 0 if an incomplete trailers block was found (missing
4947 * empty line), or > 0 if it was found. We have to continue on
4948 * incomplete messages because the trailers block might be
4949 * incomplete.
4950 */
4951
4952 /* search the new end */
4953 while (hdr <= sizeof(list)/sizeof(list[0])) {
4954 if (!list[hdr].n.len)
4955 break;
4956 hdr++;
4957 }
4958 }
4959
4960 if (!blk_end)
4961 goto end; // end not found yet
4962
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004963 chunk_reset(&outbuf);
4964
4965 while (1) {
4966 outbuf.area = b_tail(&h2c->mbuf);
4967 outbuf.size = b_contig_space(&h2c->mbuf);
4968 outbuf.data = 0;
4969
4970 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4971 break;
4972 realign_again:
4973 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4974 }
4975
4976 if (outbuf.size < 9)
4977 goto full;
4978
4979 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4980 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4981 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4982 outbuf.data = 9;
4983
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004984 /* encode all headers */
4985 for (idx = 0; idx < hdr; idx++) {
4986 /* these ones do not exist in H2 or must not appear in
4987 * trailers and must be dropped.
4988 */
4989 if (isteq(list[idx].n, ist("host")) ||
4990 isteq(list[idx].n, ist("content-length")) ||
4991 isteq(list[idx].n, ist("connection")) ||
4992 isteq(list[idx].n, ist("proxy-connection")) ||
4993 isteq(list[idx].n, ist("keep-alive")) ||
4994 isteq(list[idx].n, ist("upgrade")) ||
4995 isteq(list[idx].n, ist("te")) ||
4996 isteq(list[idx].n, ist("transfer-encoding")))
4997 continue;
4998
4999 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5000 /* output full */
5001 if (b_space_wraps(&h2c->mbuf))
5002 goto realign_again;
5003 goto full;
5004 }
5005 }
5006
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005007 if (!hdr) {
5008 /* here we have a problem, we've received an empty trailers
5009 * block followed by an EOM. Because of this we can't send a
5010 * HEADERS frame, so we have to cheat and instead send an empty
5011 * DATA frame conveying the ES flag.
5012 */
5013 outbuf.area[3] = H2_FT_DATA;
5014 outbuf.area[4] = H2_F_DATA_END_STREAM;
5015 }
5016
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005017 /* update the frame's size */
5018 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5019
5020 /* commit the H2 response */
5021 b_add(&h2c->mbuf, outbuf.data);
5022 h2s->flags |= H2_SF_ES_SENT;
5023
5024 if (h2s->st == H2_SS_OPEN)
5025 h2s->st = H2_SS_HLOC;
5026 else
5027 h2s_close(h2s);
5028
5029 /* OK we could properly deliver the response */
5030 done:
5031 /* remove all header blocks including EOM and compute the corresponding size. */
5032 ret = 0;
5033 idx = htx_get_head(htx);
5034 blk = htx_get_blk(htx, idx);
5035 while (blk != blk_end) {
5036 ret += htx_get_blksz(blk);
5037 blk = htx_remove_blk(htx, blk);
5038 }
5039 blk = htx_remove_blk(htx, blk);
5040 end:
5041 return ret;
5042 full:
5043 h2c->flags |= H2_CF_MUX_MFULL;
5044 h2s->flags |= H2_SF_BLK_MROOM;
5045 ret = 0;
5046 goto end;
5047 fail:
5048 /* unparsable HTX messages, too large ones to be produced in the local
5049 * list etc go here (unrecoverable errors).
5050 */
5051 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5052 ret = 0;
5053 goto end;
5054}
5055
Olivier Houchard6ff20392018-07-17 18:46:31 +02005056/* Called from the upper layer, to subscribe to events, such as being able to send */
5057static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5058{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005059 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005060 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005061 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005062
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005063 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005064 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005065 if (!(sw->events & SUB_RETRY_RECV)) {
5066 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005067 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005068 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005069 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005070 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005071 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005072 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005073 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005074 if (!(sw->events & SUB_RETRY_SEND)) {
5075 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005076 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005077 h2s->send_wait = sw;
5078 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5079 if (h2s->flags & H2_SF_BLK_MFCTL)
5080 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5081 else
5082 LIST_ADDQ(&h2c->send_list, &h2s->list);
5083 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005084 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005085 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005086 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005087 if (event_type != 0)
5088 return -1;
5089 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005090
5091
5092}
5093
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005094static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5095{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005096 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005097 struct h2s *h2s = cs->ctx;
5098
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005099 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005100 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005101 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005102 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005103 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005104 }
5105 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005106 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005107 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005108 if (h2s->send_wait == sw) {
5109 LIST_DEL(&h2s->list);
5110 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005111 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005112 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005113 }
5114 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005115 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5116 sw = param;
5117 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005118 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005119 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005120 LIST_DEL(&h2s->list);
5121 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005122 }
5123 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005124 return 0;
5125}
5126
5127
Olivier Houchard511efea2018-08-16 15:30:32 +02005128/* Called from the upper layer, to receive data */
5129static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5130{
Olivier Houchard638b7992018-08-16 15:41:52 +02005131 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005132 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005133 struct htx *h2s_htx = NULL;
5134 struct htx *buf_htx = NULL;
5135 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005136 size_t ret = 0;
5137
5138 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005139 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5140 /* in HTX mode we ignore the count argument */
5141 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005142 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005143 /* Here htx_to_buf() will set buffer data to 0 because
5144 * the HTX is empty.
5145 */
5146 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005147 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005148 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005149
5150 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005151 count = htx_free_data_space(buf_htx);
5152 if (flags & CO_RFL_KEEP_RSV) {
5153 if (count <= global.tune.maxrewrite)
5154 goto end;
5155 count -= global.tune.maxrewrite;
5156 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005157
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005158 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005159
5160 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5161 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5162
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005163 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005164 htx_to_buf(buf_htx, buf);
5165 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005166 ret = htx_ret.ret;
5167 }
5168 else {
5169 ret = b_xfer(buf, &h2s->rxbuf, count);
5170 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005171
Christopher Faulet37070b22019-02-14 15:12:14 +01005172 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005173 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005174 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005175 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005176 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005177 if (cs->flags & CS_FL_REOS)
5178 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005179 if (cs->flags & CS_FL_ERR_PENDING)
5180 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005181 if (b_size(&h2s->rxbuf)) {
5182 b_free(&h2s->rxbuf);
5183 offer_buffers(NULL, tasks_run_queue);
5184 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005185 }
5186
Willy Tarreau082f5592018-11-25 08:03:32 +01005187 if (ret && h2c->dsi == h2s->id) {
5188 /* demux is blocking on this stream's buffer */
5189 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005190 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005191 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005192
Olivier Houchard511efea2018-08-16 15:30:32 +02005193 return ret;
5194}
5195
Olivier Houchardd846c262018-10-19 17:24:29 +02005196static void h2_stop_senders(struct h2c *h2c)
5197{
5198 struct h2s *h2s, *h2s_back;
5199
5200 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5201 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5202 if (h2c->msi == h2s_id(h2s))
5203 continue;
5204 LIST_DEL(&h2s->list);
5205 LIST_INIT(&h2s->list);
5206 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005207 h2s->send_wait->events |= SUB_RETRY_SEND;
5208 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005209 LIST_ADD(&h2c->send_list, &h2s->list);
5210 }
5211}
5212
Willy Tarreau62f52692017-10-08 23:01:42 +02005213/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005214static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005215{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005216 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005217 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005218 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005219 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005220 struct htx *htx;
5221 struct htx_blk *blk;
5222 enum htx_blk_type btype;
5223 uint32_t bsize;
5224 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005225
Olivier Houchardd846c262018-10-19 17:24:29 +02005226 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005227 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005228 h2s->send_wait = NULL;
5229 LIST_DEL(&h2s->list);
5230 LIST_INIT(&h2s->list);
5231 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005232 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5233 return 0;
5234
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005235 /* htx will be enough to decide if we're using HTX or legacy */
5236 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5237
Willy Tarreau0bad0432018-06-14 16:54:01 +02005238 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005239 h2s->flags |= H2_SF_OUTGOING_DATA;
5240
Willy Tarreau751f2d02018-10-05 09:35:00 +02005241 if (h2s->id == 0) {
5242 int32_t id = h2c_get_next_sid(h2s->h2c);
5243
5244 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005245 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005246 return 0;
5247 }
5248
5249 eb32_delete(&h2s->by_id);
5250 h2s->by_id.key = h2s->id = id;
5251 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005252 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005253 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5254 }
5255
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005256 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005257 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5258 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005259 idx = htx_get_head(htx);
5260 blk = htx_get_blk(htx, idx);
5261 btype = htx_get_blk_type(blk);
5262 bsize = htx_get_blksz(blk);
5263
5264 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005265 case HTX_BLK_REQ_SL:
5266 /* start-line before headers */
5267 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5268 if (ret > 0) {
5269 total += ret;
5270 count -= ret;
5271 if (ret < bsize)
5272 goto done;
5273 }
5274 break;
5275
Willy Tarreau115e83b2018-12-01 19:17:53 +01005276 case HTX_BLK_RES_SL:
5277 /* start-line before headers */
5278 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5279 if (ret > 0) {
5280 total += ret;
5281 count -= ret;
5282 if (ret < bsize)
5283 goto done;
5284 }
5285 break;
5286
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005287 case HTX_BLK_DATA:
5288 case HTX_BLK_EOD:
5289 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005290 /* all these cause the emission of a DATA frame (possibly empty).
5291 * This EOM necessarily is one before trailers, as the EOM following
5292 * trailers would have been consumed by the trailers parser.
5293 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005294 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005295 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005296 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005297 total += ret;
5298 count -= ret;
5299 if (ret < bsize)
5300 goto done;
5301 }
5302 break;
5303
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005304 case HTX_BLK_TLR:
5305 /* This is the first trailers block, all the subsequent ones AND
5306 * the EOM will be swallowed by the parser.
5307 */
5308 ret = h2s_htx_make_trailers(h2s, htx);
5309 if (ret > 0) {
5310 total += ret;
5311 count -= ret;
5312 if (ret < bsize)
5313 goto done;
5314 }
5315 break;
5316
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005317 default:
5318 htx_remove_blk(htx, blk);
5319 total += bsize;
5320 count -= bsize;
5321 break;
5322 }
5323 }
5324 goto done;
5325 }
5326
5327 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005328 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005329 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005330 if (h2s->h2c->flags & H2_CF_IS_BACK)
5331 ret = -1;
5332 else
5333 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005334 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005335 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005336 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005337 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005338 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005339 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005340 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005341
Willy Tarreau5dd17352018-06-14 13:33:30 +02005342 if (unlikely((int)ret <= 0)) {
5343 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005344 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5345 break;
5346 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005347 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005348 total += count;
5349 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005350 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005351 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005352 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005353 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005354 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005355 break;
5356 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005357
5358 total += ret;
5359 count -= ret;
5360
5361 if (h2s->st >= H2_SS_ERROR)
5362 break;
5363
5364 if (h2s->flags & H2_SF_BLK_ANY)
5365 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005366 }
5367
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005368 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005369 if (h2s->st >= H2_SS_ERROR) {
5370 /* trim any possibly pending data after we close (extra CR-LF,
5371 * unprocessed trailers, abnormal extra data, ...)
5372 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005373 total += count;
5374 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005375 }
5376
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005377 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005378 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005379 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005380 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005381 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005382 }
5383
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005384 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005385 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005386 } else {
5387 b_del(buf, total);
5388 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005389
5390 /* The mux is full, cancel the pending tasks */
5391 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5392 (h2s->flags & H2_SF_BLK_MBUSY))
5393 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005394
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005395 /* If we're running HTX, and we read the whole buffer, then pretend
5396 * we read exactly what the caller specified, as with HTX the caller
5397 * will always give the buffer size, instead of the amount of data
5398 * available.
5399 */
5400 if (htx && !b_data(buf))
5401 total = orig_count;
5402
Olivier Houchard7505f942018-08-21 18:10:44 +02005403 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005404 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005405 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005406
Olivier Houchard7505f942018-08-21 18:10:44 +02005407 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005408 /* If we're waiting for flow control, and we got a shutr on the
5409 * connection, we will never be unlocked, so add an error on
5410 * the conn_stream.
5411 */
5412 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5413 !b_data(&h2s->h2c->dbuf) &&
5414 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5415 if (cs->flags & CS_FL_EOS)
5416 cs->flags |= CS_FL_ERROR;
5417 else
5418 cs->flags |= CS_FL_ERR_PENDING;
5419 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005420 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005421}
5422
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005423/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005424static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005425{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005426 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005427 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005428 struct eb32_node *node;
5429 int fctl_cnt = 0;
5430 int send_cnt = 0;
5431 int tree_cnt = 0;
5432 int orph_cnt = 0;
5433
5434 if (!h2c)
5435 return;
5436
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005437 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005438 fctl_cnt++;
5439
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005440 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005441 send_cnt++;
5442
Willy Tarreau3af37712018-12-18 14:34:41 +01005443 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005444 node = eb32_first(&h2c->streams_by_id);
5445 while (node) {
5446 h2s = container_of(node, struct h2s, by_id);
5447 tree_cnt++;
5448 if (!h2s->cs)
5449 orph_cnt++;
5450 node = eb32_next(node);
5451 }
5452
Willy Tarreau987c0632018-12-18 10:32:05 +01005453 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5454 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5455 " .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 +02005456 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5457 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005458 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005459 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5460 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5461 h2c->msi,
5462 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5463 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5464
5465 if (h2s) {
5466 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5467 h2s, h2s->id, h2s->flags,
5468 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5469 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5470 h2s->cs);
5471 if (h2s->cs)
5472 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5473 h2s->cs->flags, h2s->cs->data);
5474 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005475}
Willy Tarreau62f52692017-10-08 23:01:42 +02005476
5477/*******************************************************/
5478/* functions below are dedicated to the config parsers */
5479/*******************************************************/
5480
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005481/* config parser for global "tune.h2.header-table-size" */
5482static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5483 struct proxy *defpx, const char *file, int line,
5484 char **err)
5485{
5486 if (too_many_args(1, args, err, NULL))
5487 return -1;
5488
5489 h2_settings_header_table_size = atoi(args[1]);
5490 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5491 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5492 return -1;
5493 }
5494 return 0;
5495}
Willy Tarreau62f52692017-10-08 23:01:42 +02005496
Willy Tarreaue6baec02017-07-27 11:45:11 +02005497/* config parser for global "tune.h2.initial-window-size" */
5498static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5499 struct proxy *defpx, const char *file, int line,
5500 char **err)
5501{
5502 if (too_many_args(1, args, err, NULL))
5503 return -1;
5504
5505 h2_settings_initial_window_size = atoi(args[1]);
5506 if (h2_settings_initial_window_size < 0) {
5507 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5508 return -1;
5509 }
5510 return 0;
5511}
5512
Willy Tarreau5242ef82017-07-27 11:47:28 +02005513/* config parser for global "tune.h2.max-concurrent-streams" */
5514static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5515 struct proxy *defpx, const char *file, int line,
5516 char **err)
5517{
5518 if (too_many_args(1, args, err, NULL))
5519 return -1;
5520
5521 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005522 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005523 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5524 return -1;
5525 }
5526 return 0;
5527}
5528
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005529/* config parser for global "tune.h2.max-frame-size" */
5530static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5531 struct proxy *defpx, const char *file, int line,
5532 char **err)
5533{
5534 if (too_many_args(1, args, err, NULL))
5535 return -1;
5536
5537 h2_settings_max_frame_size = atoi(args[1]);
5538 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5539 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5540 return -1;
5541 }
5542 return 0;
5543}
5544
Willy Tarreau62f52692017-10-08 23:01:42 +02005545
5546/****************************************/
5547/* MUX initialization and instanciation */
5548/***************************************/
5549
5550/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005551static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005552 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005553 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005554 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005555 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005556 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005557 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005558 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005559 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005560 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005561 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005562 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005563 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005564 .shutr = h2_shutr,
5565 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005566 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005567 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005568 .name = "H2",
5569};
5570
Christopher Faulet32f61c02018-04-10 14:33:41 +02005571/* PROTO selection : this mux registers PROTO token "h2" */
5572static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005573 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005574
Willy Tarreau0108d902018-11-25 19:14:37 +01005575INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5576
Willy Tarreauf8957272018-10-03 10:25:20 +02005577static struct mux_proto_list mux_proto_h2_htx =
5578 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5579
5580INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5581
Willy Tarreau62f52692017-10-08 23:01:42 +02005582/* config keyword parsers */
5583static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005584 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005585 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005586 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005587 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005588 { 0, NULL, NULL }
5589}};
5590
Willy Tarreau0108d902018-11-25 19:14:37 +01005591INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);