blob: 40ff11a7e715227cf4c51f16d68ffccded92dd86 [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
1435 h2s->cs->flags |= flags;
1436 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1437 h2s->cs->flags |= CS_FL_ERROR;
1438
1439 h2s_alert(h2s);
1440
1441 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1442 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001443 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001444 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001445 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001446 h2s_close(h2s);
1447}
1448
1449/* wake the streams attached to the connection, whose id is greater than <last>
1450 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001451 */
1452static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1453{
1454 struct eb32_node *node;
1455 struct h2s *h2s;
1456
1457 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001458 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001459
1460 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001461 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001462
Christopher Fauletf02ca002019-03-07 16:21:34 +01001463 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001464 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1465 while (node) {
1466 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001467 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001468 h2s_wake_one_stream(h2s, flags);
1469 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001470
Christopher Fauletf02ca002019-03-07 16:21:34 +01001471 /* Wake all streams with unassigned ID (ID == 0) */
1472 node = eb32_lookup(&h2c->streams_by_id, 0);
1473 while (node) {
1474 h2s = container_of(node, struct h2s, by_id);
1475 if (h2s->id > 0)
1476 break;
1477 node = eb32_next(node);
1478 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001479 }
1480}
1481
Willy Tarreau3421aba2017-07-27 15:41:03 +02001482/* Increase all streams' outgoing window size by the difference passed in
1483 * argument. This is needed upon receipt of the settings frame if the initial
1484 * window size is different. The difference may be negative and the resulting
1485 * window size as well, for the time it takes to receive some window updates.
1486 */
1487static void h2c_update_all_ws(struct h2c *h2c, int diff)
1488{
1489 struct h2s *h2s;
1490 struct eb32_node *node;
1491
1492 if (!diff)
1493 return;
1494
1495 node = eb32_first(&h2c->streams_by_id);
1496 while (node) {
1497 h2s = container_of(node, struct h2s, by_id);
1498 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001499
1500 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1501 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001502 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001503 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001504 }
1505
Willy Tarreau3421aba2017-07-27 15:41:03 +02001506 node = eb32_next(node);
1507 }
1508}
1509
1510/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1511 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001512 * return an error in h2c. The caller must have already verified frame length
1513 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001514 */
1515static int h2c_handle_settings(struct h2c *h2c)
1516{
1517 unsigned int offset;
1518 int error;
1519
1520 if (h2c->dff & H2_F_SETTINGS_ACK) {
1521 if (h2c->dfl) {
1522 error = H2_ERR_FRAME_SIZE_ERROR;
1523 goto fail;
1524 }
1525 return 1;
1526 }
1527
Willy Tarreau3421aba2017-07-27 15:41:03 +02001528 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001530 return 0;
1531
1532 /* parse the frame */
1533 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001534 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1535 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001536
1537 switch (type) {
1538 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1539 /* we need to update all existing streams with the
1540 * difference from the previous iws.
1541 */
1542 if (arg < 0) { // RFC7540#6.5.2
1543 error = H2_ERR_FLOW_CONTROL_ERROR;
1544 goto fail;
1545 }
1546 h2c_update_all_ws(h2c, arg - h2c->miw);
1547 h2c->miw = arg;
1548 break;
1549 case H2_SETTINGS_MAX_FRAME_SIZE:
1550 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1551 error = H2_ERR_PROTOCOL_ERROR;
1552 goto fail;
1553 }
1554 h2c->mfs = arg;
1555 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001556 case H2_SETTINGS_ENABLE_PUSH:
1557 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto fail;
1560 }
1561 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001562 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1563 if (h2c->flags & H2_CF_IS_BACK) {
1564 /* the limit is only for the backend; for the frontend it is our limit */
1565 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1566 arg = h2_settings_max_concurrent_streams;
1567 h2c->streams_limit = arg;
1568 }
1569 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001570 }
1571 }
1572
1573 /* need to ACK this frame now */
1574 h2c->st0 = H2_CS_FRAME_A;
1575 return 1;
1576 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001577 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001578 h2c_error(h2c, error);
1579 return 0;
1580}
1581
1582/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1583 * success or one of the h2_status values.
1584 */
1585static int h2c_ack_settings(struct h2c *h2c)
1586{
1587 struct buffer *res;
1588 char str[9];
1589 int ret = -1;
1590
1591 if (h2c_mux_busy(h2c, NULL)) {
1592 h2c->flags |= H2_CF_DEM_MBUSY;
1593 return 0;
1594 }
1595
Willy Tarreau44e973f2018-03-01 17:49:30 +01001596 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001597 if (!res) {
1598 h2c->flags |= H2_CF_MUX_MALLOC;
1599 h2c->flags |= H2_CF_DEM_MROOM;
1600 return 0;
1601 }
1602
1603 memcpy(str,
1604 "\x00\x00\x00" /* length : 0 (no data) */
1605 "\x04" "\x01" /* type : 4, flags : ACK */
1606 "\x00\x00\x00\x00" /* stream ID */, 9);
1607
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001608 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001609 if (unlikely(ret <= 0)) {
1610 if (!ret) {
1611 h2c->flags |= H2_CF_MUX_MFULL;
1612 h2c->flags |= H2_CF_DEM_MROOM;
1613 return 0;
1614 }
1615 else {
1616 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1617 return 0;
1618 }
1619 }
1620 return ret;
1621}
1622
Willy Tarreaucf68c782017-10-10 17:11:41 +02001623/* processes a PING frame and schedules an ACK if needed. The caller must pass
1624 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001625 * missing data. The caller must have already verified frame length
1626 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001627 */
1628static int h2c_handle_ping(struct h2c *h2c)
1629{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001630 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001631 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001632 h2c->st0 = H2_CS_FRAME_A;
1633 return 1;
1634}
1635
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001636/* Try to send a window update for stream id <sid> and value <increment>.
1637 * Returns > 0 on success or zero on missing room or failure. It may return an
1638 * error in h2c.
1639 */
1640static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1641{
1642 struct buffer *res;
1643 char str[13];
1644 int ret = -1;
1645
1646 if (h2c_mux_busy(h2c, NULL)) {
1647 h2c->flags |= H2_CF_DEM_MBUSY;
1648 return 0;
1649 }
1650
Willy Tarreau44e973f2018-03-01 17:49:30 +01001651 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001652 if (!res) {
1653 h2c->flags |= H2_CF_MUX_MALLOC;
1654 h2c->flags |= H2_CF_DEM_MROOM;
1655 return 0;
1656 }
1657
1658 /* length: 4, type: 8, flags: none */
1659 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1660 write_n32(str + 5, sid);
1661 write_n32(str + 9, increment);
1662
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001663 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001664
1665 if (unlikely(ret <= 0)) {
1666 if (!ret) {
1667 h2c->flags |= H2_CF_MUX_MFULL;
1668 h2c->flags |= H2_CF_DEM_MROOM;
1669 return 0;
1670 }
1671 else {
1672 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1673 return 0;
1674 }
1675 }
1676 return ret;
1677}
1678
1679/* try to send pending window update for the connection. It's safe to call it
1680 * with no pending updates. Returns > 0 on success or zero on missing room or
1681 * failure. It may return an error in h2c.
1682 */
1683static int h2c_send_conn_wu(struct h2c *h2c)
1684{
1685 int ret = 1;
1686
1687 if (h2c->rcvd_c <= 0)
1688 return 1;
1689
Willy Tarreau97aaa672018-12-23 09:49:04 +01001690 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1691 /* increase the advertised connection window to 2G on
1692 * first update.
1693 */
1694 h2c->flags |= H2_CF_WINDOW_OPENED;
1695 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1696 }
1697
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001698 /* send WU for the connection */
1699 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1700 if (ret > 0)
1701 h2c->rcvd_c = 0;
1702
1703 return ret;
1704}
1705
1706/* try to send pending window update for the current dmux stream. It's safe to
1707 * call it with no pending updates. Returns > 0 on success or zero on missing
1708 * room or failure. It may return an error in h2c.
1709 */
1710static int h2c_send_strm_wu(struct h2c *h2c)
1711{
1712 int ret = 1;
1713
1714 if (h2c->rcvd_s <= 0)
1715 return 1;
1716
1717 /* send WU for the stream */
1718 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1719 if (ret > 0)
1720 h2c->rcvd_s = 0;
1721
1722 return ret;
1723}
1724
Willy Tarreaucf68c782017-10-10 17:11:41 +02001725/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1726 * success, 0 on missing data or one of the h2_status values.
1727 */
1728static int h2c_ack_ping(struct h2c *h2c)
1729{
1730 struct buffer *res;
1731 char str[17];
1732 int ret = -1;
1733
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001734 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001735 return 0;
1736
1737 if (h2c_mux_busy(h2c, NULL)) {
1738 h2c->flags |= H2_CF_DEM_MBUSY;
1739 return 0;
1740 }
1741
Willy Tarreau44e973f2018-03-01 17:49:30 +01001742 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001743 if (!res) {
1744 h2c->flags |= H2_CF_MUX_MALLOC;
1745 h2c->flags |= H2_CF_DEM_MROOM;
1746 return 0;
1747 }
1748
1749 memcpy(str,
1750 "\x00\x00\x08" /* length : 8 (same payload) */
1751 "\x06" "\x01" /* type : 6, flags : ACK */
1752 "\x00\x00\x00\x00" /* stream ID */, 9);
1753
1754 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001755 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001756
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001757 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001758 if (unlikely(ret <= 0)) {
1759 if (!ret) {
1760 h2c->flags |= H2_CF_MUX_MFULL;
1761 h2c->flags |= H2_CF_DEM_MROOM;
1762 return 0;
1763 }
1764 else {
1765 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1766 return 0;
1767 }
1768 }
1769 return ret;
1770}
1771
Willy Tarreau26f95952017-07-27 17:18:30 +02001772/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1773 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001774 * h2c or h2s. The caller must have already verified frame length and stream ID
1775 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001776 */
1777static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1778{
1779 int32_t inc;
1780 int error;
1781
Willy Tarreau26f95952017-07-27 17:18:30 +02001782 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001783 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001784 return 0;
1785
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001786 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001787
1788 if (h2c->dsi != 0) {
1789 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001790
1791 /* it's not an error to receive WU on a closed stream */
1792 if (h2s->st == H2_SS_CLOSED)
1793 return 1;
1794
1795 if (!inc) {
1796 error = H2_ERR_PROTOCOL_ERROR;
1797 goto strm_err;
1798 }
1799
1800 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1801 error = H2_ERR_FLOW_CONTROL_ERROR;
1802 goto strm_err;
1803 }
1804
1805 h2s->mws += inc;
1806 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1807 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001808 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001809 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001810 }
1811 }
1812 else {
1813 /* connection window update */
1814 if (!inc) {
1815 error = H2_ERR_PROTOCOL_ERROR;
1816 goto conn_err;
1817 }
1818
1819 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1820 error = H2_ERR_FLOW_CONTROL_ERROR;
1821 goto conn_err;
1822 }
1823
1824 h2c->mws += inc;
1825 }
1826
1827 return 1;
1828
1829 conn_err:
1830 h2c_error(h2c, error);
1831 return 0;
1832
1833 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001834 h2s_error(h2s, error);
1835 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001836 return 0;
1837}
1838
Willy Tarreaue96b0922017-10-30 00:28:29 +01001839/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001840 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1841 * have already verified frame length and stream ID validity. Described in
1842 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001843 */
1844static int h2c_handle_goaway(struct h2c *h2c)
1845{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001846 int last;
1847
Willy Tarreaue96b0922017-10-30 00:28:29 +01001848 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001849 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850 return 0;
1851
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001852 last = h2_get_n32(&h2c->dbuf, 0);
1853 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001854 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001855 if (h2c->last_sid < 0)
1856 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001858}
1859
Willy Tarreau92153fc2017-12-03 19:46:19 +01001860/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001861 * invalid. Returns > 0 on success or zero on missing data. It may return an
1862 * error in h2c. The caller must have already verified frame length and stream
1863 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864 */
1865static int h2c_handle_priority(struct h2c *h2c)
1866{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001867 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001868 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001869 return 0;
1870
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001871 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001872 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001873 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1874 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001875 }
1876 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001877}
1878
Willy Tarreaucd234e92017-08-18 10:59:39 +02001879/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001880 * Returns > 0 on success or zero on missing data. The caller must have already
1881 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001882 */
1883static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1884{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001885 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001886 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001887 return 0;
1888
1889 /* late RST, already handled */
1890 if (h2s->st == H2_SS_CLOSED)
1891 return 1;
1892
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001893 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001894 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001895
1896 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001897 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001898 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001899 }
1900
1901 h2s->flags |= H2_SF_RST_RCVD;
1902 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001903}
1904
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1906 * It may return an error in h2c or h2s. The caller must consider that the
1907 * return value is the new h2s in case one was allocated (most common case).
1908 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001909 * errors here are reported as connection errors since it's impossible to
1910 * recover from such errors after the compression context has been altered.
1911 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001912static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001913{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001915 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001916 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001917 int error;
1918
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001919 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001920 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001921
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001922 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001924
1925 /* now either the frame is complete or the buffer is complete */
1926 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001927 /* The stream exists/existed, this must be a trailers frame */
1928 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001929 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1930 /* unrecoverable error ? */
1931 if (h2c->st0 >= H2_CS_ERROR)
1932 goto out;
1933
1934 if (error == 0)
1935 goto out; // missing data
1936
1937 if (error < 0) {
1938 /* Failed to decode this frame (e.g. too large request)
1939 * but the HPACK decompressor is still synchronized.
1940 */
1941 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1942 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001943 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001944 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001945 goto done;
1946 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001947 /* the connection was already killed by an RST, let's consume
1948 * the data and send another RST.
1949 */
1950 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1951 h2s = (struct h2s*)h2_error_stream;
1952 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001953 }
1954 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1955 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1956 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001957 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001958 goto conn_err;
1959 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001960 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1961 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001962
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001963 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001964
Willy Tarreau25919232019-01-03 14:48:18 +01001965 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001966 if (h2c->st0 >= H2_CS_ERROR)
1967 goto out;
1968
Willy Tarreau25919232019-01-03 14:48:18 +01001969 if (error <= 0) {
1970 if (error == 0)
1971 goto out; // missing data
1972
1973 /* Failed to decode this stream (e.g. too large request)
1974 * but the HPACK decompressor is still synchronized.
1975 */
1976 h2s = (struct h2s*)h2_error_stream;
1977 goto send_rst;
1978 }
1979
Willy Tarreau22de8d32018-09-05 19:55:58 +02001980 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001981 * positively from h2c_frt_stream_new(), the stream will report the error,
1982 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001983 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001984 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001985 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001986 h2s = (struct h2s*)h2_refused_stream;
1987 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001988 }
1989
1990 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001991 h2s->rxbuf = rxbuf;
1992 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001993 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001994
Willy Tarreau88d138e2019-01-02 19:38:14 +01001995 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001996 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001997 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001998
1999 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002000 if (h2s->cs)
2001 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002002 if (h2s->st == H2_SS_OPEN)
2003 h2s->st = H2_SS_HREM;
2004 else
2005 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002006 }
2007
Willy Tarreau3a429f02019-01-03 11:41:50 +01002008 /* update the max stream ID if the request is being processed */
2009 if (h2s->id > h2c->max_id)
2010 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002011
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002012 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002013
2014 conn_err:
2015 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002016 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002017
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002018 out:
2019 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002020 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002021
2022 send_rst:
2023 /* make the demux send an RST for the current stream. We may only
2024 * do this if we're certain that the HEADERS frame was properly
2025 * decompressed so that the HPACK decoder is still kept up to date.
2026 */
2027 h2_release_buf(h2c, &rxbuf);
2028 h2c->st0 = H2_CS_FRAME_E;
2029 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002030}
2031
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002032/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2033 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2034 * errors here are reported as connection errors since it's impossible to
2035 * recover from such errors after the compression context has been altered.
2036 */
2037static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2038{
2039 int error;
2040
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002041 if (!b_size(&h2c->dbuf))
2042 return NULL; // empty buffer
2043
2044 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2045 return NULL; // incomplete frame
2046
Willy Tarreau1915ca22019-01-24 11:49:37 +01002047 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002048
Willy Tarreau25919232019-01-03 14:48:18 +01002049 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050 if (h2c->st0 >= H2_CS_ERROR)
2051 return NULL;
2052
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002053 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2054 /* RFC7540#5.1 */
2055 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2056 h2c->st0 = H2_CS_FRAME_E;
2057 return NULL;
2058 }
2059
Willy Tarreau25919232019-01-03 14:48:18 +01002060 if (error <= 0) {
2061 if (error == 0)
2062 return NULL; // missing data
2063
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002064 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002065 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002066 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002067 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002068 }
2069
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002070 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2071 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002072 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002073 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002074 }
2075
Willy Tarreau927b88b2019-03-04 08:03:25 +01002076 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002077 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002078 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 +02002079 h2s->st = H2_SS_HREM;
Willy Tarreau201fe402019-05-07 18:10:10 +02002080 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 +02002081 h2s_close(h2s);
2082
2083 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002084}
2085
Willy Tarreau454f9052017-10-26 19:40:35 +02002086/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2087 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2088 */
2089static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2090{
2091 int error;
2092
2093 /* note that empty DATA frames are perfectly valid and sometimes used
2094 * to signal an end of stream (with the ES flag).
2095 */
2096
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002097 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002098 return 0; // empty buffer
2099
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002100 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002101 return 0; // incomplete frame
2102
2103 /* now either the frame is complete or the buffer is complete */
2104
Willy Tarreau454f9052017-10-26 19:40:35 +02002105 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2106 /* RFC7540#6.1 */
2107 error = H2_ERR_STREAM_CLOSED;
2108 goto strm_err;
2109 }
2110
Willy Tarreau1915ca22019-01-24 11:49:37 +01002111 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2112 /* RFC7540#8.1.2 */
2113 error = H2_ERR_PROTOCOL_ERROR;
2114 goto strm_err;
2115 }
2116
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002117 if (!h2_frt_transfer_data(h2s))
2118 return 0;
2119
Willy Tarreau454f9052017-10-26 19:40:35 +02002120 /* call the upper layers to process the frame, then let the upper layer
2121 * notify the stream about any change.
2122 */
2123 if (!h2s->cs) {
2124 error = H2_ERR_STREAM_CLOSED;
2125 goto strm_err;
2126 }
2127
Willy Tarreau8f650c32017-11-21 19:36:21 +01002128 if (h2c->st0 >= H2_CS_ERROR)
2129 return 0;
2130
Willy Tarreau721c9742017-11-07 11:05:42 +01002131 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002132 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002133 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002134 }
2135
2136 /* check for completion : the callee will change this to FRAME_A or
2137 * FRAME_H once done.
2138 */
2139 if (h2c->st0 == H2_CS_FRAME_P)
2140 return 0;
2141
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002142 /* last frame */
2143 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002144 if (h2s->cs)
2145 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002146 if (h2s->st == H2_SS_OPEN)
2147 h2s->st = H2_SS_HREM;
2148 else
2149 h2s_close(h2s);
2150
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002151 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002152
2153 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2154 /* RFC7540#8.1.2 */
2155 error = H2_ERR_PROTOCOL_ERROR;
2156 goto strm_err;
2157 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002158 }
2159
Willy Tarreau454f9052017-10-26 19:40:35 +02002160 return 1;
2161
Willy Tarreau454f9052017-10-26 19:40:35 +02002162 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002163 h2s_error(h2s, error);
2164 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002165 return 0;
2166}
2167
Willy Tarreaubc933932017-10-09 16:21:43 +02002168/* process Rx frames to be demultiplexed */
2169static void h2_process_demux(struct h2c *h2c)
2170{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002171 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002172 struct h2_fh hdr;
2173 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002174
Willy Tarreau081d4722017-05-16 21:51:05 +02002175 if (h2c->st0 >= H2_CS_ERROR)
2176 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002177
2178 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2179 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002180 if (h2c->flags & H2_CF_IS_BACK)
2181 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002182 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2183 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002184 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002185 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002186 sess_log(h2c->conn->owner);
2187 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002188 goto fail;
2189 }
2190
2191 h2c->max_id = 0;
2192 h2c->st0 = H2_CS_SETTINGS1;
2193 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002194
2195 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002196 /* ensure that what is pending is a valid SETTINGS frame
2197 * without an ACK.
2198 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002199 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002200 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002201 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002202 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002203 sess_log(h2c->conn->owner);
2204 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 goto fail;
2206 }
2207
2208 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2209 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2210 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2211 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002212 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002213 goto fail;
2214 }
2215
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002216 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002217 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2218 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2219 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002220 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002221 goto fail;
2222 }
2223
Willy Tarreau3bf69182018-12-21 15:34:50 +01002224 /* that's OK, switch to FRAME_P to process it. This is
2225 * a SETTINGS frame whose header has already been
2226 * deleted above.
2227 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002228 padlen = 0;
2229 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002230 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002231 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002232
2233 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002234 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002235 int ret = 0;
2236
2237 if (h2c->st0 >= H2_CS_ERROR)
2238 break;
2239
2240 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002241 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002242 break;
2243
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002244 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002245 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002246 if (!h2c->nb_streams) {
2247 /* only log if no other stream can report the error */
2248 sess_log(h2c->conn->owner);
2249 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002250 break;
2251 }
2252
Willy Tarreau3bf69182018-12-21 15:34:50 +01002253 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2254 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2255 * we read the pad length and drop it from the remaining
2256 * payload (one byte + the 9 remaining ones = 10 total
2257 * removed), so we have a frame payload starting after the
2258 * pad len. Flow controlled frames (DATA) also count the
2259 * padlen in the flow control, so it must be adjusted.
2260 */
2261 if (hdr.len < 1) {
2262 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2263 sess_log(h2c->conn->owner);
2264 goto fail;
2265 }
2266 hdr.len--;
2267
2268 if (b_data(&h2c->dbuf) < 10)
2269 break; // missing padlen
2270
2271 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2272
2273 if (padlen > hdr.len) {
2274 /* RFC7540#6.1 : pad length = length of
2275 * frame payload or greater => error.
2276 */
2277 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2278 sess_log(h2c->conn->owner);
2279 goto fail;
2280 }
2281
2282 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2283 h2c->rcvd_c++;
2284 h2c->rcvd_s++;
2285 }
2286 b_del(&h2c->dbuf, 1);
2287 }
2288 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002289
2290 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002291 h2c->dfl = hdr.len;
2292 h2c->dsi = hdr.sid;
2293 h2c->dft = hdr.ft;
2294 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002295 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002296 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002297
2298 /* check for minimum basic frame format validity */
2299 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2300 if (ret != H2_ERR_NO_ERROR) {
2301 h2c_error(h2c, ret);
2302 sess_log(h2c->conn->owner);
2303 goto fail;
2304 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002305 }
2306
2307 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002308 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2309
Willy Tarreau567beb82018-12-18 16:52:44 +01002310 if (tmp_h2s != h2s && h2s && h2s->cs &&
2311 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002312 (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 +01002313 /* we may have to signal the upper layers */
2314 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002315 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002316 }
2317 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002318
Willy Tarreaud7901432017-12-29 11:34:40 +01002319 if (h2c->st0 == H2_CS_FRAME_E)
2320 goto strm_err;
2321
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002322 if (h2s->st == H2_SS_IDLE &&
2323 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2324 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2325 * this state MUST be treated as a connection error
2326 */
2327 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002328 if (!h2c->nb_streams) {
2329 /* only log if no other stream can report the error */
2330 sess_log(h2c->conn->owner);
2331 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002332 break;
2333 }
2334
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002335 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2336 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2337 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002338 * this state MUST be treated as a stream error.
2339 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2340 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002341 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002342 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2343 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2344 else
2345 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002346 goto strm_err;
2347 }
2348
Willy Tarreauab837502017-12-27 15:07:30 +01002349 /* Below the management of frames received in closed state is a
2350 * bit hackish because the spec makes strong differences between
2351 * streams closed by receiving RST, sending RST, and seeing ES
2352 * in both directions. In addition to this, the creation of a
2353 * new stream reusing the identifier of a closed one will be
2354 * detected here. Given that we cannot keep track of all closed
2355 * streams forever, we consider that unknown closed streams were
2356 * closed on RST received, which allows us to respond with an
2357 * RST without breaking the connection (eg: to abort a transfer).
2358 * Some frames have to be silently ignored as well.
2359 */
2360 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002361 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002362 /* #5.1.1: The identifier of a newly
2363 * established stream MUST be numerically
2364 * greater than all streams that the initiating
2365 * endpoint has opened or reserved. This
2366 * governs streams that are opened using a
2367 * HEADERS frame and streams that are reserved
2368 * using PUSH_PROMISE. An endpoint that
2369 * receives an unexpected stream identifier
2370 * MUST respond with a connection error.
2371 */
2372 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2373 goto strm_err;
2374 }
2375
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002376 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002377 /* RFC7540#5.1:closed: an endpoint that
2378 * receives any frame other than PRIORITY after
2379 * receiving a RST_STREAM MUST treat that as a
2380 * stream error of type STREAM_CLOSED.
2381 *
2382 * Note that old streams fall into this category
2383 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002384 *
2385 * However, we cannot generalize this to all frame types. Those
2386 * carrying compression state must still be processed before
2387 * being dropped or we'll desynchronize the decoder. This can
2388 * happen with request trailers received after sending an
2389 * RST_STREAM, or with header/trailers responses received after
2390 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002391 */
2392 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2393 h2c->st0 = H2_CS_FRAME_E;
2394 goto strm_err;
2395 }
2396
2397 /* RFC7540#5.1:closed: if this state is reached as a
2398 * result of sending a RST_STREAM frame, the peer that
2399 * receives the RST_STREAM might have already sent
2400 * frames on the stream that cannot be withdrawn. An
2401 * endpoint MUST ignore frames that it receives on
2402 * closed streams after it has sent a RST_STREAM
2403 * frame. An endpoint MAY choose to limit the period
2404 * over which it ignores frames and treat frames that
2405 * arrive after this time as being in error.
2406 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002407 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002408 /* RFC7540#5.1:closed: any frame other than
2409 * PRIO/WU/RST in this state MUST be treated as
2410 * a connection error
2411 */
2412 if (h2c->dft != H2_FT_RST_STREAM &&
2413 h2c->dft != H2_FT_PRIORITY &&
2414 h2c->dft != H2_FT_WINDOW_UPDATE) {
2415 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2416 goto strm_err;
2417 }
2418 }
2419 }
2420
Willy Tarreauc0da1962017-10-30 18:38:00 +01002421#if 0
2422 // problem below: it is not possible to completely ignore such
2423 // streams as we need to maintain the compression state as well
2424 // and for this we need to completely process these frames (eg:
2425 // HEADERS frames) as well as counting DATA frames to emit
2426 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2427 // This is a typical case of layer violation where the
2428 // transported contents are critical to the connection's
2429 // validity and must be ignored at the same time :-(
2430
2431 /* graceful shutdown, ignore streams whose ID is higher than
2432 * the one advertised in GOAWAY. RFC7540#6.8.
2433 */
2434 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002435 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2436 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002437 h2c->dfl -= ret;
2438 ret = h2c->dfl == 0;
2439 goto strm_err;
2440 }
2441#endif
2442
Willy Tarreau7e98c052017-10-10 15:56:59 +02002443 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002444 case H2_FT_SETTINGS:
2445 if (h2c->st0 == H2_CS_FRAME_P)
2446 ret = h2c_handle_settings(h2c);
2447
2448 if (h2c->st0 == H2_CS_FRAME_A)
2449 ret = h2c_ack_settings(h2c);
2450 break;
2451
Willy Tarreaucf68c782017-10-10 17:11:41 +02002452 case H2_FT_PING:
2453 if (h2c->st0 == H2_CS_FRAME_P)
2454 ret = h2c_handle_ping(h2c);
2455
2456 if (h2c->st0 == H2_CS_FRAME_A)
2457 ret = h2c_ack_ping(h2c);
2458 break;
2459
Willy Tarreau26f95952017-07-27 17:18:30 +02002460 case H2_FT_WINDOW_UPDATE:
2461 if (h2c->st0 == H2_CS_FRAME_P)
2462 ret = h2c_handle_window_update(h2c, h2s);
2463 break;
2464
Willy Tarreau61290ec2017-10-17 08:19:21 +02002465 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002466 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2467 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2468 * frames' parsers consume all following CONTINUATION
2469 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002470 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002471 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2472 sess_log(h2c->conn->owner);
2473 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002474
Willy Tarreau13278b42017-10-13 19:23:14 +02002475 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002476 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002477 if (h2c->flags & H2_CF_IS_BACK)
2478 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2479 else
2480 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002481 if (tmp_h2s) {
2482 h2s = tmp_h2s;
2483 ret = 1;
2484 }
2485 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002486 break;
2487
Willy Tarreau454f9052017-10-26 19:40:35 +02002488 case H2_FT_DATA:
2489 if (h2c->st0 == H2_CS_FRAME_P)
2490 ret = h2c_frt_handle_data(h2c, h2s);
2491
2492 if (h2c->st0 == H2_CS_FRAME_A)
2493 ret = h2c_send_strm_wu(h2c);
2494 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002495
Willy Tarreau92153fc2017-12-03 19:46:19 +01002496 case H2_FT_PRIORITY:
2497 if (h2c->st0 == H2_CS_FRAME_P)
2498 ret = h2c_handle_priority(h2c);
2499 break;
2500
Willy Tarreaucd234e92017-08-18 10:59:39 +02002501 case H2_FT_RST_STREAM:
2502 if (h2c->st0 == H2_CS_FRAME_P)
2503 ret = h2c_handle_rst_stream(h2c, h2s);
2504 break;
2505
Willy Tarreaue96b0922017-10-30 00:28:29 +01002506 case H2_FT_GOAWAY:
2507 if (h2c->st0 == H2_CS_FRAME_P)
2508 ret = h2c_handle_goaway(h2c);
2509 break;
2510
Willy Tarreau1c661982017-10-30 13:52:01 +01002511 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002512 default:
2513 /* drop frames that we ignore. They may be larger than
2514 * the buffer so we drain all of their contents until
2515 * we reach the end.
2516 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002517 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2518 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 h2c->dfl -= ret;
2520 ret = h2c->dfl == 0;
2521 }
2522
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002523 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002524 /* We may have to send an RST if not done yet */
2525 if (h2s->st == H2_SS_ERROR)
2526 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002527
Willy Tarreaua20a5192017-12-27 11:02:06 +01002528 if (h2c->st0 == H2_CS_FRAME_E)
2529 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002530
Willy Tarreau7e98c052017-10-10 15:56:59 +02002531 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002532 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002533 break;
2534
2535 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002536 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002537 h2c->st0 = H2_CS_FRAME_H;
2538 }
2539 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002540
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002541 if (h2c->rcvd_c > 0 &&
2542 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2543 h2c_send_conn_wu(h2c);
2544
Willy Tarreau52eed752017-09-22 15:05:09 +02002545 fail:
2546 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002547 if (h2s && h2s->cs &&
2548 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002549 (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 +01002550 /* we may have to signal the upper layers */
2551 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002552 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002553 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002554
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002555 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002556}
2557
2558/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2559 * the end.
2560 */
2561static int h2_process_mux(struct h2c *h2c)
2562{
Olivier Houchard998410a2019-04-15 19:23:37 +02002563 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002564
Willy Tarreau01b44822018-10-03 14:26:37 +02002565 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2566 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2567 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2568 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2569 if (h2c->st0 == H2_CS_ERROR) {
2570 h2c->st0 = H2_CS_ERROR2;
2571 sess_log(h2c->conn->owner);
2572 }
2573 goto fail;
2574 }
2575 h2c->st0 = H2_CS_SETTINGS1;
2576 }
2577 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002578 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002579 return 1;
2580 }
2581
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002582 /* start by sending possibly pending window updates */
2583 if (h2c->rcvd_c > 0 &&
2584 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2585 h2c_send_conn_wu(h2c) < 0)
2586 goto fail;
2587
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002588 /* First we always process the flow control list because the streams
2589 * waiting there were already elected for immediate emission but were
2590 * blocked just on this.
2591 */
2592
Olivier Houchard998410a2019-04-15 19:23:37 +02002593 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002594 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2595 h2c->st0 >= H2_CS_ERROR)
2596 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002597
Willy Tarreauc234ae32019-05-13 17:56:11 +02002598 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002599 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002600
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002601 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002602 /* For some reason, the upper layer failed to subsribe again,
2603 * so remove it from the send_list
2604 */
2605 if (!h2s->send_wait) {
2606 LIST_DEL_INIT(&h2s->list);
2607 continue;
2608 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002609 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002610 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002611 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002612 }
2613
Olivier Houchard998410a2019-04-15 19:23:37 +02002614 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002615 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2616 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002617
Willy Tarreauc234ae32019-05-13 17:56:11 +02002618 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002619 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002620
Olivier Houchard998410a2019-04-15 19:23:37 +02002621 /* For some reason, the upper layer failed to subsribe again,
2622 * so remove it from the send_list
2623 */
2624 if (!h2s->send_wait) {
2625 LIST_DEL_INIT(&h2s->list);
2626 continue;
2627 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002628 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002629 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002630 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002631 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002632 }
2633
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002634 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002635 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002636 if (h2c->st0 == H2_CS_ERROR) {
2637 if (h2c->max_id >= 0) {
2638 h2c_send_goaway_error(h2c, NULL);
2639 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2640 return 0;
2641 }
2642
2643 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2644 }
2645 return 1;
2646 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002647 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002648}
2649
Willy Tarreau62f52692017-10-08 23:01:42 +02002650
Willy Tarreau479998a2018-11-18 06:30:59 +01002651/* Attempt to read data, and subscribe if none available.
2652 * The function returns 1 if data has been received, otherwise zero.
2653 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002654static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002655{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002656 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002657 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002658 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002659 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002660
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002661 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002662 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002663
Willy Tarreau315d8072017-12-10 22:17:57 +01002664 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002665 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002666
Willy Tarreau44e973f2018-03-01 17:49:30 +01002667 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002668 if (!buf) {
2669 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002670 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002671 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002672
Olivier Houchard7505f942018-08-21 18:10:44 +02002673 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002674 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002675 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2676 /* HTX in use : try to pre-align the buffer like the
2677 * rxbufs will be to optimize memory copies. We'll make
2678 * sure that the frame header lands at the end of the
2679 * HTX block to alias it upon recv. We cannot use the
2680 * head because rcv_buf() will realign the buffer if
2681 * it's empty. Thus we cheat and pretend we already
2682 * have a few bytes there.
2683 */
2684 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002685 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002686 }
2687 else
2688 max = b_room(buf);
2689
Olivier Houchard7505f942018-08-21 18:10:44 +02002690 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002691 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002692 else
2693 ret = 0;
2694 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002695
Olivier Houchard53216e72018-10-10 15:46:36 +02002696 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002697 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002698
Olivier Houcharda1411e62018-08-17 18:42:48 +02002699 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002700 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002701 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002702 }
2703
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002704 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002705 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002706 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002707}
2708
Willy Tarreau479998a2018-11-18 06:30:59 +01002709/* Try to send data if possible.
2710 * The function returns 1 if data have been sent, otherwise zero.
2711 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002712static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002713{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002714 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002715 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002716 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002717
2718 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002719 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002720
Olivier Houchard7505f942018-08-21 18:10:44 +02002721
Willy Tarreaua2af5122017-10-09 11:56:46 +02002722 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2723 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002724 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002725 }
2726
Willy Tarreaubc933932017-10-09 16:21:43 +02002727 /* This loop is quite simple : it tries to fill as much as it can from
2728 * pending streams into the existing buffer until it's reportedly full
2729 * or the end of send requests is reached. Then it tries to send this
2730 * buffer's contents out, marks it not full if at least one byte could
2731 * be sent, and tries again.
2732 *
2733 * The snd_buf() function normally takes a "flags" argument which may
2734 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2735 * data immediately comes and CO_SFL_STREAMER to indicate that the
2736 * connection is streaming lots of data (used to increase TLS record
2737 * size at the expense of latency). The former can be sent any time
2738 * there's a buffer full flag, as it indicates at least one stream
2739 * attempted to send and failed so there are pending data. An
2740 * alternative would be to set it as long as there's an active stream
2741 * but that would be problematic for ACKs until we have an absolute
2742 * guarantee that all waiters have at least one byte to send. The
2743 * latter should possibly not be set for now.
2744 */
2745
2746 done = 0;
2747 while (!done) {
2748 unsigned int flags = 0;
2749
2750 /* fill as much as we can into the current buffer */
2751 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2752 done = h2_process_mux(h2c);
2753
Olivier Houchard2b094432019-01-29 18:28:36 +01002754 if (h2c->flags & H2_CF_MUX_MALLOC)
2755 break;
2756
Willy Tarreaubc933932017-10-09 16:21:43 +02002757 if (conn->flags & CO_FL_ERROR)
2758 break;
2759
2760 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2761 flags |= CO_SFL_MSG_MORE;
2762
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002763 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002764 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002765 if (!ret)
2766 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002767 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002768 b_del(&h2c->mbuf, ret);
2769 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002770 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002771
2772 /* wrote at least one byte, the buffer is not full anymore */
2773 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2774 }
2775
Willy Tarreaua2af5122017-10-09 11:56:46 +02002776 if (conn->flags & CO_FL_SOCK_WR_SH) {
2777 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002778 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002779 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002780 /* We're not full anymore, so we can wake any task that are waiting
2781 * for us.
2782 */
2783 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002784 struct h2s *h2s;
2785
2786 list_for_each_entry(h2s, &h2c->send_list, list) {
2787 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2788 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002789
Willy Tarreauc234ae32019-05-13 17:56:11 +02002790 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002791 continue;
2792
Olivier Houchard998410a2019-04-15 19:23:37 +02002793 /* For some reason, the upper layer failed to subsribe again,
2794 * so remove it from the send_list
2795 */
2796 if (!h2s->send_wait) {
2797 LIST_DEL_INIT(&h2s->list);
2798 continue;
2799 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002800 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002801 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002802 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002803 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002804 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002805 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002806 /* We're done, no more to send */
2807 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002808 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002809schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002810 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002811 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002812 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002813}
2814
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002815/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002816static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2817{
2818 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002819 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002820
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002821 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002822 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002823 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002824 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002825 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002826 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002827 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002828}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002829
Willy Tarreau62f52692017-10-08 23:01:42 +02002830/* callback called on any event by the connection handler.
2831 * It applies changes and returns zero, or < 0 if it wants immediate
2832 * destruction of the connection (which normally doesn not happen in h2).
2833 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002834static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002835{
Olivier Houchard7505f942018-08-21 18:10:44 +02002836 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002837
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002838 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002839 h2_process_demux(h2c);
2840
2841 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002842 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002843
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002844 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002845 h2c->flags &= ~H2_CF_DEM_DFULL;
2846 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002847 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002848
Willy Tarreau0b37d652018-10-03 10:33:02 +02002849 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002850 /* frontend is stopping, reload likely in progress, let's try
2851 * to announce a graceful shutdown if not yet done. We don't
2852 * care if it fails, it will be tried again later.
2853 */
2854 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2855 if (h2c->last_sid < 0)
2856 h2c->last_sid = (1U << 31) - 1;
2857 h2c_send_goaway_error(h2c, NULL);
2858 }
2859 }
2860
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002861 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002862 * If we received early data, and the handshake is done, wake
2863 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002864 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002865 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2866 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2867 struct eb32_node *node;
2868 struct h2s *h2s;
2869
2870 h2c->flags |= H2_CF_WAIT_FOR_HS;
2871 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2872
2873 while (node) {
2874 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002875 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002876 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002877 node = eb32_next(node);
2878 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002879 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002880
Willy Tarreau26bd7612017-10-09 16:47:04 +02002881 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002882 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2883 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2884 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002885 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002886
2887 if (eb_is_empty(&h2c->streams_by_id)) {
2888 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002889 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002890 return -1;
2891 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002892 }
2893
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002895 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002896
Olivier Houchard53216e72018-10-10 15:46:36 +02002897 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2898 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2899 (h2c->st0 != H2_CS_ERROR &&
2900 !b_data(&h2c->mbuf) &&
2901 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2902 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002903 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002904
Willy Tarreau3f133572017-10-31 19:21:06 +01002905 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002906 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002907 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002908 task_queue(h2c->task);
2909 }
2910 else
2911 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002912 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002913
Olivier Houchard7505f942018-08-21 18:10:44 +02002914 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002915 return 0;
2916}
2917
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002918/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002919static int h2_wake(struct connection *conn)
2920{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002921 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002922
2923 return (h2_process(h2c));
2924}
2925
Willy Tarreauea392822017-10-31 10:02:25 +01002926/* Connection timeout management. The principle is that if there's no receipt
2927 * nor sending for a certain amount of time, the connection is closed. If the
2928 * MUX buffer still has lying data or is not allocatable, the connection is
2929 * immediately killed. If it's allocatable and empty, we attempt to send a
2930 * GOAWAY frame.
2931 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002932static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002933{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002934 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002935 int expired = tick_is_expired(t->expire, now_ms);
2936
Willy Tarreau0975f112018-03-29 15:22:59 +02002937 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002938 return t;
2939
Olivier Houchard3f795f72019-04-17 22:51:06 +02002940 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002941
2942 if (!h2c) {
2943 /* resources were already deleted */
2944 return NULL;
2945 }
2946
2947 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002948 h2c_error(h2c, H2_ERR_NO_ERROR);
2949 h2_wake_some_streams(h2c, 0, 0);
2950
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002951 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002952 /* don't even try to send a GOAWAY, the buffer is stuck */
2953 h2c->flags |= H2_CF_GOAWAY_FAILED;
2954 }
2955
2956 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002957 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002958 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2959 h2c->flags |= H2_CF_GOAWAY_FAILED;
2960
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002961 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002962 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 +02002963 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002964 b_del(&h2c->mbuf, ret);
2965 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002966 }
2967 }
Willy Tarreauea392822017-10-31 10:02:25 +01002968
Willy Tarreau0975f112018-03-29 15:22:59 +02002969 /* either we can release everything now or it will be done later once
2970 * the last stream closes.
2971 */
2972 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002973 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002974
Willy Tarreauea392822017-10-31 10:02:25 +01002975 return NULL;
2976}
2977
2978
Willy Tarreau62f52692017-10-08 23:01:42 +02002979/*******************************************/
2980/* functions below are used by the streams */
2981/*******************************************/
2982
2983/*
2984 * Attach a new stream to a connection
2985 * (Used for outgoing connections)
2986 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002987static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002988{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002989 struct conn_stream *cs;
2990 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002991 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002992
2993 cs = cs_new(conn);
2994 if (!cs)
2995 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002996 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002997 if (!h2s) {
2998 cs_free(cs);
2999 return NULL;
3000 }
3001 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003002}
3003
Willy Tarreaufafd3982018-11-18 21:29:20 +01003004/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3005 * We have to scan because we may have some orphan streams. It might be
3006 * beneficial to scan backwards from the end to reduce the likeliness to find
3007 * orphans.
3008 */
3009static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3010{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003011 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003012 struct h2s *h2s;
3013 struct eb32_node *node;
3014
3015 node = eb32_first(&h2c->streams_by_id);
3016 while (node) {
3017 h2s = container_of(node, struct h2s, by_id);
3018 if (h2s->cs)
3019 return h2s->cs;
3020 node = eb32_next(node);
3021 }
3022 return NULL;
3023}
3024
Willy Tarreau62f52692017-10-08 23:01:42 +02003025/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003026 * Destroy the mux and the associated connection, if it is no longer used
3027 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003028static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003029{
Christopher Faulet73c12072019-04-08 11:23:22 +02003030 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003031
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003032 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003033 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003034}
3035
3036/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003037 * Detach the stream from the connection and possibly release the connection.
3038 */
3039static void h2_detach(struct conn_stream *cs)
3040{
Willy Tarreau60935142017-10-16 18:11:19 +02003041 struct h2s *h2s = cs->ctx;
3042 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003043 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003044
3045 cs->ctx = NULL;
3046 if (!h2s)
3047 return;
3048
Olivier Houchard998410a2019-04-15 19:23:37 +02003049 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003050 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003051 h2s->send_wait != &h2s->wait_event) {
3052 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3053 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003054 /*
3055 * At this point, the stream_interface is supposed to have called
3056 * h2_unsubscribe(), so the only way there's still a
3057 * subscription that came from the stream_interface (as we
3058 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3059 * without the stream_interface involved) is that we subscribed
3060 * for sending, we woke the tasklet up and removed the
3061 * SUB_RETRY_SEND flag, so the stream_interface would not
3062 * know it has to unsubscribe for send, but the tasklet hasn't
3063 * run yet. Make sure to handle that by explicitely setting
3064 * send_wait to NULL, as nothing else will do it for us.
3065 */
3066 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003067 }
3068
Olivier Houchardf502aca2018-12-14 19:42:40 +01003069 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003070 h2c = h2s->h2c;
3071 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003072 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003073 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3074 !h2_frt_has_too_many_cs(h2c)) {
3075 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003076 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003077 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003078 }
Willy Tarreau60935142017-10-16 18:11:19 +02003079
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003080 /* this stream may be blocked waiting for some data to leave (possibly
3081 * an ES or RST frame), so orphan it in this case.
3082 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003083 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003084 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003085 (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 +01003086 return;
3087
Willy Tarreau45f752e2017-10-30 15:44:59 +01003088 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3089 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3090 /* unblock the connection if it was blocked on this
3091 * stream.
3092 */
3093 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3094 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003095 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003096 }
3097
Willy Tarreau71049cc2018-03-28 13:56:39 +02003098 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003099
Olivier Houchard8a786902018-12-15 16:05:40 +01003100 if (h2c->flags & H2_CF_IS_BACK &&
3101 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003102 if (!(h2c->conn->flags &
3103 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3104 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003105 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003106 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3107 h2c->conn->owner = NULL;
3108 if (eb_is_empty(&h2c->streams_by_id)) {
3109 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3110 /* The server doesn't want it, let's kill the connection right away */
3111 h2c->conn->mux->destroy(h2c->conn);
3112 return;
3113 }
3114 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003115 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003116 if (eb_is_empty(&h2c->streams_by_id)) {
3117 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3118 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3119 return;
3120 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003121 /* Never ever allow to reuse a connection from a non-reuse backend */
3122 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3123 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003124 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003125 struct server *srv = objt_server(h2c->conn->target);
3126
3127 if (srv) {
3128 if (h2c->conn->flags & CO_FL_PRIVATE)
3129 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3130 else
3131 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3132 }
3133
3134 }
3135 }
3136 }
3137
Willy Tarreaue323f342018-03-28 13:51:45 +02003138 /* We don't want to close right now unless we're removing the
3139 * last stream, and either the connection is in error, or it
3140 * reached the ID already specified in a GOAWAY frame received
3141 * or sent (as seen by last_sid >= 0).
3142 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003143 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003144 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003145 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003146 }
3147 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003148 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003149 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3150 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003151 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003152 else
3153 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003154 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003155}
3156
Willy Tarreau88bdba32019-05-13 18:17:53 +02003157/* Performs a synchronous or asynchronous shutr(). */
3158static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003159{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003160 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003161 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003162
Willy Tarreauf983d002019-05-14 10:40:21 +02003163 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003164 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003165
Willy Tarreau18059042019-01-31 19:12:48 +01003166 /* a connstream may require us to immediately kill the whole connection
3167 * for example because of a "tcp-request content reject" rule that is
3168 * normally used to limit abuse. In this case we schedule a goaway to
3169 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003170 */
Willy Tarreau18059042019-01-31 19:12:48 +01003171 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3172 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3173 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3174 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3175 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003176 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3177 /* Nothing was never sent for this stream, so reset with
3178 * REFUSED_STREAM error to let the client retry the
3179 * request.
3180 */
3181 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3182 }
Willy Tarreau18059042019-01-31 19:12:48 +01003183
Willy Tarreau90c32322017-11-24 08:00:30 +01003184 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003185 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003186 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003187
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003188 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003189 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003190 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003191 done:
3192 h2s->flags &= ~H2_SF_WANT_SHUTR;
3193 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003195 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003196 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003197 if (h2s->flags & H2_SF_BLK_MFCTL) {
3198 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3199 h2s->send_wait = sw;
3200 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3201 h2s->send_wait = sw;
3202 LIST_ADDQ(&h2c->send_list, &h2s->list);
3203 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003204 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003205 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003206 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003207 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003208}
3209
Willy Tarreau88bdba32019-05-13 18:17:53 +02003210/* Performs a synchronous or asynchronous shutw(). */
3211static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003212{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003213 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003214 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003215
Willy Tarreauf983d002019-05-14 10:40:21 +02003216 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003217 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003218
Willy Tarreauf983d002019-05-14 10:40:21 +02003219 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3220 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003221 /* we can cleanly close using an empty data frame only after headers */
3222
3223 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3224 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003225 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003226
3227 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003228 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003229 else
3230 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003231 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003232 /* a connstream may require us to immediately kill the whole connection
3233 * for example because of a "tcp-request content reject" rule that is
3234 * normally used to limit abuse. In this case we schedule a goaway to
3235 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003236 */
Willy Tarreau18059042019-01-31 19:12:48 +01003237 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3238 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3239 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3240 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3241 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003242 else {
3243 /* Nothing was never sent for this stream, so reset with
3244 * REFUSED_STREAM error to let the client retry the
3245 * request.
3246 */
3247 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3248 }
Willy Tarreau18059042019-01-31 19:12:48 +01003249
Willy Tarreau90c32322017-11-24 08:00:30 +01003250 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003251 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003252 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003253
Willy Tarreau00dd0782018-03-01 16:31:34 +01003254 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003255 }
3256
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003257 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003258 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003259 done:
3260 h2s->flags &= ~H2_SF_WANT_SHUTW;
3261 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003262
3263 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003264 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003265 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003266 if (h2s->flags & H2_SF_BLK_MFCTL) {
3267 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3268 h2s->send_wait = sw;
3269 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3270 h2s->send_wait = sw;
3271 LIST_ADDQ(&h2c->send_list, &h2s->list);
3272 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003273 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003274 /* let the handler know we want to shutw */
3275 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003276 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003277}
3278
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003279/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3280 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3281 * and prevented the last frame from being emitted.
3282 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003283static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3284{
3285 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003286 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003287
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003288 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003289 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003290 h2_do_shutw(h2s);
3291
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003292 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003293 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003294
Willy Tarreau88bdba32019-05-13 18:17:53 +02003295 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003296 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003297 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003298
Willy Tarreau88bdba32019-05-13 18:17:53 +02003299 if (!h2s->cs) {
3300 h2s_destroy(h2s);
3301 if (h2c_is_dead(h2c))
3302 h2_release(h2c);
3303 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003304 }
3305
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003306 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003307}
3308
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003309/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003310static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3311{
3312 struct h2s *h2s = cs->ctx;
3313
3314 if (!mode)
3315 return;
3316
3317 h2_do_shutr(h2s);
3318}
3319
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003320/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003321static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3322{
3323 struct h2s *h2s = cs->ctx;
3324
3325 h2_do_shutw(h2s);
3326}
3327
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003328/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003329 * HTX request or response depending on the connection's side. Returns a
3330 * positive value on success, a negative value on failure, or 0 if it couldn't
3331 * proceed. May report connection errors in h2c->errcode if the frame is
3332 * non-decodable and the connection unrecoverable. In absence of connection
3333 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003334 *
3335 * The function may fold CONTINUATION frames into the initial HEADERS frame
3336 * by removing padding and next frame header, then moving the CONTINUATION
3337 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3338 * leaving a hole between the main frame and the beginning of the next one.
3339 * The possibly remaining incomplete or next frame at the end may be moved
3340 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3341 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3342 *
3343 * A buffer at the beginning of processing may look like this :
3344 *
3345 * ,---.---------.-----.--------------.--------------.------.---.
3346 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3347 * `---^---------^-----^--------------^--------------^------^---'
3348 * | | <-----> | |
3349 * area | dpl | wrap
3350 * |<--------------> |
3351 * | dfl |
3352 * |<-------------------------------------------------->|
3353 * head data
3354 *
3355 * Padding is automatically overwritten when folding, participating to the
3356 * hole size after dfl :
3357 *
3358 * ,---.------------------------.-----.--------------.------.---.
3359 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3360 * `---^------------------------^-----^--------------^------^---'
3361 * | | <-----> | |
3362 * area | hole | wrap
3363 * |<-----------------------> |
3364 * | dfl |
3365 * |<-------------------------------------------------->|
3366 * head data
3367 *
3368 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3369 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3370 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003371 *
3372 * The <flags> field must point to either the stream's flags or to a copy of it
3373 * so that the function can update the following flags :
3374 * - H2_SF_DATA_CLEN when content-length is seen
3375 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3376 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003377 *
3378 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3379 * decoding, in order to detect if we're dealing with a headers or a trailers
3380 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003381 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003382static 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 +02003383{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003384 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003385 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003386 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003387 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003388 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003389 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003390 int flen; // header frame len
3391 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003392 int ret = 0;
3393 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003394 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003395 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003396
Willy Tarreauea18f862018-12-22 20:19:26 +01003397next_frame:
3398 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3399 goto leave; // incomplete input frame
3400
3401 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3402 * this case, we'll try to paste it immediately after the initial
3403 * HEADERS frame payload and kill any possible padding. The initial
3404 * frame's length will be increased to represent the concatenation
3405 * of the two frames. The next frame is read from position <tlen>
3406 * and written at position <flen> (minus padding if some is present).
3407 */
3408 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3409 struct h2_fh hdr;
3410 int clen; // CONTINUATION frame's payload length
3411
3412 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3413 /* no more data, the buffer may be full, either due to
3414 * too large a frame or because of too large a hole that
3415 * we're going to compact at the end.
3416 */
3417 goto leave;
3418 }
3419
3420 if (hdr.ft != H2_FT_CONTINUATION) {
3421 /* RFC7540#6.10: frame of unexpected type */
3422 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3423 goto fail;
3424 }
3425
3426 if (hdr.sid != h2c->dsi) {
3427 /* RFC7540#6.10: frame of different stream */
3428 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3429 goto fail;
3430 }
3431
3432 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3433 /* RFC7540#4.2: invalid frame length */
3434 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3435 goto fail;
3436 }
3437
3438 /* detect when we must stop aggragating frames */
3439 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3440
3441 /* Take as much as we can of the CONTINUATION frame's payload */
3442 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3443 if (clen > hdr.len)
3444 clen = hdr.len;
3445
3446 /* Move the frame's payload over the padding, hole and frame
3447 * header. At least one of hole or dpl is null (see diagrams
3448 * above). The hole moves after the new aggragated frame.
3449 */
3450 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3451 h2c->dfl += clen - h2c->dpl;
3452 hole += h2c->dpl + 9;
3453 h2c->dpl = 0;
3454 goto next_frame;
3455 }
3456
3457 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003458
Willy Tarreau13278b42017-10-13 19:23:14 +02003459 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003460 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003461 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003462 copy = alloc_trash_chunk();
3463 if (!copy) {
3464 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3465 goto fail;
3466 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003467 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3468 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3469 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003470 }
3471
Willy Tarreau13278b42017-10-13 19:23:14 +02003472 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3473 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003474 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003475 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3476 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003477 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003478 }
3479
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003480 if (flen < 5) {
3481 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3482 goto fail;
3483 }
3484
Willy Tarreau13278b42017-10-13 19:23:14 +02003485 hdrs += 5; // stream dep = 4, weight = 1
3486 flen -= 5;
3487 }
3488
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003489 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003490 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003491 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003492 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003493
Willy Tarreau937f7602018-02-26 15:22:17 +01003494 /* we can't retry a failed decompression operation so we must be very
3495 * careful not to take any risks. In practice the output buffer is
3496 * always empty except maybe for trailers, in which case we simply have
3497 * to wait for the upper layer to finish consuming what is available.
3498 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003499
3500 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003501 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003502 if (!htx_is_empty(htx)) {
3503 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003504 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003505 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003506 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003507 if (b_data(rxbuf)) {
3508 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003509 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003510 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003511
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003512 rxbuf->head = 0;
3513 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003514 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003515
Willy Tarreau25919232019-01-03 14:48:18 +01003516 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003517 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3518 sizeof(list)/sizeof(list[0]), tmp);
3519 if (outlen < 0) {
3520 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3521 goto fail;
3522 }
3523
Willy Tarreau25919232019-01-03 14:48:18 +01003524 /* The PACK decompressor was updated, let's update the input buffer and
3525 * the parser's state to commit these changes and allow us to later
3526 * fail solely on the stream if needed.
3527 */
3528 b_del(&h2c->dbuf, h2c->dfl + hole);
3529 h2c->dfl = hole = 0;
3530 h2c->st0 = H2_CS_FRAME_H;
3531
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003532 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003533 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003534
Willy Tarreau88d138e2019-01-02 19:38:14 +01003535 if (*flags & H2_SF_HEADERS_RCVD)
3536 goto trailers;
3537
3538 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003539 if (htx) {
3540 /* HTX mode */
3541 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003542 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003543 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003544 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003545 } else {
3546 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003547 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003548 if (outlen > 0)
3549 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003550 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003551
3552 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003553 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003554 goto fail;
3555 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003556
Willy Tarreau174b06a2018-04-25 18:13:58 +02003557 if (msgf & H2_MSGF_BODY) {
3558 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003559 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003560 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003561 if (htx)
3562 htx->extra = *body_len;
3563 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003564 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003565 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003566 }
3567
Willy Tarreau88d138e2019-01-02 19:38:14 +01003568 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003569 /* indicate that a HEADERS frame was received for this stream, except
3570 * for 1xx responses. For 1xx responses, another HEADERS frame is
3571 * expected.
3572 */
3573 if (!(msgf & H2_MSGF_RSP_1XX))
3574 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003575
Christopher Faulet0b465482019-02-19 15:14:23 +01003576 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003577 /* Mark the end of message, either using EOM in HTX or with the
3578 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003579 * is not set during headers with END_STREAM. For HTX trailers,
3580 * we must not leave an HTX trailers block not followed by an
3581 * EOM block, the two must be atomic. Thus if we fail to emit
3582 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003583 */
3584 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003585 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3586 struct htx_blk *tail = htx_get_tail_blk(htx);
3587
3588 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3589 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003590 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003591 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003592 }
3593 else if (*flags & H2_SF_DATA_CHNK) {
3594 if (!b_putblk(rxbuf, "\r\n", 2))
3595 goto fail;
3596 }
3597 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003598
Willy Tarreau86277d42019-01-02 15:36:11 +01003599 /* success */
3600 ret = 1;
3601
Willy Tarreau68dd9852017-07-03 14:44:26 +02003602 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003603 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003604 * move the remaining data over it.
3605 */
3606 if (hole) {
3607 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3608 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3609 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3610 b_sub(&h2c->dbuf, hole);
3611 }
3612
Willy Tarreau97215ca2019-04-29 10:20:21 +02003613 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003614 /* too large frames */
3615 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003616 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003617 }
3618
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003619 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003620 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003621 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003622 return ret;
3623
Willy Tarreau68dd9852017-07-03 14:44:26 +02003624 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003625 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003626 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003627
3628 trailers:
3629 /* This is the last HEADERS frame hence a trailer */
3630
3631 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3632 /* It's a trailer but it's missing ES flag */
3633 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3634 goto fail;
3635 }
3636
3637 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3638 * block, and when using chunks we must send the 0 CRLF marker. For
3639 * other modes, the trailers are silently dropped.
3640 */
3641 if (htx) {
3642 if (!htx_add_endof(htx, HTX_BLK_EOD))
3643 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003644 if (h2_make_htx_trailers(list, htx) <= 0)
3645 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003646 }
3647 else if (*flags & H2_SF_DATA_CHNK) {
3648 /* Legacy mode with chunked encoding : we must finalize the
3649 * data block message emit the trailing CRLF */
3650 if (!b_putblk(rxbuf, "0\r\n", 3))
3651 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003652
3653 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3654 if (outlen > 0)
3655 b_add(rxbuf, outlen);
3656 else
3657 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003658 }
3659
3660 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003661}
3662
Willy Tarreau454f9052017-10-26 19:40:35 +02003663/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3664 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3665 * in use, a new chunk is emitted for each frame. This is supposed to fit
3666 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3667 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3668 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003669 * parser state is automatically updated. Returns > 0 if it could completely
3670 * send the current frame, 0 if it couldn't complete, in which case
3671 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3672 * DATA frame can return 0 as a valid result). Stream errors are reported in
3673 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3674 * have checked the frame header and ensured that the frame was complete or the
3675 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003676 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003677static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003678{
3679 struct h2c *h2c = h2s->h2c;
3680 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003681 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003682 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003683 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003684 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003685
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003686 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003687
Olivier Houchard638b7992018-08-16 15:41:52 +02003688 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003689 if (!csbuf) {
3690 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003691 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003692 }
3693
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003694try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003695 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003696 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3697 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003698 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003699 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003700
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003701 if (flen > b_data(&h2c->dbuf)) {
3702 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003703 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003704 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003705 }
3706
Willy Tarreaua9b77962019-01-31 07:23:00 +01003707 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003708 block1 = htx_free_data_space(htx);
3709 if (!block1) {
3710 h2c->flags |= H2_CF_DEM_SFULL;
3711 goto fail;
3712 }
3713 if (flen > block1)
3714 flen = block1;
3715
3716 /* here, flen is the max we can copy into the output buffer */
3717 block1 = b_contig_data(&h2c->dbuf, 0);
3718 if (flen > block1)
3719 flen = block1;
3720
3721 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3722 h2c->flags |= H2_CF_DEM_SFULL;
3723 goto fail;
3724 }
3725
3726 b_del(&h2c->dbuf, flen);
3727 h2c->dfl -= flen;
3728 h2c->rcvd_c += flen;
3729 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003730
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003731 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003732 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003733 htx->extra = h2s->body_len;
3734 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003735 goto try_again;
3736 }
3737 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003738 /* it doesn't fit and the buffer is fragmented,
3739 * so let's defragment it and try again.
3740 */
3741 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003742 }
3743
Willy Tarreaueba10f22018-04-25 20:44:22 +02003744 /* chunked-encoding requires more room */
3745 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003746 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003747 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3748 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3749 (chklen < 1048576) ? 4 : 8;
3750 chklen += 4; // CRLF, CRLF
3751 }
3752
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003753 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003754 if (flen + chklen > b_room(csbuf)) {
3755 if (chklen >= b_room(csbuf)) {
3756 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003757 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 }
3759 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003760 }
3761
3762 if (h2s->flags & H2_SF_DATA_CHNK) {
3763 /* emit the chunk size */
3764 unsigned int chksz = flen;
3765 char str[10];
3766 char *beg;
3767
3768 beg = str + sizeof(str);
3769 *--beg = '\n';
3770 *--beg = '\r';
3771 do {
3772 *--beg = hextab[chksz & 0xF];
3773 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003774 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003775 }
3776
Willy Tarreau454f9052017-10-26 19:40:35 +02003777 /* Block1 is the length of the first block before the buffer wraps,
3778 * block2 is the optional second block to reach the end of the frame.
3779 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003780 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003781 if (block1 > flen)
3782 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003783 block2 = flen - block1;
3784
3785 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003786 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003787
3788 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003789 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003790
Willy Tarreaueba10f22018-04-25 20:44:22 +02003791 if (h2s->flags & H2_SF_DATA_CHNK) {
3792 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003793 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003794 }
3795
Willy Tarreau454f9052017-10-26 19:40:35 +02003796 /* now mark the input data as consumed (will be deleted from the buffer
3797 * by the caller when seeing FRAME_A after sending the window update).
3798 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003799 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003800 h2c->dfl -= flen;
3801 h2c->rcvd_c += flen;
3802 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3803
Willy Tarreau1915ca22019-01-24 11:49:37 +01003804 if (h2s->flags & H2_SF_DATA_CLEN)
3805 h2s->body_len -= flen;
3806
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003807 if (h2c->dfl > h2c->dpl) {
3808 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003809 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003810 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003811 }
3812
Willy Tarreau4a28da12018-01-04 14:41:00 +01003813 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003814 /* here we're done with the frame, all the payload (except padding) was
3815 * transferred.
3816 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003817
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003818 if (h2c->dff & H2_F_DATA_END_STREAM) {
3819 if (htx) {
3820 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3821 h2c->flags |= H2_CF_DEM_SFULL;
3822 goto fail;
3823 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003824 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003825 else if (h2s->flags & H2_SF_DATA_CHNK) {
3826 /* emit the trailing 0 CRLF CRLF */
3827 if (b_room(csbuf) < 5) {
3828 h2c->flags |= H2_CF_DEM_SFULL;
3829 goto fail;
3830 }
3831 chklen += 5;
3832 b_putblk(csbuf, "0\r\n\r\n", 5);
3833 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003834 }
3835
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003836 h2c->rcvd_c += h2c->dpl;
3837 h2c->rcvd_s += h2c->dpl;
3838 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003839 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003840 if (htx)
3841 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003842 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003843 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003844 if (htx)
3845 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003846 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003847}
3848
Willy Tarreau5dd17352018-06-14 13:33:30 +02003849/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3850 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3851 * number of bytes sent. The caller must check the stream's status to detect
3852 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003853 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003854static 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 +02003855{
3856 struct http_hdr list[MAX_HTTP_HDR];
3857 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003858 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003859 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003860 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861 int es_now = 0;
3862 int ret = 0;
3863 int hdr;
3864
3865 if (h2c_mux_busy(h2c, h2s)) {
3866 h2s->flags |= H2_SF_BLK_MBUSY;
3867 return 0;
3868 }
3869
Willy Tarreau44e973f2018-03-01 17:49:30 +01003870 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003871 h2c->flags |= H2_CF_MUX_MALLOC;
3872 h2s->flags |= H2_SF_BLK_MROOM;
3873 return 0;
3874 }
3875
3876 /* First, try to parse the H1 response and index it into <list>.
3877 * NOTE! Since it comes from haproxy, we *know* that a response header
3878 * block does not wrap and we can safely read it this way without
3879 * having to realign the buffer.
3880 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003881 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003882 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003883 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003884 /* incomplete or invalid response, this is abnormal coming from
3885 * haproxy and may only result in a bad errorfile or bad Lua code
3886 * so that won't be fixed, raise an error now.
3887 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003888 * FIXME: we should instead add the ability to only return a
3889 * 502 bad gateway. But in theory this is not supposed to
3890 * happen.
3891 */
3892 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3893 ret = 0;
3894 goto end;
3895 }
3896
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003897 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003898
3899 /* certain statuses have no body or an empty one, regardless of
3900 * what the headers say.
3901 */
3902 if (sl.st.status >= 100 && sl.st.status < 200) {
3903 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3904 h1m->curr_len = h1m->body_len = 0;
3905 }
3906 else if (sl.st.status == 204 || sl.st.status == 304) {
3907 /* no contents, claim c-len is present and set to zero */
3908 h1m->flags &= ~H1_MF_CHNK;
3909 h1m->flags |= H1_MF_CLEN;
3910 h1m->curr_len = h1m->body_len = 0;
3911 }
3912
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003913 chunk_reset(&outbuf);
3914
3915 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003916 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003917 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003918 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003920 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003921 break;
3922 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003923 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924 }
3925
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003926 if (outbuf.size < 9)
3927 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003928
3929 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003930 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3931 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3932 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003933
3934 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003935 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003936 /* this is an unparsable response */
3937 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3938 ret = 0;
3939 goto end;
3940 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003941
3942 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003943 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003944 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003945 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003946 }
3947
3948 /* encode all headers, stop at empty name */
3949 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003950 /* these ones do not exist in H2 and must be dropped. */
3951 if (isteq(list[hdr].n, ist("connection")) ||
3952 isteq(list[hdr].n, ist("proxy-connection")) ||
3953 isteq(list[hdr].n, ist("keep-alive")) ||
3954 isteq(list[hdr].n, ist("upgrade")) ||
3955 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003956 continue;
3957
3958 if (isteq(list[hdr].n, ist("")))
3959 break; // end
3960
3961 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3962 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003963 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003964 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003965 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003966 }
3967 }
3968
3969 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003970 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003971 es_now = 1;
3972
3973 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003974 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003975
3976 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003977 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003978
3979 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003980 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003981
3982 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003983 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003984 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003985
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003987 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003988 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003989
Willy Tarreau801250e2018-09-11 11:45:04 +02003990 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003991 h2s->flags |= H2_SF_ES_SENT;
3992 if (h2s->st == H2_SS_OPEN)
3993 h2s->st = H2_SS_HLOC;
3994 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003995 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003996 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003997 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003998 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003999 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004000 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004001 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004002 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004003 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004004
4005 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004006
4007 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004008 //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 +02004009 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004010 full:
4011 h1m_init_res(h1m);
4012 h1m->err_pos = -1; // don't care about errors on the response path
4013 h2c->flags |= H2_CF_MUX_MFULL;
4014 h2s->flags |= H2_SF_BLK_MROOM;
4015 ret = 0;
4016 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004017}
4018
Willy Tarreau5dd17352018-06-14 13:33:30 +02004019/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4020 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4021 * the number of bytes sent. The caller must check the stream's status to
4022 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004023 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004024static 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 +02004025{
4026 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004027 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004028 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004030 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004031 int es_now = 0;
4032 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004033 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004034 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004035
4036 if (h2c_mux_busy(h2c, h2s)) {
4037 h2s->flags |= H2_SF_BLK_MBUSY;
4038 goto end;
4039 }
4040
Willy Tarreau44e973f2018-03-01 17:49:30 +01004041 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004042 h2c->flags |= H2_CF_MUX_MALLOC;
4043 h2s->flags |= H2_SF_BLK_MROOM;
4044 goto end;
4045 }
4046
4047 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004048 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004049 goto end;
4050
4051 chunk_reset(&outbuf);
4052
4053 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004054 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004055 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004056 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004057
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004058 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004059 break;
4060 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004061 /* If there are pending data in the output buffer, and we have
4062 * less than 1/4 of the mbuf's size and everything fits, we'll
4063 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4064 * is full and wait, to save some slow realign calls.
4065 */
4066 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4067 h2c->flags |= H2_CF_MUX_MFULL;
4068 h2s->flags |= H2_SF_BLK_MROOM;
4069 goto end;
4070 }
4071
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004072 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004073 }
4074
4075 if (outbuf.size < 9) {
4076 h2c->flags |= H2_CF_MUX_MFULL;
4077 h2s->flags |= H2_SF_BLK_MROOM;
4078 goto end;
4079 }
4080
4081 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004082 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4083 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4084 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004085
4086 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4087 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004088 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004089 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004090 break;
4091 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004092 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004093 if ((long long)size > h1m->curr_len)
4094 size = h1m->curr_len;
4095 break;
4096 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004097 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004098 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004099 if (!ret)
4100 goto end;
4101
4102 if (ret < 0) {
4103 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004104 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4106 goto end;
4107 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004108 max -= ret;
4109 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004110 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004111 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004112 }
4113
Willy Tarreau801250e2018-09-11 11:45:04 +02004114 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004115 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004116 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004117 if (!ret)
4118 goto end;
4119
4120 if (ret < 0) {
4121 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004122 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004123 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4124 goto end;
4125 }
4126
4127 size = chunk;
4128 h1m->curr_len = chunk;
4129 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004130 max -= ret;
4131 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004132 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004133 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004134 if (!size)
4135 goto send_empty;
4136 }
4137
4138 /* in MSG_DATA state, continue below */
4139 size = h1m->curr_len;
4140 break;
4141 }
4142
4143 /* we have in <size> the exact number of bytes we need to copy from
4144 * the H1 buffer. We need to check this against the connection's and
4145 * the stream's send windows, and to ensure that this fits in the max
4146 * frame size and in the buffer's available space minus 9 bytes (for
4147 * the frame header). The connection's flow control is applied last so
4148 * that we can use a separate list of streams which are immediately
4149 * unblocked on window opening. Note: we don't implement padding.
4150 */
4151
Willy Tarreau5dd17352018-06-14 13:33:30 +02004152 if (size > max)
4153 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004154
4155 if (size > h2s->mws)
4156 size = h2s->mws;
4157
4158 if (size <= 0) {
4159 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004160 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004161 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004162 goto end;
4163 }
4164
4165 if (h2c->mfs && size > h2c->mfs)
4166 size = h2c->mfs;
4167
4168 if (size + 9 > outbuf.size) {
4169 /* we have an opportunity for enlarging the too small
4170 * available space, let's try.
4171 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004172 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004173 goto realign_again;
4174 size = outbuf.size - 9;
4175 }
4176
4177 if (size <= 0) {
4178 h2c->flags |= H2_CF_MUX_MFULL;
4179 h2s->flags |= H2_SF_BLK_MROOM;
4180 goto end;
4181 }
4182
4183 if (size > h2c->mws)
4184 size = h2c->mws;
4185
4186 if (size <= 0) {
4187 h2s->flags |= H2_SF_BLK_MFCTL;
4188 goto end;
4189 }
4190
4191 /* copy whatever we can */
4192 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004193 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004194 if (ret == 1)
4195 len2 = 0;
4196
4197 if (!ret || len1 + len2 < size) {
4198 /* FIXME: must normally never happen */
4199 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4200 goto end;
4201 }
4202
4203 /* limit len1/len2 to size */
4204 if (len1 + len2 > size) {
4205 int sub = len1 + len2 - size;
4206
4207 if (len2 > sub)
4208 len2 -= sub;
4209 else {
4210 sub -= len2;
4211 len2 = 0;
4212 len1 -= sub;
4213 }
4214 }
4215
4216 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004217 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004218 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004219 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004220
4221 send_empty:
4222 /* we may need to add END_STREAM */
4223 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4224 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004225 *
4226 * FIXME: what we do here is not correct because we send end_stream
4227 * before knowing if we'll have to send a HEADERS frame for the
4228 * trailers. More importantly we're not consuming the trailing CRLF
4229 * after the end of trailers, so it will be left to the caller to
4230 * eat it. The right way to do it would be to measure trailers here
4231 * and to send ES only if there are no trailers.
4232 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004233 */
4234 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004235 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004236 es_now = 1;
4237
4238 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004239 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004240
4241 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004243
4244 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004245 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004246
4247 /* consume incoming H1 response */
4248 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004249 max -= size;
4250 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004251 total += size;
4252 h1m->curr_len -= size;
4253 h2s->mws -= size;
4254 h2c->mws -= size;
4255
4256 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004257 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004258 goto new_frame;
4259 }
4260 }
4261
4262 if (es_now) {
4263 if (h2s->st == H2_SS_OPEN)
4264 h2s->st = H2_SS_HLOC;
4265 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004266 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004267
Willy Tarreau35a62702018-02-27 15:37:25 +01004268 if (!(h1m->flags & H1_MF_CHNK)) {
4269 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004270 total += max;
4271 ofs += max;
4272 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004273
Willy Tarreau801250e2018-09-11 11:45:04 +02004274 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004275 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004276
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004277 h2s->flags |= H2_SF_ES_SENT;
4278 }
4279
4280 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004281 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 +02004282 return total;
4283}
4284
Willy Tarreau115e83b2018-12-01 19:17:53 +01004285/* Try to send a HEADERS frame matching HTX response present in HTX message
4286 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4287 * must check the stream's status to detect any error which might have happened
4288 * subsequently to a successful send. The htx blocks are automatically removed
4289 * from the message. The htx message is assumed to be valid since produced from
4290 * the internal code, hence it contains a start line, an optional series of
4291 * header blocks and an end of header, otherwise an invalid frame could be
4292 * emitted and the resulting htx message could be left in an inconsistent state.
4293 */
4294static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4295{
4296 struct http_hdr list[MAX_HTTP_HDR];
4297 struct h2c *h2c = h2s->h2c;
4298 struct htx_blk *blk;
4299 struct htx_blk *blk_end;
4300 struct buffer outbuf;
4301 struct htx_sl *sl;
4302 enum htx_blk_type type;
4303 int es_now = 0;
4304 int ret = 0;
4305 int hdr;
4306 int idx;
4307
4308 if (h2c_mux_busy(h2c, h2s)) {
4309 h2s->flags |= H2_SF_BLK_MBUSY;
4310 return 0;
4311 }
4312
4313 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4314 h2c->flags |= H2_CF_MUX_MALLOC;
4315 h2s->flags |= H2_SF_BLK_MROOM;
4316 return 0;
4317 }
4318
4319 /* determine the first block which must not be deleted, blk_end may
4320 * be NULL if all blocks have to be deleted.
4321 */
4322 idx = htx_get_head(htx);
4323 blk_end = NULL;
4324 while (idx != -1) {
4325 type = htx_get_blk_type(htx_get_blk(htx, idx));
4326 idx = htx_get_next(htx, idx);
4327 if (type == HTX_BLK_EOH) {
4328 if (idx != -1)
4329 blk_end = htx_get_blk(htx, idx);
4330 break;
4331 }
4332 }
4333
4334 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004335 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004336 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004337 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004338 if (h2s->status < 100 || h2s->status > 999)
4339 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004340
4341 /* and the rest of the headers, that we dump starting at header 0 */
4342 hdr = 0;
4343
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004344 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004345 while ((idx = htx_get_next(htx, idx)) != -1) {
4346 blk = htx_get_blk(htx, idx);
4347 type = htx_get_blk_type(blk);
4348
4349 if (type == HTX_BLK_UNUSED)
4350 continue;
4351
4352 if (type != HTX_BLK_HDR)
4353 break;
4354
4355 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4356 goto fail;
4357
4358 list[hdr].n = htx_get_blk_name(htx, blk);
4359 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004360 hdr++;
4361 }
4362
4363 /* marker for end of headers */
4364 list[hdr].n = ist("");
4365
4366 if (h2s->status == 204 || h2s->status == 304) {
4367 /* no contents, claim c-len is present and set to zero */
4368 es_now = 1;
4369 }
4370
4371 chunk_reset(&outbuf);
4372
4373 while (1) {
4374 outbuf.area = b_tail(&h2c->mbuf);
4375 outbuf.size = b_contig_space(&h2c->mbuf);
4376 outbuf.data = 0;
4377
4378 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4379 break;
4380 realign_again:
4381 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4382 }
4383
4384 if (outbuf.size < 9)
4385 goto full;
4386
4387 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4388 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4389 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4390 outbuf.data = 9;
4391
4392 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004393 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004394 if (b_space_wraps(&h2c->mbuf))
4395 goto realign_again;
4396 goto full;
4397 }
4398
4399 /* encode all headers, stop at empty name */
4400 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4401 /* these ones do not exist in H2 and must be dropped. */
4402 if (isteq(list[hdr].n, ist("connection")) ||
4403 isteq(list[hdr].n, ist("proxy-connection")) ||
4404 isteq(list[hdr].n, ist("keep-alive")) ||
4405 isteq(list[hdr].n, ist("upgrade")) ||
4406 isteq(list[hdr].n, ist("transfer-encoding")))
4407 continue;
4408
4409 if (isteq(list[hdr].n, ist("")))
4410 break; // end
4411
4412 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4413 /* output full */
4414 if (b_space_wraps(&h2c->mbuf))
4415 goto realign_again;
4416 goto full;
4417 }
4418 }
4419
Christopher Faulet0b465482019-02-19 15:14:23 +01004420 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004421 * FIXME: we should also set it when we know for sure that the
4422 * content-length is zero as well as on 204/304
4423 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004424 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4425 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004426 es_now = 1;
4427
Willy Tarreau927b88b2019-03-04 08:03:25 +01004428 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004429 es_now = 1;
4430
4431 /* update the frame's size */
4432 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4433
4434 if (es_now)
4435 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4436
4437 /* commit the H2 response */
4438 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004439
4440 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4441 * 1xx responses, another HEADERS frame is expected.
4442 */
4443 if (h2s->status >= 200 || h2s->status == 101)
4444 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004445
Willy Tarreau115e83b2018-12-01 19:17:53 +01004446 if (es_now) {
4447 h2s->flags |= H2_SF_ES_SENT;
4448 if (h2s->st == H2_SS_OPEN)
4449 h2s->st = H2_SS_HLOC;
4450 else
4451 h2s_close(h2s);
4452 }
4453
4454 /* OK we could properly deliver the response */
4455
4456 /* remove all header blocks including the EOH and compute the
4457 * corresponding size.
4458 *
4459 * FIXME: We should remove everything when es_now is set.
4460 */
4461 ret = 0;
4462 idx = htx_get_head(htx);
4463 blk = htx_get_blk(htx, idx);
4464 while (blk != blk_end) {
4465 ret += htx_get_blksz(blk);
4466 blk = htx_remove_blk(htx, blk);
4467 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004468
4469 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4470 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004471 end:
4472 return ret;
4473 full:
4474 h2c->flags |= H2_CF_MUX_MFULL;
4475 h2s->flags |= H2_SF_BLK_MROOM;
4476 ret = 0;
4477 goto end;
4478 fail:
4479 /* unparsable HTX messages, too large ones to be produced in the local
4480 * list etc go here (unrecoverable errors).
4481 */
4482 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4483 ret = 0;
4484 goto end;
4485}
4486
Willy Tarreau80739692018-10-05 11:35:57 +02004487/* Try to send a HEADERS frame matching HTX request present in HTX message
4488 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4489 * must check the stream's status to detect any error which might have happened
4490 * subsequently to a successful send. The htx blocks are automatically removed
4491 * from the message. The htx message is assumed to be valid since produced from
4492 * the internal code, hence it contains a start line, an optional series of
4493 * header blocks and an end of header, otherwise an invalid frame could be
4494 * emitted and the resulting htx message could be left in an inconsistent state.
4495 */
4496static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4497{
4498 struct http_hdr list[MAX_HTTP_HDR];
4499 struct h2c *h2c = h2s->h2c;
4500 struct htx_blk *blk;
4501 struct htx_blk *blk_end;
4502 struct buffer outbuf;
4503 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004504 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004505 enum htx_blk_type type;
4506 int es_now = 0;
4507 int ret = 0;
4508 int hdr;
4509 int idx;
4510
4511 if (h2c_mux_busy(h2c, h2s)) {
4512 h2s->flags |= H2_SF_BLK_MBUSY;
4513 return 0;
4514 }
4515
4516 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4517 h2c->flags |= H2_CF_MUX_MALLOC;
4518 h2s->flags |= H2_SF_BLK_MROOM;
4519 return 0;
4520 }
4521
4522 /* determine the first block which must not be deleted, blk_end may
4523 * be NULL if all blocks have to be deleted.
4524 */
4525 idx = htx_get_head(htx);
4526 blk_end = NULL;
4527 while (idx != -1) {
4528 type = htx_get_blk_type(htx_get_blk(htx, idx));
4529 idx = htx_get_next(htx, idx);
4530 if (type == HTX_BLK_EOH) {
4531 if (idx != -1)
4532 blk_end = htx_get_blk(htx, idx);
4533 break;
4534 }
4535 }
4536
4537 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004538 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004539 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004540 meth = htx_sl_req_meth(sl);
4541 path = htx_sl_req_uri(sl);
4542
4543 /* and the rest of the headers, that we dump starting at header 0 */
4544 hdr = 0;
4545
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004546 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004547 while ((idx = htx_get_next(htx, idx)) != -1) {
4548 blk = htx_get_blk(htx, idx);
4549 type = htx_get_blk_type(blk);
4550
4551 if (type == HTX_BLK_UNUSED)
4552 continue;
4553
4554 if (type != HTX_BLK_HDR)
4555 break;
4556
4557 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4558 goto fail;
4559
4560 list[hdr].n = htx_get_blk_name(htx, blk);
4561 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004562 hdr++;
4563 }
4564
4565 /* marker for end of headers */
4566 list[hdr].n = ist("");
4567
4568 chunk_reset(&outbuf);
4569
4570 while (1) {
4571 outbuf.area = b_tail(&h2c->mbuf);
4572 outbuf.size = b_contig_space(&h2c->mbuf);
4573 outbuf.data = 0;
4574
4575 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4576 break;
4577 realign_again:
4578 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4579 }
4580
4581 if (outbuf.size < 9)
4582 goto full;
4583
4584 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4585 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4586 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4587 outbuf.data = 9;
4588
4589 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004590 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004591 if (b_space_wraps(&h2c->mbuf))
4592 goto realign_again;
4593 goto full;
4594 }
4595
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004596 /* RFC7540 #8.3: the CONNECT method must have :
4597 * - :authority set to the URI part (host:port)
4598 * - :method set to CONNECT
4599 * - :scheme and :path omitted
4600 */
4601 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4602 /* encode the scheme which is always "https" (or 0x86 for "http") */
4603 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4604 /* output full */
4605 if (b_space_wraps(&h2c->mbuf))
4606 goto realign_again;
4607 goto full;
4608 }
Willy Tarreau80739692018-10-05 11:35:57 +02004609
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004610 /* encode the path, which necessarily is the second one */
4611 if (!hpack_encode_path(&outbuf, path)) {
4612 /* output full */
4613 if (b_space_wraps(&h2c->mbuf))
4614 goto realign_again;
4615 goto full;
4616 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004617
4618 /* look for the Host header and place it in :authority */
4619 auth = ist2(NULL, 0);
4620 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4621 if (isteq(list[hdr].n, ist("")))
4622 break; // end
4623
4624 if (isteq(list[hdr].n, ist("host"))) {
4625 auth = list[hdr].v;
4626 break;
4627 }
4628 }
4629 }
4630 else {
4631 /* for CONNECT, :authority is taken from the path */
4632 auth = path;
4633 }
4634
4635 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4636 /* output full */
4637 if (b_space_wraps(&h2c->mbuf))
4638 goto realign_again;
4639 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004640 }
4641
4642 /* encode all headers, stop at empty name */
4643 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4644 /* these ones do not exist in H2 and must be dropped. */
4645 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004646 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004647 isteq(list[hdr].n, ist("proxy-connection")) ||
4648 isteq(list[hdr].n, ist("keep-alive")) ||
4649 isteq(list[hdr].n, ist("upgrade")) ||
4650 isteq(list[hdr].n, ist("transfer-encoding")))
4651 continue;
4652
4653 if (isteq(list[hdr].n, ist("")))
4654 break; // end
4655
4656 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4657 /* output full */
4658 if (b_space_wraps(&h2c->mbuf))
4659 goto realign_again;
4660 goto full;
4661 }
4662 }
4663
4664 /* we may need to add END_STREAM if we have no body :
4665 * - request already closed, or :
4666 * - no transfer-encoding, and :
4667 * - no content-length or content-length:0
4668 * Fixme: this doesn't take into account CONNECT requests.
4669 */
4670 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4671 es_now = 1;
4672
4673 if (sl->flags & HTX_SL_F_BODYLESS)
4674 es_now = 1;
4675
Willy Tarreau927b88b2019-03-04 08:03:25 +01004676 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004677 es_now = 1;
4678
4679 /* update the frame's size */
4680 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4681
4682 if (es_now)
4683 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4684
4685 /* commit the H2 response */
4686 b_add(&h2c->mbuf, outbuf.data);
4687 h2s->flags |= H2_SF_HEADERS_SENT;
4688 h2s->st = H2_SS_OPEN;
4689
Willy Tarreau80739692018-10-05 11:35:57 +02004690 if (es_now) {
4691 // trim any possibly pending data (eg: inconsistent content-length)
4692 h2s->flags |= H2_SF_ES_SENT;
4693 h2s->st = H2_SS_HLOC;
4694 }
4695
4696 /* remove all header blocks including the EOH and compute the
4697 * corresponding size.
4698 *
4699 * FIXME: We should remove everything when es_now is set.
4700 */
4701 ret = 0;
4702 idx = htx_get_head(htx);
4703 blk = htx_get_blk(htx, idx);
4704 while (blk != blk_end) {
4705 ret += htx_get_blksz(blk);
4706 blk = htx_remove_blk(htx, blk);
4707 }
4708
4709 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4710 htx_remove_blk(htx, blk_end);
4711
4712 end:
4713 return ret;
4714 full:
4715 h2c->flags |= H2_CF_MUX_MFULL;
4716 h2s->flags |= H2_SF_BLK_MROOM;
4717 ret = 0;
4718 goto end;
4719 fail:
4720 /* unparsable HTX messages, too large ones to be produced in the local
4721 * list etc go here (unrecoverable errors).
4722 */
4723 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4724 ret = 0;
4725 goto end;
4726}
4727
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004728/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004729 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4730 * caller must check the stream's status to detect any error which might have
4731 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004732 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4733 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004734static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004735{
4736 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004737 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004738 struct buffer outbuf;
4739 size_t total = 0;
4740 int es_now = 0;
4741 int bsize; /* htx block size */
4742 int fsize; /* h2 frame size */
4743 struct htx_blk *blk;
4744 enum htx_blk_type type;
4745 int idx;
4746
4747 if (h2c_mux_busy(h2c, h2s)) {
4748 h2s->flags |= H2_SF_BLK_MBUSY;
4749 goto end;
4750 }
4751
4752 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4753 h2c->flags |= H2_CF_MUX_MALLOC;
4754 h2s->flags |= H2_SF_BLK_MROOM;
4755 goto end;
4756 }
4757
Willy Tarreau98de12a2018-12-12 07:03:00 +01004758 htx = htx_from_buf(buf);
4759
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004760 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4761 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4762 * the caller to handle.
4763 */
4764
4765 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004766 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004767 goto end;
4768
4769 idx = htx_get_head(htx);
4770 blk = htx_get_blk(htx, idx);
4771 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4772 bsize = htx_get_blksz(blk);
4773 fsize = bsize;
4774
4775 if (type == HTX_BLK_EOD) {
4776 /* if we have an EOD, we're dealing with chunked data. We may
4777 * have a set of trailers after us that the caller will want to
4778 * deal with. Let's simply remove the EOD and return.
4779 */
4780 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004781 total++; // EOD counts as one byte
4782 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004783 goto end;
4784 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004785 else if (type == HTX_BLK_EOM) {
4786 if (h2s->flags & H2_SF_ES_SENT) {
4787 /* ES already sent */
4788 htx_remove_blk(htx, blk);
4789 total++; // EOM counts as one byte
4790 count--;
4791 goto end;
4792 }
4793 }
4794 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004795 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004796
4797 /* Perform some optimizations to reduce the number of buffer copies.
4798 * First, if the mux's buffer is empty and the htx area contains
4799 * exactly one data block of the same size as the requested count, and
4800 * this count fits within the frame size, the stream's window size, and
4801 * the connection's window size, then it's possible to simply swap the
4802 * caller's buffer with the mux's output buffer and adjust offsets and
4803 * length to match the entire DATA HTX block in the middle. In this
4804 * case we perform a true zero-copy operation from end-to-end. This is
4805 * the situation that happens all the time with large files. Second, if
4806 * this is not possible, but the mux's output buffer is empty, we still
4807 * have an opportunity to avoid the copy to the intermediary buffer, by
4808 * making the intermediary buffer's area point to the output buffer's
4809 * area. In this case we want to skip the HTX header to make sure that
4810 * copies remain aligned and that this operation remains possible all
4811 * the time. This goes for headers, data blocks and any data extracted
4812 * from the HTX blocks.
4813 */
4814 if (unlikely(fsize == count &&
4815 htx->used == 1 && type == HTX_BLK_DATA &&
4816 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4817 void *old_area = h2c->mbuf.area;
4818
4819 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004820 /* Too bad there are data left there. We're willing to memcpy/memmove
4821 * up to 1/4 of the buffer, which means that it's OK to copy a large
4822 * frame into a buffer containing few data if it needs to be realigned,
4823 * and that it's also OK to copy few data without realigning. Otherwise
4824 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004825 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004826 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4827 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4828 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004829 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004830
Willy Tarreau98de12a2018-12-12 07:03:00 +01004831 h2c->flags |= H2_CF_MUX_MFULL;
4832 h2s->flags |= H2_SF_BLK_MROOM;
4833 goto end;
4834 }
4835
4836 /* map an H2 frame to the HTX block so that we can put the
4837 * frame header there.
4838 */
4839 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004840 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004841 h2c->mbuf.data = fsize + 9;
4842 outbuf.area = b_head(&h2c->mbuf);
4843
4844 /* prepend an H2 DATA frame header just before the DATA block */
4845 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4846 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4847 h2_set_frame_size(outbuf.area, fsize);
4848
4849 /* update windows */
4850 h2s->mws -= fsize;
4851 h2c->mws -= fsize;
4852
4853 /* and exchange with our old area */
4854 buf->area = old_area;
4855 buf->data = buf->head = 0;
4856 total += fsize;
4857 goto end;
4858 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004859
Willy Tarreau98de12a2018-12-12 07:03:00 +01004860 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004861 /* for DATA and EOM we'll have to emit a frame, even if empty */
4862
4863 while (1) {
4864 outbuf.area = b_tail(&h2c->mbuf);
4865 outbuf.size = b_contig_space(&h2c->mbuf);
4866 outbuf.data = 0;
4867
4868 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4869 break;
4870 realign_again:
4871 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4872 }
4873
4874 if (outbuf.size < 9) {
4875 h2c->flags |= H2_CF_MUX_MFULL;
4876 h2s->flags |= H2_SF_BLK_MROOM;
4877 goto end;
4878 }
4879
4880 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4881 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4882 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4883 outbuf.data = 9;
4884
4885 /* we have in <fsize> the exact number of bytes we need to copy from
4886 * the HTX buffer. We need to check this against the connection's and
4887 * the stream's send windows, and to ensure that this fits in the max
4888 * frame size and in the buffer's available space minus 9 bytes (for
4889 * the frame header). The connection's flow control is applied last so
4890 * that we can use a separate list of streams which are immediately
4891 * unblocked on window opening. Note: we don't implement padding.
4892 */
4893
4894 /* EOM is presented with bsize==1 but would lead to the emission of an
4895 * empty frame, thus we force it to zero here.
4896 */
4897 if (type == HTX_BLK_EOM)
4898 bsize = fsize = 0;
4899
4900 if (!fsize)
4901 goto send_empty;
4902
4903 if (h2s->mws <= 0) {
4904 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004905 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004906 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004907 goto end;
4908 }
4909
Willy Tarreauee573762018-12-04 15:25:57 +01004910 if (fsize > count)
4911 fsize = count;
4912
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004913 if (fsize > h2s->mws)
4914 fsize = h2s->mws; // >0
4915
4916 if (h2c->mfs && fsize > h2c->mfs)
4917 fsize = h2c->mfs; // >0
4918
4919 if (fsize + 9 > outbuf.size) {
4920 /* we have an opportunity for enlarging the too small
4921 * available space, let's try.
4922 * FIXME: is this really interesting to do? Maybe we'll
4923 * spend lots of time realigning instead of using two
4924 * frames.
4925 */
4926 if (b_space_wraps(&h2c->mbuf))
4927 goto realign_again;
4928 fsize = outbuf.size - 9;
4929
4930 if (fsize <= 0) {
4931 /* no need to send an empty frame here */
4932 h2c->flags |= H2_CF_MUX_MFULL;
4933 h2s->flags |= H2_SF_BLK_MROOM;
4934 goto end;
4935 }
4936 }
4937
4938 if (h2c->mws <= 0) {
4939 h2s->flags |= H2_SF_BLK_MFCTL;
4940 goto end;
4941 }
4942
4943 if (fsize > h2c->mws)
4944 fsize = h2c->mws;
4945
4946 /* now let's copy this this into the output buffer */
4947 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004948 h2s->mws -= fsize;
4949 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004950 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004951
4952 send_empty:
4953 /* update the frame's size */
4954 h2_set_frame_size(outbuf.area, fsize);
4955
4956 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4957 * meeting EOM. We should optimize this later.
4958 */
4959 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004960 total++; // EOM counts as one byte
4961 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004962 es_now = 1;
4963 }
4964
4965 if (es_now)
4966 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4967
4968 /* commit the H2 response */
4969 b_add(&h2c->mbuf, fsize + 9);
4970
4971 /* consume incoming HTX block, including EOM */
4972 total += fsize;
4973 if (fsize == bsize) {
4974 htx_remove_blk(htx, blk);
4975 if (fsize)
4976 goto new_frame;
4977 } else {
4978 /* we've truncated this block */
4979 htx_cut_data_blk(htx, blk, fsize);
4980 }
4981
4982 if (es_now) {
4983 if (h2s->st == H2_SS_OPEN)
4984 h2s->st = H2_SS_HLOC;
4985 else
4986 h2s_close(h2s);
4987
4988 h2s->flags |= H2_SF_ES_SENT;
4989 }
4990
4991 end:
4992 return total;
4993}
4994
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004995/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4996 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4997 * processed. The caller must check the stream's status to detect any error
4998 * which might have happened subsequently to a successful send. The htx blocks
4999 * are automatically removed from the message. The htx message is assumed to be
5000 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005001 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005002 * as a single frame. The ES flag is always set.
5003 */
5004static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5005{
5006 struct http_hdr list[MAX_HTTP_HDR];
5007 struct h2c *h2c = h2s->h2c;
5008 struct htx_blk *blk;
5009 struct htx_blk *blk_end;
5010 struct buffer outbuf;
5011 struct h1m h1m;
5012 enum htx_blk_type type;
5013 uint32_t size;
5014 int ret = 0;
5015 int hdr;
5016 int idx;
5017 void *start;
5018
5019 if (h2c_mux_busy(h2c, h2s)) {
5020 h2s->flags |= H2_SF_BLK_MBUSY;
5021 goto end;
5022 }
5023
5024 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5025 h2c->flags |= H2_CF_MUX_MALLOC;
5026 h2s->flags |= H2_SF_BLK_MROOM;
5027 goto end;
5028 }
5029
5030 /* The principle is that we parse each and every trailers block using
5031 * the H1 headers parser, and append it to the list. We don't proceed
5032 * until EOM is met. blk_end will point to the EOM block.
5033 */
5034 hdr = 0;
5035 memset(list, 0, sizeof(list));
5036 blk_end = NULL;
5037
5038 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5039 blk = htx_get_blk(htx, idx);
5040 type = htx_get_blk_type(blk);
5041
5042 if (type == HTX_BLK_UNUSED)
5043 continue;
5044
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005045 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005046 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005047
5048 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5049 goto fail;
5050
5051 size = htx_get_blksz(blk);
5052 start = htx_get_blk_ptr(htx, blk);
5053
5054 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5055 h1m.err_pos = 0;
5056 ret = h1_headers_to_hdr_list(start, start + size,
5057 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5058 &h1m, NULL);
5059 if (ret < 0)
5060 goto fail;
5061
5062 /* ret == 0 if an incomplete trailers block was found (missing
5063 * empty line), or > 0 if it was found. We have to continue on
5064 * incomplete messages because the trailers block might be
5065 * incomplete.
5066 */
5067
5068 /* search the new end */
5069 while (hdr <= sizeof(list)/sizeof(list[0])) {
5070 if (!list[hdr].n.len)
5071 break;
5072 hdr++;
5073 }
5074 }
5075
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005076 if (list[hdr].n.len != 0)
5077 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005078
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005079 chunk_reset(&outbuf);
5080
5081 while (1) {
5082 outbuf.area = b_tail(&h2c->mbuf);
5083 outbuf.size = b_contig_space(&h2c->mbuf);
5084 outbuf.data = 0;
5085
5086 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5087 break;
5088 realign_again:
5089 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5090 }
5091
5092 if (outbuf.size < 9)
5093 goto full;
5094
5095 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5096 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5097 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5098 outbuf.data = 9;
5099
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005100 /* encode all headers */
5101 for (idx = 0; idx < hdr; idx++) {
5102 /* these ones do not exist in H2 or must not appear in
5103 * trailers and must be dropped.
5104 */
5105 if (isteq(list[idx].n, ist("host")) ||
5106 isteq(list[idx].n, ist("content-length")) ||
5107 isteq(list[idx].n, ist("connection")) ||
5108 isteq(list[idx].n, ist("proxy-connection")) ||
5109 isteq(list[idx].n, ist("keep-alive")) ||
5110 isteq(list[idx].n, ist("upgrade")) ||
5111 isteq(list[idx].n, ist("te")) ||
5112 isteq(list[idx].n, ist("transfer-encoding")))
5113 continue;
5114
5115 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5116 /* output full */
5117 if (b_space_wraps(&h2c->mbuf))
5118 goto realign_again;
5119 goto full;
5120 }
5121 }
5122
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005123 if (outbuf.data == 9) {
5124 /* here we have a problem, we have nothing to emit (either we
5125 * received an empty trailers block followed or we removed its
5126 * contents above). Because of this we can't send a HEADERS
5127 * frame, so we have to cheat and instead send an empty DATA
5128 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005129 */
5130 outbuf.area[3] = H2_FT_DATA;
5131 outbuf.area[4] = H2_F_DATA_END_STREAM;
5132 }
5133
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005134 /* update the frame's size */
5135 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5136
5137 /* commit the H2 response */
5138 b_add(&h2c->mbuf, outbuf.data);
5139 h2s->flags |= H2_SF_ES_SENT;
5140
5141 if (h2s->st == H2_SS_OPEN)
5142 h2s->st = H2_SS_HLOC;
5143 else
5144 h2s_close(h2s);
5145
5146 /* OK we could properly deliver the response */
5147 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005148 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005149 ret = 0;
5150 idx = htx_get_head(htx);
5151 blk = htx_get_blk(htx, idx);
5152 while (blk != blk_end) {
5153 ret += htx_get_blksz(blk);
5154 blk = htx_remove_blk(htx, blk);
5155 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005156 end:
5157 return ret;
5158 full:
5159 h2c->flags |= H2_CF_MUX_MFULL;
5160 h2s->flags |= H2_SF_BLK_MROOM;
5161 ret = 0;
5162 goto end;
5163 fail:
5164 /* unparsable HTX messages, too large ones to be produced in the local
5165 * list etc go here (unrecoverable errors).
5166 */
5167 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5168 ret = 0;
5169 goto end;
5170}
5171
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005172/* Called from the upper layer, to subscribe to events, such as being able to send.
5173 * The <param> argument here is supposed to be a pointer to a wait_event struct
5174 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5175 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5176 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5177 * returns 0 except for the error above.
5178 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005179static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5180{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005181 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005182 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005183 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005184
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005185 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005186 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005187 if (!(sw->events & SUB_RETRY_RECV)) {
5188 sw->events |= SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005189 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005190 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005191 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005192 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005193 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005194 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005195 if (!(sw->events & SUB_RETRY_SEND)) {
5196 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005197 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005198 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
Willy Tarreauc234ae32019-05-13 17:56:11 +02005199 !LIST_ADDED(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005200 if (h2s->flags & H2_SF_BLK_MFCTL)
5201 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5202 else
5203 LIST_ADDQ(&h2c->send_list, &h2s->list);
5204 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005205 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005206 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005207 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005208 if (event_type != 0)
5209 return -1;
5210 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005211}
5212
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005213/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5214 * The <param> argument here is supposed to be a pointer to the same wait_event
5215 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5216 * It always returns zero.
5217 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005218static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5219{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005220 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005221 struct h2s *h2s = cs->ctx;
5222
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005223 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005224 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005225 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005226 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005227 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005228 }
5229 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005230 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005231 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005232 if (h2s->send_wait == sw) {
5233 LIST_DEL(&h2s->list);
5234 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005235 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005236 /* We were about to send, make sure it does not happen */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005237 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02005238 h2s->send_wait != &h2s->wait_event) {
5239 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5240 LIST_DEL_INIT(&h2s->sending_list);
5241 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005242 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005243
Olivier Houchardd846c262018-10-19 17:24:29 +02005244 }
5245 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005246 return 0;
5247}
5248
5249
Olivier Houchard511efea2018-08-16 15:30:32 +02005250/* Called from the upper layer, to receive data */
5251static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5252{
Olivier Houchard638b7992018-08-16 15:41:52 +02005253 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005254 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005255 struct htx *h2s_htx = NULL;
5256 struct htx *buf_htx = NULL;
5257 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005258 size_t ret = 0;
5259
5260 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005261 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5262 /* in HTX mode we ignore the count argument */
5263 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005264 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005265 /* Here htx_to_buf() will set buffer data to 0 because
5266 * the HTX is empty.
5267 */
5268 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005269 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005270 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005271
5272 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005273 count = htx_free_data_space(buf_htx);
5274 if (flags & CO_RFL_KEEP_RSV) {
5275 if (count <= global.tune.maxrewrite)
5276 goto end;
5277 count -= global.tune.maxrewrite;
5278 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005279
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005280 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005281
5282 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5283 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5284
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005285 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005286 htx_to_buf(buf_htx, buf);
5287 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005288 ret = htx_ret.ret;
5289 }
5290 else {
5291 ret = b_xfer(buf, &h2s->rxbuf, count);
5292 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005293
Christopher Faulet37070b22019-02-14 15:12:14 +01005294 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005295 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005296 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005297 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005298 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005299 if (cs->flags & CS_FL_REOS)
5300 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005301 if (cs->flags & CS_FL_ERR_PENDING)
5302 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005303 if (b_size(&h2s->rxbuf)) {
5304 b_free(&h2s->rxbuf);
5305 offer_buffers(NULL, tasks_run_queue);
5306 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005307 }
5308
Willy Tarreau082f5592018-11-25 08:03:32 +01005309 if (ret && h2c->dsi == h2s->id) {
5310 /* demux is blocking on this stream's buffer */
5311 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005312 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005313 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005314
Olivier Houchard511efea2018-08-16 15:30:32 +02005315 return ret;
5316}
5317
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005318/* stops all senders of this connection for example when the mux buffer is full.
5319 * They are moved from the sending_list to either fctl_list or send_list.
5320 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005321static void h2_stop_senders(struct h2c *h2c)
5322{
5323 struct h2s *h2s, *h2s_back;
5324
Olivier Houchardd360ac62019-03-22 17:37:16 +01005325 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5326 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005327 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005328 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005329 }
5330}
5331
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005332/* Called from the upper layer, to send data from buffer <buf> for no more than
5333 * <count> bytes. Returns the number of bytes effectively sent. Some status
5334 * flags may be updated on the conn_stream.
5335 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005336static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005337{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005338 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005339 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005340 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005341 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005342 struct htx *htx;
5343 struct htx_blk *blk;
5344 enum htx_blk_type btype;
5345 uint32_t bsize;
5346 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005347
Olivier Houchardd360ac62019-03-22 17:37:16 +01005348 /* If we were not just woken because we wanted to send but couldn't,
5349 * and there's somebody else that is waiting to send, do nothing,
5350 * we will subscribe later and be put at the end of the list
5351 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005352 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005353 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5354 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005355 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005356
Olivier Houchard998410a2019-04-15 19:23:37 +02005357 /* We couldn't set it to NULL before, because we needed it in case
5358 * we had to cancel the tasklet
5359 */
5360 h2s->send_wait = NULL;
5361
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005362 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5363 return 0;
5364
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005365 /* htx will be enough to decide if we're using HTX or legacy */
5366 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5367
Willy Tarreau0bad0432018-06-14 16:54:01 +02005368 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005369 h2s->flags |= H2_SF_OUTGOING_DATA;
5370
Willy Tarreau751f2d02018-10-05 09:35:00 +02005371 if (h2s->id == 0) {
5372 int32_t id = h2c_get_next_sid(h2s->h2c);
5373
5374 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005375 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005376 return 0;
5377 }
5378
5379 eb32_delete(&h2s->by_id);
5380 h2s->by_id.key = h2s->id = id;
5381 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005382 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005383 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5384 }
5385
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005386 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005387 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005388 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005389 idx = htx_get_head(htx);
5390 blk = htx_get_blk(htx, idx);
5391 btype = htx_get_blk_type(blk);
5392 bsize = htx_get_blksz(blk);
5393
5394 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005395 case HTX_BLK_REQ_SL:
5396 /* start-line before headers */
5397 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5398 if (ret > 0) {
5399 total += ret;
5400 count -= ret;
5401 if (ret < bsize)
5402 goto done;
5403 }
5404 break;
5405
Willy Tarreau115e83b2018-12-01 19:17:53 +01005406 case HTX_BLK_RES_SL:
5407 /* start-line before headers */
5408 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5409 if (ret > 0) {
5410 total += ret;
5411 count -= ret;
5412 if (ret < bsize)
5413 goto done;
5414 }
5415 break;
5416
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005417 case HTX_BLK_DATA:
5418 case HTX_BLK_EOD:
5419 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005420 /* all these cause the emission of a DATA frame (possibly empty).
5421 * This EOM necessarily is one before trailers, as the EOM following
5422 * trailers would have been consumed by the trailers parser.
5423 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005424 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005425 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005426 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005427 total += ret;
5428 count -= ret;
5429 if (ret < bsize)
5430 goto done;
5431 }
5432 break;
5433
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005434 case HTX_BLK_TLR:
5435 /* This is the first trailers block, all the subsequent ones AND
5436 * the EOM will be swallowed by the parser.
5437 */
5438 ret = h2s_htx_make_trailers(h2s, htx);
5439 if (ret > 0) {
5440 total += ret;
5441 count -= ret;
5442 if (ret < bsize)
5443 goto done;
5444 }
5445 break;
5446
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005447 default:
5448 htx_remove_blk(htx, blk);
5449 total += bsize;
5450 count -= bsize;
5451 break;
5452 }
5453 }
5454 goto done;
5455 }
5456
5457 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005458 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005459 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005460 if (h2s->h2c->flags & H2_CF_IS_BACK)
5461 ret = -1;
5462 else
5463 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005464 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005465 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005466 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005467 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005468 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005469 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005470 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005471
Willy Tarreau5dd17352018-06-14 13:33:30 +02005472 if (unlikely((int)ret <= 0)) {
5473 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005474 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5475 break;
5476 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005477 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005478 total += count;
5479 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005480 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005481 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005482 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005483 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005484 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005485 break;
5486 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005487
5488 total += ret;
5489 count -= ret;
5490
Willy Tarreau2b778482019-05-06 15:00:22 +02005491 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005492 break;
5493
5494 if (h2s->flags & H2_SF_BLK_ANY)
5495 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005496 }
5497
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005498 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005499 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005500 /* trim any possibly pending data after we close (extra CR-LF,
5501 * unprocessed trailers, abnormal extra data, ...)
5502 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005503 total += count;
5504 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005505 }
5506
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005507 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005508 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005509 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005510 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005511 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005512 }
5513
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005514 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005515 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005516 } else {
5517 b_del(buf, total);
5518 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005519
5520 /* The mux is full, cancel the pending tasks */
5521 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5522 (h2s->flags & H2_SF_BLK_MBUSY))
5523 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005524
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005525 /* If we're running HTX, and we read the whole buffer, then pretend
5526 * we read exactly what the caller specified, as with HTX the caller
5527 * will always give the buffer size, instead of the amount of data
5528 * available.
5529 */
5530 if (htx && !b_data(buf))
5531 total = orig_count;
5532
Olivier Houchard7505f942018-08-21 18:10:44 +02005533 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005534 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005535 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005536
Olivier Houchard7505f942018-08-21 18:10:44 +02005537 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005538 /* If we're waiting for flow control, and we got a shutr on the
5539 * connection, we will never be unlocked, so add an error on
5540 * the conn_stream.
5541 */
5542 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5543 !b_data(&h2s->h2c->dbuf) &&
5544 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5545 if (cs->flags & CS_FL_EOS)
5546 cs->flags |= CS_FL_ERROR;
5547 else
5548 cs->flags |= CS_FL_ERR_PENDING;
5549 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005550 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005551 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005552 LIST_DEL_INIT(&h2s->list);
5553 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005554 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005555}
5556
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005557/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005558static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005559{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005560 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005561 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005562 struct eb32_node *node;
5563 int fctl_cnt = 0;
5564 int send_cnt = 0;
5565 int tree_cnt = 0;
5566 int orph_cnt = 0;
5567
5568 if (!h2c)
5569 return;
5570
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005571 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005572 fctl_cnt++;
5573
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005574 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005575 send_cnt++;
5576
Willy Tarreau3af37712018-12-18 14:34:41 +01005577 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005578 node = eb32_first(&h2c->streams_by_id);
5579 while (node) {
5580 h2s = container_of(node, struct h2s, by_id);
5581 tree_cnt++;
5582 if (!h2s->cs)
5583 orph_cnt++;
5584 node = eb32_next(node);
5585 }
5586
Willy Tarreau987c0632018-12-18 10:32:05 +01005587 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5588 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5589 " .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 +02005590 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5591 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005592 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005593 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5594 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5595 h2c->msi,
5596 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5597 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5598
5599 if (h2s) {
5600 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5601 h2s, h2s->id, h2s->flags,
5602 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5603 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5604 h2s->cs);
5605 if (h2s->cs)
5606 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5607 h2s->cs->flags, h2s->cs->data);
5608 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005609}
Willy Tarreau62f52692017-10-08 23:01:42 +02005610
5611/*******************************************************/
5612/* functions below are dedicated to the config parsers */
5613/*******************************************************/
5614
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005615/* config parser for global "tune.h2.header-table-size" */
5616static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5617 struct proxy *defpx, const char *file, int line,
5618 char **err)
5619{
5620 if (too_many_args(1, args, err, NULL))
5621 return -1;
5622
5623 h2_settings_header_table_size = atoi(args[1]);
5624 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5625 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5626 return -1;
5627 }
5628 return 0;
5629}
Willy Tarreau62f52692017-10-08 23:01:42 +02005630
Willy Tarreaue6baec02017-07-27 11:45:11 +02005631/* config parser for global "tune.h2.initial-window-size" */
5632static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5633 struct proxy *defpx, const char *file, int line,
5634 char **err)
5635{
5636 if (too_many_args(1, args, err, NULL))
5637 return -1;
5638
5639 h2_settings_initial_window_size = atoi(args[1]);
5640 if (h2_settings_initial_window_size < 0) {
5641 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5642 return -1;
5643 }
5644 return 0;
5645}
5646
Willy Tarreau5242ef82017-07-27 11:47:28 +02005647/* config parser for global "tune.h2.max-concurrent-streams" */
5648static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5649 struct proxy *defpx, const char *file, int line,
5650 char **err)
5651{
5652 if (too_many_args(1, args, err, NULL))
5653 return -1;
5654
5655 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005656 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005657 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5658 return -1;
5659 }
5660 return 0;
5661}
5662
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005663/* config parser for global "tune.h2.max-frame-size" */
5664static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5665 struct proxy *defpx, const char *file, int line,
5666 char **err)
5667{
5668 if (too_many_args(1, args, err, NULL))
5669 return -1;
5670
5671 h2_settings_max_frame_size = atoi(args[1]);
5672 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5673 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5674 return -1;
5675 }
5676 return 0;
5677}
5678
Willy Tarreau62f52692017-10-08 23:01:42 +02005679
5680/****************************************/
5681/* MUX initialization and instanciation */
5682/***************************************/
5683
5684/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005685static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005686 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005687 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005688 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005689 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005690 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005691 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005692 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005693 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005694 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005695 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005696 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005697 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005698 .shutr = h2_shutr,
5699 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005700 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005701 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005702 .name = "H2",
5703};
5704
Christopher Faulet32f61c02018-04-10 14:33:41 +02005705/* PROTO selection : this mux registers PROTO token "h2" */
5706static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005707 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005708
Willy Tarreau0108d902018-11-25 19:14:37 +01005709INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5710
Christopher Faulet9b579102019-04-11 22:52:25 +02005711
5712/* The mux operations */
5713static const struct mux_ops h2_htx_ops = {
5714 .init = h2_init,
5715 .wake = h2_wake,
5716 .snd_buf = h2_snd_buf,
5717 .rcv_buf = h2_rcv_buf,
5718 .subscribe = h2_subscribe,
5719 .unsubscribe = h2_unsubscribe,
5720 .attach = h2_attach,
5721 .get_first_cs = h2_get_first_cs,
5722 .detach = h2_detach,
5723 .destroy = h2_destroy,
5724 .avail_streams = h2_avail_streams,
5725 .used_streams = h2_used_streams,
5726 .shutr = h2_shutr,
5727 .shutw = h2_shutw,
5728 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005729 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005730 .name = "H2",
5731};
5732
Willy Tarreauf8957272018-10-03 10:25:20 +02005733static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005734 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005735
5736INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5737
Willy Tarreau62f52692017-10-08 23:01:42 +02005738/* config keyword parsers */
5739static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005740 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005741 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005742 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005743 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005744 { 0, NULL, NULL }
5745}};
5746
Willy Tarreau0108d902018-11-25 19:14:37 +01005747INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);