blob: 5ae2297a047e8a3674201776ae7c72391f45456f [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
1029 if (h2_settings_header_table_size != 4096) {
1030 char str[6] = "\x00\x01"; /* header_table_size */
1031
1032 write_n32(str + 2, h2_settings_header_table_size);
1033 chunk_memcat(&buf, str, 6);
1034 }
1035
1036 if (h2_settings_initial_window_size != 65535) {
1037 char str[6] = "\x00\x04"; /* initial_window_size */
1038
1039 write_n32(str + 2, h2_settings_initial_window_size);
1040 chunk_memcat(&buf, str, 6);
1041 }
1042
1043 if (h2_settings_max_concurrent_streams != 0) {
1044 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1045
1046 /* Note: 0 means "unlimited" for haproxy's config but not for
1047 * the protocol, so never send this value!
1048 */
1049 write_n32(str + 2, h2_settings_max_concurrent_streams);
1050 chunk_memcat(&buf, str, 6);
1051 }
1052
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001053 mfs = h2_settings_max_frame_size;
1054 if (mfs > global.tune.bufsize)
1055 mfs = global.tune.bufsize;
1056
1057 if (!mfs)
1058 mfs = global.tune.bufsize;
1059
1060 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001061 char str[6] = "\x00\x05"; /* max_frame_size */
1062
1063 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1064 * match bufsize - rewrite size, but at the moment it seems
1065 * that clients don't take care of it.
1066 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001067 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001068 chunk_memcat(&buf, str, 6);
1069 }
1070
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001071 h2_set_frame_size(buf.area, buf.data - 9);
1072 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001073 if (unlikely(ret <= 0)) {
1074 if (!ret) {
1075 h2c->flags |= H2_CF_MUX_MFULL;
1076 h2c->flags |= H2_CF_DEM_MROOM;
1077 return 0;
1078 }
1079 else {
1080 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1081 return 0;
1082 }
1083 }
1084 return ret;
1085}
1086
Willy Tarreau52eed752017-09-22 15:05:09 +02001087/* Try to receive a connection preface, then upon success try to send our
1088 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1089 * missing data. It may return an error in h2c.
1090 */
1091static int h2c_frt_recv_preface(struct h2c *h2c)
1092{
1093 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001094 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001095
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001096 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001097
1098 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001099 if (ret1 < 0)
1100 sess_log(h2c->conn->owner);
1101
Willy Tarreau52eed752017-09-22 15:05:09 +02001102 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1103 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1104 return 0;
1105 }
1106
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001107 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001108 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001109 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001110
Willy Tarreaube5b7152017-09-25 16:25:39 +02001111 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001112}
1113
Willy Tarreau01b44822018-10-03 14:26:37 +02001114/* Try to send a connection preface, then upon success try to send our
1115 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1116 * missing data. It may return an error in h2c.
1117 */
1118static int h2c_bck_send_preface(struct h2c *h2c)
1119{
1120 struct buffer *res;
1121
1122 if (h2c_mux_busy(h2c, NULL)) {
1123 h2c->flags |= H2_CF_DEM_MBUSY;
1124 return 0;
1125 }
1126
1127 res = h2_get_buf(h2c, &h2c->mbuf);
1128 if (!res) {
1129 h2c->flags |= H2_CF_MUX_MALLOC;
1130 h2c->flags |= H2_CF_DEM_MROOM;
1131 return 0;
1132 }
1133
1134 if (!b_data(res)) {
1135 /* preface not yet sent */
1136 b_istput(res, ist(H2_CONN_PREFACE));
1137 }
1138
1139 return h2c_send_settings(h2c);
1140}
1141
Willy Tarreau081d4722017-05-16 21:51:05 +02001142/* try to send a GOAWAY frame on the connection to report an error or a graceful
1143 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1144 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1145 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1146 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1147 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1148 * on unrecoverable failure. It will not attempt to send one again in this last
1149 * case so that it is safe to use h2c_error() to report such errors.
1150 */
1151static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1152{
1153 struct buffer *res;
1154 char str[17];
1155 int ret;
1156
1157 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1158 return 1; // claim that it worked
1159
1160 if (h2c_mux_busy(h2c, h2s)) {
1161 if (h2s)
1162 h2s->flags |= H2_SF_BLK_MBUSY;
1163 else
1164 h2c->flags |= H2_CF_DEM_MBUSY;
1165 return 0;
1166 }
1167
Willy Tarreau44e973f2018-03-01 17:49:30 +01001168 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001169 if (!res) {
1170 h2c->flags |= H2_CF_MUX_MALLOC;
1171 if (h2s)
1172 h2s->flags |= H2_SF_BLK_MROOM;
1173 else
1174 h2c->flags |= H2_CF_DEM_MROOM;
1175 return 0;
1176 }
1177
1178 /* len: 8, type: 7, flags: none, sid: 0 */
1179 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1180
1181 if (h2c->last_sid < 0)
1182 h2c->last_sid = h2c->max_id;
1183
1184 write_n32(str + 9, h2c->last_sid);
1185 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001186 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001187 if (unlikely(ret <= 0)) {
1188 if (!ret) {
1189 h2c->flags |= H2_CF_MUX_MFULL;
1190 if (h2s)
1191 h2s->flags |= H2_SF_BLK_MROOM;
1192 else
1193 h2c->flags |= H2_CF_DEM_MROOM;
1194 return 0;
1195 }
1196 else {
1197 /* we cannot report this error using GOAWAY, so we mark
1198 * it and claim a success.
1199 */
1200 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1201 h2c->flags |= H2_CF_GOAWAY_FAILED;
1202 return 1;
1203 }
1204 }
1205 h2c->flags |= H2_CF_GOAWAY_SENT;
1206 return ret;
1207}
1208
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001209/* Try to send an RST_STREAM frame on the connection for the indicated stream
1210 * during mux operations. This stream must be valid and cannot be closed
1211 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1212 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1213 * not yet.
1214 *
1215 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1216 * to write the message, it subscribes the stream to future notifications.
1217 */
1218static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1219{
1220 struct buffer *res;
1221 char str[13];
1222 int ret;
1223
1224 if (!h2s || h2s->st == H2_SS_CLOSED)
1225 return 1;
1226
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001227 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1228 * RST_STREAM in response to a RST_STREAM frame.
1229 */
1230 if (h2c->dft == H2_FT_RST_STREAM) {
1231 ret = 1;
1232 goto ignore;
1233 }
1234
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001235 if (h2c_mux_busy(h2c, h2s)) {
1236 h2s->flags |= H2_SF_BLK_MBUSY;
1237 return 0;
1238 }
1239
Willy Tarreau44e973f2018-03-01 17:49:30 +01001240 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001241 if (!res) {
1242 h2c->flags |= H2_CF_MUX_MALLOC;
1243 h2s->flags |= H2_SF_BLK_MROOM;
1244 return 0;
1245 }
1246
1247 /* len: 4, type: 3, flags: none */
1248 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1249 write_n32(str + 5, h2s->id);
1250 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001251 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001252
1253 if (unlikely(ret <= 0)) {
1254 if (!ret) {
1255 h2c->flags |= H2_CF_MUX_MFULL;
1256 h2s->flags |= H2_SF_BLK_MROOM;
1257 return 0;
1258 }
1259 else {
1260 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1261 return 0;
1262 }
1263 }
1264
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001265 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001266 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001267 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001268 return ret;
1269}
1270
1271/* Try to send an RST_STREAM frame on the connection for the stream being
1272 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001273 * error code, even if the stream is one of the dummy ones, and will update
1274 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001275 *
1276 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1277 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001278 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001279 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001280 */
1281static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1282{
1283 struct buffer *res;
1284 char str[13];
1285 int ret;
1286
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001287 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1288 * RST_STREAM in response to a RST_STREAM frame.
1289 */
1290 if (h2c->dft == H2_FT_RST_STREAM) {
1291 ret = 1;
1292 goto ignore;
1293 }
1294
Willy Tarreau27a84c92017-10-17 08:10:17 +02001295 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001296 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001297 return 0;
1298 }
1299
Willy Tarreau44e973f2018-03-01 17:49:30 +01001300 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001301 if (!res) {
1302 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001304 return 0;
1305 }
1306
1307 /* len: 4, type: 3, flags: none */
1308 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001309
Willy Tarreau27a84c92017-10-17 08:10:17 +02001310 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001311 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001312 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313
Willy Tarreau27a84c92017-10-17 08:10:17 +02001314 if (unlikely(ret <= 0)) {
1315 if (!ret) {
1316 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001317 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 return 0;
1319 }
1320 else {
1321 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1322 return 0;
1323 }
1324 }
1325
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001326 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001327 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001328 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001329 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001330 }
1331
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 return ret;
1333}
1334
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001335/* try to send an empty DATA frame with the ES flag set to notify about the
1336 * end of stream and match a shutdown(write). If an ES was already sent as
1337 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1338 * on success or zero if nothing was done. In case of lack of room to write the
1339 * message, it subscribes the requesting stream to future notifications.
1340 */
1341static int h2_send_empty_data_es(struct h2s *h2s)
1342{
1343 struct h2c *h2c = h2s->h2c;
1344 struct buffer *res;
1345 char str[9];
1346 int ret;
1347
Willy Tarreau721c9742017-11-07 11:05:42 +01001348 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001349 return 1;
1350
1351 if (h2c_mux_busy(h2c, h2s)) {
1352 h2s->flags |= H2_SF_BLK_MBUSY;
1353 return 0;
1354 }
1355
Willy Tarreau44e973f2018-03-01 17:49:30 +01001356 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001357 if (!res) {
1358 h2c->flags |= H2_CF_MUX_MALLOC;
1359 h2s->flags |= H2_SF_BLK_MROOM;
1360 return 0;
1361 }
1362
1363 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1364 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1365 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001366 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001367 if (likely(ret > 0)) {
1368 h2s->flags |= H2_SF_ES_SENT;
1369 }
1370 else if (!ret) {
1371 h2c->flags |= H2_CF_MUX_MFULL;
1372 h2s->flags |= H2_SF_BLK_MROOM;
1373 return 0;
1374 }
1375 else {
1376 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1377 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001378 }
1379 return ret;
1380}
1381
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001382/* wake the streams attached to the connection, whose id is greater than <last>,
1383 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001384 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1385 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001386 */
1387static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1388{
1389 struct eb32_node *node;
1390 struct h2s *h2s;
1391
1392 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001393 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001394
1395 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001396 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001397
1398 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1399 while (node) {
1400 h2s = container_of(node, struct h2s, by_id);
1401 if (h2s->id <= last)
1402 break;
1403 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001404
1405 if (!h2s->cs) {
1406 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001407 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001408 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001409 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001410
1411 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001412 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1413 h2s->cs->flags |= CS_FL_ERROR;
1414
Willy Tarreauf830f012018-12-19 17:44:55 +01001415 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001416
Willy Tarreaua8519352018-12-18 16:44:28 +01001417 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001418 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001419 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001420 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001421 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001422 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001423 }
1424}
1425
Willy Tarreau3421aba2017-07-27 15:41:03 +02001426/* Increase all streams' outgoing window size by the difference passed in
1427 * argument. This is needed upon receipt of the settings frame if the initial
1428 * window size is different. The difference may be negative and the resulting
1429 * window size as well, for the time it takes to receive some window updates.
1430 */
1431static void h2c_update_all_ws(struct h2c *h2c, int diff)
1432{
1433 struct h2s *h2s;
1434 struct eb32_node *node;
1435
1436 if (!diff)
1437 return;
1438
1439 node = eb32_first(&h2c->streams_by_id);
1440 while (node) {
1441 h2s = container_of(node, struct h2s, by_id);
1442 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001443
1444 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1445 h2s->flags &= ~H2_SF_BLK_SFCTL;
1446 if (h2s->send_wait)
1447 LIST_ADDQ(&h2c->send_list, &h2s->list);
1448
1449 }
1450
Willy Tarreau3421aba2017-07-27 15:41:03 +02001451 node = eb32_next(node);
1452 }
1453}
1454
1455/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1456 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001457 * return an error in h2c. The caller must have already verified frame length
1458 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001459 */
1460static int h2c_handle_settings(struct h2c *h2c)
1461{
1462 unsigned int offset;
1463 int error;
1464
1465 if (h2c->dff & H2_F_SETTINGS_ACK) {
1466 if (h2c->dfl) {
1467 error = H2_ERR_FRAME_SIZE_ERROR;
1468 goto fail;
1469 }
1470 return 1;
1471 }
1472
Willy Tarreau3421aba2017-07-27 15:41:03 +02001473 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001474 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001475 return 0;
1476
1477 /* parse the frame */
1478 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001479 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1480 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481
1482 switch (type) {
1483 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1484 /* we need to update all existing streams with the
1485 * difference from the previous iws.
1486 */
1487 if (arg < 0) { // RFC7540#6.5.2
1488 error = H2_ERR_FLOW_CONTROL_ERROR;
1489 goto fail;
1490 }
1491 h2c_update_all_ws(h2c, arg - h2c->miw);
1492 h2c->miw = arg;
1493 break;
1494 case H2_SETTINGS_MAX_FRAME_SIZE:
1495 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1496 error = H2_ERR_PROTOCOL_ERROR;
1497 goto fail;
1498 }
1499 h2c->mfs = arg;
1500 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001501 case H2_SETTINGS_ENABLE_PUSH:
1502 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1503 error = H2_ERR_PROTOCOL_ERROR;
1504 goto fail;
1505 }
1506 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001507 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1508 if (h2c->flags & H2_CF_IS_BACK) {
1509 /* the limit is only for the backend; for the frontend it is our limit */
1510 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1511 arg = h2_settings_max_concurrent_streams;
1512 h2c->streams_limit = arg;
1513 }
1514 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001515 }
1516 }
1517
1518 /* need to ACK this frame now */
1519 h2c->st0 = H2_CS_FRAME_A;
1520 return 1;
1521 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001522 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001523 h2c_error(h2c, error);
1524 return 0;
1525}
1526
1527/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1528 * success or one of the h2_status values.
1529 */
1530static int h2c_ack_settings(struct h2c *h2c)
1531{
1532 struct buffer *res;
1533 char str[9];
1534 int ret = -1;
1535
1536 if (h2c_mux_busy(h2c, NULL)) {
1537 h2c->flags |= H2_CF_DEM_MBUSY;
1538 return 0;
1539 }
1540
Willy Tarreau44e973f2018-03-01 17:49:30 +01001541 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001542 if (!res) {
1543 h2c->flags |= H2_CF_MUX_MALLOC;
1544 h2c->flags |= H2_CF_DEM_MROOM;
1545 return 0;
1546 }
1547
1548 memcpy(str,
1549 "\x00\x00\x00" /* length : 0 (no data) */
1550 "\x04" "\x01" /* type : 4, flags : ACK */
1551 "\x00\x00\x00\x00" /* stream ID */, 9);
1552
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001553 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001554 if (unlikely(ret <= 0)) {
1555 if (!ret) {
1556 h2c->flags |= H2_CF_MUX_MFULL;
1557 h2c->flags |= H2_CF_DEM_MROOM;
1558 return 0;
1559 }
1560 else {
1561 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1562 return 0;
1563 }
1564 }
1565 return ret;
1566}
1567
Willy Tarreaucf68c782017-10-10 17:11:41 +02001568/* processes a PING frame and schedules an ACK if needed. The caller must pass
1569 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001570 * missing data. The caller must have already verified frame length
1571 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001572 */
1573static int h2c_handle_ping(struct h2c *h2c)
1574{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001575 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001576 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001577 h2c->st0 = H2_CS_FRAME_A;
1578 return 1;
1579}
1580
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001581/* Try to send a window update for stream id <sid> and value <increment>.
1582 * Returns > 0 on success or zero on missing room or failure. It may return an
1583 * error in h2c.
1584 */
1585static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1586{
1587 struct buffer *res;
1588 char str[13];
1589 int ret = -1;
1590
1591 if (h2c_mux_busy(h2c, NULL)) {
1592 h2c->flags |= H2_CF_DEM_MBUSY;
1593 return 0;
1594 }
1595
Willy Tarreau44e973f2018-03-01 17:49:30 +01001596 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001597 if (!res) {
1598 h2c->flags |= H2_CF_MUX_MALLOC;
1599 h2c->flags |= H2_CF_DEM_MROOM;
1600 return 0;
1601 }
1602
1603 /* length: 4, type: 8, flags: none */
1604 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1605 write_n32(str + 5, sid);
1606 write_n32(str + 9, increment);
1607
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001608 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001609
1610 if (unlikely(ret <= 0)) {
1611 if (!ret) {
1612 h2c->flags |= H2_CF_MUX_MFULL;
1613 h2c->flags |= H2_CF_DEM_MROOM;
1614 return 0;
1615 }
1616 else {
1617 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1618 return 0;
1619 }
1620 }
1621 return ret;
1622}
1623
1624/* try to send pending window update for the connection. It's safe to call it
1625 * with no pending updates. Returns > 0 on success or zero on missing room or
1626 * failure. It may return an error in h2c.
1627 */
1628static int h2c_send_conn_wu(struct h2c *h2c)
1629{
1630 int ret = 1;
1631
1632 if (h2c->rcvd_c <= 0)
1633 return 1;
1634
Willy Tarreau97aaa672018-12-23 09:49:04 +01001635 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1636 /* increase the advertised connection window to 2G on
1637 * first update.
1638 */
1639 h2c->flags |= H2_CF_WINDOW_OPENED;
1640 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1641 }
1642
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001643 /* send WU for the connection */
1644 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1645 if (ret > 0)
1646 h2c->rcvd_c = 0;
1647
1648 return ret;
1649}
1650
1651/* try to send pending window update for the current dmux stream. It's safe to
1652 * call it with no pending updates. Returns > 0 on success or zero on missing
1653 * room or failure. It may return an error in h2c.
1654 */
1655static int h2c_send_strm_wu(struct h2c *h2c)
1656{
1657 int ret = 1;
1658
1659 if (h2c->rcvd_s <= 0)
1660 return 1;
1661
1662 /* send WU for the stream */
1663 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1664 if (ret > 0)
1665 h2c->rcvd_s = 0;
1666
1667 return ret;
1668}
1669
Willy Tarreaucf68c782017-10-10 17:11:41 +02001670/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1671 * success, 0 on missing data or one of the h2_status values.
1672 */
1673static int h2c_ack_ping(struct h2c *h2c)
1674{
1675 struct buffer *res;
1676 char str[17];
1677 int ret = -1;
1678
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001679 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001680 return 0;
1681
1682 if (h2c_mux_busy(h2c, NULL)) {
1683 h2c->flags |= H2_CF_DEM_MBUSY;
1684 return 0;
1685 }
1686
Willy Tarreau44e973f2018-03-01 17:49:30 +01001687 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001688 if (!res) {
1689 h2c->flags |= H2_CF_MUX_MALLOC;
1690 h2c->flags |= H2_CF_DEM_MROOM;
1691 return 0;
1692 }
1693
1694 memcpy(str,
1695 "\x00\x00\x08" /* length : 8 (same payload) */
1696 "\x06" "\x01" /* type : 6, flags : ACK */
1697 "\x00\x00\x00\x00" /* stream ID */, 9);
1698
1699 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001700 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001701
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001702 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001703 if (unlikely(ret <= 0)) {
1704 if (!ret) {
1705 h2c->flags |= H2_CF_MUX_MFULL;
1706 h2c->flags |= H2_CF_DEM_MROOM;
1707 return 0;
1708 }
1709 else {
1710 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1711 return 0;
1712 }
1713 }
1714 return ret;
1715}
1716
Willy Tarreau26f95952017-07-27 17:18:30 +02001717/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1718 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001719 * h2c or h2s. The caller must have already verified frame length and stream ID
1720 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001721 */
1722static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1723{
1724 int32_t inc;
1725 int error;
1726
Willy Tarreau26f95952017-07-27 17:18:30 +02001727 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001728 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001729 return 0;
1730
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001731 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001732
1733 if (h2c->dsi != 0) {
1734 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001735
1736 /* it's not an error to receive WU on a closed stream */
1737 if (h2s->st == H2_SS_CLOSED)
1738 return 1;
1739
1740 if (!inc) {
1741 error = H2_ERR_PROTOCOL_ERROR;
1742 goto strm_err;
1743 }
1744
1745 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1746 error = H2_ERR_FLOW_CONTROL_ERROR;
1747 goto strm_err;
1748 }
1749
1750 h2s->mws += inc;
1751 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1752 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001753 if (h2s->send_wait)
1754 LIST_ADDQ(&h2c->send_list, &h2s->list);
1755
Willy Tarreau26f95952017-07-27 17:18:30 +02001756 }
1757 }
1758 else {
1759 /* connection window update */
1760 if (!inc) {
1761 error = H2_ERR_PROTOCOL_ERROR;
1762 goto conn_err;
1763 }
1764
1765 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1766 error = H2_ERR_FLOW_CONTROL_ERROR;
1767 goto conn_err;
1768 }
1769
1770 h2c->mws += inc;
1771 }
1772
1773 return 1;
1774
1775 conn_err:
1776 h2c_error(h2c, error);
1777 return 0;
1778
1779 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001780 h2s_error(h2s, error);
1781 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001782 return 0;
1783}
1784
Willy Tarreaue96b0922017-10-30 00:28:29 +01001785/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001786 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1787 * have already verified frame length and stream ID validity. Described in
1788 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001789 */
1790static int h2c_handle_goaway(struct h2c *h2c)
1791{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001792 int last;
1793
Willy Tarreaue96b0922017-10-30 00:28:29 +01001794 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001795 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001796 return 0;
1797
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001798 last = h2_get_n32(&h2c->dbuf, 0);
1799 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001800 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001801 if (h2c->last_sid < 0)
1802 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001803 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001804}
1805
Willy Tarreau92153fc2017-12-03 19:46:19 +01001806/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001807 * invalid. Returns > 0 on success or zero on missing data. It may return an
1808 * error in h2c. The caller must have already verified frame length and stream
1809 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001810 */
1811static int h2c_handle_priority(struct h2c *h2c)
1812{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001813 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001814 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001815 return 0;
1816
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001817 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001818 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001819 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1820 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001821 }
1822 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001823}
1824
Willy Tarreaucd234e92017-08-18 10:59:39 +02001825/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001826 * Returns > 0 on success or zero on missing data. The caller must have already
1827 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001828 */
1829static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1830{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001831 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001832 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001833 return 0;
1834
1835 /* late RST, already handled */
1836 if (h2s->st == H2_SS_CLOSED)
1837 return 1;
1838
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001839 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001840 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001841
1842 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001843 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001844 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001845 }
1846
1847 h2s->flags |= H2_SF_RST_RCVD;
1848 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001849}
1850
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001851/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1852 * It may return an error in h2c or h2s. The caller must consider that the
1853 * return value is the new h2s in case one was allocated (most common case).
1854 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001855 * errors here are reported as connection errors since it's impossible to
1856 * recover from such errors after the compression context has been altered.
1857 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001858static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001859{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001860 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001861 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001862 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001863 int error;
1864
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001865 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001866 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001867
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001868 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001869 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001870
1871 /* now either the frame is complete or the buffer is complete */
1872 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001873 /* The stream exists/existed, this must be a trailers frame */
1874 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001875 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001876 goto out;
1877 goto done;
1878 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001879 /* the connection was already killed by an RST, let's consume
1880 * the data and send another RST.
1881 */
1882 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1883 h2s = (struct h2s*)h2_error_stream;
1884 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001885 }
1886 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1887 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1888 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001889 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001890 goto conn_err;
1891 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001892 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1893 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001894
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001895 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001896
Willy Tarreau25919232019-01-03 14:48:18 +01001897 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001898 if (h2c->st0 >= H2_CS_ERROR)
1899 goto out;
1900
Willy Tarreau25919232019-01-03 14:48:18 +01001901 if (error <= 0) {
1902 if (error == 0)
1903 goto out; // missing data
1904
1905 /* Failed to decode this stream (e.g. too large request)
1906 * but the HPACK decompressor is still synchronized.
1907 */
1908 h2s = (struct h2s*)h2_error_stream;
1909 goto send_rst;
1910 }
1911
Willy Tarreau22de8d32018-09-05 19:55:58 +02001912 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001913 * positively from h2c_frt_stream_new(), the stream will report the error,
1914 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001915 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001916 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001917 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001918 h2s = (struct h2s*)h2_refused_stream;
1919 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001920 }
1921
1922 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001923 h2s->rxbuf = rxbuf;
1924 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001925 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001926
Willy Tarreau88d138e2019-01-02 19:38:14 +01001927 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001928 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001929 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001930
1931 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001932 if (h2s->st == H2_SS_OPEN)
1933 h2s->st = H2_SS_HREM;
1934 else
1935 h2s_close(h2s);
Willy Tarreau39d68502018-03-02 12:26:37 +01001936 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001937 }
1938
Willy Tarreau3a429f02019-01-03 11:41:50 +01001939 /* update the max stream ID if the request is being processed */
1940 if (h2s->id > h2c->max_id)
1941 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001942
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001943 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001944
1945 conn_err:
1946 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001947 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001948
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001949 out:
1950 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001951 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001952
1953 send_rst:
1954 /* make the demux send an RST for the current stream. We may only
1955 * do this if we're certain that the HEADERS frame was properly
1956 * decompressed so that the HPACK decoder is still kept up to date.
1957 */
1958 h2_release_buf(h2c, &rxbuf);
1959 h2c->st0 = H2_CS_FRAME_E;
1960 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001961}
1962
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001963/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1964 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1965 * errors here are reported as connection errors since it's impossible to
1966 * recover from such errors after the compression context has been altered.
1967 */
1968static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1969{
1970 int error;
1971
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001972 if (!b_size(&h2c->dbuf))
1973 return NULL; // empty buffer
1974
1975 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1976 return NULL; // incomplete frame
1977
Willy Tarreau1915ca22019-01-24 11:49:37 +01001978 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001979
Willy Tarreau25919232019-01-03 14:48:18 +01001980 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001981 if (h2c->st0 >= H2_CS_ERROR)
1982 return NULL;
1983
Willy Tarreau08bb1d62019-01-30 16:55:48 +01001984 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1985 /* RFC7540#5.1 */
1986 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1987 h2c->st0 = H2_CS_FRAME_E;
1988 return NULL;
1989 }
1990
Willy Tarreau25919232019-01-03 14:48:18 +01001991 if (error <= 0) {
1992 if (error == 0)
1993 return NULL; // missing data
1994
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001995 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01001996 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001997 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01001998 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001999 }
2000
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002001 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2002 h2s->flags |= H2_SF_ES_RCVD;
2003 h2s->cs->flags |= CS_FL_REOS;
2004 }
2005
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002006 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2007 h2s->st = H2_SS_ERROR;
2008 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2009 h2s->st = H2_SS_HREM;
2010 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2011 h2s_close(h2s);
2012
2013 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002014}
2015
Willy Tarreau454f9052017-10-26 19:40:35 +02002016/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2017 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2018 */
2019static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2020{
2021 int error;
2022
2023 /* note that empty DATA frames are perfectly valid and sometimes used
2024 * to signal an end of stream (with the ES flag).
2025 */
2026
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002027 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002028 return 0; // empty buffer
2029
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002030 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002031 return 0; // incomplete frame
2032
2033 /* now either the frame is complete or the buffer is complete */
2034
Willy Tarreau454f9052017-10-26 19:40:35 +02002035 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2036 /* RFC7540#6.1 */
2037 error = H2_ERR_STREAM_CLOSED;
2038 goto strm_err;
2039 }
2040
Willy Tarreau1915ca22019-01-24 11:49:37 +01002041 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2042 /* RFC7540#8.1.2 */
2043 error = H2_ERR_PROTOCOL_ERROR;
2044 goto strm_err;
2045 }
2046
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002047 if (!h2_frt_transfer_data(h2s))
2048 return 0;
2049
Willy Tarreau454f9052017-10-26 19:40:35 +02002050 /* call the upper layers to process the frame, then let the upper layer
2051 * notify the stream about any change.
2052 */
2053 if (!h2s->cs) {
2054 error = H2_ERR_STREAM_CLOSED;
2055 goto strm_err;
2056 }
2057
Willy Tarreau8f650c32017-11-21 19:36:21 +01002058 if (h2c->st0 >= H2_CS_ERROR)
2059 return 0;
2060
Willy Tarreau721c9742017-11-07 11:05:42 +01002061 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002062 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002063 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002064 }
2065
2066 /* check for completion : the callee will change this to FRAME_A or
2067 * FRAME_H once done.
2068 */
2069 if (h2c->st0 == H2_CS_FRAME_P)
2070 return 0;
2071
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002072 /* last frame */
2073 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002074 if (h2s->st == H2_SS_OPEN)
2075 h2s->st = H2_SS_HREM;
2076 else
2077 h2s_close(h2s);
2078
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002079 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002080 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002081
2082 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2083 /* RFC7540#8.1.2 */
2084 error = H2_ERR_PROTOCOL_ERROR;
2085 goto strm_err;
2086 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002087 }
2088
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 return 1;
2090
Willy Tarreau454f9052017-10-26 19:40:35 +02002091 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002092 h2s_error(h2s, error);
2093 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002094 return 0;
2095}
2096
Willy Tarreaubc933932017-10-09 16:21:43 +02002097/* process Rx frames to be demultiplexed */
2098static void h2_process_demux(struct h2c *h2c)
2099{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002100 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002101 struct h2_fh hdr;
2102 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002103
Willy Tarreau081d4722017-05-16 21:51:05 +02002104 if (h2c->st0 >= H2_CS_ERROR)
2105 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002106
2107 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2108 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002109 if (h2c->flags & H2_CF_IS_BACK)
2110 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002111 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2112 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002113 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002114 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002115 sess_log(h2c->conn->owner);
2116 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002117 goto fail;
2118 }
2119
2120 h2c->max_id = 0;
2121 h2c->st0 = H2_CS_SETTINGS1;
2122 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002123
2124 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002125 /* ensure that what is pending is a valid SETTINGS frame
2126 * without an ACK.
2127 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002128 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002129 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002130 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002131 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002132 sess_log(h2c->conn->owner);
2133 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002134 goto fail;
2135 }
2136
2137 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2138 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2139 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2140 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002141 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002142 goto fail;
2143 }
2144
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002145 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002146 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2147 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2148 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002149 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002150 goto fail;
2151 }
2152
Willy Tarreau3bf69182018-12-21 15:34:50 +01002153 /* that's OK, switch to FRAME_P to process it. This is
2154 * a SETTINGS frame whose header has already been
2155 * deleted above.
2156 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002157 padlen = 0;
2158 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002159 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002160 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002161
2162 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002163 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002164 int ret = 0;
2165
2166 if (h2c->st0 >= H2_CS_ERROR)
2167 break;
2168
2169 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002170 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002171 break;
2172
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002173 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002174 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002175 if (!h2c->nb_streams) {
2176 /* only log if no other stream can report the error */
2177 sess_log(h2c->conn->owner);
2178 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002179 break;
2180 }
2181
Willy Tarreau3bf69182018-12-21 15:34:50 +01002182 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2183 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2184 * we read the pad length and drop it from the remaining
2185 * payload (one byte + the 9 remaining ones = 10 total
2186 * removed), so we have a frame payload starting after the
2187 * pad len. Flow controlled frames (DATA) also count the
2188 * padlen in the flow control, so it must be adjusted.
2189 */
2190 if (hdr.len < 1) {
2191 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2192 sess_log(h2c->conn->owner);
2193 goto fail;
2194 }
2195 hdr.len--;
2196
2197 if (b_data(&h2c->dbuf) < 10)
2198 break; // missing padlen
2199
2200 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2201
2202 if (padlen > hdr.len) {
2203 /* RFC7540#6.1 : pad length = length of
2204 * frame payload or greater => error.
2205 */
2206 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2207 sess_log(h2c->conn->owner);
2208 goto fail;
2209 }
2210
2211 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2212 h2c->rcvd_c++;
2213 h2c->rcvd_s++;
2214 }
2215 b_del(&h2c->dbuf, 1);
2216 }
2217 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002218
2219 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 h2c->dfl = hdr.len;
2221 h2c->dsi = hdr.sid;
2222 h2c->dft = hdr.ft;
2223 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002224 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002225 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002226
2227 /* check for minimum basic frame format validity */
2228 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2229 if (ret != H2_ERR_NO_ERROR) {
2230 h2c_error(h2c, ret);
2231 sess_log(h2c->conn->owner);
2232 goto fail;
2233 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002234 }
2235
2236 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002237 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2238
Willy Tarreau567beb82018-12-18 16:52:44 +01002239 if (tmp_h2s != h2s && h2s && h2s->cs &&
2240 (b_data(&h2s->rxbuf) ||
2241 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002242 /* we may have to signal the upper layers */
2243 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002244 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002245 }
2246 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002247
Willy Tarreaud7901432017-12-29 11:34:40 +01002248 if (h2c->st0 == H2_CS_FRAME_E)
2249 goto strm_err;
2250
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002251 if (h2s->st == H2_SS_IDLE &&
2252 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2253 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2254 * this state MUST be treated as a connection error
2255 */
2256 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002257 if (!h2c->nb_streams) {
2258 /* only log if no other stream can report the error */
2259 sess_log(h2c->conn->owner);
2260 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002261 break;
2262 }
2263
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002264 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2265 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2266 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002267 * this state MUST be treated as a stream error.
2268 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2269 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002270 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002271 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2272 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2273 else
2274 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002275 goto strm_err;
2276 }
2277
Willy Tarreauab837502017-12-27 15:07:30 +01002278 /* Below the management of frames received in closed state is a
2279 * bit hackish because the spec makes strong differences between
2280 * streams closed by receiving RST, sending RST, and seeing ES
2281 * in both directions. In addition to this, the creation of a
2282 * new stream reusing the identifier of a closed one will be
2283 * detected here. Given that we cannot keep track of all closed
2284 * streams forever, we consider that unknown closed streams were
2285 * closed on RST received, which allows us to respond with an
2286 * RST without breaking the connection (eg: to abort a transfer).
2287 * Some frames have to be silently ignored as well.
2288 */
2289 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002290 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002291 /* #5.1.1: The identifier of a newly
2292 * established stream MUST be numerically
2293 * greater than all streams that the initiating
2294 * endpoint has opened or reserved. This
2295 * governs streams that are opened using a
2296 * HEADERS frame and streams that are reserved
2297 * using PUSH_PROMISE. An endpoint that
2298 * receives an unexpected stream identifier
2299 * MUST respond with a connection error.
2300 */
2301 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2302 goto strm_err;
2303 }
2304
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002305 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002306 /* RFC7540#5.1:closed: an endpoint that
2307 * receives any frame other than PRIORITY after
2308 * receiving a RST_STREAM MUST treat that as a
2309 * stream error of type STREAM_CLOSED.
2310 *
2311 * Note that old streams fall into this category
2312 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002313 *
2314 * However, we cannot generalize this to all frame types. Those
2315 * carrying compression state must still be processed before
2316 * being dropped or we'll desynchronize the decoder. This can
2317 * happen with request trailers received after sending an
2318 * RST_STREAM, or with header/trailers responses received after
2319 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002320 */
2321 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2322 h2c->st0 = H2_CS_FRAME_E;
2323 goto strm_err;
2324 }
2325
2326 /* RFC7540#5.1:closed: if this state is reached as a
2327 * result of sending a RST_STREAM frame, the peer that
2328 * receives the RST_STREAM might have already sent
2329 * frames on the stream that cannot be withdrawn. An
2330 * endpoint MUST ignore frames that it receives on
2331 * closed streams after it has sent a RST_STREAM
2332 * frame. An endpoint MAY choose to limit the period
2333 * over which it ignores frames and treat frames that
2334 * arrive after this time as being in error.
2335 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002336 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002337 /* RFC7540#5.1:closed: any frame other than
2338 * PRIO/WU/RST in this state MUST be treated as
2339 * a connection error
2340 */
2341 if (h2c->dft != H2_FT_RST_STREAM &&
2342 h2c->dft != H2_FT_PRIORITY &&
2343 h2c->dft != H2_FT_WINDOW_UPDATE) {
2344 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2345 goto strm_err;
2346 }
2347 }
2348 }
2349
Willy Tarreauc0da1962017-10-30 18:38:00 +01002350#if 0
2351 // problem below: it is not possible to completely ignore such
2352 // streams as we need to maintain the compression state as well
2353 // and for this we need to completely process these frames (eg:
2354 // HEADERS frames) as well as counting DATA frames to emit
2355 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2356 // This is a typical case of layer violation where the
2357 // transported contents are critical to the connection's
2358 // validity and must be ignored at the same time :-(
2359
2360 /* graceful shutdown, ignore streams whose ID is higher than
2361 * the one advertised in GOAWAY. RFC7540#6.8.
2362 */
2363 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002364 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2365 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002366 h2c->dfl -= ret;
2367 ret = h2c->dfl == 0;
2368 goto strm_err;
2369 }
2370#endif
2371
Willy Tarreau7e98c052017-10-10 15:56:59 +02002372 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002373 case H2_FT_SETTINGS:
2374 if (h2c->st0 == H2_CS_FRAME_P)
2375 ret = h2c_handle_settings(h2c);
2376
2377 if (h2c->st0 == H2_CS_FRAME_A)
2378 ret = h2c_ack_settings(h2c);
2379 break;
2380
Willy Tarreaucf68c782017-10-10 17:11:41 +02002381 case H2_FT_PING:
2382 if (h2c->st0 == H2_CS_FRAME_P)
2383 ret = h2c_handle_ping(h2c);
2384
2385 if (h2c->st0 == H2_CS_FRAME_A)
2386 ret = h2c_ack_ping(h2c);
2387 break;
2388
Willy Tarreau26f95952017-07-27 17:18:30 +02002389 case H2_FT_WINDOW_UPDATE:
2390 if (h2c->st0 == H2_CS_FRAME_P)
2391 ret = h2c_handle_window_update(h2c, h2s);
2392 break;
2393
Willy Tarreau61290ec2017-10-17 08:19:21 +02002394 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002395 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2396 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2397 * frames' parsers consume all following CONTINUATION
2398 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002399 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002400 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2401 sess_log(h2c->conn->owner);
2402 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002403
Willy Tarreau13278b42017-10-13 19:23:14 +02002404 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002405 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002406 if (h2c->flags & H2_CF_IS_BACK)
2407 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2408 else
2409 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002410 if (tmp_h2s) {
2411 h2s = tmp_h2s;
2412 ret = 1;
2413 }
2414 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002415 break;
2416
Willy Tarreau454f9052017-10-26 19:40:35 +02002417 case H2_FT_DATA:
2418 if (h2c->st0 == H2_CS_FRAME_P)
2419 ret = h2c_frt_handle_data(h2c, h2s);
2420
2421 if (h2c->st0 == H2_CS_FRAME_A)
2422 ret = h2c_send_strm_wu(h2c);
2423 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002424
Willy Tarreau92153fc2017-12-03 19:46:19 +01002425 case H2_FT_PRIORITY:
2426 if (h2c->st0 == H2_CS_FRAME_P)
2427 ret = h2c_handle_priority(h2c);
2428 break;
2429
Willy Tarreaucd234e92017-08-18 10:59:39 +02002430 case H2_FT_RST_STREAM:
2431 if (h2c->st0 == H2_CS_FRAME_P)
2432 ret = h2c_handle_rst_stream(h2c, h2s);
2433 break;
2434
Willy Tarreaue96b0922017-10-30 00:28:29 +01002435 case H2_FT_GOAWAY:
2436 if (h2c->st0 == H2_CS_FRAME_P)
2437 ret = h2c_handle_goaway(h2c);
2438 break;
2439
Willy Tarreau1c661982017-10-30 13:52:01 +01002440 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002441 default:
2442 /* drop frames that we ignore. They may be larger than
2443 * the buffer so we drain all of their contents until
2444 * we reach the end.
2445 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2447 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002448 h2c->dfl -= ret;
2449 ret = h2c->dfl == 0;
2450 }
2451
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002452 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002453 /* We may have to send an RST if not done yet */
2454 if (h2s->st == H2_SS_ERROR)
2455 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002456
Willy Tarreaua20a5192017-12-27 11:02:06 +01002457 if (h2c->st0 == H2_CS_FRAME_E)
2458 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002459
Willy Tarreau7e98c052017-10-10 15:56:59 +02002460 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002461 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002462 break;
2463
2464 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002465 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002466 h2c->st0 = H2_CS_FRAME_H;
2467 }
2468 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002469
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002470 if (h2c->rcvd_c > 0 &&
2471 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2472 h2c_send_conn_wu(h2c);
2473
Willy Tarreau52eed752017-09-22 15:05:09 +02002474 fail:
2475 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002476 if (h2s && h2s->cs &&
2477 (b_data(&h2s->rxbuf) ||
2478 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002479 /* we may have to signal the upper layers */
2480 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002481 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002482 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002483
Willy Tarreau47b515a2018-12-21 16:09:41 +01002484 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002485}
2486
2487/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2488 * the end.
2489 */
2490static int h2_process_mux(struct h2c *h2c)
2491{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002492 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002493
Willy Tarreau01b44822018-10-03 14:26:37 +02002494 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2495 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2496 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2497 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2498 if (h2c->st0 == H2_CS_ERROR) {
2499 h2c->st0 = H2_CS_ERROR2;
2500 sess_log(h2c->conn->owner);
2501 }
2502 goto fail;
2503 }
2504 h2c->st0 = H2_CS_SETTINGS1;
2505 }
2506 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002507 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002508 return 1;
2509 }
2510
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002511 /* start by sending possibly pending window updates */
2512 if (h2c->rcvd_c > 0 &&
2513 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2514 h2c_send_conn_wu(h2c) < 0)
2515 goto fail;
2516
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002517 /* First we always process the flow control list because the streams
2518 * waiting there were already elected for immediate emission but were
2519 * blocked just on this.
2520 */
2521
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002522 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002523 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2524 h2c->st0 >= H2_CS_ERROR)
2525 break;
2526
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002527 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002528 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2529 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002530 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002531 LIST_DEL(&h2s->list);
2532 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002533 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002534 }
2535
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002536 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002537 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2538 break;
2539
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002540 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002541 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2542 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002543 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002544 LIST_DEL(&h2s->list);
2545 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002546 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002547 }
2548
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002549 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002550 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002551 if (h2c->st0 == H2_CS_ERROR) {
2552 if (h2c->max_id >= 0) {
2553 h2c_send_goaway_error(h2c, NULL);
2554 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2555 return 0;
2556 }
2557
2558 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2559 }
2560 return 1;
2561 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002562 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002563}
2564
Willy Tarreau62f52692017-10-08 23:01:42 +02002565
Willy Tarreau479998a2018-11-18 06:30:59 +01002566/* Attempt to read data, and subscribe if none available.
2567 * The function returns 1 if data has been received, otherwise zero.
2568 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002569static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002570{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002571 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002572 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002573 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002574 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002575
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002576 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002577 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002578
Willy Tarreau315d8072017-12-10 22:17:57 +01002579 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002580 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002581
Willy Tarreau44e973f2018-03-01 17:49:30 +01002582 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002583 if (!buf) {
2584 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002585 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002586 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002587
Olivier Houchard7505f942018-08-21 18:10:44 +02002588 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002589 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002590 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2591 /* HTX in use : try to pre-align the buffer like the
2592 * rxbufs will be to optimize memory copies. We'll make
2593 * sure that the frame header lands at the end of the
2594 * HTX block to alias it upon recv. We cannot use the
2595 * head because rcv_buf() will realign the buffer if
2596 * it's empty. Thus we cheat and pretend we already
2597 * have a few bytes there.
2598 */
2599 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002600 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002601 }
2602 else
2603 max = b_room(buf);
2604
Olivier Houchard7505f942018-08-21 18:10:44 +02002605 if (max)
2606 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2607 else
2608 ret = 0;
2609 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002610
Olivier Houchard53216e72018-10-10 15:46:36 +02002611 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002612 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002613
Olivier Houcharda1411e62018-08-17 18:42:48 +02002614 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002615 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002616 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002617 }
2618
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002619 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002620 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002621 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002622}
2623
Willy Tarreau479998a2018-11-18 06:30:59 +01002624/* Try to send data if possible.
2625 * The function returns 1 if data have been sent, otherwise zero.
2626 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002627static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002628{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002629 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002630 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002631 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002632
2633 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002634 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002635
Olivier Houchard7505f942018-08-21 18:10:44 +02002636
Willy Tarreaua2af5122017-10-09 11:56:46 +02002637 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2638 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002639 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002640 }
2641
Willy Tarreaubc933932017-10-09 16:21:43 +02002642 /* This loop is quite simple : it tries to fill as much as it can from
2643 * pending streams into the existing buffer until it's reportedly full
2644 * or the end of send requests is reached. Then it tries to send this
2645 * buffer's contents out, marks it not full if at least one byte could
2646 * be sent, and tries again.
2647 *
2648 * The snd_buf() function normally takes a "flags" argument which may
2649 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2650 * data immediately comes and CO_SFL_STREAMER to indicate that the
2651 * connection is streaming lots of data (used to increase TLS record
2652 * size at the expense of latency). The former can be sent any time
2653 * there's a buffer full flag, as it indicates at least one stream
2654 * attempted to send and failed so there are pending data. An
2655 * alternative would be to set it as long as there's an active stream
2656 * but that would be problematic for ACKs until we have an absolute
2657 * guarantee that all waiters have at least one byte to send. The
2658 * latter should possibly not be set for now.
2659 */
2660
2661 done = 0;
2662 while (!done) {
2663 unsigned int flags = 0;
2664
2665 /* fill as much as we can into the current buffer */
2666 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2667 done = h2_process_mux(h2c);
2668
Olivier Houchard2b094432019-01-29 18:28:36 +01002669 if (h2c->flags & H2_CF_MUX_MALLOC)
2670 break;
2671
Willy Tarreaubc933932017-10-09 16:21:43 +02002672 if (conn->flags & CO_FL_ERROR)
2673 break;
2674
2675 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2676 flags |= CO_SFL_MSG_MORE;
2677
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002678 if (b_data(&h2c->mbuf)) {
2679 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002680 if (!ret)
2681 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002682 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002683 b_del(&h2c->mbuf, ret);
2684 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002685 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002686
2687 /* wrote at least one byte, the buffer is not full anymore */
2688 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2689 }
2690
Willy Tarreaua2af5122017-10-09 11:56:46 +02002691 if (conn->flags & CO_FL_SOCK_WR_SH) {
2692 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002693 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002694 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002695 /* We're not full anymore, so we can wake any task that are waiting
2696 * for us.
2697 */
2698 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002699 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002700 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2701 struct h2s *, list);
2702 LIST_DEL(&h2s->list);
2703 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002704 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002705 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2706 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002707 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002708 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002709 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002710 /* We're done, no more to send */
2711 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002712 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002713schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002714 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2715 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002716 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002717}
2718
2719static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2720{
2721 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002722 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002723
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002724 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002725 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002726 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002727 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002728 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002729 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002730 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002731}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002732
Willy Tarreau62f52692017-10-08 23:01:42 +02002733/* callback called on any event by the connection handler.
2734 * It applies changes and returns zero, or < 0 if it wants immediate
2735 * destruction of the connection (which normally doesn not happen in h2).
2736 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002737static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002738{
Olivier Houchard7505f942018-08-21 18:10:44 +02002739 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002740
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002741 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002742 h2_process_demux(h2c);
2743
2744 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002745 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002746
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002747 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002748 h2c->flags &= ~H2_CF_DEM_DFULL;
2749 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002750 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002751
Willy Tarreau0b37d652018-10-03 10:33:02 +02002752 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002753 /* frontend is stopping, reload likely in progress, let's try
2754 * to announce a graceful shutdown if not yet done. We don't
2755 * care if it fails, it will be tried again later.
2756 */
2757 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2758 if (h2c->last_sid < 0)
2759 h2c->last_sid = (1U << 31) - 1;
2760 h2c_send_goaway_error(h2c, NULL);
2761 }
2762 }
2763
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002764 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002765 * If we received early data, and the handshake is done, wake
2766 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002767 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002768 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2769 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2770 struct eb32_node *node;
2771 struct h2s *h2s;
2772
2773 h2c->flags |= H2_CF_WAIT_FOR_HS;
2774 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2775
2776 while (node) {
2777 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002778 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002779 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002780 node = eb32_next(node);
2781 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002782 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002783
Willy Tarreau26bd7612017-10-09 16:47:04 +02002784 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002785 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2786 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2787 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002788 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002789
2790 if (eb_is_empty(&h2c->streams_by_id)) {
2791 /* no more stream, kill the connection now */
2792 h2_release(conn);
2793 return -1;
2794 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002795 }
2796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002797 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002798 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002799
Olivier Houchard53216e72018-10-10 15:46:36 +02002800 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2801 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2802 (h2c->st0 != H2_CS_ERROR &&
2803 !b_data(&h2c->mbuf) &&
2804 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2805 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002806 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002807
Willy Tarreau3f133572017-10-31 19:21:06 +01002808 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002809 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002810 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002811 task_queue(h2c->task);
2812 }
2813 else
2814 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002815 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002816
Olivier Houchard7505f942018-08-21 18:10:44 +02002817 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002818 return 0;
2819}
2820
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002821static int h2_wake(struct connection *conn)
2822{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002823 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002824
2825 return (h2_process(h2c));
2826}
2827
Willy Tarreauea392822017-10-31 10:02:25 +01002828/* Connection timeout management. The principle is that if there's no receipt
2829 * nor sending for a certain amount of time, the connection is closed. If the
2830 * MUX buffer still has lying data or is not allocatable, the connection is
2831 * immediately killed. If it's allocatable and empty, we attempt to send a
2832 * GOAWAY frame.
2833 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002834static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002835{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002836 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002837 int expired = tick_is_expired(t->expire, now_ms);
2838
Willy Tarreau0975f112018-03-29 15:22:59 +02002839 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002840 return t;
2841
Willy Tarreau0975f112018-03-29 15:22:59 +02002842 task_delete(t);
2843 task_free(t);
2844
2845 if (!h2c) {
2846 /* resources were already deleted */
2847 return NULL;
2848 }
2849
2850 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002851 h2c_error(h2c, H2_ERR_NO_ERROR);
2852 h2_wake_some_streams(h2c, 0, 0);
2853
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002854 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002855 /* don't even try to send a GOAWAY, the buffer is stuck */
2856 h2c->flags |= H2_CF_GOAWAY_FAILED;
2857 }
2858
2859 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002860 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002861 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2862 h2c->flags |= H2_CF_GOAWAY_FAILED;
2863
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002864 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2865 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002866 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002867 b_del(&h2c->mbuf, ret);
2868 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002869 }
2870 }
Willy Tarreauea392822017-10-31 10:02:25 +01002871
Willy Tarreau0975f112018-03-29 15:22:59 +02002872 /* either we can release everything now or it will be done later once
2873 * the last stream closes.
2874 */
2875 if (eb_is_empty(&h2c->streams_by_id))
2876 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002877
Willy Tarreauea392822017-10-31 10:02:25 +01002878 return NULL;
2879}
2880
2881
Willy Tarreau62f52692017-10-08 23:01:42 +02002882/*******************************************/
2883/* functions below are used by the streams */
2884/*******************************************/
2885
2886/*
2887 * Attach a new stream to a connection
2888 * (Used for outgoing connections)
2889 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002890static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002891{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002892 struct conn_stream *cs;
2893 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002894 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002895
2896 cs = cs_new(conn);
2897 if (!cs)
2898 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002899 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002900 if (!h2s) {
2901 cs_free(cs);
2902 return NULL;
2903 }
2904 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002905}
2906
Willy Tarreaufafd3982018-11-18 21:29:20 +01002907/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2908 * We have to scan because we may have some orphan streams. It might be
2909 * beneficial to scan backwards from the end to reduce the likeliness to find
2910 * orphans.
2911 */
2912static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2913{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002914 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002915 struct h2s *h2s;
2916 struct eb32_node *node;
2917
2918 node = eb32_first(&h2c->streams_by_id);
2919 while (node) {
2920 h2s = container_of(node, struct h2s, by_id);
2921 if (h2s->cs)
2922 return h2s->cs;
2923 node = eb32_next(node);
2924 }
2925 return NULL;
2926}
2927
Willy Tarreau62f52692017-10-08 23:01:42 +02002928/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002929 * Destroy the mux and the associated connection, if it is no longer used
2930 */
2931static void h2_destroy(struct connection *conn)
2932{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002933 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002934
2935 if (eb_is_empty(&h2c->streams_by_id))
2936 h2_release(h2c->conn);
2937}
2938
2939/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002940 * Detach the stream from the connection and possibly release the connection.
2941 */
2942static void h2_detach(struct conn_stream *cs)
2943{
Willy Tarreau60935142017-10-16 18:11:19 +02002944 struct h2s *h2s = cs->ctx;
2945 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002946 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002947
2948 cs->ctx = NULL;
2949 if (!h2s)
2950 return;
2951
Olivier Houchardf502aca2018-12-14 19:42:40 +01002952 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002953 h2c = h2s->h2c;
2954 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002955 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01002956 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
2957 !h2_frt_has_too_many_cs(h2c)) {
2958 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02002959 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002960 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002961 }
Willy Tarreau60935142017-10-16 18:11:19 +02002962
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002963 /* this stream may be blocked waiting for some data to leave (possibly
2964 * an ES or RST frame), so orphan it in this case.
2965 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002966 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002967 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002968 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002969 return;
2970
Willy Tarreau45f752e2017-10-30 15:44:59 +01002971 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2972 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2973 /* unblock the connection if it was blocked on this
2974 * stream.
2975 */
2976 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2977 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002978 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002979 }
2980
Willy Tarreau71049cc2018-03-28 13:56:39 +02002981 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002982
Olivier Houchard8a786902018-12-15 16:05:40 +01002983 if (h2c->flags & H2_CF_IS_BACK &&
2984 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002985 if (!(h2c->conn->flags &
2986 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2987 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002988 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002989 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2990 h2c->conn->owner = NULL;
2991 if (eb_is_empty(&h2c->streams_by_id)) {
2992 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2993 /* The server doesn't want it, let's kill the connection right away */
2994 h2c->conn->mux->destroy(h2c->conn);
2995 return;
2996 }
2997 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002998 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002999 if (eb_is_empty(&h2c->streams_by_id)) {
3000 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3001 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3002 return;
3003 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003004 /* Never ever allow to reuse a connection from a non-reuse backend */
3005 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3006 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003007 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003008 struct server *srv = objt_server(h2c->conn->target);
3009
3010 if (srv) {
3011 if (h2c->conn->flags & CO_FL_PRIVATE)
3012 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3013 else
3014 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3015 }
3016
3017 }
3018 }
3019 }
3020
Willy Tarreaue323f342018-03-28 13:51:45 +02003021 /* We don't want to close right now unless we're removing the
3022 * last stream, and either the connection is in error, or it
3023 * reached the ID already specified in a GOAWAY frame received
3024 * or sent (as seen by last_sid >= 0).
3025 */
3026 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3027 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003028 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard93c88522018-11-30 15:39:16 +01003029 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003030 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003031 (conn_xprt_read0_pending(h2c->conn) ||
3032 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3033 /* no more stream will come, kill it now */
3034 h2_release(h2c->conn);
3035 }
3036 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003037 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003038 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3039 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003040 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003041 else
3042 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003043 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003044}
3045
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003046static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003047{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003048 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003049 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003050
Willy Tarreau721c9742017-11-07 11:05:42 +01003051 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003052 return;
3053
Willy Tarreau18059042019-01-31 19:12:48 +01003054 /* a connstream may require us to immediately kill the whole connection
3055 * for example because of a "tcp-request content reject" rule that is
3056 * normally used to limit abuse. In this case we schedule a goaway to
3057 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003058 */
Willy Tarreau18059042019-01-31 19:12:48 +01003059 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3060 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3061 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3062 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3063 }
3064
Willy Tarreau90c32322017-11-24 08:00:30 +01003065 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003066 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003067 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003068
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003069 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003070 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003071 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003072
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003073 return;
3074add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003075 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003076 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003077 if (h2s->flags & H2_SF_BLK_MFCTL) {
3078 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3079 h2s->send_wait = sw;
3080 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3081 h2s->send_wait = sw;
3082 LIST_ADDQ(&h2c->send_list, &h2s->list);
3083 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003084 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003085 /* Let the handler know we want shutr */
3086 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003087}
3088
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003089static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003090{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003091 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003092 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003093
Willy Tarreau721c9742017-11-07 11:05:42 +01003094 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003095 return;
3096
Willy Tarreau67434202017-11-06 20:20:51 +01003097 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003098 /* we can cleanly close using an empty data frame only after headers */
3099
3100 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3101 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003102 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003103
3104 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003105 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003106 else
3107 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003108 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003109 /* a connstream may require us to immediately kill the whole connection
3110 * for example because of a "tcp-request content reject" rule that is
3111 * normally used to limit abuse. In this case we schedule a goaway to
3112 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003113 */
Willy Tarreau18059042019-01-31 19:12:48 +01003114 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3115 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3116 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3117 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3118 }
3119
Willy Tarreau90c32322017-11-24 08:00:30 +01003120 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003121 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003122 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003123
Willy Tarreau00dd0782018-03-01 16:31:34 +01003124 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003125 }
3126
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003127 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003128 tasklet_wakeup(h2c->wait_event.task);
3129 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003130
3131 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003132 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003133 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003134 if (h2s->flags & H2_SF_BLK_MFCTL) {
3135 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3136 h2s->send_wait = sw;
3137 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3138 h2s->send_wait = sw;
3139 LIST_ADDQ(&h2c->send_list, &h2s->list);
3140 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003141 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003142 /* let the handler know we want to shutw */
3143 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003144}
3145
3146static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3147{
3148 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003149 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003150
Olivier Houchard2c68a462018-12-15 22:42:20 +01003151 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003152 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003153 h2s->send_wait = NULL;
3154 LIST_DEL(&h2s->list);
3155 LIST_INIT(&h2s->list);
3156 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003157 if (reason & 2)
3158 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003159 if (reason & 1)
3160 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003161
Olivier Houchard2c68a462018-12-15 22:42:20 +01003162 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003163 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003164 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003165 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003166}
3167
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003168static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3169{
3170 struct h2s *h2s = cs->ctx;
3171
3172 if (!mode)
3173 return;
3174
3175 h2_do_shutr(h2s);
3176}
3177
3178static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3179{
3180 struct h2s *h2s = cs->ctx;
3181
3182 h2_do_shutw(h2s);
3183}
3184
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003185/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003186 * HTX request or response depending on the connection's side. Returns a
3187 * positive value on success, a negative value on failure, or 0 if it couldn't
3188 * proceed. May report connection errors in h2c->errcode if the frame is
3189 * non-decodable and the connection unrecoverable. In absence of connection
3190 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003191 *
3192 * The function may fold CONTINUATION frames into the initial HEADERS frame
3193 * by removing padding and next frame header, then moving the CONTINUATION
3194 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3195 * leaving a hole between the main frame and the beginning of the next one.
3196 * The possibly remaining incomplete or next frame at the end may be moved
3197 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3198 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3199 *
3200 * A buffer at the beginning of processing may look like this :
3201 *
3202 * ,---.---------.-----.--------------.--------------.------.---.
3203 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3204 * `---^---------^-----^--------------^--------------^------^---'
3205 * | | <-----> | |
3206 * area | dpl | wrap
3207 * |<--------------> |
3208 * | dfl |
3209 * |<-------------------------------------------------->|
3210 * head data
3211 *
3212 * Padding is automatically overwritten when folding, participating to the
3213 * hole size after dfl :
3214 *
3215 * ,---.------------------------.-----.--------------.------.---.
3216 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3217 * `---^------------------------^-----^--------------^------^---'
3218 * | | <-----> | |
3219 * area | hole | wrap
3220 * |<-----------------------> |
3221 * | dfl |
3222 * |<-------------------------------------------------->|
3223 * head data
3224 *
3225 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3226 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3227 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003228 *
3229 * The <flags> field must point to either the stream's flags or to a copy of it
3230 * so that the function can update the following flags :
3231 * - H2_SF_DATA_CLEN when content-length is seen
3232 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3233 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003234 *
3235 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3236 * decoding, in order to detect if we're dealing with a headers or a trailers
3237 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003238 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003239static 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 +02003240{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003241 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003242 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003243 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003244 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003245 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003246 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003247 int flen; // header frame len
3248 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003249 int ret = 0;
3250 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003251 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003252 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003253
Willy Tarreauea18f862018-12-22 20:19:26 +01003254next_frame:
3255 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3256 goto leave; // incomplete input frame
3257
3258 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3259 * this case, we'll try to paste it immediately after the initial
3260 * HEADERS frame payload and kill any possible padding. The initial
3261 * frame's length will be increased to represent the concatenation
3262 * of the two frames. The next frame is read from position <tlen>
3263 * and written at position <flen> (minus padding if some is present).
3264 */
3265 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3266 struct h2_fh hdr;
3267 int clen; // CONTINUATION frame's payload length
3268
3269 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3270 /* no more data, the buffer may be full, either due to
3271 * too large a frame or because of too large a hole that
3272 * we're going to compact at the end.
3273 */
3274 goto leave;
3275 }
3276
3277 if (hdr.ft != H2_FT_CONTINUATION) {
3278 /* RFC7540#6.10: frame of unexpected type */
3279 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3280 goto fail;
3281 }
3282
3283 if (hdr.sid != h2c->dsi) {
3284 /* RFC7540#6.10: frame of different stream */
3285 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3286 goto fail;
3287 }
3288
3289 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3290 /* RFC7540#4.2: invalid frame length */
3291 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3292 goto fail;
3293 }
3294
3295 /* detect when we must stop aggragating frames */
3296 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3297
3298 /* Take as much as we can of the CONTINUATION frame's payload */
3299 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3300 if (clen > hdr.len)
3301 clen = hdr.len;
3302
3303 /* Move the frame's payload over the padding, hole and frame
3304 * header. At least one of hole or dpl is null (see diagrams
3305 * above). The hole moves after the new aggragated frame.
3306 */
3307 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3308 h2c->dfl += clen - h2c->dpl;
3309 hole += h2c->dpl + 9;
3310 h2c->dpl = 0;
3311 goto next_frame;
3312 }
3313
3314 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003315
Willy Tarreau13278b42017-10-13 19:23:14 +02003316 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003317 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003318 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003319 copy = alloc_trash_chunk();
3320 if (!copy) {
3321 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3322 goto fail;
3323 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3325 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3326 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003327 }
3328
Willy Tarreau13278b42017-10-13 19:23:14 +02003329 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3330 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003331 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003332 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3333 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003334 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003335 }
3336
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003337 if (flen < 5) {
3338 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3339 goto fail;
3340 }
3341
Willy Tarreau13278b42017-10-13 19:23:14 +02003342 hdrs += 5; // stream dep = 4, weight = 1
3343 flen -= 5;
3344 }
3345
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003346 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003347 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003348 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003349 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003350
Willy Tarreau937f7602018-02-26 15:22:17 +01003351 /* we can't retry a failed decompression operation so we must be very
3352 * careful not to take any risks. In practice the output buffer is
3353 * always empty except maybe for trailers, in which case we simply have
3354 * to wait for the upper layer to finish consuming what is available.
3355 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003356
3357 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003358 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003359 if (!htx_is_empty(htx)) {
3360 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003361 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003362 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003363 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003364 if (b_data(rxbuf)) {
3365 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003366 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003367 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003368
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003369 rxbuf->head = 0;
3370 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003371 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003372
Willy Tarreau25919232019-01-03 14:48:18 +01003373 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003374 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3375 sizeof(list)/sizeof(list[0]), tmp);
3376 if (outlen < 0) {
3377 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3378 goto fail;
3379 }
3380
Willy Tarreau25919232019-01-03 14:48:18 +01003381 /* The PACK decompressor was updated, let's update the input buffer and
3382 * the parser's state to commit these changes and allow us to later
3383 * fail solely on the stream if needed.
3384 */
3385 b_del(&h2c->dbuf, h2c->dfl + hole);
3386 h2c->dfl = hole = 0;
3387 h2c->st0 = H2_CS_FRAME_H;
3388
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003389 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003390 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003391
Willy Tarreau88d138e2019-01-02 19:38:14 +01003392 if (*flags & H2_SF_HEADERS_RCVD)
3393 goto trailers;
3394
3395 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003396 if (htx) {
3397 /* HTX mode */
3398 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003399 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003400 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003401 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003402 } else {
3403 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003404 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003405 if (outlen > 0)
3406 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003407 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003408
3409 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003410 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003411 goto fail;
3412 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003413
Willy Tarreau174b06a2018-04-25 18:13:58 +02003414 if (msgf & H2_MSGF_BODY) {
3415 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003416 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003417 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003418 if (htx)
3419 htx->extra = *body_len;
3420 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003421 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003422 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003423 }
3424
Willy Tarreau88d138e2019-01-02 19:38:14 +01003425 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003426 /* indicate that a HEADERS frame was received for this stream, except
3427 * for 1xx responses. For 1xx responses, another HEADERS frame is
3428 * expected.
3429 */
3430 if (!(msgf & H2_MSGF_RSP_1XX))
3431 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003432
Christopher Faulet0b465482019-02-19 15:14:23 +01003433 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003434 /* Mark the end of message, either using EOM in HTX or with the
3435 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3436 * is not set during headers with END_STREAM.
3437 */
3438 if (htx) {
3439 if (!htx_add_endof(htx, HTX_BLK_EOM))
3440 goto fail;
3441 }
3442 else if (*flags & H2_SF_DATA_CHNK) {
3443 if (!b_putblk(rxbuf, "\r\n", 2))
3444 goto fail;
3445 }
3446 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003447
Willy Tarreau86277d42019-01-02 15:36:11 +01003448 /* success */
3449 ret = 1;
3450
Willy Tarreau68dd9852017-07-03 14:44:26 +02003451 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003452 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003453 * move the remaining data over it.
3454 */
3455 if (hole) {
3456 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3457 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3458 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3459 b_sub(&h2c->dbuf, hole);
3460 }
3461
3462 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3463 /* too large frames */
3464 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003465 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003466 }
3467
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003468 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003469 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003470 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003471 return ret;
3472
Willy Tarreau68dd9852017-07-03 14:44:26 +02003473 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003474 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003475 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003476
3477 trailers:
3478 /* This is the last HEADERS frame hence a trailer */
3479
3480 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3481 /* It's a trailer but it's missing ES flag */
3482 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3483 goto fail;
3484 }
3485
3486 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3487 * block, and when using chunks we must send the 0 CRLF marker. For
3488 * other modes, the trailers are silently dropped.
3489 */
3490 if (htx) {
3491 if (!htx_add_endof(htx, HTX_BLK_EOD))
3492 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003493 if (h2_make_htx_trailers(list, htx) <= 0)
3494 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003495 }
3496 else if (*flags & H2_SF_DATA_CHNK) {
3497 /* Legacy mode with chunked encoding : we must finalize the
3498 * data block message emit the trailing CRLF */
3499 if (!b_putblk(rxbuf, "0\r\n", 3))
3500 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003501
3502 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3503 if (outlen > 0)
3504 b_add(rxbuf, outlen);
3505 else
3506 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003507 }
3508
3509 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003510}
3511
Willy Tarreau454f9052017-10-26 19:40:35 +02003512/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3513 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3514 * in use, a new chunk is emitted for each frame. This is supposed to fit
3515 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3516 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3517 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003518 * parser state is automatically updated. Returns > 0 if it could completely
3519 * send the current frame, 0 if it couldn't complete, in which case
3520 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3521 * DATA frame can return 0 as a valid result). Stream errors are reported in
3522 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3523 * have checked the frame header and ensured that the frame was complete or the
3524 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003525 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003526static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003527{
3528 struct h2c *h2c = h2s->h2c;
3529 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003530 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003531 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003532 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003533 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003534
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003535 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003536
Olivier Houchard638b7992018-08-16 15:41:52 +02003537 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003538 if (!csbuf) {
3539 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003540 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003541 }
3542
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003543try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003544 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003545 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3546 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003547 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003548 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003549
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003550 if (flen > b_data(&h2c->dbuf)) {
3551 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003552 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003553 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003554 }
3555
Willy Tarreaua9b77962019-01-31 07:23:00 +01003556 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003557 block1 = htx_free_data_space(htx);
3558 if (!block1) {
3559 h2c->flags |= H2_CF_DEM_SFULL;
3560 goto fail;
3561 }
3562 if (flen > block1)
3563 flen = block1;
3564
3565 /* here, flen is the max we can copy into the output buffer */
3566 block1 = b_contig_data(&h2c->dbuf, 0);
3567 if (flen > block1)
3568 flen = block1;
3569
3570 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3571 h2c->flags |= H2_CF_DEM_SFULL;
3572 goto fail;
3573 }
3574
3575 b_del(&h2c->dbuf, flen);
3576 h2c->dfl -= flen;
3577 h2c->rcvd_c += flen;
3578 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003579
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003580 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003581 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003582 htx->extra = h2s->body_len;
3583 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003584 goto try_again;
3585 }
3586 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003587 /* it doesn't fit and the buffer is fragmented,
3588 * so let's defragment it and try again.
3589 */
3590 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003591 }
3592
Willy Tarreaueba10f22018-04-25 20:44:22 +02003593 /* chunked-encoding requires more room */
3594 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003595 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003596 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3597 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3598 (chklen < 1048576) ? 4 : 8;
3599 chklen += 4; // CRLF, CRLF
3600 }
3601
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003602 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003603 if (flen + chklen > b_room(csbuf)) {
3604 if (chklen >= b_room(csbuf)) {
3605 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003606 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003607 }
3608 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003609 }
3610
3611 if (h2s->flags & H2_SF_DATA_CHNK) {
3612 /* emit the chunk size */
3613 unsigned int chksz = flen;
3614 char str[10];
3615 char *beg;
3616
3617 beg = str + sizeof(str);
3618 *--beg = '\n';
3619 *--beg = '\r';
3620 do {
3621 *--beg = hextab[chksz & 0xF];
3622 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003623 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003624 }
3625
Willy Tarreau454f9052017-10-26 19:40:35 +02003626 /* Block1 is the length of the first block before the buffer wraps,
3627 * block2 is the optional second block to reach the end of the frame.
3628 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003629 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003630 if (block1 > flen)
3631 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003632 block2 = flen - block1;
3633
3634 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003635 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003636
3637 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003638 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003639
Willy Tarreaueba10f22018-04-25 20:44:22 +02003640 if (h2s->flags & H2_SF_DATA_CHNK) {
3641 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003642 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003643 }
3644
Willy Tarreau454f9052017-10-26 19:40:35 +02003645 /* now mark the input data as consumed (will be deleted from the buffer
3646 * by the caller when seeing FRAME_A after sending the window update).
3647 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003648 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003649 h2c->dfl -= flen;
3650 h2c->rcvd_c += flen;
3651 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3652
Willy Tarreau1915ca22019-01-24 11:49:37 +01003653 if (h2s->flags & H2_SF_DATA_CLEN)
3654 h2s->body_len -= flen;
3655
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003656 if (h2c->dfl > h2c->dpl) {
3657 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003658 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003659 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003660 }
3661
Willy Tarreau4a28da12018-01-04 14:41:00 +01003662 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003663 /* here we're done with the frame, all the payload (except padding) was
3664 * transferred.
3665 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003666
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003667 if (h2c->dff & H2_F_DATA_END_STREAM) {
3668 if (htx) {
3669 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3670 h2c->flags |= H2_CF_DEM_SFULL;
3671 goto fail;
3672 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003673 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003674 else if (h2s->flags & H2_SF_DATA_CHNK) {
3675 /* emit the trailing 0 CRLF CRLF */
3676 if (b_room(csbuf) < 5) {
3677 h2c->flags |= H2_CF_DEM_SFULL;
3678 goto fail;
3679 }
3680 chklen += 5;
3681 b_putblk(csbuf, "0\r\n\r\n", 5);
3682 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003683 }
3684
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003685 h2c->rcvd_c += h2c->dpl;
3686 h2c->rcvd_s += h2c->dpl;
3687 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003688 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3689
Willy Tarreau39d68502018-03-02 12:26:37 +01003690 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003691 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003692 h2s->cs->flags |= CS_FL_REOS;
3693 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003694 if (htx)
3695 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003696 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003697 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003698 if (htx)
3699 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003700 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003701}
3702
Willy Tarreau5dd17352018-06-14 13:33:30 +02003703/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3704 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3705 * number of bytes sent. The caller must check the stream's status to detect
3706 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003707 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003708static 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 +02003709{
3710 struct http_hdr list[MAX_HTTP_HDR];
3711 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003712 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003713 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003714 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003715 int es_now = 0;
3716 int ret = 0;
3717 int hdr;
3718
3719 if (h2c_mux_busy(h2c, h2s)) {
3720 h2s->flags |= H2_SF_BLK_MBUSY;
3721 return 0;
3722 }
3723
Willy Tarreau44e973f2018-03-01 17:49:30 +01003724 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003725 h2c->flags |= H2_CF_MUX_MALLOC;
3726 h2s->flags |= H2_SF_BLK_MROOM;
3727 return 0;
3728 }
3729
3730 /* First, try to parse the H1 response and index it into <list>.
3731 * NOTE! Since it comes from haproxy, we *know* that a response header
3732 * block does not wrap and we can safely read it this way without
3733 * having to realign the buffer.
3734 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003735 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003736 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003737 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003738 /* incomplete or invalid response, this is abnormal coming from
3739 * haproxy and may only result in a bad errorfile or bad Lua code
3740 * so that won't be fixed, raise an error now.
3741 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003742 * FIXME: we should instead add the ability to only return a
3743 * 502 bad gateway. But in theory this is not supposed to
3744 * happen.
3745 */
3746 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3747 ret = 0;
3748 goto end;
3749 }
3750
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003751 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003752
3753 /* certain statuses have no body or an empty one, regardless of
3754 * what the headers say.
3755 */
3756 if (sl.st.status >= 100 && sl.st.status < 200) {
3757 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3758 h1m->curr_len = h1m->body_len = 0;
3759 }
3760 else if (sl.st.status == 204 || sl.st.status == 304) {
3761 /* no contents, claim c-len is present and set to zero */
3762 h1m->flags &= ~H1_MF_CHNK;
3763 h1m->flags |= H1_MF_CLEN;
3764 h1m->curr_len = h1m->body_len = 0;
3765 }
3766
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003767 chunk_reset(&outbuf);
3768
3769 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003770 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003771 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003772 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003773
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003774 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003775 break;
3776 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003777 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003778 }
3779
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003780 if (outbuf.size < 9)
3781 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003782
3783 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003784 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3785 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3786 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787
3788 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003789 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003790 /* this is an unparsable response */
3791 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3792 ret = 0;
3793 goto end;
3794 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003795
3796 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003797 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003798 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003799 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003800 }
3801
3802 /* encode all headers, stop at empty name */
3803 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003804 /* these ones do not exist in H2 and must be dropped. */
3805 if (isteq(list[hdr].n, ist("connection")) ||
3806 isteq(list[hdr].n, ist("proxy-connection")) ||
3807 isteq(list[hdr].n, ist("keep-alive")) ||
3808 isteq(list[hdr].n, ist("upgrade")) ||
3809 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003810 continue;
3811
3812 if (isteq(list[hdr].n, ist("")))
3813 break; // end
3814
3815 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3816 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003817 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003818 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003819 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003820 }
3821 }
3822
3823 /* we may need to add END_STREAM */
3824 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3825 es_now = 1;
3826
3827 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003828 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003829
3830 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003831 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003832
3833 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003834 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003835
3836 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003837 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003838 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003839
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003840 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003841 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003842 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003843
Willy Tarreau801250e2018-09-11 11:45:04 +02003844 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003845 h2s->flags |= H2_SF_ES_SENT;
3846 if (h2s->st == H2_SS_OPEN)
3847 h2s->st = H2_SS_HLOC;
3848 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003849 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003850 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003851 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003852 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003853 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003854 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003855 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003856 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003857 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003858
3859 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003860
3861 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003862 //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 +02003863 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003864 full:
3865 h1m_init_res(h1m);
3866 h1m->err_pos = -1; // don't care about errors on the response path
3867 h2c->flags |= H2_CF_MUX_MFULL;
3868 h2s->flags |= H2_SF_BLK_MROOM;
3869 ret = 0;
3870 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003871}
3872
Willy Tarreau5dd17352018-06-14 13:33:30 +02003873/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3874 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3875 * the number of bytes sent. The caller must check the stream's status to
3876 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003877 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003878static 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 +02003879{
3880 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003881 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003882 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003883 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003884 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003885 int es_now = 0;
3886 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003887 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003888 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003889
3890 if (h2c_mux_busy(h2c, h2s)) {
3891 h2s->flags |= H2_SF_BLK_MBUSY;
3892 goto end;
3893 }
3894
Willy Tarreau44e973f2018-03-01 17:49:30 +01003895 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003896 h2c->flags |= H2_CF_MUX_MALLOC;
3897 h2s->flags |= H2_SF_BLK_MROOM;
3898 goto end;
3899 }
3900
3901 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003902 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003903 goto end;
3904
3905 chunk_reset(&outbuf);
3906
3907 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003909 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003910 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003911
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003912 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003913 break;
3914 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003915 /* If there are pending data in the output buffer, and we have
3916 * less than 1/4 of the mbuf's size and everything fits, we'll
3917 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3918 * is full and wait, to save some slow realign calls.
3919 */
3920 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3921 h2c->flags |= H2_CF_MUX_MFULL;
3922 h2s->flags |= H2_SF_BLK_MROOM;
3923 goto end;
3924 }
3925
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003926 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003927 }
3928
3929 if (outbuf.size < 9) {
3930 h2c->flags |= H2_CF_MUX_MFULL;
3931 h2s->flags |= H2_SF_BLK_MROOM;
3932 goto end;
3933 }
3934
3935 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003936 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3937 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3938 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003939
3940 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3941 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003942 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003943 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944 break;
3945 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003946 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003947 if ((long long)size > h1m->curr_len)
3948 size = h1m->curr_len;
3949 break;
3950 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003951 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003952 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003953 if (!ret)
3954 goto end;
3955
3956 if (ret < 0) {
3957 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003958 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003959 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3960 goto end;
3961 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003962 max -= ret;
3963 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003964 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003965 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003966 }
3967
Willy Tarreau801250e2018-09-11 11:45:04 +02003968 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003970 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003971 if (!ret)
3972 goto end;
3973
3974 if (ret < 0) {
3975 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003976 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003977 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3978 goto end;
3979 }
3980
3981 size = chunk;
3982 h1m->curr_len = chunk;
3983 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003984 max -= ret;
3985 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003986 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003987 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003988 if (!size)
3989 goto send_empty;
3990 }
3991
3992 /* in MSG_DATA state, continue below */
3993 size = h1m->curr_len;
3994 break;
3995 }
3996
3997 /* we have in <size> the exact number of bytes we need to copy from
3998 * the H1 buffer. We need to check this against the connection's and
3999 * the stream's send windows, and to ensure that this fits in the max
4000 * frame size and in the buffer's available space minus 9 bytes (for
4001 * the frame header). The connection's flow control is applied last so
4002 * that we can use a separate list of streams which are immediately
4003 * unblocked on window opening. Note: we don't implement padding.
4004 */
4005
Willy Tarreau5dd17352018-06-14 13:33:30 +02004006 if (size > max)
4007 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004008
4009 if (size > h2s->mws)
4010 size = h2s->mws;
4011
4012 if (size <= 0) {
4013 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004014 if (h2s->send_wait) {
4015 LIST_DEL(&h2s->list);
4016 LIST_INIT(&h2s->list);
4017 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 goto end;
4019 }
4020
4021 if (h2c->mfs && size > h2c->mfs)
4022 size = h2c->mfs;
4023
4024 if (size + 9 > outbuf.size) {
4025 /* we have an opportunity for enlarging the too small
4026 * available space, let's try.
4027 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004028 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 goto realign_again;
4030 size = outbuf.size - 9;
4031 }
4032
4033 if (size <= 0) {
4034 h2c->flags |= H2_CF_MUX_MFULL;
4035 h2s->flags |= H2_SF_BLK_MROOM;
4036 goto end;
4037 }
4038
4039 if (size > h2c->mws)
4040 size = h2c->mws;
4041
4042 if (size <= 0) {
4043 h2s->flags |= H2_SF_BLK_MFCTL;
4044 goto end;
4045 }
4046
4047 /* copy whatever we can */
4048 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004049 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 if (ret == 1)
4051 len2 = 0;
4052
4053 if (!ret || len1 + len2 < size) {
4054 /* FIXME: must normally never happen */
4055 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4056 goto end;
4057 }
4058
4059 /* limit len1/len2 to size */
4060 if (len1 + len2 > size) {
4061 int sub = len1 + len2 - size;
4062
4063 if (len2 > sub)
4064 len2 -= sub;
4065 else {
4066 sub -= len2;
4067 len2 = 0;
4068 len1 -= sub;
4069 }
4070 }
4071
4072 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004073 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004075 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004076
4077 send_empty:
4078 /* we may need to add END_STREAM */
4079 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4080 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004081 *
4082 * FIXME: what we do here is not correct because we send end_stream
4083 * before knowing if we'll have to send a HEADERS frame for the
4084 * trailers. More importantly we're not consuming the trailing CRLF
4085 * after the end of trailers, so it will be left to the caller to
4086 * eat it. The right way to do it would be to measure trailers here
4087 * and to send ES only if there are no trailers.
4088 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004089 */
4090 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004091 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004092 es_now = 1;
4093
4094 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004095 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004096
4097 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004098 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004099
4100 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004101 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004102
4103 /* consume incoming H1 response */
4104 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004105 max -= size;
4106 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004107 total += size;
4108 h1m->curr_len -= size;
4109 h2s->mws -= size;
4110 h2c->mws -= size;
4111
4112 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004113 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004114 goto new_frame;
4115 }
4116 }
4117
4118 if (es_now) {
4119 if (h2s->st == H2_SS_OPEN)
4120 h2s->st = H2_SS_HLOC;
4121 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004122 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004123
Willy Tarreau35a62702018-02-27 15:37:25 +01004124 if (!(h1m->flags & H1_MF_CHNK)) {
4125 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004126 total += max;
4127 ofs += max;
4128 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004129
Willy Tarreau801250e2018-09-11 11:45:04 +02004130 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004131 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004132
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 h2s->flags |= H2_SF_ES_SENT;
4134 }
4135
4136 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004137 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 +02004138 return total;
4139}
4140
Willy Tarreau115e83b2018-12-01 19:17:53 +01004141/* Try to send a HEADERS frame matching HTX response present in HTX message
4142 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4143 * must check the stream's status to detect any error which might have happened
4144 * subsequently to a successful send. The htx blocks are automatically removed
4145 * from the message. The htx message is assumed to be valid since produced from
4146 * the internal code, hence it contains a start line, an optional series of
4147 * header blocks and an end of header, otherwise an invalid frame could be
4148 * emitted and the resulting htx message could be left in an inconsistent state.
4149 */
4150static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4151{
4152 struct http_hdr list[MAX_HTTP_HDR];
4153 struct h2c *h2c = h2s->h2c;
4154 struct htx_blk *blk;
4155 struct htx_blk *blk_end;
4156 struct buffer outbuf;
4157 struct htx_sl *sl;
4158 enum htx_blk_type type;
4159 int es_now = 0;
4160 int ret = 0;
4161 int hdr;
4162 int idx;
4163
4164 if (h2c_mux_busy(h2c, h2s)) {
4165 h2s->flags |= H2_SF_BLK_MBUSY;
4166 return 0;
4167 }
4168
4169 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4170 h2c->flags |= H2_CF_MUX_MALLOC;
4171 h2s->flags |= H2_SF_BLK_MROOM;
4172 return 0;
4173 }
4174
4175 /* determine the first block which must not be deleted, blk_end may
4176 * be NULL if all blocks have to be deleted.
4177 */
4178 idx = htx_get_head(htx);
4179 blk_end = NULL;
4180 while (idx != -1) {
4181 type = htx_get_blk_type(htx_get_blk(htx, idx));
4182 idx = htx_get_next(htx, idx);
4183 if (type == HTX_BLK_EOH) {
4184 if (idx != -1)
4185 blk_end = htx_get_blk(htx, idx);
4186 break;
4187 }
4188 }
4189
4190 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004191 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004192 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004193 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004194 if (h2s->status < 100 || h2s->status > 999)
4195 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004196
4197 /* and the rest of the headers, that we dump starting at header 0 */
4198 hdr = 0;
4199
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004200 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004201 while ((idx = htx_get_next(htx, idx)) != -1) {
4202 blk = htx_get_blk(htx, idx);
4203 type = htx_get_blk_type(blk);
4204
4205 if (type == HTX_BLK_UNUSED)
4206 continue;
4207
4208 if (type != HTX_BLK_HDR)
4209 break;
4210
4211 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4212 goto fail;
4213
4214 list[hdr].n = htx_get_blk_name(htx, blk);
4215 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004216 hdr++;
4217 }
4218
4219 /* marker for end of headers */
4220 list[hdr].n = ist("");
4221
4222 if (h2s->status == 204 || h2s->status == 304) {
4223 /* no contents, claim c-len is present and set to zero */
4224 es_now = 1;
4225 }
4226
4227 chunk_reset(&outbuf);
4228
4229 while (1) {
4230 outbuf.area = b_tail(&h2c->mbuf);
4231 outbuf.size = b_contig_space(&h2c->mbuf);
4232 outbuf.data = 0;
4233
4234 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4235 break;
4236 realign_again:
4237 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4238 }
4239
4240 if (outbuf.size < 9)
4241 goto full;
4242
4243 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4244 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4245 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4246 outbuf.data = 9;
4247
4248 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004249 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004250 if (b_space_wraps(&h2c->mbuf))
4251 goto realign_again;
4252 goto full;
4253 }
4254
4255 /* encode all headers, stop at empty name */
4256 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4257 /* these ones do not exist in H2 and must be dropped. */
4258 if (isteq(list[hdr].n, ist("connection")) ||
4259 isteq(list[hdr].n, ist("proxy-connection")) ||
4260 isteq(list[hdr].n, ist("keep-alive")) ||
4261 isteq(list[hdr].n, ist("upgrade")) ||
4262 isteq(list[hdr].n, ist("transfer-encoding")))
4263 continue;
4264
4265 if (isteq(list[hdr].n, ist("")))
4266 break; // end
4267
4268 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4269 /* output full */
4270 if (b_space_wraps(&h2c->mbuf))
4271 goto realign_again;
4272 goto full;
4273 }
4274 }
4275
Christopher Faulet0b465482019-02-19 15:14:23 +01004276 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004277 * FIXME: we should also set it when we know for sure that the
4278 * content-length is zero as well as on 204/304
4279 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004280 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4281 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004282 es_now = 1;
4283
4284 if (h2s->cs->flags & CS_FL_SHW)
4285 es_now = 1;
4286
4287 /* update the frame's size */
4288 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4289
4290 if (es_now)
4291 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4292
4293 /* commit the H2 response */
4294 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004295
4296 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4297 * 1xx responses, another HEADERS frame is expected.
4298 */
4299 if (h2s->status >= 200 || h2s->status == 101)
4300 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004301
Willy Tarreau115e83b2018-12-01 19:17:53 +01004302 if (es_now) {
4303 h2s->flags |= H2_SF_ES_SENT;
4304 if (h2s->st == H2_SS_OPEN)
4305 h2s->st = H2_SS_HLOC;
4306 else
4307 h2s_close(h2s);
4308 }
4309
4310 /* OK we could properly deliver the response */
4311
4312 /* remove all header blocks including the EOH and compute the
4313 * corresponding size.
4314 *
4315 * FIXME: We should remove everything when es_now is set.
4316 */
4317 ret = 0;
4318 idx = htx_get_head(htx);
4319 blk = htx_get_blk(htx, idx);
4320 while (blk != blk_end) {
4321 ret += htx_get_blksz(blk);
4322 blk = htx_remove_blk(htx, blk);
4323 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004324
4325 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4326 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004327 end:
4328 return ret;
4329 full:
4330 h2c->flags |= H2_CF_MUX_MFULL;
4331 h2s->flags |= H2_SF_BLK_MROOM;
4332 ret = 0;
4333 goto end;
4334 fail:
4335 /* unparsable HTX messages, too large ones to be produced in the local
4336 * list etc go here (unrecoverable errors).
4337 */
4338 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4339 ret = 0;
4340 goto end;
4341}
4342
Willy Tarreau80739692018-10-05 11:35:57 +02004343/* Try to send a HEADERS frame matching HTX request present in HTX message
4344 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4345 * must check the stream's status to detect any error which might have happened
4346 * subsequently to a successful send. The htx blocks are automatically removed
4347 * from the message. The htx message is assumed to be valid since produced from
4348 * the internal code, hence it contains a start line, an optional series of
4349 * header blocks and an end of header, otherwise an invalid frame could be
4350 * emitted and the resulting htx message could be left in an inconsistent state.
4351 */
4352static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4353{
4354 struct http_hdr list[MAX_HTTP_HDR];
4355 struct h2c *h2c = h2s->h2c;
4356 struct htx_blk *blk;
4357 struct htx_blk *blk_end;
4358 struct buffer outbuf;
4359 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004360 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004361 enum htx_blk_type type;
4362 int es_now = 0;
4363 int ret = 0;
4364 int hdr;
4365 int idx;
4366
4367 if (h2c_mux_busy(h2c, h2s)) {
4368 h2s->flags |= H2_SF_BLK_MBUSY;
4369 return 0;
4370 }
4371
4372 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4373 h2c->flags |= H2_CF_MUX_MALLOC;
4374 h2s->flags |= H2_SF_BLK_MROOM;
4375 return 0;
4376 }
4377
4378 /* determine the first block which must not be deleted, blk_end may
4379 * be NULL if all blocks have to be deleted.
4380 */
4381 idx = htx_get_head(htx);
4382 blk_end = NULL;
4383 while (idx != -1) {
4384 type = htx_get_blk_type(htx_get_blk(htx, idx));
4385 idx = htx_get_next(htx, idx);
4386 if (type == HTX_BLK_EOH) {
4387 if (idx != -1)
4388 blk_end = htx_get_blk(htx, idx);
4389 break;
4390 }
4391 }
4392
4393 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004394 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004395 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004396 meth = htx_sl_req_meth(sl);
4397 path = htx_sl_req_uri(sl);
4398
4399 /* and the rest of the headers, that we dump starting at header 0 */
4400 hdr = 0;
4401
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004402 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004403 while ((idx = htx_get_next(htx, idx)) != -1) {
4404 blk = htx_get_blk(htx, idx);
4405 type = htx_get_blk_type(blk);
4406
4407 if (type == HTX_BLK_UNUSED)
4408 continue;
4409
4410 if (type != HTX_BLK_HDR)
4411 break;
4412
4413 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4414 goto fail;
4415
4416 list[hdr].n = htx_get_blk_name(htx, blk);
4417 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004418 hdr++;
4419 }
4420
4421 /* marker for end of headers */
4422 list[hdr].n = ist("");
4423
4424 chunk_reset(&outbuf);
4425
4426 while (1) {
4427 outbuf.area = b_tail(&h2c->mbuf);
4428 outbuf.size = b_contig_space(&h2c->mbuf);
4429 outbuf.data = 0;
4430
4431 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4432 break;
4433 realign_again:
4434 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4435 }
4436
4437 if (outbuf.size < 9)
4438 goto full;
4439
4440 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4441 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4442 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4443 outbuf.data = 9;
4444
4445 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004446 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004447 if (b_space_wraps(&h2c->mbuf))
4448 goto realign_again;
4449 goto full;
4450 }
4451
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004452 /* RFC7540 #8.3: the CONNECT method must have :
4453 * - :authority set to the URI part (host:port)
4454 * - :method set to CONNECT
4455 * - :scheme and :path omitted
4456 */
4457 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4458 /* encode the scheme which is always "https" (or 0x86 for "http") */
4459 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4460 /* output full */
4461 if (b_space_wraps(&h2c->mbuf))
4462 goto realign_again;
4463 goto full;
4464 }
Willy Tarreau80739692018-10-05 11:35:57 +02004465
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004466 /* encode the path, which necessarily is the second one */
4467 if (!hpack_encode_path(&outbuf, path)) {
4468 /* output full */
4469 if (b_space_wraps(&h2c->mbuf))
4470 goto realign_again;
4471 goto full;
4472 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004473
4474 /* look for the Host header and place it in :authority */
4475 auth = ist2(NULL, 0);
4476 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4477 if (isteq(list[hdr].n, ist("")))
4478 break; // end
4479
4480 if (isteq(list[hdr].n, ist("host"))) {
4481 auth = list[hdr].v;
4482 break;
4483 }
4484 }
4485 }
4486 else {
4487 /* for CONNECT, :authority is taken from the path */
4488 auth = path;
4489 }
4490
4491 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4492 /* output full */
4493 if (b_space_wraps(&h2c->mbuf))
4494 goto realign_again;
4495 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004496 }
4497
4498 /* encode all headers, stop at empty name */
4499 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4500 /* these ones do not exist in H2 and must be dropped. */
4501 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004502 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004503 isteq(list[hdr].n, ist("proxy-connection")) ||
4504 isteq(list[hdr].n, ist("keep-alive")) ||
4505 isteq(list[hdr].n, ist("upgrade")) ||
4506 isteq(list[hdr].n, ist("transfer-encoding")))
4507 continue;
4508
4509 if (isteq(list[hdr].n, ist("")))
4510 break; // end
4511
4512 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4513 /* output full */
4514 if (b_space_wraps(&h2c->mbuf))
4515 goto realign_again;
4516 goto full;
4517 }
4518 }
4519
4520 /* we may need to add END_STREAM if we have no body :
4521 * - request already closed, or :
4522 * - no transfer-encoding, and :
4523 * - no content-length or content-length:0
4524 * Fixme: this doesn't take into account CONNECT requests.
4525 */
4526 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4527 es_now = 1;
4528
4529 if (sl->flags & HTX_SL_F_BODYLESS)
4530 es_now = 1;
4531
4532 if (h2s->cs->flags & CS_FL_SHW)
4533 es_now = 1;
4534
4535 /* update the frame's size */
4536 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4537
4538 if (es_now)
4539 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4540
4541 /* commit the H2 response */
4542 b_add(&h2c->mbuf, outbuf.data);
4543 h2s->flags |= H2_SF_HEADERS_SENT;
4544 h2s->st = H2_SS_OPEN;
4545
Willy Tarreau80739692018-10-05 11:35:57 +02004546 if (es_now) {
4547 // trim any possibly pending data (eg: inconsistent content-length)
4548 h2s->flags |= H2_SF_ES_SENT;
4549 h2s->st = H2_SS_HLOC;
4550 }
4551
4552 /* remove all header blocks including the EOH and compute the
4553 * corresponding size.
4554 *
4555 * FIXME: We should remove everything when es_now is set.
4556 */
4557 ret = 0;
4558 idx = htx_get_head(htx);
4559 blk = htx_get_blk(htx, idx);
4560 while (blk != blk_end) {
4561 ret += htx_get_blksz(blk);
4562 blk = htx_remove_blk(htx, blk);
4563 }
4564
4565 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4566 htx_remove_blk(htx, blk_end);
4567
4568 end:
4569 return ret;
4570 full:
4571 h2c->flags |= H2_CF_MUX_MFULL;
4572 h2s->flags |= H2_SF_BLK_MROOM;
4573 ret = 0;
4574 goto end;
4575 fail:
4576 /* unparsable HTX messages, too large ones to be produced in the local
4577 * list etc go here (unrecoverable errors).
4578 */
4579 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4580 ret = 0;
4581 goto end;
4582}
4583
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004584/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004585 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4586 * caller must check the stream's status to detect any error which might have
4587 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004588 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4589 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004590static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004591{
4592 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004593 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004594 struct buffer outbuf;
4595 size_t total = 0;
4596 int es_now = 0;
4597 int bsize; /* htx block size */
4598 int fsize; /* h2 frame size */
4599 struct htx_blk *blk;
4600 enum htx_blk_type type;
4601 int idx;
4602
4603 if (h2c_mux_busy(h2c, h2s)) {
4604 h2s->flags |= H2_SF_BLK_MBUSY;
4605 goto end;
4606 }
4607
4608 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4609 h2c->flags |= H2_CF_MUX_MALLOC;
4610 h2s->flags |= H2_SF_BLK_MROOM;
4611 goto end;
4612 }
4613
Willy Tarreau98de12a2018-12-12 07:03:00 +01004614 htx = htx_from_buf(buf);
4615
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004616 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4617 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4618 * the caller to handle.
4619 */
4620
4621 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004622 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004623 goto end;
4624
4625 idx = htx_get_head(htx);
4626 blk = htx_get_blk(htx, idx);
4627 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4628 bsize = htx_get_blksz(blk);
4629 fsize = bsize;
4630
4631 if (type == HTX_BLK_EOD) {
4632 /* if we have an EOD, we're dealing with chunked data. We may
4633 * have a set of trailers after us that the caller will want to
4634 * deal with. Let's simply remove the EOD and return.
4635 */
4636 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004637 total++; // EOD counts as one byte
4638 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004639 goto end;
4640 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004641 else if (type == HTX_BLK_EOM) {
4642 if (h2s->flags & H2_SF_ES_SENT) {
4643 /* ES already sent */
4644 htx_remove_blk(htx, blk);
4645 total++; // EOM counts as one byte
4646 count--;
4647 goto end;
4648 }
4649 }
4650 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004651 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004652
4653 /* Perform some optimizations to reduce the number of buffer copies.
4654 * First, if the mux's buffer is empty and the htx area contains
4655 * exactly one data block of the same size as the requested count, and
4656 * this count fits within the frame size, the stream's window size, and
4657 * the connection's window size, then it's possible to simply swap the
4658 * caller's buffer with the mux's output buffer and adjust offsets and
4659 * length to match the entire DATA HTX block in the middle. In this
4660 * case we perform a true zero-copy operation from end-to-end. This is
4661 * the situation that happens all the time with large files. Second, if
4662 * this is not possible, but the mux's output buffer is empty, we still
4663 * have an opportunity to avoid the copy to the intermediary buffer, by
4664 * making the intermediary buffer's area point to the output buffer's
4665 * area. In this case we want to skip the HTX header to make sure that
4666 * copies remain aligned and that this operation remains possible all
4667 * the time. This goes for headers, data blocks and any data extracted
4668 * from the HTX blocks.
4669 */
4670 if (unlikely(fsize == count &&
4671 htx->used == 1 && type == HTX_BLK_DATA &&
4672 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4673 void *old_area = h2c->mbuf.area;
4674
4675 if (b_data(&h2c->mbuf)) {
4676 /* too bad there are data left there. If we have less
4677 * than 1/4 of the mbuf's size and everything fits,
4678 * we'll perform a copy anyway. Otherwise we'll pretend
4679 * the mbuf is full and wait.
4680 */
4681 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4682 goto copy;
4683 h2c->flags |= H2_CF_MUX_MFULL;
4684 h2s->flags |= H2_SF_BLK_MROOM;
4685 goto end;
4686 }
4687
4688 /* map an H2 frame to the HTX block so that we can put the
4689 * frame header there.
4690 */
4691 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004692 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004693 h2c->mbuf.data = fsize + 9;
4694 outbuf.area = b_head(&h2c->mbuf);
4695
4696 /* prepend an H2 DATA frame header just before the DATA block */
4697 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4698 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4699 h2_set_frame_size(outbuf.area, fsize);
4700
4701 /* update windows */
4702 h2s->mws -= fsize;
4703 h2c->mws -= fsize;
4704
4705 /* and exchange with our old area */
4706 buf->area = old_area;
4707 buf->data = buf->head = 0;
4708 total += fsize;
4709 goto end;
4710 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004711
Willy Tarreau98de12a2018-12-12 07:03:00 +01004712 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004713 /* for DATA and EOM we'll have to emit a frame, even if empty */
4714
4715 while (1) {
4716 outbuf.area = b_tail(&h2c->mbuf);
4717 outbuf.size = b_contig_space(&h2c->mbuf);
4718 outbuf.data = 0;
4719
4720 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4721 break;
4722 realign_again:
4723 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4724 }
4725
4726 if (outbuf.size < 9) {
4727 h2c->flags |= H2_CF_MUX_MFULL;
4728 h2s->flags |= H2_SF_BLK_MROOM;
4729 goto end;
4730 }
4731
4732 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4733 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4734 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4735 outbuf.data = 9;
4736
4737 /* we have in <fsize> the exact number of bytes we need to copy from
4738 * the HTX buffer. We need to check this against the connection's and
4739 * the stream's send windows, and to ensure that this fits in the max
4740 * frame size and in the buffer's available space minus 9 bytes (for
4741 * the frame header). The connection's flow control is applied last so
4742 * that we can use a separate list of streams which are immediately
4743 * unblocked on window opening. Note: we don't implement padding.
4744 */
4745
4746 /* EOM is presented with bsize==1 but would lead to the emission of an
4747 * empty frame, thus we force it to zero here.
4748 */
4749 if (type == HTX_BLK_EOM)
4750 bsize = fsize = 0;
4751
4752 if (!fsize)
4753 goto send_empty;
4754
4755 if (h2s->mws <= 0) {
4756 h2s->flags |= H2_SF_BLK_SFCTL;
4757 if (h2s->send_wait) {
4758 LIST_DEL(&h2s->list);
4759 LIST_INIT(&h2s->list);
4760 }
4761 goto end;
4762 }
4763
Willy Tarreauee573762018-12-04 15:25:57 +01004764 if (fsize > count)
4765 fsize = count;
4766
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004767 if (fsize > h2s->mws)
4768 fsize = h2s->mws; // >0
4769
4770 if (h2c->mfs && fsize > h2c->mfs)
4771 fsize = h2c->mfs; // >0
4772
4773 if (fsize + 9 > outbuf.size) {
4774 /* we have an opportunity for enlarging the too small
4775 * available space, let's try.
4776 * FIXME: is this really interesting to do? Maybe we'll
4777 * spend lots of time realigning instead of using two
4778 * frames.
4779 */
4780 if (b_space_wraps(&h2c->mbuf))
4781 goto realign_again;
4782 fsize = outbuf.size - 9;
4783
4784 if (fsize <= 0) {
4785 /* no need to send an empty frame here */
4786 h2c->flags |= H2_CF_MUX_MFULL;
4787 h2s->flags |= H2_SF_BLK_MROOM;
4788 goto end;
4789 }
4790 }
4791
4792 if (h2c->mws <= 0) {
4793 h2s->flags |= H2_SF_BLK_MFCTL;
4794 goto end;
4795 }
4796
4797 if (fsize > h2c->mws)
4798 fsize = h2c->mws;
4799
4800 /* now let's copy this this into the output buffer */
4801 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004802 h2s->mws -= fsize;
4803 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004804 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004805
4806 send_empty:
4807 /* update the frame's size */
4808 h2_set_frame_size(outbuf.area, fsize);
4809
4810 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4811 * meeting EOM. We should optimize this later.
4812 */
4813 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004814 total++; // EOM counts as one byte
4815 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004816 es_now = 1;
4817 }
4818
4819 if (es_now)
4820 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4821
4822 /* commit the H2 response */
4823 b_add(&h2c->mbuf, fsize + 9);
4824
4825 /* consume incoming HTX block, including EOM */
4826 total += fsize;
4827 if (fsize == bsize) {
4828 htx_remove_blk(htx, blk);
4829 if (fsize)
4830 goto new_frame;
4831 } else {
4832 /* we've truncated this block */
4833 htx_cut_data_blk(htx, blk, fsize);
4834 }
4835
4836 if (es_now) {
4837 if (h2s->st == H2_SS_OPEN)
4838 h2s->st = H2_SS_HLOC;
4839 else
4840 h2s_close(h2s);
4841
4842 h2s->flags |= H2_SF_ES_SENT;
4843 }
4844
4845 end:
4846 return total;
4847}
4848
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004849/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4850 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4851 * processed. The caller must check the stream's status to detect any error
4852 * which might have happened subsequently to a successful send. The htx blocks
4853 * are automatically removed from the message. The htx message is assumed to be
4854 * valid since produced from the internal code. Processing stops when meeting
4855 * the EOM, which is also removed. All trailers are processed at once and sent
4856 * as a single frame. The ES flag is always set.
4857 */
4858static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4859{
4860 struct http_hdr list[MAX_HTTP_HDR];
4861 struct h2c *h2c = h2s->h2c;
4862 struct htx_blk *blk;
4863 struct htx_blk *blk_end;
4864 struct buffer outbuf;
4865 struct h1m h1m;
4866 enum htx_blk_type type;
4867 uint32_t size;
4868 int ret = 0;
4869 int hdr;
4870 int idx;
4871 void *start;
4872
4873 if (h2c_mux_busy(h2c, h2s)) {
4874 h2s->flags |= H2_SF_BLK_MBUSY;
4875 goto end;
4876 }
4877
4878 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4879 h2c->flags |= H2_CF_MUX_MALLOC;
4880 h2s->flags |= H2_SF_BLK_MROOM;
4881 goto end;
4882 }
4883
4884 /* The principle is that we parse each and every trailers block using
4885 * the H1 headers parser, and append it to the list. We don't proceed
4886 * until EOM is met. blk_end will point to the EOM block.
4887 */
4888 hdr = 0;
4889 memset(list, 0, sizeof(list));
4890 blk_end = NULL;
4891
4892 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4893 blk = htx_get_blk(htx, idx);
4894 type = htx_get_blk_type(blk);
4895
4896 if (type == HTX_BLK_UNUSED)
4897 continue;
4898
4899 if (type != HTX_BLK_TLR) {
4900 if (type == HTX_BLK_EOM)
4901 blk_end = blk;
4902 break;
4903 }
4904
4905 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4906 goto fail;
4907
4908 size = htx_get_blksz(blk);
4909 start = htx_get_blk_ptr(htx, blk);
4910
4911 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4912 h1m.err_pos = 0;
4913 ret = h1_headers_to_hdr_list(start, start + size,
4914 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4915 &h1m, NULL);
4916 if (ret < 0)
4917 goto fail;
4918
4919 /* ret == 0 if an incomplete trailers block was found (missing
4920 * empty line), or > 0 if it was found. We have to continue on
4921 * incomplete messages because the trailers block might be
4922 * incomplete.
4923 */
4924
4925 /* search the new end */
4926 while (hdr <= sizeof(list)/sizeof(list[0])) {
4927 if (!list[hdr].n.len)
4928 break;
4929 hdr++;
4930 }
4931 }
4932
4933 if (!blk_end)
4934 goto end; // end not found yet
4935
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004936 chunk_reset(&outbuf);
4937
4938 while (1) {
4939 outbuf.area = b_tail(&h2c->mbuf);
4940 outbuf.size = b_contig_space(&h2c->mbuf);
4941 outbuf.data = 0;
4942
4943 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4944 break;
4945 realign_again:
4946 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4947 }
4948
4949 if (outbuf.size < 9)
4950 goto full;
4951
4952 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4953 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4954 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4955 outbuf.data = 9;
4956
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004957 /* encode all headers */
4958 for (idx = 0; idx < hdr; idx++) {
4959 /* these ones do not exist in H2 or must not appear in
4960 * trailers and must be dropped.
4961 */
4962 if (isteq(list[idx].n, ist("host")) ||
4963 isteq(list[idx].n, ist("content-length")) ||
4964 isteq(list[idx].n, ist("connection")) ||
4965 isteq(list[idx].n, ist("proxy-connection")) ||
4966 isteq(list[idx].n, ist("keep-alive")) ||
4967 isteq(list[idx].n, ist("upgrade")) ||
4968 isteq(list[idx].n, ist("te")) ||
4969 isteq(list[idx].n, ist("transfer-encoding")))
4970 continue;
4971
4972 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4973 /* output full */
4974 if (b_space_wraps(&h2c->mbuf))
4975 goto realign_again;
4976 goto full;
4977 }
4978 }
4979
Willy Tarreau67b8cae2019-02-21 18:16:35 +01004980 if (!hdr) {
4981 /* here we have a problem, we've received an empty trailers
4982 * block followed by an EOM. Because of this we can't send a
4983 * HEADERS frame, so we have to cheat and instead send an empty
4984 * DATA frame conveying the ES flag.
4985 */
4986 outbuf.area[3] = H2_FT_DATA;
4987 outbuf.area[4] = H2_F_DATA_END_STREAM;
4988 }
4989
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004990 /* update the frame's size */
4991 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4992
4993 /* commit the H2 response */
4994 b_add(&h2c->mbuf, outbuf.data);
4995 h2s->flags |= H2_SF_ES_SENT;
4996
4997 if (h2s->st == H2_SS_OPEN)
4998 h2s->st = H2_SS_HLOC;
4999 else
5000 h2s_close(h2s);
5001
5002 /* OK we could properly deliver the response */
5003 done:
5004 /* remove all header blocks including EOM and compute the corresponding size. */
5005 ret = 0;
5006 idx = htx_get_head(htx);
5007 blk = htx_get_blk(htx, idx);
5008 while (blk != blk_end) {
5009 ret += htx_get_blksz(blk);
5010 blk = htx_remove_blk(htx, blk);
5011 }
5012 blk = htx_remove_blk(htx, blk);
5013 end:
5014 return ret;
5015 full:
5016 h2c->flags |= H2_CF_MUX_MFULL;
5017 h2s->flags |= H2_SF_BLK_MROOM;
5018 ret = 0;
5019 goto end;
5020 fail:
5021 /* unparsable HTX messages, too large ones to be produced in the local
5022 * list etc go here (unrecoverable errors).
5023 */
5024 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5025 ret = 0;
5026 goto end;
5027}
5028
Olivier Houchard6ff20392018-07-17 18:46:31 +02005029/* Called from the upper layer, to subscribe to events, such as being able to send */
5030static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5031{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005032 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005033 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005034 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005035
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005036 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005037 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005038 if (!(sw->events & SUB_RETRY_RECV)) {
5039 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005040 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005041 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005042 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005043 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005044 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005045 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005046 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005047 if (!(sw->events & SUB_RETRY_SEND)) {
5048 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005049 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005050 h2s->send_wait = sw;
5051 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5052 if (h2s->flags & H2_SF_BLK_MFCTL)
5053 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5054 else
5055 LIST_ADDQ(&h2c->send_list, &h2s->list);
5056 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005057 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005058 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005059 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005060 if (event_type != 0)
5061 return -1;
5062 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005063
5064
5065}
5066
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005067static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5068{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005069 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005070 struct h2s *h2s = cs->ctx;
5071
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005072 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005073 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005074 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005075 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005076 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005077 }
5078 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005079 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005080 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005081 if (h2s->send_wait == sw) {
5082 LIST_DEL(&h2s->list);
5083 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005084 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005085 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005086 }
5087 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005088 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5089 sw = param;
5090 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005091 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005092 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005093 LIST_DEL(&h2s->list);
5094 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005095 }
5096 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005097 return 0;
5098}
5099
5100
Olivier Houchard511efea2018-08-16 15:30:32 +02005101/* Called from the upper layer, to receive data */
5102static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5103{
Olivier Houchard638b7992018-08-16 15:41:52 +02005104 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005105 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005106 struct htx *h2s_htx = NULL;
5107 struct htx *buf_htx = NULL;
5108 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005109 size_t ret = 0;
5110
5111 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005112 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5113 /* in HTX mode we ignore the count argument */
5114 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005115 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005116 /* Here htx_to_buf() will set buffer data to 0 because
5117 * the HTX is empty.
5118 */
5119 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005120 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005121 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005122
5123 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005124 count = htx_free_data_space(buf_htx);
5125 if (flags & CO_RFL_KEEP_RSV) {
5126 if (count <= global.tune.maxrewrite)
5127 goto end;
5128 count -= global.tune.maxrewrite;
5129 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005130
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005131 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005132 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005133 htx_to_buf(buf_htx, buf);
5134 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005135 ret = htx_ret.ret;
5136 }
5137 else {
5138 ret = b_xfer(buf, &h2s->rxbuf, count);
5139 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005140
Christopher Faulet37070b22019-02-14 15:12:14 +01005141 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005142 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005143 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005144 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005145 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005146 if (cs->flags & CS_FL_REOS)
5147 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005148 if (cs->flags & CS_FL_ERR_PENDING)
5149 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005150 if (b_size(&h2s->rxbuf)) {
5151 b_free(&h2s->rxbuf);
5152 offer_buffers(NULL, tasks_run_queue);
5153 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005154 }
5155
Willy Tarreau082f5592018-11-25 08:03:32 +01005156 if (ret && h2c->dsi == h2s->id) {
5157 /* demux is blocking on this stream's buffer */
5158 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005159 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005160 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005161
Olivier Houchard511efea2018-08-16 15:30:32 +02005162 return ret;
5163}
5164
Olivier Houchardd846c262018-10-19 17:24:29 +02005165static void h2_stop_senders(struct h2c *h2c)
5166{
5167 struct h2s *h2s, *h2s_back;
5168
5169 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5170 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5171 if (h2c->msi == h2s_id(h2s))
5172 continue;
5173 LIST_DEL(&h2s->list);
5174 LIST_INIT(&h2s->list);
5175 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005176 h2s->send_wait->events |= SUB_RETRY_SEND;
5177 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005178 LIST_ADD(&h2c->send_list, &h2s->list);
5179 }
5180}
5181
Willy Tarreau62f52692017-10-08 23:01:42 +02005182/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005183static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005184{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005185 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005186 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005187 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005188 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005189 struct htx *htx;
5190 struct htx_blk *blk;
5191 enum htx_blk_type btype;
5192 uint32_t bsize;
5193 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005194
Olivier Houchardd846c262018-10-19 17:24:29 +02005195 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005196 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005197 h2s->send_wait = NULL;
5198 LIST_DEL(&h2s->list);
5199 LIST_INIT(&h2s->list);
5200 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005201 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5202 return 0;
5203
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005204 /* htx will be enough to decide if we're using HTX or legacy */
5205 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5206
Willy Tarreau0bad0432018-06-14 16:54:01 +02005207 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005208 h2s->flags |= H2_SF_OUTGOING_DATA;
5209
Willy Tarreau751f2d02018-10-05 09:35:00 +02005210 if (h2s->id == 0) {
5211 int32_t id = h2c_get_next_sid(h2s->h2c);
5212
5213 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005214 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005215 return 0;
5216 }
5217
5218 eb32_delete(&h2s->by_id);
5219 h2s->by_id.key = h2s->id = id;
5220 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005221 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005222 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5223 }
5224
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005225 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005226 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5227 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005228 idx = htx_get_head(htx);
5229 blk = htx_get_blk(htx, idx);
5230 btype = htx_get_blk_type(blk);
5231 bsize = htx_get_blksz(blk);
5232
5233 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005234 case HTX_BLK_REQ_SL:
5235 /* start-line before headers */
5236 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5237 if (ret > 0) {
5238 total += ret;
5239 count -= ret;
5240 if (ret < bsize)
5241 goto done;
5242 }
5243 break;
5244
Willy Tarreau115e83b2018-12-01 19:17:53 +01005245 case HTX_BLK_RES_SL:
5246 /* start-line before headers */
5247 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5248 if (ret > 0) {
5249 total += ret;
5250 count -= ret;
5251 if (ret < bsize)
5252 goto done;
5253 }
5254 break;
5255
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005256 case HTX_BLK_DATA:
5257 case HTX_BLK_EOD:
5258 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005259 /* all these cause the emission of a DATA frame (possibly empty).
5260 * This EOM necessarily is one before trailers, as the EOM following
5261 * trailers would have been consumed by the trailers parser.
5262 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005263 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005264 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005265 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005266 total += ret;
5267 count -= ret;
5268 if (ret < bsize)
5269 goto done;
5270 }
5271 break;
5272
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005273 case HTX_BLK_TLR:
5274 /* This is the first trailers block, all the subsequent ones AND
5275 * the EOM will be swallowed by the parser.
5276 */
5277 ret = h2s_htx_make_trailers(h2s, htx);
5278 if (ret > 0) {
5279 total += ret;
5280 count -= ret;
5281 if (ret < bsize)
5282 goto done;
5283 }
5284 break;
5285
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005286 default:
5287 htx_remove_blk(htx, blk);
5288 total += bsize;
5289 count -= bsize;
5290 break;
5291 }
5292 }
5293 goto done;
5294 }
5295
5296 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005297 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005298 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005299 if (h2s->h2c->flags & H2_CF_IS_BACK)
5300 ret = -1;
5301 else
5302 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005303 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005304 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005305 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005306 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005307 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005308 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005309 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005310
Willy Tarreau5dd17352018-06-14 13:33:30 +02005311 if (unlikely((int)ret <= 0)) {
5312 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005313 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5314 break;
5315 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005316 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005317 total += count;
5318 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005319 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005320 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005321 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005322 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005323 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005324 break;
5325 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005326
5327 total += ret;
5328 count -= ret;
5329
5330 if (h2s->st >= H2_SS_ERROR)
5331 break;
5332
5333 if (h2s->flags & H2_SF_BLK_ANY)
5334 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005335 }
5336
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005337 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005338 if (h2s->st >= H2_SS_ERROR) {
5339 /* trim any possibly pending data after we close (extra CR-LF,
5340 * unprocessed trailers, abnormal extra data, ...)
5341 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005342 total += count;
5343 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005344 }
5345
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005346 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005347 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005348 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005349 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005350 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005351 }
5352
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005353 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005354 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005355 } else {
5356 b_del(buf, total);
5357 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005358
5359 /* The mux is full, cancel the pending tasks */
5360 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5361 (h2s->flags & H2_SF_BLK_MBUSY))
5362 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005363
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005364 /* If we're running HTX, and we read the whole buffer, then pretend
5365 * we read exactly what the caller specified, as with HTX the caller
5366 * will always give the buffer size, instead of the amount of data
5367 * available.
5368 */
5369 if (htx && !b_data(buf))
5370 total = orig_count;
5371
Olivier Houchard7505f942018-08-21 18:10:44 +02005372 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005373 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005374 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005375
Olivier Houchard7505f942018-08-21 18:10:44 +02005376 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005377 /* If we're waiting for flow control, and we got a shutr on the
5378 * connection, we will never be unlocked, so add an error on
5379 * the conn_stream.
5380 */
5381 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5382 !b_data(&h2s->h2c->dbuf) &&
5383 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5384 if (cs->flags & CS_FL_EOS)
5385 cs->flags |= CS_FL_ERROR;
5386 else
5387 cs->flags |= CS_FL_ERR_PENDING;
5388 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005389 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005390}
5391
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005392/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005393static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005394{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005395 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005396 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005397 struct eb32_node *node;
5398 int fctl_cnt = 0;
5399 int send_cnt = 0;
5400 int tree_cnt = 0;
5401 int orph_cnt = 0;
5402
5403 if (!h2c)
5404 return;
5405
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005406 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005407 fctl_cnt++;
5408
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005409 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005410 send_cnt++;
5411
Willy Tarreau3af37712018-12-18 14:34:41 +01005412 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005413 node = eb32_first(&h2c->streams_by_id);
5414 while (node) {
5415 h2s = container_of(node, struct h2s, by_id);
5416 tree_cnt++;
5417 if (!h2s->cs)
5418 orph_cnt++;
5419 node = eb32_next(node);
5420 }
5421
Willy Tarreau987c0632018-12-18 10:32:05 +01005422 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5423 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5424 " .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 +02005425 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5426 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005427 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005428 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5429 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5430 h2c->msi,
5431 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5432 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5433
5434 if (h2s) {
5435 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5436 h2s, h2s->id, h2s->flags,
5437 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5438 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5439 h2s->cs);
5440 if (h2s->cs)
5441 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5442 h2s->cs->flags, h2s->cs->data);
5443 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005444}
Willy Tarreau62f52692017-10-08 23:01:42 +02005445
5446/*******************************************************/
5447/* functions below are dedicated to the config parsers */
5448/*******************************************************/
5449
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005450/* config parser for global "tune.h2.header-table-size" */
5451static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5452 struct proxy *defpx, const char *file, int line,
5453 char **err)
5454{
5455 if (too_many_args(1, args, err, NULL))
5456 return -1;
5457
5458 h2_settings_header_table_size = atoi(args[1]);
5459 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5460 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5461 return -1;
5462 }
5463 return 0;
5464}
Willy Tarreau62f52692017-10-08 23:01:42 +02005465
Willy Tarreaue6baec02017-07-27 11:45:11 +02005466/* config parser for global "tune.h2.initial-window-size" */
5467static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5468 struct proxy *defpx, const char *file, int line,
5469 char **err)
5470{
5471 if (too_many_args(1, args, err, NULL))
5472 return -1;
5473
5474 h2_settings_initial_window_size = atoi(args[1]);
5475 if (h2_settings_initial_window_size < 0) {
5476 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5477 return -1;
5478 }
5479 return 0;
5480}
5481
Willy Tarreau5242ef82017-07-27 11:47:28 +02005482/* config parser for global "tune.h2.max-concurrent-streams" */
5483static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5484 struct proxy *defpx, const char *file, int line,
5485 char **err)
5486{
5487 if (too_many_args(1, args, err, NULL))
5488 return -1;
5489
5490 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005491 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005492 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5493 return -1;
5494 }
5495 return 0;
5496}
5497
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005498/* config parser for global "tune.h2.max-frame-size" */
5499static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5500 struct proxy *defpx, const char *file, int line,
5501 char **err)
5502{
5503 if (too_many_args(1, args, err, NULL))
5504 return -1;
5505
5506 h2_settings_max_frame_size = atoi(args[1]);
5507 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5508 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5509 return -1;
5510 }
5511 return 0;
5512}
5513
Willy Tarreau62f52692017-10-08 23:01:42 +02005514
5515/****************************************/
5516/* MUX initialization and instanciation */
5517/***************************************/
5518
5519/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005520static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005521 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005522 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005523 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005524 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005525 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005526 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005527 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005528 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005529 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005530 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005531 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005532 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005533 .shutr = h2_shutr,
5534 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005535 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005536 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005537 .name = "H2",
5538};
5539
Christopher Faulet32f61c02018-04-10 14:33:41 +02005540/* PROTO selection : this mux registers PROTO token "h2" */
5541static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005542 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005543
Willy Tarreau0108d902018-11-25 19:14:37 +01005544INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5545
Willy Tarreauf8957272018-10-03 10:25:20 +02005546static struct mux_proto_list mux_proto_h2_htx =
5547 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5548
5549INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5550
Willy Tarreau62f52692017-10-08 23:01:42 +02005551/* config keyword parsers */
5552static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005553 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005554 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005555 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005556 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005557 { 0, NULL, NULL }
5558}};
5559
Willy Tarreau0108d902018-11-25 19:14:37 +01005560INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);