blob: d1fa25a5d76998c4f262f2abbdfc74d0b118e79a [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 */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static 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 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200578 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200579 if (h2c->wait_event.task)
580 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100581 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200582 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200583 return -1;
584}
585
Willy Tarreau751f2d02018-10-05 09:35:00 +0200586/* returns the next allocatable outgoing stream ID for the H2 connection, or
587 * -1 if no more is allocatable.
588 */
589static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
590{
591 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100592
593 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200594 id = -1;
595 return id;
596}
597
Willy Tarreau2373acc2017-10-12 17:35:14 +0200598/* returns the stream associated with id <id> or NULL if not found */
599static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
600{
601 struct eb32_node *node;
602
Willy Tarreau751f2d02018-10-05 09:35:00 +0200603 if (id == 0)
604 return (struct h2s *)h2_closed_stream;
605
Willy Tarreau2a856182017-05-16 15:20:39 +0200606 if (id > h2c->max_id)
607 return (struct h2s *)h2_idle_stream;
608
Willy Tarreau2373acc2017-10-12 17:35:14 +0200609 node = eb32_lookup(&h2c->streams_by_id, id);
610 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200611 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200612
613 return container_of(node, struct h2s, by_id);
614}
615
Christopher Faulet73c12072019-04-08 11:23:22 +0200616/* release function. This one should be called to free all resources allocated
617 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200618 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200619static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200620{
Christopher Faulet61840e72019-04-15 09:33:32 +0200621 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200622
Willy Tarreau32218eb2017-09-22 08:07:25 +0200623 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200624 /* The connection must be aattached to this mux to be released */
625 if (h2c->conn && h2c->conn->ctx == h2c)
626 conn = h2c->conn;
627
Willy Tarreau32218eb2017-09-22 08:07:25 +0200628 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200629
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100630 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100631 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100632 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200633
Willy Tarreau44e973f2018-03-01 17:49:30 +0100634 h2_release_buf(h2c, &h2c->dbuf);
635 h2_release_buf(h2c, &h2c->mbuf);
636
Willy Tarreauea392822017-10-31 10:02:25 +0100637 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200638 h2c->task->context = NULL;
639 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100640 h2c->task = NULL;
641 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200642 if (h2c->wait_event.task)
643 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200644 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100645 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200646 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100647
Willy Tarreaubafbe012017-11-24 17:34:44 +0100648 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200649 }
650
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200651 if (conn) {
652 conn->mux = NULL;
653 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200654
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200655 conn_stop_tracking(conn);
656 conn_full_close(conn);
657 if (conn->destroy_cb)
658 conn->destroy_cb(conn);
659 conn_free(conn);
660 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200661}
662
663
Willy Tarreau71681172017-10-23 14:39:06 +0200664/******************************************************/
665/* functions below are for the H2 protocol processing */
666/******************************************************/
667
668/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200670{
671 return h2s ? h2s->id : 0;
672}
673
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200674/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200676{
677 if (h2c->msi < 0)
678 return 0;
679
680 if (h2c->msi == h2s_id(h2s))
681 return 0;
682
683 return 1;
684}
685
Willy Tarreau741d6df2017-10-17 08:00:59 +0200686/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100687static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200688{
689 h2c->errcode = err;
690 h2c->st0 = H2_CS_ERROR;
691}
692
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693/* marks an error on the stream. It may also update an already closed stream
694 * (e.g. to report an error after an RST was received).
695 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100696static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200697{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100698 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200699 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100700 if (h2s->st < H2_SS_ERROR)
701 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100702 if (h2s->cs)
703 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200704 }
705}
706
Willy Tarreau7e094452018-12-19 18:08:52 +0100707/* attempt to notify the data layer of recv availability */
708static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
709{
710 struct wait_event *sw;
711
712 if (h2s->recv_wait) {
713 sw = h2s->recv_wait;
714 sw->events &= ~SUB_RETRY_RECV;
715 tasklet_wakeup(sw->task);
716 h2s->recv_wait = NULL;
717 }
718}
719
720/* attempt to notify the data layer of send availability */
721static void __maybe_unused h2s_notify_send(struct h2s *h2s)
722{
723 struct wait_event *sw;
724
Olivier Houchard998410a2019-04-15 19:23:37 +0200725 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100726 sw = h2s->send_wait;
727 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100728 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100729 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 }
731}
732
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100733/* alerts the data layer, trying to wake it up by all means, following
734 * this sequence :
735 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
736 * - if its subscribed to send, then it's woken up for send
737 * - if it was subscribed to neither, its ->wake() callback is called
738 * It is safe to call this function with a closed stream which doesn't have a
739 * conn_stream anymore.
740 */
741static void __maybe_unused h2s_alert(struct h2s *h2s)
742{
743 if (h2s->recv_wait || h2s->send_wait) {
744 h2s_notify_recv(h2s);
745 h2s_notify_send(h2s);
746 }
747 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
748 h2s->cs->data_cb->wake(h2s->cs);
749}
750
Willy Tarreaue4820742017-07-27 13:37:23 +0200751/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100752static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200753{
754 uint8_t *out = frame;
755
756 *out = len >> 16;
757 write_n16(out + 1, len);
758}
759
Willy Tarreau54c15062017-10-10 17:10:03 +0200760/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
761 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
762 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200763 * available in the buffer's input prior to calling this function. The buffer
764 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200765 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200767 const struct buffer *b, int o)
768{
Willy Tarreau591d4452018-06-15 17:21:00 +0200769 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 +0200770}
771
Willy Tarreau1f094672017-11-20 21:27:45 +0100772static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200773{
Willy Tarreau591d4452018-06-15 17:21:00 +0200774 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200775}
776
Willy Tarreau1f094672017-11-20 21:27:45 +0100777static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200778{
Willy Tarreau591d4452018-06-15 17:21:00 +0200779 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200780}
781
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200783{
Willy Tarreau591d4452018-06-15 17:21:00 +0200784 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200785}
786
787
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100788/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
789 * The algorithm is not obvious. It turns out that H2 headers are neither
790 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
791 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200792 *
793 * b0 b1 b2 b3 b4 b5..b8
794 * +----------+---------+--------+----+----+----------------------+
795 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
796 * +----------+---------+--------+----+----+----------------------+
797 *
798 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
799 * we get the sid properly aligned and ordered, and 16 bits of len properly
800 * ordered as well. The type and flags can be extracted using bit shifts from
801 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200802 * Returns zero if some bytes are missing, otherwise non-zero on success. The
803 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200806{
807 uint64_t w;
808
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100809 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200810 return 0;
811
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100812 w = h2_get_n64(b, o + 1);
813 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200814 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
815 h->ff = w >> 32;
816 h->ft = w >> 40;
817 h->len += w >> 48;
818 return 1;
819}
820
821/* skip the next 9 bytes corresponding to the frame header possibly parsed by
822 * h2_peek_frame_hdr() above.
823 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200826 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200827}
828
829/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100830static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200831{
832 int ret;
833
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100834 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200835 if (ret > 0)
836 h2_skip_frame_hdr(b);
837 return ret;
838}
839
Willy Tarreau00dd0782018-03-01 16:31:34 +0100840/* marks stream <h2s> as CLOSED and decrement the number of active streams for
841 * its connection if the stream was not yet closed. Please use this exclusively
842 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100843 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100844static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100846 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 if (!h2s->id)
849 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100850 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100851 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100852 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
853 h2s_notify_recv(h2s);
854 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100855 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100856 h2s->st = H2_SS_CLOSED;
857}
858
Willy Tarreau71049cc2018-03-28 13:56:39 +0200859/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
860static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100861{
862 h2s_close(h2s);
863 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200864 if (b_size(&h2s->rxbuf)) {
865 b_free(&h2s->rxbuf);
866 offer_buffers(NULL, tasks_run_queue);
867 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100869 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200870 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100871 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800872 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200873 * reference left would be in the h2c send_list/fctl_list, and if
874 * we're in it, we're getting out anyway
875 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100876 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200877 if (!LIST_ISEMPTY(&h2s->sending_list)) {
878 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
879 LIST_DEL_INIT(&h2s->sending_list);
880 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200881 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100882 pool_free(pool_head_h2s, h2s);
883}
884
Willy Tarreaua8e49542018-10-03 18:53:55 +0200885/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
886 * stream tree. In case of error, nothing is added and NULL is returned. The
887 * causes of errors can be any failed memory allocation. The caller is
888 * responsible for checking if the connection may support an extra stream
889 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200891static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200892{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893 struct h2s *h2s;
894
Willy Tarreaubafbe012017-11-24 17:34:44 +0100895 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896 if (!h2s)
897 goto out;
898
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200899 h2s->wait_event.task = tasklet_new();
900 if (!h2s->wait_event.task) {
901 pool_free(pool_head_h2s, h2s);
902 goto out;
903 }
904 h2s->send_wait = NULL;
905 h2s->recv_wait = NULL;
906 h2s->wait_event.task->process = h2_deferred_shut;
907 h2s->wait_event.task->context = h2s;
908 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100909 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200910 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100911 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200912 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200913 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200914 h2s->mws = h2c->miw;
915 h2s->flags = H2_SF_NONE;
916 h2s->errcode = H2_ERR_NO_ERROR;
917 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200918 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100919 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200920 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200921
922 if (h2c->flags & H2_CF_IS_BACK) {
923 h1m_init_req(&h2s->h1m);
924 h2s->h1m.err_pos = -1; // don't care about errors on the request path
925 h2s->h1m.flags |= H1_MF_TOLOWER;
926 } else {
927 h1m_init_res(&h2s->h1m);
928 h2s->h1m.err_pos = -1; // don't care about errors on the response path
929 h2s->h1m.flags |= H1_MF_TOLOWER;
930 }
931
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200933 if (id > 0)
934 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100935 else
936 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200937
938 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100939 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100940 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200941
942 return h2s;
943
944 out_free_h2s:
945 pool_free(pool_head_h2s, h2s);
946 out:
947 return NULL;
948}
949
950/* creates a new stream <id> on the h2c connection and returns it, or NULL in
951 * case of memory allocation error.
952 */
953static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
954{
955 struct session *sess = h2c->conn->owner;
956 struct conn_stream *cs;
957 struct h2s *h2s;
958
959 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
960 goto out;
961
962 h2s = h2s_new(h2c, id);
963 if (!h2s)
964 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965
966 cs = cs_new(h2c->conn);
967 if (!cs)
968 goto out_close;
969
Olivier Houchard746fb772018-12-15 19:42:00 +0100970 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200971 h2s->cs = cs;
972 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200973 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974
975 if (stream_create_from_cs(cs) < 0)
976 goto out_free_cs;
977
Willy Tarreau590a0512018-09-05 11:56:48 +0200978 /* We want the accept date presented to the next stream to be the one
979 * we have now, the handshake time to be null (since the next stream
980 * is not delayed by a handshake), and the idle time to count since
981 * right now.
982 */
983 sess->accept_date = date;
984 sess->tv_accept = now;
985 sess->t_handshake = 0;
986
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100988 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200989 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990 return h2s;
991
992 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200993 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200994 cs_free(cs);
995 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200996 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200998 sess_log(sess);
999 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000}
1001
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1003 * and returns it, or NULL in case of memory allocation error or if the highest
1004 * possible stream ID was reached.
1005 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001006static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001007{
1008 struct h2s *h2s = NULL;
1009
Willy Tarreau86949782019-01-31 10:42:05 +01001010 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001011 goto out;
1012
Willy Tarreaua80dca82019-01-24 17:08:28 +01001013 if (h2_streams_left(h2c) < 1)
1014 goto out;
1015
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016 /* Defer choosing the ID until we send the first message to create the stream */
1017 h2s = h2s_new(h2c, 0);
1018 if (!h2s)
1019 goto out;
1020
1021 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001022 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001023 cs->ctx = h2s;
1024 h2c->nb_cs++;
1025
Willy Tarreau751f2d02018-10-05 09:35:00 +02001026 out:
1027 return h2s;
1028}
1029
Willy Tarreaube5b7152017-09-25 16:25:39 +02001030/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1031 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1032 * the various settings codes.
1033 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001034static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001035{
1036 struct buffer *res;
1037 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001038 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001039 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001040 int ret;
1041
1042 if (h2c_mux_busy(h2c, NULL)) {
1043 h2c->flags |= H2_CF_DEM_MBUSY;
1044 return 0;
1045 }
1046
Willy Tarreau44e973f2018-03-01 17:49:30 +01001047 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001048 if (!res) {
1049 h2c->flags |= H2_CF_MUX_MALLOC;
1050 h2c->flags |= H2_CF_DEM_MROOM;
1051 return 0;
1052 }
1053
1054 chunk_init(&buf, buf_data, sizeof(buf_data));
1055 chunk_memcpy(&buf,
1056 "\x00\x00\x00" /* length : 0 for now */
1057 "\x04\x00" /* type : 4 (settings), flags : 0 */
1058 "\x00\x00\x00\x00", /* stream ID : 0 */
1059 9);
1060
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001061 if (h2c->flags & H2_CF_IS_BACK) {
1062 /* send settings_enable_push=0 */
1063 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1064 }
1065
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066 if (h2_settings_header_table_size != 4096) {
1067 char str[6] = "\x00\x01"; /* header_table_size */
1068
1069 write_n32(str + 2, h2_settings_header_table_size);
1070 chunk_memcat(&buf, str, 6);
1071 }
1072
1073 if (h2_settings_initial_window_size != 65535) {
1074 char str[6] = "\x00\x04"; /* initial_window_size */
1075
1076 write_n32(str + 2, h2_settings_initial_window_size);
1077 chunk_memcat(&buf, str, 6);
1078 }
1079
1080 if (h2_settings_max_concurrent_streams != 0) {
1081 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1082
1083 /* Note: 0 means "unlimited" for haproxy's config but not for
1084 * the protocol, so never send this value!
1085 */
1086 write_n32(str + 2, h2_settings_max_concurrent_streams);
1087 chunk_memcat(&buf, str, 6);
1088 }
1089
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001090 mfs = h2_settings_max_frame_size;
1091 if (mfs > global.tune.bufsize)
1092 mfs = global.tune.bufsize;
1093
1094 if (!mfs)
1095 mfs = global.tune.bufsize;
1096
1097 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098 char str[6] = "\x00\x05"; /* max_frame_size */
1099
1100 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1101 * match bufsize - rewrite size, but at the moment it seems
1102 * that clients don't take care of it.
1103 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001104 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001105 chunk_memcat(&buf, str, 6);
1106 }
1107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001108 h2_set_frame_size(buf.area, buf.data - 9);
1109 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001110 if (unlikely(ret <= 0)) {
1111 if (!ret) {
1112 h2c->flags |= H2_CF_MUX_MFULL;
1113 h2c->flags |= H2_CF_DEM_MROOM;
1114 return 0;
1115 }
1116 else {
1117 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1118 return 0;
1119 }
1120 }
1121 return ret;
1122}
1123
Willy Tarreau52eed752017-09-22 15:05:09 +02001124/* Try to receive a connection preface, then upon success try to send our
1125 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1126 * missing data. It may return an error in h2c.
1127 */
1128static int h2c_frt_recv_preface(struct h2c *h2c)
1129{
1130 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001131 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001132
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001133 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001134
1135 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001136 if (ret1 < 0)
1137 sess_log(h2c->conn->owner);
1138
Willy Tarreau52eed752017-09-22 15:05:09 +02001139 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1140 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1141 return 0;
1142 }
1143
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001144 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001145 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001146 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001147
Willy Tarreaube5b7152017-09-25 16:25:39 +02001148 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001149}
1150
Willy Tarreau01b44822018-10-03 14:26:37 +02001151/* Try to send a connection preface, then upon success try to send our
1152 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1153 * missing data. It may return an error in h2c.
1154 */
1155static int h2c_bck_send_preface(struct h2c *h2c)
1156{
1157 struct buffer *res;
1158
1159 if (h2c_mux_busy(h2c, NULL)) {
1160 h2c->flags |= H2_CF_DEM_MBUSY;
1161 return 0;
1162 }
1163
1164 res = h2_get_buf(h2c, &h2c->mbuf);
1165 if (!res) {
1166 h2c->flags |= H2_CF_MUX_MALLOC;
1167 h2c->flags |= H2_CF_DEM_MROOM;
1168 return 0;
1169 }
1170
1171 if (!b_data(res)) {
1172 /* preface not yet sent */
1173 b_istput(res, ist(H2_CONN_PREFACE));
1174 }
1175
1176 return h2c_send_settings(h2c);
1177}
1178
Willy Tarreau081d4722017-05-16 21:51:05 +02001179/* try to send a GOAWAY frame on the connection to report an error or a graceful
1180 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1181 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1182 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1183 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1184 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1185 * on unrecoverable failure. It will not attempt to send one again in this last
1186 * case so that it is safe to use h2c_error() to report such errors.
1187 */
1188static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1189{
1190 struct buffer *res;
1191 char str[17];
1192 int ret;
1193
1194 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1195 return 1; // claim that it worked
1196
1197 if (h2c_mux_busy(h2c, h2s)) {
1198 if (h2s)
1199 h2s->flags |= H2_SF_BLK_MBUSY;
1200 else
1201 h2c->flags |= H2_CF_DEM_MBUSY;
1202 return 0;
1203 }
1204
Willy Tarreau44e973f2018-03-01 17:49:30 +01001205 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001206 if (!res) {
1207 h2c->flags |= H2_CF_MUX_MALLOC;
1208 if (h2s)
1209 h2s->flags |= H2_SF_BLK_MROOM;
1210 else
1211 h2c->flags |= H2_CF_DEM_MROOM;
1212 return 0;
1213 }
1214
1215 /* len: 8, type: 7, flags: none, sid: 0 */
1216 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1217
1218 if (h2c->last_sid < 0)
1219 h2c->last_sid = h2c->max_id;
1220
1221 write_n32(str + 9, h2c->last_sid);
1222 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001223 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001224 if (unlikely(ret <= 0)) {
1225 if (!ret) {
1226 h2c->flags |= H2_CF_MUX_MFULL;
1227 if (h2s)
1228 h2s->flags |= H2_SF_BLK_MROOM;
1229 else
1230 h2c->flags |= H2_CF_DEM_MROOM;
1231 return 0;
1232 }
1233 else {
1234 /* we cannot report this error using GOAWAY, so we mark
1235 * it and claim a success.
1236 */
1237 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1238 h2c->flags |= H2_CF_GOAWAY_FAILED;
1239 return 1;
1240 }
1241 }
1242 h2c->flags |= H2_CF_GOAWAY_SENT;
1243 return ret;
1244}
1245
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246/* Try to send an RST_STREAM frame on the connection for the indicated stream
1247 * during mux operations. This stream must be valid and cannot be closed
1248 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1249 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1250 * not yet.
1251 *
1252 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1253 * to write the message, it subscribes the stream to future notifications.
1254 */
1255static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1256{
1257 struct buffer *res;
1258 char str[13];
1259 int ret;
1260
1261 if (!h2s || h2s->st == H2_SS_CLOSED)
1262 return 1;
1263
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001264 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1265 * RST_STREAM in response to a RST_STREAM frame.
1266 */
1267 if (h2c->dft == H2_FT_RST_STREAM) {
1268 ret = 1;
1269 goto ignore;
1270 }
1271
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001272 if (h2c_mux_busy(h2c, h2s)) {
1273 h2s->flags |= H2_SF_BLK_MBUSY;
1274 return 0;
1275 }
1276
Willy Tarreau44e973f2018-03-01 17:49:30 +01001277 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001278 if (!res) {
1279 h2c->flags |= H2_CF_MUX_MALLOC;
1280 h2s->flags |= H2_SF_BLK_MROOM;
1281 return 0;
1282 }
1283
1284 /* len: 4, type: 3, flags: none */
1285 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1286 write_n32(str + 5, h2s->id);
1287 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001288 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289
1290 if (unlikely(ret <= 0)) {
1291 if (!ret) {
1292 h2c->flags |= H2_CF_MUX_MFULL;
1293 h2s->flags |= H2_SF_BLK_MROOM;
1294 return 0;
1295 }
1296 else {
1297 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1298 return 0;
1299 }
1300 }
1301
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001302 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001304 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001305 return ret;
1306}
1307
1308/* Try to send an RST_STREAM frame on the connection for the stream being
1309 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001310 * error code, even if the stream is one of the dummy ones, and will update
1311 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001312 *
1313 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1314 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001315 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001316 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001317 */
1318static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1319{
1320 struct buffer *res;
1321 char str[13];
1322 int ret;
1323
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001324 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1325 * RST_STREAM in response to a RST_STREAM frame.
1326 */
1327 if (h2c->dft == H2_FT_RST_STREAM) {
1328 ret = 1;
1329 goto ignore;
1330 }
1331
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001334 return 0;
1335 }
1336
Willy Tarreau44e973f2018-03-01 17:49:30 +01001337 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 if (!res) {
1339 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 return 0;
1342 }
1343
1344 /* len: 4, type: 3, flags: none */
1345 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001346
Willy Tarreau27a84c92017-10-17 08:10:17 +02001347 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001348 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001349 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001350
Willy Tarreau27a84c92017-10-17 08:10:17 +02001351 if (unlikely(ret <= 0)) {
1352 if (!ret) {
1353 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001354 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 return 0;
1356 }
1357 else {
1358 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1359 return 0;
1360 }
1361 }
1362
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001363 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001364 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001365 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001366 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001367 }
1368
Willy Tarreau27a84c92017-10-17 08:10:17 +02001369 return ret;
1370}
1371
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001372/* try to send an empty DATA frame with the ES flag set to notify about the
1373 * end of stream and match a shutdown(write). If an ES was already sent as
1374 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1375 * on success or zero if nothing was done. In case of lack of room to write the
1376 * message, it subscribes the requesting stream to future notifications.
1377 */
1378static int h2_send_empty_data_es(struct h2s *h2s)
1379{
1380 struct h2c *h2c = h2s->h2c;
1381 struct buffer *res;
1382 char str[9];
1383 int ret;
1384
Willy Tarreau721c9742017-11-07 11:05:42 +01001385 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001386 return 1;
1387
1388 if (h2c_mux_busy(h2c, h2s)) {
1389 h2s->flags |= H2_SF_BLK_MBUSY;
1390 return 0;
1391 }
1392
Willy Tarreau44e973f2018-03-01 17:49:30 +01001393 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001394 if (!res) {
1395 h2c->flags |= H2_CF_MUX_MALLOC;
1396 h2s->flags |= H2_SF_BLK_MROOM;
1397 return 0;
1398 }
1399
1400 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1401 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1402 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001403 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001404 if (likely(ret > 0)) {
1405 h2s->flags |= H2_SF_ES_SENT;
1406 }
1407 else if (!ret) {
1408 h2c->flags |= H2_CF_MUX_MFULL;
1409 h2s->flags |= H2_SF_BLK_MROOM;
1410 return 0;
1411 }
1412 else {
1413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1414 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001415 }
1416 return ret;
1417}
1418
Christopher Fauletf02ca002019-03-07 16:21:34 +01001419/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1420 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1421 * connection. The stream's state is automatically updated accordingly. If the
1422 * stream is orphaned, it is destroyed.
1423 */
1424static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1425{
1426 if (!h2s->cs) {
1427 /* this stream was already orphaned */
1428 h2s_destroy(h2s);
1429 return;
1430 }
1431
1432 h2s->cs->flags |= flags;
1433 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1434 h2s->cs->flags |= CS_FL_ERROR;
1435
1436 h2s_alert(h2s);
1437
1438 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1439 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001440 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001441 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001442 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001443 h2s_close(h2s);
1444}
1445
1446/* wake the streams attached to the connection, whose id is greater than <last>
1447 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001448 */
1449static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1450{
1451 struct eb32_node *node;
1452 struct h2s *h2s;
1453
1454 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001455 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001456
1457 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001458 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001459
Christopher Fauletf02ca002019-03-07 16:21:34 +01001460 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001461 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1462 while (node) {
1463 h2s = container_of(node, struct h2s, by_id);
1464 if (h2s->id <= last)
1465 break;
1466 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467 h2s_wake_one_stream(h2s, flags);
1468 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001469
Christopher Fauletf02ca002019-03-07 16:21:34 +01001470 /* Wake all streams with unassigned ID (ID == 0) */
1471 node = eb32_lookup(&h2c->streams_by_id, 0);
1472 while (node) {
1473 h2s = container_of(node, struct h2s, by_id);
1474 if (h2s->id > 0)
1475 break;
1476 node = eb32_next(node);
1477 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001478 }
1479}
1480
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481/* Increase all streams' outgoing window size by the difference passed in
1482 * argument. This is needed upon receipt of the settings frame if the initial
1483 * window size is different. The difference may be negative and the resulting
1484 * window size as well, for the time it takes to receive some window updates.
1485 */
1486static void h2c_update_all_ws(struct h2c *h2c, int diff)
1487{
1488 struct h2s *h2s;
1489 struct eb32_node *node;
1490
1491 if (!diff)
1492 return;
1493
1494 node = eb32_first(&h2c->streams_by_id);
1495 while (node) {
1496 h2s = container_of(node, struct h2s, by_id);
1497 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001498
1499 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1500 h2s->flags &= ~H2_SF_BLK_SFCTL;
1501 if (h2s->send_wait)
1502 LIST_ADDQ(&h2c->send_list, &h2s->list);
1503
1504 }
1505
Willy Tarreau3421aba2017-07-27 15:41:03 +02001506 node = eb32_next(node);
1507 }
1508}
1509
1510/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1511 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001512 * return an error in h2c. The caller must have already verified frame length
1513 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001514 */
1515static int h2c_handle_settings(struct h2c *h2c)
1516{
1517 unsigned int offset;
1518 int error;
1519
1520 if (h2c->dff & H2_F_SETTINGS_ACK) {
1521 if (h2c->dfl) {
1522 error = H2_ERR_FRAME_SIZE_ERROR;
1523 goto fail;
1524 }
1525 return 1;
1526 }
1527
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001530 return 0;
1531
1532 /* parse the frame */
1533 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001534 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1535 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001536
1537 switch (type) {
1538 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1539 /* we need to update all existing streams with the
1540 * difference from the previous iws.
1541 */
1542 if (arg < 0) { // RFC7540#6.5.2
1543 error = H2_ERR_FLOW_CONTROL_ERROR;
1544 goto fail;
1545 }
1546 h2c_update_all_ws(h2c, arg - h2c->miw);
1547 h2c->miw = arg;
1548 break;
1549 case H2_SETTINGS_MAX_FRAME_SIZE:
1550 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1551 error = H2_ERR_PROTOCOL_ERROR;
1552 goto fail;
1553 }
1554 h2c->mfs = arg;
1555 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001556 case H2_SETTINGS_ENABLE_PUSH:
1557 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto fail;
1560 }
1561 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001562 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1563 if (h2c->flags & H2_CF_IS_BACK) {
1564 /* the limit is only for the backend; for the frontend it is our limit */
1565 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1566 arg = h2_settings_max_concurrent_streams;
1567 h2c->streams_limit = arg;
1568 }
1569 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001570 }
1571 }
1572
1573 /* need to ACK this frame now */
1574 h2c->st0 = H2_CS_FRAME_A;
1575 return 1;
1576 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001577 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001578 h2c_error(h2c, error);
1579 return 0;
1580}
1581
1582/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1583 * success or one of the h2_status values.
1584 */
1585static int h2c_ack_settings(struct h2c *h2c)
1586{
1587 struct buffer *res;
1588 char str[9];
1589 int ret = -1;
1590
1591 if (h2c_mux_busy(h2c, NULL)) {
1592 h2c->flags |= H2_CF_DEM_MBUSY;
1593 return 0;
1594 }
1595
Willy Tarreau44e973f2018-03-01 17:49:30 +01001596 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001597 if (!res) {
1598 h2c->flags |= H2_CF_MUX_MALLOC;
1599 h2c->flags |= H2_CF_DEM_MROOM;
1600 return 0;
1601 }
1602
1603 memcpy(str,
1604 "\x00\x00\x00" /* length : 0 (no data) */
1605 "\x04" "\x01" /* type : 4, flags : ACK */
1606 "\x00\x00\x00\x00" /* stream ID */, 9);
1607
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001608 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001609 if (unlikely(ret <= 0)) {
1610 if (!ret) {
1611 h2c->flags |= H2_CF_MUX_MFULL;
1612 h2c->flags |= H2_CF_DEM_MROOM;
1613 return 0;
1614 }
1615 else {
1616 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1617 return 0;
1618 }
1619 }
1620 return ret;
1621}
1622
Willy Tarreaucf68c782017-10-10 17:11:41 +02001623/* processes a PING frame and schedules an ACK if needed. The caller must pass
1624 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001625 * missing data. The caller must have already verified frame length
1626 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001627 */
1628static int h2c_handle_ping(struct h2c *h2c)
1629{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001630 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001631 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001632 h2c->st0 = H2_CS_FRAME_A;
1633 return 1;
1634}
1635
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001636/* Try to send a window update for stream id <sid> and value <increment>.
1637 * Returns > 0 on success or zero on missing room or failure. It may return an
1638 * error in h2c.
1639 */
1640static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1641{
1642 struct buffer *res;
1643 char str[13];
1644 int ret = -1;
1645
1646 if (h2c_mux_busy(h2c, NULL)) {
1647 h2c->flags |= H2_CF_DEM_MBUSY;
1648 return 0;
1649 }
1650
Willy Tarreau44e973f2018-03-01 17:49:30 +01001651 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001652 if (!res) {
1653 h2c->flags |= H2_CF_MUX_MALLOC;
1654 h2c->flags |= H2_CF_DEM_MROOM;
1655 return 0;
1656 }
1657
1658 /* length: 4, type: 8, flags: none */
1659 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1660 write_n32(str + 5, sid);
1661 write_n32(str + 9, increment);
1662
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001663 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001664
1665 if (unlikely(ret <= 0)) {
1666 if (!ret) {
1667 h2c->flags |= H2_CF_MUX_MFULL;
1668 h2c->flags |= H2_CF_DEM_MROOM;
1669 return 0;
1670 }
1671 else {
1672 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1673 return 0;
1674 }
1675 }
1676 return ret;
1677}
1678
1679/* try to send pending window update for the connection. It's safe to call it
1680 * with no pending updates. Returns > 0 on success or zero on missing room or
1681 * failure. It may return an error in h2c.
1682 */
1683static int h2c_send_conn_wu(struct h2c *h2c)
1684{
1685 int ret = 1;
1686
1687 if (h2c->rcvd_c <= 0)
1688 return 1;
1689
Willy Tarreau97aaa672018-12-23 09:49:04 +01001690 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1691 /* increase the advertised connection window to 2G on
1692 * first update.
1693 */
1694 h2c->flags |= H2_CF_WINDOW_OPENED;
1695 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1696 }
1697
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001698 /* send WU for the connection */
1699 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1700 if (ret > 0)
1701 h2c->rcvd_c = 0;
1702
1703 return ret;
1704}
1705
1706/* try to send pending window update for the current dmux stream. It's safe to
1707 * call it with no pending updates. Returns > 0 on success or zero on missing
1708 * room or failure. It may return an error in h2c.
1709 */
1710static int h2c_send_strm_wu(struct h2c *h2c)
1711{
1712 int ret = 1;
1713
1714 if (h2c->rcvd_s <= 0)
1715 return 1;
1716
1717 /* send WU for the stream */
1718 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1719 if (ret > 0)
1720 h2c->rcvd_s = 0;
1721
1722 return ret;
1723}
1724
Willy Tarreaucf68c782017-10-10 17:11:41 +02001725/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1726 * success, 0 on missing data or one of the h2_status values.
1727 */
1728static int h2c_ack_ping(struct h2c *h2c)
1729{
1730 struct buffer *res;
1731 char str[17];
1732 int ret = -1;
1733
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001734 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001735 return 0;
1736
1737 if (h2c_mux_busy(h2c, NULL)) {
1738 h2c->flags |= H2_CF_DEM_MBUSY;
1739 return 0;
1740 }
1741
Willy Tarreau44e973f2018-03-01 17:49:30 +01001742 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001743 if (!res) {
1744 h2c->flags |= H2_CF_MUX_MALLOC;
1745 h2c->flags |= H2_CF_DEM_MROOM;
1746 return 0;
1747 }
1748
1749 memcpy(str,
1750 "\x00\x00\x08" /* length : 8 (same payload) */
1751 "\x06" "\x01" /* type : 6, flags : ACK */
1752 "\x00\x00\x00\x00" /* stream ID */, 9);
1753
1754 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001755 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001756
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001757 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001758 if (unlikely(ret <= 0)) {
1759 if (!ret) {
1760 h2c->flags |= H2_CF_MUX_MFULL;
1761 h2c->flags |= H2_CF_DEM_MROOM;
1762 return 0;
1763 }
1764 else {
1765 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1766 return 0;
1767 }
1768 }
1769 return ret;
1770}
1771
Willy Tarreau26f95952017-07-27 17:18:30 +02001772/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1773 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001774 * h2c or h2s. The caller must have already verified frame length and stream ID
1775 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001776 */
1777static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1778{
1779 int32_t inc;
1780 int error;
1781
Willy Tarreau26f95952017-07-27 17:18:30 +02001782 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001783 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001784 return 0;
1785
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001787
1788 if (h2c->dsi != 0) {
1789 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001790
1791 /* it's not an error to receive WU on a closed stream */
1792 if (h2s->st == H2_SS_CLOSED)
1793 return 1;
1794
1795 if (!inc) {
1796 error = H2_ERR_PROTOCOL_ERROR;
1797 goto strm_err;
1798 }
1799
1800 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1801 error = H2_ERR_FLOW_CONTROL_ERROR;
1802 goto strm_err;
1803 }
1804
1805 h2s->mws += inc;
1806 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1807 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001808 if (h2s->send_wait)
1809 LIST_ADDQ(&h2c->send_list, &h2s->list);
1810
Willy Tarreau26f95952017-07-27 17:18:30 +02001811 }
1812 }
1813 else {
1814 /* connection window update */
1815 if (!inc) {
1816 error = H2_ERR_PROTOCOL_ERROR;
1817 goto conn_err;
1818 }
1819
1820 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1821 error = H2_ERR_FLOW_CONTROL_ERROR;
1822 goto conn_err;
1823 }
1824
1825 h2c->mws += inc;
1826 }
1827
1828 return 1;
1829
1830 conn_err:
1831 h2c_error(h2c, error);
1832 return 0;
1833
1834 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001835 h2s_error(h2s, error);
1836 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001837 return 0;
1838}
1839
Willy Tarreaue96b0922017-10-30 00:28:29 +01001840/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001841 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1842 * have already verified frame length and stream ID validity. Described in
1843 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001844 */
1845static int h2c_handle_goaway(struct h2c *h2c)
1846{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847 int last;
1848
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001851 return 0;
1852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001853 last = h2_get_n32(&h2c->dbuf, 0);
1854 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001855 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001856 if (h2c->last_sid < 0)
1857 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001858 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859}
1860
Willy Tarreau92153fc2017-12-03 19:46:19 +01001861/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001862 * invalid. Returns > 0 on success or zero on missing data. It may return an
1863 * error in h2c. The caller must have already verified frame length and stream
1864 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001865 */
1866static int h2c_handle_priority(struct h2c *h2c)
1867{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001869 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001870 return 0;
1871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001872 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001873 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001874 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1875 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001876 }
1877 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001878}
1879
Willy Tarreaucd234e92017-08-18 10:59:39 +02001880/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001881 * Returns > 0 on success or zero on missing data. The caller must have already
1882 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883 */
1884static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1885{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001887 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001888 return 0;
1889
1890 /* late RST, already handled */
1891 if (h2s->st == H2_SS_CLOSED)
1892 return 1;
1893
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001894 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001895 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001896
1897 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001898 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001899 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001900 }
1901
1902 h2s->flags |= H2_SF_RST_RCVD;
1903 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001904}
1905
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001906/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1907 * It may return an error in h2c or h2s. The caller must consider that the
1908 * return value is the new h2s in case one was allocated (most common case).
1909 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001910 * errors here are reported as connection errors since it's impossible to
1911 * recover from such errors after the compression context has been altered.
1912 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001913static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001914{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001915 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001916 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001917 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 int error;
1919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001920 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001921 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001923 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001924 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001925
1926 /* now either the frame is complete or the buffer is complete */
1927 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001928 /* The stream exists/existed, this must be a trailers frame */
1929 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001930 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1931 /* unrecoverable error ? */
1932 if (h2c->st0 >= H2_CS_ERROR)
1933 goto out;
1934
1935 if (error == 0)
1936 goto out; // missing data
1937
1938 if (error < 0) {
1939 /* Failed to decode this frame (e.g. too large request)
1940 * but the HPACK decompressor is still synchronized.
1941 */
1942 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1943 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001944 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001945 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001946 goto done;
1947 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001948 /* the connection was already killed by an RST, let's consume
1949 * the data and send another RST.
1950 */
1951 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1952 h2s = (struct h2s*)h2_error_stream;
1953 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001954 }
1955 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1956 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1957 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001958 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001959 goto conn_err;
1960 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001961 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1962 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001963
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001964 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001965
Willy Tarreau25919232019-01-03 14:48:18 +01001966 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001967 if (h2c->st0 >= H2_CS_ERROR)
1968 goto out;
1969
Willy Tarreau25919232019-01-03 14:48:18 +01001970 if (error <= 0) {
1971 if (error == 0)
1972 goto out; // missing data
1973
1974 /* Failed to decode this stream (e.g. too large request)
1975 * but the HPACK decompressor is still synchronized.
1976 */
1977 h2s = (struct h2s*)h2_error_stream;
1978 goto send_rst;
1979 }
1980
Willy Tarreau22de8d32018-09-05 19:55:58 +02001981 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001982 * positively from h2c_frt_stream_new(), the stream will report the error,
1983 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001984 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001985 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001986 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001987 h2s = (struct h2s*)h2_refused_stream;
1988 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 }
1990
1991 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001992 h2s->rxbuf = rxbuf;
1993 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001994 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001995
Willy Tarreau88d138e2019-01-02 19:38:14 +01001996 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001997 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001998 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001999
2000 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002001 if (h2s->cs)
2002 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002003 if (h2s->st == H2_SS_OPEN)
2004 h2s->st = H2_SS_HREM;
2005 else
2006 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002007 }
2008
Willy Tarreau3a429f02019-01-03 11:41:50 +01002009 /* update the max stream ID if the request is being processed */
2010 if (h2s->id > h2c->max_id)
2011 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002012
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002013 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002014
2015 conn_err:
2016 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002017 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002018
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002019 out:
2020 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002021 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002022
2023 send_rst:
2024 /* make the demux send an RST for the current stream. We may only
2025 * do this if we're certain that the HEADERS frame was properly
2026 * decompressed so that the HPACK decoder is still kept up to date.
2027 */
2028 h2_release_buf(h2c, &rxbuf);
2029 h2c->st0 = H2_CS_FRAME_E;
2030 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002031}
2032
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002033/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2034 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2035 * errors here are reported as connection errors since it's impossible to
2036 * recover from such errors after the compression context has been altered.
2037 */
2038static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2039{
2040 int error;
2041
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002042 if (!b_size(&h2c->dbuf))
2043 return NULL; // empty buffer
2044
2045 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2046 return NULL; // incomplete frame
2047
Willy Tarreau1915ca22019-01-24 11:49:37 +01002048 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002049
Willy Tarreau25919232019-01-03 14:48:18 +01002050 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002051 if (h2c->st0 >= H2_CS_ERROR)
2052 return NULL;
2053
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002054 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2055 /* RFC7540#5.1 */
2056 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2057 h2c->st0 = H2_CS_FRAME_E;
2058 return NULL;
2059 }
2060
Willy Tarreau25919232019-01-03 14:48:18 +01002061 if (error <= 0) {
2062 if (error == 0)
2063 return NULL; // missing data
2064
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002066 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002067 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002068 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002069 }
2070
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002071 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2072 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002073 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002074 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002075 }
2076
Willy Tarreau927b88b2019-03-04 08:03:25 +01002077 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002078 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002079 else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002080 h2s->st = H2_SS_HREM;
Willy Tarreau201fe402019-05-07 18:10:10 +02002081 else if ((!h2s->cs || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002082 h2s_close(h2s);
2083
2084 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002085}
2086
Willy Tarreau454f9052017-10-26 19:40:35 +02002087/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2088 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2089 */
2090static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2091{
2092 int error;
2093
2094 /* note that empty DATA frames are perfectly valid and sometimes used
2095 * to signal an end of stream (with the ES flag).
2096 */
2097
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002098 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002099 return 0; // empty buffer
2100
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002101 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002102 return 0; // incomplete frame
2103
2104 /* now either the frame is complete or the buffer is complete */
2105
Willy Tarreau454f9052017-10-26 19:40:35 +02002106 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2107 /* RFC7540#6.1 */
2108 error = H2_ERR_STREAM_CLOSED;
2109 goto strm_err;
2110 }
2111
Willy Tarreau1915ca22019-01-24 11:49:37 +01002112 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2113 /* RFC7540#8.1.2 */
2114 error = H2_ERR_PROTOCOL_ERROR;
2115 goto strm_err;
2116 }
2117
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002118 if (!h2_frt_transfer_data(h2s))
2119 return 0;
2120
Willy Tarreau454f9052017-10-26 19:40:35 +02002121 /* call the upper layers to process the frame, then let the upper layer
2122 * notify the stream about any change.
2123 */
2124 if (!h2s->cs) {
2125 error = H2_ERR_STREAM_CLOSED;
2126 goto strm_err;
2127 }
2128
Willy Tarreau8f650c32017-11-21 19:36:21 +01002129 if (h2c->st0 >= H2_CS_ERROR)
2130 return 0;
2131
Willy Tarreau721c9742017-11-07 11:05:42 +01002132 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002133 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002134 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002135 }
2136
2137 /* check for completion : the callee will change this to FRAME_A or
2138 * FRAME_H once done.
2139 */
2140 if (h2c->st0 == H2_CS_FRAME_P)
2141 return 0;
2142
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002143 /* last frame */
2144 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002145 if (h2s->cs)
2146 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002147 if (h2s->st == H2_SS_OPEN)
2148 h2s->st = H2_SS_HREM;
2149 else
2150 h2s_close(h2s);
2151
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002152 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002153
2154 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2155 /* RFC7540#8.1.2 */
2156 error = H2_ERR_PROTOCOL_ERROR;
2157 goto strm_err;
2158 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002159 }
2160
Willy Tarreau454f9052017-10-26 19:40:35 +02002161 return 1;
2162
Willy Tarreau454f9052017-10-26 19:40:35 +02002163 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002164 h2s_error(h2s, error);
2165 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002166 return 0;
2167}
2168
Willy Tarreaubc933932017-10-09 16:21:43 +02002169/* process Rx frames to be demultiplexed */
2170static void h2_process_demux(struct h2c *h2c)
2171{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002172 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002173 struct h2_fh hdr;
2174 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002175
Willy Tarreau081d4722017-05-16 21:51:05 +02002176 if (h2c->st0 >= H2_CS_ERROR)
2177 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002178
2179 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2180 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002181 if (h2c->flags & H2_CF_IS_BACK)
2182 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002183 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2184 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002186 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002187 sess_log(h2c->conn->owner);
2188 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002189 goto fail;
2190 }
2191
2192 h2c->max_id = 0;
2193 h2c->st0 = H2_CS_SETTINGS1;
2194 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195
2196 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002197 /* ensure that what is pending is a valid SETTINGS frame
2198 * without an ACK.
2199 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002200 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002202 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002203 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002204 sess_log(h2c->conn->owner);
2205 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002206 goto fail;
2207 }
2208
2209 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2210 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2211 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2212 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002213 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002214 goto fail;
2215 }
2216
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002217 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2219 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2220 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002221 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002222 goto fail;
2223 }
2224
Willy Tarreau3bf69182018-12-21 15:34:50 +01002225 /* that's OK, switch to FRAME_P to process it. This is
2226 * a SETTINGS frame whose header has already been
2227 * deleted above.
2228 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002229 padlen = 0;
2230 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002231 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002232 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002233
2234 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002235 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236 int ret = 0;
2237
2238 if (h2c->st0 >= H2_CS_ERROR)
2239 break;
2240
2241 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002242 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002243 break;
2244
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002245 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002247 if (!h2c->nb_streams) {
2248 /* only log if no other stream can report the error */
2249 sess_log(h2c->conn->owner);
2250 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002251 break;
2252 }
2253
Willy Tarreau3bf69182018-12-21 15:34:50 +01002254 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2255 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2256 * we read the pad length and drop it from the remaining
2257 * payload (one byte + the 9 remaining ones = 10 total
2258 * removed), so we have a frame payload starting after the
2259 * pad len. Flow controlled frames (DATA) also count the
2260 * padlen in the flow control, so it must be adjusted.
2261 */
2262 if (hdr.len < 1) {
2263 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2264 sess_log(h2c->conn->owner);
2265 goto fail;
2266 }
2267 hdr.len--;
2268
2269 if (b_data(&h2c->dbuf) < 10)
2270 break; // missing padlen
2271
2272 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2273
2274 if (padlen > hdr.len) {
2275 /* RFC7540#6.1 : pad length = length of
2276 * frame payload or greater => error.
2277 */
2278 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2279 sess_log(h2c->conn->owner);
2280 goto fail;
2281 }
2282
2283 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2284 h2c->rcvd_c++;
2285 h2c->rcvd_s++;
2286 }
2287 b_del(&h2c->dbuf, 1);
2288 }
2289 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002290
2291 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002292 h2c->dfl = hdr.len;
2293 h2c->dsi = hdr.sid;
2294 h2c->dft = hdr.ft;
2295 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002296 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002297 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002298
2299 /* check for minimum basic frame format validity */
2300 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2301 if (ret != H2_ERR_NO_ERROR) {
2302 h2c_error(h2c, ret);
2303 sess_log(h2c->conn->owner);
2304 goto fail;
2305 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 }
2307
2308 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002309 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2310
Willy Tarreau567beb82018-12-18 16:52:44 +01002311 if (tmp_h2s != h2s && h2s && h2s->cs &&
2312 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002313 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002314 /* we may have to signal the upper layers */
2315 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002316 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002317 }
2318 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319
Willy Tarreaud7901432017-12-29 11:34:40 +01002320 if (h2c->st0 == H2_CS_FRAME_E)
2321 goto strm_err;
2322
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002323 if (h2s->st == H2_SS_IDLE &&
2324 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2325 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2326 * this state MUST be treated as a connection error
2327 */
2328 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002329 if (!h2c->nb_streams) {
2330 /* only log if no other stream can report the error */
2331 sess_log(h2c->conn->owner);
2332 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002333 break;
2334 }
2335
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002336 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2337 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2338 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002339 * this state MUST be treated as a stream error.
2340 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2341 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002342 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002343 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2345 else
2346 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002347 goto strm_err;
2348 }
2349
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* Below the management of frames received in closed state is a
2351 * bit hackish because the spec makes strong differences between
2352 * streams closed by receiving RST, sending RST, and seeing ES
2353 * in both directions. In addition to this, the creation of a
2354 * new stream reusing the identifier of a closed one will be
2355 * detected here. Given that we cannot keep track of all closed
2356 * streams forever, we consider that unknown closed streams were
2357 * closed on RST received, which allows us to respond with an
2358 * RST without breaking the connection (eg: to abort a transfer).
2359 * Some frames have to be silently ignored as well.
2360 */
2361 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002362 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002363 /* #5.1.1: The identifier of a newly
2364 * established stream MUST be numerically
2365 * greater than all streams that the initiating
2366 * endpoint has opened or reserved. This
2367 * governs streams that are opened using a
2368 * HEADERS frame and streams that are reserved
2369 * using PUSH_PROMISE. An endpoint that
2370 * receives an unexpected stream identifier
2371 * MUST respond with a connection error.
2372 */
2373 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2374 goto strm_err;
2375 }
2376
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002377 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002378 /* RFC7540#5.1:closed: an endpoint that
2379 * receives any frame other than PRIORITY after
2380 * receiving a RST_STREAM MUST treat that as a
2381 * stream error of type STREAM_CLOSED.
2382 *
2383 * Note that old streams fall into this category
2384 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002385 *
2386 * However, we cannot generalize this to all frame types. Those
2387 * carrying compression state must still be processed before
2388 * being dropped or we'll desynchronize the decoder. This can
2389 * happen with request trailers received after sending an
2390 * RST_STREAM, or with header/trailers responses received after
2391 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002392 */
2393 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2394 h2c->st0 = H2_CS_FRAME_E;
2395 goto strm_err;
2396 }
2397
2398 /* RFC7540#5.1:closed: if this state is reached as a
2399 * result of sending a RST_STREAM frame, the peer that
2400 * receives the RST_STREAM might have already sent
2401 * frames on the stream that cannot be withdrawn. An
2402 * endpoint MUST ignore frames that it receives on
2403 * closed streams after it has sent a RST_STREAM
2404 * frame. An endpoint MAY choose to limit the period
2405 * over which it ignores frames and treat frames that
2406 * arrive after this time as being in error.
2407 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002408 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002409 /* RFC7540#5.1:closed: any frame other than
2410 * PRIO/WU/RST in this state MUST be treated as
2411 * a connection error
2412 */
2413 if (h2c->dft != H2_FT_RST_STREAM &&
2414 h2c->dft != H2_FT_PRIORITY &&
2415 h2c->dft != H2_FT_WINDOW_UPDATE) {
2416 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2417 goto strm_err;
2418 }
2419 }
2420 }
2421
Willy Tarreauc0da1962017-10-30 18:38:00 +01002422#if 0
2423 // problem below: it is not possible to completely ignore such
2424 // streams as we need to maintain the compression state as well
2425 // and for this we need to completely process these frames (eg:
2426 // HEADERS frames) as well as counting DATA frames to emit
2427 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2428 // This is a typical case of layer violation where the
2429 // transported contents are critical to the connection's
2430 // validity and must be ignored at the same time :-(
2431
2432 /* graceful shutdown, ignore streams whose ID is higher than
2433 * the one advertised in GOAWAY. RFC7540#6.8.
2434 */
2435 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002436 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2437 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002438 h2c->dfl -= ret;
2439 ret = h2c->dfl == 0;
2440 goto strm_err;
2441 }
2442#endif
2443
Willy Tarreau7e98c052017-10-10 15:56:59 +02002444 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002445 case H2_FT_SETTINGS:
2446 if (h2c->st0 == H2_CS_FRAME_P)
2447 ret = h2c_handle_settings(h2c);
2448
2449 if (h2c->st0 == H2_CS_FRAME_A)
2450 ret = h2c_ack_settings(h2c);
2451 break;
2452
Willy Tarreaucf68c782017-10-10 17:11:41 +02002453 case H2_FT_PING:
2454 if (h2c->st0 == H2_CS_FRAME_P)
2455 ret = h2c_handle_ping(h2c);
2456
2457 if (h2c->st0 == H2_CS_FRAME_A)
2458 ret = h2c_ack_ping(h2c);
2459 break;
2460
Willy Tarreau26f95952017-07-27 17:18:30 +02002461 case H2_FT_WINDOW_UPDATE:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_handle_window_update(h2c, h2s);
2464 break;
2465
Willy Tarreau61290ec2017-10-17 08:19:21 +02002466 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002467 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2468 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2469 * frames' parsers consume all following CONTINUATION
2470 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002471 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002472 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2473 sess_log(h2c->conn->owner);
2474 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002475
Willy Tarreau13278b42017-10-13 19:23:14 +02002476 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002477 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002478 if (h2c->flags & H2_CF_IS_BACK)
2479 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2480 else
2481 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002482 if (tmp_h2s) {
2483 h2s = tmp_h2s;
2484 ret = 1;
2485 }
2486 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002487 break;
2488
Willy Tarreau454f9052017-10-26 19:40:35 +02002489 case H2_FT_DATA:
2490 if (h2c->st0 == H2_CS_FRAME_P)
2491 ret = h2c_frt_handle_data(h2c, h2s);
2492
2493 if (h2c->st0 == H2_CS_FRAME_A)
2494 ret = h2c_send_strm_wu(h2c);
2495 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002496
Willy Tarreau92153fc2017-12-03 19:46:19 +01002497 case H2_FT_PRIORITY:
2498 if (h2c->st0 == H2_CS_FRAME_P)
2499 ret = h2c_handle_priority(h2c);
2500 break;
2501
Willy Tarreaucd234e92017-08-18 10:59:39 +02002502 case H2_FT_RST_STREAM:
2503 if (h2c->st0 == H2_CS_FRAME_P)
2504 ret = h2c_handle_rst_stream(h2c, h2s);
2505 break;
2506
Willy Tarreaue96b0922017-10-30 00:28:29 +01002507 case H2_FT_GOAWAY:
2508 if (h2c->st0 == H2_CS_FRAME_P)
2509 ret = h2c_handle_goaway(h2c);
2510 break;
2511
Willy Tarreau1c661982017-10-30 13:52:01 +01002512 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002513 default:
2514 /* drop frames that we ignore. They may be larger than
2515 * the buffer so we drain all of their contents until
2516 * we reach the end.
2517 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002518 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2519 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002520 h2c->dfl -= ret;
2521 ret = h2c->dfl == 0;
2522 }
2523
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002524 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002525 /* We may have to send an RST if not done yet */
2526 if (h2s->st == H2_SS_ERROR)
2527 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002528
Willy Tarreaua20a5192017-12-27 11:02:06 +01002529 if (h2c->st0 == H2_CS_FRAME_E)
2530 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002531
Willy Tarreau7e98c052017-10-10 15:56:59 +02002532 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002533 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002534 break;
2535
2536 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002537 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002538 h2c->st0 = H2_CS_FRAME_H;
2539 }
2540 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002541
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002542 if (h2c->rcvd_c > 0 &&
2543 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2544 h2c_send_conn_wu(h2c);
2545
Willy Tarreau52eed752017-09-22 15:05:09 +02002546 fail:
2547 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002548 if (h2s && h2s->cs &&
2549 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002550 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002551 /* we may have to signal the upper layers */
2552 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002553 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002554 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002555
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002556 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002557}
2558
2559/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2560 * the end.
2561 */
2562static int h2_process_mux(struct h2c *h2c)
2563{
Olivier Houchard998410a2019-04-15 19:23:37 +02002564 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002565
Willy Tarreau01b44822018-10-03 14:26:37 +02002566 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2567 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2568 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2569 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2570 if (h2c->st0 == H2_CS_ERROR) {
2571 h2c->st0 = H2_CS_ERROR2;
2572 sess_log(h2c->conn->owner);
2573 }
2574 goto fail;
2575 }
2576 h2c->st0 = H2_CS_SETTINGS1;
2577 }
2578 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002579 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002580 return 1;
2581 }
2582
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002583 /* start by sending possibly pending window updates */
2584 if (h2c->rcvd_c > 0 &&
2585 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2586 h2c_send_conn_wu(h2c) < 0)
2587 goto fail;
2588
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002589 /* First we always process the flow control list because the streams
2590 * waiting there were already elected for immediate emission but were
2591 * blocked just on this.
2592 */
2593
Olivier Houchard998410a2019-04-15 19:23:37 +02002594 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002595 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2596 h2c->st0 >= H2_CS_ERROR)
2597 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002598
2599 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002600 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002601
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002602 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002603 /* For some reason, the upper layer failed to subsribe again,
2604 * so remove it from the send_list
2605 */
2606 if (!h2s->send_wait) {
2607 LIST_DEL_INIT(&h2s->list);
2608 continue;
2609 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002610 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002611 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002612 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002613 }
2614
Olivier Houchard998410a2019-04-15 19:23:37 +02002615 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002616 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2617 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002618
2619 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002620 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002621
Olivier Houchard998410a2019-04-15 19:23:37 +02002622 /* For some reason, the upper layer failed to subsribe again,
2623 * so remove it from the send_list
2624 */
2625 if (!h2s->send_wait) {
2626 LIST_DEL_INIT(&h2s->list);
2627 continue;
2628 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002629 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002630 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002631 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002632 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002633 }
2634
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002635 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002636 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002637 if (h2c->st0 == H2_CS_ERROR) {
2638 if (h2c->max_id >= 0) {
2639 h2c_send_goaway_error(h2c, NULL);
2640 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2641 return 0;
2642 }
2643
2644 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2645 }
2646 return 1;
2647 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002648 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002649}
2650
Willy Tarreau62f52692017-10-08 23:01:42 +02002651
Willy Tarreau479998a2018-11-18 06:30:59 +01002652/* Attempt to read data, and subscribe if none available.
2653 * The function returns 1 if data has been received, otherwise zero.
2654 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002655static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002656{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002657 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002658 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002659 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002660 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002662 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002663 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002664
Willy Tarreau315d8072017-12-10 22:17:57 +01002665 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002666 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002667
Willy Tarreau44e973f2018-03-01 17:49:30 +01002668 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002669 if (!buf) {
2670 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002671 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002672 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002673
Olivier Houchard7505f942018-08-21 18:10:44 +02002674 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002675 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002676 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2677 /* HTX in use : try to pre-align the buffer like the
2678 * rxbufs will be to optimize memory copies. We'll make
2679 * sure that the frame header lands at the end of the
2680 * HTX block to alias it upon recv. We cannot use the
2681 * head because rcv_buf() will realign the buffer if
2682 * it's empty. Thus we cheat and pretend we already
2683 * have a few bytes there.
2684 */
2685 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002686 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002687 }
2688 else
2689 max = b_room(buf);
2690
Olivier Houchard7505f942018-08-21 18:10:44 +02002691 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002692 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002693 else
2694 ret = 0;
2695 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002696
Olivier Houchard53216e72018-10-10 15:46:36 +02002697 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002698 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002699
Olivier Houcharda1411e62018-08-17 18:42:48 +02002700 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002701 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002702 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002703 }
2704
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002705 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002706 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002707 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002708}
2709
Willy Tarreau479998a2018-11-18 06:30:59 +01002710/* Try to send data if possible.
2711 * The function returns 1 if data have been sent, otherwise zero.
2712 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002713static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002714{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002715 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002716 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002717 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002718
2719 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002720 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002721
Olivier Houchard7505f942018-08-21 18:10:44 +02002722
Willy Tarreaua2af5122017-10-09 11:56:46 +02002723 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2724 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002725 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002726 }
2727
Willy Tarreaubc933932017-10-09 16:21:43 +02002728 /* This loop is quite simple : it tries to fill as much as it can from
2729 * pending streams into the existing buffer until it's reportedly full
2730 * or the end of send requests is reached. Then it tries to send this
2731 * buffer's contents out, marks it not full if at least one byte could
2732 * be sent, and tries again.
2733 *
2734 * The snd_buf() function normally takes a "flags" argument which may
2735 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2736 * data immediately comes and CO_SFL_STREAMER to indicate that the
2737 * connection is streaming lots of data (used to increase TLS record
2738 * size at the expense of latency). The former can be sent any time
2739 * there's a buffer full flag, as it indicates at least one stream
2740 * attempted to send and failed so there are pending data. An
2741 * alternative would be to set it as long as there's an active stream
2742 * but that would be problematic for ACKs until we have an absolute
2743 * guarantee that all waiters have at least one byte to send. The
2744 * latter should possibly not be set for now.
2745 */
2746
2747 done = 0;
2748 while (!done) {
2749 unsigned int flags = 0;
2750
2751 /* fill as much as we can into the current buffer */
2752 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2753 done = h2_process_mux(h2c);
2754
Olivier Houchard2b094432019-01-29 18:28:36 +01002755 if (h2c->flags & H2_CF_MUX_MALLOC)
2756 break;
2757
Willy Tarreaubc933932017-10-09 16:21:43 +02002758 if (conn->flags & CO_FL_ERROR)
2759 break;
2760
2761 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2762 flags |= CO_SFL_MSG_MORE;
2763
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002764 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002765 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002766 if (!ret)
2767 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002768 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002769 b_del(&h2c->mbuf, ret);
2770 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002771 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002772
2773 /* wrote at least one byte, the buffer is not full anymore */
2774 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2775 }
2776
Willy Tarreaua2af5122017-10-09 11:56:46 +02002777 if (conn->flags & CO_FL_SOCK_WR_SH) {
2778 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002779 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002780 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002781 /* We're not full anymore, so we can wake any task that are waiting
2782 * for us.
2783 */
2784 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002785 struct h2s *h2s;
2786
2787 list_for_each_entry(h2s, &h2c->send_list, list) {
2788 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2789 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002790
2791 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002792 continue;
2793
Olivier Houchard998410a2019-04-15 19:23:37 +02002794 /* For some reason, the upper layer failed to subsribe again,
2795 * so remove it from the send_list
2796 */
2797 if (!h2s->send_wait) {
2798 LIST_DEL_INIT(&h2s->list);
2799 continue;
2800 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002801 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002802 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002803 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002804 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002805 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002806 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002807 /* We're done, no more to send */
2808 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002809 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002810schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002811 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002812 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002813 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002814}
2815
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002816/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002817static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2818{
2819 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002820 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002821
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002822 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002823 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002824 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002825 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002826 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002827 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002828 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002829}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002830
Willy Tarreau62f52692017-10-08 23:01:42 +02002831/* callback called on any event by the connection handler.
2832 * It applies changes and returns zero, or < 0 if it wants immediate
2833 * destruction of the connection (which normally doesn not happen in h2).
2834 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002835static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002836{
Olivier Houchard7505f942018-08-21 18:10:44 +02002837 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002838
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002839 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002840 h2_process_demux(h2c);
2841
2842 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002844
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002846 h2c->flags &= ~H2_CF_DEM_DFULL;
2847 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002848 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002849
Willy Tarreau0b37d652018-10-03 10:33:02 +02002850 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002851 /* frontend is stopping, reload likely in progress, let's try
2852 * to announce a graceful shutdown if not yet done. We don't
2853 * care if it fails, it will be tried again later.
2854 */
2855 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2856 if (h2c->last_sid < 0)
2857 h2c->last_sid = (1U << 31) - 1;
2858 h2c_send_goaway_error(h2c, NULL);
2859 }
2860 }
2861
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002862 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002863 * If we received early data, and the handshake is done, wake
2864 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002865 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002866 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2867 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2868 struct eb32_node *node;
2869 struct h2s *h2s;
2870
2871 h2c->flags |= H2_CF_WAIT_FOR_HS;
2872 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2873
2874 while (node) {
2875 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002876 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002877 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002878 node = eb32_next(node);
2879 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002880 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002881
Willy Tarreau26bd7612017-10-09 16:47:04 +02002882 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002883 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2884 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2885 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002886 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002887
2888 if (eb_is_empty(&h2c->streams_by_id)) {
2889 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002890 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002891 return -1;
2892 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002893 }
2894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002895 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002896 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002897
Olivier Houchard53216e72018-10-10 15:46:36 +02002898 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2899 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2900 (h2c->st0 != H2_CS_ERROR &&
2901 !b_data(&h2c->mbuf) &&
2902 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2903 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002904 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002905
Willy Tarreau3f133572017-10-31 19:21:06 +01002906 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002907 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002908 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002909 task_queue(h2c->task);
2910 }
2911 else
2912 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002913 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002914
Olivier Houchard7505f942018-08-21 18:10:44 +02002915 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002916 return 0;
2917}
2918
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002919/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002920static int h2_wake(struct connection *conn)
2921{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002922 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002923
2924 return (h2_process(h2c));
2925}
2926
Willy Tarreauea392822017-10-31 10:02:25 +01002927/* Connection timeout management. The principle is that if there's no receipt
2928 * nor sending for a certain amount of time, the connection is closed. If the
2929 * MUX buffer still has lying data or is not allocatable, the connection is
2930 * immediately killed. If it's allocatable and empty, we attempt to send a
2931 * GOAWAY frame.
2932 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002933static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002934{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002935 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002936 int expired = tick_is_expired(t->expire, now_ms);
2937
Willy Tarreau0975f112018-03-29 15:22:59 +02002938 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002939 return t;
2940
Olivier Houchard3f795f72019-04-17 22:51:06 +02002941 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002942
2943 if (!h2c) {
2944 /* resources were already deleted */
2945 return NULL;
2946 }
2947
2948 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002949 h2c_error(h2c, H2_ERR_NO_ERROR);
2950 h2_wake_some_streams(h2c, 0, 0);
2951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002953 /* don't even try to send a GOAWAY, the buffer is stuck */
2954 h2c->flags |= H2_CF_GOAWAY_FAILED;
2955 }
2956
2957 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002958 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002959 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2960 h2c->flags |= H2_CF_GOAWAY_FAILED;
2961
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002962 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002963 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002964 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 b_del(&h2c->mbuf, ret);
2966 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002967 }
2968 }
Willy Tarreauea392822017-10-31 10:02:25 +01002969
Willy Tarreau0975f112018-03-29 15:22:59 +02002970 /* either we can release everything now or it will be done later once
2971 * the last stream closes.
2972 */
2973 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002974 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002975
Willy Tarreauea392822017-10-31 10:02:25 +01002976 return NULL;
2977}
2978
2979
Willy Tarreau62f52692017-10-08 23:01:42 +02002980/*******************************************/
2981/* functions below are used by the streams */
2982/*******************************************/
2983
2984/*
2985 * Attach a new stream to a connection
2986 * (Used for outgoing connections)
2987 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002988static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002989{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002990 struct conn_stream *cs;
2991 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002992 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002993
2994 cs = cs_new(conn);
2995 if (!cs)
2996 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002997 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002998 if (!h2s) {
2999 cs_free(cs);
3000 return NULL;
3001 }
3002 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003003}
3004
Willy Tarreaufafd3982018-11-18 21:29:20 +01003005/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3006 * We have to scan because we may have some orphan streams. It might be
3007 * beneficial to scan backwards from the end to reduce the likeliness to find
3008 * orphans.
3009 */
3010static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3011{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003012 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003013 struct h2s *h2s;
3014 struct eb32_node *node;
3015
3016 node = eb32_first(&h2c->streams_by_id);
3017 while (node) {
3018 h2s = container_of(node, struct h2s, by_id);
3019 if (h2s->cs)
3020 return h2s->cs;
3021 node = eb32_next(node);
3022 }
3023 return NULL;
3024}
3025
Willy Tarreau62f52692017-10-08 23:01:42 +02003026/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003027 * Destroy the mux and the associated connection, if it is no longer used
3028 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003029static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003030{
Christopher Faulet73c12072019-04-08 11:23:22 +02003031 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003032
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003033 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003034 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003035}
3036
3037/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003038 * Detach the stream from the connection and possibly release the connection.
3039 */
3040static void h2_detach(struct conn_stream *cs)
3041{
Willy Tarreau60935142017-10-16 18:11:19 +02003042 struct h2s *h2s = cs->ctx;
3043 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003044 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003045
3046 cs->ctx = NULL;
3047 if (!h2s)
3048 return;
3049
Olivier Houchard998410a2019-04-15 19:23:37 +02003050 /* The stream is about to die, so no need to attempt to run its task */
3051 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3052 h2s->send_wait != &h2s->wait_event) {
3053 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3054 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003055 /*
3056 * At this point, the stream_interface is supposed to have called
3057 * h2_unsubscribe(), so the only way there's still a
3058 * subscription that came from the stream_interface (as we
3059 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3060 * without the stream_interface involved) is that we subscribed
3061 * for sending, we woke the tasklet up and removed the
3062 * SUB_RETRY_SEND flag, so the stream_interface would not
3063 * know it has to unsubscribe for send, but the tasklet hasn't
3064 * run yet. Make sure to handle that by explicitely setting
3065 * send_wait to NULL, as nothing else will do it for us.
3066 */
3067 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003068 }
3069
Olivier Houchardf502aca2018-12-14 19:42:40 +01003070 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003071 h2c = h2s->h2c;
3072 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003073 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003074 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3075 !h2_frt_has_too_many_cs(h2c)) {
3076 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003077 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003078 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003079 }
Willy Tarreau60935142017-10-16 18:11:19 +02003080
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003081 /* this stream may be blocked waiting for some data to leave (possibly
3082 * an ES or RST frame), so orphan it in this case.
3083 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003084 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003085 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003086 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003087 return;
3088
Willy Tarreau45f752e2017-10-30 15:44:59 +01003089 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3090 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3091 /* unblock the connection if it was blocked on this
3092 * stream.
3093 */
3094 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3095 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003096 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003097 }
3098
Willy Tarreau71049cc2018-03-28 13:56:39 +02003099 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003100
Olivier Houchard8a786902018-12-15 16:05:40 +01003101 if (h2c->flags & H2_CF_IS_BACK &&
3102 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003103 if (!(h2c->conn->flags &
3104 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3105 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003106 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003107 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3108 h2c->conn->owner = NULL;
3109 if (eb_is_empty(&h2c->streams_by_id)) {
3110 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3111 /* The server doesn't want it, let's kill the connection right away */
3112 h2c->conn->mux->destroy(h2c->conn);
3113 return;
3114 }
3115 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003116 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003117 if (eb_is_empty(&h2c->streams_by_id)) {
3118 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3119 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3120 return;
3121 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003122 /* Never ever allow to reuse a connection from a non-reuse backend */
3123 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3124 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003125 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003126 struct server *srv = objt_server(h2c->conn->target);
3127
3128 if (srv) {
3129 if (h2c->conn->flags & CO_FL_PRIVATE)
3130 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3131 else
3132 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3133 }
3134
3135 }
3136 }
3137 }
3138
Willy Tarreaue323f342018-03-28 13:51:45 +02003139 /* We don't want to close right now unless we're removing the
3140 * last stream, and either the connection is in error, or it
3141 * reached the ID already specified in a GOAWAY frame received
3142 * or sent (as seen by last_sid >= 0).
3143 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003144 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003145 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003146 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003147 }
3148 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003149 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003150 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3151 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003152 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003153 else
3154 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003155 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003156}
3157
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003158/* Performs a synchronous or asynchronous shutr().
3159 * FIXME: guess what the return code tries to indicate!
3160 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003161static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003162{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003163 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003164 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003165
Willy Tarreau721c9742017-11-07 11:05:42 +01003166 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003167 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003168
Willy Tarreau18059042019-01-31 19:12:48 +01003169 /* a connstream may require us to immediately kill the whole connection
3170 * for example because of a "tcp-request content reject" rule that is
3171 * normally used to limit abuse. In this case we schedule a goaway to
3172 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003173 */
Willy Tarreau18059042019-01-31 19:12:48 +01003174 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3175 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3176 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3177 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3178 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003179 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3180 /* Nothing was never sent for this stream, so reset with
3181 * REFUSED_STREAM error to let the client retry the
3182 * request.
3183 */
3184 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3185 }
Willy Tarreau18059042019-01-31 19:12:48 +01003186
Willy Tarreau90c32322017-11-24 08:00:30 +01003187 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003188 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003189 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003190
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003191 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003192 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003193 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003194
Olivier Houchard7a977432019-03-21 15:47:13 +01003195 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003196add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003197 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003198 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003199 if (h2s->flags & H2_SF_BLK_MFCTL) {
3200 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3201 h2s->send_wait = sw;
3202 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3203 h2s->send_wait = sw;
3204 LIST_ADDQ(&h2c->send_list, &h2s->list);
3205 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003206 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003207 /* Let the handler know we want shutr */
3208 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003209 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003210}
3211
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003212/* Performs a synchronous or asynchronous shutw().
3213 * FIXME: guess what the return code tries to indicate!
3214 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003215static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003216{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003217 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003218 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003219
Willy Tarreau721c9742017-11-07 11:05:42 +01003220 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003221 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003222
Willy Tarreau67434202017-11-06 20:20:51 +01003223 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003224 /* we can cleanly close using an empty data frame only after headers */
3225
3226 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3227 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003228 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003229
3230 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003231 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003232 else
3233 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003234 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003235 /* a connstream may require us to immediately kill the whole connection
3236 * for example because of a "tcp-request content reject" rule that is
3237 * normally used to limit abuse. In this case we schedule a goaway to
3238 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003239 */
Willy Tarreau18059042019-01-31 19:12:48 +01003240 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3241 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3242 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3243 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3244 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003245 else {
3246 /* Nothing was never sent for this stream, so reset with
3247 * REFUSED_STREAM error to let the client retry the
3248 * request.
3249 */
3250 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3251 }
Willy Tarreau18059042019-01-31 19:12:48 +01003252
Willy Tarreau90c32322017-11-24 08:00:30 +01003253 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003254 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003255 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003256
Willy Tarreau00dd0782018-03-01 16:31:34 +01003257 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003258 }
3259
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003260 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003261 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003262 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003263
3264 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003265 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003266 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003267 if (h2s->flags & H2_SF_BLK_MFCTL) {
3268 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3269 h2s->send_wait = sw;
3270 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3271 h2s->send_wait = sw;
3272 LIST_ADDQ(&h2c->send_list, &h2s->list);
3273 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003274 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003275 /* let the handler know we want to shutw */
3276 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003277 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003278}
3279
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003280/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3281 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3282 * and prevented the last frame from being emitted.
3283 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003284static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3285{
3286 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003287 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003288 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003289
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003290 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003291 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003292 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003293 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003294 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003295
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003296 if (!ret) {
3297 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003298 LIST_DEL_INIT(&h2s->list);
3299 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003300 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003301 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003302 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003303 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003304
3305 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003306 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003307 }
3308
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003309 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003310}
3311
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003312/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003313static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3314{
3315 struct h2s *h2s = cs->ctx;
3316
3317 if (!mode)
3318 return;
3319
3320 h2_do_shutr(h2s);
3321}
3322
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003323/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003324static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3325{
3326 struct h2s *h2s = cs->ctx;
3327
3328 h2_do_shutw(h2s);
3329}
3330
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003331/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003332 * HTX request or response depending on the connection's side. Returns a
3333 * positive value on success, a negative value on failure, or 0 if it couldn't
3334 * proceed. May report connection errors in h2c->errcode if the frame is
3335 * non-decodable and the connection unrecoverable. In absence of connection
3336 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003337 *
3338 * The function may fold CONTINUATION frames into the initial HEADERS frame
3339 * by removing padding and next frame header, then moving the CONTINUATION
3340 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3341 * leaving a hole between the main frame and the beginning of the next one.
3342 * The possibly remaining incomplete or next frame at the end may be moved
3343 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3344 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3345 *
3346 * A buffer at the beginning of processing may look like this :
3347 *
3348 * ,---.---------.-----.--------------.--------------.------.---.
3349 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3350 * `---^---------^-----^--------------^--------------^------^---'
3351 * | | <-----> | |
3352 * area | dpl | wrap
3353 * |<--------------> |
3354 * | dfl |
3355 * |<-------------------------------------------------->|
3356 * head data
3357 *
3358 * Padding is automatically overwritten when folding, participating to the
3359 * hole size after dfl :
3360 *
3361 * ,---.------------------------.-----.--------------.------.---.
3362 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3363 * `---^------------------------^-----^--------------^------^---'
3364 * | | <-----> | |
3365 * area | hole | wrap
3366 * |<-----------------------> |
3367 * | dfl |
3368 * |<-------------------------------------------------->|
3369 * head data
3370 *
3371 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3372 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3373 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003374 *
3375 * The <flags> field must point to either the stream's flags or to a copy of it
3376 * so that the function can update the following flags :
3377 * - H2_SF_DATA_CLEN when content-length is seen
3378 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3379 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003380 *
3381 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3382 * decoding, in order to detect if we're dealing with a headers or a trailers
3383 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003384 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003385static 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 +02003386{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003387 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003388 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003389 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003390 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003391 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003392 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003393 int flen; // header frame len
3394 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003395 int ret = 0;
3396 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003397 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003398 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003399
Willy Tarreauea18f862018-12-22 20:19:26 +01003400next_frame:
3401 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3402 goto leave; // incomplete input frame
3403
3404 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3405 * this case, we'll try to paste it immediately after the initial
3406 * HEADERS frame payload and kill any possible padding. The initial
3407 * frame's length will be increased to represent the concatenation
3408 * of the two frames. The next frame is read from position <tlen>
3409 * and written at position <flen> (minus padding if some is present).
3410 */
3411 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3412 struct h2_fh hdr;
3413 int clen; // CONTINUATION frame's payload length
3414
3415 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3416 /* no more data, the buffer may be full, either due to
3417 * too large a frame or because of too large a hole that
3418 * we're going to compact at the end.
3419 */
3420 goto leave;
3421 }
3422
3423 if (hdr.ft != H2_FT_CONTINUATION) {
3424 /* RFC7540#6.10: frame of unexpected type */
3425 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3426 goto fail;
3427 }
3428
3429 if (hdr.sid != h2c->dsi) {
3430 /* RFC7540#6.10: frame of different stream */
3431 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3432 goto fail;
3433 }
3434
3435 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3436 /* RFC7540#4.2: invalid frame length */
3437 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3438 goto fail;
3439 }
3440
3441 /* detect when we must stop aggragating frames */
3442 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3443
3444 /* Take as much as we can of the CONTINUATION frame's payload */
3445 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3446 if (clen > hdr.len)
3447 clen = hdr.len;
3448
3449 /* Move the frame's payload over the padding, hole and frame
3450 * header. At least one of hole or dpl is null (see diagrams
3451 * above). The hole moves after the new aggragated frame.
3452 */
3453 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3454 h2c->dfl += clen - h2c->dpl;
3455 hole += h2c->dpl + 9;
3456 h2c->dpl = 0;
3457 goto next_frame;
3458 }
3459
3460 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003461
Willy Tarreau13278b42017-10-13 19:23:14 +02003462 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003463 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003464 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003465 copy = alloc_trash_chunk();
3466 if (!copy) {
3467 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3468 goto fail;
3469 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3471 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3472 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003473 }
3474
Willy Tarreau13278b42017-10-13 19:23:14 +02003475 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3476 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003477 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003478 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3479 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003480 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003481 }
3482
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003483 if (flen < 5) {
3484 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3485 goto fail;
3486 }
3487
Willy Tarreau13278b42017-10-13 19:23:14 +02003488 hdrs += 5; // stream dep = 4, weight = 1
3489 flen -= 5;
3490 }
3491
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003492 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003493 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003494 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003495 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003496
Willy Tarreau937f7602018-02-26 15:22:17 +01003497 /* we can't retry a failed decompression operation so we must be very
3498 * careful not to take any risks. In practice the output buffer is
3499 * always empty except maybe for trailers, in which case we simply have
3500 * to wait for the upper layer to finish consuming what is available.
3501 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003502
3503 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003504 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003505 if (!htx_is_empty(htx)) {
3506 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003507 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003508 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003509 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003510 if (b_data(rxbuf)) {
3511 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003512 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003513 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003514
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003515 rxbuf->head = 0;
3516 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003517 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003518
Willy Tarreau25919232019-01-03 14:48:18 +01003519 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003520 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3521 sizeof(list)/sizeof(list[0]), tmp);
3522 if (outlen < 0) {
3523 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3524 goto fail;
3525 }
3526
Willy Tarreau25919232019-01-03 14:48:18 +01003527 /* The PACK decompressor was updated, let's update the input buffer and
3528 * the parser's state to commit these changes and allow us to later
3529 * fail solely on the stream if needed.
3530 */
3531 b_del(&h2c->dbuf, h2c->dfl + hole);
3532 h2c->dfl = hole = 0;
3533 h2c->st0 = H2_CS_FRAME_H;
3534
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003535 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003536 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003537
Willy Tarreau88d138e2019-01-02 19:38:14 +01003538 if (*flags & H2_SF_HEADERS_RCVD)
3539 goto trailers;
3540
3541 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003542 if (htx) {
3543 /* HTX mode */
3544 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003545 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003546 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003547 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003548 } else {
3549 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003550 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003551 if (outlen > 0)
3552 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003553 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003554
3555 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003556 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003557 goto fail;
3558 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003559
Willy Tarreau174b06a2018-04-25 18:13:58 +02003560 if (msgf & H2_MSGF_BODY) {
3561 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003562 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003563 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003564 if (htx)
3565 htx->extra = *body_len;
3566 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003567 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003568 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003569 }
3570
Willy Tarreau88d138e2019-01-02 19:38:14 +01003571 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003572 /* indicate that a HEADERS frame was received for this stream, except
3573 * for 1xx responses. For 1xx responses, another HEADERS frame is
3574 * expected.
3575 */
3576 if (!(msgf & H2_MSGF_RSP_1XX))
3577 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003578
Christopher Faulet0b465482019-02-19 15:14:23 +01003579 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003580 /* Mark the end of message, either using EOM in HTX or with the
3581 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003582 * is not set during headers with END_STREAM. For HTX trailers,
3583 * we must not leave an HTX trailers block not followed by an
3584 * EOM block, the two must be atomic. Thus if we fail to emit
3585 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003586 */
3587 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003588 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3589 struct htx_blk *tail = htx_get_tail_blk(htx);
3590
3591 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3592 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003593 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003594 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003595 }
3596 else if (*flags & H2_SF_DATA_CHNK) {
3597 if (!b_putblk(rxbuf, "\r\n", 2))
3598 goto fail;
3599 }
3600 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003601
Willy Tarreau86277d42019-01-02 15:36:11 +01003602 /* success */
3603 ret = 1;
3604
Willy Tarreau68dd9852017-07-03 14:44:26 +02003605 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003606 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003607 * move the remaining data over it.
3608 */
3609 if (hole) {
3610 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3611 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3612 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3613 b_sub(&h2c->dbuf, hole);
3614 }
3615
Willy Tarreau97215ca2019-04-29 10:20:21 +02003616 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003617 /* too large frames */
3618 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003619 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003620 }
3621
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003622 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003623 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003624 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003625 return ret;
3626
Willy Tarreau68dd9852017-07-03 14:44:26 +02003627 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003628 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003629 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003630
3631 trailers:
3632 /* This is the last HEADERS frame hence a trailer */
3633
3634 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3635 /* It's a trailer but it's missing ES flag */
3636 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3637 goto fail;
3638 }
3639
3640 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3641 * block, and when using chunks we must send the 0 CRLF marker. For
3642 * other modes, the trailers are silently dropped.
3643 */
3644 if (htx) {
3645 if (!htx_add_endof(htx, HTX_BLK_EOD))
3646 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003647 if (h2_make_htx_trailers(list, htx) <= 0)
3648 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003649 }
3650 else if (*flags & H2_SF_DATA_CHNK) {
3651 /* Legacy mode with chunked encoding : we must finalize the
3652 * data block message emit the trailing CRLF */
3653 if (!b_putblk(rxbuf, "0\r\n", 3))
3654 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003655
3656 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3657 if (outlen > 0)
3658 b_add(rxbuf, outlen);
3659 else
3660 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003661 }
3662
3663 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003664}
3665
Willy Tarreau454f9052017-10-26 19:40:35 +02003666/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3667 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3668 * in use, a new chunk is emitted for each frame. This is supposed to fit
3669 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3670 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3671 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003672 * parser state is automatically updated. Returns > 0 if it could completely
3673 * send the current frame, 0 if it couldn't complete, in which case
3674 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3675 * DATA frame can return 0 as a valid result). Stream errors are reported in
3676 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3677 * have checked the frame header and ensured that the frame was complete or the
3678 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003679 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003680static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003681{
3682 struct h2c *h2c = h2s->h2c;
3683 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003685 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003686 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003687 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003688
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003689 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003690
Olivier Houchard638b7992018-08-16 15:41:52 +02003691 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003692 if (!csbuf) {
3693 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003694 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003695 }
3696
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003697try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003698 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003699 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3700 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003701 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003702 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003703
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003704 if (flen > b_data(&h2c->dbuf)) {
3705 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003706 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003707 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003708 }
3709
Willy Tarreaua9b77962019-01-31 07:23:00 +01003710 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003711 block1 = htx_free_data_space(htx);
3712 if (!block1) {
3713 h2c->flags |= H2_CF_DEM_SFULL;
3714 goto fail;
3715 }
3716 if (flen > block1)
3717 flen = block1;
3718
3719 /* here, flen is the max we can copy into the output buffer */
3720 block1 = b_contig_data(&h2c->dbuf, 0);
3721 if (flen > block1)
3722 flen = block1;
3723
3724 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3725 h2c->flags |= H2_CF_DEM_SFULL;
3726 goto fail;
3727 }
3728
3729 b_del(&h2c->dbuf, flen);
3730 h2c->dfl -= flen;
3731 h2c->rcvd_c += flen;
3732 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003733
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003734 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003735 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003736 htx->extra = h2s->body_len;
3737 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003738 goto try_again;
3739 }
3740 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003741 /* it doesn't fit and the buffer is fragmented,
3742 * so let's defragment it and try again.
3743 */
3744 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003745 }
3746
Willy Tarreaueba10f22018-04-25 20:44:22 +02003747 /* chunked-encoding requires more room */
3748 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003749 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003750 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3751 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3752 (chklen < 1048576) ? 4 : 8;
3753 chklen += 4; // CRLF, CRLF
3754 }
3755
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003756 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003757 if (flen + chklen > b_room(csbuf)) {
3758 if (chklen >= b_room(csbuf)) {
3759 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003760 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003761 }
3762 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003763 }
3764
3765 if (h2s->flags & H2_SF_DATA_CHNK) {
3766 /* emit the chunk size */
3767 unsigned int chksz = flen;
3768 char str[10];
3769 char *beg;
3770
3771 beg = str + sizeof(str);
3772 *--beg = '\n';
3773 *--beg = '\r';
3774 do {
3775 *--beg = hextab[chksz & 0xF];
3776 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003777 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003778 }
3779
Willy Tarreau454f9052017-10-26 19:40:35 +02003780 /* Block1 is the length of the first block before the buffer wraps,
3781 * block2 is the optional second block to reach the end of the frame.
3782 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003783 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003784 if (block1 > flen)
3785 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003786 block2 = flen - block1;
3787
3788 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003789 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003790
3791 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003792 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003793
Willy Tarreaueba10f22018-04-25 20:44:22 +02003794 if (h2s->flags & H2_SF_DATA_CHNK) {
3795 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003796 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003797 }
3798
Willy Tarreau454f9052017-10-26 19:40:35 +02003799 /* now mark the input data as consumed (will be deleted from the buffer
3800 * by the caller when seeing FRAME_A after sending the window update).
3801 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003802 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003803 h2c->dfl -= flen;
3804 h2c->rcvd_c += flen;
3805 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3806
Willy Tarreau1915ca22019-01-24 11:49:37 +01003807 if (h2s->flags & H2_SF_DATA_CLEN)
3808 h2s->body_len -= flen;
3809
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003810 if (h2c->dfl > h2c->dpl) {
3811 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003812 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003813 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003814 }
3815
Willy Tarreau4a28da12018-01-04 14:41:00 +01003816 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003817 /* here we're done with the frame, all the payload (except padding) was
3818 * transferred.
3819 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003820
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003821 if (h2c->dff & H2_F_DATA_END_STREAM) {
3822 if (htx) {
3823 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3824 h2c->flags |= H2_CF_DEM_SFULL;
3825 goto fail;
3826 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003827 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003828 else if (h2s->flags & H2_SF_DATA_CHNK) {
3829 /* emit the trailing 0 CRLF CRLF */
3830 if (b_room(csbuf) < 5) {
3831 h2c->flags |= H2_CF_DEM_SFULL;
3832 goto fail;
3833 }
3834 chklen += 5;
3835 b_putblk(csbuf, "0\r\n\r\n", 5);
3836 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003837 }
3838
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003839 h2c->rcvd_c += h2c->dpl;
3840 h2c->rcvd_s += h2c->dpl;
3841 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003842 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003843 if (htx)
3844 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003845 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003846 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003847 if (htx)
3848 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003849 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003850}
3851
Willy Tarreau5dd17352018-06-14 13:33:30 +02003852/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3853 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3854 * number of bytes sent. The caller must check the stream's status to detect
3855 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003856 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003857static 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 +02003858{
3859 struct http_hdr list[MAX_HTTP_HDR];
3860 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003861 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003862 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003863 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003864 int es_now = 0;
3865 int ret = 0;
3866 int hdr;
3867
3868 if (h2c_mux_busy(h2c, h2s)) {
3869 h2s->flags |= H2_SF_BLK_MBUSY;
3870 return 0;
3871 }
3872
Willy Tarreau44e973f2018-03-01 17:49:30 +01003873 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874 h2c->flags |= H2_CF_MUX_MALLOC;
3875 h2s->flags |= H2_SF_BLK_MROOM;
3876 return 0;
3877 }
3878
3879 /* First, try to parse the H1 response and index it into <list>.
3880 * NOTE! Since it comes from haproxy, we *know* that a response header
3881 * block does not wrap and we can safely read it this way without
3882 * having to realign the buffer.
3883 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003884 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003885 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003887 /* incomplete or invalid response, this is abnormal coming from
3888 * haproxy and may only result in a bad errorfile or bad Lua code
3889 * so that won't be fixed, raise an error now.
3890 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003891 * FIXME: we should instead add the ability to only return a
3892 * 502 bad gateway. But in theory this is not supposed to
3893 * happen.
3894 */
3895 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3896 ret = 0;
3897 goto end;
3898 }
3899
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003900 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003901
3902 /* certain statuses have no body or an empty one, regardless of
3903 * what the headers say.
3904 */
3905 if (sl.st.status >= 100 && sl.st.status < 200) {
3906 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3907 h1m->curr_len = h1m->body_len = 0;
3908 }
3909 else if (sl.st.status == 204 || sl.st.status == 304) {
3910 /* no contents, claim c-len is present and set to zero */
3911 h1m->flags &= ~H1_MF_CHNK;
3912 h1m->flags |= H1_MF_CLEN;
3913 h1m->curr_len = h1m->body_len = 0;
3914 }
3915
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003916 chunk_reset(&outbuf);
3917
3918 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003920 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003923 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924 break;
3925 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003926 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003927 }
3928
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003929 if (outbuf.size < 9)
3930 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003931
3932 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003933 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3934 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3935 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936
3937 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003938 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003939 /* this is an unparsable response */
3940 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3941 ret = 0;
3942 goto end;
3943 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003944
3945 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003946 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003947 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003948 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003949 }
3950
3951 /* encode all headers, stop at empty name */
3952 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003953 /* these ones do not exist in H2 and must be dropped. */
3954 if (isteq(list[hdr].n, ist("connection")) ||
3955 isteq(list[hdr].n, ist("proxy-connection")) ||
3956 isteq(list[hdr].n, ist("keep-alive")) ||
3957 isteq(list[hdr].n, ist("upgrade")) ||
3958 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003959 continue;
3960
3961 if (isteq(list[hdr].n, ist("")))
3962 break; // end
3963
3964 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3965 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003966 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003967 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003968 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003969 }
3970 }
3971
3972 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003973 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003974 es_now = 1;
3975
3976 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003978
3979 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003980 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003981
3982 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003983 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003984
3985 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003986 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003987 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003988
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003989 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003990 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003991 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003992
Willy Tarreau801250e2018-09-11 11:45:04 +02003993 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003994 h2s->flags |= H2_SF_ES_SENT;
3995 if (h2s->st == H2_SS_OPEN)
3996 h2s->st = H2_SS_HLOC;
3997 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003998 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003999 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004000 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004001 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004002 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004003 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004004 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004005 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004006 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004007
4008 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004009
4010 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004011 //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 +02004012 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004013 full:
4014 h1m_init_res(h1m);
4015 h1m->err_pos = -1; // don't care about errors on the response path
4016 h2c->flags |= H2_CF_MUX_MFULL;
4017 h2s->flags |= H2_SF_BLK_MROOM;
4018 ret = 0;
4019 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004020}
4021
Willy Tarreau5dd17352018-06-14 13:33:30 +02004022/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4023 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4024 * the number of bytes sent. The caller must check the stream's status to
4025 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004026 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004027static 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 +02004028{
4029 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004030 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004031 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004032 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004033 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004034 int es_now = 0;
4035 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004036 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004037 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038
4039 if (h2c_mux_busy(h2c, h2s)) {
4040 h2s->flags |= H2_SF_BLK_MBUSY;
4041 goto end;
4042 }
4043
Willy Tarreau44e973f2018-03-01 17:49:30 +01004044 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 h2c->flags |= H2_CF_MUX_MALLOC;
4046 h2s->flags |= H2_SF_BLK_MROOM;
4047 goto end;
4048 }
4049
4050 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004051 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004052 goto end;
4053
4054 chunk_reset(&outbuf);
4055
4056 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004057 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004058 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004059 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004061 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 break;
4063 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004064 /* If there are pending data in the output buffer, and we have
4065 * less than 1/4 of the mbuf's size and everything fits, we'll
4066 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4067 * is full and wait, to save some slow realign calls.
4068 */
4069 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4070 h2c->flags |= H2_CF_MUX_MFULL;
4071 h2s->flags |= H2_SF_BLK_MROOM;
4072 goto end;
4073 }
4074
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004075 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004076 }
4077
4078 if (outbuf.size < 9) {
4079 h2c->flags |= H2_CF_MUX_MFULL;
4080 h2s->flags |= H2_SF_BLK_MROOM;
4081 goto end;
4082 }
4083
4084 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004085 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4086 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4087 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004088
4089 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4090 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004091 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004092 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004093 break;
4094 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004095 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004096 if ((long long)size > h1m->curr_len)
4097 size = h1m->curr_len;
4098 break;
4099 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004100 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004101 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004102 if (!ret)
4103 goto end;
4104
4105 if (ret < 0) {
4106 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004107 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004108 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4109 goto end;
4110 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004111 max -= ret;
4112 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004114 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 }
4116
Willy Tarreau801250e2018-09-11 11:45:04 +02004117 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004118 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004119 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004120 if (!ret)
4121 goto end;
4122
4123 if (ret < 0) {
4124 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004125 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004126 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4127 goto end;
4128 }
4129
4130 size = chunk;
4131 h1m->curr_len = chunk;
4132 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004133 max -= ret;
4134 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004135 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004136 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004137 if (!size)
4138 goto send_empty;
4139 }
4140
4141 /* in MSG_DATA state, continue below */
4142 size = h1m->curr_len;
4143 break;
4144 }
4145
4146 /* we have in <size> the exact number of bytes we need to copy from
4147 * the H1 buffer. We need to check this against the connection's and
4148 * the stream's send windows, and to ensure that this fits in the max
4149 * frame size and in the buffer's available space minus 9 bytes (for
4150 * the frame header). The connection's flow control is applied last so
4151 * that we can use a separate list of streams which are immediately
4152 * unblocked on window opening. Note: we don't implement padding.
4153 */
4154
Willy Tarreau5dd17352018-06-14 13:33:30 +02004155 if (size > max)
4156 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004157
4158 if (size > h2s->mws)
4159 size = h2s->mws;
4160
4161 if (size <= 0) {
4162 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004163 if (!LIST_ISEMPTY(&h2s->list))
4164 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004165 goto end;
4166 }
4167
4168 if (h2c->mfs && size > h2c->mfs)
4169 size = h2c->mfs;
4170
4171 if (size + 9 > outbuf.size) {
4172 /* we have an opportunity for enlarging the too small
4173 * available space, let's try.
4174 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004175 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004176 goto realign_again;
4177 size = outbuf.size - 9;
4178 }
4179
4180 if (size <= 0) {
4181 h2c->flags |= H2_CF_MUX_MFULL;
4182 h2s->flags |= H2_SF_BLK_MROOM;
4183 goto end;
4184 }
4185
4186 if (size > h2c->mws)
4187 size = h2c->mws;
4188
4189 if (size <= 0) {
4190 h2s->flags |= H2_SF_BLK_MFCTL;
4191 goto end;
4192 }
4193
4194 /* copy whatever we can */
4195 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004196 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004197 if (ret == 1)
4198 len2 = 0;
4199
4200 if (!ret || len1 + len2 < size) {
4201 /* FIXME: must normally never happen */
4202 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4203 goto end;
4204 }
4205
4206 /* limit len1/len2 to size */
4207 if (len1 + len2 > size) {
4208 int sub = len1 + len2 - size;
4209
4210 if (len2 > sub)
4211 len2 -= sub;
4212 else {
4213 sub -= len2;
4214 len2 = 0;
4215 len1 -= sub;
4216 }
4217 }
4218
4219 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004220 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004221 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004222 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004223
4224 send_empty:
4225 /* we may need to add END_STREAM */
4226 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4227 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004228 *
4229 * FIXME: what we do here is not correct because we send end_stream
4230 * before knowing if we'll have to send a HEADERS frame for the
4231 * trailers. More importantly we're not consuming the trailing CRLF
4232 * after the end of trailers, so it will be left to the caller to
4233 * eat it. The right way to do it would be to measure trailers here
4234 * and to send ES only if there are no trailers.
4235 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004236 */
4237 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004238 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004239 es_now = 1;
4240
4241 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004243
4244 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004245 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004246
4247 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004248 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004249
4250 /* consume incoming H1 response */
4251 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004252 max -= size;
4253 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004254 total += size;
4255 h1m->curr_len -= size;
4256 h2s->mws -= size;
4257 h2c->mws -= size;
4258
4259 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004260 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004261 goto new_frame;
4262 }
4263 }
4264
4265 if (es_now) {
4266 if (h2s->st == H2_SS_OPEN)
4267 h2s->st = H2_SS_HLOC;
4268 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004269 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004270
Willy Tarreau35a62702018-02-27 15:37:25 +01004271 if (!(h1m->flags & H1_MF_CHNK)) {
4272 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004273 total += max;
4274 ofs += max;
4275 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004276
Willy Tarreau801250e2018-09-11 11:45:04 +02004277 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004278 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004279
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004280 h2s->flags |= H2_SF_ES_SENT;
4281 }
4282
4283 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004284 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 +02004285 return total;
4286}
4287
Willy Tarreau115e83b2018-12-01 19:17:53 +01004288/* Try to send a HEADERS frame matching HTX response present in HTX message
4289 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4290 * must check the stream's status to detect any error which might have happened
4291 * subsequently to a successful send. The htx blocks are automatically removed
4292 * from the message. The htx message is assumed to be valid since produced from
4293 * the internal code, hence it contains a start line, an optional series of
4294 * header blocks and an end of header, otherwise an invalid frame could be
4295 * emitted and the resulting htx message could be left in an inconsistent state.
4296 */
4297static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4298{
4299 struct http_hdr list[MAX_HTTP_HDR];
4300 struct h2c *h2c = h2s->h2c;
4301 struct htx_blk *blk;
4302 struct htx_blk *blk_end;
4303 struct buffer outbuf;
4304 struct htx_sl *sl;
4305 enum htx_blk_type type;
4306 int es_now = 0;
4307 int ret = 0;
4308 int hdr;
4309 int idx;
4310
4311 if (h2c_mux_busy(h2c, h2s)) {
4312 h2s->flags |= H2_SF_BLK_MBUSY;
4313 return 0;
4314 }
4315
4316 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4317 h2c->flags |= H2_CF_MUX_MALLOC;
4318 h2s->flags |= H2_SF_BLK_MROOM;
4319 return 0;
4320 }
4321
4322 /* determine the first block which must not be deleted, blk_end may
4323 * be NULL if all blocks have to be deleted.
4324 */
4325 idx = htx_get_head(htx);
4326 blk_end = NULL;
4327 while (idx != -1) {
4328 type = htx_get_blk_type(htx_get_blk(htx, idx));
4329 idx = htx_get_next(htx, idx);
4330 if (type == HTX_BLK_EOH) {
4331 if (idx != -1)
4332 blk_end = htx_get_blk(htx, idx);
4333 break;
4334 }
4335 }
4336
4337 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004338 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004339 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004340 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004341 if (h2s->status < 100 || h2s->status > 999)
4342 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004343
4344 /* and the rest of the headers, that we dump starting at header 0 */
4345 hdr = 0;
4346
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004347 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004348 while ((idx = htx_get_next(htx, idx)) != -1) {
4349 blk = htx_get_blk(htx, idx);
4350 type = htx_get_blk_type(blk);
4351
4352 if (type == HTX_BLK_UNUSED)
4353 continue;
4354
4355 if (type != HTX_BLK_HDR)
4356 break;
4357
4358 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4359 goto fail;
4360
4361 list[hdr].n = htx_get_blk_name(htx, blk);
4362 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004363 hdr++;
4364 }
4365
4366 /* marker for end of headers */
4367 list[hdr].n = ist("");
4368
4369 if (h2s->status == 204 || h2s->status == 304) {
4370 /* no contents, claim c-len is present and set to zero */
4371 es_now = 1;
4372 }
4373
4374 chunk_reset(&outbuf);
4375
4376 while (1) {
4377 outbuf.area = b_tail(&h2c->mbuf);
4378 outbuf.size = b_contig_space(&h2c->mbuf);
4379 outbuf.data = 0;
4380
4381 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4382 break;
4383 realign_again:
4384 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4385 }
4386
4387 if (outbuf.size < 9)
4388 goto full;
4389
4390 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4391 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4392 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4393 outbuf.data = 9;
4394
4395 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004396 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004397 if (b_space_wraps(&h2c->mbuf))
4398 goto realign_again;
4399 goto full;
4400 }
4401
4402 /* encode all headers, stop at empty name */
4403 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4404 /* these ones do not exist in H2 and must be dropped. */
4405 if (isteq(list[hdr].n, ist("connection")) ||
4406 isteq(list[hdr].n, ist("proxy-connection")) ||
4407 isteq(list[hdr].n, ist("keep-alive")) ||
4408 isteq(list[hdr].n, ist("upgrade")) ||
4409 isteq(list[hdr].n, ist("transfer-encoding")))
4410 continue;
4411
4412 if (isteq(list[hdr].n, ist("")))
4413 break; // end
4414
4415 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4416 /* output full */
4417 if (b_space_wraps(&h2c->mbuf))
4418 goto realign_again;
4419 goto full;
4420 }
4421 }
4422
Christopher Faulet0b465482019-02-19 15:14:23 +01004423 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004424 * FIXME: we should also set it when we know for sure that the
4425 * content-length is zero as well as on 204/304
4426 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004427 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4428 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004429 es_now = 1;
4430
Willy Tarreau927b88b2019-03-04 08:03:25 +01004431 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004432 es_now = 1;
4433
4434 /* update the frame's size */
4435 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4436
4437 if (es_now)
4438 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4439
4440 /* commit the H2 response */
4441 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004442
4443 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4444 * 1xx responses, another HEADERS frame is expected.
4445 */
4446 if (h2s->status >= 200 || h2s->status == 101)
4447 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004448
Willy Tarreau115e83b2018-12-01 19:17:53 +01004449 if (es_now) {
4450 h2s->flags |= H2_SF_ES_SENT;
4451 if (h2s->st == H2_SS_OPEN)
4452 h2s->st = H2_SS_HLOC;
4453 else
4454 h2s_close(h2s);
4455 }
4456
4457 /* OK we could properly deliver the response */
4458
4459 /* remove all header blocks including the EOH and compute the
4460 * corresponding size.
4461 *
4462 * FIXME: We should remove everything when es_now is set.
4463 */
4464 ret = 0;
4465 idx = htx_get_head(htx);
4466 blk = htx_get_blk(htx, idx);
4467 while (blk != blk_end) {
4468 ret += htx_get_blksz(blk);
4469 blk = htx_remove_blk(htx, blk);
4470 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004471
4472 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4473 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004474 end:
4475 return ret;
4476 full:
4477 h2c->flags |= H2_CF_MUX_MFULL;
4478 h2s->flags |= H2_SF_BLK_MROOM;
4479 ret = 0;
4480 goto end;
4481 fail:
4482 /* unparsable HTX messages, too large ones to be produced in the local
4483 * list etc go here (unrecoverable errors).
4484 */
4485 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4486 ret = 0;
4487 goto end;
4488}
4489
Willy Tarreau80739692018-10-05 11:35:57 +02004490/* Try to send a HEADERS frame matching HTX request present in HTX message
4491 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4492 * must check the stream's status to detect any error which might have happened
4493 * subsequently to a successful send. The htx blocks are automatically removed
4494 * from the message. The htx message is assumed to be valid since produced from
4495 * the internal code, hence it contains a start line, an optional series of
4496 * header blocks and an end of header, otherwise an invalid frame could be
4497 * emitted and the resulting htx message could be left in an inconsistent state.
4498 */
4499static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4500{
4501 struct http_hdr list[MAX_HTTP_HDR];
4502 struct h2c *h2c = h2s->h2c;
4503 struct htx_blk *blk;
4504 struct htx_blk *blk_end;
4505 struct buffer outbuf;
4506 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004507 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004508 enum htx_blk_type type;
4509 int es_now = 0;
4510 int ret = 0;
4511 int hdr;
4512 int idx;
4513
4514 if (h2c_mux_busy(h2c, h2s)) {
4515 h2s->flags |= H2_SF_BLK_MBUSY;
4516 return 0;
4517 }
4518
4519 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4520 h2c->flags |= H2_CF_MUX_MALLOC;
4521 h2s->flags |= H2_SF_BLK_MROOM;
4522 return 0;
4523 }
4524
4525 /* determine the first block which must not be deleted, blk_end may
4526 * be NULL if all blocks have to be deleted.
4527 */
4528 idx = htx_get_head(htx);
4529 blk_end = NULL;
4530 while (idx != -1) {
4531 type = htx_get_blk_type(htx_get_blk(htx, idx));
4532 idx = htx_get_next(htx, idx);
4533 if (type == HTX_BLK_EOH) {
4534 if (idx != -1)
4535 blk_end = htx_get_blk(htx, idx);
4536 break;
4537 }
4538 }
4539
4540 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004541 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004542 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004543 meth = htx_sl_req_meth(sl);
4544 path = htx_sl_req_uri(sl);
4545
4546 /* and the rest of the headers, that we dump starting at header 0 */
4547 hdr = 0;
4548
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004549 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004550 while ((idx = htx_get_next(htx, idx)) != -1) {
4551 blk = htx_get_blk(htx, idx);
4552 type = htx_get_blk_type(blk);
4553
4554 if (type == HTX_BLK_UNUSED)
4555 continue;
4556
4557 if (type != HTX_BLK_HDR)
4558 break;
4559
4560 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4561 goto fail;
4562
4563 list[hdr].n = htx_get_blk_name(htx, blk);
4564 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004565 hdr++;
4566 }
4567
4568 /* marker for end of headers */
4569 list[hdr].n = ist("");
4570
4571 chunk_reset(&outbuf);
4572
4573 while (1) {
4574 outbuf.area = b_tail(&h2c->mbuf);
4575 outbuf.size = b_contig_space(&h2c->mbuf);
4576 outbuf.data = 0;
4577
4578 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4579 break;
4580 realign_again:
4581 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4582 }
4583
4584 if (outbuf.size < 9)
4585 goto full;
4586
4587 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4588 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4589 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4590 outbuf.data = 9;
4591
4592 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004593 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004594 if (b_space_wraps(&h2c->mbuf))
4595 goto realign_again;
4596 goto full;
4597 }
4598
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004599 /* RFC7540 #8.3: the CONNECT method must have :
4600 * - :authority set to the URI part (host:port)
4601 * - :method set to CONNECT
4602 * - :scheme and :path omitted
4603 */
4604 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4605 /* encode the scheme which is always "https" (or 0x86 for "http") */
4606 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4607 /* output full */
4608 if (b_space_wraps(&h2c->mbuf))
4609 goto realign_again;
4610 goto full;
4611 }
Willy Tarreau80739692018-10-05 11:35:57 +02004612
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004613 /* encode the path, which necessarily is the second one */
4614 if (!hpack_encode_path(&outbuf, path)) {
4615 /* output full */
4616 if (b_space_wraps(&h2c->mbuf))
4617 goto realign_again;
4618 goto full;
4619 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004620
4621 /* look for the Host header and place it in :authority */
4622 auth = ist2(NULL, 0);
4623 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4624 if (isteq(list[hdr].n, ist("")))
4625 break; // end
4626
4627 if (isteq(list[hdr].n, ist("host"))) {
4628 auth = list[hdr].v;
4629 break;
4630 }
4631 }
4632 }
4633 else {
4634 /* for CONNECT, :authority is taken from the path */
4635 auth = path;
4636 }
4637
4638 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4639 /* output full */
4640 if (b_space_wraps(&h2c->mbuf))
4641 goto realign_again;
4642 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004643 }
4644
4645 /* encode all headers, stop at empty name */
4646 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4647 /* these ones do not exist in H2 and must be dropped. */
4648 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004649 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004650 isteq(list[hdr].n, ist("proxy-connection")) ||
4651 isteq(list[hdr].n, ist("keep-alive")) ||
4652 isteq(list[hdr].n, ist("upgrade")) ||
4653 isteq(list[hdr].n, ist("transfer-encoding")))
4654 continue;
4655
4656 if (isteq(list[hdr].n, ist("")))
4657 break; // end
4658
4659 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4660 /* output full */
4661 if (b_space_wraps(&h2c->mbuf))
4662 goto realign_again;
4663 goto full;
4664 }
4665 }
4666
4667 /* we may need to add END_STREAM if we have no body :
4668 * - request already closed, or :
4669 * - no transfer-encoding, and :
4670 * - no content-length or content-length:0
4671 * Fixme: this doesn't take into account CONNECT requests.
4672 */
4673 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4674 es_now = 1;
4675
4676 if (sl->flags & HTX_SL_F_BODYLESS)
4677 es_now = 1;
4678
Willy Tarreau927b88b2019-03-04 08:03:25 +01004679 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004680 es_now = 1;
4681
4682 /* update the frame's size */
4683 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4684
4685 if (es_now)
4686 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4687
4688 /* commit the H2 response */
4689 b_add(&h2c->mbuf, outbuf.data);
4690 h2s->flags |= H2_SF_HEADERS_SENT;
4691 h2s->st = H2_SS_OPEN;
4692
Willy Tarreau80739692018-10-05 11:35:57 +02004693 if (es_now) {
4694 // trim any possibly pending data (eg: inconsistent content-length)
4695 h2s->flags |= H2_SF_ES_SENT;
4696 h2s->st = H2_SS_HLOC;
4697 }
4698
4699 /* remove all header blocks including the EOH and compute the
4700 * corresponding size.
4701 *
4702 * FIXME: We should remove everything when es_now is set.
4703 */
4704 ret = 0;
4705 idx = htx_get_head(htx);
4706 blk = htx_get_blk(htx, idx);
4707 while (blk != blk_end) {
4708 ret += htx_get_blksz(blk);
4709 blk = htx_remove_blk(htx, blk);
4710 }
4711
4712 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4713 htx_remove_blk(htx, blk_end);
4714
4715 end:
4716 return ret;
4717 full:
4718 h2c->flags |= H2_CF_MUX_MFULL;
4719 h2s->flags |= H2_SF_BLK_MROOM;
4720 ret = 0;
4721 goto end;
4722 fail:
4723 /* unparsable HTX messages, too large ones to be produced in the local
4724 * list etc go here (unrecoverable errors).
4725 */
4726 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4727 ret = 0;
4728 goto end;
4729}
4730
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004731/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004732 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4733 * caller must check the stream's status to detect any error which might have
4734 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004735 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4736 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004737static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004738{
4739 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004740 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004741 struct buffer outbuf;
4742 size_t total = 0;
4743 int es_now = 0;
4744 int bsize; /* htx block size */
4745 int fsize; /* h2 frame size */
4746 struct htx_blk *blk;
4747 enum htx_blk_type type;
4748 int idx;
4749
4750 if (h2c_mux_busy(h2c, h2s)) {
4751 h2s->flags |= H2_SF_BLK_MBUSY;
4752 goto end;
4753 }
4754
4755 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4756 h2c->flags |= H2_CF_MUX_MALLOC;
4757 h2s->flags |= H2_SF_BLK_MROOM;
4758 goto end;
4759 }
4760
Willy Tarreau98de12a2018-12-12 07:03:00 +01004761 htx = htx_from_buf(buf);
4762
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004763 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4764 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4765 * the caller to handle.
4766 */
4767
4768 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004769 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004770 goto end;
4771
4772 idx = htx_get_head(htx);
4773 blk = htx_get_blk(htx, idx);
4774 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4775 bsize = htx_get_blksz(blk);
4776 fsize = bsize;
4777
4778 if (type == HTX_BLK_EOD) {
4779 /* if we have an EOD, we're dealing with chunked data. We may
4780 * have a set of trailers after us that the caller will want to
4781 * deal with. Let's simply remove the EOD and return.
4782 */
4783 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004784 total++; // EOD counts as one byte
4785 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004786 goto end;
4787 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004788 else if (type == HTX_BLK_EOM) {
4789 if (h2s->flags & H2_SF_ES_SENT) {
4790 /* ES already sent */
4791 htx_remove_blk(htx, blk);
4792 total++; // EOM counts as one byte
4793 count--;
4794 goto end;
4795 }
4796 }
4797 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004798 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004799
4800 /* Perform some optimizations to reduce the number of buffer copies.
4801 * First, if the mux's buffer is empty and the htx area contains
4802 * exactly one data block of the same size as the requested count, and
4803 * this count fits within the frame size, the stream's window size, and
4804 * the connection's window size, then it's possible to simply swap the
4805 * caller's buffer with the mux's output buffer and adjust offsets and
4806 * length to match the entire DATA HTX block in the middle. In this
4807 * case we perform a true zero-copy operation from end-to-end. This is
4808 * the situation that happens all the time with large files. Second, if
4809 * this is not possible, but the mux's output buffer is empty, we still
4810 * have an opportunity to avoid the copy to the intermediary buffer, by
4811 * making the intermediary buffer's area point to the output buffer's
4812 * area. In this case we want to skip the HTX header to make sure that
4813 * copies remain aligned and that this operation remains possible all
4814 * the time. This goes for headers, data blocks and any data extracted
4815 * from the HTX blocks.
4816 */
4817 if (unlikely(fsize == count &&
4818 htx->used == 1 && type == HTX_BLK_DATA &&
4819 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4820 void *old_area = h2c->mbuf.area;
4821
4822 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004823 /* Too bad there are data left there. We're willing to memcpy/memmove
4824 * up to 1/4 of the buffer, which means that it's OK to copy a large
4825 * frame into a buffer containing few data if it needs to be realigned,
4826 * and that it's also OK to copy few data without realigning. Otherwise
4827 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004828 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004829 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4830 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4831 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004832 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004833
Willy Tarreau98de12a2018-12-12 07:03:00 +01004834 h2c->flags |= H2_CF_MUX_MFULL;
4835 h2s->flags |= H2_SF_BLK_MROOM;
4836 goto end;
4837 }
4838
4839 /* map an H2 frame to the HTX block so that we can put the
4840 * frame header there.
4841 */
4842 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004843 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004844 h2c->mbuf.data = fsize + 9;
4845 outbuf.area = b_head(&h2c->mbuf);
4846
4847 /* prepend an H2 DATA frame header just before the DATA block */
4848 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4849 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4850 h2_set_frame_size(outbuf.area, fsize);
4851
4852 /* update windows */
4853 h2s->mws -= fsize;
4854 h2c->mws -= fsize;
4855
4856 /* and exchange with our old area */
4857 buf->area = old_area;
4858 buf->data = buf->head = 0;
4859 total += fsize;
4860 goto end;
4861 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004862
Willy Tarreau98de12a2018-12-12 07:03:00 +01004863 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004864 /* for DATA and EOM we'll have to emit a frame, even if empty */
4865
4866 while (1) {
4867 outbuf.area = b_tail(&h2c->mbuf);
4868 outbuf.size = b_contig_space(&h2c->mbuf);
4869 outbuf.data = 0;
4870
4871 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4872 break;
4873 realign_again:
4874 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4875 }
4876
4877 if (outbuf.size < 9) {
4878 h2c->flags |= H2_CF_MUX_MFULL;
4879 h2s->flags |= H2_SF_BLK_MROOM;
4880 goto end;
4881 }
4882
4883 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4884 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4885 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4886 outbuf.data = 9;
4887
4888 /* we have in <fsize> the exact number of bytes we need to copy from
4889 * the HTX buffer. We need to check this against the connection's and
4890 * the stream's send windows, and to ensure that this fits in the max
4891 * frame size and in the buffer's available space minus 9 bytes (for
4892 * the frame header). The connection's flow control is applied last so
4893 * that we can use a separate list of streams which are immediately
4894 * unblocked on window opening. Note: we don't implement padding.
4895 */
4896
4897 /* EOM is presented with bsize==1 but would lead to the emission of an
4898 * empty frame, thus we force it to zero here.
4899 */
4900 if (type == HTX_BLK_EOM)
4901 bsize = fsize = 0;
4902
4903 if (!fsize)
4904 goto send_empty;
4905
4906 if (h2s->mws <= 0) {
4907 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004908 if (!LIST_ISEMPTY(&h2s->list))
4909 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004910 goto end;
4911 }
4912
Willy Tarreauee573762018-12-04 15:25:57 +01004913 if (fsize > count)
4914 fsize = count;
4915
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004916 if (fsize > h2s->mws)
4917 fsize = h2s->mws; // >0
4918
4919 if (h2c->mfs && fsize > h2c->mfs)
4920 fsize = h2c->mfs; // >0
4921
4922 if (fsize + 9 > outbuf.size) {
4923 /* we have an opportunity for enlarging the too small
4924 * available space, let's try.
4925 * FIXME: is this really interesting to do? Maybe we'll
4926 * spend lots of time realigning instead of using two
4927 * frames.
4928 */
4929 if (b_space_wraps(&h2c->mbuf))
4930 goto realign_again;
4931 fsize = outbuf.size - 9;
4932
4933 if (fsize <= 0) {
4934 /* no need to send an empty frame here */
4935 h2c->flags |= H2_CF_MUX_MFULL;
4936 h2s->flags |= H2_SF_BLK_MROOM;
4937 goto end;
4938 }
4939 }
4940
4941 if (h2c->mws <= 0) {
4942 h2s->flags |= H2_SF_BLK_MFCTL;
4943 goto end;
4944 }
4945
4946 if (fsize > h2c->mws)
4947 fsize = h2c->mws;
4948
4949 /* now let's copy this this into the output buffer */
4950 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004951 h2s->mws -= fsize;
4952 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004953 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004954
4955 send_empty:
4956 /* update the frame's size */
4957 h2_set_frame_size(outbuf.area, fsize);
4958
4959 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4960 * meeting EOM. We should optimize this later.
4961 */
4962 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004963 total++; // EOM counts as one byte
4964 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004965 es_now = 1;
4966 }
4967
4968 if (es_now)
4969 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4970
4971 /* commit the H2 response */
4972 b_add(&h2c->mbuf, fsize + 9);
4973
4974 /* consume incoming HTX block, including EOM */
4975 total += fsize;
4976 if (fsize == bsize) {
4977 htx_remove_blk(htx, blk);
4978 if (fsize)
4979 goto new_frame;
4980 } else {
4981 /* we've truncated this block */
4982 htx_cut_data_blk(htx, blk, fsize);
4983 }
4984
4985 if (es_now) {
4986 if (h2s->st == H2_SS_OPEN)
4987 h2s->st = H2_SS_HLOC;
4988 else
4989 h2s_close(h2s);
4990
4991 h2s->flags |= H2_SF_ES_SENT;
4992 }
4993
4994 end:
4995 return total;
4996}
4997
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004998/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4999 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5000 * processed. The caller must check the stream's status to detect any error
5001 * which might have happened subsequently to a successful send. The htx blocks
5002 * are automatically removed from the message. The htx message is assumed to be
5003 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005004 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005005 * as a single frame. The ES flag is always set.
5006 */
5007static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5008{
5009 struct http_hdr list[MAX_HTTP_HDR];
5010 struct h2c *h2c = h2s->h2c;
5011 struct htx_blk *blk;
5012 struct htx_blk *blk_end;
5013 struct buffer outbuf;
5014 struct h1m h1m;
5015 enum htx_blk_type type;
5016 uint32_t size;
5017 int ret = 0;
5018 int hdr;
5019 int idx;
5020 void *start;
5021
5022 if (h2c_mux_busy(h2c, h2s)) {
5023 h2s->flags |= H2_SF_BLK_MBUSY;
5024 goto end;
5025 }
5026
5027 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5028 h2c->flags |= H2_CF_MUX_MALLOC;
5029 h2s->flags |= H2_SF_BLK_MROOM;
5030 goto end;
5031 }
5032
5033 /* The principle is that we parse each and every trailers block using
5034 * the H1 headers parser, and append it to the list. We don't proceed
5035 * until EOM is met. blk_end will point to the EOM block.
5036 */
5037 hdr = 0;
5038 memset(list, 0, sizeof(list));
5039 blk_end = NULL;
5040
5041 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5042 blk = htx_get_blk(htx, idx);
5043 type = htx_get_blk_type(blk);
5044
5045 if (type == HTX_BLK_UNUSED)
5046 continue;
5047
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005048 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005049 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005050
5051 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5052 goto fail;
5053
5054 size = htx_get_blksz(blk);
5055 start = htx_get_blk_ptr(htx, blk);
5056
5057 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5058 h1m.err_pos = 0;
5059 ret = h1_headers_to_hdr_list(start, start + size,
5060 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5061 &h1m, NULL);
5062 if (ret < 0)
5063 goto fail;
5064
5065 /* ret == 0 if an incomplete trailers block was found (missing
5066 * empty line), or > 0 if it was found. We have to continue on
5067 * incomplete messages because the trailers block might be
5068 * incomplete.
5069 */
5070
5071 /* search the new end */
5072 while (hdr <= sizeof(list)/sizeof(list[0])) {
5073 if (!list[hdr].n.len)
5074 break;
5075 hdr++;
5076 }
5077 }
5078
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005079 if (list[hdr].n.len != 0)
5080 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005081
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005082 chunk_reset(&outbuf);
5083
5084 while (1) {
5085 outbuf.area = b_tail(&h2c->mbuf);
5086 outbuf.size = b_contig_space(&h2c->mbuf);
5087 outbuf.data = 0;
5088
5089 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5090 break;
5091 realign_again:
5092 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5093 }
5094
5095 if (outbuf.size < 9)
5096 goto full;
5097
5098 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5099 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5100 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5101 outbuf.data = 9;
5102
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005103 /* encode all headers */
5104 for (idx = 0; idx < hdr; idx++) {
5105 /* these ones do not exist in H2 or must not appear in
5106 * trailers and must be dropped.
5107 */
5108 if (isteq(list[idx].n, ist("host")) ||
5109 isteq(list[idx].n, ist("content-length")) ||
5110 isteq(list[idx].n, ist("connection")) ||
5111 isteq(list[idx].n, ist("proxy-connection")) ||
5112 isteq(list[idx].n, ist("keep-alive")) ||
5113 isteq(list[idx].n, ist("upgrade")) ||
5114 isteq(list[idx].n, ist("te")) ||
5115 isteq(list[idx].n, ist("transfer-encoding")))
5116 continue;
5117
5118 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5119 /* output full */
5120 if (b_space_wraps(&h2c->mbuf))
5121 goto realign_again;
5122 goto full;
5123 }
5124 }
5125
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005126 if (outbuf.data == 9) {
5127 /* here we have a problem, we have nothing to emit (either we
5128 * received an empty trailers block followed or we removed its
5129 * contents above). Because of this we can't send a HEADERS
5130 * frame, so we have to cheat and instead send an empty DATA
5131 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005132 */
5133 outbuf.area[3] = H2_FT_DATA;
5134 outbuf.area[4] = H2_F_DATA_END_STREAM;
5135 }
5136
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005137 /* update the frame's size */
5138 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5139
5140 /* commit the H2 response */
5141 b_add(&h2c->mbuf, outbuf.data);
5142 h2s->flags |= H2_SF_ES_SENT;
5143
5144 if (h2s->st == H2_SS_OPEN)
5145 h2s->st = H2_SS_HLOC;
5146 else
5147 h2s_close(h2s);
5148
5149 /* OK we could properly deliver the response */
5150 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005151 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005152 ret = 0;
5153 idx = htx_get_head(htx);
5154 blk = htx_get_blk(htx, idx);
5155 while (blk != blk_end) {
5156 ret += htx_get_blksz(blk);
5157 blk = htx_remove_blk(htx, blk);
5158 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005159 end:
5160 return ret;
5161 full:
5162 h2c->flags |= H2_CF_MUX_MFULL;
5163 h2s->flags |= H2_SF_BLK_MROOM;
5164 ret = 0;
5165 goto end;
5166 fail:
5167 /* unparsable HTX messages, too large ones to be produced in the local
5168 * list etc go here (unrecoverable errors).
5169 */
5170 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5171 ret = 0;
5172 goto end;
5173}
5174
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005175/* Called from the upper layer, to subscribe to events, such as being able to send.
5176 * The <param> argument here is supposed to be a pointer to a wait_event struct
5177 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5178 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5179 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5180 * returns 0 except for the error above.
5181 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005182static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5183{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005184 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005185 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005186 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005187
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005188 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005189 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005190 if (!(sw->events & SUB_RETRY_RECV)) {
5191 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005192 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005193 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005194 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005195 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005196 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005197 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005198 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005199 if (!(sw->events & SUB_RETRY_SEND)) {
5200 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005201 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005202 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005203 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5204 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005205 if (h2s->flags & H2_SF_BLK_MFCTL)
5206 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5207 else
5208 LIST_ADDQ(&h2c->send_list, &h2s->list);
5209 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005210 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005211 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005212 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005213 if (event_type != 0)
5214 return -1;
5215 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005216}
5217
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005218/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5219 * The <param> argument here is supposed to be a pointer to the same wait_event
5220 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5221 * It always returns zero.
5222 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005223static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5224{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005225 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005226 struct h2s *h2s = cs->ctx;
5227
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005228 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005229 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005230 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005231 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005232 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005233 }
5234 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005235 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005236 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005237 if (h2s->send_wait == sw) {
5238 LIST_DEL(&h2s->list);
5239 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005240 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005241 /* We were about to send, make sure it does not happen */
5242 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5243 h2s->send_wait != &h2s->wait_event) {
5244 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5245 LIST_DEL_INIT(&h2s->sending_list);
5246 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005247 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005248
Olivier Houchardd846c262018-10-19 17:24:29 +02005249 }
5250 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005251 return 0;
5252}
5253
5254
Olivier Houchard511efea2018-08-16 15:30:32 +02005255/* Called from the upper layer, to receive data */
5256static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5257{
Olivier Houchard638b7992018-08-16 15:41:52 +02005258 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005259 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005260 struct htx *h2s_htx = NULL;
5261 struct htx *buf_htx = NULL;
5262 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005263 size_t ret = 0;
5264
5265 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005266 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5267 /* in HTX mode we ignore the count argument */
5268 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005269 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005270 /* Here htx_to_buf() will set buffer data to 0 because
5271 * the HTX is empty.
5272 */
5273 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005274 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005275 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005276
5277 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005278 count = htx_free_data_space(buf_htx);
5279 if (flags & CO_RFL_KEEP_RSV) {
5280 if (count <= global.tune.maxrewrite)
5281 goto end;
5282 count -= global.tune.maxrewrite;
5283 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005284
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005285 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005286
5287 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5288 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5289
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005290 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005291 htx_to_buf(buf_htx, buf);
5292 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005293 ret = htx_ret.ret;
5294 }
5295 else {
5296 ret = b_xfer(buf, &h2s->rxbuf, count);
5297 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005298
Christopher Faulet37070b22019-02-14 15:12:14 +01005299 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005300 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005301 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005302 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005303 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005304 if (cs->flags & CS_FL_REOS)
5305 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005306 if (cs->flags & CS_FL_ERR_PENDING)
5307 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005308 if (b_size(&h2s->rxbuf)) {
5309 b_free(&h2s->rxbuf);
5310 offer_buffers(NULL, tasks_run_queue);
5311 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005312 }
5313
Willy Tarreau082f5592018-11-25 08:03:32 +01005314 if (ret && h2c->dsi == h2s->id) {
5315 /* demux is blocking on this stream's buffer */
5316 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005317 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005318 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005319
Olivier Houchard511efea2018-08-16 15:30:32 +02005320 return ret;
5321}
5322
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005323/* stops all senders of this connection for example when the mux buffer is full.
5324 * They are moved from the sending_list to either fctl_list or send_list.
5325 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005326static void h2_stop_senders(struct h2c *h2c)
5327{
5328 struct h2s *h2s, *h2s_back;
5329
Olivier Houchardd360ac62019-03-22 17:37:16 +01005330 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5331 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005332 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005333 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005334 }
5335}
5336
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005337/* Called from the upper layer, to send data from buffer <buf> for no more than
5338 * <count> bytes. Returns the number of bytes effectively sent. Some status
5339 * flags may be updated on the conn_stream.
5340 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005341static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005342{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005343 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005344 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005345 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005346 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005347 struct htx *htx;
5348 struct htx_blk *blk;
5349 enum htx_blk_type btype;
5350 uint32_t bsize;
5351 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005352
Olivier Houchardd360ac62019-03-22 17:37:16 +01005353 /* If we were not just woken because we wanted to send but couldn't,
5354 * and there's somebody else that is waiting to send, do nothing,
5355 * we will subscribe later and be put at the end of the list
5356 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005357 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005358 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5359 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005360 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005361
Olivier Houchard998410a2019-04-15 19:23:37 +02005362 /* We couldn't set it to NULL before, because we needed it in case
5363 * we had to cancel the tasklet
5364 */
5365 h2s->send_wait = NULL;
5366
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005367 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5368 return 0;
5369
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005370 /* htx will be enough to decide if we're using HTX or legacy */
5371 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5372
Willy Tarreau0bad0432018-06-14 16:54:01 +02005373 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005374 h2s->flags |= H2_SF_OUTGOING_DATA;
5375
Willy Tarreau751f2d02018-10-05 09:35:00 +02005376 if (h2s->id == 0) {
5377 int32_t id = h2c_get_next_sid(h2s->h2c);
5378
5379 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005380 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005381 return 0;
5382 }
5383
5384 eb32_delete(&h2s->by_id);
5385 h2s->by_id.key = h2s->id = id;
5386 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005387 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005388 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5389 }
5390
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005391 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005392 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005393 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005394 idx = htx_get_head(htx);
5395 blk = htx_get_blk(htx, idx);
5396 btype = htx_get_blk_type(blk);
5397 bsize = htx_get_blksz(blk);
5398
5399 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005400 case HTX_BLK_REQ_SL:
5401 /* start-line before headers */
5402 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5403 if (ret > 0) {
5404 total += ret;
5405 count -= ret;
5406 if (ret < bsize)
5407 goto done;
5408 }
5409 break;
5410
Willy Tarreau115e83b2018-12-01 19:17:53 +01005411 case HTX_BLK_RES_SL:
5412 /* start-line before headers */
5413 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5414 if (ret > 0) {
5415 total += ret;
5416 count -= ret;
5417 if (ret < bsize)
5418 goto done;
5419 }
5420 break;
5421
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005422 case HTX_BLK_DATA:
5423 case HTX_BLK_EOD:
5424 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005425 /* all these cause the emission of a DATA frame (possibly empty).
5426 * This EOM necessarily is one before trailers, as the EOM following
5427 * trailers would have been consumed by the trailers parser.
5428 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005429 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005430 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005431 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005432 total += ret;
5433 count -= ret;
5434 if (ret < bsize)
5435 goto done;
5436 }
5437 break;
5438
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005439 case HTX_BLK_TLR:
5440 /* This is the first trailers block, all the subsequent ones AND
5441 * the EOM will be swallowed by the parser.
5442 */
5443 ret = h2s_htx_make_trailers(h2s, htx);
5444 if (ret > 0) {
5445 total += ret;
5446 count -= ret;
5447 if (ret < bsize)
5448 goto done;
5449 }
5450 break;
5451
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005452 default:
5453 htx_remove_blk(htx, blk);
5454 total += bsize;
5455 count -= bsize;
5456 break;
5457 }
5458 }
5459 goto done;
5460 }
5461
5462 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005463 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005464 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005465 if (h2s->h2c->flags & H2_CF_IS_BACK)
5466 ret = -1;
5467 else
5468 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005469 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005470 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005471 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005472 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005473 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005474 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005475 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005476
Willy Tarreau5dd17352018-06-14 13:33:30 +02005477 if (unlikely((int)ret <= 0)) {
5478 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005479 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5480 break;
5481 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005482 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005483 total += count;
5484 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005485 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005486 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005487 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005488 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005489 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005490 break;
5491 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005492
5493 total += ret;
5494 count -= ret;
5495
Willy Tarreau2b778482019-05-06 15:00:22 +02005496 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005497 break;
5498
5499 if (h2s->flags & H2_SF_BLK_ANY)
5500 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005501 }
5502
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005503 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005504 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005505 /* trim any possibly pending data after we close (extra CR-LF,
5506 * unprocessed trailers, abnormal extra data, ...)
5507 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005508 total += count;
5509 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005510 }
5511
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005512 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005513 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005514 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005515 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005516 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005517 }
5518
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005519 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005520 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005521 } else {
5522 b_del(buf, total);
5523 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005524
5525 /* The mux is full, cancel the pending tasks */
5526 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5527 (h2s->flags & H2_SF_BLK_MBUSY))
5528 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005529
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005530 /* If we're running HTX, and we read the whole buffer, then pretend
5531 * we read exactly what the caller specified, as with HTX the caller
5532 * will always give the buffer size, instead of the amount of data
5533 * available.
5534 */
5535 if (htx && !b_data(buf))
5536 total = orig_count;
5537
Olivier Houchard7505f942018-08-21 18:10:44 +02005538 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005539 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005540 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005541
Olivier Houchard7505f942018-08-21 18:10:44 +02005542 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005543 /* If we're waiting for flow control, and we got a shutr on the
5544 * connection, we will never be unlocked, so add an error on
5545 * the conn_stream.
5546 */
5547 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5548 !b_data(&h2s->h2c->dbuf) &&
5549 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5550 if (cs->flags & CS_FL_EOS)
5551 cs->flags |= CS_FL_ERROR;
5552 else
5553 cs->flags |= CS_FL_ERR_PENDING;
5554 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005555 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005556 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005557 LIST_DEL_INIT(&h2s->list);
5558 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005559 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005560}
5561
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005562/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005563static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005564{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005565 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005566 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005567 struct eb32_node *node;
5568 int fctl_cnt = 0;
5569 int send_cnt = 0;
5570 int tree_cnt = 0;
5571 int orph_cnt = 0;
5572
5573 if (!h2c)
5574 return;
5575
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005576 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005577 fctl_cnt++;
5578
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005579 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005580 send_cnt++;
5581
Willy Tarreau3af37712018-12-18 14:34:41 +01005582 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005583 node = eb32_first(&h2c->streams_by_id);
5584 while (node) {
5585 h2s = container_of(node, struct h2s, by_id);
5586 tree_cnt++;
5587 if (!h2s->cs)
5588 orph_cnt++;
5589 node = eb32_next(node);
5590 }
5591
Willy Tarreau987c0632018-12-18 10:32:05 +01005592 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5593 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5594 " .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 +02005595 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5596 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005597 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005598 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5599 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5600 h2c->msi,
5601 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5602 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5603
5604 if (h2s) {
5605 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5606 h2s, h2s->id, h2s->flags,
5607 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5608 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5609 h2s->cs);
5610 if (h2s->cs)
5611 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5612 h2s->cs->flags, h2s->cs->data);
5613 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005614}
Willy Tarreau62f52692017-10-08 23:01:42 +02005615
5616/*******************************************************/
5617/* functions below are dedicated to the config parsers */
5618/*******************************************************/
5619
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005620/* config parser for global "tune.h2.header-table-size" */
5621static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5622 struct proxy *defpx, const char *file, int line,
5623 char **err)
5624{
5625 if (too_many_args(1, args, err, NULL))
5626 return -1;
5627
5628 h2_settings_header_table_size = atoi(args[1]);
5629 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5630 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5631 return -1;
5632 }
5633 return 0;
5634}
Willy Tarreau62f52692017-10-08 23:01:42 +02005635
Willy Tarreaue6baec02017-07-27 11:45:11 +02005636/* config parser for global "tune.h2.initial-window-size" */
5637static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5638 struct proxy *defpx, const char *file, int line,
5639 char **err)
5640{
5641 if (too_many_args(1, args, err, NULL))
5642 return -1;
5643
5644 h2_settings_initial_window_size = atoi(args[1]);
5645 if (h2_settings_initial_window_size < 0) {
5646 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5647 return -1;
5648 }
5649 return 0;
5650}
5651
Willy Tarreau5242ef82017-07-27 11:47:28 +02005652/* config parser for global "tune.h2.max-concurrent-streams" */
5653static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5654 struct proxy *defpx, const char *file, int line,
5655 char **err)
5656{
5657 if (too_many_args(1, args, err, NULL))
5658 return -1;
5659
5660 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005661 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005662 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5663 return -1;
5664 }
5665 return 0;
5666}
5667
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005668/* config parser for global "tune.h2.max-frame-size" */
5669static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5670 struct proxy *defpx, const char *file, int line,
5671 char **err)
5672{
5673 if (too_many_args(1, args, err, NULL))
5674 return -1;
5675
5676 h2_settings_max_frame_size = atoi(args[1]);
5677 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5678 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5679 return -1;
5680 }
5681 return 0;
5682}
5683
Willy Tarreau62f52692017-10-08 23:01:42 +02005684
5685/****************************************/
5686/* MUX initialization and instanciation */
5687/***************************************/
5688
5689/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005690static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005691 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005692 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005693 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005694 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005695 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005696 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005697 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005698 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005699 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005700 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005701 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005702 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005703 .shutr = h2_shutr,
5704 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005705 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005706 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005707 .name = "H2",
5708};
5709
Christopher Faulet32f61c02018-04-10 14:33:41 +02005710/* PROTO selection : this mux registers PROTO token "h2" */
5711static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005712 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005713
Willy Tarreau0108d902018-11-25 19:14:37 +01005714INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5715
Christopher Faulet9b579102019-04-11 22:52:25 +02005716
5717/* The mux operations */
5718static const struct mux_ops h2_htx_ops = {
5719 .init = h2_init,
5720 .wake = h2_wake,
5721 .snd_buf = h2_snd_buf,
5722 .rcv_buf = h2_rcv_buf,
5723 .subscribe = h2_subscribe,
5724 .unsubscribe = h2_unsubscribe,
5725 .attach = h2_attach,
5726 .get_first_cs = h2_get_first_cs,
5727 .detach = h2_detach,
5728 .destroy = h2_destroy,
5729 .avail_streams = h2_avail_streams,
5730 .used_streams = h2_used_streams,
5731 .shutr = h2_shutr,
5732 .shutw = h2_shutw,
5733 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005734 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005735 .name = "H2",
5736};
5737
Willy Tarreauf8957272018-10-03 10:25:20 +02005738static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005739 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005740
5741INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5742
Willy Tarreau62f52692017-10-08 23:01:42 +02005743/* config keyword parsers */
5744static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005745 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005746 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005747 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005748 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005749 { 0, NULL, NULL }
5750}};
5751
Willy Tarreau0108d902018-11-25 19:14:37 +01005752INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);