blob: 27d58e752033d00ab586d101ab3f82f8094cc6fe [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 Tarreau2c249eb2019-05-13 18:06:17 +0200181#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
182#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
183
184
Willy Tarreau18312642017-10-11 07:57:07 +0200185/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
186 * it is being processed in the internal HTTP representation (H1 for now).
187 */
188struct h2s {
189 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100190 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200191 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200192 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200193 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200194 int32_t id; /* stream ID */
195 uint32_t flags; /* H2_SF_* */
196 int mws; /* mux window size for this stream */
197 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
198 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200199 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100200 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200201 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200202 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 +0100203 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
204 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 +0200205 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100206 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200207};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200208
Willy Tarreauc6405142017-09-21 20:23:50 +0200209/* descriptor for an h2 frame header */
210struct h2_fh {
211 uint32_t len; /* length, host order, 24 bits */
212 uint32_t sid; /* stream id, host order, 31 bits */
213 uint8_t ft; /* frame type */
214 uint8_t ff; /* frame flags */
215};
216
Willy Tarreau8ceae722018-11-26 11:58:30 +0100217/* the h2c connection pool */
218DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
219
220/* the h2s stream pool */
221DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
222
Willy Tarreaudc572362018-12-12 08:08:05 +0100223/* The default connection window size is 65535, it may only be enlarged using
224 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
225 * we'll pretend we already received the difference between the two to send
226 * an equivalent window update to enlarge it to 2G-1.
227 */
228#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
229
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200230/* a few settings from the global section */
231static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200232static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100233static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100234static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200235
Willy Tarreau2a856182017-05-16 15:20:39 +0200236/* a dmumy closed stream */
237static const struct h2s *h2_closed_stream = &(const struct h2s){
238 .cs = NULL,
239 .h2c = NULL,
240 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100241 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100242 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200243 .id = 0,
244};
245
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100246/* a dmumy closed stream returning a PROTOCOL_ERROR error */
247static const struct h2s *h2_error_stream = &(const struct h2s){
248 .cs = NULL,
249 .h2c = NULL,
250 .st = H2_SS_CLOSED,
251 .errcode = H2_ERR_PROTOCOL_ERROR,
252 .flags = 0,
253 .id = 0,
254};
255
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100256/* a dmumy closed stream returning a REFUSED_STREAM error */
257static const struct h2s *h2_refused_stream = &(const struct h2s){
258 .cs = NULL,
259 .h2c = NULL,
260 .st = H2_SS_CLOSED,
261 .errcode = H2_ERR_REFUSED_STREAM,
262 .flags = 0,
263 .id = 0,
264};
265
Willy Tarreau2a856182017-05-16 15:20:39 +0200266/* and a dummy idle stream for use with any unannounced stream */
267static const struct h2s *h2_idle_stream = &(const struct h2s){
268 .cs = NULL,
269 .h2c = NULL,
270 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100271 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200272 .id = 0,
273};
274
Olivier Houchard9f6af332018-05-25 14:04:04 +0200275static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200276static int h2_send(struct h2c *h2c);
277static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200278static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200279static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100280static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100281static 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 +0100282static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200283static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100284static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100285static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200286
Olivier Houchard7a977432019-03-21 15:47:13 +0100287static __inline int
288h2c_is_dead(struct h2c *h2c)
289{
290 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
291 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
292 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
293 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
294 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
295 (conn_xprt_read0_pending(h2c->conn) ||
296 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
297 return 1;
298
299 return 0;
300
301}
302
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200303/*****************************************************/
304/* functions below are for dynamic buffer management */
305/*****************************************************/
306
Willy Tarreau315d8072017-12-10 22:17:57 +0100307/* indicates whether or not the we may call the h2_recv() function to attempt
308 * to receive data into the buffer and/or demux pending data. The condition is
309 * a bit complex due to some API limits for now. The rules are the following :
310 * - if an error or a shutdown was detected on the connection and the buffer
311 * is empty, we must not attempt to receive
312 * - if the demux buf failed to be allocated, we must not try to receive and
313 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100314 * - if no flag indicates a blocking condition, we may attempt to receive,
315 * regardless of whether the demux buffer is full or not, so that only
316 * de demux part decides whether or not to block. This is needed because
317 * the connection API indeed prevents us from re-enabling receipt that is
318 * already enabled in a polled state, so we must always immediately stop
319 * as soon as the demux can't proceed so as never to hit an end of read
320 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100321 * - otherwise must may not attempt
322 */
323static inline int h2_recv_allowed(const struct h2c *h2c)
324{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200325 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100326 (h2c->st0 >= H2_CS_ERROR ||
327 h2c->conn->flags & CO_FL_ERROR ||
328 conn_xprt_read0_pending(h2c->conn)))
329 return 0;
330
331 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100332 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100333 return 1;
334
335 return 0;
336}
337
Willy Tarreau47b515a2018-12-21 16:09:41 +0100338/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100340{
341 if (!h2_recv_allowed(h2c))
342 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200343 if ((!consider_buffer || !b_data(&h2c->dbuf))
344 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100345 return;
346 tasklet_wakeup(h2c->wait_event.task);
347}
348
349
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100350/* returns true if the front connection has too many conn_streams attached */
351static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200352{
Willy Tarreaua8754662018-12-23 20:43:58 +0100353 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200354}
355
Willy Tarreau44e973f2018-03-01 17:49:30 +0100356/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
357 * flags are used to figure what buffer was requested. It returns 1 if the
358 * allocation succeeds, in which case the connection is woken up, or 0 if it's
359 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200362{
363 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100364 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365
Willy Tarreau44e973f2018-03-01 17:49:30 +0100366 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200367 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200368 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200369 return 1;
370 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200371
Willy Tarreau44e973f2018-03-01 17:49:30 +0100372 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
373 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374
375 if (h2c->flags & H2_CF_DEM_MROOM) {
376 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200377 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200378 }
Willy Tarreau14398122017-09-22 14:26:04 +0200379 return 1;
380 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100381
382 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
383 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200384 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100385 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200386 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100387 return 1;
388 }
389
Willy Tarreau14398122017-09-22 14:26:04 +0200390 return 0;
391}
392
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200393static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200394{
395 struct buffer *buf = NULL;
396
Willy Tarreauc234ae32019-05-13 17:56:11 +0200397 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
399 h2c->buf_wait.target = h2c;
400 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100401 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100402 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100403 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200404 __conn_xprt_stop_recv(h2c->conn);
405 }
406 return buf;
407}
408
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200409static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200410{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200411 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100412 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200413 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200414 }
415}
416
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100417/* returns the number of allocatable outgoing streams for the connection taking
418 * the last_sid and the reserved ones into account.
419 */
420static inline int h2_streams_left(const struct h2c *h2c)
421{
422 int ret;
423
424 /* consider the number of outgoing streams we're allowed to create before
425 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
426 * nb_reserved is the number of streams which don't yet have an ID.
427 */
428 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
429 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
430 if (ret < 0)
431 ret = 0;
432 return ret;
433}
434
Willy Tarreau00f18a32019-01-26 12:19:01 +0100435/* returns the number of streams in use on a connection to figure if it's
436 * idle or not. We check nb_cs and not nb_streams as the caller will want
437 * to know if it was the last one after a detach().
438 */
439static int h2_used_streams(struct connection *conn)
440{
441 struct h2c *h2c = conn->ctx;
442
443 return h2c->nb_cs;
444}
445
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100446/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100447static int h2_avail_streams(struct connection *conn)
448{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100449 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100450 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100451 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100452
Willy Tarreau6afec462019-01-28 06:40:19 +0100453 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
454 * streams on the connection.
455 */
456 if (h2c->last_sid >= 0)
457 return 0;
458
Willy Tarreau86949782019-01-31 10:42:05 +0100459 /* note: may be negative if a SETTINGS frame changes the limit */
460 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100461
462 /* we must also consider the limit imposed by stream IDs */
463 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100464 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100465 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100466 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
467 ret1 = MIN(ret1, ret2);
468 }
469 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100470}
471
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200472
Willy Tarreau62f52692017-10-08 23:01:42 +0200473/*****************************************************************/
474/* functions below are dedicated to the mux setup and management */
475/*****************************************************************/
476
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200477/* Initialize the mux once it's attached. For outgoing connections, the context
478 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479 * connections from the fact that the context is still NULL (even during mux
480 * upgrades). <input> is always used as Input buffer and may contain data. It is
481 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200482 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200483static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
484 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200485{
486 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100487 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Willy Tarreaubafbe012017-11-24 17:34:44 +0100489 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200490 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200491 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200492
Christopher Faulete9b70722019-04-08 10:46:02 +0200493 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200494 h2c->flags = H2_CF_IS_BACK;
495 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
496 if (tick_isset(prx->timeout.serverfin))
497 h2c->shut_timeout = prx->timeout.serverfin;
498 } else {
499 h2c->flags = H2_CF_NONE;
500 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
501 if (tick_isset(prx->timeout.clientfin))
502 h2c->shut_timeout = prx->timeout.clientfin;
503 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100504
Willy Tarreau0b37d652018-10-03 10:33:02 +0200505 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100506 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100507 if (tick_isset(h2c->timeout)) {
508 t = task_new(tid_bit);
509 if (!t)
510 goto fail;
511
512 h2c->task = t;
513 t->process = h2_timeout_task;
514 t->context = h2c;
515 t->expire = tick_add(now_ms, h2c->timeout);
516 }
Willy Tarreauea392822017-10-31 10:02:25 +0100517
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200518 h2c->wait_event.task = tasklet_new();
519 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200521 h2c->wait_event.task->process = h2_io_cb;
522 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100523 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200524
Willy Tarreau32218eb2017-09-22 08:07:25 +0200525 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
526 if (!h2c->ddht)
527 goto fail;
528
529 /* Initialise the context. */
530 h2c->st0 = H2_CS_PREFACE;
531 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100532 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200533 h2c->max_id = -1;
534 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100535 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100537 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200538 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100539 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100540 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200541
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200542 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200543 h2c->dsi = -1;
544 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100545
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 h2c->last_sid = -1;
547
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200548 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 h2c->miw = 65535; /* mux initial window size */
550 h2c->mws = 65535; /* mux window size */
551 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200552 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553 LIST_INIT(&h2c->send_list);
554 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200555 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100556 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200557
Willy Tarreau3f133572017-10-31 19:21:06 +0100558 if (t)
559 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100560
Willy Tarreau01b44822018-10-03 14:26:37 +0200561 if (h2c->flags & H2_CF_IS_BACK) {
562 /* FIXME: this is temporary, for outgoing connections we need
563 * to immediately allocate a stream until the code is modified
564 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 */
567 struct h2s *h2s;
568
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100569 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200570 if (!h2s)
571 goto fail_stream;
572 }
573
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100574 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575
Willy Tarreau0f383582018-10-03 14:22:21 +0200576 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200577 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200578 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200579 fail_stream:
580 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200581 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200582 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200583 if (h2c->wait_event.task)
584 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100585 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200586 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200587 return -1;
588}
589
Willy Tarreau751f2d02018-10-05 09:35:00 +0200590/* returns the next allocatable outgoing stream ID for the H2 connection, or
591 * -1 if no more is allocatable.
592 */
593static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
594{
595 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100596
597 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200598 id = -1;
599 return id;
600}
601
Willy Tarreau2373acc2017-10-12 17:35:14 +0200602/* returns the stream associated with id <id> or NULL if not found */
603static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
604{
605 struct eb32_node *node;
606
Willy Tarreau751f2d02018-10-05 09:35:00 +0200607 if (id == 0)
608 return (struct h2s *)h2_closed_stream;
609
Willy Tarreau2a856182017-05-16 15:20:39 +0200610 if (id > h2c->max_id)
611 return (struct h2s *)h2_idle_stream;
612
Willy Tarreau2373acc2017-10-12 17:35:14 +0200613 node = eb32_lookup(&h2c->streams_by_id, id);
614 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200615 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200616
617 return container_of(node, struct h2s, by_id);
618}
619
Christopher Faulet73c12072019-04-08 11:23:22 +0200620/* release function. This one should be called to free all resources allocated
621 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200622 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200623static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200624{
Christopher Faulet61840e72019-04-15 09:33:32 +0200625 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200626
Willy Tarreau32218eb2017-09-22 08:07:25 +0200627 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200628 /* The connection must be aattached to this mux to be released */
629 if (h2c->conn && h2c->conn->ctx == h2c)
630 conn = h2c->conn;
631
Willy Tarreau32218eb2017-09-22 08:07:25 +0200632 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200633
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100634 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100635 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100636 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200637
Willy Tarreau44e973f2018-03-01 17:49:30 +0100638 h2_release_buf(h2c, &h2c->dbuf);
639 h2_release_buf(h2c, &h2c->mbuf);
640
Willy Tarreauea392822017-10-31 10:02:25 +0100641 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200642 h2c->task->context = NULL;
643 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100644 h2c->task = NULL;
645 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200646 if (h2c->wait_event.task)
647 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200648 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100649 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200650 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100651
Willy Tarreaubafbe012017-11-24 17:34:44 +0100652 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200653 }
654
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200655 if (conn) {
656 conn->mux = NULL;
657 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200658
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200659 conn_stop_tracking(conn);
660 conn_full_close(conn);
661 if (conn->destroy_cb)
662 conn->destroy_cb(conn);
663 conn_free(conn);
664 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200665}
666
667
Willy Tarreau71681172017-10-23 14:39:06 +0200668/******************************************************/
669/* functions below are for the H2 protocol processing */
670/******************************************************/
671
672/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100673static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200674{
675 return h2s ? h2s->id : 0;
676}
677
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200678/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100679static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200680{
681 if (h2c->msi < 0)
682 return 0;
683
684 if (h2c->msi == h2s_id(h2s))
685 return 0;
686
687 return 1;
688}
689
Willy Tarreau741d6df2017-10-17 08:00:59 +0200690/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100691static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200692{
693 h2c->errcode = err;
694 h2c->st0 = H2_CS_ERROR;
695}
696
Willy Tarreau175cebb2019-01-24 10:02:24 +0100697/* marks an error on the stream. It may also update an already closed stream
698 * (e.g. to report an error after an RST was received).
699 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100700static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200701{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100702 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200703 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100704 if (h2s->st < H2_SS_ERROR)
705 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100706 if (h2s->cs)
707 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200708 }
709}
710
Willy Tarreau7e094452018-12-19 18:08:52 +0100711/* attempt to notify the data layer of recv availability */
712static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
713{
714 struct wait_event *sw;
715
716 if (h2s->recv_wait) {
717 sw = h2s->recv_wait;
718 sw->events &= ~SUB_RETRY_RECV;
719 tasklet_wakeup(sw->task);
720 h2s->recv_wait = NULL;
721 }
722}
723
724/* attempt to notify the data layer of send availability */
725static void __maybe_unused h2s_notify_send(struct h2s *h2s)
726{
727 struct wait_event *sw;
728
Willy Tarreauc234ae32019-05-13 17:56:11 +0200729 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 sw = h2s->send_wait;
731 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100732 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100733 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100734 }
735}
736
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100737/* alerts the data layer, trying to wake it up by all means, following
738 * this sequence :
739 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
740 * - if its subscribed to send, then it's woken up for send
741 * - if it was subscribed to neither, its ->wake() callback is called
742 * It is safe to call this function with a closed stream which doesn't have a
743 * conn_stream anymore.
744 */
745static void __maybe_unused h2s_alert(struct h2s *h2s)
746{
747 if (h2s->recv_wait || h2s->send_wait) {
748 h2s_notify_recv(h2s);
749 h2s_notify_send(h2s);
750 }
751 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
752 h2s->cs->data_cb->wake(h2s->cs);
753}
754
Willy Tarreaue4820742017-07-27 13:37:23 +0200755/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100756static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200757{
758 uint8_t *out = frame;
759
760 *out = len >> 16;
761 write_n16(out + 1, len);
762}
763
Willy Tarreau54c15062017-10-10 17:10:03 +0200764/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
765 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
766 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200767 * available in the buffer's input prior to calling this function. The buffer
768 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200769 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100770static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200771 const struct buffer *b, int o)
772{
Willy Tarreau591d4452018-06-15 17:21:00 +0200773 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 +0200774}
775
Willy Tarreau1f094672017-11-20 21:27:45 +0100776static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200777{
Willy Tarreau591d4452018-06-15 17:21:00 +0200778 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200779}
780
Willy Tarreau1f094672017-11-20 21:27:45 +0100781static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200782{
Willy Tarreau591d4452018-06-15 17:21:00 +0200783 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200784}
785
Willy Tarreau1f094672017-11-20 21:27:45 +0100786static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200787{
Willy Tarreau591d4452018-06-15 17:21:00 +0200788 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200789}
790
791
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100792/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
793 * The algorithm is not obvious. It turns out that H2 headers are neither
794 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
795 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200796 *
797 * b0 b1 b2 b3 b4 b5..b8
798 * +----------+---------+--------+----+----+----------------------+
799 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
800 * +----------+---------+--------+----+----+----------------------+
801 *
802 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
803 * we get the sid properly aligned and ordered, and 16 bits of len properly
804 * ordered as well. The type and flags can be extracted using bit shifts from
805 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200806 * Returns zero if some bytes are missing, otherwise non-zero on success. The
807 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100809static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200810{
811 uint64_t w;
812
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100813 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200814 return 0;
815
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100816 w = h2_get_n64(b, o + 1);
817 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200818 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
819 h->ff = w >> 32;
820 h->ft = w >> 40;
821 h->len += w >> 48;
822 return 1;
823}
824
825/* skip the next 9 bytes corresponding to the frame header possibly parsed by
826 * h2_peek_frame_hdr() above.
827 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100828static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200829{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200830 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200831}
832
833/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100834static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200835{
836 int ret;
837
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100838 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200839 if (ret > 0)
840 h2_skip_frame_hdr(b);
841 return ret;
842}
843
Willy Tarreau00dd0782018-03-01 16:31:34 +0100844/* marks stream <h2s> as CLOSED and decrement the number of active streams for
845 * its connection if the stream was not yet closed. Please use this exclusively
846 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100848static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100849{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100850 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100851 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100852 if (!h2s->id)
853 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100854 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100855 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100856 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
857 h2s_notify_recv(h2s);
858 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100859 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100860 h2s->st = H2_SS_CLOSED;
861}
862
Willy Tarreau71049cc2018-03-28 13:56:39 +0200863/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
864static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100865{
866 h2s_close(h2s);
867 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200868 if (b_size(&h2s->rxbuf)) {
869 b_free(&h2s->rxbuf);
870 offer_buffers(NULL, tasks_run_queue);
871 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200872 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100873 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200874 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100875 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800876 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200877 * reference left would be in the h2c send_list/fctl_list, and if
878 * we're in it, we're getting out anyway
879 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100880 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200881 if (LIST_ADDED(&h2s->sending_list)) {
Olivier Houchard998410a2019-04-15 19:23:37 +0200882 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
883 LIST_DEL_INIT(&h2s->sending_list);
884 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200885 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100886 pool_free(pool_head_h2s, h2s);
887}
888
Willy Tarreaua8e49542018-10-03 18:53:55 +0200889/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
890 * stream tree. In case of error, nothing is added and NULL is returned. The
891 * causes of errors can be any failed memory allocation. The caller is
892 * responsible for checking if the connection may support an extra stream
893 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200895static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 struct h2s *h2s;
898
Willy Tarreaubafbe012017-11-24 17:34:44 +0100899 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200900 if (!h2s)
901 goto out;
902
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200903 h2s->wait_event.task = tasklet_new();
904 if (!h2s->wait_event.task) {
905 pool_free(pool_head_h2s, h2s);
906 goto out;
907 }
908 h2s->send_wait = NULL;
909 h2s->recv_wait = NULL;
910 h2s->wait_event.task->process = h2_deferred_shut;
911 h2s->wait_event.task->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100912 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200913 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100914 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200915 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200916 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200917 h2s->mws = h2c->miw;
918 h2s->flags = H2_SF_NONE;
919 h2s->errcode = H2_ERR_NO_ERROR;
920 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200921 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100922 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200923 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200924
925 if (h2c->flags & H2_CF_IS_BACK) {
926 h1m_init_req(&h2s->h1m);
927 h2s->h1m.err_pos = -1; // don't care about errors on the request path
928 h2s->h1m.flags |= H1_MF_TOLOWER;
929 } else {
930 h1m_init_res(&h2s->h1m);
931 h2s->h1m.err_pos = -1; // don't care about errors on the response path
932 h2s->h1m.flags |= H1_MF_TOLOWER;
933 }
934
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200935 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200936 if (id > 0)
937 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100938 else
939 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200940
941 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100942 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100943 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200944
945 return h2s;
946
947 out_free_h2s:
948 pool_free(pool_head_h2s, h2s);
949 out:
950 return NULL;
951}
952
953/* creates a new stream <id> on the h2c connection and returns it, or NULL in
954 * case of memory allocation error.
955 */
956static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
957{
958 struct session *sess = h2c->conn->owner;
959 struct conn_stream *cs;
960 struct h2s *h2s;
961
962 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
963 goto out;
964
965 h2s = h2s_new(h2c, id);
966 if (!h2s)
967 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200968
969 cs = cs_new(h2c->conn);
970 if (!cs)
971 goto out_close;
972
Olivier Houchard746fb772018-12-15 19:42:00 +0100973 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974 h2s->cs = cs;
975 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200976 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200977
978 if (stream_create_from_cs(cs) < 0)
979 goto out_free_cs;
980
Willy Tarreau590a0512018-09-05 11:56:48 +0200981 /* We want the accept date presented to the next stream to be the one
982 * we have now, the handshake time to be null (since the next stream
983 * is not delayed by a handshake), and the idle time to count since
984 * right now.
985 */
986 sess->accept_date = date;
987 sess->tv_accept = now;
988 sess->t_handshake = 0;
989
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100991 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200992 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200993 return h2s;
994
995 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200996 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997 cs_free(cs);
998 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200999 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001001 sess_log(sess);
1002 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001003}
1004
Willy Tarreau751f2d02018-10-05 09:35:00 +02001005/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1006 * and returns it, or NULL in case of memory allocation error or if the highest
1007 * possible stream ID was reached.
1008 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001009static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001010{
1011 struct h2s *h2s = NULL;
1012
Willy Tarreau86949782019-01-31 10:42:05 +01001013 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001014 goto out;
1015
Willy Tarreaua80dca82019-01-24 17:08:28 +01001016 if (h2_streams_left(h2c) < 1)
1017 goto out;
1018
Willy Tarreau751f2d02018-10-05 09:35:00 +02001019 /* Defer choosing the ID until we send the first message to create the stream */
1020 h2s = h2s_new(h2c, 0);
1021 if (!h2s)
1022 goto out;
1023
1024 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001025 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001026 cs->ctx = h2s;
1027 h2c->nb_cs++;
1028
Willy Tarreau751f2d02018-10-05 09:35:00 +02001029 out:
1030 return h2s;
1031}
1032
Willy Tarreaube5b7152017-09-25 16:25:39 +02001033/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1034 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1035 * the various settings codes.
1036 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001037static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001038{
1039 struct buffer *res;
1040 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001041 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001042 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001043 int ret;
1044
1045 if (h2c_mux_busy(h2c, NULL)) {
1046 h2c->flags |= H2_CF_DEM_MBUSY;
1047 return 0;
1048 }
1049
Willy Tarreau44e973f2018-03-01 17:49:30 +01001050 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001051 if (!res) {
1052 h2c->flags |= H2_CF_MUX_MALLOC;
1053 h2c->flags |= H2_CF_DEM_MROOM;
1054 return 0;
1055 }
1056
1057 chunk_init(&buf, buf_data, sizeof(buf_data));
1058 chunk_memcpy(&buf,
1059 "\x00\x00\x00" /* length : 0 for now */
1060 "\x04\x00" /* type : 4 (settings), flags : 0 */
1061 "\x00\x00\x00\x00", /* stream ID : 0 */
1062 9);
1063
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001064 if (h2c->flags & H2_CF_IS_BACK) {
1065 /* send settings_enable_push=0 */
1066 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1067 }
1068
Willy Tarreaube5b7152017-09-25 16:25:39 +02001069 if (h2_settings_header_table_size != 4096) {
1070 char str[6] = "\x00\x01"; /* header_table_size */
1071
1072 write_n32(str + 2, h2_settings_header_table_size);
1073 chunk_memcat(&buf, str, 6);
1074 }
1075
1076 if (h2_settings_initial_window_size != 65535) {
1077 char str[6] = "\x00\x04"; /* initial_window_size */
1078
1079 write_n32(str + 2, h2_settings_initial_window_size);
1080 chunk_memcat(&buf, str, 6);
1081 }
1082
1083 if (h2_settings_max_concurrent_streams != 0) {
1084 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1085
1086 /* Note: 0 means "unlimited" for haproxy's config but not for
1087 * the protocol, so never send this value!
1088 */
1089 write_n32(str + 2, h2_settings_max_concurrent_streams);
1090 chunk_memcat(&buf, str, 6);
1091 }
1092
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001093 mfs = h2_settings_max_frame_size;
1094 if (mfs > global.tune.bufsize)
1095 mfs = global.tune.bufsize;
1096
1097 if (!mfs)
1098 mfs = global.tune.bufsize;
1099
1100 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001101 char str[6] = "\x00\x05"; /* max_frame_size */
1102
1103 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1104 * match bufsize - rewrite size, but at the moment it seems
1105 * that clients don't take care of it.
1106 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001107 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001108 chunk_memcat(&buf, str, 6);
1109 }
1110
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001111 h2_set_frame_size(buf.area, buf.data - 9);
1112 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001113 if (unlikely(ret <= 0)) {
1114 if (!ret) {
1115 h2c->flags |= H2_CF_MUX_MFULL;
1116 h2c->flags |= H2_CF_DEM_MROOM;
1117 return 0;
1118 }
1119 else {
1120 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1121 return 0;
1122 }
1123 }
1124 return ret;
1125}
1126
Willy Tarreau52eed752017-09-22 15:05:09 +02001127/* Try to receive a connection preface, then upon success try to send our
1128 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1129 * missing data. It may return an error in h2c.
1130 */
1131static int h2c_frt_recv_preface(struct h2c *h2c)
1132{
1133 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001134 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001135
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001136 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001137
1138 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001139 if (ret1 < 0)
1140 sess_log(h2c->conn->owner);
1141
Willy Tarreau52eed752017-09-22 15:05:09 +02001142 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1143 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1144 return 0;
1145 }
1146
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001147 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001148 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001149 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001150
Willy Tarreaube5b7152017-09-25 16:25:39 +02001151 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001152}
1153
Willy Tarreau01b44822018-10-03 14:26:37 +02001154/* Try to send a connection preface, then upon success try to send our
1155 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1156 * missing data. It may return an error in h2c.
1157 */
1158static int h2c_bck_send_preface(struct h2c *h2c)
1159{
1160 struct buffer *res;
1161
1162 if (h2c_mux_busy(h2c, NULL)) {
1163 h2c->flags |= H2_CF_DEM_MBUSY;
1164 return 0;
1165 }
1166
1167 res = h2_get_buf(h2c, &h2c->mbuf);
1168 if (!res) {
1169 h2c->flags |= H2_CF_MUX_MALLOC;
1170 h2c->flags |= H2_CF_DEM_MROOM;
1171 return 0;
1172 }
1173
1174 if (!b_data(res)) {
1175 /* preface not yet sent */
1176 b_istput(res, ist(H2_CONN_PREFACE));
1177 }
1178
1179 return h2c_send_settings(h2c);
1180}
1181
Willy Tarreau081d4722017-05-16 21:51:05 +02001182/* try to send a GOAWAY frame on the connection to report an error or a graceful
1183 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1184 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1185 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1186 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1187 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1188 * on unrecoverable failure. It will not attempt to send one again in this last
1189 * case so that it is safe to use h2c_error() to report such errors.
1190 */
1191static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1192{
1193 struct buffer *res;
1194 char str[17];
1195 int ret;
1196
1197 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1198 return 1; // claim that it worked
1199
1200 if (h2c_mux_busy(h2c, h2s)) {
1201 if (h2s)
1202 h2s->flags |= H2_SF_BLK_MBUSY;
1203 else
1204 h2c->flags |= H2_CF_DEM_MBUSY;
1205 return 0;
1206 }
1207
Willy Tarreau44e973f2018-03-01 17:49:30 +01001208 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001209 if (!res) {
1210 h2c->flags |= H2_CF_MUX_MALLOC;
1211 if (h2s)
1212 h2s->flags |= H2_SF_BLK_MROOM;
1213 else
1214 h2c->flags |= H2_CF_DEM_MROOM;
1215 return 0;
1216 }
1217
1218 /* len: 8, type: 7, flags: none, sid: 0 */
1219 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1220
1221 if (h2c->last_sid < 0)
1222 h2c->last_sid = h2c->max_id;
1223
1224 write_n32(str + 9, h2c->last_sid);
1225 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001226 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001227 if (unlikely(ret <= 0)) {
1228 if (!ret) {
1229 h2c->flags |= H2_CF_MUX_MFULL;
1230 if (h2s)
1231 h2s->flags |= H2_SF_BLK_MROOM;
1232 else
1233 h2c->flags |= H2_CF_DEM_MROOM;
1234 return 0;
1235 }
1236 else {
1237 /* we cannot report this error using GOAWAY, so we mark
1238 * it and claim a success.
1239 */
1240 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1241 h2c->flags |= H2_CF_GOAWAY_FAILED;
1242 return 1;
1243 }
1244 }
1245 h2c->flags |= H2_CF_GOAWAY_SENT;
1246 return ret;
1247}
1248
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001249/* Try to send an RST_STREAM frame on the connection for the indicated stream
1250 * during mux operations. This stream must be valid and cannot be closed
1251 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1252 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1253 * not yet.
1254 *
1255 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1256 * to write the message, it subscribes the stream to future notifications.
1257 */
1258static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1259{
1260 struct buffer *res;
1261 char str[13];
1262 int ret;
1263
1264 if (!h2s || h2s->st == H2_SS_CLOSED)
1265 return 1;
1266
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001267 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1268 * RST_STREAM in response to a RST_STREAM frame.
1269 */
1270 if (h2c->dft == H2_FT_RST_STREAM) {
1271 ret = 1;
1272 goto ignore;
1273 }
1274
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001275 if (h2c_mux_busy(h2c, h2s)) {
1276 h2s->flags |= H2_SF_BLK_MBUSY;
1277 return 0;
1278 }
1279
Willy Tarreau44e973f2018-03-01 17:49:30 +01001280 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001281 if (!res) {
1282 h2c->flags |= H2_CF_MUX_MALLOC;
1283 h2s->flags |= H2_SF_BLK_MROOM;
1284 return 0;
1285 }
1286
1287 /* len: 4, type: 3, flags: none */
1288 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1289 write_n32(str + 5, h2s->id);
1290 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001291 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001292
1293 if (unlikely(ret <= 0)) {
1294 if (!ret) {
1295 h2c->flags |= H2_CF_MUX_MFULL;
1296 h2s->flags |= H2_SF_BLK_MROOM;
1297 return 0;
1298 }
1299 else {
1300 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1301 return 0;
1302 }
1303 }
1304
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001305 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001306 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001307 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001308 return ret;
1309}
1310
1311/* Try to send an RST_STREAM frame on the connection for the stream being
1312 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001313 * error code, even if the stream is one of the dummy ones, and will update
1314 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001315 *
1316 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1317 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001318 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001319 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001320 */
1321static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1322{
1323 struct buffer *res;
1324 char str[13];
1325 int ret;
1326
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001327 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1328 * RST_STREAM in response to a RST_STREAM frame.
1329 */
1330 if (h2c->dft == H2_FT_RST_STREAM) {
1331 ret = 1;
1332 goto ignore;
1333 }
1334
Willy Tarreau27a84c92017-10-17 08:10:17 +02001335 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001336 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 return 0;
1338 }
1339
Willy Tarreau44e973f2018-03-01 17:49:30 +01001340 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 if (!res) {
1342 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001343 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001344 return 0;
1345 }
1346
1347 /* len: 4, type: 3, flags: none */
1348 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001349
Willy Tarreau27a84c92017-10-17 08:10:17 +02001350 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001351 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001352 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001353
Willy Tarreau27a84c92017-10-17 08:10:17 +02001354 if (unlikely(ret <= 0)) {
1355 if (!ret) {
1356 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001357 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001358 return 0;
1359 }
1360 else {
1361 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1362 return 0;
1363 }
1364 }
1365
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001366 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001367 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001368 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001369 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001370 }
1371
Willy Tarreau27a84c92017-10-17 08:10:17 +02001372 return ret;
1373}
1374
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001375/* try to send an empty DATA frame with the ES flag set to notify about the
1376 * end of stream and match a shutdown(write). If an ES was already sent as
1377 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1378 * on success or zero if nothing was done. In case of lack of room to write the
1379 * message, it subscribes the requesting stream to future notifications.
1380 */
1381static int h2_send_empty_data_es(struct h2s *h2s)
1382{
1383 struct h2c *h2c = h2s->h2c;
1384 struct buffer *res;
1385 char str[9];
1386 int ret;
1387
Willy Tarreau721c9742017-11-07 11:05:42 +01001388 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001389 return 1;
1390
1391 if (h2c_mux_busy(h2c, h2s)) {
1392 h2s->flags |= H2_SF_BLK_MBUSY;
1393 return 0;
1394 }
1395
Willy Tarreau44e973f2018-03-01 17:49:30 +01001396 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001397 if (!res) {
1398 h2c->flags |= H2_CF_MUX_MALLOC;
1399 h2s->flags |= H2_SF_BLK_MROOM;
1400 return 0;
1401 }
1402
1403 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1404 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1405 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001406 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001407 if (likely(ret > 0)) {
1408 h2s->flags |= H2_SF_ES_SENT;
1409 }
1410 else if (!ret) {
1411 h2c->flags |= H2_CF_MUX_MFULL;
1412 h2s->flags |= H2_SF_BLK_MROOM;
1413 return 0;
1414 }
1415 else {
1416 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1417 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001418 }
1419 return ret;
1420}
1421
Christopher Fauletf02ca002019-03-07 16:21:34 +01001422/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1423 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1424 * connection. The stream's state is automatically updated accordingly. If the
1425 * stream is orphaned, it is destroyed.
1426 */
1427static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1428{
1429 if (!h2s->cs) {
1430 /* this stream was already orphaned */
1431 h2s_destroy(h2s);
1432 return;
1433 }
1434
Willy Tarreau23482912019-05-07 15:23:14 +02001435 if (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))
1436 flags |= CS_FL_ERR_PENDING;
1437
Christopher Fauletf02ca002019-03-07 16:21:34 +01001438 h2s->cs->flags |= flags;
1439 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1440 h2s->cs->flags |= CS_FL_ERROR;
1441
1442 h2s_alert(h2s);
1443
1444 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1445 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001446 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001447 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001448 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001449 h2s_close(h2s);
1450}
1451
1452/* wake the streams attached to the connection, whose id is greater than <last>
1453 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001454 */
Willy Tarreau23482912019-05-07 15:23:14 +02001455static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001456{
1457 struct eb32_node *node;
1458 struct h2s *h2s;
Willy Tarreau23482912019-05-07 15:23:14 +02001459 uint32_t flags = 0;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001460
1461 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001462 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001463
1464 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001465 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001466
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001468 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1469 while (node) {
1470 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001471 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001472 h2s_wake_one_stream(h2s, flags);
1473 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001474
Christopher Fauletf02ca002019-03-07 16:21:34 +01001475 /* Wake all streams with unassigned ID (ID == 0) */
1476 node = eb32_lookup(&h2c->streams_by_id, 0);
1477 while (node) {
1478 h2s = container_of(node, struct h2s, by_id);
1479 if (h2s->id > 0)
1480 break;
1481 node = eb32_next(node);
1482 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001483 }
1484}
1485
Willy Tarreau3421aba2017-07-27 15:41:03 +02001486/* Increase all streams' outgoing window size by the difference passed in
1487 * argument. This is needed upon receipt of the settings frame if the initial
1488 * window size is different. The difference may be negative and the resulting
1489 * window size as well, for the time it takes to receive some window updates.
1490 */
1491static void h2c_update_all_ws(struct h2c *h2c, int diff)
1492{
1493 struct h2s *h2s;
1494 struct eb32_node *node;
1495
1496 if (!diff)
1497 return;
1498
1499 node = eb32_first(&h2c->streams_by_id);
1500 while (node) {
1501 h2s = container_of(node, struct h2s, by_id);
1502 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001503
1504 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1505 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001506 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001507 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001508 }
1509
Willy Tarreau3421aba2017-07-27 15:41:03 +02001510 node = eb32_next(node);
1511 }
1512}
1513
1514/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1515 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001516 * return an error in h2c. The caller must have already verified frame length
1517 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001518 */
1519static int h2c_handle_settings(struct h2c *h2c)
1520{
1521 unsigned int offset;
1522 int error;
1523
1524 if (h2c->dff & H2_F_SETTINGS_ACK) {
1525 if (h2c->dfl) {
1526 error = H2_ERR_FRAME_SIZE_ERROR;
1527 goto fail;
1528 }
1529 return 1;
1530 }
1531
Willy Tarreau3421aba2017-07-27 15:41:03 +02001532 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001533 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001534 return 0;
1535
1536 /* parse the frame */
1537 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001538 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1539 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001540
1541 switch (type) {
1542 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1543 /* we need to update all existing streams with the
1544 * difference from the previous iws.
1545 */
1546 if (arg < 0) { // RFC7540#6.5.2
1547 error = H2_ERR_FLOW_CONTROL_ERROR;
1548 goto fail;
1549 }
1550 h2c_update_all_ws(h2c, arg - h2c->miw);
1551 h2c->miw = arg;
1552 break;
1553 case H2_SETTINGS_MAX_FRAME_SIZE:
1554 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1555 error = H2_ERR_PROTOCOL_ERROR;
1556 goto fail;
1557 }
1558 h2c->mfs = arg;
1559 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001560 case H2_SETTINGS_ENABLE_PUSH:
1561 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1562 error = H2_ERR_PROTOCOL_ERROR;
1563 goto fail;
1564 }
1565 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001566 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1567 if (h2c->flags & H2_CF_IS_BACK) {
1568 /* the limit is only for the backend; for the frontend it is our limit */
1569 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1570 arg = h2_settings_max_concurrent_streams;
1571 h2c->streams_limit = arg;
1572 }
1573 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001574 }
1575 }
1576
1577 /* need to ACK this frame now */
1578 h2c->st0 = H2_CS_FRAME_A;
1579 return 1;
1580 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001581 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001582 h2c_error(h2c, error);
1583 return 0;
1584}
1585
1586/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1587 * success or one of the h2_status values.
1588 */
1589static int h2c_ack_settings(struct h2c *h2c)
1590{
1591 struct buffer *res;
1592 char str[9];
1593 int ret = -1;
1594
1595 if (h2c_mux_busy(h2c, NULL)) {
1596 h2c->flags |= H2_CF_DEM_MBUSY;
1597 return 0;
1598 }
1599
Willy Tarreau44e973f2018-03-01 17:49:30 +01001600 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001601 if (!res) {
1602 h2c->flags |= H2_CF_MUX_MALLOC;
1603 h2c->flags |= H2_CF_DEM_MROOM;
1604 return 0;
1605 }
1606
1607 memcpy(str,
1608 "\x00\x00\x00" /* length : 0 (no data) */
1609 "\x04" "\x01" /* type : 4, flags : ACK */
1610 "\x00\x00\x00\x00" /* stream ID */, 9);
1611
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001612 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001613 if (unlikely(ret <= 0)) {
1614 if (!ret) {
1615 h2c->flags |= H2_CF_MUX_MFULL;
1616 h2c->flags |= H2_CF_DEM_MROOM;
1617 return 0;
1618 }
1619 else {
1620 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1621 return 0;
1622 }
1623 }
1624 return ret;
1625}
1626
Willy Tarreaucf68c782017-10-10 17:11:41 +02001627/* processes a PING frame and schedules an ACK if needed. The caller must pass
1628 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001629 * missing data. The caller must have already verified frame length
1630 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 */
1632static int h2c_handle_ping(struct h2c *h2c)
1633{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001634 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001635 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001636 h2c->st0 = H2_CS_FRAME_A;
1637 return 1;
1638}
1639
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001640/* Try to send a window update for stream id <sid> and value <increment>.
1641 * Returns > 0 on success or zero on missing room or failure. It may return an
1642 * error in h2c.
1643 */
1644static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1645{
1646 struct buffer *res;
1647 char str[13];
1648 int ret = -1;
1649
1650 if (h2c_mux_busy(h2c, NULL)) {
1651 h2c->flags |= H2_CF_DEM_MBUSY;
1652 return 0;
1653 }
1654
Willy Tarreau44e973f2018-03-01 17:49:30 +01001655 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001656 if (!res) {
1657 h2c->flags |= H2_CF_MUX_MALLOC;
1658 h2c->flags |= H2_CF_DEM_MROOM;
1659 return 0;
1660 }
1661
1662 /* length: 4, type: 8, flags: none */
1663 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1664 write_n32(str + 5, sid);
1665 write_n32(str + 9, increment);
1666
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001667 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001668
1669 if (unlikely(ret <= 0)) {
1670 if (!ret) {
1671 h2c->flags |= H2_CF_MUX_MFULL;
1672 h2c->flags |= H2_CF_DEM_MROOM;
1673 return 0;
1674 }
1675 else {
1676 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1677 return 0;
1678 }
1679 }
1680 return ret;
1681}
1682
1683/* try to send pending window update for the connection. It's safe to call it
1684 * with no pending updates. Returns > 0 on success or zero on missing room or
1685 * failure. It may return an error in h2c.
1686 */
1687static int h2c_send_conn_wu(struct h2c *h2c)
1688{
1689 int ret = 1;
1690
1691 if (h2c->rcvd_c <= 0)
1692 return 1;
1693
Willy Tarreau97aaa672018-12-23 09:49:04 +01001694 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1695 /* increase the advertised connection window to 2G on
1696 * first update.
1697 */
1698 h2c->flags |= H2_CF_WINDOW_OPENED;
1699 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1700 }
1701
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001702 /* send WU for the connection */
1703 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1704 if (ret > 0)
1705 h2c->rcvd_c = 0;
1706
1707 return ret;
1708}
1709
1710/* try to send pending window update for the current dmux stream. It's safe to
1711 * call it with no pending updates. Returns > 0 on success or zero on missing
1712 * room or failure. It may return an error in h2c.
1713 */
1714static int h2c_send_strm_wu(struct h2c *h2c)
1715{
1716 int ret = 1;
1717
1718 if (h2c->rcvd_s <= 0)
1719 return 1;
1720
1721 /* send WU for the stream */
1722 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1723 if (ret > 0)
1724 h2c->rcvd_s = 0;
1725
1726 return ret;
1727}
1728
Willy Tarreaucf68c782017-10-10 17:11:41 +02001729/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1730 * success, 0 on missing data or one of the h2_status values.
1731 */
1732static int h2c_ack_ping(struct h2c *h2c)
1733{
1734 struct buffer *res;
1735 char str[17];
1736 int ret = -1;
1737
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001738 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001739 return 0;
1740
1741 if (h2c_mux_busy(h2c, NULL)) {
1742 h2c->flags |= H2_CF_DEM_MBUSY;
1743 return 0;
1744 }
1745
Willy Tarreau44e973f2018-03-01 17:49:30 +01001746 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001747 if (!res) {
1748 h2c->flags |= H2_CF_MUX_MALLOC;
1749 h2c->flags |= H2_CF_DEM_MROOM;
1750 return 0;
1751 }
1752
1753 memcpy(str,
1754 "\x00\x00\x08" /* length : 8 (same payload) */
1755 "\x06" "\x01" /* type : 6, flags : ACK */
1756 "\x00\x00\x00\x00" /* stream ID */, 9);
1757
1758 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001759 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001760
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001761 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001762 if (unlikely(ret <= 0)) {
1763 if (!ret) {
1764 h2c->flags |= H2_CF_MUX_MFULL;
1765 h2c->flags |= H2_CF_DEM_MROOM;
1766 return 0;
1767 }
1768 else {
1769 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1770 return 0;
1771 }
1772 }
1773 return ret;
1774}
1775
Willy Tarreau26f95952017-07-27 17:18:30 +02001776/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1777 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001778 * h2c or h2s. The caller must have already verified frame length and stream ID
1779 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001780 */
1781static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1782{
1783 int32_t inc;
1784 int error;
1785
Willy Tarreau26f95952017-07-27 17:18:30 +02001786 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001787 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001788 return 0;
1789
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001790 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001791
1792 if (h2c->dsi != 0) {
1793 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001794
1795 /* it's not an error to receive WU on a closed stream */
1796 if (h2s->st == H2_SS_CLOSED)
1797 return 1;
1798
1799 if (!inc) {
1800 error = H2_ERR_PROTOCOL_ERROR;
1801 goto strm_err;
1802 }
1803
1804 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1805 error = H2_ERR_FLOW_CONTROL_ERROR;
1806 goto strm_err;
1807 }
1808
1809 h2s->mws += inc;
1810 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1811 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001812 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001813 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001814 }
1815 }
1816 else {
1817 /* connection window update */
1818 if (!inc) {
1819 error = H2_ERR_PROTOCOL_ERROR;
1820 goto conn_err;
1821 }
1822
1823 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1824 error = H2_ERR_FLOW_CONTROL_ERROR;
1825 goto conn_err;
1826 }
1827
1828 h2c->mws += inc;
1829 }
1830
1831 return 1;
1832
1833 conn_err:
1834 h2c_error(h2c, error);
1835 return 0;
1836
1837 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001838 h2s_error(h2s, error);
1839 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001840 return 0;
1841}
1842
Willy Tarreaue96b0922017-10-30 00:28:29 +01001843/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001844 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1845 * have already verified frame length and stream ID validity. Described in
1846 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847 */
1848static int h2c_handle_goaway(struct h2c *h2c)
1849{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 int last;
1851
Willy Tarreaue96b0922017-10-30 00:28:29 +01001852 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001853 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001854 return 0;
1855
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 last = h2_get_n32(&h2c->dbuf, 0);
1857 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001858 if (h2c->last_sid < 0)
1859 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001860 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001861 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001862}
1863
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001865 * invalid. Returns > 0 on success or zero on missing data. It may return an
1866 * error in h2c. The caller must have already verified frame length and stream
1867 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868 */
1869static int h2c_handle_priority(struct h2c *h2c)
1870{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001872 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001873 return 0;
1874
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001875 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001876 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001877 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1878 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001879 }
1880 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001881}
1882
Willy Tarreaucd234e92017-08-18 10:59:39 +02001883/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001884 * Returns > 0 on success or zero on missing data. The caller must have already
1885 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 */
1887static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1888{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001889 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001890 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001891 return 0;
1892
1893 /* late RST, already handled */
1894 if (h2s->st == H2_SS_CLOSED)
1895 return 1;
1896
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001897 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001898 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001899
1900 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001901 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001902 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001903 }
1904
1905 h2s->flags |= H2_SF_RST_RCVD;
1906 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001907}
1908
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001909/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1910 * It may return an error in h2c or h2s. The caller must consider that the
1911 * return value is the new h2s in case one was allocated (most common case).
1912 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001913 * errors here are reported as connection errors since it's impossible to
1914 * recover from such errors after the compression context has been altered.
1915 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001916static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001917{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001918 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001919 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001920 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001921 int error;
1922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001923 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001924 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001926 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001927 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001928
1929 /* now either the frame is complete or the buffer is complete */
1930 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001931 /* The stream exists/existed, this must be a trailers frame */
1932 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001933 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1934 /* unrecoverable error ? */
1935 if (h2c->st0 >= H2_CS_ERROR)
1936 goto out;
1937
1938 if (error == 0)
1939 goto out; // missing data
1940
1941 if (error < 0) {
1942 /* Failed to decode this frame (e.g. too large request)
1943 * but the HPACK decompressor is still synchronized.
1944 */
1945 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1946 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001947 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001948 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001949 goto done;
1950 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001951 /* the connection was already killed by an RST, let's consume
1952 * the data and send another RST.
1953 */
1954 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1955 h2s = (struct h2s*)h2_error_stream;
1956 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001957 }
1958 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1959 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1960 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001961 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001962 goto conn_err;
1963 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001964 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1965 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001966
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001967 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001968
Willy Tarreau25919232019-01-03 14:48:18 +01001969 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001970 if (h2c->st0 >= H2_CS_ERROR)
1971 goto out;
1972
Willy Tarreau25919232019-01-03 14:48:18 +01001973 if (error <= 0) {
1974 if (error == 0)
1975 goto out; // missing data
1976
1977 /* Failed to decode this stream (e.g. too large request)
1978 * but the HPACK decompressor is still synchronized.
1979 */
1980 h2s = (struct h2s*)h2_error_stream;
1981 goto send_rst;
1982 }
1983
Willy Tarreau22de8d32018-09-05 19:55:58 +02001984 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001985 * positively from h2c_frt_stream_new(), the stream will report the error,
1986 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001987 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001988 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001990 h2s = (struct h2s*)h2_refused_stream;
1991 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001992 }
1993
1994 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001995 h2s->rxbuf = rxbuf;
1996 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001997 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001998
Willy Tarreau88d138e2019-01-02 19:38:14 +01001999 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002000 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002001 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002002
2003 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002004 if (h2s->cs)
2005 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002006 if (h2s->st == H2_SS_OPEN)
2007 h2s->st = H2_SS_HREM;
2008 else
2009 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002010 }
2011
Willy Tarreau3a429f02019-01-03 11:41:50 +01002012 /* update the max stream ID if the request is being processed */
2013 if (h2s->id > h2c->max_id)
2014 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002015
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002016 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002017
2018 conn_err:
2019 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002020 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002021
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002022 out:
2023 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002024 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002025
2026 send_rst:
2027 /* make the demux send an RST for the current stream. We may only
2028 * do this if we're certain that the HEADERS frame was properly
2029 * decompressed so that the HPACK decoder is still kept up to date.
2030 */
2031 h2_release_buf(h2c, &rxbuf);
2032 h2c->st0 = H2_CS_FRAME_E;
2033 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002034}
2035
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002036/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2037 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2038 * errors here are reported as connection errors since it's impossible to
2039 * recover from such errors after the compression context has been altered.
2040 */
2041static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2042{
2043 int error;
2044
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002045 if (!b_size(&h2c->dbuf))
2046 return NULL; // empty buffer
2047
2048 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2049 return NULL; // incomplete frame
2050
Willy Tarreau1915ca22019-01-24 11:49:37 +01002051 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052
Willy Tarreau25919232019-01-03 14:48:18 +01002053 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 if (h2c->st0 >= H2_CS_ERROR)
2055 return NULL;
2056
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002057 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2058 /* RFC7540#5.1 */
2059 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2060 h2c->st0 = H2_CS_FRAME_E;
2061 return NULL;
2062 }
2063
Willy Tarreau25919232019-01-03 14:48:18 +01002064 if (error <= 0) {
2065 if (error == 0)
2066 return NULL; // missing data
2067
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002068 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002069 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002070 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002071 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002072 }
2073
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002074 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2075 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002076 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002077 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002078 }
2079
Willy Tarreau927b88b2019-03-04 08:03:25 +01002080 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002081 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002082 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 +02002083 h2s->st = H2_SS_HREM;
Willy Tarreau201fe402019-05-07 18:10:10 +02002084 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 +02002085 h2s_close(h2s);
2086
2087 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002088}
2089
Willy Tarreau454f9052017-10-26 19:40:35 +02002090/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2091 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2092 */
2093static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2094{
2095 int error;
2096
2097 /* note that empty DATA frames are perfectly valid and sometimes used
2098 * to signal an end of stream (with the ES flag).
2099 */
2100
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002101 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002102 return 0; // empty buffer
2103
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002104 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002105 return 0; // incomplete frame
2106
2107 /* now either the frame is complete or the buffer is complete */
2108
Willy Tarreau454f9052017-10-26 19:40:35 +02002109 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2110 /* RFC7540#6.1 */
2111 error = H2_ERR_STREAM_CLOSED;
2112 goto strm_err;
2113 }
2114
Willy Tarreau1915ca22019-01-24 11:49:37 +01002115 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2116 /* RFC7540#8.1.2 */
2117 error = H2_ERR_PROTOCOL_ERROR;
2118 goto strm_err;
2119 }
2120
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002121 if (!h2_frt_transfer_data(h2s))
2122 return 0;
2123
Willy Tarreau454f9052017-10-26 19:40:35 +02002124 /* call the upper layers to process the frame, then let the upper layer
2125 * notify the stream about any change.
2126 */
2127 if (!h2s->cs) {
2128 error = H2_ERR_STREAM_CLOSED;
2129 goto strm_err;
2130 }
2131
Willy Tarreau8f650c32017-11-21 19:36:21 +01002132 if (h2c->st0 >= H2_CS_ERROR)
2133 return 0;
2134
Willy Tarreau721c9742017-11-07 11:05:42 +01002135 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002136 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002137 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002138 }
2139
2140 /* check for completion : the callee will change this to FRAME_A or
2141 * FRAME_H once done.
2142 */
2143 if (h2c->st0 == H2_CS_FRAME_P)
2144 return 0;
2145
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002146 /* last frame */
2147 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002148 if (h2s->cs)
2149 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002150 if (h2s->st == H2_SS_OPEN)
2151 h2s->st = H2_SS_HREM;
2152 else
2153 h2s_close(h2s);
2154
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002155 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002156
2157 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2158 /* RFC7540#8.1.2 */
2159 error = H2_ERR_PROTOCOL_ERROR;
2160 goto strm_err;
2161 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002162 }
2163
Willy Tarreau454f9052017-10-26 19:40:35 +02002164 return 1;
2165
Willy Tarreau454f9052017-10-26 19:40:35 +02002166 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002167 h2s_error(h2s, error);
2168 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002169 return 0;
2170}
2171
Willy Tarreaubc933932017-10-09 16:21:43 +02002172/* process Rx frames to be demultiplexed */
2173static void h2_process_demux(struct h2c *h2c)
2174{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002175 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002176 struct h2_fh hdr;
2177 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002178
Willy Tarreau081d4722017-05-16 21:51:05 +02002179 if (h2c->st0 >= H2_CS_ERROR)
2180 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002181
2182 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2183 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002184 if (h2c->flags & H2_CF_IS_BACK)
2185 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002186 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2187 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002188 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002189 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002190 sess_log(h2c->conn->owner);
2191 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002192 goto fail;
2193 }
2194
2195 h2c->max_id = 0;
2196 h2c->st0 = H2_CS_SETTINGS1;
2197 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002198
2199 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002200 /* ensure that what is pending is a valid SETTINGS frame
2201 * without an ACK.
2202 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002203 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002204 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002205 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002206 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002207 sess_log(h2c->conn->owner);
2208 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002209 goto fail;
2210 }
2211
2212 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2213 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2214 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2215 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002216 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002217 goto fail;
2218 }
2219
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002220 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002221 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2222 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2223 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002224 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002225 goto fail;
2226 }
2227
Willy Tarreau3bf69182018-12-21 15:34:50 +01002228 /* that's OK, switch to FRAME_P to process it. This is
2229 * a SETTINGS frame whose header has already been
2230 * deleted above.
2231 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002232 padlen = 0;
2233 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002234 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002235 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236
2237 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002238 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002239 int ret = 0;
2240
2241 if (h2c->st0 >= H2_CS_ERROR)
2242 break;
2243
2244 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002245 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002246 break;
2247
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002248 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002249 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002250 if (!h2c->nb_streams) {
2251 /* only log if no other stream can report the error */
2252 sess_log(h2c->conn->owner);
2253 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002254 break;
2255 }
2256
Willy Tarreau3bf69182018-12-21 15:34:50 +01002257 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2258 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2259 * we read the pad length and drop it from the remaining
2260 * payload (one byte + the 9 remaining ones = 10 total
2261 * removed), so we have a frame payload starting after the
2262 * pad len. Flow controlled frames (DATA) also count the
2263 * padlen in the flow control, so it must be adjusted.
2264 */
2265 if (hdr.len < 1) {
2266 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2267 sess_log(h2c->conn->owner);
2268 goto fail;
2269 }
2270 hdr.len--;
2271
2272 if (b_data(&h2c->dbuf) < 10)
2273 break; // missing padlen
2274
2275 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2276
2277 if (padlen > hdr.len) {
2278 /* RFC7540#6.1 : pad length = length of
2279 * frame payload or greater => error.
2280 */
2281 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2282 sess_log(h2c->conn->owner);
2283 goto fail;
2284 }
2285
2286 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2287 h2c->rcvd_c++;
2288 h2c->rcvd_s++;
2289 }
2290 b_del(&h2c->dbuf, 1);
2291 }
2292 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002293
2294 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002295 h2c->dfl = hdr.len;
2296 h2c->dsi = hdr.sid;
2297 h2c->dft = hdr.ft;
2298 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002299 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002300 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002301
2302 /* check for minimum basic frame format validity */
2303 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2304 if (ret != H2_ERR_NO_ERROR) {
2305 h2c_error(h2c, ret);
2306 sess_log(h2c->conn->owner);
2307 goto fail;
2308 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002309 }
2310
2311 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002312 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2313
Willy Tarreau567beb82018-12-18 16:52:44 +01002314 if (tmp_h2s != h2s && h2s && h2s->cs &&
2315 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002316 (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 +01002317 /* we may have to signal the upper layers */
2318 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002319 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002320 }
2321 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002322
Willy Tarreaud7901432017-12-29 11:34:40 +01002323 if (h2c->st0 == H2_CS_FRAME_E)
2324 goto strm_err;
2325
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002326 if (h2s->st == H2_SS_IDLE &&
2327 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2328 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2329 * this state MUST be treated as a connection error
2330 */
2331 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002332 if (!h2c->nb_streams) {
2333 /* only log if no other stream can report the error */
2334 sess_log(h2c->conn->owner);
2335 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002336 break;
2337 }
2338
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002339 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2340 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2341 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002342 * this state MUST be treated as a stream error.
2343 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2344 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002345 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002346 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2347 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2348 else
2349 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002350 goto strm_err;
2351 }
2352
Willy Tarreauab837502017-12-27 15:07:30 +01002353 /* Below the management of frames received in closed state is a
2354 * bit hackish because the spec makes strong differences between
2355 * streams closed by receiving RST, sending RST, and seeing ES
2356 * in both directions. In addition to this, the creation of a
2357 * new stream reusing the identifier of a closed one will be
2358 * detected here. Given that we cannot keep track of all closed
2359 * streams forever, we consider that unknown closed streams were
2360 * closed on RST received, which allows us to respond with an
2361 * RST without breaking the connection (eg: to abort a transfer).
2362 * Some frames have to be silently ignored as well.
2363 */
2364 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002365 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002366 /* #5.1.1: The identifier of a newly
2367 * established stream MUST be numerically
2368 * greater than all streams that the initiating
2369 * endpoint has opened or reserved. This
2370 * governs streams that are opened using a
2371 * HEADERS frame and streams that are reserved
2372 * using PUSH_PROMISE. An endpoint that
2373 * receives an unexpected stream identifier
2374 * MUST respond with a connection error.
2375 */
2376 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2377 goto strm_err;
2378 }
2379
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002380 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002381 /* RFC7540#5.1:closed: an endpoint that
2382 * receives any frame other than PRIORITY after
2383 * receiving a RST_STREAM MUST treat that as a
2384 * stream error of type STREAM_CLOSED.
2385 *
2386 * Note that old streams fall into this category
2387 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002388 *
2389 * However, we cannot generalize this to all frame types. Those
2390 * carrying compression state must still be processed before
2391 * being dropped or we'll desynchronize the decoder. This can
2392 * happen with request trailers received after sending an
2393 * RST_STREAM, or with header/trailers responses received after
2394 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002395 */
2396 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2397 h2c->st0 = H2_CS_FRAME_E;
2398 goto strm_err;
2399 }
2400
2401 /* RFC7540#5.1:closed: if this state is reached as a
2402 * result of sending a RST_STREAM frame, the peer that
2403 * receives the RST_STREAM might have already sent
2404 * frames on the stream that cannot be withdrawn. An
2405 * endpoint MUST ignore frames that it receives on
2406 * closed streams after it has sent a RST_STREAM
2407 * frame. An endpoint MAY choose to limit the period
2408 * over which it ignores frames and treat frames that
2409 * arrive after this time as being in error.
2410 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002411 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002412 /* RFC7540#5.1:closed: any frame other than
2413 * PRIO/WU/RST in this state MUST be treated as
2414 * a connection error
2415 */
2416 if (h2c->dft != H2_FT_RST_STREAM &&
2417 h2c->dft != H2_FT_PRIORITY &&
2418 h2c->dft != H2_FT_WINDOW_UPDATE) {
2419 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2420 goto strm_err;
2421 }
2422 }
2423 }
2424
Willy Tarreauc0da1962017-10-30 18:38:00 +01002425#if 0
2426 // problem below: it is not possible to completely ignore such
2427 // streams as we need to maintain the compression state as well
2428 // and for this we need to completely process these frames (eg:
2429 // HEADERS frames) as well as counting DATA frames to emit
2430 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2431 // This is a typical case of layer violation where the
2432 // transported contents are critical to the connection's
2433 // validity and must be ignored at the same time :-(
2434
2435 /* graceful shutdown, ignore streams whose ID is higher than
2436 * the one advertised in GOAWAY. RFC7540#6.8.
2437 */
2438 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002439 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2440 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002441 h2c->dfl -= ret;
2442 ret = h2c->dfl == 0;
2443 goto strm_err;
2444 }
2445#endif
2446
Willy Tarreau7e98c052017-10-10 15:56:59 +02002447 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002448 case H2_FT_SETTINGS:
2449 if (h2c->st0 == H2_CS_FRAME_P)
2450 ret = h2c_handle_settings(h2c);
2451
2452 if (h2c->st0 == H2_CS_FRAME_A)
2453 ret = h2c_ack_settings(h2c);
2454 break;
2455
Willy Tarreaucf68c782017-10-10 17:11:41 +02002456 case H2_FT_PING:
2457 if (h2c->st0 == H2_CS_FRAME_P)
2458 ret = h2c_handle_ping(h2c);
2459
2460 if (h2c->st0 == H2_CS_FRAME_A)
2461 ret = h2c_ack_ping(h2c);
2462 break;
2463
Willy Tarreau26f95952017-07-27 17:18:30 +02002464 case H2_FT_WINDOW_UPDATE:
2465 if (h2c->st0 == H2_CS_FRAME_P)
2466 ret = h2c_handle_window_update(h2c, h2s);
2467 break;
2468
Willy Tarreau61290ec2017-10-17 08:19:21 +02002469 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002470 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2471 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2472 * frames' parsers consume all following CONTINUATION
2473 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002474 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002475 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2476 sess_log(h2c->conn->owner);
2477 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002478
Willy Tarreau13278b42017-10-13 19:23:14 +02002479 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002480 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002481 if (h2c->flags & H2_CF_IS_BACK)
2482 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2483 else
2484 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002485 if (tmp_h2s) {
2486 h2s = tmp_h2s;
2487 ret = 1;
2488 }
2489 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002490 break;
2491
Willy Tarreau454f9052017-10-26 19:40:35 +02002492 case H2_FT_DATA:
2493 if (h2c->st0 == H2_CS_FRAME_P)
2494 ret = h2c_frt_handle_data(h2c, h2s);
2495
2496 if (h2c->st0 == H2_CS_FRAME_A)
2497 ret = h2c_send_strm_wu(h2c);
2498 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002499
Willy Tarreau92153fc2017-12-03 19:46:19 +01002500 case H2_FT_PRIORITY:
2501 if (h2c->st0 == H2_CS_FRAME_P)
2502 ret = h2c_handle_priority(h2c);
2503 break;
2504
Willy Tarreaucd234e92017-08-18 10:59:39 +02002505 case H2_FT_RST_STREAM:
2506 if (h2c->st0 == H2_CS_FRAME_P)
2507 ret = h2c_handle_rst_stream(h2c, h2s);
2508 break;
2509
Willy Tarreaue96b0922017-10-30 00:28:29 +01002510 case H2_FT_GOAWAY:
2511 if (h2c->st0 == H2_CS_FRAME_P)
2512 ret = h2c_handle_goaway(h2c);
2513 break;
2514
Willy Tarreau1c661982017-10-30 13:52:01 +01002515 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002516 default:
2517 /* drop frames that we ignore. They may be larger than
2518 * the buffer so we drain all of their contents until
2519 * we reach the end.
2520 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002521 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2522 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002523 h2c->dfl -= ret;
2524 ret = h2c->dfl == 0;
2525 }
2526
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002527 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002528 /* We may have to send an RST if not done yet */
2529 if (h2s->st == H2_SS_ERROR)
2530 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002531
Willy Tarreaua20a5192017-12-27 11:02:06 +01002532 if (h2c->st0 == H2_CS_FRAME_E)
2533 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002534
Willy Tarreau7e98c052017-10-10 15:56:59 +02002535 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002536 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002537 break;
2538
2539 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002540 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002541 h2c->st0 = H2_CS_FRAME_H;
2542 }
2543 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002544
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002545 if (h2c->rcvd_c > 0 &&
2546 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2547 h2c_send_conn_wu(h2c);
2548
Willy Tarreau52eed752017-09-22 15:05:09 +02002549 fail:
2550 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002551 if (h2s && h2s->cs &&
2552 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002553 (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 +01002554 /* we may have to signal the upper layers */
2555 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002556 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002557 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002558
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002559 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002560}
2561
2562/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2563 * the end.
2564 */
2565static int h2_process_mux(struct h2c *h2c)
2566{
Olivier Houchard998410a2019-04-15 19:23:37 +02002567 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002568
Willy Tarreau01b44822018-10-03 14:26:37 +02002569 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2570 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2571 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2572 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2573 if (h2c->st0 == H2_CS_ERROR) {
2574 h2c->st0 = H2_CS_ERROR2;
2575 sess_log(h2c->conn->owner);
2576 }
2577 goto fail;
2578 }
2579 h2c->st0 = H2_CS_SETTINGS1;
2580 }
2581 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002582 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002583 return 1;
2584 }
2585
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002586 /* start by sending possibly pending window updates */
2587 if (h2c->rcvd_c > 0 &&
2588 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2589 h2c_send_conn_wu(h2c) < 0)
2590 goto fail;
2591
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002592 /* First we always process the flow control list because the streams
2593 * waiting there were already elected for immediate emission but were
2594 * blocked just on this.
2595 */
2596
Olivier Houchard998410a2019-04-15 19:23:37 +02002597 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002598 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2599 h2c->st0 >= H2_CS_ERROR)
2600 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002601
Willy Tarreauc234ae32019-05-13 17:56:11 +02002602 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002603 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002604
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002605 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002606 /* For some reason, the upper layer failed to subsribe again,
2607 * so remove it from the send_list
2608 */
2609 if (!h2s->send_wait) {
2610 LIST_DEL_INIT(&h2s->list);
2611 continue;
2612 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002613 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002614 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002615 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002616 }
2617
Olivier Houchard998410a2019-04-15 19:23:37 +02002618 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002619 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2620 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002621
Willy Tarreauc234ae32019-05-13 17:56:11 +02002622 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002623 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002624
Olivier Houchard998410a2019-04-15 19:23:37 +02002625 /* For some reason, the upper layer failed to subsribe again,
2626 * so remove it from the send_list
2627 */
2628 if (!h2s->send_wait) {
2629 LIST_DEL_INIT(&h2s->list);
2630 continue;
2631 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002632 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002633 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002634 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002635 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002636 }
2637
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002638 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002639 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002640 if (h2c->st0 == H2_CS_ERROR) {
2641 if (h2c->max_id >= 0) {
2642 h2c_send_goaway_error(h2c, NULL);
2643 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2644 return 0;
2645 }
2646
2647 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2648 }
2649 return 1;
2650 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002651 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002652}
2653
Willy Tarreau62f52692017-10-08 23:01:42 +02002654
Willy Tarreau479998a2018-11-18 06:30:59 +01002655/* Attempt to read data, and subscribe if none available.
2656 * The function returns 1 if data has been received, otherwise zero.
2657 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002658static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002659{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002660 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002661 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002662 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002663 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002664
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002665 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002666 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002667
Willy Tarreau315d8072017-12-10 22:17:57 +01002668 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002669 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002670
Willy Tarreau44e973f2018-03-01 17:49:30 +01002671 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002672 if (!buf) {
2673 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002674 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002675 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002676
Olivier Houchard7505f942018-08-21 18:10:44 +02002677 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002678 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002679 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2680 /* HTX in use : try to pre-align the buffer like the
2681 * rxbufs will be to optimize memory copies. We'll make
2682 * sure that the frame header lands at the end of the
2683 * HTX block to alias it upon recv. We cannot use the
2684 * head because rcv_buf() will realign the buffer if
2685 * it's empty. Thus we cheat and pretend we already
2686 * have a few bytes there.
2687 */
2688 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002689 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002690 }
2691 else
2692 max = b_room(buf);
2693
Olivier Houchard7505f942018-08-21 18:10:44 +02002694 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002695 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002696 else
2697 ret = 0;
2698 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002699
Olivier Houchard53216e72018-10-10 15:46:36 +02002700 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002701 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002702
Olivier Houcharda1411e62018-08-17 18:42:48 +02002703 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002704 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002705 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002706 }
2707
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002708 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002709 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002710 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002711}
2712
Willy Tarreau479998a2018-11-18 06:30:59 +01002713/* Try to send data if possible.
2714 * The function returns 1 if data have been sent, otherwise zero.
2715 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002716static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002717{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002718 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002719 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002720 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002721
2722 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002723 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002724
Olivier Houchard7505f942018-08-21 18:10:44 +02002725
Willy Tarreaua2af5122017-10-09 11:56:46 +02002726 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2727 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002728 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002729 }
2730
Willy Tarreaubc933932017-10-09 16:21:43 +02002731 /* This loop is quite simple : it tries to fill as much as it can from
2732 * pending streams into the existing buffer until it's reportedly full
2733 * or the end of send requests is reached. Then it tries to send this
2734 * buffer's contents out, marks it not full if at least one byte could
2735 * be sent, and tries again.
2736 *
2737 * The snd_buf() function normally takes a "flags" argument which may
2738 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2739 * data immediately comes and CO_SFL_STREAMER to indicate that the
2740 * connection is streaming lots of data (used to increase TLS record
2741 * size at the expense of latency). The former can be sent any time
2742 * there's a buffer full flag, as it indicates at least one stream
2743 * attempted to send and failed so there are pending data. An
2744 * alternative would be to set it as long as there's an active stream
2745 * but that would be problematic for ACKs until we have an absolute
2746 * guarantee that all waiters have at least one byte to send. The
2747 * latter should possibly not be set for now.
2748 */
2749
2750 done = 0;
2751 while (!done) {
2752 unsigned int flags = 0;
2753
2754 /* fill as much as we can into the current buffer */
2755 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2756 done = h2_process_mux(h2c);
2757
Olivier Houchard2b094432019-01-29 18:28:36 +01002758 if (h2c->flags & H2_CF_MUX_MALLOC)
2759 break;
2760
Willy Tarreaubc933932017-10-09 16:21:43 +02002761 if (conn->flags & CO_FL_ERROR)
2762 break;
2763
2764 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2765 flags |= CO_SFL_MSG_MORE;
2766
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002767 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002768 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002769 if (!ret)
2770 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002771 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002772 b_del(&h2c->mbuf, ret);
2773 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002774 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002775
2776 /* wrote at least one byte, the buffer is not full anymore */
2777 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2778 }
2779
Willy Tarreaua2af5122017-10-09 11:56:46 +02002780 if (conn->flags & CO_FL_SOCK_WR_SH) {
2781 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002782 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002783 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002784 /* We're not full anymore, so we can wake any task that are waiting
2785 * for us.
2786 */
2787 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002788 struct h2s *h2s;
2789
2790 list_for_each_entry(h2s, &h2c->send_list, list) {
2791 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2792 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002793
Willy Tarreauc234ae32019-05-13 17:56:11 +02002794 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002795 continue;
2796
Olivier Houchard998410a2019-04-15 19:23:37 +02002797 /* For some reason, the upper layer failed to subsribe again,
2798 * so remove it from the send_list
2799 */
2800 if (!h2s->send_wait) {
2801 LIST_DEL_INIT(&h2s->list);
2802 continue;
2803 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002804 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002805 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002806 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002807 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002808 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002809 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002810 /* We're done, no more to send */
2811 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002812 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002813schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002814 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002815 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002816 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002817}
2818
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002819/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002820static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2821{
2822 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002823 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002824
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002825 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002826 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002827 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002828 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002829 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002830 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002831 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002832}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002833
Willy Tarreau62f52692017-10-08 23:01:42 +02002834/* callback called on any event by the connection handler.
2835 * It applies changes and returns zero, or < 0 if it wants immediate
2836 * destruction of the connection (which normally doesn not happen in h2).
2837 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002838static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002839{
Olivier Houchard7505f942018-08-21 18:10:44 +02002840 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002841
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002842 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002843 h2_process_demux(h2c);
2844
2845 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002847
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002849 h2c->flags &= ~H2_CF_DEM_DFULL;
2850 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002851 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002852
Willy Tarreau0b37d652018-10-03 10:33:02 +02002853 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002854 /* frontend is stopping, reload likely in progress, let's try
2855 * to announce a graceful shutdown if not yet done. We don't
2856 * care if it fails, it will be tried again later.
2857 */
2858 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2859 if (h2c->last_sid < 0)
2860 h2c->last_sid = (1U << 31) - 1;
2861 h2c_send_goaway_error(h2c, NULL);
2862 }
2863 }
2864
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002865 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002866 * If we received early data, and the handshake is done, wake
2867 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002868 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002869 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2870 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2871 struct eb32_node *node;
2872 struct h2s *h2s;
2873
2874 h2c->flags |= H2_CF_WAIT_FOR_HS;
2875 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2876
2877 while (node) {
2878 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002879 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002880 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002881 node = eb32_next(node);
2882 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002883 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002884
Willy Tarreau26bd7612017-10-09 16:47:04 +02002885 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002886 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2887 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2888 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002889 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002890
2891 if (eb_is_empty(&h2c->streams_by_id)) {
2892 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002893 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002894 return -1;
2895 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002896 }
2897
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002898 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002899 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002900
Olivier Houchard53216e72018-10-10 15:46:36 +02002901 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2902 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2903 (h2c->st0 != H2_CS_ERROR &&
2904 !b_data(&h2c->mbuf) &&
2905 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2906 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002907 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002908
Willy Tarreau3f133572017-10-31 19:21:06 +01002909 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002910 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002911 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002912 task_queue(h2c->task);
2913 }
2914 else
2915 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002916 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002917
Olivier Houchard7505f942018-08-21 18:10:44 +02002918 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002919 return 0;
2920}
2921
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002922/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002923static int h2_wake(struct connection *conn)
2924{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002925 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002926
2927 return (h2_process(h2c));
2928}
2929
Willy Tarreauea392822017-10-31 10:02:25 +01002930/* Connection timeout management. The principle is that if there's no receipt
2931 * nor sending for a certain amount of time, the connection is closed. If the
2932 * MUX buffer still has lying data or is not allocatable, the connection is
2933 * immediately killed. If it's allocatable and empty, we attempt to send a
2934 * GOAWAY frame.
2935 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002936static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002937{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002938 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002939 int expired = tick_is_expired(t->expire, now_ms);
2940
Willy Tarreau0975f112018-03-29 15:22:59 +02002941 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002942 return t;
2943
Olivier Houchard3f795f72019-04-17 22:51:06 +02002944 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002945
2946 if (!h2c) {
2947 /* resources were already deleted */
2948 return NULL;
2949 }
2950
2951 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002952 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02002953 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01002954
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002955 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002956 /* don't even try to send a GOAWAY, the buffer is stuck */
2957 h2c->flags |= H2_CF_GOAWAY_FAILED;
2958 }
2959
2960 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002961 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002962 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2963 h2c->flags |= H2_CF_GOAWAY_FAILED;
2964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002966 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 +02002967 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002968 b_del(&h2c->mbuf, ret);
2969 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002970 }
2971 }
Willy Tarreauea392822017-10-31 10:02:25 +01002972
Willy Tarreau0975f112018-03-29 15:22:59 +02002973 /* either we can release everything now or it will be done later once
2974 * the last stream closes.
2975 */
2976 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002977 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002978
Willy Tarreauea392822017-10-31 10:02:25 +01002979 return NULL;
2980}
2981
2982
Willy Tarreau62f52692017-10-08 23:01:42 +02002983/*******************************************/
2984/* functions below are used by the streams */
2985/*******************************************/
2986
2987/*
2988 * Attach a new stream to a connection
2989 * (Used for outgoing connections)
2990 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002991static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002992{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002993 struct conn_stream *cs;
2994 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002995 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002996
2997 cs = cs_new(conn);
2998 if (!cs)
2999 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003000 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003001 if (!h2s) {
3002 cs_free(cs);
3003 return NULL;
3004 }
3005 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003006}
3007
Willy Tarreaufafd3982018-11-18 21:29:20 +01003008/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3009 * We have to scan because we may have some orphan streams. It might be
3010 * beneficial to scan backwards from the end to reduce the likeliness to find
3011 * orphans.
3012 */
3013static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3014{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003015 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003016 struct h2s *h2s;
3017 struct eb32_node *node;
3018
3019 node = eb32_first(&h2c->streams_by_id);
3020 while (node) {
3021 h2s = container_of(node, struct h2s, by_id);
3022 if (h2s->cs)
3023 return h2s->cs;
3024 node = eb32_next(node);
3025 }
3026 return NULL;
3027}
3028
Willy Tarreau62f52692017-10-08 23:01:42 +02003029/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003030 * Destroy the mux and the associated connection, if it is no longer used
3031 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003032static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003033{
Christopher Faulet73c12072019-04-08 11:23:22 +02003034 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003035
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003036 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003037 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003038}
3039
3040/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003041 * Detach the stream from the connection and possibly release the connection.
3042 */
3043static void h2_detach(struct conn_stream *cs)
3044{
Willy Tarreau60935142017-10-16 18:11:19 +02003045 struct h2s *h2s = cs->ctx;
3046 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003047 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003048
3049 cs->ctx = NULL;
3050 if (!h2s)
3051 return;
3052
Olivier Houchard998410a2019-04-15 19:23:37 +02003053 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003054 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003055 h2s->send_wait != &h2s->wait_event) {
3056 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3057 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003058 /*
3059 * At this point, the stream_interface is supposed to have called
3060 * h2_unsubscribe(), so the only way there's still a
3061 * subscription that came from the stream_interface (as we
3062 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3063 * without the stream_interface involved) is that we subscribed
3064 * for sending, we woke the tasklet up and removed the
3065 * SUB_RETRY_SEND flag, so the stream_interface would not
3066 * know it has to unsubscribe for send, but the tasklet hasn't
3067 * run yet. Make sure to handle that by explicitely setting
3068 * send_wait to NULL, as nothing else will do it for us.
3069 */
3070 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003071 }
3072
Olivier Houchardf502aca2018-12-14 19:42:40 +01003073 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003074 h2c = h2s->h2c;
3075 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003076 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003077 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3078 !h2_frt_has_too_many_cs(h2c)) {
3079 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003080 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003081 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003082 }
Willy Tarreau60935142017-10-16 18:11:19 +02003083
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003084 /* this stream may be blocked waiting for some data to leave (possibly
3085 * an ES or RST frame), so orphan it in this case.
3086 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003087 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003088 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003089 (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 +01003090 return;
3091
Willy Tarreau45f752e2017-10-30 15:44:59 +01003092 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3093 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3094 /* unblock the connection if it was blocked on this
3095 * stream.
3096 */
3097 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3098 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003099 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003100 }
3101
Willy Tarreau71049cc2018-03-28 13:56:39 +02003102 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003103
Olivier Houchard8a786902018-12-15 16:05:40 +01003104 if (h2c->flags & H2_CF_IS_BACK &&
3105 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003106 if (!(h2c->conn->flags &
3107 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3108 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003109 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003110 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3111 h2c->conn->owner = NULL;
3112 if (eb_is_empty(&h2c->streams_by_id)) {
3113 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3114 /* The server doesn't want it, let's kill the connection right away */
3115 h2c->conn->mux->destroy(h2c->conn);
3116 return;
3117 }
3118 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003119 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003120 if (eb_is_empty(&h2c->streams_by_id)) {
3121 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3122 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3123 return;
3124 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003125 /* Never ever allow to reuse a connection from a non-reuse backend */
3126 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3127 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003128 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003129 struct server *srv = objt_server(h2c->conn->target);
3130
3131 if (srv) {
3132 if (h2c->conn->flags & CO_FL_PRIVATE)
3133 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3134 else
3135 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3136 }
3137
3138 }
3139 }
3140 }
3141
Willy Tarreaue323f342018-03-28 13:51:45 +02003142 /* We don't want to close right now unless we're removing the
3143 * last stream, and either the connection is in error, or it
3144 * reached the ID already specified in a GOAWAY frame received
3145 * or sent (as seen by last_sid >= 0).
3146 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003147 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003148 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003149 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003150 }
3151 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003152 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003153 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3154 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003155 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003156 else
3157 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003158 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003159}
3160
Willy Tarreau88bdba32019-05-13 18:17:53 +02003161/* Performs a synchronous or asynchronous shutr(). */
3162static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003163{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003164 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003165 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003166
Willy Tarreauf983d002019-05-14 10:40:21 +02003167 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003168 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003169
Willy Tarreau18059042019-01-31 19:12:48 +01003170 /* a connstream may require us to immediately kill the whole connection
3171 * for example because of a "tcp-request content reject" rule that is
3172 * normally used to limit abuse. In this case we schedule a goaway to
3173 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003174 */
Willy Tarreau18059042019-01-31 19:12:48 +01003175 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3176 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3177 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3178 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3179 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003180 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3181 /* Nothing was never sent for this stream, so reset with
3182 * REFUSED_STREAM error to let the client retry the
3183 * request.
3184 */
3185 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3186 }
Willy Tarreau18059042019-01-31 19:12:48 +01003187
Willy Tarreau90c32322017-11-24 08:00:30 +01003188 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003189 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003190 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003191
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003192 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003193 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003194 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003195 done:
3196 h2s->flags &= ~H2_SF_WANT_SHUTR;
3197 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003198add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003199 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003200 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003201 if (h2s->flags & H2_SF_BLK_MFCTL) {
3202 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3203 h2s->send_wait = sw;
3204 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3205 h2s->send_wait = sw;
3206 LIST_ADDQ(&h2c->send_list, &h2s->list);
3207 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003208 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003209 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003210 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003211 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003212}
3213
Willy Tarreau88bdba32019-05-13 18:17:53 +02003214/* Performs a synchronous or asynchronous shutw(). */
3215static void 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 Tarreauf983d002019-05-14 10:40:21 +02003220 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003221 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003222
Willy Tarreauf983d002019-05-14 10:40:21 +02003223 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3224 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003225 /* we can cleanly close using an empty data frame only after headers */
3226
3227 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3228 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003229 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003230
3231 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003232 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003233 else
3234 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003235 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003236 /* a connstream may require us to immediately kill the whole connection
3237 * for example because of a "tcp-request content reject" rule that is
3238 * normally used to limit abuse. In this case we schedule a goaway to
3239 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003240 */
Willy Tarreau18059042019-01-31 19:12:48 +01003241 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3242 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3243 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3244 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3245 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003246 else {
3247 /* Nothing was never sent for this stream, so reset with
3248 * REFUSED_STREAM error to let the client retry the
3249 * request.
3250 */
3251 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3252 }
Willy Tarreau18059042019-01-31 19:12:48 +01003253
Willy Tarreau90c32322017-11-24 08:00:30 +01003254 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003255 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003256 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003257
Willy Tarreau00dd0782018-03-01 16:31:34 +01003258 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003259 }
3260
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003261 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003262 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003263 done:
3264 h2s->flags &= ~H2_SF_WANT_SHUTW;
3265 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003266
3267 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003268 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003269 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003270 if (h2s->flags & H2_SF_BLK_MFCTL) {
3271 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3272 h2s->send_wait = sw;
3273 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3274 h2s->send_wait = sw;
3275 LIST_ADDQ(&h2c->send_list, &h2s->list);
3276 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003277 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003278 /* let the handler know we want to shutw */
3279 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003280 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003281}
3282
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003283/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3284 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3285 * and prevented the last frame from being emitted.
3286 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003287static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3288{
3289 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003290 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003291
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003292 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003293 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003294 h2_do_shutw(h2s);
3295
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003296 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003297 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003298
Willy Tarreau88bdba32019-05-13 18:17:53 +02003299 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003300 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003301 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003302
Willy Tarreau88bdba32019-05-13 18:17:53 +02003303 if (!h2s->cs) {
3304 h2s_destroy(h2s);
3305 if (h2c_is_dead(h2c))
3306 h2_release(h2c);
3307 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003308 }
3309
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003310 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003311}
3312
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003313/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003314static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3315{
3316 struct h2s *h2s = cs->ctx;
3317
3318 if (!mode)
3319 return;
3320
3321 h2_do_shutr(h2s);
3322}
3323
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003324/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003325static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3326{
3327 struct h2s *h2s = cs->ctx;
3328
3329 h2_do_shutw(h2s);
3330}
3331
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003332/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003333 * HTX request or response depending on the connection's side. Returns a
3334 * positive value on success, a negative value on failure, or 0 if it couldn't
3335 * proceed. May report connection errors in h2c->errcode if the frame is
3336 * non-decodable and the connection unrecoverable. In absence of connection
3337 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003338 *
3339 * The function may fold CONTINUATION frames into the initial HEADERS frame
3340 * by removing padding and next frame header, then moving the CONTINUATION
3341 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3342 * leaving a hole between the main frame and the beginning of the next one.
3343 * The possibly remaining incomplete or next frame at the end may be moved
3344 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3345 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3346 *
3347 * A buffer at the beginning of processing may look like this :
3348 *
3349 * ,---.---------.-----.--------------.--------------.------.---.
3350 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3351 * `---^---------^-----^--------------^--------------^------^---'
3352 * | | <-----> | |
3353 * area | dpl | wrap
3354 * |<--------------> |
3355 * | dfl |
3356 * |<-------------------------------------------------->|
3357 * head data
3358 *
3359 * Padding is automatically overwritten when folding, participating to the
3360 * hole size after dfl :
3361 *
3362 * ,---.------------------------.-----.--------------.------.---.
3363 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3364 * `---^------------------------^-----^--------------^------^---'
3365 * | | <-----> | |
3366 * area | hole | wrap
3367 * |<-----------------------> |
3368 * | dfl |
3369 * |<-------------------------------------------------->|
3370 * head data
3371 *
3372 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3373 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3374 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003375 *
3376 * The <flags> field must point to either the stream's flags or to a copy of it
3377 * so that the function can update the following flags :
3378 * - H2_SF_DATA_CLEN when content-length is seen
3379 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3380 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003381 *
3382 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3383 * decoding, in order to detect if we're dealing with a headers or a trailers
3384 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003385 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003386static 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 +02003387{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003388 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003389 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003390 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003391 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003392 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003393 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003394 int flen; // header frame len
3395 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003396 int ret = 0;
3397 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003398 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003399 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003400
Willy Tarreauea18f862018-12-22 20:19:26 +01003401next_frame:
3402 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3403 goto leave; // incomplete input frame
3404
3405 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3406 * this case, we'll try to paste it immediately after the initial
3407 * HEADERS frame payload and kill any possible padding. The initial
3408 * frame's length will be increased to represent the concatenation
3409 * of the two frames. The next frame is read from position <tlen>
3410 * and written at position <flen> (minus padding if some is present).
3411 */
3412 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3413 struct h2_fh hdr;
3414 int clen; // CONTINUATION frame's payload length
3415
3416 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3417 /* no more data, the buffer may be full, either due to
3418 * too large a frame or because of too large a hole that
3419 * we're going to compact at the end.
3420 */
3421 goto leave;
3422 }
3423
3424 if (hdr.ft != H2_FT_CONTINUATION) {
3425 /* RFC7540#6.10: frame of unexpected type */
3426 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3427 goto fail;
3428 }
3429
3430 if (hdr.sid != h2c->dsi) {
3431 /* RFC7540#6.10: frame of different stream */
3432 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3433 goto fail;
3434 }
3435
3436 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3437 /* RFC7540#4.2: invalid frame length */
3438 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3439 goto fail;
3440 }
3441
3442 /* detect when we must stop aggragating frames */
3443 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3444
3445 /* Take as much as we can of the CONTINUATION frame's payload */
3446 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3447 if (clen > hdr.len)
3448 clen = hdr.len;
3449
3450 /* Move the frame's payload over the padding, hole and frame
3451 * header. At least one of hole or dpl is null (see diagrams
3452 * above). The hole moves after the new aggragated frame.
3453 */
3454 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3455 h2c->dfl += clen - h2c->dpl;
3456 hole += h2c->dpl + 9;
3457 h2c->dpl = 0;
3458 goto next_frame;
3459 }
3460
3461 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003462
Willy Tarreau13278b42017-10-13 19:23:14 +02003463 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003464 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003465 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003466 copy = alloc_trash_chunk();
3467 if (!copy) {
3468 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3469 goto fail;
3470 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003471 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3472 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3473 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003474 }
3475
Willy Tarreau13278b42017-10-13 19:23:14 +02003476 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3477 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003478 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003479 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3480 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003481 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003482 }
3483
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003484 if (flen < 5) {
3485 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3486 goto fail;
3487 }
3488
Willy Tarreau13278b42017-10-13 19:23:14 +02003489 hdrs += 5; // stream dep = 4, weight = 1
3490 flen -= 5;
3491 }
3492
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003493 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003494 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003495 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003496 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003497
Willy Tarreau937f7602018-02-26 15:22:17 +01003498 /* we can't retry a failed decompression operation so we must be very
3499 * careful not to take any risks. In practice the output buffer is
3500 * always empty except maybe for trailers, in which case we simply have
3501 * to wait for the upper layer to finish consuming what is available.
3502 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003503
3504 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003505 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003506 if (!htx_is_empty(htx)) {
3507 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003508 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003509 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003510 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003511 if (b_data(rxbuf)) {
3512 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003513 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003514 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003515
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003516 rxbuf->head = 0;
3517 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003518 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003519
Willy Tarreau25919232019-01-03 14:48:18 +01003520 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003521 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3522 sizeof(list)/sizeof(list[0]), tmp);
3523 if (outlen < 0) {
3524 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3525 goto fail;
3526 }
3527
Willy Tarreau25919232019-01-03 14:48:18 +01003528 /* The PACK decompressor was updated, let's update the input buffer and
3529 * the parser's state to commit these changes and allow us to later
3530 * fail solely on the stream if needed.
3531 */
3532 b_del(&h2c->dbuf, h2c->dfl + hole);
3533 h2c->dfl = hole = 0;
3534 h2c->st0 = H2_CS_FRAME_H;
3535
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003536 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003537 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003538
Willy Tarreau88d138e2019-01-02 19:38:14 +01003539 if (*flags & H2_SF_HEADERS_RCVD)
3540 goto trailers;
3541
3542 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003543 if (htx) {
3544 /* HTX mode */
3545 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003546 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003547 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003548 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003549 } else {
3550 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003551 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003552 if (outlen > 0)
3553 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003554 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003555
3556 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003557 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003558 goto fail;
3559 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003560
Willy Tarreau174b06a2018-04-25 18:13:58 +02003561 if (msgf & H2_MSGF_BODY) {
3562 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003563 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003564 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003565 if (htx)
3566 htx->extra = *body_len;
3567 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003568 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003569 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003570 }
3571
Willy Tarreau88d138e2019-01-02 19:38:14 +01003572 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003573 /* indicate that a HEADERS frame was received for this stream, except
3574 * for 1xx responses. For 1xx responses, another HEADERS frame is
3575 * expected.
3576 */
3577 if (!(msgf & H2_MSGF_RSP_1XX))
3578 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003579
Christopher Faulet0b465482019-02-19 15:14:23 +01003580 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003581 /* Mark the end of message, either using EOM in HTX or with the
3582 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003583 * is not set during headers with END_STREAM. For HTX trailers,
3584 * we must not leave an HTX trailers block not followed by an
3585 * EOM block, the two must be atomic. Thus if we fail to emit
3586 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003587 */
3588 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003589 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3590 struct htx_blk *tail = htx_get_tail_blk(htx);
3591
3592 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3593 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003594 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003595 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003596 }
3597 else if (*flags & H2_SF_DATA_CHNK) {
3598 if (!b_putblk(rxbuf, "\r\n", 2))
3599 goto fail;
3600 }
3601 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003602
Willy Tarreau86277d42019-01-02 15:36:11 +01003603 /* success */
3604 ret = 1;
3605
Willy Tarreau68dd9852017-07-03 14:44:26 +02003606 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003607 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003608 * move the remaining data over it.
3609 */
3610 if (hole) {
3611 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3612 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3613 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3614 b_sub(&h2c->dbuf, hole);
3615 }
3616
Willy Tarreau97215ca2019-04-29 10:20:21 +02003617 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003618 /* too large frames */
3619 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003620 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003621 }
3622
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003623 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003624 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003625 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003626 return ret;
3627
Willy Tarreau68dd9852017-07-03 14:44:26 +02003628 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003629 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003630 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003631
3632 trailers:
3633 /* This is the last HEADERS frame hence a trailer */
3634
3635 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3636 /* It's a trailer but it's missing ES flag */
3637 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3638 goto fail;
3639 }
3640
3641 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3642 * block, and when using chunks we must send the 0 CRLF marker. For
3643 * other modes, the trailers are silently dropped.
3644 */
3645 if (htx) {
3646 if (!htx_add_endof(htx, HTX_BLK_EOD))
3647 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003648 if (h2_make_htx_trailers(list, htx) <= 0)
3649 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003650 }
3651 else if (*flags & H2_SF_DATA_CHNK) {
3652 /* Legacy mode with chunked encoding : we must finalize the
3653 * data block message emit the trailing CRLF */
3654 if (!b_putblk(rxbuf, "0\r\n", 3))
3655 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003656
3657 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3658 if (outlen > 0)
3659 b_add(rxbuf, outlen);
3660 else
3661 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003662 }
3663
3664 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003665}
3666
Willy Tarreau454f9052017-10-26 19:40:35 +02003667/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3668 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3669 * in use, a new chunk is emitted for each frame. This is supposed to fit
3670 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3671 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3672 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003673 * parser state is automatically updated. Returns > 0 if it could completely
3674 * send the current frame, 0 if it couldn't complete, in which case
3675 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3676 * DATA frame can return 0 as a valid result). Stream errors are reported in
3677 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3678 * have checked the frame header and ensured that the frame was complete or the
3679 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003680 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003681static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003682{
3683 struct h2c *h2c = h2s->h2c;
3684 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003685 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003686 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003687 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003688 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003689
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003690 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003691
Olivier Houchard638b7992018-08-16 15:41:52 +02003692 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003693 if (!csbuf) {
3694 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003695 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003696 }
3697
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003698try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003699 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003700 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3701 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003702 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003703 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003704
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003705 if (flen > b_data(&h2c->dbuf)) {
3706 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003707 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003708 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003709 }
3710
Willy Tarreaua9b77962019-01-31 07:23:00 +01003711 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003712 block1 = htx_free_data_space(htx);
3713 if (!block1) {
3714 h2c->flags |= H2_CF_DEM_SFULL;
3715 goto fail;
3716 }
3717 if (flen > block1)
3718 flen = block1;
3719
3720 /* here, flen is the max we can copy into the output buffer */
3721 block1 = b_contig_data(&h2c->dbuf, 0);
3722 if (flen > block1)
3723 flen = block1;
3724
3725 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3726 h2c->flags |= H2_CF_DEM_SFULL;
3727 goto fail;
3728 }
3729
3730 b_del(&h2c->dbuf, flen);
3731 h2c->dfl -= flen;
3732 h2c->rcvd_c += flen;
3733 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003734
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003735 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003736 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003737 htx->extra = h2s->body_len;
3738 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003739 goto try_again;
3740 }
3741 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003742 /* it doesn't fit and the buffer is fragmented,
3743 * so let's defragment it and try again.
3744 */
3745 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003746 }
3747
Willy Tarreaueba10f22018-04-25 20:44:22 +02003748 /* chunked-encoding requires more room */
3749 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003750 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003751 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3752 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3753 (chklen < 1048576) ? 4 : 8;
3754 chklen += 4; // CRLF, CRLF
3755 }
3756
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003757 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 if (flen + chklen > b_room(csbuf)) {
3759 if (chklen >= b_room(csbuf)) {
3760 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003761 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003762 }
3763 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003764 }
3765
3766 if (h2s->flags & H2_SF_DATA_CHNK) {
3767 /* emit the chunk size */
3768 unsigned int chksz = flen;
3769 char str[10];
3770 char *beg;
3771
3772 beg = str + sizeof(str);
3773 *--beg = '\n';
3774 *--beg = '\r';
3775 do {
3776 *--beg = hextab[chksz & 0xF];
3777 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003778 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003779 }
3780
Willy Tarreau454f9052017-10-26 19:40:35 +02003781 /* Block1 is the length of the first block before the buffer wraps,
3782 * block2 is the optional second block to reach the end of the frame.
3783 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003784 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003785 if (block1 > flen)
3786 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003787 block2 = flen - block1;
3788
3789 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003790 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003791
3792 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003793 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003794
Willy Tarreaueba10f22018-04-25 20:44:22 +02003795 if (h2s->flags & H2_SF_DATA_CHNK) {
3796 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003797 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003798 }
3799
Willy Tarreau454f9052017-10-26 19:40:35 +02003800 /* now mark the input data as consumed (will be deleted from the buffer
3801 * by the caller when seeing FRAME_A after sending the window update).
3802 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003803 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003804 h2c->dfl -= flen;
3805 h2c->rcvd_c += flen;
3806 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3807
Willy Tarreau1915ca22019-01-24 11:49:37 +01003808 if (h2s->flags & H2_SF_DATA_CLEN)
3809 h2s->body_len -= flen;
3810
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003811 if (h2c->dfl > h2c->dpl) {
3812 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003813 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003814 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003815 }
3816
Willy Tarreau4a28da12018-01-04 14:41:00 +01003817 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003818 /* here we're done with the frame, all the payload (except padding) was
3819 * transferred.
3820 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003821
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003822 if (h2c->dff & H2_F_DATA_END_STREAM) {
3823 if (htx) {
3824 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3825 h2c->flags |= H2_CF_DEM_SFULL;
3826 goto fail;
3827 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003828 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003829 else if (h2s->flags & H2_SF_DATA_CHNK) {
3830 /* emit the trailing 0 CRLF CRLF */
3831 if (b_room(csbuf) < 5) {
3832 h2c->flags |= H2_CF_DEM_SFULL;
3833 goto fail;
3834 }
3835 chklen += 5;
3836 b_putblk(csbuf, "0\r\n\r\n", 5);
3837 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003838 }
3839
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003840 h2c->rcvd_c += h2c->dpl;
3841 h2c->rcvd_s += h2c->dpl;
3842 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003843 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003844 if (htx)
3845 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003846 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003847 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003848 if (htx)
3849 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003850 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003851}
3852
Willy Tarreau5dd17352018-06-14 13:33:30 +02003853/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3854 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3855 * number of bytes sent. The caller must check the stream's status to detect
3856 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003857 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003858static 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 +02003859{
3860 struct http_hdr list[MAX_HTTP_HDR];
3861 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003862 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003863 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003864 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003865 int es_now = 0;
3866 int ret = 0;
3867 int hdr;
3868
3869 if (h2c_mux_busy(h2c, h2s)) {
3870 h2s->flags |= H2_SF_BLK_MBUSY;
3871 return 0;
3872 }
3873
Willy Tarreau44e973f2018-03-01 17:49:30 +01003874 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003875 h2c->flags |= H2_CF_MUX_MALLOC;
3876 h2s->flags |= H2_SF_BLK_MROOM;
3877 return 0;
3878 }
3879
3880 /* First, try to parse the H1 response and index it into <list>.
3881 * NOTE! Since it comes from haproxy, we *know* that a response header
3882 * block does not wrap and we can safely read it this way without
3883 * having to realign the buffer.
3884 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003885 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003886 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003887 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003888 /* incomplete or invalid response, this is abnormal coming from
3889 * haproxy and may only result in a bad errorfile or bad Lua code
3890 * so that won't be fixed, raise an error now.
3891 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003892 * FIXME: we should instead add the ability to only return a
3893 * 502 bad gateway. But in theory this is not supposed to
3894 * happen.
3895 */
3896 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3897 ret = 0;
3898 goto end;
3899 }
3900
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003901 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003902
3903 /* certain statuses have no body or an empty one, regardless of
3904 * what the headers say.
3905 */
3906 if (sl.st.status >= 100 && sl.st.status < 200) {
3907 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3908 h1m->curr_len = h1m->body_len = 0;
3909 }
3910 else if (sl.st.status == 204 || sl.st.status == 304) {
3911 /* no contents, claim c-len is present and set to zero */
3912 h1m->flags &= ~H1_MF_CHNK;
3913 h1m->flags |= H1_MF_CLEN;
3914 h1m->curr_len = h1m->body_len = 0;
3915 }
3916
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003917 chunk_reset(&outbuf);
3918
3919 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003921 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003922 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003924 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925 break;
3926 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003927 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003928 }
3929
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003930 if (outbuf.size < 9)
3931 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003932
3933 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003934 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3935 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3936 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003937
3938 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003939 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003940 /* this is an unparsable response */
3941 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3942 ret = 0;
3943 goto end;
3944 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003945
3946 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003947 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003948 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003949 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003950 }
3951
3952 /* encode all headers, stop at empty name */
3953 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003954 /* these ones do not exist in H2 and must be dropped. */
3955 if (isteq(list[hdr].n, ist("connection")) ||
3956 isteq(list[hdr].n, ist("proxy-connection")) ||
3957 isteq(list[hdr].n, ist("keep-alive")) ||
3958 isteq(list[hdr].n, ist("upgrade")) ||
3959 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003960 continue;
3961
3962 if (isteq(list[hdr].n, ist("")))
3963 break; // end
3964
3965 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3966 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003967 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003968 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003969 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003970 }
3971 }
3972
3973 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003974 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003975 es_now = 1;
3976
3977 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003978 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003979
3980 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003981 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003982
3983 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003984 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003985
3986 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003987 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003988 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003989
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003990 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003991 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003992 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003993
Willy Tarreau801250e2018-09-11 11:45:04 +02003994 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003995 h2s->flags |= H2_SF_ES_SENT;
3996 if (h2s->st == H2_SS_OPEN)
3997 h2s->st = H2_SS_HLOC;
3998 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003999 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004000 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004001 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004002 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004003 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004004 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004005 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004006 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004007 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004008
4009 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004010
4011 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004012 //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 +02004013 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004014 full:
4015 h1m_init_res(h1m);
4016 h1m->err_pos = -1; // don't care about errors on the response path
4017 h2c->flags |= H2_CF_MUX_MFULL;
4018 h2s->flags |= H2_SF_BLK_MROOM;
4019 ret = 0;
4020 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004021}
4022
Willy Tarreau5dd17352018-06-14 13:33:30 +02004023/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4024 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4025 * the number of bytes sent. The caller must check the stream's status to
4026 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004028static 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 +02004029{
4030 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004031 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004032 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004033 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004034 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004035 int es_now = 0;
4036 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004037 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004038 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004039
4040 if (h2c_mux_busy(h2c, h2s)) {
4041 h2s->flags |= H2_SF_BLK_MBUSY;
4042 goto end;
4043 }
4044
Willy Tarreau44e973f2018-03-01 17:49:30 +01004045 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004046 h2c->flags |= H2_CF_MUX_MALLOC;
4047 h2s->flags |= H2_SF_BLK_MROOM;
4048 goto end;
4049 }
4050
4051 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004052 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004053 goto end;
4054
4055 chunk_reset(&outbuf);
4056
4057 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004058 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004059 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004060 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004061
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004062 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004063 break;
4064 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004065 /* If there are pending data in the output buffer, and we have
4066 * less than 1/4 of the mbuf's size and everything fits, we'll
4067 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4068 * is full and wait, to save some slow realign calls.
4069 */
4070 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4071 h2c->flags |= H2_CF_MUX_MFULL;
4072 h2s->flags |= H2_SF_BLK_MROOM;
4073 goto end;
4074 }
4075
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004076 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004077 }
4078
4079 if (outbuf.size < 9) {
4080 h2c->flags |= H2_CF_MUX_MFULL;
4081 h2s->flags |= H2_SF_BLK_MROOM;
4082 goto end;
4083 }
4084
4085 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004086 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4087 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4088 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004089
4090 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4091 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004092 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004093 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004094 break;
4095 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004096 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004097 if ((long long)size > h1m->curr_len)
4098 size = h1m->curr_len;
4099 break;
4100 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004101 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004102 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103 if (!ret)
4104 goto end;
4105
4106 if (ret < 0) {
4107 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004108 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004109 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4110 goto end;
4111 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004112 max -= ret;
4113 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004114 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004115 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004116 }
4117
Willy Tarreau801250e2018-09-11 11:45:04 +02004118 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004119 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004120 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004121 if (!ret)
4122 goto end;
4123
4124 if (ret < 0) {
4125 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004126 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004127 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4128 goto end;
4129 }
4130
4131 size = chunk;
4132 h1m->curr_len = chunk;
4133 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004134 max -= ret;
4135 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004136 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004137 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004138 if (!size)
4139 goto send_empty;
4140 }
4141
4142 /* in MSG_DATA state, continue below */
4143 size = h1m->curr_len;
4144 break;
4145 }
4146
4147 /* we have in <size> the exact number of bytes we need to copy from
4148 * the H1 buffer. We need to check this against the connection's and
4149 * the stream's send windows, and to ensure that this fits in the max
4150 * frame size and in the buffer's available space minus 9 bytes (for
4151 * the frame header). The connection's flow control is applied last so
4152 * that we can use a separate list of streams which are immediately
4153 * unblocked on window opening. Note: we don't implement padding.
4154 */
4155
Willy Tarreau5dd17352018-06-14 13:33:30 +02004156 if (size > max)
4157 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004158
4159 if (size > h2s->mws)
4160 size = h2s->mws;
4161
4162 if (size <= 0) {
4163 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004164 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004165 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166 goto end;
4167 }
4168
4169 if (h2c->mfs && size > h2c->mfs)
4170 size = h2c->mfs;
4171
4172 if (size + 9 > outbuf.size) {
4173 /* we have an opportunity for enlarging the too small
4174 * available space, let's try.
4175 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004176 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004177 goto realign_again;
4178 size = outbuf.size - 9;
4179 }
4180
4181 if (size <= 0) {
4182 h2c->flags |= H2_CF_MUX_MFULL;
4183 h2s->flags |= H2_SF_BLK_MROOM;
4184 goto end;
4185 }
4186
4187 if (size > h2c->mws)
4188 size = h2c->mws;
4189
4190 if (size <= 0) {
4191 h2s->flags |= H2_SF_BLK_MFCTL;
4192 goto end;
4193 }
4194
4195 /* copy whatever we can */
4196 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004197 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004198 if (ret == 1)
4199 len2 = 0;
4200
4201 if (!ret || len1 + len2 < size) {
4202 /* FIXME: must normally never happen */
4203 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4204 goto end;
4205 }
4206
4207 /* limit len1/len2 to size */
4208 if (len1 + len2 > size) {
4209 int sub = len1 + len2 - size;
4210
4211 if (len2 > sub)
4212 len2 -= sub;
4213 else {
4214 sub -= len2;
4215 len2 = 0;
4216 len1 -= sub;
4217 }
4218 }
4219
4220 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004221 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004222 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004223 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004224
4225 send_empty:
4226 /* we may need to add END_STREAM */
4227 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4228 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004229 *
4230 * FIXME: what we do here is not correct because we send end_stream
4231 * before knowing if we'll have to send a HEADERS frame for the
4232 * trailers. More importantly we're not consuming the trailing CRLF
4233 * after the end of trailers, so it will be left to the caller to
4234 * eat it. The right way to do it would be to measure trailers here
4235 * and to send ES only if there are no trailers.
4236 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004237 */
4238 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004239 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004240 es_now = 1;
4241
4242 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004243 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004244
4245 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004246 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004247
4248 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004249 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004250
4251 /* consume incoming H1 response */
4252 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004253 max -= size;
4254 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004255 total += size;
4256 h1m->curr_len -= size;
4257 h2s->mws -= size;
4258 h2c->mws -= size;
4259
4260 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004261 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004262 goto new_frame;
4263 }
4264 }
4265
4266 if (es_now) {
4267 if (h2s->st == H2_SS_OPEN)
4268 h2s->st = H2_SS_HLOC;
4269 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004270 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004271
Willy Tarreau35a62702018-02-27 15:37:25 +01004272 if (!(h1m->flags & H1_MF_CHNK)) {
4273 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004274 total += max;
4275 ofs += max;
4276 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004277
Willy Tarreau801250e2018-09-11 11:45:04 +02004278 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004279 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004280
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004281 h2s->flags |= H2_SF_ES_SENT;
4282 }
4283
4284 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004285 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 +02004286 return total;
4287}
4288
Willy Tarreau115e83b2018-12-01 19:17:53 +01004289/* Try to send a HEADERS frame matching HTX response present in HTX message
4290 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4291 * must check the stream's status to detect any error which might have happened
4292 * subsequently to a successful send. The htx blocks are automatically removed
4293 * from the message. The htx message is assumed to be valid since produced from
4294 * the internal code, hence it contains a start line, an optional series of
4295 * header blocks and an end of header, otherwise an invalid frame could be
4296 * emitted and the resulting htx message could be left in an inconsistent state.
4297 */
4298static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4299{
4300 struct http_hdr list[MAX_HTTP_HDR];
4301 struct h2c *h2c = h2s->h2c;
4302 struct htx_blk *blk;
4303 struct htx_blk *blk_end;
4304 struct buffer outbuf;
4305 struct htx_sl *sl;
4306 enum htx_blk_type type;
4307 int es_now = 0;
4308 int ret = 0;
4309 int hdr;
4310 int idx;
4311
4312 if (h2c_mux_busy(h2c, h2s)) {
4313 h2s->flags |= H2_SF_BLK_MBUSY;
4314 return 0;
4315 }
4316
4317 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4318 h2c->flags |= H2_CF_MUX_MALLOC;
4319 h2s->flags |= H2_SF_BLK_MROOM;
4320 return 0;
4321 }
4322
4323 /* determine the first block which must not be deleted, blk_end may
4324 * be NULL if all blocks have to be deleted.
4325 */
4326 idx = htx_get_head(htx);
4327 blk_end = NULL;
4328 while (idx != -1) {
4329 type = htx_get_blk_type(htx_get_blk(htx, idx));
4330 idx = htx_get_next(htx, idx);
4331 if (type == HTX_BLK_EOH) {
4332 if (idx != -1)
4333 blk_end = htx_get_blk(htx, idx);
4334 break;
4335 }
4336 }
4337
4338 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004339 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004340 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004341 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004342 if (h2s->status < 100 || h2s->status > 999)
4343 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004344
4345 /* and the rest of the headers, that we dump starting at header 0 */
4346 hdr = 0;
4347
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004348 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004349 while ((idx = htx_get_next(htx, idx)) != -1) {
4350 blk = htx_get_blk(htx, idx);
4351 type = htx_get_blk_type(blk);
4352
4353 if (type == HTX_BLK_UNUSED)
4354 continue;
4355
4356 if (type != HTX_BLK_HDR)
4357 break;
4358
4359 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4360 goto fail;
4361
4362 list[hdr].n = htx_get_blk_name(htx, blk);
4363 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004364 hdr++;
4365 }
4366
4367 /* marker for end of headers */
4368 list[hdr].n = ist("");
4369
4370 if (h2s->status == 204 || h2s->status == 304) {
4371 /* no contents, claim c-len is present and set to zero */
4372 es_now = 1;
4373 }
4374
4375 chunk_reset(&outbuf);
4376
4377 while (1) {
4378 outbuf.area = b_tail(&h2c->mbuf);
4379 outbuf.size = b_contig_space(&h2c->mbuf);
4380 outbuf.data = 0;
4381
4382 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4383 break;
4384 realign_again:
4385 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4386 }
4387
4388 if (outbuf.size < 9)
4389 goto full;
4390
4391 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4392 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4393 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4394 outbuf.data = 9;
4395
4396 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004397 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004398 if (b_space_wraps(&h2c->mbuf))
4399 goto realign_again;
4400 goto full;
4401 }
4402
4403 /* encode all headers, stop at empty name */
4404 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4405 /* these ones do not exist in H2 and must be dropped. */
4406 if (isteq(list[hdr].n, ist("connection")) ||
4407 isteq(list[hdr].n, ist("proxy-connection")) ||
4408 isteq(list[hdr].n, ist("keep-alive")) ||
4409 isteq(list[hdr].n, ist("upgrade")) ||
4410 isteq(list[hdr].n, ist("transfer-encoding")))
4411 continue;
4412
4413 if (isteq(list[hdr].n, ist("")))
4414 break; // end
4415
4416 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4417 /* output full */
4418 if (b_space_wraps(&h2c->mbuf))
4419 goto realign_again;
4420 goto full;
4421 }
4422 }
4423
Christopher Faulet0b465482019-02-19 15:14:23 +01004424 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004425 * FIXME: we should also set it when we know for sure that the
4426 * content-length is zero as well as on 204/304
4427 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004428 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4429 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004430 es_now = 1;
4431
Willy Tarreau927b88b2019-03-04 08:03:25 +01004432 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004433 es_now = 1;
4434
4435 /* update the frame's size */
4436 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4437
4438 if (es_now)
4439 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4440
4441 /* commit the H2 response */
4442 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004443
4444 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4445 * 1xx responses, another HEADERS frame is expected.
4446 */
4447 if (h2s->status >= 200 || h2s->status == 101)
4448 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004449
Willy Tarreau115e83b2018-12-01 19:17:53 +01004450 if (es_now) {
4451 h2s->flags |= H2_SF_ES_SENT;
4452 if (h2s->st == H2_SS_OPEN)
4453 h2s->st = H2_SS_HLOC;
4454 else
4455 h2s_close(h2s);
4456 }
4457
4458 /* OK we could properly deliver the response */
4459
4460 /* remove all header blocks including the EOH and compute the
4461 * corresponding size.
4462 *
4463 * FIXME: We should remove everything when es_now is set.
4464 */
4465 ret = 0;
4466 idx = htx_get_head(htx);
4467 blk = htx_get_blk(htx, idx);
4468 while (blk != blk_end) {
4469 ret += htx_get_blksz(blk);
4470 blk = htx_remove_blk(htx, blk);
4471 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004472
4473 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4474 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004475 end:
4476 return ret;
4477 full:
4478 h2c->flags |= H2_CF_MUX_MFULL;
4479 h2s->flags |= H2_SF_BLK_MROOM;
4480 ret = 0;
4481 goto end;
4482 fail:
4483 /* unparsable HTX messages, too large ones to be produced in the local
4484 * list etc go here (unrecoverable errors).
4485 */
4486 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4487 ret = 0;
4488 goto end;
4489}
4490
Willy Tarreau80739692018-10-05 11:35:57 +02004491/* Try to send a HEADERS frame matching HTX request present in HTX message
4492 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4493 * must check the stream's status to detect any error which might have happened
4494 * subsequently to a successful send. The htx blocks are automatically removed
4495 * from the message. The htx message is assumed to be valid since produced from
4496 * the internal code, hence it contains a start line, an optional series of
4497 * header blocks and an end of header, otherwise an invalid frame could be
4498 * emitted and the resulting htx message could be left in an inconsistent state.
4499 */
4500static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4501{
4502 struct http_hdr list[MAX_HTTP_HDR];
4503 struct h2c *h2c = h2s->h2c;
4504 struct htx_blk *blk;
4505 struct htx_blk *blk_end;
4506 struct buffer outbuf;
4507 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004508 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004509 enum htx_blk_type type;
4510 int es_now = 0;
4511 int ret = 0;
4512 int hdr;
4513 int idx;
4514
4515 if (h2c_mux_busy(h2c, h2s)) {
4516 h2s->flags |= H2_SF_BLK_MBUSY;
4517 return 0;
4518 }
4519
4520 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4521 h2c->flags |= H2_CF_MUX_MALLOC;
4522 h2s->flags |= H2_SF_BLK_MROOM;
4523 return 0;
4524 }
4525
4526 /* determine the first block which must not be deleted, blk_end may
4527 * be NULL if all blocks have to be deleted.
4528 */
4529 idx = htx_get_head(htx);
4530 blk_end = NULL;
4531 while (idx != -1) {
4532 type = htx_get_blk_type(htx_get_blk(htx, idx));
4533 idx = htx_get_next(htx, idx);
4534 if (type == HTX_BLK_EOH) {
4535 if (idx != -1)
4536 blk_end = htx_get_blk(htx, idx);
4537 break;
4538 }
4539 }
4540
4541 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004542 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004543 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004544 meth = htx_sl_req_meth(sl);
4545 path = htx_sl_req_uri(sl);
4546
4547 /* and the rest of the headers, that we dump starting at header 0 */
4548 hdr = 0;
4549
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004550 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004551 while ((idx = htx_get_next(htx, idx)) != -1) {
4552 blk = htx_get_blk(htx, idx);
4553 type = htx_get_blk_type(blk);
4554
4555 if (type == HTX_BLK_UNUSED)
4556 continue;
4557
4558 if (type != HTX_BLK_HDR)
4559 break;
4560
4561 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4562 goto fail;
4563
4564 list[hdr].n = htx_get_blk_name(htx, blk);
4565 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004566 hdr++;
4567 }
4568
4569 /* marker for end of headers */
4570 list[hdr].n = ist("");
4571
4572 chunk_reset(&outbuf);
4573
4574 while (1) {
4575 outbuf.area = b_tail(&h2c->mbuf);
4576 outbuf.size = b_contig_space(&h2c->mbuf);
4577 outbuf.data = 0;
4578
4579 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4580 break;
4581 realign_again:
4582 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4583 }
4584
4585 if (outbuf.size < 9)
4586 goto full;
4587
4588 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4589 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4590 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4591 outbuf.data = 9;
4592
4593 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004594 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004595 if (b_space_wraps(&h2c->mbuf))
4596 goto realign_again;
4597 goto full;
4598 }
4599
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004600 /* RFC7540 #8.3: the CONNECT method must have :
4601 * - :authority set to the URI part (host:port)
4602 * - :method set to CONNECT
4603 * - :scheme and :path omitted
4604 */
4605 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4606 /* encode the scheme which is always "https" (or 0x86 for "http") */
4607 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4608 /* output full */
4609 if (b_space_wraps(&h2c->mbuf))
4610 goto realign_again;
4611 goto full;
4612 }
Willy Tarreau80739692018-10-05 11:35:57 +02004613
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004614 /* encode the path, which necessarily is the second one */
4615 if (!hpack_encode_path(&outbuf, path)) {
4616 /* output full */
4617 if (b_space_wraps(&h2c->mbuf))
4618 goto realign_again;
4619 goto full;
4620 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004621
4622 /* look for the Host header and place it in :authority */
4623 auth = ist2(NULL, 0);
4624 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4625 if (isteq(list[hdr].n, ist("")))
4626 break; // end
4627
4628 if (isteq(list[hdr].n, ist("host"))) {
4629 auth = list[hdr].v;
4630 break;
4631 }
4632 }
4633 }
4634 else {
4635 /* for CONNECT, :authority is taken from the path */
4636 auth = path;
4637 }
4638
4639 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4640 /* output full */
4641 if (b_space_wraps(&h2c->mbuf))
4642 goto realign_again;
4643 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004644 }
4645
4646 /* encode all headers, stop at empty name */
4647 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4648 /* these ones do not exist in H2 and must be dropped. */
4649 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004650 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004651 isteq(list[hdr].n, ist("proxy-connection")) ||
4652 isteq(list[hdr].n, ist("keep-alive")) ||
4653 isteq(list[hdr].n, ist("upgrade")) ||
4654 isteq(list[hdr].n, ist("transfer-encoding")))
4655 continue;
4656
4657 if (isteq(list[hdr].n, ist("")))
4658 break; // end
4659
4660 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4661 /* output full */
4662 if (b_space_wraps(&h2c->mbuf))
4663 goto realign_again;
4664 goto full;
4665 }
4666 }
4667
4668 /* we may need to add END_STREAM if we have no body :
4669 * - request already closed, or :
4670 * - no transfer-encoding, and :
4671 * - no content-length or content-length:0
4672 * Fixme: this doesn't take into account CONNECT requests.
4673 */
4674 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4675 es_now = 1;
4676
4677 if (sl->flags & HTX_SL_F_BODYLESS)
4678 es_now = 1;
4679
Willy Tarreau927b88b2019-03-04 08:03:25 +01004680 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004681 es_now = 1;
4682
4683 /* update the frame's size */
4684 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4685
4686 if (es_now)
4687 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4688
4689 /* commit the H2 response */
4690 b_add(&h2c->mbuf, outbuf.data);
4691 h2s->flags |= H2_SF_HEADERS_SENT;
4692 h2s->st = H2_SS_OPEN;
4693
Willy Tarreau80739692018-10-05 11:35:57 +02004694 if (es_now) {
4695 // trim any possibly pending data (eg: inconsistent content-length)
4696 h2s->flags |= H2_SF_ES_SENT;
4697 h2s->st = H2_SS_HLOC;
4698 }
4699
4700 /* remove all header blocks including the EOH and compute the
4701 * corresponding size.
4702 *
4703 * FIXME: We should remove everything when es_now is set.
4704 */
4705 ret = 0;
4706 idx = htx_get_head(htx);
4707 blk = htx_get_blk(htx, idx);
4708 while (blk != blk_end) {
4709 ret += htx_get_blksz(blk);
4710 blk = htx_remove_blk(htx, blk);
4711 }
4712
4713 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4714 htx_remove_blk(htx, blk_end);
4715
4716 end:
4717 return ret;
4718 full:
4719 h2c->flags |= H2_CF_MUX_MFULL;
4720 h2s->flags |= H2_SF_BLK_MROOM;
4721 ret = 0;
4722 goto end;
4723 fail:
4724 /* unparsable HTX messages, too large ones to be produced in the local
4725 * list etc go here (unrecoverable errors).
4726 */
4727 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4728 ret = 0;
4729 goto end;
4730}
4731
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004732/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004733 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4734 * caller must check the stream's status to detect any error which might have
4735 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004736 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4737 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004738static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004739{
4740 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004741 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004742 struct buffer outbuf;
4743 size_t total = 0;
4744 int es_now = 0;
4745 int bsize; /* htx block size */
4746 int fsize; /* h2 frame size */
4747 struct htx_blk *blk;
4748 enum htx_blk_type type;
4749 int idx;
4750
4751 if (h2c_mux_busy(h2c, h2s)) {
4752 h2s->flags |= H2_SF_BLK_MBUSY;
4753 goto end;
4754 }
4755
4756 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4757 h2c->flags |= H2_CF_MUX_MALLOC;
4758 h2s->flags |= H2_SF_BLK_MROOM;
4759 goto end;
4760 }
4761
Willy Tarreau98de12a2018-12-12 07:03:00 +01004762 htx = htx_from_buf(buf);
4763
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004764 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4765 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4766 * the caller to handle.
4767 */
4768
4769 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004770 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004771 goto end;
4772
4773 idx = htx_get_head(htx);
4774 blk = htx_get_blk(htx, idx);
4775 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4776 bsize = htx_get_blksz(blk);
4777 fsize = bsize;
4778
4779 if (type == HTX_BLK_EOD) {
4780 /* if we have an EOD, we're dealing with chunked data. We may
4781 * have a set of trailers after us that the caller will want to
4782 * deal with. Let's simply remove the EOD and return.
4783 */
4784 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004785 total++; // EOD counts as one byte
4786 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004787 goto end;
4788 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004789 else if (type == HTX_BLK_EOM) {
4790 if (h2s->flags & H2_SF_ES_SENT) {
4791 /* ES already sent */
4792 htx_remove_blk(htx, blk);
4793 total++; // EOM counts as one byte
4794 count--;
4795 goto end;
4796 }
4797 }
4798 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004799 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004800
4801 /* Perform some optimizations to reduce the number of buffer copies.
4802 * First, if the mux's buffer is empty and the htx area contains
4803 * exactly one data block of the same size as the requested count, and
4804 * this count fits within the frame size, the stream's window size, and
4805 * the connection's window size, then it's possible to simply swap the
4806 * caller's buffer with the mux's output buffer and adjust offsets and
4807 * length to match the entire DATA HTX block in the middle. In this
4808 * case we perform a true zero-copy operation from end-to-end. This is
4809 * the situation that happens all the time with large files. Second, if
4810 * this is not possible, but the mux's output buffer is empty, we still
4811 * have an opportunity to avoid the copy to the intermediary buffer, by
4812 * making the intermediary buffer's area point to the output buffer's
4813 * area. In this case we want to skip the HTX header to make sure that
4814 * copies remain aligned and that this operation remains possible all
4815 * the time. This goes for headers, data blocks and any data extracted
4816 * from the HTX blocks.
4817 */
4818 if (unlikely(fsize == count &&
4819 htx->used == 1 && type == HTX_BLK_DATA &&
4820 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4821 void *old_area = h2c->mbuf.area;
4822
4823 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004824 /* Too bad there are data left there. We're willing to memcpy/memmove
4825 * up to 1/4 of the buffer, which means that it's OK to copy a large
4826 * frame into a buffer containing few data if it needs to be realigned,
4827 * and that it's also OK to copy few data without realigning. Otherwise
4828 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004829 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004830 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4831 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4832 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004833 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004834
Willy Tarreau98de12a2018-12-12 07:03:00 +01004835 h2c->flags |= H2_CF_MUX_MFULL;
4836 h2s->flags |= H2_SF_BLK_MROOM;
4837 goto end;
4838 }
4839
4840 /* map an H2 frame to the HTX block so that we can put the
4841 * frame header there.
4842 */
4843 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004844 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004845 h2c->mbuf.data = fsize + 9;
4846 outbuf.area = b_head(&h2c->mbuf);
4847
4848 /* prepend an H2 DATA frame header just before the DATA block */
4849 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4850 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4851 h2_set_frame_size(outbuf.area, fsize);
4852
4853 /* update windows */
4854 h2s->mws -= fsize;
4855 h2c->mws -= fsize;
4856
4857 /* and exchange with our old area */
4858 buf->area = old_area;
4859 buf->data = buf->head = 0;
4860 total += fsize;
4861 goto end;
4862 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004863
Willy Tarreau98de12a2018-12-12 07:03:00 +01004864 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004865 /* for DATA and EOM we'll have to emit a frame, even if empty */
4866
4867 while (1) {
4868 outbuf.area = b_tail(&h2c->mbuf);
4869 outbuf.size = b_contig_space(&h2c->mbuf);
4870 outbuf.data = 0;
4871
4872 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4873 break;
4874 realign_again:
4875 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4876 }
4877
4878 if (outbuf.size < 9) {
4879 h2c->flags |= H2_CF_MUX_MFULL;
4880 h2s->flags |= H2_SF_BLK_MROOM;
4881 goto end;
4882 }
4883
4884 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4885 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4886 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4887 outbuf.data = 9;
4888
4889 /* we have in <fsize> the exact number of bytes we need to copy from
4890 * the HTX buffer. We need to check this against the connection's and
4891 * the stream's send windows, and to ensure that this fits in the max
4892 * frame size and in the buffer's available space minus 9 bytes (for
4893 * the frame header). The connection's flow control is applied last so
4894 * that we can use a separate list of streams which are immediately
4895 * unblocked on window opening. Note: we don't implement padding.
4896 */
4897
4898 /* EOM is presented with bsize==1 but would lead to the emission of an
4899 * empty frame, thus we force it to zero here.
4900 */
4901 if (type == HTX_BLK_EOM)
4902 bsize = fsize = 0;
4903
4904 if (!fsize)
4905 goto send_empty;
4906
4907 if (h2s->mws <= 0) {
4908 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004909 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004910 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004911 goto end;
4912 }
4913
Willy Tarreauee573762018-12-04 15:25:57 +01004914 if (fsize > count)
4915 fsize = count;
4916
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004917 if (fsize > h2s->mws)
4918 fsize = h2s->mws; // >0
4919
4920 if (h2c->mfs && fsize > h2c->mfs)
4921 fsize = h2c->mfs; // >0
4922
4923 if (fsize + 9 > outbuf.size) {
4924 /* we have an opportunity for enlarging the too small
4925 * available space, let's try.
4926 * FIXME: is this really interesting to do? Maybe we'll
4927 * spend lots of time realigning instead of using two
4928 * frames.
4929 */
4930 if (b_space_wraps(&h2c->mbuf))
4931 goto realign_again;
4932 fsize = outbuf.size - 9;
4933
4934 if (fsize <= 0) {
4935 /* no need to send an empty frame here */
4936 h2c->flags |= H2_CF_MUX_MFULL;
4937 h2s->flags |= H2_SF_BLK_MROOM;
4938 goto end;
4939 }
4940 }
4941
4942 if (h2c->mws <= 0) {
4943 h2s->flags |= H2_SF_BLK_MFCTL;
4944 goto end;
4945 }
4946
4947 if (fsize > h2c->mws)
4948 fsize = h2c->mws;
4949
4950 /* now let's copy this this into the output buffer */
4951 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004952 h2s->mws -= fsize;
4953 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004954 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004955
4956 send_empty:
4957 /* update the frame's size */
4958 h2_set_frame_size(outbuf.area, fsize);
4959
4960 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4961 * meeting EOM. We should optimize this later.
4962 */
4963 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004964 total++; // EOM counts as one byte
4965 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004966 es_now = 1;
4967 }
4968
4969 if (es_now)
4970 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4971
4972 /* commit the H2 response */
4973 b_add(&h2c->mbuf, fsize + 9);
4974
4975 /* consume incoming HTX block, including EOM */
4976 total += fsize;
4977 if (fsize == bsize) {
4978 htx_remove_blk(htx, blk);
4979 if (fsize)
4980 goto new_frame;
4981 } else {
4982 /* we've truncated this block */
4983 htx_cut_data_blk(htx, blk, fsize);
4984 }
4985
4986 if (es_now) {
4987 if (h2s->st == H2_SS_OPEN)
4988 h2s->st = H2_SS_HLOC;
4989 else
4990 h2s_close(h2s);
4991
4992 h2s->flags |= H2_SF_ES_SENT;
4993 }
4994
4995 end:
4996 return total;
4997}
4998
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004999/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5000 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5001 * processed. The caller must check the stream's status to detect any error
5002 * which might have happened subsequently to a successful send. The htx blocks
5003 * are automatically removed from the message. The htx message is assumed to be
5004 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005005 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005006 * as a single frame. The ES flag is always set.
5007 */
5008static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5009{
5010 struct http_hdr list[MAX_HTTP_HDR];
5011 struct h2c *h2c = h2s->h2c;
5012 struct htx_blk *blk;
5013 struct htx_blk *blk_end;
5014 struct buffer outbuf;
5015 struct h1m h1m;
5016 enum htx_blk_type type;
5017 uint32_t size;
5018 int ret = 0;
5019 int hdr;
5020 int idx;
5021 void *start;
5022
5023 if (h2c_mux_busy(h2c, h2s)) {
5024 h2s->flags |= H2_SF_BLK_MBUSY;
5025 goto end;
5026 }
5027
5028 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5029 h2c->flags |= H2_CF_MUX_MALLOC;
5030 h2s->flags |= H2_SF_BLK_MROOM;
5031 goto end;
5032 }
5033
5034 /* The principle is that we parse each and every trailers block using
5035 * the H1 headers parser, and append it to the list. We don't proceed
5036 * until EOM is met. blk_end will point to the EOM block.
5037 */
5038 hdr = 0;
5039 memset(list, 0, sizeof(list));
5040 blk_end = NULL;
5041
5042 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5043 blk = htx_get_blk(htx, idx);
5044 type = htx_get_blk_type(blk);
5045
5046 if (type == HTX_BLK_UNUSED)
5047 continue;
5048
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005049 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005050 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005051
5052 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5053 goto fail;
5054
5055 size = htx_get_blksz(blk);
5056 start = htx_get_blk_ptr(htx, blk);
5057
5058 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5059 h1m.err_pos = 0;
5060 ret = h1_headers_to_hdr_list(start, start + size,
5061 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5062 &h1m, NULL);
5063 if (ret < 0)
5064 goto fail;
5065
5066 /* ret == 0 if an incomplete trailers block was found (missing
5067 * empty line), or > 0 if it was found. We have to continue on
5068 * incomplete messages because the trailers block might be
5069 * incomplete.
5070 */
5071
5072 /* search the new end */
5073 while (hdr <= sizeof(list)/sizeof(list[0])) {
5074 if (!list[hdr].n.len)
5075 break;
5076 hdr++;
5077 }
5078 }
5079
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005080 if (list[hdr].n.len != 0)
5081 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005082
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005083 chunk_reset(&outbuf);
5084
5085 while (1) {
5086 outbuf.area = b_tail(&h2c->mbuf);
5087 outbuf.size = b_contig_space(&h2c->mbuf);
5088 outbuf.data = 0;
5089
5090 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5091 break;
5092 realign_again:
5093 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5094 }
5095
5096 if (outbuf.size < 9)
5097 goto full;
5098
5099 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5100 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5101 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5102 outbuf.data = 9;
5103
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005104 /* encode all headers */
5105 for (idx = 0; idx < hdr; idx++) {
5106 /* these ones do not exist in H2 or must not appear in
5107 * trailers and must be dropped.
5108 */
5109 if (isteq(list[idx].n, ist("host")) ||
5110 isteq(list[idx].n, ist("content-length")) ||
5111 isteq(list[idx].n, ist("connection")) ||
5112 isteq(list[idx].n, ist("proxy-connection")) ||
5113 isteq(list[idx].n, ist("keep-alive")) ||
5114 isteq(list[idx].n, ist("upgrade")) ||
5115 isteq(list[idx].n, ist("te")) ||
5116 isteq(list[idx].n, ist("transfer-encoding")))
5117 continue;
5118
5119 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5120 /* output full */
5121 if (b_space_wraps(&h2c->mbuf))
5122 goto realign_again;
5123 goto full;
5124 }
5125 }
5126
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005127 if (outbuf.data == 9) {
5128 /* here we have a problem, we have nothing to emit (either we
5129 * received an empty trailers block followed or we removed its
5130 * contents above). Because of this we can't send a HEADERS
5131 * frame, so we have to cheat and instead send an empty DATA
5132 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005133 */
5134 outbuf.area[3] = H2_FT_DATA;
5135 outbuf.area[4] = H2_F_DATA_END_STREAM;
5136 }
5137
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005138 /* update the frame's size */
5139 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5140
5141 /* commit the H2 response */
5142 b_add(&h2c->mbuf, outbuf.data);
5143 h2s->flags |= H2_SF_ES_SENT;
5144
5145 if (h2s->st == H2_SS_OPEN)
5146 h2s->st = H2_SS_HLOC;
5147 else
5148 h2s_close(h2s);
5149
5150 /* OK we could properly deliver the response */
5151 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005152 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005153 ret = 0;
5154 idx = htx_get_head(htx);
5155 blk = htx_get_blk(htx, idx);
5156 while (blk != blk_end) {
5157 ret += htx_get_blksz(blk);
5158 blk = htx_remove_blk(htx, blk);
5159 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005160 end:
5161 return ret;
5162 full:
5163 h2c->flags |= H2_CF_MUX_MFULL;
5164 h2s->flags |= H2_SF_BLK_MROOM;
5165 ret = 0;
5166 goto end;
5167 fail:
5168 /* unparsable HTX messages, too large ones to be produced in the local
5169 * list etc go here (unrecoverable errors).
5170 */
5171 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5172 ret = 0;
5173 goto end;
5174}
5175
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005176/* Called from the upper layer, to subscribe to events, such as being able to send.
5177 * The <param> argument here is supposed to be a pointer to a wait_event struct
5178 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5179 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5180 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5181 * returns 0 except for the error above.
5182 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005183static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5184{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005185 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005186 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005187 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005188
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005189 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005190 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005191 if (!(sw->events & SUB_RETRY_RECV)) {
5192 sw->events |= SUB_RETRY_RECV;
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 Houchardfa8aa862018-10-10 18:25:41 +02005201 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005202 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreauc234ae32019-05-13 17:56:11 +02005203 !LIST_ADDED(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005204 if (h2s->flags & H2_SF_BLK_MFCTL)
5205 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5206 else
5207 LIST_ADDQ(&h2c->send_list, &h2s->list);
5208 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005209 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005210 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005211 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005212 if (event_type != 0)
5213 return -1;
5214 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005215}
5216
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005217/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5218 * The <param> argument here is supposed to be a pointer to the same wait_event
5219 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5220 * It always returns zero.
5221 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005222static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5223{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005224 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005225 struct h2s *h2s = cs->ctx;
5226
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005227 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005228 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005229 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005230 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005231 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005232 }
5233 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005234 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005235 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005236 if (h2s->send_wait == sw) {
5237 LIST_DEL(&h2s->list);
5238 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005239 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005240 /* We were about to send, make sure it does not happen */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005241 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02005242 h2s->send_wait != &h2s->wait_event) {
5243 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5244 LIST_DEL_INIT(&h2s->sending_list);
5245 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005246 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005247
Olivier Houchardd846c262018-10-19 17:24:29 +02005248 }
5249 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005250 return 0;
5251}
5252
5253
Olivier Houchard511efea2018-08-16 15:30:32 +02005254/* Called from the upper layer, to receive data */
5255static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5256{
Olivier Houchard638b7992018-08-16 15:41:52 +02005257 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005258 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005259 struct htx *h2s_htx = NULL;
5260 struct htx *buf_htx = NULL;
5261 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005262 size_t ret = 0;
5263
5264 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005265 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5266 /* in HTX mode we ignore the count argument */
5267 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005268 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005269 /* Here htx_to_buf() will set buffer data to 0 because
5270 * the HTX is empty.
5271 */
5272 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005273 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005274 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005275
5276 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005277 count = htx_free_data_space(buf_htx);
5278 if (flags & CO_RFL_KEEP_RSV) {
5279 if (count <= global.tune.maxrewrite)
5280 goto end;
5281 count -= global.tune.maxrewrite;
5282 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005283
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005284 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005285
5286 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5287 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5288
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005289 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005290 htx_to_buf(buf_htx, buf);
5291 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005292 ret = htx_ret.ret;
5293 }
5294 else {
5295 ret = b_xfer(buf, &h2s->rxbuf, count);
5296 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005297
Christopher Faulet37070b22019-02-14 15:12:14 +01005298 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005299 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005300 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005301 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005302 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005303 if (cs->flags & CS_FL_REOS)
5304 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005305 if (cs->flags & CS_FL_ERR_PENDING)
5306 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005307 if (b_size(&h2s->rxbuf)) {
5308 b_free(&h2s->rxbuf);
5309 offer_buffers(NULL, tasks_run_queue);
5310 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005311 }
5312
Willy Tarreau082f5592018-11-25 08:03:32 +01005313 if (ret && h2c->dsi == h2s->id) {
5314 /* demux is blocking on this stream's buffer */
5315 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005316 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005317 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005318
Olivier Houchard511efea2018-08-16 15:30:32 +02005319 return ret;
5320}
5321
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005322/* stops all senders of this connection for example when the mux buffer is full.
5323 * They are moved from the sending_list to either fctl_list or send_list.
5324 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005325static void h2_stop_senders(struct h2c *h2c)
5326{
5327 struct h2s *h2s, *h2s_back;
5328
Olivier Houchardd360ac62019-03-22 17:37:16 +01005329 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5330 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005331 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005332 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005333 }
5334}
5335
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005336/* Called from the upper layer, to send data from buffer <buf> for no more than
5337 * <count> bytes. Returns the number of bytes effectively sent. Some status
5338 * flags may be updated on the conn_stream.
5339 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005340static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005341{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005342 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005343 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005344 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005345 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005346 struct htx *htx;
5347 struct htx_blk *blk;
5348 enum htx_blk_type btype;
5349 uint32_t bsize;
5350 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005351
Olivier Houchardd360ac62019-03-22 17:37:16 +01005352 /* If we were not just woken because we wanted to send but couldn't,
5353 * and there's somebody else that is waiting to send, do nothing,
5354 * we will subscribe later and be put at the end of the list
5355 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005356 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005357 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5358 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005359 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005360
Olivier Houchard998410a2019-04-15 19:23:37 +02005361 /* We couldn't set it to NULL before, because we needed it in case
5362 * we had to cancel the tasklet
5363 */
5364 h2s->send_wait = NULL;
5365
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005366 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5367 return 0;
5368
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005369 /* htx will be enough to decide if we're using HTX or legacy */
5370 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5371
Willy Tarreau0bad0432018-06-14 16:54:01 +02005372 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005373 h2s->flags |= H2_SF_OUTGOING_DATA;
5374
Willy Tarreau751f2d02018-10-05 09:35:00 +02005375 if (h2s->id == 0) {
5376 int32_t id = h2c_get_next_sid(h2s->h2c);
5377
5378 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005379 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005380 return 0;
5381 }
5382
5383 eb32_delete(&h2s->by_id);
5384 h2s->by_id.key = h2s->id = id;
5385 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005386 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005387 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5388 }
5389
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005390 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005391 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005392 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005393 idx = htx_get_head(htx);
5394 blk = htx_get_blk(htx, idx);
5395 btype = htx_get_blk_type(blk);
5396 bsize = htx_get_blksz(blk);
5397
5398 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005399 case HTX_BLK_REQ_SL:
5400 /* start-line before headers */
5401 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5402 if (ret > 0) {
5403 total += ret;
5404 count -= ret;
5405 if (ret < bsize)
5406 goto done;
5407 }
5408 break;
5409
Willy Tarreau115e83b2018-12-01 19:17:53 +01005410 case HTX_BLK_RES_SL:
5411 /* start-line before headers */
5412 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5413 if (ret > 0) {
5414 total += ret;
5415 count -= ret;
5416 if (ret < bsize)
5417 goto done;
5418 }
5419 break;
5420
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005421 case HTX_BLK_DATA:
5422 case HTX_BLK_EOD:
5423 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005424 /* all these cause the emission of a DATA frame (possibly empty).
5425 * This EOM necessarily is one before trailers, as the EOM following
5426 * trailers would have been consumed by the trailers parser.
5427 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005428 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005429 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005430 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005431 total += ret;
5432 count -= ret;
5433 if (ret < bsize)
5434 goto done;
5435 }
5436 break;
5437
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005438 case HTX_BLK_TLR:
5439 /* This is the first trailers block, all the subsequent ones AND
5440 * the EOM will be swallowed by the parser.
5441 */
5442 ret = h2s_htx_make_trailers(h2s, htx);
5443 if (ret > 0) {
5444 total += ret;
5445 count -= ret;
5446 if (ret < bsize)
5447 goto done;
5448 }
5449 break;
5450
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005451 default:
5452 htx_remove_blk(htx, blk);
5453 total += bsize;
5454 count -= bsize;
5455 break;
5456 }
5457 }
5458 goto done;
5459 }
5460
5461 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005462 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005463 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005464 if (h2s->h2c->flags & H2_CF_IS_BACK)
5465 ret = -1;
5466 else
5467 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005468 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005469 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005470 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005471 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005472 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005473 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005474 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005475
Willy Tarreau5dd17352018-06-14 13:33:30 +02005476 if (unlikely((int)ret <= 0)) {
5477 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005478 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5479 break;
5480 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005481 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005482 total += count;
5483 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005484 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005485 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005486 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005487 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005488 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005489 break;
5490 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005491
5492 total += ret;
5493 count -= ret;
5494
Willy Tarreau2b778482019-05-06 15:00:22 +02005495 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005496 break;
5497
5498 if (h2s->flags & H2_SF_BLK_ANY)
5499 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005500 }
5501
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005502 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005503 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005504 /* trim any possibly pending data after we close (extra CR-LF,
5505 * unprocessed trailers, abnormal extra data, ...)
5506 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005507 total += count;
5508 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005509 }
5510
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005511 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005512 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005513 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005514 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005515 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005516 }
5517
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005518 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005519 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005520 } else {
5521 b_del(buf, total);
5522 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005523
5524 /* The mux is full, cancel the pending tasks */
5525 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5526 (h2s->flags & H2_SF_BLK_MBUSY))
5527 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005528
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005529 /* If we're running HTX, and we read the whole buffer, then pretend
5530 * we read exactly what the caller specified, as with HTX the caller
5531 * will always give the buffer size, instead of the amount of data
5532 * available.
5533 */
5534 if (htx && !b_data(buf))
5535 total = orig_count;
5536
Olivier Houchard7505f942018-08-21 18:10:44 +02005537 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005538 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005539 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005540
Olivier Houchard7505f942018-08-21 18:10:44 +02005541 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005542 /* If we're waiting for flow control, and we got a shutr on the
5543 * connection, we will never be unlocked, so add an error on
5544 * the conn_stream.
5545 */
5546 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5547 !b_data(&h2s->h2c->dbuf) &&
5548 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5549 if (cs->flags & CS_FL_EOS)
5550 cs->flags |= CS_FL_ERROR;
5551 else
5552 cs->flags |= CS_FL_ERR_PENDING;
5553 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005554 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005555 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005556 LIST_DEL_INIT(&h2s->list);
5557 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005558 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005559}
5560
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005561/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005562static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005563{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005564 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005565 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005566 struct eb32_node *node;
5567 int fctl_cnt = 0;
5568 int send_cnt = 0;
5569 int tree_cnt = 0;
5570 int orph_cnt = 0;
5571
5572 if (!h2c)
5573 return;
5574
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005575 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005576 fctl_cnt++;
5577
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005578 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005579 send_cnt++;
5580
Willy Tarreau3af37712018-12-18 14:34:41 +01005581 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005582 node = eb32_first(&h2c->streams_by_id);
5583 while (node) {
5584 h2s = container_of(node, struct h2s, by_id);
5585 tree_cnt++;
5586 if (!h2s->cs)
5587 orph_cnt++;
5588 node = eb32_next(node);
5589 }
5590
Willy Tarreau987c0632018-12-18 10:32:05 +01005591 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5592 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5593 " .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 +02005594 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5595 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005596 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005597 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5598 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5599 h2c->msi,
5600 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5601 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5602
5603 if (h2s) {
5604 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5605 h2s, h2s->id, h2s->flags,
5606 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5607 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5608 h2s->cs);
5609 if (h2s->cs)
5610 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5611 h2s->cs->flags, h2s->cs->data);
5612 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005613}
Willy Tarreau62f52692017-10-08 23:01:42 +02005614
5615/*******************************************************/
5616/* functions below are dedicated to the config parsers */
5617/*******************************************************/
5618
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005619/* config parser for global "tune.h2.header-table-size" */
5620static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5621 struct proxy *defpx, const char *file, int line,
5622 char **err)
5623{
5624 if (too_many_args(1, args, err, NULL))
5625 return -1;
5626
5627 h2_settings_header_table_size = atoi(args[1]);
5628 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5629 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5630 return -1;
5631 }
5632 return 0;
5633}
Willy Tarreau62f52692017-10-08 23:01:42 +02005634
Willy Tarreaue6baec02017-07-27 11:45:11 +02005635/* config parser for global "tune.h2.initial-window-size" */
5636static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5637 struct proxy *defpx, const char *file, int line,
5638 char **err)
5639{
5640 if (too_many_args(1, args, err, NULL))
5641 return -1;
5642
5643 h2_settings_initial_window_size = atoi(args[1]);
5644 if (h2_settings_initial_window_size < 0) {
5645 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5646 return -1;
5647 }
5648 return 0;
5649}
5650
Willy Tarreau5242ef82017-07-27 11:47:28 +02005651/* config parser for global "tune.h2.max-concurrent-streams" */
5652static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5653 struct proxy *defpx, const char *file, int line,
5654 char **err)
5655{
5656 if (too_many_args(1, args, err, NULL))
5657 return -1;
5658
5659 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005660 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005661 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5662 return -1;
5663 }
5664 return 0;
5665}
5666
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005667/* config parser for global "tune.h2.max-frame-size" */
5668static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5669 struct proxy *defpx, const char *file, int line,
5670 char **err)
5671{
5672 if (too_many_args(1, args, err, NULL))
5673 return -1;
5674
5675 h2_settings_max_frame_size = atoi(args[1]);
5676 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5677 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5678 return -1;
5679 }
5680 return 0;
5681}
5682
Willy Tarreau62f52692017-10-08 23:01:42 +02005683
5684/****************************************/
5685/* MUX initialization and instanciation */
5686/***************************************/
5687
5688/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005689static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005690 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005691 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005692 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005693 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005694 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005695 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005696 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005697 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005698 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005699 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005700 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005701 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005702 .shutr = h2_shutr,
5703 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005704 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005705 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005706 .name = "H2",
5707};
5708
Christopher Faulet32f61c02018-04-10 14:33:41 +02005709/* PROTO selection : this mux registers PROTO token "h2" */
5710static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005711 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005712
Willy Tarreau0108d902018-11-25 19:14:37 +01005713INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5714
Christopher Faulet9b579102019-04-11 22:52:25 +02005715
5716/* The mux operations */
5717static const struct mux_ops h2_htx_ops = {
5718 .init = h2_init,
5719 .wake = h2_wake,
5720 .snd_buf = h2_snd_buf,
5721 .rcv_buf = h2_rcv_buf,
5722 .subscribe = h2_subscribe,
5723 .unsubscribe = h2_unsubscribe,
5724 .attach = h2_attach,
5725 .get_first_cs = h2_get_first_cs,
5726 .detach = h2_detach,
5727 .destroy = h2_destroy,
5728 .avail_streams = h2_avail_streams,
5729 .used_streams = h2_used_streams,
5730 .shutr = h2_shutr,
5731 .shutw = h2_shutw,
5732 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005733 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005734 .name = "H2",
5735};
5736
Willy Tarreauf8957272018-10-03 10:25:20 +02005737static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005738 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005739
5740INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5741
Willy Tarreau62f52692017-10-08 23:01:42 +02005742/* config keyword parsers */
5743static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005744 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005745 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005746 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005747 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005748 { 0, NULL, NULL }
5749}};
5750
Willy Tarreau0108d902018-11-25 19:14:37 +01005751INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);