blob: ebd4fe83837ab7a53402e1224abf760b13d59b7b [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
199 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
200 struct wait_event *send_wait; /* The streeam is waiting for flow control */
201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200202};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200203
Willy Tarreauc6405142017-09-21 20:23:50 +0200204/* descriptor for an h2 frame header */
205struct h2_fh {
206 uint32_t len; /* length, host order, 24 bits */
207 uint32_t sid; /* stream id, host order, 31 bits */
208 uint8_t ft; /* frame type */
209 uint8_t ff; /* frame flags */
210};
211
Willy Tarreau8ceae722018-11-26 11:58:30 +0100212/* the h2c connection pool */
213DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
214
215/* the h2s stream pool */
216DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
217
Willy Tarreaudc572362018-12-12 08:08:05 +0100218/* The default connection window size is 65535, it may only be enlarged using
219 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
220 * we'll pretend we already received the difference between the two to send
221 * an equivalent window update to enlarge it to 2G-1.
222 */
223#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
224
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200225/* a few settings from the global section */
226static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200227static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100228static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100229static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200230
Willy Tarreau2a856182017-05-16 15:20:39 +0200231/* a dmumy closed stream */
232static const struct h2s *h2_closed_stream = &(const struct h2s){
233 .cs = NULL,
234 .h2c = NULL,
235 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100236 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100237 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200238 .id = 0,
239};
240
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100241/* a dmumy closed stream returning a PROTOCOL_ERROR error */
242static const struct h2s *h2_error_stream = &(const struct h2s){
243 .cs = NULL,
244 .h2c = NULL,
245 .st = H2_SS_CLOSED,
246 .errcode = H2_ERR_PROTOCOL_ERROR,
247 .flags = 0,
248 .id = 0,
249};
250
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100251/* a dmumy closed stream returning a REFUSED_STREAM error */
252static const struct h2s *h2_refused_stream = &(const struct h2s){
253 .cs = NULL,
254 .h2c = NULL,
255 .st = H2_SS_CLOSED,
256 .errcode = H2_ERR_REFUSED_STREAM,
257 .flags = 0,
258 .id = 0,
259};
260
Willy Tarreau2a856182017-05-16 15:20:39 +0200261/* and a dummy idle stream for use with any unannounced stream */
262static const struct h2s *h2_idle_stream = &(const struct h2s){
263 .cs = NULL,
264 .h2c = NULL,
265 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100266 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200267 .id = 0,
268};
269
Olivier Houchard9f6af332018-05-25 14:04:04 +0200270static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200271static int h2_send(struct h2c *h2c);
272static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200273static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200274static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100275static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100276static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100277static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200278static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100279static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100280static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200281
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282/*****************************************************/
283/* functions below are for dynamic buffer management */
284/*****************************************************/
285
Willy Tarreau315d8072017-12-10 22:17:57 +0100286/* indicates whether or not the we may call the h2_recv() function to attempt
287 * to receive data into the buffer and/or demux pending data. The condition is
288 * a bit complex due to some API limits for now. The rules are the following :
289 * - if an error or a shutdown was detected on the connection and the buffer
290 * is empty, we must not attempt to receive
291 * - if the demux buf failed to be allocated, we must not try to receive and
292 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100293 * - if no flag indicates a blocking condition, we may attempt to receive,
294 * regardless of whether the demux buffer is full or not, so that only
295 * de demux part decides whether or not to block. This is needed because
296 * the connection API indeed prevents us from re-enabling receipt that is
297 * already enabled in a polled state, so we must always immediately stop
298 * as soon as the demux can't proceed so as never to hit an end of read
299 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100300 * - otherwise must may not attempt
301 */
302static inline int h2_recv_allowed(const struct h2c *h2c)
303{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200304 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100305 (h2c->st0 >= H2_CS_ERROR ||
306 h2c->conn->flags & CO_FL_ERROR ||
307 conn_xprt_read0_pending(h2c->conn)))
308 return 0;
309
310 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100311 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100312 return 1;
313
314 return 0;
315}
316
Willy Tarreau47b515a2018-12-21 16:09:41 +0100317/* restarts reading on the connection if it was not enabled */
318static inline void h2c_restart_reading(const struct h2c *h2c)
319{
320 if (!h2_recv_allowed(h2c))
321 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100322 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100323 return;
324 tasklet_wakeup(h2c->wait_event.task);
325}
326
327
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100328/* returns true if the front connection has too many conn_streams attached */
329static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200330{
Willy Tarreaua8754662018-12-23 20:43:58 +0100331 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200332}
333
Willy Tarreau44e973f2018-03-01 17:49:30 +0100334/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
335 * flags are used to figure what buffer was requested. It returns 1 if the
336 * allocation succeeds, in which case the connection is woken up, or 0 if it's
337 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200338 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100339static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200340{
341 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100342 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200343
Willy Tarreau44e973f2018-03-01 17:49:30 +0100344 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200345 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100346 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200347 return 1;
348 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200349
Willy Tarreau44e973f2018-03-01 17:49:30 +0100350 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
351 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200352
353 if (h2c->flags & H2_CF_DEM_MROOM) {
354 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100355 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200356 }
Willy Tarreau14398122017-09-22 14:26:04 +0200357 return 1;
358 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100359
360 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
361 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200362 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100363 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100364 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100365 return 1;
366 }
367
Willy Tarreau14398122017-09-22 14:26:04 +0200368 return 0;
369}
370
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200372{
373 struct buffer *buf = NULL;
374
Willy Tarreau44e973f2018-03-01 17:49:30 +0100375 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
376 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
377 h2c->buf_wait.target = h2c;
378 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100379 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100380 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100381 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200382 __conn_xprt_stop_recv(h2c->conn);
383 }
384 return buf;
385}
386
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200387static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200388{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100390 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200391 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200392 }
393}
394
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100395/* returns the number of allocatable outgoing streams for the connection taking
396 * the last_sid and the reserved ones into account.
397 */
398static inline int h2_streams_left(const struct h2c *h2c)
399{
400 int ret;
401
402 /* consider the number of outgoing streams we're allowed to create before
403 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
404 * nb_reserved is the number of streams which don't yet have an ID.
405 */
406 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
407 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
408 if (ret < 0)
409 ret = 0;
410 return ret;
411}
412
Willy Tarreau00f18a32019-01-26 12:19:01 +0100413/* returns the number of streams in use on a connection to figure if it's
414 * idle or not. We check nb_cs and not nb_streams as the caller will want
415 * to know if it was the last one after a detach().
416 */
417static int h2_used_streams(struct connection *conn)
418{
419 struct h2c *h2c = conn->ctx;
420
421 return h2c->nb_cs;
422}
423
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100424/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100425static int h2_avail_streams(struct connection *conn)
426{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100427 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100428 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100429 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100430
Willy Tarreau6afec462019-01-28 06:40:19 +0100431 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
432 * streams on the connection.
433 */
434 if (h2c->last_sid >= 0)
435 return 0;
436
Willy Tarreau86949782019-01-31 10:42:05 +0100437 /* note: may be negative if a SETTINGS frame changes the limit */
438 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100439
440 /* we must also consider the limit imposed by stream IDs */
441 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100442 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100443 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
445 ret1 = MIN(ret1, ret2);
446 }
447 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448}
449
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200450
Willy Tarreau62f52692017-10-08 23:01:42 +0200451/*****************************************************************/
452/* functions below are dedicated to the mux setup and management */
453/*****************************************************************/
454
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200455/* Initialize the mux once it's attached. For outgoing connections, the context
456 * is already initialized before installing the mux, so we detect incoming
457 * connections from the fact that the context is still NULL. Returns < 0 on
458 * error.
459 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100460static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200461{
462 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100463 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200464
Willy Tarreaubafbe012017-11-24 17:34:44 +0100465 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200467 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100469 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200470 h2c->flags = H2_CF_IS_BACK;
471 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
472 if (tick_isset(prx->timeout.serverfin))
473 h2c->shut_timeout = prx->timeout.serverfin;
474 } else {
475 h2c->flags = H2_CF_NONE;
476 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
477 if (tick_isset(prx->timeout.clientfin))
478 h2c->shut_timeout = prx->timeout.clientfin;
479 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100480
Willy Tarreau0b37d652018-10-03 10:33:02 +0200481 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100482 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100483 if (tick_isset(h2c->timeout)) {
484 t = task_new(tid_bit);
485 if (!t)
486 goto fail;
487
488 h2c->task = t;
489 t->process = h2_timeout_task;
490 t->context = h2c;
491 t->expire = tick_add(now_ms, h2c->timeout);
492 }
Willy Tarreauea392822017-10-31 10:02:25 +0100493
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200494 h2c->wait_event.task = tasklet_new();
495 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200496 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200497 h2c->wait_event.task->process = h2_io_cb;
498 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100499 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200500
Willy Tarreau32218eb2017-09-22 08:07:25 +0200501 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
502 if (!h2c->ddht)
503 goto fail;
504
505 /* Initialise the context. */
506 h2c->st0 = H2_CS_PREFACE;
507 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100508 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200509 h2c->max_id = -1;
510 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100511 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200512 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100513 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200514 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100515 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100516 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200517
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200518 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519 h2c->dsi = -1;
520 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100521
Willy Tarreau32218eb2017-09-22 08:07:25 +0200522 h2c->last_sid = -1;
523
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200524 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200525 h2c->miw = 65535; /* mux initial window size */
526 h2c->mws = 65535; /* mux window size */
527 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200528 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 LIST_INIT(&h2c->send_list);
530 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200531 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100532 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200533
Willy Tarreau3f133572017-10-31 19:21:06 +0100534 if (t)
535 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100536
Willy Tarreau01b44822018-10-03 14:26:37 +0200537 if (h2c->flags & H2_CF_IS_BACK) {
538 /* FIXME: this is temporary, for outgoing connections we need
539 * to immediately allocate a stream until the code is modified
540 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100541 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200542 */
543 struct h2s *h2s;
544
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100545 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200546 if (!h2s)
547 goto fail_stream;
548 }
549
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100550 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200551
Willy Tarreau0f383582018-10-03 14:22:21 +0200552 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100553 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200554 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200555 fail_stream:
556 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200557 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100558 if (t)
559 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200560 if (h2c->wait_event.task)
561 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100562 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200563 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200564 return -1;
565}
566
Willy Tarreau751f2d02018-10-05 09:35:00 +0200567/* returns the next allocatable outgoing stream ID for the H2 connection, or
568 * -1 if no more is allocatable.
569 */
570static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
571{
572 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100573
574 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200575 id = -1;
576 return id;
577}
578
Willy Tarreau2373acc2017-10-12 17:35:14 +0200579/* returns the stream associated with id <id> or NULL if not found */
580static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
581{
582 struct eb32_node *node;
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584 if (id == 0)
585 return (struct h2s *)h2_closed_stream;
586
Willy Tarreau2a856182017-05-16 15:20:39 +0200587 if (id > h2c->max_id)
588 return (struct h2s *)h2_idle_stream;
589
Willy Tarreau2373acc2017-10-12 17:35:14 +0200590 node = eb32_lookup(&h2c->streams_by_id, id);
591 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200592 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200593
594 return container_of(node, struct h2s, by_id);
595}
596
Willy Tarreau62f52692017-10-08 23:01:42 +0200597/* release function for a connection. This one should be called to free all
598 * resources allocated to the mux.
599 */
600static void h2_release(struct connection *conn)
601{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100602 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200603
Willy Tarreau32218eb2017-09-22 08:07:25 +0200604 if (h2c) {
605 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200606
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100607 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100608 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100609 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200610
Willy Tarreau44e973f2018-03-01 17:49:30 +0100611 h2_release_buf(h2c, &h2c->dbuf);
612 h2_release_buf(h2c, &h2c->mbuf);
613
Willy Tarreauea392822017-10-31 10:02:25 +0100614 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200615 h2c->task->context = NULL;
616 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100617 h2c->task = NULL;
618 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200619 if (h2c->wait_event.task)
620 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100621 if (h2c->wait_event.events != 0)
622 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200623 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100624
Willy Tarreaubafbe012017-11-24 17:34:44 +0100625 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200626 }
627
628 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100629 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200630
631 conn_stop_tracking(conn);
632 conn_full_close(conn);
633 if (conn->destroy_cb)
634 conn->destroy_cb(conn);
635 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200636}
637
638
Willy Tarreau71681172017-10-23 14:39:06 +0200639/******************************************************/
640/* functions below are for the H2 protocol processing */
641/******************************************************/
642
643/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100644static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200645{
646 return h2s ? h2s->id : 0;
647}
648
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200649/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100650static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200651{
652 if (h2c->msi < 0)
653 return 0;
654
655 if (h2c->msi == h2s_id(h2s))
656 return 0;
657
658 return 1;
659}
660
Willy Tarreau741d6df2017-10-17 08:00:59 +0200661/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100662static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200663{
664 h2c->errcode = err;
665 h2c->st0 = H2_CS_ERROR;
666}
667
Willy Tarreau175cebb2019-01-24 10:02:24 +0100668/* marks an error on the stream. It may also update an already closed stream
669 * (e.g. to report an error after an RST was received).
670 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100671static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200672{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100673 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200674 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100675 if (h2s->st < H2_SS_ERROR)
676 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100677 if (h2s->cs)
678 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200679 }
680}
681
Willy Tarreau7e094452018-12-19 18:08:52 +0100682/* attempt to notify the data layer of recv availability */
683static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
684{
685 struct wait_event *sw;
686
687 if (h2s->recv_wait) {
688 sw = h2s->recv_wait;
689 sw->events &= ~SUB_RETRY_RECV;
690 tasklet_wakeup(sw->task);
691 h2s->recv_wait = NULL;
692 }
693}
694
695/* attempt to notify the data layer of send availability */
696static void __maybe_unused h2s_notify_send(struct h2s *h2s)
697{
698 struct wait_event *sw;
699
700 if (h2s->send_wait) {
701 sw = h2s->send_wait;
702 sw->events &= ~SUB_RETRY_SEND;
703 tasklet_wakeup(sw->task);
704 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100705 LIST_DEL(&h2s->list);
706 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100707 }
708}
709
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100710/* alerts the data layer, trying to wake it up by all means, following
711 * this sequence :
712 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
713 * - if its subscribed to send, then it's woken up for send
714 * - if it was subscribed to neither, its ->wake() callback is called
715 * It is safe to call this function with a closed stream which doesn't have a
716 * conn_stream anymore.
717 */
718static void __maybe_unused h2s_alert(struct h2s *h2s)
719{
720 if (h2s->recv_wait || h2s->send_wait) {
721 h2s_notify_recv(h2s);
722 h2s_notify_send(h2s);
723 }
724 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
725 h2s->cs->data_cb->wake(h2s->cs);
726}
727
Willy Tarreaue4820742017-07-27 13:37:23 +0200728/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100729static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200730{
731 uint8_t *out = frame;
732
733 *out = len >> 16;
734 write_n16(out + 1, len);
735}
736
Willy Tarreau54c15062017-10-10 17:10:03 +0200737/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
738 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
739 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200740 * available in the buffer's input prior to calling this function. The buffer
741 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200742 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100743static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200744 const struct buffer *b, int o)
745{
Willy Tarreau591d4452018-06-15 17:21:00 +0200746 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200747}
748
Willy Tarreau1f094672017-11-20 21:27:45 +0100749static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200750{
Willy Tarreau591d4452018-06-15 17:21:00 +0200751 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200752}
753
Willy Tarreau1f094672017-11-20 21:27:45 +0100754static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200755{
Willy Tarreau591d4452018-06-15 17:21:00 +0200756 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200757}
758
Willy Tarreau1f094672017-11-20 21:27:45 +0100759static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200760{
Willy Tarreau591d4452018-06-15 17:21:00 +0200761 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200762}
763
764
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100765/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
766 * The algorithm is not obvious. It turns out that H2 headers are neither
767 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
768 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200769 *
770 * b0 b1 b2 b3 b4 b5..b8
771 * +----------+---------+--------+----+----+----------------------+
772 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
773 * +----------+---------+--------+----+----+----------------------+
774 *
775 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
776 * we get the sid properly aligned and ordered, and 16 bits of len properly
777 * ordered as well. The type and flags can be extracted using bit shifts from
778 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200779 * Returns zero if some bytes are missing, otherwise non-zero on success. The
780 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200781 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100782static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200783{
784 uint64_t w;
785
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100786 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200787 return 0;
788
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100789 w = h2_get_n64(b, o + 1);
790 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200791 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
792 h->ff = w >> 32;
793 h->ft = w >> 40;
794 h->len += w >> 48;
795 return 1;
796}
797
798/* skip the next 9 bytes corresponding to the frame header possibly parsed by
799 * h2_peek_frame_hdr() above.
800 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100801static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200802{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200803 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200804}
805
806/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100807static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200808{
809 int ret;
810
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100811 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200812 if (ret > 0)
813 h2_skip_frame_hdr(b);
814 return ret;
815}
816
Willy Tarreau00dd0782018-03-01 16:31:34 +0100817/* marks stream <h2s> as CLOSED and decrement the number of active streams for
818 * its connection if the stream was not yet closed. Please use this exclusively
819 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100820 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100821static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100822{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100823 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100824 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100825 if (!h2s->id)
826 h2s->h2c->nb_reserved--;
827 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100828 h2s->st = H2_SS_CLOSED;
829}
830
Willy Tarreau71049cc2018-03-28 13:56:39 +0200831/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
832static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100833{
834 h2s_close(h2s);
835 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200836 if (b_size(&h2s->rxbuf)) {
837 b_free(&h2s->rxbuf);
838 offer_buffers(NULL, tasks_run_queue);
839 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200840 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100841 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200842 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100843 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800844 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200845 * reference left would be in the h2c send_list/fctl_list, and if
846 * we're in it, we're getting out anyway
847 */
848 LIST_DEL(&h2s->list);
849 LIST_INIT(&h2s->list);
850 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100851 pool_free(pool_head_h2s, h2s);
852}
853
Willy Tarreaua8e49542018-10-03 18:53:55 +0200854/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
855 * stream tree. In case of error, nothing is added and NULL is returned. The
856 * causes of errors can be any failed memory allocation. The caller is
857 * responsible for checking if the connection may support an extra stream
858 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200859 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200860static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200861{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200862 struct h2s *h2s;
863
Willy Tarreaubafbe012017-11-24 17:34:44 +0100864 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200865 if (!h2s)
866 goto out;
867
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 h2s->wait_event.task = tasklet_new();
869 if (!h2s->wait_event.task) {
870 pool_free(pool_head_h2s, h2s);
871 goto out;
872 }
873 h2s->send_wait = NULL;
874 h2s->recv_wait = NULL;
875 h2s->wait_event.task->process = h2_deferred_shut;
876 h2s->wait_event.task->context = h2s;
877 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100878 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200879 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200880 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200881 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200882 h2s->mws = h2c->miw;
883 h2s->flags = H2_SF_NONE;
884 h2s->errcode = H2_ERR_NO_ERROR;
885 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200886 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100887 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200888 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200889
890 if (h2c->flags & H2_CF_IS_BACK) {
891 h1m_init_req(&h2s->h1m);
892 h2s->h1m.err_pos = -1; // don't care about errors on the request path
893 h2s->h1m.flags |= H1_MF_TOLOWER;
894 } else {
895 h1m_init_res(&h2s->h1m);
896 h2s->h1m.err_pos = -1; // don't care about errors on the response path
897 h2s->h1m.flags |= H1_MF_TOLOWER;
898 }
899
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200900 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200901 if (id > 0)
902 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100903 else
904 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200905
906 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100907 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100908 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200909
910 return h2s;
911
912 out_free_h2s:
913 pool_free(pool_head_h2s, h2s);
914 out:
915 return NULL;
916}
917
918/* creates a new stream <id> on the h2c connection and returns it, or NULL in
919 * case of memory allocation error.
920 */
921static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
922{
923 struct session *sess = h2c->conn->owner;
924 struct conn_stream *cs;
925 struct h2s *h2s;
926
927 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
928 goto out;
929
930 h2s = h2s_new(h2c, id);
931 if (!h2s)
932 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933
934 cs = cs_new(h2c->conn);
935 if (!cs)
936 goto out_close;
937
Olivier Houchard746fb772018-12-15 19:42:00 +0100938 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200939 h2s->cs = cs;
940 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200941 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200942
943 if (stream_create_from_cs(cs) < 0)
944 goto out_free_cs;
945
Willy Tarreau590a0512018-09-05 11:56:48 +0200946 /* We want the accept date presented to the next stream to be the one
947 * we have now, the handshake time to be null (since the next stream
948 * is not delayed by a handshake), and the idle time to count since
949 * right now.
950 */
951 sess->accept_date = date;
952 sess->tv_accept = now;
953 sess->t_handshake = 0;
954
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200955 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100956 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200957 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 return h2s;
959
960 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200961 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200962 cs_free(cs);
963 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200964 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200966 sess_log(sess);
967 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200968}
969
Willy Tarreau751f2d02018-10-05 09:35:00 +0200970/* allocates a new stream associated to conn_stream <cs> on the h2c connection
971 * and returns it, or NULL in case of memory allocation error or if the highest
972 * possible stream ID was reached.
973 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100974static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200975{
976 struct h2s *h2s = NULL;
977
Willy Tarreau86949782019-01-31 10:42:05 +0100978 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200979 goto out;
980
Willy Tarreaua80dca82019-01-24 17:08:28 +0100981 if (h2_streams_left(h2c) < 1)
982 goto out;
983
Willy Tarreau751f2d02018-10-05 09:35:00 +0200984 /* Defer choosing the ID until we send the first message to create the stream */
985 h2s = h2s_new(h2c, 0);
986 if (!h2s)
987 goto out;
988
989 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100990 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200991 cs->ctx = h2s;
992 h2c->nb_cs++;
993
Willy Tarreau751f2d02018-10-05 09:35:00 +0200994 out:
995 return h2s;
996}
997
Willy Tarreaube5b7152017-09-25 16:25:39 +0200998/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
999 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1000 * the various settings codes.
1001 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001002static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001003{
1004 struct buffer *res;
1005 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001006 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001007 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001008 int ret;
1009
1010 if (h2c_mux_busy(h2c, NULL)) {
1011 h2c->flags |= H2_CF_DEM_MBUSY;
1012 return 0;
1013 }
1014
Willy Tarreau44e973f2018-03-01 17:49:30 +01001015 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001016 if (!res) {
1017 h2c->flags |= H2_CF_MUX_MALLOC;
1018 h2c->flags |= H2_CF_DEM_MROOM;
1019 return 0;
1020 }
1021
1022 chunk_init(&buf, buf_data, sizeof(buf_data));
1023 chunk_memcpy(&buf,
1024 "\x00\x00\x00" /* length : 0 for now */
1025 "\x04\x00" /* type : 4 (settings), flags : 0 */
1026 "\x00\x00\x00\x00", /* stream ID : 0 */
1027 9);
1028
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001029 if (h2c->flags & H2_CF_IS_BACK) {
1030 /* send settings_enable_push=0 */
1031 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1032 }
1033
Willy Tarreaube5b7152017-09-25 16:25:39 +02001034 if (h2_settings_header_table_size != 4096) {
1035 char str[6] = "\x00\x01"; /* header_table_size */
1036
1037 write_n32(str + 2, h2_settings_header_table_size);
1038 chunk_memcat(&buf, str, 6);
1039 }
1040
1041 if (h2_settings_initial_window_size != 65535) {
1042 char str[6] = "\x00\x04"; /* initial_window_size */
1043
1044 write_n32(str + 2, h2_settings_initial_window_size);
1045 chunk_memcat(&buf, str, 6);
1046 }
1047
1048 if (h2_settings_max_concurrent_streams != 0) {
1049 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1050
1051 /* Note: 0 means "unlimited" for haproxy's config but not for
1052 * the protocol, so never send this value!
1053 */
1054 write_n32(str + 2, h2_settings_max_concurrent_streams);
1055 chunk_memcat(&buf, str, 6);
1056 }
1057
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001058 mfs = h2_settings_max_frame_size;
1059 if (mfs > global.tune.bufsize)
1060 mfs = global.tune.bufsize;
1061
1062 if (!mfs)
1063 mfs = global.tune.bufsize;
1064
1065 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066 char str[6] = "\x00\x05"; /* max_frame_size */
1067
1068 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1069 * match bufsize - rewrite size, but at the moment it seems
1070 * that clients don't take care of it.
1071 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001072 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001073 chunk_memcat(&buf, str, 6);
1074 }
1075
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001076 h2_set_frame_size(buf.area, buf.data - 9);
1077 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001078 if (unlikely(ret <= 0)) {
1079 if (!ret) {
1080 h2c->flags |= H2_CF_MUX_MFULL;
1081 h2c->flags |= H2_CF_DEM_MROOM;
1082 return 0;
1083 }
1084 else {
1085 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1086 return 0;
1087 }
1088 }
1089 return ret;
1090}
1091
Willy Tarreau52eed752017-09-22 15:05:09 +02001092/* Try to receive a connection preface, then upon success try to send our
1093 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1094 * missing data. It may return an error in h2c.
1095 */
1096static int h2c_frt_recv_preface(struct h2c *h2c)
1097{
1098 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001099 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001100
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001101 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001102
1103 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001104 if (ret1 < 0)
1105 sess_log(h2c->conn->owner);
1106
Willy Tarreau52eed752017-09-22 15:05:09 +02001107 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1108 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1109 return 0;
1110 }
1111
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001112 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001113 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001114 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001115
Willy Tarreaube5b7152017-09-25 16:25:39 +02001116 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001117}
1118
Willy Tarreau01b44822018-10-03 14:26:37 +02001119/* Try to send a connection preface, then upon success try to send our
1120 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1121 * missing data. It may return an error in h2c.
1122 */
1123static int h2c_bck_send_preface(struct h2c *h2c)
1124{
1125 struct buffer *res;
1126
1127 if (h2c_mux_busy(h2c, NULL)) {
1128 h2c->flags |= H2_CF_DEM_MBUSY;
1129 return 0;
1130 }
1131
1132 res = h2_get_buf(h2c, &h2c->mbuf);
1133 if (!res) {
1134 h2c->flags |= H2_CF_MUX_MALLOC;
1135 h2c->flags |= H2_CF_DEM_MROOM;
1136 return 0;
1137 }
1138
1139 if (!b_data(res)) {
1140 /* preface not yet sent */
1141 b_istput(res, ist(H2_CONN_PREFACE));
1142 }
1143
1144 return h2c_send_settings(h2c);
1145}
1146
Willy Tarreau081d4722017-05-16 21:51:05 +02001147/* try to send a GOAWAY frame on the connection to report an error or a graceful
1148 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1149 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1150 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1151 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1152 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1153 * on unrecoverable failure. It will not attempt to send one again in this last
1154 * case so that it is safe to use h2c_error() to report such errors.
1155 */
1156static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1157{
1158 struct buffer *res;
1159 char str[17];
1160 int ret;
1161
1162 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1163 return 1; // claim that it worked
1164
1165 if (h2c_mux_busy(h2c, h2s)) {
1166 if (h2s)
1167 h2s->flags |= H2_SF_BLK_MBUSY;
1168 else
1169 h2c->flags |= H2_CF_DEM_MBUSY;
1170 return 0;
1171 }
1172
Willy Tarreau44e973f2018-03-01 17:49:30 +01001173 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001174 if (!res) {
1175 h2c->flags |= H2_CF_MUX_MALLOC;
1176 if (h2s)
1177 h2s->flags |= H2_SF_BLK_MROOM;
1178 else
1179 h2c->flags |= H2_CF_DEM_MROOM;
1180 return 0;
1181 }
1182
1183 /* len: 8, type: 7, flags: none, sid: 0 */
1184 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1185
1186 if (h2c->last_sid < 0)
1187 h2c->last_sid = h2c->max_id;
1188
1189 write_n32(str + 9, h2c->last_sid);
1190 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001191 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001192 if (unlikely(ret <= 0)) {
1193 if (!ret) {
1194 h2c->flags |= H2_CF_MUX_MFULL;
1195 if (h2s)
1196 h2s->flags |= H2_SF_BLK_MROOM;
1197 else
1198 h2c->flags |= H2_CF_DEM_MROOM;
1199 return 0;
1200 }
1201 else {
1202 /* we cannot report this error using GOAWAY, so we mark
1203 * it and claim a success.
1204 */
1205 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1206 h2c->flags |= H2_CF_GOAWAY_FAILED;
1207 return 1;
1208 }
1209 }
1210 h2c->flags |= H2_CF_GOAWAY_SENT;
1211 return ret;
1212}
1213
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001214/* Try to send an RST_STREAM frame on the connection for the indicated stream
1215 * during mux operations. This stream must be valid and cannot be closed
1216 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1217 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1218 * not yet.
1219 *
1220 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1221 * to write the message, it subscribes the stream to future notifications.
1222 */
1223static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1224{
1225 struct buffer *res;
1226 char str[13];
1227 int ret;
1228
1229 if (!h2s || h2s->st == H2_SS_CLOSED)
1230 return 1;
1231
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001232 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1233 * RST_STREAM in response to a RST_STREAM frame.
1234 */
1235 if (h2c->dft == H2_FT_RST_STREAM) {
1236 ret = 1;
1237 goto ignore;
1238 }
1239
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001240 if (h2c_mux_busy(h2c, h2s)) {
1241 h2s->flags |= H2_SF_BLK_MBUSY;
1242 return 0;
1243 }
1244
Willy Tarreau44e973f2018-03-01 17:49:30 +01001245 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246 if (!res) {
1247 h2c->flags |= H2_CF_MUX_MALLOC;
1248 h2s->flags |= H2_SF_BLK_MROOM;
1249 return 0;
1250 }
1251
1252 /* len: 4, type: 3, flags: none */
1253 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1254 write_n32(str + 5, h2s->id);
1255 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001256 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001257
1258 if (unlikely(ret <= 0)) {
1259 if (!ret) {
1260 h2c->flags |= H2_CF_MUX_MFULL;
1261 h2s->flags |= H2_SF_BLK_MROOM;
1262 return 0;
1263 }
1264 else {
1265 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1266 return 0;
1267 }
1268 }
1269
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001270 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001271 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001272 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001273 return ret;
1274}
1275
1276/* Try to send an RST_STREAM frame on the connection for the stream being
1277 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001278 * error code, even if the stream is one of the dummy ones, and will update
1279 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001280 *
1281 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1282 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001283 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001284 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001285 */
1286static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1287{
1288 struct buffer *res;
1289 char str[13];
1290 int ret;
1291
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001292 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1293 * RST_STREAM in response to a RST_STREAM frame.
1294 */
1295 if (h2c->dft == H2_FT_RST_STREAM) {
1296 ret = 1;
1297 goto ignore;
1298 }
1299
Willy Tarreau27a84c92017-10-17 08:10:17 +02001300 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001301 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001302 return 0;
1303 }
1304
Willy Tarreau44e973f2018-03-01 17:49:30 +01001305 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001306 if (!res) {
1307 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001308 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001309 return 0;
1310 }
1311
1312 /* len: 4, type: 3, flags: none */
1313 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001314
Willy Tarreau27a84c92017-10-17 08:10:17 +02001315 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001316 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001317 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001318
Willy Tarreau27a84c92017-10-17 08:10:17 +02001319 if (unlikely(ret <= 0)) {
1320 if (!ret) {
1321 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001322 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001323 return 0;
1324 }
1325 else {
1326 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1327 return 0;
1328 }
1329 }
1330
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001331 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001332 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001334 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001335 }
1336
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 return ret;
1338}
1339
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001340/* try to send an empty DATA frame with the ES flag set to notify about the
1341 * end of stream and match a shutdown(write). If an ES was already sent as
1342 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1343 * on success or zero if nothing was done. In case of lack of room to write the
1344 * message, it subscribes the requesting stream to future notifications.
1345 */
1346static int h2_send_empty_data_es(struct h2s *h2s)
1347{
1348 struct h2c *h2c = h2s->h2c;
1349 struct buffer *res;
1350 char str[9];
1351 int ret;
1352
Willy Tarreau721c9742017-11-07 11:05:42 +01001353 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001354 return 1;
1355
1356 if (h2c_mux_busy(h2c, h2s)) {
1357 h2s->flags |= H2_SF_BLK_MBUSY;
1358 return 0;
1359 }
1360
Willy Tarreau44e973f2018-03-01 17:49:30 +01001361 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001362 if (!res) {
1363 h2c->flags |= H2_CF_MUX_MALLOC;
1364 h2s->flags |= H2_SF_BLK_MROOM;
1365 return 0;
1366 }
1367
1368 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1369 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1370 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001371 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001372 if (likely(ret > 0)) {
1373 h2s->flags |= H2_SF_ES_SENT;
1374 }
1375 else if (!ret) {
1376 h2c->flags |= H2_CF_MUX_MFULL;
1377 h2s->flags |= H2_SF_BLK_MROOM;
1378 return 0;
1379 }
1380 else {
1381 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1382 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001383 }
1384 return ret;
1385}
1386
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001387/* wake the streams attached to the connection, whose id is greater than <last>,
1388 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001389 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1390 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001391 */
1392static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1393{
1394 struct eb32_node *node;
1395 struct h2s *h2s;
1396
1397 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001398 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001399
1400 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001401 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001402
1403 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1404 while (node) {
1405 h2s = container_of(node, struct h2s, by_id);
1406 if (h2s->id <= last)
1407 break;
1408 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001409
1410 if (!h2s->cs) {
1411 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001412 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001413 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001414 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001415
1416 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001417 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1418 h2s->cs->flags |= CS_FL_ERROR;
1419
Willy Tarreauf830f012018-12-19 17:44:55 +01001420 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001421
Willy Tarreaua8519352018-12-18 16:44:28 +01001422 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001423 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001424 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001425 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001426 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001427 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001428 }
1429}
1430
Willy Tarreau3421aba2017-07-27 15:41:03 +02001431/* Increase all streams' outgoing window size by the difference passed in
1432 * argument. This is needed upon receipt of the settings frame if the initial
1433 * window size is different. The difference may be negative and the resulting
1434 * window size as well, for the time it takes to receive some window updates.
1435 */
1436static void h2c_update_all_ws(struct h2c *h2c, int diff)
1437{
1438 struct h2s *h2s;
1439 struct eb32_node *node;
1440
1441 if (!diff)
1442 return;
1443
1444 node = eb32_first(&h2c->streams_by_id);
1445 while (node) {
1446 h2s = container_of(node, struct h2s, by_id);
1447 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001448
1449 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1450 h2s->flags &= ~H2_SF_BLK_SFCTL;
1451 if (h2s->send_wait)
1452 LIST_ADDQ(&h2c->send_list, &h2s->list);
1453
1454 }
1455
Willy Tarreau3421aba2017-07-27 15:41:03 +02001456 node = eb32_next(node);
1457 }
1458}
1459
1460/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1461 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001462 * return an error in h2c. The caller must have already verified frame length
1463 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001464 */
1465static int h2c_handle_settings(struct h2c *h2c)
1466{
1467 unsigned int offset;
1468 int error;
1469
1470 if (h2c->dff & H2_F_SETTINGS_ACK) {
1471 if (h2c->dfl) {
1472 error = H2_ERR_FRAME_SIZE_ERROR;
1473 goto fail;
1474 }
1475 return 1;
1476 }
1477
Willy Tarreau3421aba2017-07-27 15:41:03 +02001478 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001479 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001480 return 0;
1481
1482 /* parse the frame */
1483 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001484 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1485 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001486
1487 switch (type) {
1488 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1489 /* we need to update all existing streams with the
1490 * difference from the previous iws.
1491 */
1492 if (arg < 0) { // RFC7540#6.5.2
1493 error = H2_ERR_FLOW_CONTROL_ERROR;
1494 goto fail;
1495 }
1496 h2c_update_all_ws(h2c, arg - h2c->miw);
1497 h2c->miw = arg;
1498 break;
1499 case H2_SETTINGS_MAX_FRAME_SIZE:
1500 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1501 error = H2_ERR_PROTOCOL_ERROR;
1502 goto fail;
1503 }
1504 h2c->mfs = arg;
1505 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001506 case H2_SETTINGS_ENABLE_PUSH:
1507 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1508 error = H2_ERR_PROTOCOL_ERROR;
1509 goto fail;
1510 }
1511 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001512 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1513 if (h2c->flags & H2_CF_IS_BACK) {
1514 /* the limit is only for the backend; for the frontend it is our limit */
1515 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1516 arg = h2_settings_max_concurrent_streams;
1517 h2c->streams_limit = arg;
1518 }
1519 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001520 }
1521 }
1522
1523 /* need to ACK this frame now */
1524 h2c->st0 = H2_CS_FRAME_A;
1525 return 1;
1526 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001527 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 h2c_error(h2c, error);
1529 return 0;
1530}
1531
1532/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1533 * success or one of the h2_status values.
1534 */
1535static int h2c_ack_settings(struct h2c *h2c)
1536{
1537 struct buffer *res;
1538 char str[9];
1539 int ret = -1;
1540
1541 if (h2c_mux_busy(h2c, NULL)) {
1542 h2c->flags |= H2_CF_DEM_MBUSY;
1543 return 0;
1544 }
1545
Willy Tarreau44e973f2018-03-01 17:49:30 +01001546 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001547 if (!res) {
1548 h2c->flags |= H2_CF_MUX_MALLOC;
1549 h2c->flags |= H2_CF_DEM_MROOM;
1550 return 0;
1551 }
1552
1553 memcpy(str,
1554 "\x00\x00\x00" /* length : 0 (no data) */
1555 "\x04" "\x01" /* type : 4, flags : ACK */
1556 "\x00\x00\x00\x00" /* stream ID */, 9);
1557
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001558 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001559 if (unlikely(ret <= 0)) {
1560 if (!ret) {
1561 h2c->flags |= H2_CF_MUX_MFULL;
1562 h2c->flags |= H2_CF_DEM_MROOM;
1563 return 0;
1564 }
1565 else {
1566 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1567 return 0;
1568 }
1569 }
1570 return ret;
1571}
1572
Willy Tarreaucf68c782017-10-10 17:11:41 +02001573/* processes a PING frame and schedules an ACK if needed. The caller must pass
1574 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001575 * missing data. The caller must have already verified frame length
1576 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001577 */
1578static int h2c_handle_ping(struct h2c *h2c)
1579{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001580 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001581 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001582 h2c->st0 = H2_CS_FRAME_A;
1583 return 1;
1584}
1585
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001586/* Try to send a window update for stream id <sid> and value <increment>.
1587 * Returns > 0 on success or zero on missing room or failure. It may return an
1588 * error in h2c.
1589 */
1590static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1591{
1592 struct buffer *res;
1593 char str[13];
1594 int ret = -1;
1595
1596 if (h2c_mux_busy(h2c, NULL)) {
1597 h2c->flags |= H2_CF_DEM_MBUSY;
1598 return 0;
1599 }
1600
Willy Tarreau44e973f2018-03-01 17:49:30 +01001601 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001602 if (!res) {
1603 h2c->flags |= H2_CF_MUX_MALLOC;
1604 h2c->flags |= H2_CF_DEM_MROOM;
1605 return 0;
1606 }
1607
1608 /* length: 4, type: 8, flags: none */
1609 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1610 write_n32(str + 5, sid);
1611 write_n32(str + 9, increment);
1612
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001613 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001614
1615 if (unlikely(ret <= 0)) {
1616 if (!ret) {
1617 h2c->flags |= H2_CF_MUX_MFULL;
1618 h2c->flags |= H2_CF_DEM_MROOM;
1619 return 0;
1620 }
1621 else {
1622 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1623 return 0;
1624 }
1625 }
1626 return ret;
1627}
1628
1629/* try to send pending window update for the connection. It's safe to call it
1630 * with no pending updates. Returns > 0 on success or zero on missing room or
1631 * failure. It may return an error in h2c.
1632 */
1633static int h2c_send_conn_wu(struct h2c *h2c)
1634{
1635 int ret = 1;
1636
1637 if (h2c->rcvd_c <= 0)
1638 return 1;
1639
Willy Tarreau97aaa672018-12-23 09:49:04 +01001640 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1641 /* increase the advertised connection window to 2G on
1642 * first update.
1643 */
1644 h2c->flags |= H2_CF_WINDOW_OPENED;
1645 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1646 }
1647
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001648 /* send WU for the connection */
1649 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1650 if (ret > 0)
1651 h2c->rcvd_c = 0;
1652
1653 return ret;
1654}
1655
1656/* try to send pending window update for the current dmux stream. It's safe to
1657 * call it with no pending updates. Returns > 0 on success or zero on missing
1658 * room or failure. It may return an error in h2c.
1659 */
1660static int h2c_send_strm_wu(struct h2c *h2c)
1661{
1662 int ret = 1;
1663
1664 if (h2c->rcvd_s <= 0)
1665 return 1;
1666
1667 /* send WU for the stream */
1668 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1669 if (ret > 0)
1670 h2c->rcvd_s = 0;
1671
1672 return ret;
1673}
1674
Willy Tarreaucf68c782017-10-10 17:11:41 +02001675/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1676 * success, 0 on missing data or one of the h2_status values.
1677 */
1678static int h2c_ack_ping(struct h2c *h2c)
1679{
1680 struct buffer *res;
1681 char str[17];
1682 int ret = -1;
1683
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001684 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001685 return 0;
1686
1687 if (h2c_mux_busy(h2c, NULL)) {
1688 h2c->flags |= H2_CF_DEM_MBUSY;
1689 return 0;
1690 }
1691
Willy Tarreau44e973f2018-03-01 17:49:30 +01001692 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001693 if (!res) {
1694 h2c->flags |= H2_CF_MUX_MALLOC;
1695 h2c->flags |= H2_CF_DEM_MROOM;
1696 return 0;
1697 }
1698
1699 memcpy(str,
1700 "\x00\x00\x08" /* length : 8 (same payload) */
1701 "\x06" "\x01" /* type : 6, flags : ACK */
1702 "\x00\x00\x00\x00" /* stream ID */, 9);
1703
1704 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001705 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001706
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001707 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001708 if (unlikely(ret <= 0)) {
1709 if (!ret) {
1710 h2c->flags |= H2_CF_MUX_MFULL;
1711 h2c->flags |= H2_CF_DEM_MROOM;
1712 return 0;
1713 }
1714 else {
1715 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1716 return 0;
1717 }
1718 }
1719 return ret;
1720}
1721
Willy Tarreau26f95952017-07-27 17:18:30 +02001722/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1723 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001724 * h2c or h2s. The caller must have already verified frame length and stream ID
1725 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001726 */
1727static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1728{
1729 int32_t inc;
1730 int error;
1731
Willy Tarreau26f95952017-07-27 17:18:30 +02001732 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001734 return 0;
1735
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001736 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001737
1738 if (h2c->dsi != 0) {
1739 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001740
1741 /* it's not an error to receive WU on a closed stream */
1742 if (h2s->st == H2_SS_CLOSED)
1743 return 1;
1744
1745 if (!inc) {
1746 error = H2_ERR_PROTOCOL_ERROR;
1747 goto strm_err;
1748 }
1749
1750 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1751 error = H2_ERR_FLOW_CONTROL_ERROR;
1752 goto strm_err;
1753 }
1754
1755 h2s->mws += inc;
1756 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1757 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001758 if (h2s->send_wait)
1759 LIST_ADDQ(&h2c->send_list, &h2s->list);
1760
Willy Tarreau26f95952017-07-27 17:18:30 +02001761 }
1762 }
1763 else {
1764 /* connection window update */
1765 if (!inc) {
1766 error = H2_ERR_PROTOCOL_ERROR;
1767 goto conn_err;
1768 }
1769
1770 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1771 error = H2_ERR_FLOW_CONTROL_ERROR;
1772 goto conn_err;
1773 }
1774
1775 h2c->mws += inc;
1776 }
1777
1778 return 1;
1779
1780 conn_err:
1781 h2c_error(h2c, error);
1782 return 0;
1783
1784 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001785 h2s_error(h2s, error);
1786 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001787 return 0;
1788}
1789
Willy Tarreaue96b0922017-10-30 00:28:29 +01001790/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001791 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1792 * have already verified frame length and stream ID validity. Described in
1793 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001794 */
1795static int h2c_handle_goaway(struct h2c *h2c)
1796{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001797 int last;
1798
Willy Tarreaue96b0922017-10-30 00:28:29 +01001799 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001800 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001801 return 0;
1802
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001803 last = h2_get_n32(&h2c->dbuf, 0);
1804 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001805 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001806 if (h2c->last_sid < 0)
1807 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001808 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001809}
1810
Willy Tarreau92153fc2017-12-03 19:46:19 +01001811/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001812 * invalid. Returns > 0 on success or zero on missing data. It may return an
1813 * error in h2c. The caller must have already verified frame length and stream
1814 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001815 */
1816static int h2c_handle_priority(struct h2c *h2c)
1817{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001818 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001819 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001820 return 0;
1821
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001822 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001823 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001824 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1825 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001826 }
1827 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001828}
1829
Willy Tarreaucd234e92017-08-18 10:59:39 +02001830/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001831 * Returns > 0 on success or zero on missing data. The caller must have already
1832 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001833 */
1834static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1835{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001836 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001837 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001838 return 0;
1839
1840 /* late RST, already handled */
1841 if (h2s->st == H2_SS_CLOSED)
1842 return 1;
1843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001845 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001846
1847 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001848 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001849 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001850 }
1851
1852 h2s->flags |= H2_SF_RST_RCVD;
1853 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001854}
1855
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001856/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1857 * It may return an error in h2c or h2s. The caller must consider that the
1858 * return value is the new h2s in case one was allocated (most common case).
1859 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001860 * errors here are reported as connection errors since it's impossible to
1861 * recover from such errors after the compression context has been altered.
1862 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001863static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001864{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001865 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001866 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001867 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001868 int error;
1869
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001871 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001874 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001875
1876 /* now either the frame is complete or the buffer is complete */
1877 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001878 /* The stream exists/existed, this must be a trailers frame */
1879 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001880 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001881 goto out;
1882 goto done;
1883 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001884 /* the connection was already killed by an RST, let's consume
1885 * the data and send another RST.
1886 */
1887 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1888 h2s = (struct h2s*)h2_error_stream;
1889 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001890 }
1891 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1892 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1893 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001894 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001895 goto conn_err;
1896 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001897 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1898 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001899
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001900 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001901
Willy Tarreau25919232019-01-03 14:48:18 +01001902 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001903 if (h2c->st0 >= H2_CS_ERROR)
1904 goto out;
1905
Willy Tarreau25919232019-01-03 14:48:18 +01001906 if (error <= 0) {
1907 if (error == 0)
1908 goto out; // missing data
1909
1910 /* Failed to decode this stream (e.g. too large request)
1911 * but the HPACK decompressor is still synchronized.
1912 */
1913 h2s = (struct h2s*)h2_error_stream;
1914 goto send_rst;
1915 }
1916
Willy Tarreau22de8d32018-09-05 19:55:58 +02001917 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001918 * positively from h2c_frt_stream_new(), the stream will report the error,
1919 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001920 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001921 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001922 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001923 h2s = (struct h2s*)h2_refused_stream;
1924 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001925 }
1926
1927 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001928 h2s->rxbuf = rxbuf;
1929 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001930 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001931
Willy Tarreau88d138e2019-01-02 19:38:14 +01001932 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001933 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001934 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001935
1936 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001937 if (h2s->st == H2_SS_OPEN)
1938 h2s->st = H2_SS_HREM;
1939 else
1940 h2s_close(h2s);
Willy Tarreau39d68502018-03-02 12:26:37 +01001941 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001942 }
1943
Willy Tarreau3a429f02019-01-03 11:41:50 +01001944 /* update the max stream ID if the request is being processed */
1945 if (h2s->id > h2c->max_id)
1946 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001947
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001948 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001949
1950 conn_err:
1951 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001952 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001953
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001954 out:
1955 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001956 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001957
1958 send_rst:
1959 /* make the demux send an RST for the current stream. We may only
1960 * do this if we're certain that the HEADERS frame was properly
1961 * decompressed so that the HPACK decoder is still kept up to date.
1962 */
1963 h2_release_buf(h2c, &rxbuf);
1964 h2c->st0 = H2_CS_FRAME_E;
1965 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001966}
1967
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001968/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1969 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1970 * errors here are reported as connection errors since it's impossible to
1971 * recover from such errors after the compression context has been altered.
1972 */
1973static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1974{
1975 int error;
1976
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001977 if (!b_size(&h2c->dbuf))
1978 return NULL; // empty buffer
1979
1980 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1981 return NULL; // incomplete frame
1982
Willy Tarreau1915ca22019-01-24 11:49:37 +01001983 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001984
Willy Tarreau25919232019-01-03 14:48:18 +01001985 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001986 if (h2c->st0 >= H2_CS_ERROR)
1987 return NULL;
1988
Willy Tarreau08bb1d62019-01-30 16:55:48 +01001989 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1990 /* RFC7540#5.1 */
1991 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1992 h2c->st0 = H2_CS_FRAME_E;
1993 return NULL;
1994 }
1995
Willy Tarreau25919232019-01-03 14:48:18 +01001996 if (error <= 0) {
1997 if (error == 0)
1998 return NULL; // missing data
1999
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002000 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002001 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002002 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002003 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002004 }
2005
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002006 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2007 h2s->flags |= H2_SF_ES_RCVD;
2008 h2s->cs->flags |= CS_FL_REOS;
2009 }
2010
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002011 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2012 h2s->st = H2_SS_ERROR;
2013 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2014 h2s->st = H2_SS_HREM;
2015 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2016 h2s_close(h2s);
2017
2018 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002019}
2020
Willy Tarreau454f9052017-10-26 19:40:35 +02002021/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2022 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2023 */
2024static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2025{
2026 int error;
2027
2028 /* note that empty DATA frames are perfectly valid and sometimes used
2029 * to signal an end of stream (with the ES flag).
2030 */
2031
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002032 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002033 return 0; // empty buffer
2034
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002035 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002036 return 0; // incomplete frame
2037
2038 /* now either the frame is complete or the buffer is complete */
2039
Willy Tarreau454f9052017-10-26 19:40:35 +02002040 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2041 /* RFC7540#6.1 */
2042 error = H2_ERR_STREAM_CLOSED;
2043 goto strm_err;
2044 }
2045
Willy Tarreau1915ca22019-01-24 11:49:37 +01002046 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2047 /* RFC7540#8.1.2 */
2048 error = H2_ERR_PROTOCOL_ERROR;
2049 goto strm_err;
2050 }
2051
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002052 if (!h2_frt_transfer_data(h2s))
2053 return 0;
2054
Willy Tarreau454f9052017-10-26 19:40:35 +02002055 /* call the upper layers to process the frame, then let the upper layer
2056 * notify the stream about any change.
2057 */
2058 if (!h2s->cs) {
2059 error = H2_ERR_STREAM_CLOSED;
2060 goto strm_err;
2061 }
2062
Willy Tarreau8f650c32017-11-21 19:36:21 +01002063 if (h2c->st0 >= H2_CS_ERROR)
2064 return 0;
2065
Willy Tarreau721c9742017-11-07 11:05:42 +01002066 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002067 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002068 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002069 }
2070
2071 /* check for completion : the callee will change this to FRAME_A or
2072 * FRAME_H once done.
2073 */
2074 if (h2c->st0 == H2_CS_FRAME_P)
2075 return 0;
2076
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002077 /* last frame */
2078 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002079 if (h2s->st == H2_SS_OPEN)
2080 h2s->st = H2_SS_HREM;
2081 else
2082 h2s_close(h2s);
2083
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002084 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002085 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002086
2087 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2088 /* RFC7540#8.1.2 */
2089 error = H2_ERR_PROTOCOL_ERROR;
2090 goto strm_err;
2091 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002092 }
2093
Willy Tarreau454f9052017-10-26 19:40:35 +02002094 return 1;
2095
Willy Tarreau454f9052017-10-26 19:40:35 +02002096 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002097 h2s_error(h2s, error);
2098 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002099 return 0;
2100}
2101
Willy Tarreaubc933932017-10-09 16:21:43 +02002102/* process Rx frames to be demultiplexed */
2103static void h2_process_demux(struct h2c *h2c)
2104{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002105 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002106 struct h2_fh hdr;
2107 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002108
Willy Tarreau081d4722017-05-16 21:51:05 +02002109 if (h2c->st0 >= H2_CS_ERROR)
2110 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002111
2112 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2113 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002114 if (h2c->flags & H2_CF_IS_BACK)
2115 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002116 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2117 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002118 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002119 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002120 sess_log(h2c->conn->owner);
2121 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002122 goto fail;
2123 }
2124
2125 h2c->max_id = 0;
2126 h2c->st0 = H2_CS_SETTINGS1;
2127 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002128
2129 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002130 /* ensure that what is pending is a valid SETTINGS frame
2131 * without an ACK.
2132 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002133 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002134 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002135 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002136 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002137 sess_log(h2c->conn->owner);
2138 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002139 goto fail;
2140 }
2141
2142 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2143 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2144 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2145 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002146 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002147 goto fail;
2148 }
2149
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002150 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002151 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2152 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2153 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002154 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002155 goto fail;
2156 }
2157
Willy Tarreau3bf69182018-12-21 15:34:50 +01002158 /* that's OK, switch to FRAME_P to process it. This is
2159 * a SETTINGS frame whose header has already been
2160 * deleted above.
2161 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002162 padlen = 0;
2163 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002164 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002165 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002166
2167 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002168 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002169 int ret = 0;
2170
2171 if (h2c->st0 >= H2_CS_ERROR)
2172 break;
2173
2174 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002175 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002176 break;
2177
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002178 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002179 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002180 if (!h2c->nb_streams) {
2181 /* only log if no other stream can report the error */
2182 sess_log(h2c->conn->owner);
2183 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002184 break;
2185 }
2186
Willy Tarreau3bf69182018-12-21 15:34:50 +01002187 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2188 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2189 * we read the pad length and drop it from the remaining
2190 * payload (one byte + the 9 remaining ones = 10 total
2191 * removed), so we have a frame payload starting after the
2192 * pad len. Flow controlled frames (DATA) also count the
2193 * padlen in the flow control, so it must be adjusted.
2194 */
2195 if (hdr.len < 1) {
2196 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2197 sess_log(h2c->conn->owner);
2198 goto fail;
2199 }
2200 hdr.len--;
2201
2202 if (b_data(&h2c->dbuf) < 10)
2203 break; // missing padlen
2204
2205 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2206
2207 if (padlen > hdr.len) {
2208 /* RFC7540#6.1 : pad length = length of
2209 * frame payload or greater => error.
2210 */
2211 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2212 sess_log(h2c->conn->owner);
2213 goto fail;
2214 }
2215
2216 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2217 h2c->rcvd_c++;
2218 h2c->rcvd_s++;
2219 }
2220 b_del(&h2c->dbuf, 1);
2221 }
2222 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002223
2224 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002225 h2c->dfl = hdr.len;
2226 h2c->dsi = hdr.sid;
2227 h2c->dft = hdr.ft;
2228 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002229 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002230 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002231
2232 /* check for minimum basic frame format validity */
2233 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2234 if (ret != H2_ERR_NO_ERROR) {
2235 h2c_error(h2c, ret);
2236 sess_log(h2c->conn->owner);
2237 goto fail;
2238 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002239 }
2240
2241 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002242 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2243
Willy Tarreau567beb82018-12-18 16:52:44 +01002244 if (tmp_h2s != h2s && h2s && h2s->cs &&
2245 (b_data(&h2s->rxbuf) ||
2246 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002247 /* we may have to signal the upper layers */
2248 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002249 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002250 }
2251 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002252
Willy Tarreaud7901432017-12-29 11:34:40 +01002253 if (h2c->st0 == H2_CS_FRAME_E)
2254 goto strm_err;
2255
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002256 if (h2s->st == H2_SS_IDLE &&
2257 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2258 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2259 * this state MUST be treated as a connection error
2260 */
2261 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002262 if (!h2c->nb_streams) {
2263 /* only log if no other stream can report the error */
2264 sess_log(h2c->conn->owner);
2265 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002266 break;
2267 }
2268
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002269 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2270 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2271 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002272 * this state MUST be treated as a stream error.
2273 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2274 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002275 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002276 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2277 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2278 else
2279 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002280 goto strm_err;
2281 }
2282
Willy Tarreauab837502017-12-27 15:07:30 +01002283 /* Below the management of frames received in closed state is a
2284 * bit hackish because the spec makes strong differences between
2285 * streams closed by receiving RST, sending RST, and seeing ES
2286 * in both directions. In addition to this, the creation of a
2287 * new stream reusing the identifier of a closed one will be
2288 * detected here. Given that we cannot keep track of all closed
2289 * streams forever, we consider that unknown closed streams were
2290 * closed on RST received, which allows us to respond with an
2291 * RST without breaking the connection (eg: to abort a transfer).
2292 * Some frames have to be silently ignored as well.
2293 */
2294 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002295 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002296 /* #5.1.1: The identifier of a newly
2297 * established stream MUST be numerically
2298 * greater than all streams that the initiating
2299 * endpoint has opened or reserved. This
2300 * governs streams that are opened using a
2301 * HEADERS frame and streams that are reserved
2302 * using PUSH_PROMISE. An endpoint that
2303 * receives an unexpected stream identifier
2304 * MUST respond with a connection error.
2305 */
2306 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2307 goto strm_err;
2308 }
2309
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002310 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002311 /* RFC7540#5.1:closed: an endpoint that
2312 * receives any frame other than PRIORITY after
2313 * receiving a RST_STREAM MUST treat that as a
2314 * stream error of type STREAM_CLOSED.
2315 *
2316 * Note that old streams fall into this category
2317 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002318 *
2319 * However, we cannot generalize this to all frame types. Those
2320 * carrying compression state must still be processed before
2321 * being dropped or we'll desynchronize the decoder. This can
2322 * happen with request trailers received after sending an
2323 * RST_STREAM, or with header/trailers responses received after
2324 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002325 */
2326 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2327 h2c->st0 = H2_CS_FRAME_E;
2328 goto strm_err;
2329 }
2330
2331 /* RFC7540#5.1:closed: if this state is reached as a
2332 * result of sending a RST_STREAM frame, the peer that
2333 * receives the RST_STREAM might have already sent
2334 * frames on the stream that cannot be withdrawn. An
2335 * endpoint MUST ignore frames that it receives on
2336 * closed streams after it has sent a RST_STREAM
2337 * frame. An endpoint MAY choose to limit the period
2338 * over which it ignores frames and treat frames that
2339 * arrive after this time as being in error.
2340 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002341 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002342 /* RFC7540#5.1:closed: any frame other than
2343 * PRIO/WU/RST in this state MUST be treated as
2344 * a connection error
2345 */
2346 if (h2c->dft != H2_FT_RST_STREAM &&
2347 h2c->dft != H2_FT_PRIORITY &&
2348 h2c->dft != H2_FT_WINDOW_UPDATE) {
2349 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2350 goto strm_err;
2351 }
2352 }
2353 }
2354
Willy Tarreauc0da1962017-10-30 18:38:00 +01002355#if 0
2356 // problem below: it is not possible to completely ignore such
2357 // streams as we need to maintain the compression state as well
2358 // and for this we need to completely process these frames (eg:
2359 // HEADERS frames) as well as counting DATA frames to emit
2360 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2361 // This is a typical case of layer violation where the
2362 // transported contents are critical to the connection's
2363 // validity and must be ignored at the same time :-(
2364
2365 /* graceful shutdown, ignore streams whose ID is higher than
2366 * the one advertised in GOAWAY. RFC7540#6.8.
2367 */
2368 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002369 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2370 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002371 h2c->dfl -= ret;
2372 ret = h2c->dfl == 0;
2373 goto strm_err;
2374 }
2375#endif
2376
Willy Tarreau7e98c052017-10-10 15:56:59 +02002377 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002378 case H2_FT_SETTINGS:
2379 if (h2c->st0 == H2_CS_FRAME_P)
2380 ret = h2c_handle_settings(h2c);
2381
2382 if (h2c->st0 == H2_CS_FRAME_A)
2383 ret = h2c_ack_settings(h2c);
2384 break;
2385
Willy Tarreaucf68c782017-10-10 17:11:41 +02002386 case H2_FT_PING:
2387 if (h2c->st0 == H2_CS_FRAME_P)
2388 ret = h2c_handle_ping(h2c);
2389
2390 if (h2c->st0 == H2_CS_FRAME_A)
2391 ret = h2c_ack_ping(h2c);
2392 break;
2393
Willy Tarreau26f95952017-07-27 17:18:30 +02002394 case H2_FT_WINDOW_UPDATE:
2395 if (h2c->st0 == H2_CS_FRAME_P)
2396 ret = h2c_handle_window_update(h2c, h2s);
2397 break;
2398
Willy Tarreau61290ec2017-10-17 08:19:21 +02002399 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002400 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2401 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2402 * frames' parsers consume all following CONTINUATION
2403 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002404 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002405 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2406 sess_log(h2c->conn->owner);
2407 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002408
Willy Tarreau13278b42017-10-13 19:23:14 +02002409 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002410 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002411 if (h2c->flags & H2_CF_IS_BACK)
2412 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2413 else
2414 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002415 if (tmp_h2s) {
2416 h2s = tmp_h2s;
2417 ret = 1;
2418 }
2419 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002420 break;
2421
Willy Tarreau454f9052017-10-26 19:40:35 +02002422 case H2_FT_DATA:
2423 if (h2c->st0 == H2_CS_FRAME_P)
2424 ret = h2c_frt_handle_data(h2c, h2s);
2425
2426 if (h2c->st0 == H2_CS_FRAME_A)
2427 ret = h2c_send_strm_wu(h2c);
2428 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002429
Willy Tarreau92153fc2017-12-03 19:46:19 +01002430 case H2_FT_PRIORITY:
2431 if (h2c->st0 == H2_CS_FRAME_P)
2432 ret = h2c_handle_priority(h2c);
2433 break;
2434
Willy Tarreaucd234e92017-08-18 10:59:39 +02002435 case H2_FT_RST_STREAM:
2436 if (h2c->st0 == H2_CS_FRAME_P)
2437 ret = h2c_handle_rst_stream(h2c, h2s);
2438 break;
2439
Willy Tarreaue96b0922017-10-30 00:28:29 +01002440 case H2_FT_GOAWAY:
2441 if (h2c->st0 == H2_CS_FRAME_P)
2442 ret = h2c_handle_goaway(h2c);
2443 break;
2444
Willy Tarreau1c661982017-10-30 13:52:01 +01002445 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002446 default:
2447 /* drop frames that we ignore. They may be larger than
2448 * the buffer so we drain all of their contents until
2449 * we reach the end.
2450 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002451 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2452 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002453 h2c->dfl -= ret;
2454 ret = h2c->dfl == 0;
2455 }
2456
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002457 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002458 /* We may have to send an RST if not done yet */
2459 if (h2s->st == H2_SS_ERROR)
2460 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002461
Willy Tarreaua20a5192017-12-27 11:02:06 +01002462 if (h2c->st0 == H2_CS_FRAME_E)
2463 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002464
Willy Tarreau7e98c052017-10-10 15:56:59 +02002465 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002466 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002467 break;
2468
2469 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002470 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002471 h2c->st0 = H2_CS_FRAME_H;
2472 }
2473 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002474
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002475 if (h2c->rcvd_c > 0 &&
2476 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2477 h2c_send_conn_wu(h2c);
2478
Willy Tarreau52eed752017-09-22 15:05:09 +02002479 fail:
2480 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002481 if (h2s && h2s->cs &&
2482 (b_data(&h2s->rxbuf) ||
2483 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002484 /* we may have to signal the upper layers */
2485 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002486 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002487 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002488
Willy Tarreau47b515a2018-12-21 16:09:41 +01002489 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002490}
2491
2492/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2493 * the end.
2494 */
2495static int h2_process_mux(struct h2c *h2c)
2496{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002497 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002498
Willy Tarreau01b44822018-10-03 14:26:37 +02002499 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2500 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2501 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2502 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2503 if (h2c->st0 == H2_CS_ERROR) {
2504 h2c->st0 = H2_CS_ERROR2;
2505 sess_log(h2c->conn->owner);
2506 }
2507 goto fail;
2508 }
2509 h2c->st0 = H2_CS_SETTINGS1;
2510 }
2511 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002512 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002513 return 1;
2514 }
2515
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002516 /* start by sending possibly pending window updates */
2517 if (h2c->rcvd_c > 0 &&
2518 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2519 h2c_send_conn_wu(h2c) < 0)
2520 goto fail;
2521
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002522 /* First we always process the flow control list because the streams
2523 * waiting there were already elected for immediate emission but were
2524 * blocked just on this.
2525 */
2526
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002527 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002528 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2529 h2c->st0 >= H2_CS_ERROR)
2530 break;
2531
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002532 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002533 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2534 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002535 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002536 LIST_DEL(&h2s->list);
2537 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002538 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002539 }
2540
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002541 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002542 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2543 break;
2544
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002546 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2547 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002548 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002549 LIST_DEL(&h2s->list);
2550 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002551 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002552 }
2553
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002554 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002555 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002556 if (h2c->st0 == H2_CS_ERROR) {
2557 if (h2c->max_id >= 0) {
2558 h2c_send_goaway_error(h2c, NULL);
2559 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2560 return 0;
2561 }
2562
2563 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2564 }
2565 return 1;
2566 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002567 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002568}
2569
Willy Tarreau62f52692017-10-08 23:01:42 +02002570
Willy Tarreau479998a2018-11-18 06:30:59 +01002571/* Attempt to read data, and subscribe if none available.
2572 * The function returns 1 if data has been received, otherwise zero.
2573 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002574static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002575{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002576 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002577 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002578 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002579 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002580
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002581 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002582 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002583
Willy Tarreau315d8072017-12-10 22:17:57 +01002584 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002585 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002586
Willy Tarreau44e973f2018-03-01 17:49:30 +01002587 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002588 if (!buf) {
2589 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002590 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002591 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002592
Olivier Houchard7505f942018-08-21 18:10:44 +02002593 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002594 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002595 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2596 /* HTX in use : try to pre-align the buffer like the
2597 * rxbufs will be to optimize memory copies. We'll make
2598 * sure that the frame header lands at the end of the
2599 * HTX block to alias it upon recv. We cannot use the
2600 * head because rcv_buf() will realign the buffer if
2601 * it's empty. Thus we cheat and pretend we already
2602 * have a few bytes there.
2603 */
2604 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002605 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002606 }
2607 else
2608 max = b_room(buf);
2609
Olivier Houchard7505f942018-08-21 18:10:44 +02002610 if (max)
2611 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2612 else
2613 ret = 0;
2614 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002615
Olivier Houchard53216e72018-10-10 15:46:36 +02002616 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002617 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002618
Olivier Houcharda1411e62018-08-17 18:42:48 +02002619 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002620 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002621 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002622 }
2623
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002624 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002625 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002626 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002627}
2628
Willy Tarreau479998a2018-11-18 06:30:59 +01002629/* Try to send data if possible.
2630 * The function returns 1 if data have been sent, otherwise zero.
2631 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002632static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002633{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002634 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002635 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002636 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002637
2638 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002639 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002640
Olivier Houchard7505f942018-08-21 18:10:44 +02002641
Willy Tarreaua2af5122017-10-09 11:56:46 +02002642 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2643 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002644 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002645 }
2646
Willy Tarreaubc933932017-10-09 16:21:43 +02002647 /* This loop is quite simple : it tries to fill as much as it can from
2648 * pending streams into the existing buffer until it's reportedly full
2649 * or the end of send requests is reached. Then it tries to send this
2650 * buffer's contents out, marks it not full if at least one byte could
2651 * be sent, and tries again.
2652 *
2653 * The snd_buf() function normally takes a "flags" argument which may
2654 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2655 * data immediately comes and CO_SFL_STREAMER to indicate that the
2656 * connection is streaming lots of data (used to increase TLS record
2657 * size at the expense of latency). The former can be sent any time
2658 * there's a buffer full flag, as it indicates at least one stream
2659 * attempted to send and failed so there are pending data. An
2660 * alternative would be to set it as long as there's an active stream
2661 * but that would be problematic for ACKs until we have an absolute
2662 * guarantee that all waiters have at least one byte to send. The
2663 * latter should possibly not be set for now.
2664 */
2665
2666 done = 0;
2667 while (!done) {
2668 unsigned int flags = 0;
2669
2670 /* fill as much as we can into the current buffer */
2671 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2672 done = h2_process_mux(h2c);
2673
Olivier Houchard2b094432019-01-29 18:28:36 +01002674 if (h2c->flags & H2_CF_MUX_MALLOC)
2675 break;
2676
Willy Tarreaubc933932017-10-09 16:21:43 +02002677 if (conn->flags & CO_FL_ERROR)
2678 break;
2679
2680 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2681 flags |= CO_SFL_MSG_MORE;
2682
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002683 if (b_data(&h2c->mbuf)) {
2684 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002685 if (!ret)
2686 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002687 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002688 b_del(&h2c->mbuf, ret);
2689 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002690 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002691
2692 /* wrote at least one byte, the buffer is not full anymore */
2693 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2694 }
2695
Willy Tarreaua2af5122017-10-09 11:56:46 +02002696 if (conn->flags & CO_FL_SOCK_WR_SH) {
2697 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002698 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002699 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002700 /* We're not full anymore, so we can wake any task that are waiting
2701 * for us.
2702 */
2703 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002704 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002705 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2706 struct h2s *, list);
2707 LIST_DEL(&h2s->list);
2708 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002709 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002710 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2711 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002712 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002713 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002714 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002715 /* We're done, no more to send */
2716 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002717 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002718schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002719 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2720 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002721 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002722}
2723
2724static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2725{
2726 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002727 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002728
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002729 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002730 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002731 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002732 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002733 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002734 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002735 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002736}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002737
Willy Tarreau62f52692017-10-08 23:01:42 +02002738/* callback called on any event by the connection handler.
2739 * It applies changes and returns zero, or < 0 if it wants immediate
2740 * destruction of the connection (which normally doesn not happen in h2).
2741 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002742static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002743{
Olivier Houchard7505f942018-08-21 18:10:44 +02002744 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002745
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002746 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002747 h2_process_demux(h2c);
2748
2749 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002750 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002751
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002752 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002753 h2c->flags &= ~H2_CF_DEM_DFULL;
2754 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002755 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002756
Willy Tarreau0b37d652018-10-03 10:33:02 +02002757 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002758 /* frontend is stopping, reload likely in progress, let's try
2759 * to announce a graceful shutdown if not yet done. We don't
2760 * care if it fails, it will be tried again later.
2761 */
2762 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2763 if (h2c->last_sid < 0)
2764 h2c->last_sid = (1U << 31) - 1;
2765 h2c_send_goaway_error(h2c, NULL);
2766 }
2767 }
2768
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002769 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002770 * If we received early data, and the handshake is done, wake
2771 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002772 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002773 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2774 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2775 struct eb32_node *node;
2776 struct h2s *h2s;
2777
2778 h2c->flags |= H2_CF_WAIT_FOR_HS;
2779 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2780
2781 while (node) {
2782 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002783 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002784 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002785 node = eb32_next(node);
2786 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002787 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002788
Willy Tarreau26bd7612017-10-09 16:47:04 +02002789 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002790 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2791 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2792 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002793 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002794
2795 if (eb_is_empty(&h2c->streams_by_id)) {
2796 /* no more stream, kill the connection now */
2797 h2_release(conn);
2798 return -1;
2799 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002800 }
2801
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002803 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002804
Olivier Houchard53216e72018-10-10 15:46:36 +02002805 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2806 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2807 (h2c->st0 != H2_CS_ERROR &&
2808 !b_data(&h2c->mbuf) &&
2809 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2810 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002811 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002812
Willy Tarreau3f133572017-10-31 19:21:06 +01002813 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002814 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002815 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002816 task_queue(h2c->task);
2817 }
2818 else
2819 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002820 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002821
Olivier Houchard7505f942018-08-21 18:10:44 +02002822 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002823 return 0;
2824}
2825
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002826static int h2_wake(struct connection *conn)
2827{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002828 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002829
2830 return (h2_process(h2c));
2831}
2832
Willy Tarreauea392822017-10-31 10:02:25 +01002833/* Connection timeout management. The principle is that if there's no receipt
2834 * nor sending for a certain amount of time, the connection is closed. If the
2835 * MUX buffer still has lying data or is not allocatable, the connection is
2836 * immediately killed. If it's allocatable and empty, we attempt to send a
2837 * GOAWAY frame.
2838 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002839static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002840{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002841 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002842 int expired = tick_is_expired(t->expire, now_ms);
2843
Willy Tarreau0975f112018-03-29 15:22:59 +02002844 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002845 return t;
2846
Willy Tarreau0975f112018-03-29 15:22:59 +02002847 task_delete(t);
2848 task_free(t);
2849
2850 if (!h2c) {
2851 /* resources were already deleted */
2852 return NULL;
2853 }
2854
2855 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002856 h2c_error(h2c, H2_ERR_NO_ERROR);
2857 h2_wake_some_streams(h2c, 0, 0);
2858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002860 /* don't even try to send a GOAWAY, the buffer is stuck */
2861 h2c->flags |= H2_CF_GOAWAY_FAILED;
2862 }
2863
2864 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002865 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002866 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2867 h2c->flags |= H2_CF_GOAWAY_FAILED;
2868
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2870 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002871 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 b_del(&h2c->mbuf, ret);
2873 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002874 }
2875 }
Willy Tarreauea392822017-10-31 10:02:25 +01002876
Willy Tarreau0975f112018-03-29 15:22:59 +02002877 /* either we can release everything now or it will be done later once
2878 * the last stream closes.
2879 */
2880 if (eb_is_empty(&h2c->streams_by_id))
2881 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002882
Willy Tarreauea392822017-10-31 10:02:25 +01002883 return NULL;
2884}
2885
2886
Willy Tarreau62f52692017-10-08 23:01:42 +02002887/*******************************************/
2888/* functions below are used by the streams */
2889/*******************************************/
2890
2891/*
2892 * Attach a new stream to a connection
2893 * (Used for outgoing connections)
2894 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002895static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002896{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002897 struct conn_stream *cs;
2898 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002899 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002900
2901 cs = cs_new(conn);
2902 if (!cs)
2903 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002904 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002905 if (!h2s) {
2906 cs_free(cs);
2907 return NULL;
2908 }
2909 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002910}
2911
Willy Tarreaufafd3982018-11-18 21:29:20 +01002912/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2913 * We have to scan because we may have some orphan streams. It might be
2914 * beneficial to scan backwards from the end to reduce the likeliness to find
2915 * orphans.
2916 */
2917static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2918{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002919 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002920 struct h2s *h2s;
2921 struct eb32_node *node;
2922
2923 node = eb32_first(&h2c->streams_by_id);
2924 while (node) {
2925 h2s = container_of(node, struct h2s, by_id);
2926 if (h2s->cs)
2927 return h2s->cs;
2928 node = eb32_next(node);
2929 }
2930 return NULL;
2931}
2932
Willy Tarreau62f52692017-10-08 23:01:42 +02002933/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002934 * Destroy the mux and the associated connection, if it is no longer used
2935 */
2936static void h2_destroy(struct connection *conn)
2937{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002938 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002939
2940 if (eb_is_empty(&h2c->streams_by_id))
2941 h2_release(h2c->conn);
2942}
2943
2944/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002945 * Detach the stream from the connection and possibly release the connection.
2946 */
2947static void h2_detach(struct conn_stream *cs)
2948{
Willy Tarreau60935142017-10-16 18:11:19 +02002949 struct h2s *h2s = cs->ctx;
2950 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002951 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002952
2953 cs->ctx = NULL;
2954 if (!h2s)
2955 return;
2956
Olivier Houchardf502aca2018-12-14 19:42:40 +01002957 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002958 h2c = h2s->h2c;
2959 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002960 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01002961 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
2962 !h2_frt_has_too_many_cs(h2c)) {
2963 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02002964 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002965 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002966 }
Willy Tarreau60935142017-10-16 18:11:19 +02002967
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002968 /* this stream may be blocked waiting for some data to leave (possibly
2969 * an ES or RST frame), so orphan it in this case.
2970 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002971 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002972 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002973 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002974 return;
2975
Willy Tarreau45f752e2017-10-30 15:44:59 +01002976 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2977 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2978 /* unblock the connection if it was blocked on this
2979 * stream.
2980 */
2981 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2982 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002983 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002984 }
2985
Willy Tarreau71049cc2018-03-28 13:56:39 +02002986 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002987
Olivier Houchard8a786902018-12-15 16:05:40 +01002988 if (h2c->flags & H2_CF_IS_BACK &&
2989 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002990 if (!(h2c->conn->flags &
2991 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2992 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002993 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002994 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2995 h2c->conn->owner = NULL;
2996 if (eb_is_empty(&h2c->streams_by_id)) {
2997 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2998 /* The server doesn't want it, let's kill the connection right away */
2999 h2c->conn->mux->destroy(h2c->conn);
3000 return;
3001 }
3002 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003003 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003004 if (eb_is_empty(&h2c->streams_by_id)) {
3005 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3006 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3007 return;
3008 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003009 /* Never ever allow to reuse a connection from a non-reuse backend */
3010 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3011 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003012 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003013 struct server *srv = objt_server(h2c->conn->target);
3014
3015 if (srv) {
3016 if (h2c->conn->flags & CO_FL_PRIVATE)
3017 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3018 else
3019 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3020 }
3021
3022 }
3023 }
3024 }
3025
Willy Tarreaue323f342018-03-28 13:51:45 +02003026 /* We don't want to close right now unless we're removing the
3027 * last stream, and either the connection is in error, or it
3028 * reached the ID already specified in a GOAWAY frame received
3029 * or sent (as seen by last_sid >= 0).
3030 */
3031 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3032 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003033 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard93c88522018-11-30 15:39:16 +01003034 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003035 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003036 (conn_xprt_read0_pending(h2c->conn) ||
3037 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3038 /* no more stream will come, kill it now */
3039 h2_release(h2c->conn);
3040 }
3041 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003042 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003043 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3044 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003045 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003046 else
3047 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003048 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003049}
3050
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003051static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003052{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003053 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003054 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003055
Willy Tarreau721c9742017-11-07 11:05:42 +01003056 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003057 return;
3058
Willy Tarreau18059042019-01-31 19:12:48 +01003059 /* a connstream may require us to immediately kill the whole connection
3060 * for example because of a "tcp-request content reject" rule that is
3061 * normally used to limit abuse. In this case we schedule a goaway to
3062 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003063 */
Willy Tarreau18059042019-01-31 19:12:48 +01003064 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3065 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3066 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3067 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3068 }
3069
Willy Tarreau90c32322017-11-24 08:00:30 +01003070 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003071 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003072 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003073
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003074 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003075 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003076 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003077
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003078 return;
3079add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003080 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003081 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003082 if (h2s->flags & H2_SF_BLK_MFCTL) {
3083 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3084 h2s->send_wait = sw;
3085 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3086 h2s->send_wait = sw;
3087 LIST_ADDQ(&h2c->send_list, &h2s->list);
3088 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003089 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003090 /* Let the handler know we want shutr */
3091 sw->handle = (void *)((long)sw->handle | 1);
Willy Tarreau62f52692017-10-08 23:01:42 +02003092}
3093
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003094static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003095{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003096 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003097 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003098
Willy Tarreau721c9742017-11-07 11:05:42 +01003099 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003100 return;
3101
Willy Tarreau67434202017-11-06 20:20:51 +01003102 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003103 /* we can cleanly close using an empty data frame only after headers */
3104
3105 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3106 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003107 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003108
3109 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003110 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003111 else
3112 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003113 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003114 /* a connstream may require us to immediately kill the whole connection
3115 * for example because of a "tcp-request content reject" rule that is
3116 * normally used to limit abuse. In this case we schedule a goaway to
3117 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003118 */
Willy Tarreau18059042019-01-31 19:12:48 +01003119 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3120 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3121 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3122 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3123 }
3124
Willy Tarreau90c32322017-11-24 08:00:30 +01003125 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003126 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003127 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003128
Willy Tarreau00dd0782018-03-01 16:31:34 +01003129 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003130 }
3131
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003132 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003133 tasklet_wakeup(h2c->wait_event.task);
3134 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003135
3136 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003137 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003138 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003139 if (h2s->flags & H2_SF_BLK_MFCTL) {
3140 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3141 h2s->send_wait = sw;
3142 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3143 h2s->send_wait = sw;
3144 LIST_ADDQ(&h2c->send_list, &h2s->list);
3145 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003146 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003147 /* let the handler know we want to shutw */
3148 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003149}
3150
3151static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3152{
3153 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003154 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003155
Olivier Houchard2c68a462018-12-15 22:42:20 +01003156 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003157 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003158 h2s->send_wait = NULL;
3159 LIST_DEL(&h2s->list);
3160 LIST_INIT(&h2s->list);
3161 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003162 if (reason & 2)
3163 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003164 if (reason & 1)
3165 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003166
Olivier Houchard2c68a462018-12-15 22:42:20 +01003167 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003168 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003169 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003170 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003171}
3172
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003173static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3174{
3175 struct h2s *h2s = cs->ctx;
3176
3177 if (!mode)
3178 return;
3179
3180 h2_do_shutr(h2s);
3181}
3182
3183static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3184{
3185 struct h2s *h2s = cs->ctx;
3186
3187 h2_do_shutw(h2s);
3188}
3189
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003190/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003191 * HTX request or response depending on the connection's side. Returns a
3192 * positive value on success, a negative value on failure, or 0 if it couldn't
3193 * proceed. May report connection errors in h2c->errcode if the frame is
3194 * non-decodable and the connection unrecoverable. In absence of connection
3195 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003196 *
3197 * The function may fold CONTINUATION frames into the initial HEADERS frame
3198 * by removing padding and next frame header, then moving the CONTINUATION
3199 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3200 * leaving a hole between the main frame and the beginning of the next one.
3201 * The possibly remaining incomplete or next frame at the end may be moved
3202 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3203 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3204 *
3205 * A buffer at the beginning of processing may look like this :
3206 *
3207 * ,---.---------.-----.--------------.--------------.------.---.
3208 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3209 * `---^---------^-----^--------------^--------------^------^---'
3210 * | | <-----> | |
3211 * area | dpl | wrap
3212 * |<--------------> |
3213 * | dfl |
3214 * |<-------------------------------------------------->|
3215 * head data
3216 *
3217 * Padding is automatically overwritten when folding, participating to the
3218 * hole size after dfl :
3219 *
3220 * ,---.------------------------.-----.--------------.------.---.
3221 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3222 * `---^------------------------^-----^--------------^------^---'
3223 * | | <-----> | |
3224 * area | hole | wrap
3225 * |<-----------------------> |
3226 * | dfl |
3227 * |<-------------------------------------------------->|
3228 * head data
3229 *
3230 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3231 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3232 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003233 *
3234 * The <flags> field must point to either the stream's flags or to a copy of it
3235 * so that the function can update the following flags :
3236 * - H2_SF_DATA_CLEN when content-length is seen
3237 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3238 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003239 *
3240 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3241 * decoding, in order to detect if we're dealing with a headers or a trailers
3242 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003243 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003244static 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 +02003245{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003246 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003247 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003248 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003249 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003250 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003251 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003252 int flen; // header frame len
3253 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003254 int ret = 0;
3255 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003256 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003257 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003258
Willy Tarreauea18f862018-12-22 20:19:26 +01003259next_frame:
3260 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3261 goto leave; // incomplete input frame
3262
3263 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3264 * this case, we'll try to paste it immediately after the initial
3265 * HEADERS frame payload and kill any possible padding. The initial
3266 * frame's length will be increased to represent the concatenation
3267 * of the two frames. The next frame is read from position <tlen>
3268 * and written at position <flen> (minus padding if some is present).
3269 */
3270 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3271 struct h2_fh hdr;
3272 int clen; // CONTINUATION frame's payload length
3273
3274 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3275 /* no more data, the buffer may be full, either due to
3276 * too large a frame or because of too large a hole that
3277 * we're going to compact at the end.
3278 */
3279 goto leave;
3280 }
3281
3282 if (hdr.ft != H2_FT_CONTINUATION) {
3283 /* RFC7540#6.10: frame of unexpected type */
3284 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3285 goto fail;
3286 }
3287
3288 if (hdr.sid != h2c->dsi) {
3289 /* RFC7540#6.10: frame of different stream */
3290 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3291 goto fail;
3292 }
3293
3294 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3295 /* RFC7540#4.2: invalid frame length */
3296 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3297 goto fail;
3298 }
3299
3300 /* detect when we must stop aggragating frames */
3301 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3302
3303 /* Take as much as we can of the CONTINUATION frame's payload */
3304 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3305 if (clen > hdr.len)
3306 clen = hdr.len;
3307
3308 /* Move the frame's payload over the padding, hole and frame
3309 * header. At least one of hole or dpl is null (see diagrams
3310 * above). The hole moves after the new aggragated frame.
3311 */
3312 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3313 h2c->dfl += clen - h2c->dpl;
3314 hole += h2c->dpl + 9;
3315 h2c->dpl = 0;
3316 goto next_frame;
3317 }
3318
3319 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003320
Willy Tarreau13278b42017-10-13 19:23:14 +02003321 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003322 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003323 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003324 copy = alloc_trash_chunk();
3325 if (!copy) {
3326 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3327 goto fail;
3328 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003329 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3330 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3331 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003332 }
3333
Willy Tarreau13278b42017-10-13 19:23:14 +02003334 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3335 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003336 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003337 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3338 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003339 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003340 }
3341
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003342 if (flen < 5) {
3343 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3344 goto fail;
3345 }
3346
Willy Tarreau13278b42017-10-13 19:23:14 +02003347 hdrs += 5; // stream dep = 4, weight = 1
3348 flen -= 5;
3349 }
3350
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003351 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003352 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003353 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003354 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003355
Willy Tarreau937f7602018-02-26 15:22:17 +01003356 /* we can't retry a failed decompression operation so we must be very
3357 * careful not to take any risks. In practice the output buffer is
3358 * always empty except maybe for trailers, in which case we simply have
3359 * to wait for the upper layer to finish consuming what is available.
3360 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003361
3362 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003363 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003364 if (!htx_is_empty(htx)) {
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 Tarreaubd4a6b62018-11-27 09:29:36 +01003368 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003369 if (b_data(rxbuf)) {
3370 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003371 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003372 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003373
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003374 rxbuf->head = 0;
3375 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003376 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003377
Willy Tarreau25919232019-01-03 14:48:18 +01003378 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003379 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3380 sizeof(list)/sizeof(list[0]), tmp);
3381 if (outlen < 0) {
3382 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3383 goto fail;
3384 }
3385
Willy Tarreau25919232019-01-03 14:48:18 +01003386 /* The PACK decompressor was updated, let's update the input buffer and
3387 * the parser's state to commit these changes and allow us to later
3388 * fail solely on the stream if needed.
3389 */
3390 b_del(&h2c->dbuf, h2c->dfl + hole);
3391 h2c->dfl = hole = 0;
3392 h2c->st0 = H2_CS_FRAME_H;
3393
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003394 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003395 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003396
Willy Tarreau88d138e2019-01-02 19:38:14 +01003397 if (*flags & H2_SF_HEADERS_RCVD)
3398 goto trailers;
3399
3400 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003401 if (htx) {
3402 /* HTX mode */
3403 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003404 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003405 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003406 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003407 } else {
3408 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003409 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003410 if (outlen > 0)
3411 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003412 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003413
3414 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003415 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003416 goto fail;
3417 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003418
Willy Tarreau174b06a2018-04-25 18:13:58 +02003419 if (msgf & H2_MSGF_BODY) {
3420 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003421 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003422 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003423 if (htx)
3424 htx->extra = *body_len;
3425 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003426 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003427 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003428 }
3429
Willy Tarreau88d138e2019-01-02 19:38:14 +01003430 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003431 /* indicate that a HEADERS frame was received for this stream, except
3432 * for 1xx responses. For 1xx responses, another HEADERS frame is
3433 * expected.
3434 */
3435 if (!(msgf & H2_MSGF_RSP_1XX))
3436 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003437
Christopher Faulet0b465482019-02-19 15:14:23 +01003438 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003439 /* Mark the end of message, either using EOM in HTX or with the
3440 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3441 * is not set during headers with END_STREAM.
3442 */
3443 if (htx) {
3444 if (!htx_add_endof(htx, HTX_BLK_EOM))
3445 goto fail;
3446 }
3447 else if (*flags & H2_SF_DATA_CHNK) {
3448 if (!b_putblk(rxbuf, "\r\n", 2))
3449 goto fail;
3450 }
3451 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003452
Willy Tarreau86277d42019-01-02 15:36:11 +01003453 /* success */
3454 ret = 1;
3455
Willy Tarreau68dd9852017-07-03 14:44:26 +02003456 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003457 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003458 * move the remaining data over it.
3459 */
3460 if (hole) {
3461 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3462 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3463 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3464 b_sub(&h2c->dbuf, hole);
3465 }
3466
3467 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3468 /* too large frames */
3469 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003470 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003471 }
3472
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003473 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003474 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003475 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003476 return ret;
3477
Willy Tarreau68dd9852017-07-03 14:44:26 +02003478 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003479 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003480 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003481
3482 trailers:
3483 /* This is the last HEADERS frame hence a trailer */
3484
3485 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3486 /* It's a trailer but it's missing ES flag */
3487 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3488 goto fail;
3489 }
3490
3491 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3492 * block, and when using chunks we must send the 0 CRLF marker. For
3493 * other modes, the trailers are silently dropped.
3494 */
3495 if (htx) {
3496 if (!htx_add_endof(htx, HTX_BLK_EOD))
3497 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003498 if (h2_make_htx_trailers(list, htx) <= 0)
3499 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003500 }
3501 else if (*flags & H2_SF_DATA_CHNK) {
3502 /* Legacy mode with chunked encoding : we must finalize the
3503 * data block message emit the trailing CRLF */
3504 if (!b_putblk(rxbuf, "0\r\n", 3))
3505 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003506
3507 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3508 if (outlen > 0)
3509 b_add(rxbuf, outlen);
3510 else
3511 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003512 }
3513
3514 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003515}
3516
Willy Tarreau454f9052017-10-26 19:40:35 +02003517/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3518 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3519 * in use, a new chunk is emitted for each frame. This is supposed to fit
3520 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3521 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3522 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003523 * parser state is automatically updated. Returns > 0 if it could completely
3524 * send the current frame, 0 if it couldn't complete, in which case
3525 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3526 * DATA frame can return 0 as a valid result). Stream errors are reported in
3527 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3528 * have checked the frame header and ensured that the frame was complete or the
3529 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003530 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003531static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003532{
3533 struct h2c *h2c = h2s->h2c;
3534 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003535 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003536 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003537 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003538 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003539
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003540 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003541
Olivier Houchard638b7992018-08-16 15:41:52 +02003542 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003543 if (!csbuf) {
3544 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003545 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003546 }
3547
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003548try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003549 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003550 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3551 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003552 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003553 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003554
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003555 if (flen > b_data(&h2c->dbuf)) {
3556 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003557 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003558 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003559 }
3560
Willy Tarreaua9b77962019-01-31 07:23:00 +01003561 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003562 block1 = htx_free_data_space(htx);
3563 if (!block1) {
3564 h2c->flags |= H2_CF_DEM_SFULL;
3565 goto fail;
3566 }
3567 if (flen > block1)
3568 flen = block1;
3569
3570 /* here, flen is the max we can copy into the output buffer */
3571 block1 = b_contig_data(&h2c->dbuf, 0);
3572 if (flen > block1)
3573 flen = block1;
3574
3575 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3576 h2c->flags |= H2_CF_DEM_SFULL;
3577 goto fail;
3578 }
3579
3580 b_del(&h2c->dbuf, flen);
3581 h2c->dfl -= flen;
3582 h2c->rcvd_c += flen;
3583 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003584
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003585 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003586 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003587 htx->extra = h2s->body_len;
3588 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003589 goto try_again;
3590 }
3591 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003592 /* it doesn't fit and the buffer is fragmented,
3593 * so let's defragment it and try again.
3594 */
3595 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003596 }
3597
Willy Tarreaueba10f22018-04-25 20:44:22 +02003598 /* chunked-encoding requires more room */
3599 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003600 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003601 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3602 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3603 (chklen < 1048576) ? 4 : 8;
3604 chklen += 4; // CRLF, CRLF
3605 }
3606
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003607 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003608 if (flen + chklen > b_room(csbuf)) {
3609 if (chklen >= b_room(csbuf)) {
3610 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003611 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003612 }
3613 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003614 }
3615
3616 if (h2s->flags & H2_SF_DATA_CHNK) {
3617 /* emit the chunk size */
3618 unsigned int chksz = flen;
3619 char str[10];
3620 char *beg;
3621
3622 beg = str + sizeof(str);
3623 *--beg = '\n';
3624 *--beg = '\r';
3625 do {
3626 *--beg = hextab[chksz & 0xF];
3627 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003628 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003629 }
3630
Willy Tarreau454f9052017-10-26 19:40:35 +02003631 /* Block1 is the length of the first block before the buffer wraps,
3632 * block2 is the optional second block to reach the end of the frame.
3633 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003634 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003635 if (block1 > flen)
3636 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003637 block2 = flen - block1;
3638
3639 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003640 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003641
3642 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003643 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003644
Willy Tarreaueba10f22018-04-25 20:44:22 +02003645 if (h2s->flags & H2_SF_DATA_CHNK) {
3646 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003647 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003648 }
3649
Willy Tarreau454f9052017-10-26 19:40:35 +02003650 /* now mark the input data as consumed (will be deleted from the buffer
3651 * by the caller when seeing FRAME_A after sending the window update).
3652 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003653 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003654 h2c->dfl -= flen;
3655 h2c->rcvd_c += flen;
3656 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3657
Willy Tarreau1915ca22019-01-24 11:49:37 +01003658 if (h2s->flags & H2_SF_DATA_CLEN)
3659 h2s->body_len -= flen;
3660
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003661 if (h2c->dfl > h2c->dpl) {
3662 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003663 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003664 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003665 }
3666
Willy Tarreau4a28da12018-01-04 14:41:00 +01003667 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003668 /* here we're done with the frame, all the payload (except padding) was
3669 * transferred.
3670 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003671
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003672 if (h2c->dff & H2_F_DATA_END_STREAM) {
3673 if (htx) {
3674 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3675 h2c->flags |= H2_CF_DEM_SFULL;
3676 goto fail;
3677 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003678 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003679 else if (h2s->flags & H2_SF_DATA_CHNK) {
3680 /* emit the trailing 0 CRLF CRLF */
3681 if (b_room(csbuf) < 5) {
3682 h2c->flags |= H2_CF_DEM_SFULL;
3683 goto fail;
3684 }
3685 chklen += 5;
3686 b_putblk(csbuf, "0\r\n\r\n", 5);
3687 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003688 }
3689
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003690 h2c->rcvd_c += h2c->dpl;
3691 h2c->rcvd_s += h2c->dpl;
3692 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003693 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3694
Willy Tarreau39d68502018-03-02 12:26:37 +01003695 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003696 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003697 h2s->cs->flags |= CS_FL_REOS;
3698 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003699 if (htx)
3700 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003701 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003702 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003703 if (htx)
3704 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003705 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003706}
3707
Willy Tarreau5dd17352018-06-14 13:33:30 +02003708/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3709 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3710 * number of bytes sent. The caller must check the stream's status to detect
3711 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003712 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003713static 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 +02003714{
3715 struct http_hdr list[MAX_HTTP_HDR];
3716 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003717 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003718 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003719 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003720 int es_now = 0;
3721 int ret = 0;
3722 int hdr;
3723
3724 if (h2c_mux_busy(h2c, h2s)) {
3725 h2s->flags |= H2_SF_BLK_MBUSY;
3726 return 0;
3727 }
3728
Willy Tarreau44e973f2018-03-01 17:49:30 +01003729 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003730 h2c->flags |= H2_CF_MUX_MALLOC;
3731 h2s->flags |= H2_SF_BLK_MROOM;
3732 return 0;
3733 }
3734
3735 /* First, try to parse the H1 response and index it into <list>.
3736 * NOTE! Since it comes from haproxy, we *know* that a response header
3737 * block does not wrap and we can safely read it this way without
3738 * having to realign the buffer.
3739 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003740 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003741 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003742 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003743 /* incomplete or invalid response, this is abnormal coming from
3744 * haproxy and may only result in a bad errorfile or bad Lua code
3745 * so that won't be fixed, raise an error now.
3746 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003747 * FIXME: we should instead add the ability to only return a
3748 * 502 bad gateway. But in theory this is not supposed to
3749 * happen.
3750 */
3751 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3752 ret = 0;
3753 goto end;
3754 }
3755
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003756 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003757
3758 /* certain statuses have no body or an empty one, regardless of
3759 * what the headers say.
3760 */
3761 if (sl.st.status >= 100 && sl.st.status < 200) {
3762 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3763 h1m->curr_len = h1m->body_len = 0;
3764 }
3765 else if (sl.st.status == 204 || sl.st.status == 304) {
3766 /* no contents, claim c-len is present and set to zero */
3767 h1m->flags &= ~H1_MF_CHNK;
3768 h1m->flags |= H1_MF_CLEN;
3769 h1m->curr_len = h1m->body_len = 0;
3770 }
3771
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003772 chunk_reset(&outbuf);
3773
3774 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003775 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003776 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003777 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003778
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003779 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003780 break;
3781 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003782 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003783 }
3784
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003785 if (outbuf.size < 9)
3786 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003787
3788 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003789 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3790 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3791 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003792
3793 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003794 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003795 /* this is an unparsable response */
3796 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3797 ret = 0;
3798 goto end;
3799 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003800
3801 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003802 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003803 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003804 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003805 }
3806
3807 /* encode all headers, stop at empty name */
3808 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003809 /* these ones do not exist in H2 and must be dropped. */
3810 if (isteq(list[hdr].n, ist("connection")) ||
3811 isteq(list[hdr].n, ist("proxy-connection")) ||
3812 isteq(list[hdr].n, ist("keep-alive")) ||
3813 isteq(list[hdr].n, ist("upgrade")) ||
3814 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003815 continue;
3816
3817 if (isteq(list[hdr].n, ist("")))
3818 break; // end
3819
3820 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3821 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003822 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003823 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003824 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003825 }
3826 }
3827
3828 /* we may need to add END_STREAM */
3829 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3830 es_now = 1;
3831
3832 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003833 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003834
3835 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003836 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003837
3838 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003839 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003840
3841 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003842 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003843 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003844
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003845 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003846 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003847 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003848
Willy Tarreau801250e2018-09-11 11:45:04 +02003849 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003850 h2s->flags |= H2_SF_ES_SENT;
3851 if (h2s->st == H2_SS_OPEN)
3852 h2s->st = H2_SS_HLOC;
3853 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003854 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003855 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003856 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003857 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003858 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003859 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003860 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003861 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003862 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003863
3864 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003865
3866 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003867 //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 +02003868 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003869 full:
3870 h1m_init_res(h1m);
3871 h1m->err_pos = -1; // don't care about errors on the response path
3872 h2c->flags |= H2_CF_MUX_MFULL;
3873 h2s->flags |= H2_SF_BLK_MROOM;
3874 ret = 0;
3875 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003876}
3877
Willy Tarreau5dd17352018-06-14 13:33:30 +02003878/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3879 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3880 * the number of bytes sent. The caller must check the stream's status to
3881 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003882 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003883static 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 +02003884{
3885 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003886 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003887 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003888 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003889 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003890 int es_now = 0;
3891 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003892 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003893 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003894
3895 if (h2c_mux_busy(h2c, h2s)) {
3896 h2s->flags |= H2_SF_BLK_MBUSY;
3897 goto end;
3898 }
3899
Willy Tarreau44e973f2018-03-01 17:49:30 +01003900 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003901 h2c->flags |= H2_CF_MUX_MALLOC;
3902 h2s->flags |= H2_SF_BLK_MROOM;
3903 goto end;
3904 }
3905
3906 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003907 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003908 goto end;
3909
3910 chunk_reset(&outbuf);
3911
3912 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003914 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003915 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003916
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003917 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003918 break;
3919 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003920 /* If there are pending data in the output buffer, and we have
3921 * less than 1/4 of the mbuf's size and everything fits, we'll
3922 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3923 * is full and wait, to save some slow realign calls.
3924 */
3925 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3926 h2c->flags |= H2_CF_MUX_MFULL;
3927 h2s->flags |= H2_SF_BLK_MROOM;
3928 goto end;
3929 }
3930
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003931 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003932 }
3933
3934 if (outbuf.size < 9) {
3935 h2c->flags |= H2_CF_MUX_MFULL;
3936 h2s->flags |= H2_SF_BLK_MROOM;
3937 goto end;
3938 }
3939
3940 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003941 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3942 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3943 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944
3945 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3946 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003947 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003948 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003949 break;
3950 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003951 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003952 if ((long long)size > h1m->curr_len)
3953 size = h1m->curr_len;
3954 break;
3955 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003956 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003957 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003958 if (!ret)
3959 goto end;
3960
3961 if (ret < 0) {
3962 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003963 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003964 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3965 goto end;
3966 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003967 max -= ret;
3968 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003969 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003970 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003971 }
3972
Willy Tarreau801250e2018-09-11 11:45:04 +02003973 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003974 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003975 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003976 if (!ret)
3977 goto end;
3978
3979 if (ret < 0) {
3980 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003981 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003982 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3983 goto end;
3984 }
3985
3986 size = chunk;
3987 h1m->curr_len = chunk;
3988 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003989 max -= ret;
3990 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003991 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003992 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003993 if (!size)
3994 goto send_empty;
3995 }
3996
3997 /* in MSG_DATA state, continue below */
3998 size = h1m->curr_len;
3999 break;
4000 }
4001
4002 /* we have in <size> the exact number of bytes we need to copy from
4003 * the H1 buffer. We need to check this against the connection's and
4004 * the stream's send windows, and to ensure that this fits in the max
4005 * frame size and in the buffer's available space minus 9 bytes (for
4006 * the frame header). The connection's flow control is applied last so
4007 * that we can use a separate list of streams which are immediately
4008 * unblocked on window opening. Note: we don't implement padding.
4009 */
4010
Willy Tarreau5dd17352018-06-14 13:33:30 +02004011 if (size > max)
4012 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004013
4014 if (size > h2s->mws)
4015 size = h2s->mws;
4016
4017 if (size <= 0) {
4018 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004019 if (h2s->send_wait) {
4020 LIST_DEL(&h2s->list);
4021 LIST_INIT(&h2s->list);
4022 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004023 goto end;
4024 }
4025
4026 if (h2c->mfs && size > h2c->mfs)
4027 size = h2c->mfs;
4028
4029 if (size + 9 > outbuf.size) {
4030 /* we have an opportunity for enlarging the too small
4031 * available space, let's try.
4032 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004033 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004034 goto realign_again;
4035 size = outbuf.size - 9;
4036 }
4037
4038 if (size <= 0) {
4039 h2c->flags |= H2_CF_MUX_MFULL;
4040 h2s->flags |= H2_SF_BLK_MROOM;
4041 goto end;
4042 }
4043
4044 if (size > h2c->mws)
4045 size = h2c->mws;
4046
4047 if (size <= 0) {
4048 h2s->flags |= H2_SF_BLK_MFCTL;
4049 goto end;
4050 }
4051
4052 /* copy whatever we can */
4053 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004054 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004055 if (ret == 1)
4056 len2 = 0;
4057
4058 if (!ret || len1 + len2 < size) {
4059 /* FIXME: must normally never happen */
4060 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4061 goto end;
4062 }
4063
4064 /* limit len1/len2 to size */
4065 if (len1 + len2 > size) {
4066 int sub = len1 + len2 - size;
4067
4068 if (len2 > sub)
4069 len2 -= sub;
4070 else {
4071 sub -= len2;
4072 len2 = 0;
4073 len1 -= sub;
4074 }
4075 }
4076
4077 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004078 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004079 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004080 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004081
4082 send_empty:
4083 /* we may need to add END_STREAM */
4084 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4085 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004086 *
4087 * FIXME: what we do here is not correct because we send end_stream
4088 * before knowing if we'll have to send a HEADERS frame for the
4089 * trailers. More importantly we're not consuming the trailing CRLF
4090 * after the end of trailers, so it will be left to the caller to
4091 * eat it. The right way to do it would be to measure trailers here
4092 * and to send ES only if there are no trailers.
4093 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004094 */
4095 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004096 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004097 es_now = 1;
4098
4099 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004100 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004101
4102 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004103 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004104
4105 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004106 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004107
4108 /* consume incoming H1 response */
4109 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004110 max -= size;
4111 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004112 total += size;
4113 h1m->curr_len -= size;
4114 h2s->mws -= size;
4115 h2c->mws -= size;
4116
4117 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004118 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004119 goto new_frame;
4120 }
4121 }
4122
4123 if (es_now) {
4124 if (h2s->st == H2_SS_OPEN)
4125 h2s->st = H2_SS_HLOC;
4126 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004127 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004128
Willy Tarreau35a62702018-02-27 15:37:25 +01004129 if (!(h1m->flags & H1_MF_CHNK)) {
4130 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004131 total += max;
4132 ofs += max;
4133 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004134
Willy Tarreau801250e2018-09-11 11:45:04 +02004135 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004136 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004137
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004138 h2s->flags |= H2_SF_ES_SENT;
4139 }
4140
4141 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004142 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 +02004143 return total;
4144}
4145
Willy Tarreau115e83b2018-12-01 19:17:53 +01004146/* Try to send a HEADERS frame matching HTX response present in HTX message
4147 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4148 * must check the stream's status to detect any error which might have happened
4149 * subsequently to a successful send. The htx blocks are automatically removed
4150 * from the message. The htx message is assumed to be valid since produced from
4151 * the internal code, hence it contains a start line, an optional series of
4152 * header blocks and an end of header, otherwise an invalid frame could be
4153 * emitted and the resulting htx message could be left in an inconsistent state.
4154 */
4155static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4156{
4157 struct http_hdr list[MAX_HTTP_HDR];
4158 struct h2c *h2c = h2s->h2c;
4159 struct htx_blk *blk;
4160 struct htx_blk *blk_end;
4161 struct buffer outbuf;
4162 struct htx_sl *sl;
4163 enum htx_blk_type type;
4164 int es_now = 0;
4165 int ret = 0;
4166 int hdr;
4167 int idx;
4168
4169 if (h2c_mux_busy(h2c, h2s)) {
4170 h2s->flags |= H2_SF_BLK_MBUSY;
4171 return 0;
4172 }
4173
4174 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4175 h2c->flags |= H2_CF_MUX_MALLOC;
4176 h2s->flags |= H2_SF_BLK_MROOM;
4177 return 0;
4178 }
4179
4180 /* determine the first block which must not be deleted, blk_end may
4181 * be NULL if all blocks have to be deleted.
4182 */
4183 idx = htx_get_head(htx);
4184 blk_end = NULL;
4185 while (idx != -1) {
4186 type = htx_get_blk_type(htx_get_blk(htx, idx));
4187 idx = htx_get_next(htx, idx);
4188 if (type == HTX_BLK_EOH) {
4189 if (idx != -1)
4190 blk_end = htx_get_blk(htx, idx);
4191 break;
4192 }
4193 }
4194
4195 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004196 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004197 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004198 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004199 if (h2s->status < 100 || h2s->status > 999)
4200 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004201
4202 /* and the rest of the headers, that we dump starting at header 0 */
4203 hdr = 0;
4204
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004205 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004206 while ((idx = htx_get_next(htx, idx)) != -1) {
4207 blk = htx_get_blk(htx, idx);
4208 type = htx_get_blk_type(blk);
4209
4210 if (type == HTX_BLK_UNUSED)
4211 continue;
4212
4213 if (type != HTX_BLK_HDR)
4214 break;
4215
4216 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4217 goto fail;
4218
4219 list[hdr].n = htx_get_blk_name(htx, blk);
4220 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004221 hdr++;
4222 }
4223
4224 /* marker for end of headers */
4225 list[hdr].n = ist("");
4226
4227 if (h2s->status == 204 || h2s->status == 304) {
4228 /* no contents, claim c-len is present and set to zero */
4229 es_now = 1;
4230 }
4231
4232 chunk_reset(&outbuf);
4233
4234 while (1) {
4235 outbuf.area = b_tail(&h2c->mbuf);
4236 outbuf.size = b_contig_space(&h2c->mbuf);
4237 outbuf.data = 0;
4238
4239 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4240 break;
4241 realign_again:
4242 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4243 }
4244
4245 if (outbuf.size < 9)
4246 goto full;
4247
4248 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4249 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4250 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4251 outbuf.data = 9;
4252
4253 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004254 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004255 if (b_space_wraps(&h2c->mbuf))
4256 goto realign_again;
4257 goto full;
4258 }
4259
4260 /* encode all headers, stop at empty name */
4261 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4262 /* these ones do not exist in H2 and must be dropped. */
4263 if (isteq(list[hdr].n, ist("connection")) ||
4264 isteq(list[hdr].n, ist("proxy-connection")) ||
4265 isteq(list[hdr].n, ist("keep-alive")) ||
4266 isteq(list[hdr].n, ist("upgrade")) ||
4267 isteq(list[hdr].n, ist("transfer-encoding")))
4268 continue;
4269
4270 if (isteq(list[hdr].n, ist("")))
4271 break; // end
4272
4273 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4274 /* output full */
4275 if (b_space_wraps(&h2c->mbuf))
4276 goto realign_again;
4277 goto full;
4278 }
4279 }
4280
Christopher Faulet0b465482019-02-19 15:14:23 +01004281 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004282 * FIXME: we should also set it when we know for sure that the
4283 * content-length is zero as well as on 204/304
4284 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004285 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4286 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004287 es_now = 1;
4288
4289 if (h2s->cs->flags & CS_FL_SHW)
4290 es_now = 1;
4291
4292 /* update the frame's size */
4293 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4294
4295 if (es_now)
4296 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4297
4298 /* commit the H2 response */
4299 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004300
4301 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4302 * 1xx responses, another HEADERS frame is expected.
4303 */
4304 if (h2s->status >= 200 || h2s->status == 101)
4305 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004306
Willy Tarreau115e83b2018-12-01 19:17:53 +01004307 if (es_now) {
4308 h2s->flags |= H2_SF_ES_SENT;
4309 if (h2s->st == H2_SS_OPEN)
4310 h2s->st = H2_SS_HLOC;
4311 else
4312 h2s_close(h2s);
4313 }
4314
4315 /* OK we could properly deliver the response */
4316
4317 /* remove all header blocks including the EOH and compute the
4318 * corresponding size.
4319 *
4320 * FIXME: We should remove everything when es_now is set.
4321 */
4322 ret = 0;
4323 idx = htx_get_head(htx);
4324 blk = htx_get_blk(htx, idx);
4325 while (blk != blk_end) {
4326 ret += htx_get_blksz(blk);
4327 blk = htx_remove_blk(htx, blk);
4328 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004329
4330 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4331 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004332 end:
4333 return ret;
4334 full:
4335 h2c->flags |= H2_CF_MUX_MFULL;
4336 h2s->flags |= H2_SF_BLK_MROOM;
4337 ret = 0;
4338 goto end;
4339 fail:
4340 /* unparsable HTX messages, too large ones to be produced in the local
4341 * list etc go here (unrecoverable errors).
4342 */
4343 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4344 ret = 0;
4345 goto end;
4346}
4347
Willy Tarreau80739692018-10-05 11:35:57 +02004348/* Try to send a HEADERS frame matching HTX request present in HTX message
4349 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4350 * must check the stream's status to detect any error which might have happened
4351 * subsequently to a successful send. The htx blocks are automatically removed
4352 * from the message. The htx message is assumed to be valid since produced from
4353 * the internal code, hence it contains a start line, an optional series of
4354 * header blocks and an end of header, otherwise an invalid frame could be
4355 * emitted and the resulting htx message could be left in an inconsistent state.
4356 */
4357static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4358{
4359 struct http_hdr list[MAX_HTTP_HDR];
4360 struct h2c *h2c = h2s->h2c;
4361 struct htx_blk *blk;
4362 struct htx_blk *blk_end;
4363 struct buffer outbuf;
4364 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004365 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004366 enum htx_blk_type type;
4367 int es_now = 0;
4368 int ret = 0;
4369 int hdr;
4370 int idx;
4371
4372 if (h2c_mux_busy(h2c, h2s)) {
4373 h2s->flags |= H2_SF_BLK_MBUSY;
4374 return 0;
4375 }
4376
4377 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4378 h2c->flags |= H2_CF_MUX_MALLOC;
4379 h2s->flags |= H2_SF_BLK_MROOM;
4380 return 0;
4381 }
4382
4383 /* determine the first block which must not be deleted, blk_end may
4384 * be NULL if all blocks have to be deleted.
4385 */
4386 idx = htx_get_head(htx);
4387 blk_end = NULL;
4388 while (idx != -1) {
4389 type = htx_get_blk_type(htx_get_blk(htx, idx));
4390 idx = htx_get_next(htx, idx);
4391 if (type == HTX_BLK_EOH) {
4392 if (idx != -1)
4393 blk_end = htx_get_blk(htx, idx);
4394 break;
4395 }
4396 }
4397
4398 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004399 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004400 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004401 meth = htx_sl_req_meth(sl);
4402 path = htx_sl_req_uri(sl);
4403
4404 /* and the rest of the headers, that we dump starting at header 0 */
4405 hdr = 0;
4406
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004407 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004408 while ((idx = htx_get_next(htx, idx)) != -1) {
4409 blk = htx_get_blk(htx, idx);
4410 type = htx_get_blk_type(blk);
4411
4412 if (type == HTX_BLK_UNUSED)
4413 continue;
4414
4415 if (type != HTX_BLK_HDR)
4416 break;
4417
4418 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4419 goto fail;
4420
4421 list[hdr].n = htx_get_blk_name(htx, blk);
4422 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004423 hdr++;
4424 }
4425
4426 /* marker for end of headers */
4427 list[hdr].n = ist("");
4428
4429 chunk_reset(&outbuf);
4430
4431 while (1) {
4432 outbuf.area = b_tail(&h2c->mbuf);
4433 outbuf.size = b_contig_space(&h2c->mbuf);
4434 outbuf.data = 0;
4435
4436 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4437 break;
4438 realign_again:
4439 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4440 }
4441
4442 if (outbuf.size < 9)
4443 goto full;
4444
4445 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4446 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4447 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4448 outbuf.data = 9;
4449
4450 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004451 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004452 if (b_space_wraps(&h2c->mbuf))
4453 goto realign_again;
4454 goto full;
4455 }
4456
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004457 /* RFC7540 #8.3: the CONNECT method must have :
4458 * - :authority set to the URI part (host:port)
4459 * - :method set to CONNECT
4460 * - :scheme and :path omitted
4461 */
4462 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4463 /* encode the scheme which is always "https" (or 0x86 for "http") */
4464 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4465 /* output full */
4466 if (b_space_wraps(&h2c->mbuf))
4467 goto realign_again;
4468 goto full;
4469 }
Willy Tarreau80739692018-10-05 11:35:57 +02004470
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004471 /* encode the path, which necessarily is the second one */
4472 if (!hpack_encode_path(&outbuf, path)) {
4473 /* output full */
4474 if (b_space_wraps(&h2c->mbuf))
4475 goto realign_again;
4476 goto full;
4477 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004478
4479 /* look for the Host header and place it in :authority */
4480 auth = ist2(NULL, 0);
4481 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4482 if (isteq(list[hdr].n, ist("")))
4483 break; // end
4484
4485 if (isteq(list[hdr].n, ist("host"))) {
4486 auth = list[hdr].v;
4487 break;
4488 }
4489 }
4490 }
4491 else {
4492 /* for CONNECT, :authority is taken from the path */
4493 auth = path;
4494 }
4495
4496 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4497 /* output full */
4498 if (b_space_wraps(&h2c->mbuf))
4499 goto realign_again;
4500 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004501 }
4502
4503 /* encode all headers, stop at empty name */
4504 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4505 /* these ones do not exist in H2 and must be dropped. */
4506 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004507 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004508 isteq(list[hdr].n, ist("proxy-connection")) ||
4509 isteq(list[hdr].n, ist("keep-alive")) ||
4510 isteq(list[hdr].n, ist("upgrade")) ||
4511 isteq(list[hdr].n, ist("transfer-encoding")))
4512 continue;
4513
4514 if (isteq(list[hdr].n, ist("")))
4515 break; // end
4516
4517 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4518 /* output full */
4519 if (b_space_wraps(&h2c->mbuf))
4520 goto realign_again;
4521 goto full;
4522 }
4523 }
4524
4525 /* we may need to add END_STREAM if we have no body :
4526 * - request already closed, or :
4527 * - no transfer-encoding, and :
4528 * - no content-length or content-length:0
4529 * Fixme: this doesn't take into account CONNECT requests.
4530 */
4531 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4532 es_now = 1;
4533
4534 if (sl->flags & HTX_SL_F_BODYLESS)
4535 es_now = 1;
4536
4537 if (h2s->cs->flags & CS_FL_SHW)
4538 es_now = 1;
4539
4540 /* update the frame's size */
4541 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4542
4543 if (es_now)
4544 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4545
4546 /* commit the H2 response */
4547 b_add(&h2c->mbuf, outbuf.data);
4548 h2s->flags |= H2_SF_HEADERS_SENT;
4549 h2s->st = H2_SS_OPEN;
4550
Willy Tarreau80739692018-10-05 11:35:57 +02004551 if (es_now) {
4552 // trim any possibly pending data (eg: inconsistent content-length)
4553 h2s->flags |= H2_SF_ES_SENT;
4554 h2s->st = H2_SS_HLOC;
4555 }
4556
4557 /* remove all header blocks including the EOH and compute the
4558 * corresponding size.
4559 *
4560 * FIXME: We should remove everything when es_now is set.
4561 */
4562 ret = 0;
4563 idx = htx_get_head(htx);
4564 blk = htx_get_blk(htx, idx);
4565 while (blk != blk_end) {
4566 ret += htx_get_blksz(blk);
4567 blk = htx_remove_blk(htx, blk);
4568 }
4569
4570 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4571 htx_remove_blk(htx, blk_end);
4572
4573 end:
4574 return ret;
4575 full:
4576 h2c->flags |= H2_CF_MUX_MFULL;
4577 h2s->flags |= H2_SF_BLK_MROOM;
4578 ret = 0;
4579 goto end;
4580 fail:
4581 /* unparsable HTX messages, too large ones to be produced in the local
4582 * list etc go here (unrecoverable errors).
4583 */
4584 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4585 ret = 0;
4586 goto end;
4587}
4588
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004589/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004590 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4591 * caller must check the stream's status to detect any error which might have
4592 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004593 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4594 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004595static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004596{
4597 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004598 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004599 struct buffer outbuf;
4600 size_t total = 0;
4601 int es_now = 0;
4602 int bsize; /* htx block size */
4603 int fsize; /* h2 frame size */
4604 struct htx_blk *blk;
4605 enum htx_blk_type type;
4606 int idx;
4607
4608 if (h2c_mux_busy(h2c, h2s)) {
4609 h2s->flags |= H2_SF_BLK_MBUSY;
4610 goto end;
4611 }
4612
4613 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4614 h2c->flags |= H2_CF_MUX_MALLOC;
4615 h2s->flags |= H2_SF_BLK_MROOM;
4616 goto end;
4617 }
4618
Willy Tarreau98de12a2018-12-12 07:03:00 +01004619 htx = htx_from_buf(buf);
4620
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004621 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4622 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4623 * the caller to handle.
4624 */
4625
4626 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004627 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004628 goto end;
4629
4630 idx = htx_get_head(htx);
4631 blk = htx_get_blk(htx, idx);
4632 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4633 bsize = htx_get_blksz(blk);
4634 fsize = bsize;
4635
4636 if (type == HTX_BLK_EOD) {
4637 /* if we have an EOD, we're dealing with chunked data. We may
4638 * have a set of trailers after us that the caller will want to
4639 * deal with. Let's simply remove the EOD and return.
4640 */
4641 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004642 total++; // EOD counts as one byte
4643 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004644 goto end;
4645 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004646 else if (type == HTX_BLK_EOM) {
4647 if (h2s->flags & H2_SF_ES_SENT) {
4648 /* ES already sent */
4649 htx_remove_blk(htx, blk);
4650 total++; // EOM counts as one byte
4651 count--;
4652 goto end;
4653 }
4654 }
4655 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004656 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004657
4658 /* Perform some optimizations to reduce the number of buffer copies.
4659 * First, if the mux's buffer is empty and the htx area contains
4660 * exactly one data block of the same size as the requested count, and
4661 * this count fits within the frame size, the stream's window size, and
4662 * the connection's window size, then it's possible to simply swap the
4663 * caller's buffer with the mux's output buffer and adjust offsets and
4664 * length to match the entire DATA HTX block in the middle. In this
4665 * case we perform a true zero-copy operation from end-to-end. This is
4666 * the situation that happens all the time with large files. Second, if
4667 * this is not possible, but the mux's output buffer is empty, we still
4668 * have an opportunity to avoid the copy to the intermediary buffer, by
4669 * making the intermediary buffer's area point to the output buffer's
4670 * area. In this case we want to skip the HTX header to make sure that
4671 * copies remain aligned and that this operation remains possible all
4672 * the time. This goes for headers, data blocks and any data extracted
4673 * from the HTX blocks.
4674 */
4675 if (unlikely(fsize == count &&
4676 htx->used == 1 && type == HTX_BLK_DATA &&
4677 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4678 void *old_area = h2c->mbuf.area;
4679
4680 if (b_data(&h2c->mbuf)) {
4681 /* too bad there are data left there. If we have less
4682 * than 1/4 of the mbuf's size and everything fits,
4683 * we'll perform a copy anyway. Otherwise we'll pretend
4684 * the mbuf is full and wait.
4685 */
4686 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4687 goto copy;
4688 h2c->flags |= H2_CF_MUX_MFULL;
4689 h2s->flags |= H2_SF_BLK_MROOM;
4690 goto end;
4691 }
4692
4693 /* map an H2 frame to the HTX block so that we can put the
4694 * frame header there.
4695 */
4696 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004697 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004698 h2c->mbuf.data = fsize + 9;
4699 outbuf.area = b_head(&h2c->mbuf);
4700
4701 /* prepend an H2 DATA frame header just before the DATA block */
4702 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4703 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4704 h2_set_frame_size(outbuf.area, fsize);
4705
4706 /* update windows */
4707 h2s->mws -= fsize;
4708 h2c->mws -= fsize;
4709
4710 /* and exchange with our old area */
4711 buf->area = old_area;
4712 buf->data = buf->head = 0;
4713 total += fsize;
4714 goto end;
4715 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004716
Willy Tarreau98de12a2018-12-12 07:03:00 +01004717 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004718 /* for DATA and EOM we'll have to emit a frame, even if empty */
4719
4720 while (1) {
4721 outbuf.area = b_tail(&h2c->mbuf);
4722 outbuf.size = b_contig_space(&h2c->mbuf);
4723 outbuf.data = 0;
4724
4725 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4726 break;
4727 realign_again:
4728 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4729 }
4730
4731 if (outbuf.size < 9) {
4732 h2c->flags |= H2_CF_MUX_MFULL;
4733 h2s->flags |= H2_SF_BLK_MROOM;
4734 goto end;
4735 }
4736
4737 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4738 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4739 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4740 outbuf.data = 9;
4741
4742 /* we have in <fsize> the exact number of bytes we need to copy from
4743 * the HTX buffer. We need to check this against the connection's and
4744 * the stream's send windows, and to ensure that this fits in the max
4745 * frame size and in the buffer's available space minus 9 bytes (for
4746 * the frame header). The connection's flow control is applied last so
4747 * that we can use a separate list of streams which are immediately
4748 * unblocked on window opening. Note: we don't implement padding.
4749 */
4750
4751 /* EOM is presented with bsize==1 but would lead to the emission of an
4752 * empty frame, thus we force it to zero here.
4753 */
4754 if (type == HTX_BLK_EOM)
4755 bsize = fsize = 0;
4756
4757 if (!fsize)
4758 goto send_empty;
4759
4760 if (h2s->mws <= 0) {
4761 h2s->flags |= H2_SF_BLK_SFCTL;
4762 if (h2s->send_wait) {
4763 LIST_DEL(&h2s->list);
4764 LIST_INIT(&h2s->list);
4765 }
4766 goto end;
4767 }
4768
Willy Tarreauee573762018-12-04 15:25:57 +01004769 if (fsize > count)
4770 fsize = count;
4771
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004772 if (fsize > h2s->mws)
4773 fsize = h2s->mws; // >0
4774
4775 if (h2c->mfs && fsize > h2c->mfs)
4776 fsize = h2c->mfs; // >0
4777
4778 if (fsize + 9 > outbuf.size) {
4779 /* we have an opportunity for enlarging the too small
4780 * available space, let's try.
4781 * FIXME: is this really interesting to do? Maybe we'll
4782 * spend lots of time realigning instead of using two
4783 * frames.
4784 */
4785 if (b_space_wraps(&h2c->mbuf))
4786 goto realign_again;
4787 fsize = outbuf.size - 9;
4788
4789 if (fsize <= 0) {
4790 /* no need to send an empty frame here */
4791 h2c->flags |= H2_CF_MUX_MFULL;
4792 h2s->flags |= H2_SF_BLK_MROOM;
4793 goto end;
4794 }
4795 }
4796
4797 if (h2c->mws <= 0) {
4798 h2s->flags |= H2_SF_BLK_MFCTL;
4799 goto end;
4800 }
4801
4802 if (fsize > h2c->mws)
4803 fsize = h2c->mws;
4804
4805 /* now let's copy this this into the output buffer */
4806 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004807 h2s->mws -= fsize;
4808 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004809 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004810
4811 send_empty:
4812 /* update the frame's size */
4813 h2_set_frame_size(outbuf.area, fsize);
4814
4815 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4816 * meeting EOM. We should optimize this later.
4817 */
4818 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004819 total++; // EOM counts as one byte
4820 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004821 es_now = 1;
4822 }
4823
4824 if (es_now)
4825 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4826
4827 /* commit the H2 response */
4828 b_add(&h2c->mbuf, fsize + 9);
4829
4830 /* consume incoming HTX block, including EOM */
4831 total += fsize;
4832 if (fsize == bsize) {
4833 htx_remove_blk(htx, blk);
4834 if (fsize)
4835 goto new_frame;
4836 } else {
4837 /* we've truncated this block */
4838 htx_cut_data_blk(htx, blk, fsize);
4839 }
4840
4841 if (es_now) {
4842 if (h2s->st == H2_SS_OPEN)
4843 h2s->st = H2_SS_HLOC;
4844 else
4845 h2s_close(h2s);
4846
4847 h2s->flags |= H2_SF_ES_SENT;
4848 }
4849
4850 end:
4851 return total;
4852}
4853
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004854/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4855 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4856 * processed. The caller must check the stream's status to detect any error
4857 * which might have happened subsequently to a successful send. The htx blocks
4858 * are automatically removed from the message. The htx message is assumed to be
4859 * valid since produced from the internal code. Processing stops when meeting
4860 * the EOM, which is also removed. All trailers are processed at once and sent
4861 * as a single frame. The ES flag is always set.
4862 */
4863static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4864{
4865 struct http_hdr list[MAX_HTTP_HDR];
4866 struct h2c *h2c = h2s->h2c;
4867 struct htx_blk *blk;
4868 struct htx_blk *blk_end;
4869 struct buffer outbuf;
4870 struct h1m h1m;
4871 enum htx_blk_type type;
4872 uint32_t size;
4873 int ret = 0;
4874 int hdr;
4875 int idx;
4876 void *start;
4877
4878 if (h2c_mux_busy(h2c, h2s)) {
4879 h2s->flags |= H2_SF_BLK_MBUSY;
4880 goto end;
4881 }
4882
4883 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4884 h2c->flags |= H2_CF_MUX_MALLOC;
4885 h2s->flags |= H2_SF_BLK_MROOM;
4886 goto end;
4887 }
4888
4889 /* The principle is that we parse each and every trailers block using
4890 * the H1 headers parser, and append it to the list. We don't proceed
4891 * until EOM is met. blk_end will point to the EOM block.
4892 */
4893 hdr = 0;
4894 memset(list, 0, sizeof(list));
4895 blk_end = NULL;
4896
4897 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4898 blk = htx_get_blk(htx, idx);
4899 type = htx_get_blk_type(blk);
4900
4901 if (type == HTX_BLK_UNUSED)
4902 continue;
4903
4904 if (type != HTX_BLK_TLR) {
4905 if (type == HTX_BLK_EOM)
4906 blk_end = blk;
4907 break;
4908 }
4909
4910 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4911 goto fail;
4912
4913 size = htx_get_blksz(blk);
4914 start = htx_get_blk_ptr(htx, blk);
4915
4916 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4917 h1m.err_pos = 0;
4918 ret = h1_headers_to_hdr_list(start, start + size,
4919 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4920 &h1m, NULL);
4921 if (ret < 0)
4922 goto fail;
4923
4924 /* ret == 0 if an incomplete trailers block was found (missing
4925 * empty line), or > 0 if it was found. We have to continue on
4926 * incomplete messages because the trailers block might be
4927 * incomplete.
4928 */
4929
4930 /* search the new end */
4931 while (hdr <= sizeof(list)/sizeof(list[0])) {
4932 if (!list[hdr].n.len)
4933 break;
4934 hdr++;
4935 }
4936 }
4937
4938 if (!blk_end)
4939 goto end; // end not found yet
4940
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004941 chunk_reset(&outbuf);
4942
4943 while (1) {
4944 outbuf.area = b_tail(&h2c->mbuf);
4945 outbuf.size = b_contig_space(&h2c->mbuf);
4946 outbuf.data = 0;
4947
4948 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4949 break;
4950 realign_again:
4951 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4952 }
4953
4954 if (outbuf.size < 9)
4955 goto full;
4956
4957 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4958 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4959 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4960 outbuf.data = 9;
4961
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004962 /* encode all headers */
4963 for (idx = 0; idx < hdr; idx++) {
4964 /* these ones do not exist in H2 or must not appear in
4965 * trailers and must be dropped.
4966 */
4967 if (isteq(list[idx].n, ist("host")) ||
4968 isteq(list[idx].n, ist("content-length")) ||
4969 isteq(list[idx].n, ist("connection")) ||
4970 isteq(list[idx].n, ist("proxy-connection")) ||
4971 isteq(list[idx].n, ist("keep-alive")) ||
4972 isteq(list[idx].n, ist("upgrade")) ||
4973 isteq(list[idx].n, ist("te")) ||
4974 isteq(list[idx].n, ist("transfer-encoding")))
4975 continue;
4976
4977 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4978 /* output full */
4979 if (b_space_wraps(&h2c->mbuf))
4980 goto realign_again;
4981 goto full;
4982 }
4983 }
4984
Willy Tarreau67b8cae2019-02-21 18:16:35 +01004985 if (!hdr) {
4986 /* here we have a problem, we've received an empty trailers
4987 * block followed by an EOM. Because of this we can't send a
4988 * HEADERS frame, so we have to cheat and instead send an empty
4989 * DATA frame conveying the ES flag.
4990 */
4991 outbuf.area[3] = H2_FT_DATA;
4992 outbuf.area[4] = H2_F_DATA_END_STREAM;
4993 }
4994
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004995 /* update the frame's size */
4996 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4997
4998 /* commit the H2 response */
4999 b_add(&h2c->mbuf, outbuf.data);
5000 h2s->flags |= H2_SF_ES_SENT;
5001
5002 if (h2s->st == H2_SS_OPEN)
5003 h2s->st = H2_SS_HLOC;
5004 else
5005 h2s_close(h2s);
5006
5007 /* OK we could properly deliver the response */
5008 done:
5009 /* remove all header blocks including EOM and compute the corresponding size. */
5010 ret = 0;
5011 idx = htx_get_head(htx);
5012 blk = htx_get_blk(htx, idx);
5013 while (blk != blk_end) {
5014 ret += htx_get_blksz(blk);
5015 blk = htx_remove_blk(htx, blk);
5016 }
5017 blk = htx_remove_blk(htx, blk);
5018 end:
5019 return ret;
5020 full:
5021 h2c->flags |= H2_CF_MUX_MFULL;
5022 h2s->flags |= H2_SF_BLK_MROOM;
5023 ret = 0;
5024 goto end;
5025 fail:
5026 /* unparsable HTX messages, too large ones to be produced in the local
5027 * list etc go here (unrecoverable errors).
5028 */
5029 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5030 ret = 0;
5031 goto end;
5032}
5033
Olivier Houchard6ff20392018-07-17 18:46:31 +02005034/* Called from the upper layer, to subscribe to events, such as being able to send */
5035static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5036{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005037 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005038 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005039 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005040
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005041 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005042 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005043 if (!(sw->events & SUB_RETRY_RECV)) {
5044 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005045 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005046 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005047 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005048 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005049 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005050 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005051 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005052 if (!(sw->events & SUB_RETRY_SEND)) {
5053 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005054 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005055 h2s->send_wait = sw;
5056 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5057 if (h2s->flags & H2_SF_BLK_MFCTL)
5058 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5059 else
5060 LIST_ADDQ(&h2c->send_list, &h2s->list);
5061 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005062 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005063 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005064 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005065 if (event_type != 0)
5066 return -1;
5067 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005068
5069
5070}
5071
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005072static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5073{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005074 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005075 struct h2s *h2s = cs->ctx;
5076
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005077 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005078 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005079 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005080 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005081 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005082 }
5083 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005084 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005085 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005086 if (h2s->send_wait == sw) {
5087 LIST_DEL(&h2s->list);
5088 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005089 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005090 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005091 }
5092 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005093 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5094 sw = param;
5095 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005096 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005097 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005098 LIST_DEL(&h2s->list);
5099 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005100 }
5101 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005102 return 0;
5103}
5104
5105
Olivier Houchard511efea2018-08-16 15:30:32 +02005106/* Called from the upper layer, to receive data */
5107static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5108{
Olivier Houchard638b7992018-08-16 15:41:52 +02005109 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005110 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005111 struct htx *h2s_htx = NULL;
5112 struct htx *buf_htx = NULL;
5113 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005114 size_t ret = 0;
5115
5116 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005117 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5118 /* in HTX mode we ignore the count argument */
5119 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005120 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005121 /* Here htx_to_buf() will set buffer data to 0 because
5122 * the HTX is empty.
5123 */
5124 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005125 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005126 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005127
5128 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005129 count = htx_free_data_space(buf_htx);
5130 if (flags & CO_RFL_KEEP_RSV) {
5131 if (count <= global.tune.maxrewrite)
5132 goto end;
5133 count -= global.tune.maxrewrite;
5134 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005135
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005136 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005137 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005138 htx_to_buf(buf_htx, buf);
5139 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005140 ret = htx_ret.ret;
5141 }
5142 else {
5143 ret = b_xfer(buf, &h2s->rxbuf, count);
5144 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005145
Christopher Faulet37070b22019-02-14 15:12:14 +01005146 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005147 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005148 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005149 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005150 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005151 if (cs->flags & CS_FL_REOS)
5152 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005153 if (cs->flags & CS_FL_ERR_PENDING)
5154 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005155 if (b_size(&h2s->rxbuf)) {
5156 b_free(&h2s->rxbuf);
5157 offer_buffers(NULL, tasks_run_queue);
5158 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005159 }
5160
Willy Tarreau082f5592018-11-25 08:03:32 +01005161 if (ret && h2c->dsi == h2s->id) {
5162 /* demux is blocking on this stream's buffer */
5163 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005164 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005165 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005166
Olivier Houchard511efea2018-08-16 15:30:32 +02005167 return ret;
5168}
5169
Olivier Houchardd846c262018-10-19 17:24:29 +02005170static void h2_stop_senders(struct h2c *h2c)
5171{
5172 struct h2s *h2s, *h2s_back;
5173
5174 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
5175 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
5176 if (h2c->msi == h2s_id(h2s))
5177 continue;
5178 LIST_DEL(&h2s->list);
5179 LIST_INIT(&h2s->list);
5180 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005181 h2s->send_wait->events |= SUB_RETRY_SEND;
5182 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005183 LIST_ADD(&h2c->send_list, &h2s->list);
5184 }
5185}
5186
Willy Tarreau62f52692017-10-08 23:01:42 +02005187/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005188static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005189{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005190 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005191 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005192 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005193 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005194 struct htx *htx;
5195 struct htx_blk *blk;
5196 enum htx_blk_type btype;
5197 uint32_t bsize;
5198 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005199
Olivier Houchardd846c262018-10-19 17:24:29 +02005200 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005201 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005202 h2s->send_wait = NULL;
5203 LIST_DEL(&h2s->list);
5204 LIST_INIT(&h2s->list);
5205 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005206 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5207 return 0;
5208
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005209 /* htx will be enough to decide if we're using HTX or legacy */
5210 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5211
Willy Tarreau0bad0432018-06-14 16:54:01 +02005212 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005213 h2s->flags |= H2_SF_OUTGOING_DATA;
5214
Willy Tarreau751f2d02018-10-05 09:35:00 +02005215 if (h2s->id == 0) {
5216 int32_t id = h2c_get_next_sid(h2s->h2c);
5217
5218 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005219 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005220 return 0;
5221 }
5222
5223 eb32_delete(&h2s->by_id);
5224 h2s->by_id.key = h2s->id = id;
5225 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005226 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005227 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5228 }
5229
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005230 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005231 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5232 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005233 idx = htx_get_head(htx);
5234 blk = htx_get_blk(htx, idx);
5235 btype = htx_get_blk_type(blk);
5236 bsize = htx_get_blksz(blk);
5237
5238 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005239 case HTX_BLK_REQ_SL:
5240 /* start-line before headers */
5241 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5242 if (ret > 0) {
5243 total += ret;
5244 count -= ret;
5245 if (ret < bsize)
5246 goto done;
5247 }
5248 break;
5249
Willy Tarreau115e83b2018-12-01 19:17:53 +01005250 case HTX_BLK_RES_SL:
5251 /* start-line before headers */
5252 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5253 if (ret > 0) {
5254 total += ret;
5255 count -= ret;
5256 if (ret < bsize)
5257 goto done;
5258 }
5259 break;
5260
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005261 case HTX_BLK_DATA:
5262 case HTX_BLK_EOD:
5263 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005264 /* all these cause the emission of a DATA frame (possibly empty).
5265 * This EOM necessarily is one before trailers, as the EOM following
5266 * trailers would have been consumed by the trailers parser.
5267 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005268 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005269 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005270 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005271 total += ret;
5272 count -= ret;
5273 if (ret < bsize)
5274 goto done;
5275 }
5276 break;
5277
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005278 case HTX_BLK_TLR:
5279 /* This is the first trailers block, all the subsequent ones AND
5280 * the EOM will be swallowed by the parser.
5281 */
5282 ret = h2s_htx_make_trailers(h2s, htx);
5283 if (ret > 0) {
5284 total += ret;
5285 count -= ret;
5286 if (ret < bsize)
5287 goto done;
5288 }
5289 break;
5290
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005291 default:
5292 htx_remove_blk(htx, blk);
5293 total += bsize;
5294 count -= bsize;
5295 break;
5296 }
5297 }
5298 goto done;
5299 }
5300
5301 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005302 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005303 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005304 if (h2s->h2c->flags & H2_CF_IS_BACK)
5305 ret = -1;
5306 else
5307 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005308 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005309 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005310 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005311 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005312 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005313 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005314 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005315
Willy Tarreau5dd17352018-06-14 13:33:30 +02005316 if (unlikely((int)ret <= 0)) {
5317 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005318 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5319 break;
5320 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005321 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005322 total += count;
5323 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005324 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005325 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005326 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005327 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005328 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005329 break;
5330 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005331
5332 total += ret;
5333 count -= ret;
5334
5335 if (h2s->st >= H2_SS_ERROR)
5336 break;
5337
5338 if (h2s->flags & H2_SF_BLK_ANY)
5339 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005340 }
5341
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005342 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005343 if (h2s->st >= H2_SS_ERROR) {
5344 /* trim any possibly pending data after we close (extra CR-LF,
5345 * unprocessed trailers, abnormal extra data, ...)
5346 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005347 total += count;
5348 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005349 }
5350
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005351 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005352 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005353 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005354 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005355 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005356 }
5357
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005358 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005359 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005360 } else {
5361 b_del(buf, total);
5362 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005363
5364 /* The mux is full, cancel the pending tasks */
5365 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5366 (h2s->flags & H2_SF_BLK_MBUSY))
5367 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005368
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005369 /* If we're running HTX, and we read the whole buffer, then pretend
5370 * we read exactly what the caller specified, as with HTX the caller
5371 * will always give the buffer size, instead of the amount of data
5372 * available.
5373 */
5374 if (htx && !b_data(buf))
5375 total = orig_count;
5376
Olivier Houchard7505f942018-08-21 18:10:44 +02005377 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005378 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005379 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005380
Olivier Houchard7505f942018-08-21 18:10:44 +02005381 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005382 /* If we're waiting for flow control, and we got a shutr on the
5383 * connection, we will never be unlocked, so add an error on
5384 * the conn_stream.
5385 */
5386 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5387 !b_data(&h2s->h2c->dbuf) &&
5388 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5389 if (cs->flags & CS_FL_EOS)
5390 cs->flags |= CS_FL_ERROR;
5391 else
5392 cs->flags |= CS_FL_ERR_PENDING;
5393 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005394 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005395}
5396
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005397/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005398static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005399{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005400 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005401 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005402 struct eb32_node *node;
5403 int fctl_cnt = 0;
5404 int send_cnt = 0;
5405 int tree_cnt = 0;
5406 int orph_cnt = 0;
5407
5408 if (!h2c)
5409 return;
5410
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005411 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005412 fctl_cnt++;
5413
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005414 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005415 send_cnt++;
5416
Willy Tarreau3af37712018-12-18 14:34:41 +01005417 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005418 node = eb32_first(&h2c->streams_by_id);
5419 while (node) {
5420 h2s = container_of(node, struct h2s, by_id);
5421 tree_cnt++;
5422 if (!h2s->cs)
5423 orph_cnt++;
5424 node = eb32_next(node);
5425 }
5426
Willy Tarreau987c0632018-12-18 10:32:05 +01005427 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5428 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5429 " .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 +02005430 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5431 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005432 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005433 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5434 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5435 h2c->msi,
5436 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5437 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5438
5439 if (h2s) {
5440 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5441 h2s, h2s->id, h2s->flags,
5442 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5443 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5444 h2s->cs);
5445 if (h2s->cs)
5446 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5447 h2s->cs->flags, h2s->cs->data);
5448 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005449}
Willy Tarreau62f52692017-10-08 23:01:42 +02005450
5451/*******************************************************/
5452/* functions below are dedicated to the config parsers */
5453/*******************************************************/
5454
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005455/* config parser for global "tune.h2.header-table-size" */
5456static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5457 struct proxy *defpx, const char *file, int line,
5458 char **err)
5459{
5460 if (too_many_args(1, args, err, NULL))
5461 return -1;
5462
5463 h2_settings_header_table_size = atoi(args[1]);
5464 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5465 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5466 return -1;
5467 }
5468 return 0;
5469}
Willy Tarreau62f52692017-10-08 23:01:42 +02005470
Willy Tarreaue6baec02017-07-27 11:45:11 +02005471/* config parser for global "tune.h2.initial-window-size" */
5472static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5473 struct proxy *defpx, const char *file, int line,
5474 char **err)
5475{
5476 if (too_many_args(1, args, err, NULL))
5477 return -1;
5478
5479 h2_settings_initial_window_size = atoi(args[1]);
5480 if (h2_settings_initial_window_size < 0) {
5481 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5482 return -1;
5483 }
5484 return 0;
5485}
5486
Willy Tarreau5242ef82017-07-27 11:47:28 +02005487/* config parser for global "tune.h2.max-concurrent-streams" */
5488static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5489 struct proxy *defpx, const char *file, int line,
5490 char **err)
5491{
5492 if (too_many_args(1, args, err, NULL))
5493 return -1;
5494
5495 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005496 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005497 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5498 return -1;
5499 }
5500 return 0;
5501}
5502
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005503/* config parser for global "tune.h2.max-frame-size" */
5504static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5505 struct proxy *defpx, const char *file, int line,
5506 char **err)
5507{
5508 if (too_many_args(1, args, err, NULL))
5509 return -1;
5510
5511 h2_settings_max_frame_size = atoi(args[1]);
5512 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5513 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5514 return -1;
5515 }
5516 return 0;
5517}
5518
Willy Tarreau62f52692017-10-08 23:01:42 +02005519
5520/****************************************/
5521/* MUX initialization and instanciation */
5522/***************************************/
5523
5524/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005525static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005526 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005527 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005528 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005529 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005530 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005531 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005532 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005533 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005534 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005535 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005536 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005537 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005538 .shutr = h2_shutr,
5539 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005540 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005541 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005542 .name = "H2",
5543};
5544
Christopher Faulet32f61c02018-04-10 14:33:41 +02005545/* PROTO selection : this mux registers PROTO token "h2" */
5546static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005547 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005548
Willy Tarreau0108d902018-11-25 19:14:37 +01005549INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5550
Willy Tarreauf8957272018-10-03 10:25:20 +02005551static struct mux_proto_list mux_proto_h2_htx =
5552 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5553
5554INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5555
Willy Tarreau62f52692017-10-08 23:01:42 +02005556/* config keyword parsers */
5557static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005558 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005559 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005560 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005561 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005562 { 0, NULL, NULL }
5563}};
5564
Willy Tarreau0108d902018-11-25 19:14:37 +01005565INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);