blob: a06ea5f1e9bcab226e751cd497bf94afe5c8f5be [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_* */
90 int32_t max_id; /* highest ID known on this connection, <0 before preface */
91 uint32_t rcvd_c; /* newly received data to ACK for the connection */
92 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
93
94 /* states for the demux direction */
95 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020096 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097
98 int32_t dsi; /* demux stream ID (<0 = idle) */
99 int32_t dfl; /* demux frame length (if dsi >= 0) */
100 int8_t dft; /* demux frame type (if dsi >= 0) */
101 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100102 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
103 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200104 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
105
106 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 int32_t msi; /* mux stream ID (<0 = idle) */
109 int32_t mfl; /* mux frame length (if dsi >= 0) */
110 int8_t mft; /* mux frame type (if dsi >= 0) */
111 int8_t mff; /* mux frame flags (if dsi >= 0) */
112 /* 16 bit hole here */
113 int32_t miw; /* mux initial window size for all new streams */
114 int32_t mws; /* mux window size. Can be negative. */
115 int32_t mfs; /* mux's max frame size */
116
Willy Tarreauea392822017-10-31 10:02:25 +0100117 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100118 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100119 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200120 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200121 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100122 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123 struct eb_root streams_by_id; /* all active streams by their ID */
124 struct list send_list; /* list of blocked streams requesting to send */
125 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200126 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100127 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200128 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200129};
130
Willy Tarreau18312642017-10-11 07:57:07 +0200131/* H2 stream state, in h2s->st */
132enum h2_ss {
133 H2_SS_IDLE = 0, // idle
134 H2_SS_RLOC, // reserved(local)
135 H2_SS_RREM, // reserved(remote)
136 H2_SS_OPEN, // open
137 H2_SS_HREM, // half-closed(remote)
138 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200139 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200140 H2_SS_CLOSED, // closed
141 H2_SS_ENTRIES // must be last
142} __attribute__((packed));
143
144/* HTTP/2 stream flags (32 bit), in h2s->flags */
145#define H2_SF_NONE 0x00000000
146#define H2_SF_ES_RCVD 0x00000001
147#define H2_SF_ES_SENT 0x00000002
148
149#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
150#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
151
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200152/* stream flags indicating the reason the stream is blocked */
153#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
154#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
155#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
156#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
157#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
158
Willy Tarreau454f9052017-10-26 19:40:35 +0200159/* stream flags indicating how data is supposed to be sent */
160#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
161#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
162
163/* step we're currently in when sending chunks. This is needed because we may
164 * have to transfer chunks as large as a full buffer so there's no room left
165 * for size nor crlf around.
166 */
167#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
168#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
169#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
170
171#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
172
Willy Tarreau67434202017-11-06 20:20:51 +0100173#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100174#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100175
Willy Tarreau18312642017-10-11 07:57:07 +0200176/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
177 * it is being processed in the internal HTTP representation (H1 for now).
178 */
179struct h2s {
180 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100181 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200182 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200183 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200184 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200185 int32_t id; /* stream ID */
186 uint32_t flags; /* H2_SF_* */
187 int mws; /* mux window size for this stream */
188 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
189 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200190 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200191 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200192 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
193 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
194 struct wait_event *send_wait; /* The streeam is waiting for flow control */
195 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200196};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200197
Willy Tarreauc6405142017-09-21 20:23:50 +0200198/* descriptor for an h2 frame header */
199struct h2_fh {
200 uint32_t len; /* length, host order, 24 bits */
201 uint32_t sid; /* stream id, host order, 31 bits */
202 uint8_t ft; /* frame type */
203 uint8_t ff; /* frame flags */
204};
205
Willy Tarreau8ceae722018-11-26 11:58:30 +0100206/* the h2c connection pool */
207DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
208
209/* the h2s stream pool */
210DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
211
Willy Tarreaudc572362018-12-12 08:08:05 +0100212/* The default connection window size is 65535, it may only be enlarged using
213 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
214 * we'll pretend we already received the difference between the two to send
215 * an equivalent window update to enlarge it to 2G-1.
216 */
217#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
218
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219/* a few settings from the global section */
220static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200221static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200222static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200223
Willy Tarreau2a856182017-05-16 15:20:39 +0200224/* a dmumy closed stream */
225static const struct h2s *h2_closed_stream = &(const struct h2s){
226 .cs = NULL,
227 .h2c = NULL,
228 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100229 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100230 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200231 .id = 0,
232};
233
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100234/* a dmumy closed stream returning a PROTOCOL_ERROR error */
235static const struct h2s *h2_error_stream = &(const struct h2s){
236 .cs = NULL,
237 .h2c = NULL,
238 .st = H2_SS_CLOSED,
239 .errcode = H2_ERR_PROTOCOL_ERROR,
240 .flags = 0,
241 .id = 0,
242};
243
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100244/* a dmumy closed stream returning a REFUSED_STREAM error */
245static const struct h2s *h2_refused_stream = &(const struct h2s){
246 .cs = NULL,
247 .h2c = NULL,
248 .st = H2_SS_CLOSED,
249 .errcode = H2_ERR_REFUSED_STREAM,
250 .flags = 0,
251 .id = 0,
252};
253
Willy Tarreau2a856182017-05-16 15:20:39 +0200254/* and a dummy idle stream for use with any unannounced stream */
255static const struct h2s *h2_idle_stream = &(const struct h2s){
256 .cs = NULL,
257 .h2c = NULL,
258 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100259 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200260 .id = 0,
261};
262
Olivier Houchard9f6af332018-05-25 14:04:04 +0200263static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200264static int h2_send(struct h2c *h2c);
265static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200266static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200267static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100268static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +0100269static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100270static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200271static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100272static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100273static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200274
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200275/*****************************************************/
276/* functions below are for dynamic buffer management */
277/*****************************************************/
278
Willy Tarreau315d8072017-12-10 22:17:57 +0100279/* indicates whether or not the we may call the h2_recv() function to attempt
280 * to receive data into the buffer and/or demux pending data. The condition is
281 * a bit complex due to some API limits for now. The rules are the following :
282 * - if an error or a shutdown was detected on the connection and the buffer
283 * is empty, we must not attempt to receive
284 * - if the demux buf failed to be allocated, we must not try to receive and
285 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100286 * - if no flag indicates a blocking condition, we may attempt to receive,
287 * regardless of whether the demux buffer is full or not, so that only
288 * de demux part decides whether or not to block. This is needed because
289 * the connection API indeed prevents us from re-enabling receipt that is
290 * already enabled in a polled state, so we must always immediately stop
291 * as soon as the demux can't proceed so as never to hit an end of read
292 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100293 * - otherwise must may not attempt
294 */
295static inline int h2_recv_allowed(const struct h2c *h2c)
296{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200297 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100298 (h2c->st0 >= H2_CS_ERROR ||
299 h2c->conn->flags & CO_FL_ERROR ||
300 conn_xprt_read0_pending(h2c->conn)))
301 return 0;
302
303 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100304 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100305 return 1;
306
307 return 0;
308}
309
Willy Tarreau47b515a2018-12-21 16:09:41 +0100310/* restarts reading on the connection if it was not enabled */
311static inline void h2c_restart_reading(const struct h2c *h2c)
312{
313 if (!h2_recv_allowed(h2c))
314 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100315 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100316 return;
317 tasklet_wakeup(h2c->wait_event.task);
318}
319
320
Willy Tarreauf2101912018-07-19 10:11:38 +0200321/* returns true if the connection has too many conn_streams attached */
322static inline int h2_has_too_many_cs(const struct h2c *h2c)
323{
Willy Tarreaua8754662018-12-23 20:43:58 +0100324 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200325}
326
Willy Tarreau44e973f2018-03-01 17:49:30 +0100327/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
328 * flags are used to figure what buffer was requested. It returns 1 if the
329 * allocation succeeds, in which case the connection is woken up, or 0 if it's
330 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200331 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200333{
334 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100335 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200336
Willy Tarreau44e973f2018-03-01 17:49:30 +0100337 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200338 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100339 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200340 return 1;
341 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200342
Willy Tarreau44e973f2018-03-01 17:49:30 +0100343 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
344 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200345
346 if (h2c->flags & H2_CF_DEM_MROOM) {
347 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100348 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200349 }
Willy Tarreau14398122017-09-22 14:26:04 +0200350 return 1;
351 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100352
353 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
354 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200355 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100356 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100357 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100358 return 1;
359 }
360
Willy Tarreau14398122017-09-22 14:26:04 +0200361 return 0;
362}
363
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200364static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200365{
366 struct buffer *buf = NULL;
367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
369 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
370 h2c->buf_wait.target = h2c;
371 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100372 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100374 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200375 __conn_xprt_stop_recv(h2c->conn);
376 }
377 return buf;
378}
379
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200380static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200381{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200382 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100383 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200384 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200385 }
386}
387
Olivier Houchardd540b362018-11-05 18:37:53 +0100388static int h2_avail_streams(struct connection *conn)
389{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100390 struct h2c *h2c = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100391
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100392 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100393 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
394}
395
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100396static int h2_max_streams(struct connection *conn)
397{
398 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
399 return h2_settings_max_concurrent_streams;
400}
401
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200402
Willy Tarreau62f52692017-10-08 23:01:42 +0200403/*****************************************************************/
404/* functions below are dedicated to the mux setup and management */
405/*****************************************************************/
406
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200407/* Initialize the mux once it's attached. For outgoing connections, the context
408 * is already initialized before installing the mux, so we detect incoming
409 * connections from the fact that the context is still NULL. Returns < 0 on
410 * error.
411 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100412static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413{
414 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100415 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416
Willy Tarreaubafbe012017-11-24 17:34:44 +0100417 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200419 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100421 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200422 h2c->flags = H2_CF_IS_BACK;
423 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
424 if (tick_isset(prx->timeout.serverfin))
425 h2c->shut_timeout = prx->timeout.serverfin;
426 } else {
427 h2c->flags = H2_CF_NONE;
428 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
429 if (tick_isset(prx->timeout.clientfin))
430 h2c->shut_timeout = prx->timeout.clientfin;
431 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100432
Willy Tarreau0b37d652018-10-03 10:33:02 +0200433 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100434 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100435 if (tick_isset(h2c->timeout)) {
436 t = task_new(tid_bit);
437 if (!t)
438 goto fail;
439
440 h2c->task = t;
441 t->process = h2_timeout_task;
442 t->context = h2c;
443 t->expire = tick_add(now_ms, h2c->timeout);
444 }
Willy Tarreauea392822017-10-31 10:02:25 +0100445
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200446 h2c->wait_event.task = tasklet_new();
447 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200448 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200449 h2c->wait_event.task->process = h2_io_cb;
450 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100451 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200452
Willy Tarreau32218eb2017-09-22 08:07:25 +0200453 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
454 if (!h2c->ddht)
455 goto fail;
456
457 /* Initialise the context. */
458 h2c->st0 = H2_CS_PREFACE;
459 h2c->conn = conn;
460 h2c->max_id = -1;
461 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100462 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100464 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200465 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200467 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468 h2c->dsi = -1;
469 h2c->msi = -1;
470 h2c->last_sid = -1;
471
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200472 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200473 h2c->miw = 65535; /* mux initial window size */
474 h2c->mws = 65535; /* mux window size */
475 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200476 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200477 LIST_INIT(&h2c->send_list);
478 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200479 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100480 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreau3f133572017-10-31 19:21:06 +0100482 if (t)
483 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100484
Willy Tarreau01b44822018-10-03 14:26:37 +0200485 if (h2c->flags & H2_CF_IS_BACK) {
486 /* FIXME: this is temporary, for outgoing connections we need
487 * to immediately allocate a stream until the code is modified
488 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100489 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 */
491 struct h2s *h2s;
492
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100493 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200494 if (!h2s)
495 goto fail_stream;
496 }
497
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100498 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200499
Willy Tarreau0f383582018-10-03 14:22:21 +0200500 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100501 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200502 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200503 fail_stream:
504 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200505 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100506 if (t)
507 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200508 if (h2c->wait_event.task)
509 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100510 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200511 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200512 return -1;
513}
514
Willy Tarreau751f2d02018-10-05 09:35:00 +0200515/* returns the next allocatable outgoing stream ID for the H2 connection, or
516 * -1 if no more is allocatable.
517 */
518static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
519{
520 int32_t id = (h2c->max_id + 1) | 1;
521 if (id & 0x80000000U)
522 id = -1;
523 return id;
524}
525
Willy Tarreau2373acc2017-10-12 17:35:14 +0200526/* returns the stream associated with id <id> or NULL if not found */
527static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
528{
529 struct eb32_node *node;
530
Willy Tarreau751f2d02018-10-05 09:35:00 +0200531 if (id == 0)
532 return (struct h2s *)h2_closed_stream;
533
Willy Tarreau2a856182017-05-16 15:20:39 +0200534 if (id > h2c->max_id)
535 return (struct h2s *)h2_idle_stream;
536
Willy Tarreau2373acc2017-10-12 17:35:14 +0200537 node = eb32_lookup(&h2c->streams_by_id, id);
538 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200539 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200540
541 return container_of(node, struct h2s, by_id);
542}
543
Willy Tarreau62f52692017-10-08 23:01:42 +0200544/* release function for a connection. This one should be called to free all
545 * resources allocated to the mux.
546 */
547static void h2_release(struct connection *conn)
548{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100549 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
551 LIST_DEL(&conn->list);
552
553 if (h2c) {
554 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200555
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100556 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100557 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100558 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200559
Willy Tarreau44e973f2018-03-01 17:49:30 +0100560 h2_release_buf(h2c, &h2c->dbuf);
561 h2_release_buf(h2c, &h2c->mbuf);
562
Willy Tarreauea392822017-10-31 10:02:25 +0100563 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200564 h2c->task->context = NULL;
565 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100566 h2c->task = NULL;
567 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200568 if (h2c->wait_event.task)
569 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100570 if (h2c->wait_event.events != 0)
571 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200572 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100573
Willy Tarreaubafbe012017-11-24 17:34:44 +0100574 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200575 }
576
577 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100578 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200579
580 conn_stop_tracking(conn);
581 conn_full_close(conn);
582 if (conn->destroy_cb)
583 conn->destroy_cb(conn);
584 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200585}
586
587
Willy Tarreau71681172017-10-23 14:39:06 +0200588/******************************************************/
589/* functions below are for the H2 protocol processing */
590/******************************************************/
591
592/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100593static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200594{
595 return h2s ? h2s->id : 0;
596}
597
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200598/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100599static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200600{
601 if (h2c->msi < 0)
602 return 0;
603
604 if (h2c->msi == h2s_id(h2s))
605 return 0;
606
607 return 1;
608}
609
Willy Tarreau741d6df2017-10-17 08:00:59 +0200610/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100611static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200612{
613 h2c->errcode = err;
614 h2c->st0 = H2_CS_ERROR;
615}
616
Willy Tarreau2e43f082017-10-17 08:03:59 +0200617/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100618static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200619{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200620 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200621 h2s->errcode = err;
622 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100623 if (h2s->cs)
624 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200625 }
626}
627
Willy Tarreau7e094452018-12-19 18:08:52 +0100628/* attempt to notify the data layer of recv availability */
629static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
630{
631 struct wait_event *sw;
632
633 if (h2s->recv_wait) {
634 sw = h2s->recv_wait;
635 sw->events &= ~SUB_RETRY_RECV;
636 tasklet_wakeup(sw->task);
637 h2s->recv_wait = NULL;
638 }
639}
640
641/* attempt to notify the data layer of send availability */
642static void __maybe_unused h2s_notify_send(struct h2s *h2s)
643{
644 struct wait_event *sw;
645
646 if (h2s->send_wait) {
647 sw = h2s->send_wait;
648 sw->events &= ~SUB_RETRY_SEND;
649 tasklet_wakeup(sw->task);
650 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100651 LIST_DEL(&h2s->list);
652 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100653 }
654}
655
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100656/* alerts the data layer, trying to wake it up by all means, following
657 * this sequence :
658 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
659 * - if its subscribed to send, then it's woken up for send
660 * - if it was subscribed to neither, its ->wake() callback is called
661 * It is safe to call this function with a closed stream which doesn't have a
662 * conn_stream anymore.
663 */
664static void __maybe_unused h2s_alert(struct h2s *h2s)
665{
666 if (h2s->recv_wait || h2s->send_wait) {
667 h2s_notify_recv(h2s);
668 h2s_notify_send(h2s);
669 }
670 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
671 h2s->cs->data_cb->wake(h2s->cs);
672}
673
Willy Tarreaue4820742017-07-27 13:37:23 +0200674/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200676{
677 uint8_t *out = frame;
678
679 *out = len >> 16;
680 write_n16(out + 1, len);
681}
682
Willy Tarreau54c15062017-10-10 17:10:03 +0200683/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
684 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
685 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200686 * available in the buffer's input prior to calling this function. The buffer
687 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200688 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100689static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200690 const struct buffer *b, int o)
691{
Willy Tarreau591d4452018-06-15 17:21:00 +0200692 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 +0200693}
694
Willy Tarreau1f094672017-11-20 21:27:45 +0100695static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200696{
Willy Tarreau591d4452018-06-15 17:21:00 +0200697 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200698}
699
Willy Tarreau1f094672017-11-20 21:27:45 +0100700static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200701{
Willy Tarreau591d4452018-06-15 17:21:00 +0200702 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200703}
704
Willy Tarreau1f094672017-11-20 21:27:45 +0100705static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200706{
Willy Tarreau591d4452018-06-15 17:21:00 +0200707 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200708}
709
710
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100711/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
712 * The algorithm is not obvious. It turns out that H2 headers are neither
713 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
714 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200715 *
716 * b0 b1 b2 b3 b4 b5..b8
717 * +----------+---------+--------+----+----+----------------------+
718 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
719 * +----------+---------+--------+----+----+----------------------+
720 *
721 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
722 * we get the sid properly aligned and ordered, and 16 bits of len properly
723 * ordered as well. The type and flags can be extracted using bit shifts from
724 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200725 * Returns zero if some bytes are missing, otherwise non-zero on success. The
726 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200727 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100728static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200729{
730 uint64_t w;
731
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100732 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200733 return 0;
734
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100735 w = h2_get_n64(b, o + 1);
736 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200737 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
738 h->ff = w >> 32;
739 h->ft = w >> 40;
740 h->len += w >> 48;
741 return 1;
742}
743
744/* skip the next 9 bytes corresponding to the frame header possibly parsed by
745 * h2_peek_frame_hdr() above.
746 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100747static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200748{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200749 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200750}
751
752/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200754{
755 int ret;
756
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100757 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200758 if (ret > 0)
759 h2_skip_frame_hdr(b);
760 return ret;
761}
762
Willy Tarreau00dd0782018-03-01 16:31:34 +0100763/* marks stream <h2s> as CLOSED and decrement the number of active streams for
764 * its connection if the stream was not yet closed. Please use this exclusively
765 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100766 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100767static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100768{
769 if (h2s->st != H2_SS_CLOSED)
770 h2s->h2c->nb_streams--;
771 h2s->st = H2_SS_CLOSED;
772}
773
Willy Tarreau71049cc2018-03-28 13:56:39 +0200774/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
775static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100776{
777 h2s_close(h2s);
778 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200779 if (b_size(&h2s->rxbuf)) {
780 b_free(&h2s->rxbuf);
781 offer_buffers(NULL, tasks_run_queue);
782 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200783 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100784 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200785 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100786 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800787 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200788 * reference left would be in the h2c send_list/fctl_list, and if
789 * we're in it, we're getting out anyway
790 */
791 LIST_DEL(&h2s->list);
792 LIST_INIT(&h2s->list);
793 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100794 pool_free(pool_head_h2s, h2s);
795}
796
Willy Tarreaua8e49542018-10-03 18:53:55 +0200797/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
798 * stream tree. In case of error, nothing is added and NULL is returned. The
799 * causes of errors can be any failed memory allocation. The caller is
800 * responsible for checking if the connection may support an extra stream
801 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200802 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200803static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200804{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 struct h2s *h2s;
806
Willy Tarreaubafbe012017-11-24 17:34:44 +0100807 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 if (!h2s)
809 goto out;
810
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200811 h2s->wait_event.task = tasklet_new();
812 if (!h2s->wait_event.task) {
813 pool_free(pool_head_h2s, h2s);
814 goto out;
815 }
816 h2s->send_wait = NULL;
817 h2s->recv_wait = NULL;
818 h2s->wait_event.task->process = h2_deferred_shut;
819 h2s->wait_event.task->context = h2s;
820 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100821 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200822 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200823 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200824 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200825 h2s->mws = h2c->miw;
826 h2s->flags = H2_SF_NONE;
827 h2s->errcode = H2_ERR_NO_ERROR;
828 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200829 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200830 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200831
832 if (h2c->flags & H2_CF_IS_BACK) {
833 h1m_init_req(&h2s->h1m);
834 h2s->h1m.err_pos = -1; // don't care about errors on the request path
835 h2s->h1m.flags |= H1_MF_TOLOWER;
836 } else {
837 h1m_init_res(&h2s->h1m);
838 h2s->h1m.err_pos = -1; // don't care about errors on the response path
839 h2s->h1m.flags |= H1_MF_TOLOWER;
840 }
841
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200842 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200843 if (id > 0)
844 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200845
846 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100847 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200848
849 return h2s;
850
851 out_free_h2s:
852 pool_free(pool_head_h2s, h2s);
853 out:
854 return NULL;
855}
856
857/* creates a new stream <id> on the h2c connection and returns it, or NULL in
858 * case of memory allocation error.
859 */
860static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
861{
862 struct session *sess = h2c->conn->owner;
863 struct conn_stream *cs;
864 struct h2s *h2s;
865
866 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
867 goto out;
868
869 h2s = h2s_new(h2c, id);
870 if (!h2s)
871 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200872
873 cs = cs_new(h2c->conn);
874 if (!cs)
875 goto out_close;
876
Olivier Houchard746fb772018-12-15 19:42:00 +0100877 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200878 h2s->cs = cs;
879 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200880 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200881
882 if (stream_create_from_cs(cs) < 0)
883 goto out_free_cs;
884
Willy Tarreau590a0512018-09-05 11:56:48 +0200885 /* We want the accept date presented to the next stream to be the one
886 * we have now, the handshake time to be null (since the next stream
887 * is not delayed by a handshake), and the idle time to count since
888 * right now.
889 */
890 sess->accept_date = date;
891 sess->tv_accept = now;
892 sess->t_handshake = 0;
893
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200894 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200895 if (h2_has_too_many_cs(h2c))
896 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200897 return h2s;
898
899 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200900 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200901 cs_free(cs);
902 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200903 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200904 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200905 sess_log(sess);
906 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200907}
908
Willy Tarreau751f2d02018-10-05 09:35:00 +0200909/* allocates a new stream associated to conn_stream <cs> on the h2c connection
910 * and returns it, or NULL in case of memory allocation error or if the highest
911 * possible stream ID was reached.
912 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100913static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200914{
915 struct h2s *h2s = NULL;
916
917 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
918 goto out;
919
920 /* Defer choosing the ID until we send the first message to create the stream */
921 h2s = h2s_new(h2c, 0);
922 if (!h2s)
923 goto out;
924
925 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100926 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200927 cs->ctx = h2s;
928 h2c->nb_cs++;
929
Willy Tarreau751f2d02018-10-05 09:35:00 +0200930 out:
931 return h2s;
932}
933
Willy Tarreaube5b7152017-09-25 16:25:39 +0200934/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
935 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
936 * the various settings codes.
937 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200938static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200939{
940 struct buffer *res;
941 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200942 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200943 int ret;
944
945 if (h2c_mux_busy(h2c, NULL)) {
946 h2c->flags |= H2_CF_DEM_MBUSY;
947 return 0;
948 }
949
Willy Tarreau44e973f2018-03-01 17:49:30 +0100950 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200951 if (!res) {
952 h2c->flags |= H2_CF_MUX_MALLOC;
953 h2c->flags |= H2_CF_DEM_MROOM;
954 return 0;
955 }
956
957 chunk_init(&buf, buf_data, sizeof(buf_data));
958 chunk_memcpy(&buf,
959 "\x00\x00\x00" /* length : 0 for now */
960 "\x04\x00" /* type : 4 (settings), flags : 0 */
961 "\x00\x00\x00\x00", /* stream ID : 0 */
962 9);
963
964 if (h2_settings_header_table_size != 4096) {
965 char str[6] = "\x00\x01"; /* header_table_size */
966
967 write_n32(str + 2, h2_settings_header_table_size);
968 chunk_memcat(&buf, str, 6);
969 }
970
971 if (h2_settings_initial_window_size != 65535) {
972 char str[6] = "\x00\x04"; /* initial_window_size */
973
974 write_n32(str + 2, h2_settings_initial_window_size);
975 chunk_memcat(&buf, str, 6);
976 }
977
978 if (h2_settings_max_concurrent_streams != 0) {
979 char str[6] = "\x00\x03"; /* max_concurrent_streams */
980
981 /* Note: 0 means "unlimited" for haproxy's config but not for
982 * the protocol, so never send this value!
983 */
984 write_n32(str + 2, h2_settings_max_concurrent_streams);
985 chunk_memcat(&buf, str, 6);
986 }
987
988 if (global.tune.bufsize != 16384) {
989 char str[6] = "\x00\x05"; /* max_frame_size */
990
991 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
992 * match bufsize - rewrite size, but at the moment it seems
993 * that clients don't take care of it.
994 */
995 write_n32(str + 2, global.tune.bufsize);
996 chunk_memcat(&buf, str, 6);
997 }
998
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200999 h2_set_frame_size(buf.area, buf.data - 9);
1000 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001001 if (unlikely(ret <= 0)) {
1002 if (!ret) {
1003 h2c->flags |= H2_CF_MUX_MFULL;
1004 h2c->flags |= H2_CF_DEM_MROOM;
1005 return 0;
1006 }
1007 else {
1008 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1009 return 0;
1010 }
1011 }
1012 return ret;
1013}
1014
Willy Tarreau52eed752017-09-22 15:05:09 +02001015/* Try to receive a connection preface, then upon success try to send our
1016 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1017 * missing data. It may return an error in h2c.
1018 */
1019static int h2c_frt_recv_preface(struct h2c *h2c)
1020{
1021 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001022 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001023
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001024 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001025
1026 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001027 if (ret1 < 0)
1028 sess_log(h2c->conn->owner);
1029
Willy Tarreau52eed752017-09-22 15:05:09 +02001030 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1031 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1032 return 0;
1033 }
1034
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001035 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001036 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001037 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001038
Willy Tarreaube5b7152017-09-25 16:25:39 +02001039 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001040}
1041
Willy Tarreau01b44822018-10-03 14:26:37 +02001042/* Try to send a connection preface, then upon success try to send our
1043 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1044 * missing data. It may return an error in h2c.
1045 */
1046static int h2c_bck_send_preface(struct h2c *h2c)
1047{
1048 struct buffer *res;
1049
1050 if (h2c_mux_busy(h2c, NULL)) {
1051 h2c->flags |= H2_CF_DEM_MBUSY;
1052 return 0;
1053 }
1054
1055 res = h2_get_buf(h2c, &h2c->mbuf);
1056 if (!res) {
1057 h2c->flags |= H2_CF_MUX_MALLOC;
1058 h2c->flags |= H2_CF_DEM_MROOM;
1059 return 0;
1060 }
1061
1062 if (!b_data(res)) {
1063 /* preface not yet sent */
1064 b_istput(res, ist(H2_CONN_PREFACE));
1065 }
1066
1067 return h2c_send_settings(h2c);
1068}
1069
Willy Tarreau081d4722017-05-16 21:51:05 +02001070/* try to send a GOAWAY frame on the connection to report an error or a graceful
1071 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1072 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1073 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1074 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1075 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1076 * on unrecoverable failure. It will not attempt to send one again in this last
1077 * case so that it is safe to use h2c_error() to report such errors.
1078 */
1079static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1080{
1081 struct buffer *res;
1082 char str[17];
1083 int ret;
1084
1085 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1086 return 1; // claim that it worked
1087
1088 if (h2c_mux_busy(h2c, h2s)) {
1089 if (h2s)
1090 h2s->flags |= H2_SF_BLK_MBUSY;
1091 else
1092 h2c->flags |= H2_CF_DEM_MBUSY;
1093 return 0;
1094 }
1095
Willy Tarreau44e973f2018-03-01 17:49:30 +01001096 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001097 if (!res) {
1098 h2c->flags |= H2_CF_MUX_MALLOC;
1099 if (h2s)
1100 h2s->flags |= H2_SF_BLK_MROOM;
1101 else
1102 h2c->flags |= H2_CF_DEM_MROOM;
1103 return 0;
1104 }
1105
1106 /* len: 8, type: 7, flags: none, sid: 0 */
1107 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1108
1109 if (h2c->last_sid < 0)
1110 h2c->last_sid = h2c->max_id;
1111
1112 write_n32(str + 9, h2c->last_sid);
1113 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001114 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001115 if (unlikely(ret <= 0)) {
1116 if (!ret) {
1117 h2c->flags |= H2_CF_MUX_MFULL;
1118 if (h2s)
1119 h2s->flags |= H2_SF_BLK_MROOM;
1120 else
1121 h2c->flags |= H2_CF_DEM_MROOM;
1122 return 0;
1123 }
1124 else {
1125 /* we cannot report this error using GOAWAY, so we mark
1126 * it and claim a success.
1127 */
1128 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1129 h2c->flags |= H2_CF_GOAWAY_FAILED;
1130 return 1;
1131 }
1132 }
1133 h2c->flags |= H2_CF_GOAWAY_SENT;
1134 return ret;
1135}
1136
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001137/* Try to send an RST_STREAM frame on the connection for the indicated stream
1138 * during mux operations. This stream must be valid and cannot be closed
1139 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1140 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1141 * not yet.
1142 *
1143 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1144 * to write the message, it subscribes the stream to future notifications.
1145 */
1146static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1147{
1148 struct buffer *res;
1149 char str[13];
1150 int ret;
1151
1152 if (!h2s || h2s->st == H2_SS_CLOSED)
1153 return 1;
1154
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001155 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1156 * RST_STREAM in response to a RST_STREAM frame.
1157 */
1158 if (h2c->dft == H2_FT_RST_STREAM) {
1159 ret = 1;
1160 goto ignore;
1161 }
1162
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163 if (h2c_mux_busy(h2c, h2s)) {
1164 h2s->flags |= H2_SF_BLK_MBUSY;
1165 return 0;
1166 }
1167
Willy Tarreau44e973f2018-03-01 17:49:30 +01001168 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001169 if (!res) {
1170 h2c->flags |= H2_CF_MUX_MALLOC;
1171 h2s->flags |= H2_SF_BLK_MROOM;
1172 return 0;
1173 }
1174
1175 /* len: 4, type: 3, flags: none */
1176 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1177 write_n32(str + 5, h2s->id);
1178 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001179 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001180
1181 if (unlikely(ret <= 0)) {
1182 if (!ret) {
1183 h2c->flags |= H2_CF_MUX_MFULL;
1184 h2s->flags |= H2_SF_BLK_MROOM;
1185 return 0;
1186 }
1187 else {
1188 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1189 return 0;
1190 }
1191 }
1192
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001193 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001194 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001195 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001196 return ret;
1197}
1198
1199/* Try to send an RST_STREAM frame on the connection for the stream being
1200 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001201 * error code, even if the stream is one of the dummy ones, and will update
1202 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001203 *
1204 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1205 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001206 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001207 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001208 */
1209static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1210{
1211 struct buffer *res;
1212 char str[13];
1213 int ret;
1214
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001215 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1216 * RST_STREAM in response to a RST_STREAM frame.
1217 */
1218 if (h2c->dft == H2_FT_RST_STREAM) {
1219 ret = 1;
1220 goto ignore;
1221 }
1222
Willy Tarreau27a84c92017-10-17 08:10:17 +02001223 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001224 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001225 return 0;
1226 }
1227
Willy Tarreau44e973f2018-03-01 17:49:30 +01001228 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001229 if (!res) {
1230 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001231 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001232 return 0;
1233 }
1234
1235 /* len: 4, type: 3, flags: none */
1236 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001237
Willy Tarreau27a84c92017-10-17 08:10:17 +02001238 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001239 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001240 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001241
Willy Tarreau27a84c92017-10-17 08:10:17 +02001242 if (unlikely(ret <= 0)) {
1243 if (!ret) {
1244 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001245 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001246 return 0;
1247 }
1248 else {
1249 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1250 return 0;
1251 }
1252 }
1253
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001254 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001255 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001256 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001257 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001258 }
1259
Willy Tarreau27a84c92017-10-17 08:10:17 +02001260 return ret;
1261}
1262
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001263/* try to send an empty DATA frame with the ES flag set to notify about the
1264 * end of stream and match a shutdown(write). If an ES was already sent as
1265 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1266 * on success or zero if nothing was done. In case of lack of room to write the
1267 * message, it subscribes the requesting stream to future notifications.
1268 */
1269static int h2_send_empty_data_es(struct h2s *h2s)
1270{
1271 struct h2c *h2c = h2s->h2c;
1272 struct buffer *res;
1273 char str[9];
1274 int ret;
1275
Willy Tarreau721c9742017-11-07 11:05:42 +01001276 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001277 return 1;
1278
1279 if (h2c_mux_busy(h2c, h2s)) {
1280 h2s->flags |= H2_SF_BLK_MBUSY;
1281 return 0;
1282 }
1283
Willy Tarreau44e973f2018-03-01 17:49:30 +01001284 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001285 if (!res) {
1286 h2c->flags |= H2_CF_MUX_MALLOC;
1287 h2s->flags |= H2_SF_BLK_MROOM;
1288 return 0;
1289 }
1290
1291 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1292 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1293 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001294 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001295 if (likely(ret > 0)) {
1296 h2s->flags |= H2_SF_ES_SENT;
1297 }
1298 else if (!ret) {
1299 h2c->flags |= H2_CF_MUX_MFULL;
1300 h2s->flags |= H2_SF_BLK_MROOM;
1301 return 0;
1302 }
1303 else {
1304 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1305 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001306 }
1307 return ret;
1308}
1309
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001310/* wake the streams attached to the connection, whose id is greater than <last>,
1311 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001312 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1313 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001314 */
1315static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1316{
1317 struct eb32_node *node;
1318 struct h2s *h2s;
1319
1320 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001321 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001322
1323 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001324 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001325
1326 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1327 while (node) {
1328 h2s = container_of(node, struct h2s, by_id);
1329 if (h2s->id <= last)
1330 break;
1331 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001332
1333 if (!h2s->cs) {
1334 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001335 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001336 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001337 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001338
1339 h2s->cs->flags |= flags;
Willy Tarreaua8519352018-12-18 16:44:28 +01001340 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1341 h2s->cs->flags |= CS_FL_ERROR;
1342
Willy Tarreauf830f012018-12-19 17:44:55 +01001343 h2s_alert(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001344
Willy Tarreaua8519352018-12-18 16:44:28 +01001345 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001346 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001347 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001348 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001349 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001350 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001351 }
1352}
1353
Willy Tarreau3421aba2017-07-27 15:41:03 +02001354/* Increase all streams' outgoing window size by the difference passed in
1355 * argument. This is needed upon receipt of the settings frame if the initial
1356 * window size is different. The difference may be negative and the resulting
1357 * window size as well, for the time it takes to receive some window updates.
1358 */
1359static void h2c_update_all_ws(struct h2c *h2c, int diff)
1360{
1361 struct h2s *h2s;
1362 struct eb32_node *node;
1363
1364 if (!diff)
1365 return;
1366
1367 node = eb32_first(&h2c->streams_by_id);
1368 while (node) {
1369 h2s = container_of(node, struct h2s, by_id);
1370 h2s->mws += diff;
1371 node = eb32_next(node);
1372 }
1373}
1374
1375/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1376 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1377 * return an error in h2c. Described in RFC7540#6.5.
1378 */
1379static int h2c_handle_settings(struct h2c *h2c)
1380{
1381 unsigned int offset;
1382 int error;
1383
1384 if (h2c->dff & H2_F_SETTINGS_ACK) {
1385 if (h2c->dfl) {
1386 error = H2_ERR_FRAME_SIZE_ERROR;
1387 goto fail;
1388 }
1389 return 1;
1390 }
1391
1392 if (h2c->dsi != 0) {
1393 error = H2_ERR_PROTOCOL_ERROR;
1394 goto fail;
1395 }
1396
1397 if (h2c->dfl % 6) {
1398 error = H2_ERR_FRAME_SIZE_ERROR;
1399 goto fail;
1400 }
1401
1402 /* that's the limit we can process */
1403 if (h2c->dfl > global.tune.bufsize) {
1404 error = H2_ERR_FRAME_SIZE_ERROR;
1405 goto fail;
1406 }
1407
1408 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001409 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001410 return 0;
1411
1412 /* parse the frame */
1413 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001414 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1415 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001416
1417 switch (type) {
1418 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1419 /* we need to update all existing streams with the
1420 * difference from the previous iws.
1421 */
1422 if (arg < 0) { // RFC7540#6.5.2
1423 error = H2_ERR_FLOW_CONTROL_ERROR;
1424 goto fail;
1425 }
1426 h2c_update_all_ws(h2c, arg - h2c->miw);
1427 h2c->miw = arg;
1428 break;
1429 case H2_SETTINGS_MAX_FRAME_SIZE:
1430 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1431 error = H2_ERR_PROTOCOL_ERROR;
1432 goto fail;
1433 }
1434 h2c->mfs = arg;
1435 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001436 case H2_SETTINGS_ENABLE_PUSH:
1437 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1438 error = H2_ERR_PROTOCOL_ERROR;
1439 goto fail;
1440 }
1441 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001442 }
1443 }
1444
1445 /* need to ACK this frame now */
1446 h2c->st0 = H2_CS_FRAME_A;
1447 return 1;
1448 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001449 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001450 h2c_error(h2c, error);
1451 return 0;
1452}
1453
1454/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1455 * success or one of the h2_status values.
1456 */
1457static int h2c_ack_settings(struct h2c *h2c)
1458{
1459 struct buffer *res;
1460 char str[9];
1461 int ret = -1;
1462
1463 if (h2c_mux_busy(h2c, NULL)) {
1464 h2c->flags |= H2_CF_DEM_MBUSY;
1465 return 0;
1466 }
1467
Willy Tarreau44e973f2018-03-01 17:49:30 +01001468 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001469 if (!res) {
1470 h2c->flags |= H2_CF_MUX_MALLOC;
1471 h2c->flags |= H2_CF_DEM_MROOM;
1472 return 0;
1473 }
1474
1475 memcpy(str,
1476 "\x00\x00\x00" /* length : 0 (no data) */
1477 "\x04" "\x01" /* type : 4, flags : ACK */
1478 "\x00\x00\x00\x00" /* stream ID */, 9);
1479
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001480 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481 if (unlikely(ret <= 0)) {
1482 if (!ret) {
1483 h2c->flags |= H2_CF_MUX_MFULL;
1484 h2c->flags |= H2_CF_DEM_MROOM;
1485 return 0;
1486 }
1487 else {
1488 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1489 return 0;
1490 }
1491 }
1492 return ret;
1493}
1494
Willy Tarreaucf68c782017-10-10 17:11:41 +02001495/* processes a PING frame and schedules an ACK if needed. The caller must pass
1496 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1497 * missing data. It may return an error in h2c.
1498 */
1499static int h2c_handle_ping(struct h2c *h2c)
1500{
1501 /* frame length must be exactly 8 */
1502 if (h2c->dfl != 8) {
1503 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1504 return 0;
1505 }
1506
1507 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001508 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001509 h2c->st0 = H2_CS_FRAME_A;
1510 return 1;
1511}
1512
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001513/* Try to send a window update for stream id <sid> and value <increment>.
1514 * Returns > 0 on success or zero on missing room or failure. It may return an
1515 * error in h2c.
1516 */
1517static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1518{
1519 struct buffer *res;
1520 char str[13];
1521 int ret = -1;
1522
1523 if (h2c_mux_busy(h2c, NULL)) {
1524 h2c->flags |= H2_CF_DEM_MBUSY;
1525 return 0;
1526 }
1527
Willy Tarreau44e973f2018-03-01 17:49:30 +01001528 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001529 if (!res) {
1530 h2c->flags |= H2_CF_MUX_MALLOC;
1531 h2c->flags |= H2_CF_DEM_MROOM;
1532 return 0;
1533 }
1534
1535 /* length: 4, type: 8, flags: none */
1536 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1537 write_n32(str + 5, sid);
1538 write_n32(str + 9, increment);
1539
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001540 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001541
1542 if (unlikely(ret <= 0)) {
1543 if (!ret) {
1544 h2c->flags |= H2_CF_MUX_MFULL;
1545 h2c->flags |= H2_CF_DEM_MROOM;
1546 return 0;
1547 }
1548 else {
1549 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1550 return 0;
1551 }
1552 }
1553 return ret;
1554}
1555
1556/* try to send pending window update for the connection. It's safe to call it
1557 * with no pending updates. Returns > 0 on success or zero on missing room or
1558 * failure. It may return an error in h2c.
1559 */
1560static int h2c_send_conn_wu(struct h2c *h2c)
1561{
1562 int ret = 1;
1563
1564 if (h2c->rcvd_c <= 0)
1565 return 1;
1566
Willy Tarreau97aaa672018-12-23 09:49:04 +01001567 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1568 /* increase the advertised connection window to 2G on
1569 * first update.
1570 */
1571 h2c->flags |= H2_CF_WINDOW_OPENED;
1572 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1573 }
1574
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001575 /* send WU for the connection */
1576 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1577 if (ret > 0)
1578 h2c->rcvd_c = 0;
1579
1580 return ret;
1581}
1582
1583/* try to send pending window update for the current dmux stream. It's safe to
1584 * call it with no pending updates. Returns > 0 on success or zero on missing
1585 * room or failure. It may return an error in h2c.
1586 */
1587static int h2c_send_strm_wu(struct h2c *h2c)
1588{
1589 int ret = 1;
1590
1591 if (h2c->rcvd_s <= 0)
1592 return 1;
1593
1594 /* send WU for the stream */
1595 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1596 if (ret > 0)
1597 h2c->rcvd_s = 0;
1598
1599 return ret;
1600}
1601
Willy Tarreaucf68c782017-10-10 17:11:41 +02001602/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1603 * success, 0 on missing data or one of the h2_status values.
1604 */
1605static int h2c_ack_ping(struct h2c *h2c)
1606{
1607 struct buffer *res;
1608 char str[17];
1609 int ret = -1;
1610
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001611 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001612 return 0;
1613
1614 if (h2c_mux_busy(h2c, NULL)) {
1615 h2c->flags |= H2_CF_DEM_MBUSY;
1616 return 0;
1617 }
1618
Willy Tarreau44e973f2018-03-01 17:49:30 +01001619 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001620 if (!res) {
1621 h2c->flags |= H2_CF_MUX_MALLOC;
1622 h2c->flags |= H2_CF_DEM_MROOM;
1623 return 0;
1624 }
1625
1626 memcpy(str,
1627 "\x00\x00\x08" /* length : 8 (same payload) */
1628 "\x06" "\x01" /* type : 6, flags : ACK */
1629 "\x00\x00\x00\x00" /* stream ID */, 9);
1630
1631 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001632 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001633
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001634 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001635 if (unlikely(ret <= 0)) {
1636 if (!ret) {
1637 h2c->flags |= H2_CF_MUX_MFULL;
1638 h2c->flags |= H2_CF_DEM_MROOM;
1639 return 0;
1640 }
1641 else {
1642 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1643 return 0;
1644 }
1645 }
1646 return ret;
1647}
1648
Willy Tarreau26f95952017-07-27 17:18:30 +02001649/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1650 * Returns > 0 on success or zero on missing data. It may return an error in
1651 * h2c or h2s. Described in RFC7540#6.9.
1652 */
1653static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1654{
1655 int32_t inc;
1656 int error;
1657
1658 if (h2c->dfl != 4) {
1659 error = H2_ERR_FRAME_SIZE_ERROR;
1660 goto conn_err;
1661 }
1662
1663 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001664 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001665 return 0;
1666
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001667 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001668
1669 if (h2c->dsi != 0) {
1670 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001671
1672 /* it's not an error to receive WU on a closed stream */
1673 if (h2s->st == H2_SS_CLOSED)
1674 return 1;
1675
1676 if (!inc) {
1677 error = H2_ERR_PROTOCOL_ERROR;
1678 goto strm_err;
1679 }
1680
1681 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1682 error = H2_ERR_FLOW_CONTROL_ERROR;
1683 goto strm_err;
1684 }
1685
1686 h2s->mws += inc;
1687 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1688 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001689 if (h2s->send_wait)
1690 LIST_ADDQ(&h2c->send_list, &h2s->list);
1691
Willy Tarreau26f95952017-07-27 17:18:30 +02001692 }
1693 }
1694 else {
1695 /* connection window update */
1696 if (!inc) {
1697 error = H2_ERR_PROTOCOL_ERROR;
1698 goto conn_err;
1699 }
1700
1701 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1702 error = H2_ERR_FLOW_CONTROL_ERROR;
1703 goto conn_err;
1704 }
1705
1706 h2c->mws += inc;
1707 }
1708
1709 return 1;
1710
1711 conn_err:
1712 h2c_error(h2c, error);
1713 return 0;
1714
1715 strm_err:
1716 if (h2s) {
1717 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001718 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001719 }
1720 else
1721 h2c_error(h2c, error);
1722 return 0;
1723}
1724
Willy Tarreaue96b0922017-10-30 00:28:29 +01001725/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1726 * the last ID. Returns > 0 on success or zero on missing data. It may return
1727 * an error in h2c. Described in RFC7540#6.8.
1728 */
1729static int h2c_handle_goaway(struct h2c *h2c)
1730{
1731 int error;
1732 int last;
1733
1734 if (h2c->dsi != 0) {
1735 error = H2_ERR_PROTOCOL_ERROR;
1736 goto conn_err;
1737 }
1738
1739 if (h2c->dfl < 8) {
1740 error = H2_ERR_FRAME_SIZE_ERROR;
1741 goto conn_err;
1742 }
1743
1744 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001745 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001746 return 0;
1747
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001748 last = h2_get_n32(&h2c->dbuf, 0);
1749 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001750 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001751 if (h2c->last_sid < 0)
1752 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001753 return 1;
1754
1755 conn_err:
1756 h2c_error(h2c, error);
1757 return 0;
1758}
1759
Willy Tarreau92153fc2017-12-03 19:46:19 +01001760/* processes a PRIORITY frame, and either skips it or rejects if it is
1761 * invalid. Returns > 0 on success or zero on missing data. It may return
1762 * an error in h2c. Described in RFC7540#6.3.
1763 */
1764static int h2c_handle_priority(struct h2c *h2c)
1765{
1766 int error;
1767
1768 if (h2c->dsi == 0) {
1769 error = H2_ERR_PROTOCOL_ERROR;
1770 goto conn_err;
1771 }
1772
1773 if (h2c->dfl != 5) {
1774 error = H2_ERR_FRAME_SIZE_ERROR;
1775 goto conn_err;
1776 }
1777
1778 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001779 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001780 return 0;
1781
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001783 /* 7540#5.3 : can't depend on itself */
1784 error = H2_ERR_PROTOCOL_ERROR;
1785 goto conn_err;
1786 }
1787 return 1;
1788
1789 conn_err:
1790 h2c_error(h2c, error);
1791 return 0;
1792}
1793
Willy Tarreaucd234e92017-08-18 10:59:39 +02001794/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1795 * Returns > 0 on success or zero on missing data. It may return an error in
1796 * h2c. Described in RFC7540#6.4.
1797 */
1798static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1799{
1800 int error;
1801
1802 if (h2c->dsi == 0) {
1803 error = H2_ERR_PROTOCOL_ERROR;
1804 goto conn_err;
1805 }
1806
Willy Tarreaucd234e92017-08-18 10:59:39 +02001807 if (h2c->dfl != 4) {
1808 error = H2_ERR_FRAME_SIZE_ERROR;
1809 goto conn_err;
1810 }
1811
1812 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001813 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001814 return 0;
1815
1816 /* late RST, already handled */
1817 if (h2s->st == H2_SS_CLOSED)
1818 return 1;
1819
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001820 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001821 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001822
1823 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001824 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001825 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001826 }
1827
1828 h2s->flags |= H2_SF_RST_RCVD;
1829 return 1;
1830
1831 conn_err:
1832 h2c_error(h2c, error);
1833 return 0;
1834}
1835
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001836/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1837 * It may return an error in h2c or h2s. The caller must consider that the
1838 * return value is the new h2s in case one was allocated (most common case).
1839 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001840 * errors here are reported as connection errors since it's impossible to
1841 * recover from such errors after the compression context has been altered.
1842 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001843static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001844{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001845 struct buffer rxbuf = BUF_NULL;
1846 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001847 int error;
1848
1849 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001850 /* RFC7540#4.2 */
1851 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001852 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001853 goto conn_err;
Willy Tarreau13278b42017-10-13 19:23:14 +02001854 }
1855
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001857 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001860 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001861
Willy Tarreauf2101912018-07-19 10:11:38 +02001862 if (h2c->flags & H2_CF_DEM_TOOMANY)
1863 return 0; // too many cs still present
1864
Willy Tarreau13278b42017-10-13 19:23:14 +02001865 /* now either the frame is complete or the buffer is complete */
1866 if (h2s->st != H2_SS_IDLE) {
1867 /* FIXME: stream already exists, this is only allowed for
1868 * trailers (not supported for now).
1869 */
1870 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001871 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001872 goto conn_err;
1873 }
1874 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1875 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1876 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001877 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001878 goto conn_err;
1879 }
1880
Willy Tarreau25919232019-01-03 14:48:18 +01001881 error = h2c_decode_headers(h2c, &rxbuf, &flags);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001882
Willy Tarreau25919232019-01-03 14:48:18 +01001883 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001884 if (h2c->st0 >= H2_CS_ERROR)
1885 goto out;
1886
Willy Tarreau25919232019-01-03 14:48:18 +01001887 if (error <= 0) {
1888 if (error == 0)
1889 goto out; // missing data
1890
1891 /* Failed to decode this stream (e.g. too large request)
1892 * but the HPACK decompressor is still synchronized.
1893 */
1894 h2s = (struct h2s*)h2_error_stream;
1895 goto send_rst;
1896 }
1897
Willy Tarreau22de8d32018-09-05 19:55:58 +02001898 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001899 * positively from h2c_frt_stream_new(), the stream will report the error,
1900 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001901 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001902 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001903 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001904 h2s = (struct h2s*)h2_refused_stream;
1905 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001906 }
1907
1908 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001909 h2s->rxbuf = rxbuf;
1910 h2s->flags |= flags;
1911
1912 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001913 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001914
1915 if (h2s->flags & H2_SF_ES_RCVD) {
1916 h2s->st = H2_SS_HREM;
Willy Tarreau39d68502018-03-02 12:26:37 +01001917 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001918 }
1919
Willy Tarreau721c9742017-11-07 11:05:42 +01001920 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001921 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001922 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001923 }
1924 else {
1925 /* update the max stream ID if the request is being processed */
1926 if (h2s->id > h2c->max_id)
1927 h2c->max_id = h2s->id;
1928 }
1929
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001930 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001931
1932 conn_err:
1933 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001934 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
1936 strm_err:
1937 if (h2s) {
1938 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001939 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001940 }
1941 else
1942 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001943 out:
1944 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001945 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001946
1947 send_rst:
1948 /* make the demux send an RST for the current stream. We may only
1949 * do this if we're certain that the HEADERS frame was properly
1950 * decompressed so that the HPACK decoder is still kept up to date.
1951 */
1952 h2_release_buf(h2c, &rxbuf);
1953 h2c->st0 = H2_CS_FRAME_E;
1954 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001955}
1956
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001957/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1958 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1959 * errors here are reported as connection errors since it's impossible to
1960 * recover from such errors after the compression context has been altered.
1961 */
1962static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1963{
1964 int error;
1965
1966 if (!h2c->dfl) {
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001967 /* RFC7540#4.2 */
1968 error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame!
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001969 sess_log(h2c->conn->owner);
Willy Tarreauc4ea04c2018-12-23 08:13:59 +01001970 goto conn_err;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001971 }
1972
1973 if (!b_size(&h2c->dbuf))
1974 return NULL; // empty buffer
1975
1976 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1977 return NULL; // incomplete frame
1978
Willy Tarreau25919232019-01-03 14:48:18 +01001979 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001980
Willy Tarreau25919232019-01-03 14:48:18 +01001981 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001982 if (h2c->st0 >= H2_CS_ERROR)
1983 return NULL;
1984
Willy Tarreau25919232019-01-03 14:48:18 +01001985 if (error <= 0) {
1986 if (error == 0)
1987 return NULL; // missing data
1988
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001989 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01001990 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001991 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01001992 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001993 }
1994
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01001995 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1996 h2s->flags |= H2_SF_ES_RCVD;
1997 h2s->cs->flags |= CS_FL_REOS;
1998 }
1999
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002000 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
2001 h2s->st = H2_SS_ERROR;
2002 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
2003 h2s->st = H2_SS_HREM;
2004 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
2005 h2s_close(h2s);
2006
2007 return h2s;
2008
2009 conn_err:
2010 h2c_error(h2c, error);
2011 return NULL;
2012
2013 strm_err:
2014 if (h2s) {
2015 h2s_error(h2s, error);
2016 h2c->st0 = H2_CS_FRAME_E;
2017 }
2018 else
2019 h2c_error(h2c, error);
2020 return NULL;
2021}
2022
Willy Tarreau454f9052017-10-26 19:40:35 +02002023/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2024 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2025 */
2026static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2027{
2028 int error;
2029
2030 /* note that empty DATA frames are perfectly valid and sometimes used
2031 * to signal an end of stream (with the ES flag).
2032 */
2033
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002035 return 0; // empty buffer
2036
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002037 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002038 return 0; // incomplete frame
2039
2040 /* now either the frame is complete or the buffer is complete */
2041
2042 if (!h2c->dsi) {
2043 /* RFC7540#6.1 */
2044 error = H2_ERR_PROTOCOL_ERROR;
2045 goto conn_err;
2046 }
2047
2048 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2049 /* RFC7540#6.1 */
2050 error = H2_ERR_STREAM_CLOSED;
2051 goto strm_err;
2052 }
2053
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002054 if (!h2_frt_transfer_data(h2s))
2055 return 0;
2056
Willy Tarreau454f9052017-10-26 19:40:35 +02002057 /* call the upper layers to process the frame, then let the upper layer
2058 * notify the stream about any change.
2059 */
2060 if (!h2s->cs) {
2061 error = H2_ERR_STREAM_CLOSED;
2062 goto strm_err;
2063 }
2064
Willy Tarreau8f650c32017-11-21 19:36:21 +01002065 if (h2c->st0 >= H2_CS_ERROR)
2066 return 0;
2067
Willy Tarreau721c9742017-11-07 11:05:42 +01002068 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002069 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002070 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002071 }
2072
2073 /* check for completion : the callee will change this to FRAME_A or
2074 * FRAME_H once done.
2075 */
2076 if (h2c->st0 == H2_CS_FRAME_P)
2077 return 0;
2078
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002079
2080 /* last frame */
2081 if (h2c->dff & H2_F_DATA_END_STREAM) {
2082 h2s->st = H2_SS_HREM;
2083 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002084 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002085 }
2086
Willy Tarreau454f9052017-10-26 19:40:35 +02002087 return 1;
2088
2089 conn_err:
2090 h2c_error(h2c, error);
2091 return 0;
2092
2093 strm_err:
2094 if (h2s) {
2095 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002096 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002097 }
2098 else
2099 h2c_error(h2c, error);
2100 return 0;
2101}
2102
Willy Tarreaubc933932017-10-09 16:21:43 +02002103/* process Rx frames to be demultiplexed */
2104static void h2_process_demux(struct h2c *h2c)
2105{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002106 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002107
Willy Tarreau081d4722017-05-16 21:51:05 +02002108 if (h2c->st0 >= H2_CS_ERROR)
2109 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002110
2111 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2112 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002113 if (h2c->flags & H2_CF_IS_BACK)
2114 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002115 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2116 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002117 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002118 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002119 sess_log(h2c->conn->owner);
2120 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002121 goto fail;
2122 }
2123
2124 h2c->max_id = 0;
2125 h2c->st0 = H2_CS_SETTINGS1;
2126 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002127
2128 if (h2c->st0 == H2_CS_SETTINGS1) {
2129 struct h2_fh hdr;
2130
2131 /* ensure that what is pending is a valid SETTINGS frame
2132 * without an ACK.
2133 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002134 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002135 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002136 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002137 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002138 sess_log(h2c->conn->owner);
2139 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002140 goto fail;
2141 }
2142
2143 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2144 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2145 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2146 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002147 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002148 goto fail;
2149 }
2150
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002151 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002152 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2153 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2154 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002155 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002156 goto fail;
2157 }
2158
Willy Tarreau3bf69182018-12-21 15:34:50 +01002159 /* that's OK, switch to FRAME_P to process it. This is
2160 * a SETTINGS frame whose header has already been
2161 * deleted above.
2162 */
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002163 h2c->dfl = hdr.len;
2164 h2c->dsi = hdr.sid;
2165 h2c->dft = hdr.ft;
2166 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002167 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002168 h2c->st0 = H2_CS_FRAME_P;
2169 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002170 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002171
2172 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002173 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002174 int ret = 0;
2175
2176 if (h2c->st0 >= H2_CS_ERROR)
2177 break;
2178
2179 if (h2c->st0 == H2_CS_FRAME_H) {
2180 struct h2_fh hdr;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002181 unsigned int padlen = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002182
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002183 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002184 break;
2185
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002186 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002187 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2188 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 if (!h2c->nb_streams) {
2190 /* only log if no other stream can report the error */
2191 sess_log(h2c->conn->owner);
2192 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002193 break;
2194 }
2195
Willy Tarreau3bf69182018-12-21 15:34:50 +01002196 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2197 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2198 * we read the pad length and drop it from the remaining
2199 * payload (one byte + the 9 remaining ones = 10 total
2200 * removed), so we have a frame payload starting after the
2201 * pad len. Flow controlled frames (DATA) also count the
2202 * padlen in the flow control, so it must be adjusted.
2203 */
2204 if (hdr.len < 1) {
2205 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2206 sess_log(h2c->conn->owner);
2207 goto fail;
2208 }
2209 hdr.len--;
2210
2211 if (b_data(&h2c->dbuf) < 10)
2212 break; // missing padlen
2213
2214 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2215
2216 if (padlen > hdr.len) {
2217 /* RFC7540#6.1 : pad length = length of
2218 * frame payload or greater => error.
2219 */
2220 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2221 sess_log(h2c->conn->owner);
2222 goto fail;
2223 }
2224
2225 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2226 h2c->rcvd_c++;
2227 h2c->rcvd_s++;
2228 }
2229 b_del(&h2c->dbuf, 1);
2230 }
2231 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002232 h2c->dfl = hdr.len;
2233 h2c->dsi = hdr.sid;
2234 h2c->dft = hdr.ft;
2235 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002236 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002237 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002238 }
2239
2240 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002241 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2242
Willy Tarreau567beb82018-12-18 16:52:44 +01002243 if (tmp_h2s != h2s && h2s && h2s->cs &&
2244 (b_data(&h2s->rxbuf) ||
2245 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002246 /* we may have to signal the upper layers */
2247 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002248 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002249 }
2250 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002251
Willy Tarreaud7901432017-12-29 11:34:40 +01002252 if (h2c->st0 == H2_CS_FRAME_E)
2253 goto strm_err;
2254
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002255 if (h2s->st == H2_SS_IDLE &&
2256 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2257 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2258 * this state MUST be treated as a connection error
2259 */
2260 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2261 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002262 if (!h2c->nb_streams) {
2263 /* only log if no other stream can report the error */
2264 sess_log(h2c->conn->owner);
2265 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002266 break;
2267 }
2268
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002269 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2270 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2271 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2272 * this state MUST be treated as a stream error
2273 */
2274 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2275 goto strm_err;
2276 }
2277
Willy Tarreauab837502017-12-27 15:07:30 +01002278 /* Below the management of frames received in closed state is a
2279 * bit hackish because the spec makes strong differences between
2280 * streams closed by receiving RST, sending RST, and seeing ES
2281 * in both directions. In addition to this, the creation of a
2282 * new stream reusing the identifier of a closed one will be
2283 * detected here. Given that we cannot keep track of all closed
2284 * streams forever, we consider that unknown closed streams were
2285 * closed on RST received, which allows us to respond with an
2286 * RST without breaking the connection (eg: to abort a transfer).
2287 * Some frames have to be silently ignored as well.
2288 */
2289 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2290 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2291 /* #5.1.1: The identifier of a newly
2292 * established stream MUST be numerically
2293 * greater than all streams that the initiating
2294 * endpoint has opened or reserved. This
2295 * governs streams that are opened using a
2296 * HEADERS frame and streams that are reserved
2297 * using PUSH_PROMISE. An endpoint that
2298 * receives an unexpected stream identifier
2299 * MUST respond with a connection error.
2300 */
2301 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2302 goto strm_err;
2303 }
2304
2305 if (h2s->flags & H2_SF_RST_RCVD) {
2306 /* RFC7540#5.1:closed: an endpoint that
2307 * receives any frame other than PRIORITY after
2308 * receiving a RST_STREAM MUST treat that as a
2309 * stream error of type STREAM_CLOSED.
2310 *
2311 * Note that old streams fall into this category
2312 * and will lead to an RST being sent.
2313 */
2314 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2315 h2c->st0 = H2_CS_FRAME_E;
2316 goto strm_err;
2317 }
2318
2319 /* RFC7540#5.1:closed: if this state is reached as a
2320 * result of sending a RST_STREAM frame, the peer that
2321 * receives the RST_STREAM might have already sent
2322 * frames on the stream that cannot be withdrawn. An
2323 * endpoint MUST ignore frames that it receives on
2324 * closed streams after it has sent a RST_STREAM
2325 * frame. An endpoint MAY choose to limit the period
2326 * over which it ignores frames and treat frames that
2327 * arrive after this time as being in error.
2328 */
2329 if (!(h2s->flags & H2_SF_RST_SENT)) {
2330 /* RFC7540#5.1:closed: any frame other than
2331 * PRIO/WU/RST in this state MUST be treated as
2332 * a connection error
2333 */
2334 if (h2c->dft != H2_FT_RST_STREAM &&
2335 h2c->dft != H2_FT_PRIORITY &&
2336 h2c->dft != H2_FT_WINDOW_UPDATE) {
2337 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2338 goto strm_err;
2339 }
2340 }
2341 }
2342
Willy Tarreauc0da1962017-10-30 18:38:00 +01002343#if 0
2344 // problem below: it is not possible to completely ignore such
2345 // streams as we need to maintain the compression state as well
2346 // and for this we need to completely process these frames (eg:
2347 // HEADERS frames) as well as counting DATA frames to emit
2348 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2349 // This is a typical case of layer violation where the
2350 // transported contents are critical to the connection's
2351 // validity and must be ignored at the same time :-(
2352
2353 /* graceful shutdown, ignore streams whose ID is higher than
2354 * the one advertised in GOAWAY. RFC7540#6.8.
2355 */
2356 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002357 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2358 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002359 h2c->dfl -= ret;
2360 ret = h2c->dfl == 0;
2361 goto strm_err;
2362 }
2363#endif
2364
Willy Tarreau7e98c052017-10-10 15:56:59 +02002365 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002366 case H2_FT_SETTINGS:
2367 if (h2c->st0 == H2_CS_FRAME_P)
2368 ret = h2c_handle_settings(h2c);
2369
2370 if (h2c->st0 == H2_CS_FRAME_A)
2371 ret = h2c_ack_settings(h2c);
2372 break;
2373
Willy Tarreaucf68c782017-10-10 17:11:41 +02002374 case H2_FT_PING:
2375 if (h2c->st0 == H2_CS_FRAME_P)
2376 ret = h2c_handle_ping(h2c);
2377
2378 if (h2c->st0 == H2_CS_FRAME_A)
2379 ret = h2c_ack_ping(h2c);
2380 break;
2381
Willy Tarreau26f95952017-07-27 17:18:30 +02002382 case H2_FT_WINDOW_UPDATE:
2383 if (h2c->st0 == H2_CS_FRAME_P)
2384 ret = h2c_handle_window_update(h2c, h2s);
2385 break;
2386
Willy Tarreau61290ec2017-10-17 08:19:21 +02002387 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002388 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2389 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2390 * frames' parsers consume all following CONTINUATION
2391 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002392 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002393 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2394 sess_log(h2c->conn->owner);
2395 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002396
Willy Tarreau13278b42017-10-13 19:23:14 +02002397 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002398 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002399 if (h2c->flags & H2_CF_IS_BACK)
2400 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2401 else
2402 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002403 if (tmp_h2s) {
2404 h2s = tmp_h2s;
2405 ret = 1;
2406 }
2407 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002408 break;
2409
Willy Tarreau454f9052017-10-26 19:40:35 +02002410 case H2_FT_DATA:
2411 if (h2c->st0 == H2_CS_FRAME_P)
2412 ret = h2c_frt_handle_data(h2c, h2s);
2413
2414 if (h2c->st0 == H2_CS_FRAME_A)
2415 ret = h2c_send_strm_wu(h2c);
2416 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002417
Willy Tarreau92153fc2017-12-03 19:46:19 +01002418 case H2_FT_PRIORITY:
2419 if (h2c->st0 == H2_CS_FRAME_P)
2420 ret = h2c_handle_priority(h2c);
2421 break;
2422
Willy Tarreaucd234e92017-08-18 10:59:39 +02002423 case H2_FT_RST_STREAM:
2424 if (h2c->st0 == H2_CS_FRAME_P)
2425 ret = h2c_handle_rst_stream(h2c, h2s);
2426 break;
2427
Willy Tarreaue96b0922017-10-30 00:28:29 +01002428 case H2_FT_GOAWAY:
2429 if (h2c->st0 == H2_CS_FRAME_P)
2430 ret = h2c_handle_goaway(h2c);
2431 break;
2432
Willy Tarreau1c661982017-10-30 13:52:01 +01002433 case H2_FT_PUSH_PROMISE:
2434 /* not permitted here, RFC7540#5.1 */
2435 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002436 if (!h2c->nb_streams) {
2437 /* only log if no other stream can report the error */
2438 sess_log(h2c->conn->owner);
2439 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002440 break;
2441
2442 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002443 default:
2444 /* drop frames that we ignore. They may be larger than
2445 * the buffer so we drain all of their contents until
2446 * we reach the end.
2447 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002448 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2449 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002450 h2c->dfl -= ret;
2451 ret = h2c->dfl == 0;
2452 }
2453
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002454 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002455 /* We may have to send an RST if not done yet */
2456 if (h2s->st == H2_SS_ERROR)
2457 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002458
Willy Tarreaua20a5192017-12-27 11:02:06 +01002459 if (h2c->st0 == H2_CS_FRAME_E)
2460 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002461
Willy Tarreau7e98c052017-10-10 15:56:59 +02002462 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002463 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002464 break;
2465
2466 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002467 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002468 h2c->st0 = H2_CS_FRAME_H;
2469 }
2470 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002471
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002472 if (h2c->rcvd_c > 0 &&
2473 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2474 h2c_send_conn_wu(h2c);
2475
Willy Tarreau52eed752017-09-22 15:05:09 +02002476 fail:
2477 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002478 if (h2s && h2s->cs &&
2479 (b_data(&h2s->rxbuf) ||
2480 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002481 /* we may have to signal the upper layers */
2482 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002483 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002484 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002485
Willy Tarreau47b515a2018-12-21 16:09:41 +01002486 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002487}
2488
2489/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2490 * the end.
2491 */
2492static int h2_process_mux(struct h2c *h2c)
2493{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002494 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002495
Willy Tarreau01b44822018-10-03 14:26:37 +02002496 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2497 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2498 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2499 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2500 if (h2c->st0 == H2_CS_ERROR) {
2501 h2c->st0 = H2_CS_ERROR2;
2502 sess_log(h2c->conn->owner);
2503 }
2504 goto fail;
2505 }
2506 h2c->st0 = H2_CS_SETTINGS1;
2507 }
2508 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002509 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002510 return 1;
2511 }
2512
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002513 /* start by sending possibly pending window updates */
2514 if (h2c->rcvd_c > 0 &&
2515 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2516 h2c_send_conn_wu(h2c) < 0)
2517 goto fail;
2518
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002519 /* First we always process the flow control list because the streams
2520 * waiting there were already elected for immediate emission but were
2521 * blocked just on this.
2522 */
2523
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002524 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002525 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2526 h2c->st0 >= H2_CS_ERROR)
2527 break;
2528
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002529 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002530 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2531 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002532 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002533 LIST_DEL(&h2s->list);
2534 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002535 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002536 }
2537
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002539 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2540 break;
2541
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002542 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002543 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2544 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002546 LIST_DEL(&h2s->list);
2547 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002548 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002549 }
2550
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002551 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002552 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002553 if (h2c->st0 == H2_CS_ERROR) {
2554 if (h2c->max_id >= 0) {
2555 h2c_send_goaway_error(h2c, NULL);
2556 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2557 return 0;
2558 }
2559
2560 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2561 }
2562 return 1;
2563 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002564 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002565}
2566
Willy Tarreau62f52692017-10-08 23:01:42 +02002567
Willy Tarreau479998a2018-11-18 06:30:59 +01002568/* Attempt to read data, and subscribe if none available.
2569 * The function returns 1 if data has been received, otherwise zero.
2570 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002571static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002572{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002573 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002574 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002575 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002576 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002577
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002578 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002579 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002580
Willy Tarreau315d8072017-12-10 22:17:57 +01002581 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002582 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002583
Willy Tarreau44e973f2018-03-01 17:49:30 +01002584 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002585 if (!buf) {
2586 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002587 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002588 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002589
Olivier Houchard7505f942018-08-21 18:10:44 +02002590 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002591 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002592 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2593 /* HTX in use : try to pre-align the buffer like the
2594 * rxbufs will be to optimize memory copies. We'll make
2595 * sure that the frame header lands at the end of the
2596 * HTX block to alias it upon recv. We cannot use the
2597 * head because rcv_buf() will realign the buffer if
2598 * it's empty. Thus we cheat and pretend we already
2599 * have a few bytes there.
2600 */
2601 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002602 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002603 }
2604 else
2605 max = b_room(buf);
2606
Olivier Houchard7505f942018-08-21 18:10:44 +02002607 if (max)
2608 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2609 else
2610 ret = 0;
2611 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002612
Olivier Houchard53216e72018-10-10 15:46:36 +02002613 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002614 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002615
Olivier Houcharda1411e62018-08-17 18:42:48 +02002616 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002617 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002618 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002619 }
2620
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002621 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002622 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002623 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002624}
2625
Willy Tarreau479998a2018-11-18 06:30:59 +01002626/* Try to send data if possible.
2627 * The function returns 1 if data have been sent, otherwise zero.
2628 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002629static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002630{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002631 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002632 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002633 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002634
2635 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002636 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002637
Olivier Houchard7505f942018-08-21 18:10:44 +02002638
Willy Tarreaua2af5122017-10-09 11:56:46 +02002639 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2640 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002641 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002642 }
2643
Willy Tarreaubc933932017-10-09 16:21:43 +02002644 /* This loop is quite simple : it tries to fill as much as it can from
2645 * pending streams into the existing buffer until it's reportedly full
2646 * or the end of send requests is reached. Then it tries to send this
2647 * buffer's contents out, marks it not full if at least one byte could
2648 * be sent, and tries again.
2649 *
2650 * The snd_buf() function normally takes a "flags" argument which may
2651 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2652 * data immediately comes and CO_SFL_STREAMER to indicate that the
2653 * connection is streaming lots of data (used to increase TLS record
2654 * size at the expense of latency). The former can be sent any time
2655 * there's a buffer full flag, as it indicates at least one stream
2656 * attempted to send and failed so there are pending data. An
2657 * alternative would be to set it as long as there's an active stream
2658 * but that would be problematic for ACKs until we have an absolute
2659 * guarantee that all waiters have at least one byte to send. The
2660 * latter should possibly not be set for now.
2661 */
2662
2663 done = 0;
2664 while (!done) {
2665 unsigned int flags = 0;
2666
2667 /* fill as much as we can into the current buffer */
2668 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2669 done = h2_process_mux(h2c);
2670
2671 if (conn->flags & CO_FL_ERROR)
2672 break;
2673
2674 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2675 flags |= CO_SFL_MSG_MORE;
2676
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002677 if (b_data(&h2c->mbuf)) {
2678 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002679 if (!ret)
2680 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002681 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002682 b_del(&h2c->mbuf, ret);
2683 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002684 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002685
2686 /* wrote at least one byte, the buffer is not full anymore */
2687 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2688 }
2689
Willy Tarreaua2af5122017-10-09 11:56:46 +02002690 if (conn->flags & CO_FL_SOCK_WR_SH) {
2691 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002692 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002693 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002694 /* We're not full anymore, so we can wake any task that are waiting
2695 * for us.
2696 */
2697 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002698 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002699 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2700 struct h2s *, list);
2701 LIST_DEL(&h2s->list);
2702 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002703 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002704 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2705 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002706 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002707 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002708 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002709 /* We're done, no more to send */
2710 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002711 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002712schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002713 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2714 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002715 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002716}
2717
2718static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2719{
2720 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002721 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002722
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002723 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002724 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002725 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002726 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002727 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002728 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002729 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002730}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731
Willy Tarreau62f52692017-10-08 23:01:42 +02002732/* callback called on any event by the connection handler.
2733 * It applies changes and returns zero, or < 0 if it wants immediate
2734 * destruction of the connection (which normally doesn not happen in h2).
2735 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002736static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002737{
Olivier Houchard7505f942018-08-21 18:10:44 +02002738 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002739
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002740 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002741 h2_process_demux(h2c);
2742
2743 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002744 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002745
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002746 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002747 h2c->flags &= ~H2_CF_DEM_DFULL;
2748 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002749 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002750
Willy Tarreau0b37d652018-10-03 10:33:02 +02002751 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002752 /* frontend is stopping, reload likely in progress, let's try
2753 * to announce a graceful shutdown if not yet done. We don't
2754 * care if it fails, it will be tried again later.
2755 */
2756 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2757 if (h2c->last_sid < 0)
2758 h2c->last_sid = (1U << 31) - 1;
2759 h2c_send_goaway_error(h2c, NULL);
2760 }
2761 }
2762
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002763 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002764 * If we received early data, and the handshake is done, wake
2765 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002766 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002767 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2768 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2769 struct eb32_node *node;
2770 struct h2s *h2s;
2771
2772 h2c->flags |= H2_CF_WAIT_FOR_HS;
2773 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2774
2775 while (node) {
2776 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002777 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002778 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002779 node = eb32_next(node);
2780 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002781 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002782
Willy Tarreau26bd7612017-10-09 16:47:04 +02002783 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002784 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2785 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2786 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002787 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002788
2789 if (eb_is_empty(&h2c->streams_by_id)) {
2790 /* no more stream, kill the connection now */
2791 h2_release(conn);
2792 return -1;
2793 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002794 }
2795
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002796 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002797 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002798
Olivier Houchard53216e72018-10-10 15:46:36 +02002799 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2800 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2801 (h2c->st0 != H2_CS_ERROR &&
2802 !b_data(&h2c->mbuf) &&
2803 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2804 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002805 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002806
Willy Tarreau3f133572017-10-31 19:21:06 +01002807 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002809 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002810 task_queue(h2c->task);
2811 }
2812 else
2813 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002814 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002815
Olivier Houchard7505f942018-08-21 18:10:44 +02002816 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002817 return 0;
2818}
2819
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002820static int h2_wake(struct connection *conn)
2821{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002822 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002823
2824 return (h2_process(h2c));
2825}
2826
Willy Tarreauea392822017-10-31 10:02:25 +01002827/* Connection timeout management. The principle is that if there's no receipt
2828 * nor sending for a certain amount of time, the connection is closed. If the
2829 * MUX buffer still has lying data or is not allocatable, the connection is
2830 * immediately killed. If it's allocatable and empty, we attempt to send a
2831 * GOAWAY frame.
2832 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002833static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002834{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002835 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002836 int expired = tick_is_expired(t->expire, now_ms);
2837
Willy Tarreau0975f112018-03-29 15:22:59 +02002838 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002839 return t;
2840
Willy Tarreau0975f112018-03-29 15:22:59 +02002841 task_delete(t);
2842 task_free(t);
2843
2844 if (!h2c) {
2845 /* resources were already deleted */
2846 return NULL;
2847 }
2848
2849 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002850 h2c_error(h2c, H2_ERR_NO_ERROR);
2851 h2_wake_some_streams(h2c, 0, 0);
2852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002854 /* don't even try to send a GOAWAY, the buffer is stuck */
2855 h2c->flags |= H2_CF_GOAWAY_FAILED;
2856 }
2857
2858 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002859 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002860 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2861 h2c->flags |= H2_CF_GOAWAY_FAILED;
2862
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002863 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2864 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002865 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002866 b_del(&h2c->mbuf, ret);
2867 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002868 }
2869 }
Willy Tarreauea392822017-10-31 10:02:25 +01002870
Willy Tarreau0975f112018-03-29 15:22:59 +02002871 /* either we can release everything now or it will be done later once
2872 * the last stream closes.
2873 */
2874 if (eb_is_empty(&h2c->streams_by_id))
2875 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002876
Willy Tarreauea392822017-10-31 10:02:25 +01002877 return NULL;
2878}
2879
2880
Willy Tarreau62f52692017-10-08 23:01:42 +02002881/*******************************************/
2882/* functions below are used by the streams */
2883/*******************************************/
2884
2885/*
2886 * Attach a new stream to a connection
2887 * (Used for outgoing connections)
2888 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002889static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002890{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002891 struct conn_stream *cs;
2892 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002893 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002894
2895 cs = cs_new(conn);
2896 if (!cs)
2897 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002898 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002899 if (!h2s) {
2900 cs_free(cs);
2901 return NULL;
2902 }
2903 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002904}
2905
Willy Tarreaufafd3982018-11-18 21:29:20 +01002906/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2907 * We have to scan because we may have some orphan streams. It might be
2908 * beneficial to scan backwards from the end to reduce the likeliness to find
2909 * orphans.
2910 */
2911static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2912{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002913 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002914 struct h2s *h2s;
2915 struct eb32_node *node;
2916
2917 node = eb32_first(&h2c->streams_by_id);
2918 while (node) {
2919 h2s = container_of(node, struct h2s, by_id);
2920 if (h2s->cs)
2921 return h2s->cs;
2922 node = eb32_next(node);
2923 }
2924 return NULL;
2925}
2926
Willy Tarreau62f52692017-10-08 23:01:42 +02002927/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002928 * Destroy the mux and the associated connection, if it is no longer used
2929 */
2930static void h2_destroy(struct connection *conn)
2931{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002932 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002933
2934 if (eb_is_empty(&h2c->streams_by_id))
2935 h2_release(h2c->conn);
2936}
2937
2938/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002939 * Detach the stream from the connection and possibly release the connection.
2940 */
2941static void h2_detach(struct conn_stream *cs)
2942{
Willy Tarreau60935142017-10-16 18:11:19 +02002943 struct h2s *h2s = cs->ctx;
2944 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002945 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002946
2947 cs->ctx = NULL;
2948 if (!h2s)
2949 return;
2950
Olivier Houchardf502aca2018-12-14 19:42:40 +01002951 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002952 h2c = h2s->h2c;
2953 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002954 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002955 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2956 !h2_has_too_many_cs(h2c)) {
2957 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002958 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002959 }
Willy Tarreau60935142017-10-16 18:11:19 +02002960
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002961 /* this stream may be blocked waiting for some data to leave (possibly
2962 * an ES or RST frame), so orphan it in this case.
2963 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002964 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002965 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002966 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002967 return;
2968
Willy Tarreau45f752e2017-10-30 15:44:59 +01002969 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2970 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2971 /* unblock the connection if it was blocked on this
2972 * stream.
2973 */
2974 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2975 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01002976 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002977 }
2978
Willy Tarreau71049cc2018-03-28 13:56:39 +02002979 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002980
Olivier Houchard8a786902018-12-15 16:05:40 +01002981 if (h2c->flags & H2_CF_IS_BACK &&
2982 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002983 if (!(h2c->conn->flags &
2984 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2985 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002986 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01002987 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
2988 h2c->conn->owner = NULL;
2989 if (eb_is_empty(&h2c->streams_by_id)) {
2990 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
2991 /* The server doesn't want it, let's kill the connection right away */
2992 h2c->conn->mux->destroy(h2c->conn);
2993 return;
2994 }
2995 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002996 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002997 if (eb_is_empty(&h2c->streams_by_id)) {
2998 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2999 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3000 return;
3001 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003002 /* Never ever allow to reuse a connection from a non-reuse backend */
3003 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3004 h2c->conn->flags |= CO_FL_PRIVATE;
Olivier Houchard855ac252018-12-28 14:44:41 +01003005 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003006 struct server *srv = objt_server(h2c->conn->target);
3007
3008 if (srv) {
3009 if (h2c->conn->flags & CO_FL_PRIVATE)
3010 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3011 else
3012 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3013 }
3014
3015 }
3016 }
3017 }
3018
Willy Tarreaue323f342018-03-28 13:51:45 +02003019 /* We don't want to close right now unless we're removing the
3020 * last stream, and either the connection is in error, or it
3021 * reached the ID already specified in a GOAWAY frame received
3022 * or sent (as seen by last_sid >= 0).
3023 */
3024 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
3025 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02003026 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02003027 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01003028 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003029 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02003030 (conn_xprt_read0_pending(h2c->conn) ||
3031 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
3032 /* no more stream will come, kill it now */
3033 h2_release(h2c->conn);
3034 }
3035 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003036 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003037 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3038 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003039 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003040 else
3041 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003042 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003043}
3044
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003045static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003046{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003047 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003048 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003049
Willy Tarreau721c9742017-11-07 11:05:42 +01003050 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003051 return;
3052
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003053 /* if no outgoing data was seen on this stream, it means it was
3054 * closed with a "tcp-request content" rule that is normally
3055 * used to kill the connection ASAP (eg: limit abuse). In this
3056 * case we send a goaway to close the connection.
3057 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003058 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003059 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003060 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003061
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003062 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3063 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003064 h2c_send_goaway_error(h2c, h2s) <= 0)
3065 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003066
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003067 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003068 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003069 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003070
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003071 return;
3072add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003073 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003074 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003075 if (h2s->flags & H2_SF_BLK_MFCTL) {
3076 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3077 h2s->send_wait = sw;
3078 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3079 h2s->send_wait = sw;
3080 LIST_ADDQ(&h2c->send_list, &h2s->list);
3081 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003082 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003083 /* Let the handler know we want shutr */
3084 sw->handle = (void *)((long)sw->handle | 1);
3085
Willy Tarreau62f52692017-10-08 23:01:42 +02003086}
3087
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003088static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003089{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003090 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003091 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003092
Willy Tarreau721c9742017-11-07 11:05:42 +01003093 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003094 return;
3095
Willy Tarreau67434202017-11-06 20:20:51 +01003096 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003097 /* we can cleanly close using an empty data frame only after headers */
3098
3099 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3100 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003101 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003102
3103 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003104 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003105 else
3106 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003107 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003108 /* if no outgoing data was seen on this stream, it means it was
3109 * closed with a "tcp-request content" rule that is normally
3110 * used to kill the connection ASAP (eg: limit abuse). In this
3111 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003112 */
Willy Tarreau90c32322017-11-24 08:00:30 +01003113 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003114 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003115 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003116
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003117 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
3118 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003119 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003120 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01003121
Willy Tarreau00dd0782018-03-01 16:31:34 +01003122 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003123 }
3124
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003125 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003126 tasklet_wakeup(h2c->wait_event.task);
3127 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003128
3129 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003130 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003131 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003132 if (h2s->flags & H2_SF_BLK_MFCTL) {
3133 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3134 h2s->send_wait = sw;
3135 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3136 h2s->send_wait = sw;
3137 LIST_ADDQ(&h2c->send_list, &h2s->list);
3138 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003139 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003140 /* let the handler know we want to shutw */
3141 sw->handle = (void *)((long)(sw->handle) | 2);
3142
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003143}
3144
3145static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3146{
3147 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003148 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003149
Olivier Houchard2c68a462018-12-15 22:42:20 +01003150 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003151 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003152 h2s->send_wait = NULL;
3153 LIST_DEL(&h2s->list);
3154 LIST_INIT(&h2s->list);
3155 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003156 if (reason & 2)
3157 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003158 if (reason & 1)
3159 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003160
Olivier Houchard2c68a462018-12-15 22:42:20 +01003161 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003162 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003163 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003164 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003165}
3166
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003167static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3168{
3169 struct h2s *h2s = cs->ctx;
3170
3171 if (!mode)
3172 return;
3173
3174 h2_do_shutr(h2s);
3175}
3176
3177static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3178{
3179 struct h2s *h2s = cs->ctx;
3180
3181 h2_do_shutw(h2s);
3182}
3183
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003184/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003185 * HTX request or response depending on the connection's side. Returns a
3186 * positive value on success, a negative value on failure, or 0 if it couldn't
3187 * proceed. May report connection errors in h2c->errcode if the frame is
3188 * non-decodable and the connection unrecoverable. In absence of connection
3189 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003190 *
3191 * The function may fold CONTINUATION frames into the initial HEADERS frame
3192 * by removing padding and next frame header, then moving the CONTINUATION
3193 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3194 * leaving a hole between the main frame and the beginning of the next one.
3195 * The possibly remaining incomplete or next frame at the end may be moved
3196 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3197 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3198 *
3199 * A buffer at the beginning of processing may look like this :
3200 *
3201 * ,---.---------.-----.--------------.--------------.------.---.
3202 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3203 * `---^---------^-----^--------------^--------------^------^---'
3204 * | | <-----> | |
3205 * area | dpl | wrap
3206 * |<--------------> |
3207 * | dfl |
3208 * |<-------------------------------------------------->|
3209 * head data
3210 *
3211 * Padding is automatically overwritten when folding, participating to the
3212 * hole size after dfl :
3213 *
3214 * ,---.------------------------.-----.--------------.------.---.
3215 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3216 * `---^------------------------^-----^--------------^------^---'
3217 * | | <-----> | |
3218 * area | hole | wrap
3219 * |<-----------------------> |
3220 * | dfl |
3221 * |<-------------------------------------------------->|
3222 * head data
3223 *
3224 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3225 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3226 * bit.
Willy Tarreau13278b42017-10-13 19:23:14 +02003227 */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003228static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02003229{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003230 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003231 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003232 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003233 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003234 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003235 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003236 int flen; // header frame len
3237 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003238 int ret = 0;
3239 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003240 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003241 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003242
Willy Tarreauea18f862018-12-22 20:19:26 +01003243next_frame:
3244 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3245 goto leave; // incomplete input frame
3246
3247 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3248 * this case, we'll try to paste it immediately after the initial
3249 * HEADERS frame payload and kill any possible padding. The initial
3250 * frame's length will be increased to represent the concatenation
3251 * of the two frames. The next frame is read from position <tlen>
3252 * and written at position <flen> (minus padding if some is present).
3253 */
3254 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3255 struct h2_fh hdr;
3256 int clen; // CONTINUATION frame's payload length
3257
3258 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3259 /* no more data, the buffer may be full, either due to
3260 * too large a frame or because of too large a hole that
3261 * we're going to compact at the end.
3262 */
3263 goto leave;
3264 }
3265
3266 if (hdr.ft != H2_FT_CONTINUATION) {
3267 /* RFC7540#6.10: frame of unexpected type */
3268 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3269 goto fail;
3270 }
3271
3272 if (hdr.sid != h2c->dsi) {
3273 /* RFC7540#6.10: frame of different stream */
3274 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3275 goto fail;
3276 }
3277
3278 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3279 /* RFC7540#4.2: invalid frame length */
3280 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3281 goto fail;
3282 }
3283
3284 /* detect when we must stop aggragating frames */
3285 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3286
3287 /* Take as much as we can of the CONTINUATION frame's payload */
3288 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3289 if (clen > hdr.len)
3290 clen = hdr.len;
3291
3292 /* Move the frame's payload over the padding, hole and frame
3293 * header. At least one of hole or dpl is null (see diagrams
3294 * above). The hole moves after the new aggragated frame.
3295 */
3296 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3297 h2c->dfl += clen - h2c->dpl;
3298 hole += h2c->dpl + 9;
3299 h2c->dpl = 0;
3300 goto next_frame;
3301 }
3302
3303 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003304
Willy Tarreau13278b42017-10-13 19:23:14 +02003305 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003306 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003307 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003308 copy = alloc_trash_chunk();
3309 if (!copy) {
3310 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3311 goto fail;
3312 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003313 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3314 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3315 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003316 }
3317
Willy Tarreau13278b42017-10-13 19:23:14 +02003318 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3319 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003320 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003321 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3322 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003323 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003324 }
3325
Willy Tarreau13278b42017-10-13 19:23:14 +02003326 hdrs += 5; // stream dep = 4, weight = 1
3327 flen -= 5;
3328 }
3329
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003330 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003331 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003332 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003333 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003334
Willy Tarreau937f7602018-02-26 15:22:17 +01003335 /* we can't retry a failed decompression operation so we must be very
3336 * careful not to take any risks. In practice the output buffer is
3337 * always empty except maybe for trailers, in which case we simply have
3338 * to wait for the upper layer to finish consuming what is available.
3339 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003340
3341 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003342 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003343 if (!htx_is_empty(htx)) {
3344 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003345 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003346 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003347 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003348 if (b_data(rxbuf)) {
3349 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003350 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003351 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003352
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003353 rxbuf->head = 0;
3354 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003355 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003356
Willy Tarreau25919232019-01-03 14:48:18 +01003357 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003358 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3359 sizeof(list)/sizeof(list[0]), tmp);
3360 if (outlen < 0) {
3361 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3362 goto fail;
3363 }
3364
Willy Tarreau25919232019-01-03 14:48:18 +01003365 /* The PACK decompressor was updated, let's update the input buffer and
3366 * the parser's state to commit these changes and allow us to later
3367 * fail solely on the stream if needed.
3368 */
3369 b_del(&h2c->dbuf, h2c->dfl + hole);
3370 h2c->dfl = hole = 0;
3371 h2c->st0 = H2_CS_FRAME_H;
3372
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003373 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003374 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003375
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003376 if (htx) {
3377 /* HTX mode */
3378 if (h2c->flags & H2_CF_IS_BACK)
3379 outlen = h2_make_htx_response(list, htx, &msgf);
3380 else
3381 outlen = h2_make_htx_request(list, htx, &msgf);
3382 } else {
3383 /* HTTP/1 mode */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003384 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf);
Willy Tarreau83195932019-01-03 10:26:23 +01003385 if (outlen > 0)
3386 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003387 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003388
3389 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003390 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003391 goto fail;
3392 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003393
Willy Tarreau174b06a2018-04-25 18:13:58 +02003394 if (msgf & H2_MSGF_BODY) {
3395 /* a payload is present */
3396 if (msgf & H2_MSGF_BODY_CL)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003397 *flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003398 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003399 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003400 }
3401
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003402 if (htx && h2c->dff & H2_F_HEADERS_END_STREAM)
3403 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau937f7602018-02-26 15:22:17 +01003404
Willy Tarreau86277d42019-01-02 15:36:11 +01003405 /* success */
3406 ret = 1;
3407
Willy Tarreau68dd9852017-07-03 14:44:26 +02003408 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003409 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003410 * move the remaining data over it.
3411 */
3412 if (hole) {
3413 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3414 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3415 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3416 b_sub(&h2c->dbuf, hole);
3417 }
3418
3419 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3420 /* too large frames */
3421 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003422 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003423 }
3424
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003425 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003426 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003427 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003428 return ret;
3429
Willy Tarreau68dd9852017-07-03 14:44:26 +02003430 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003431 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003432 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003433}
3434
Willy Tarreau454f9052017-10-26 19:40:35 +02003435/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3436 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3437 * in use, a new chunk is emitted for each frame. This is supposed to fit
3438 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3439 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3440 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003441 * parser state is automatically updated. Returns > 0 if it could completely
3442 * send the current frame, 0 if it couldn't complete, in which case
3443 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3444 * DATA frame can return 0 as a valid result). Stream errors are reported in
3445 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3446 * have checked the frame header and ensured that the frame was complete or the
3447 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003448 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003449static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003450{
3451 struct h2c *h2c = h2s->h2c;
3452 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003453 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003454 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003455 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003456 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003457
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003458 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003459
Olivier Houchard638b7992018-08-16 15:41:52 +02003460 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003461 if (!csbuf) {
3462 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003463 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003464 }
3465
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003466try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003467 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003468 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3469 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003470 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003471 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003472
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003473 if (flen > b_data(&h2c->dbuf)) {
3474 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003475 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003476 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003477 }
3478
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003479 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003480 block1 = htx_free_data_space(htx);
3481 if (!block1) {
3482 h2c->flags |= H2_CF_DEM_SFULL;
3483 goto fail;
3484 }
3485 if (flen > block1)
3486 flen = block1;
3487
3488 /* here, flen is the max we can copy into the output buffer */
3489 block1 = b_contig_data(&h2c->dbuf, 0);
3490 if (flen > block1)
3491 flen = block1;
3492
3493 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3494 h2c->flags |= H2_CF_DEM_SFULL;
3495 goto fail;
3496 }
3497
3498 b_del(&h2c->dbuf, flen);
3499 h2c->dfl -= flen;
3500 h2c->rcvd_c += flen;
3501 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3502 goto try_again;
3503 }
3504 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003505 /* it doesn't fit and the buffer is fragmented,
3506 * so let's defragment it and try again.
3507 */
3508 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003509 }
3510
Willy Tarreaueba10f22018-04-25 20:44:22 +02003511 /* chunked-encoding requires more room */
3512 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003513 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003514 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3515 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3516 (chklen < 1048576) ? 4 : 8;
3517 chklen += 4; // CRLF, CRLF
3518 }
3519
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003520 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003521 if (flen + chklen > b_room(csbuf)) {
3522 if (chklen >= b_room(csbuf)) {
3523 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003524 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003525 }
3526 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003527 }
3528
3529 if (h2s->flags & H2_SF_DATA_CHNK) {
3530 /* emit the chunk size */
3531 unsigned int chksz = flen;
3532 char str[10];
3533 char *beg;
3534
3535 beg = str + sizeof(str);
3536 *--beg = '\n';
3537 *--beg = '\r';
3538 do {
3539 *--beg = hextab[chksz & 0xF];
3540 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003541 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003542 }
3543
Willy Tarreau454f9052017-10-26 19:40:35 +02003544 /* Block1 is the length of the first block before the buffer wraps,
3545 * block2 is the optional second block to reach the end of the frame.
3546 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003547 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003548 if (block1 > flen)
3549 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003550 block2 = flen - block1;
3551
3552 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003553 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003554
3555 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003556 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003557
Willy Tarreaueba10f22018-04-25 20:44:22 +02003558 if (h2s->flags & H2_SF_DATA_CHNK) {
3559 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003560 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003561 }
3562
Willy Tarreau454f9052017-10-26 19:40:35 +02003563 /* now mark the input data as consumed (will be deleted from the buffer
3564 * by the caller when seeing FRAME_A after sending the window update).
3565 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003566 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003567 h2c->dfl -= flen;
3568 h2c->rcvd_c += flen;
3569 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3570
3571 if (h2c->dfl > h2c->dpl) {
3572 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003573 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003574 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003575 }
3576
Willy Tarreau4a28da12018-01-04 14:41:00 +01003577 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003578 /* here we're done with the frame, all the payload (except padding) was
3579 * transferred.
3580 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003581
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003582 if (h2c->dff & H2_F_DATA_END_STREAM) {
3583 if (htx) {
3584 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3585 h2c->flags |= H2_CF_DEM_SFULL;
3586 goto fail;
3587 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003588 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003589 else if (h2s->flags & H2_SF_DATA_CHNK) {
3590 /* emit the trailing 0 CRLF CRLF */
3591 if (b_room(csbuf) < 5) {
3592 h2c->flags |= H2_CF_DEM_SFULL;
3593 goto fail;
3594 }
3595 chklen += 5;
3596 b_putblk(csbuf, "0\r\n\r\n", 5);
3597 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003598 }
3599
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003600 h2c->rcvd_c += h2c->dpl;
3601 h2c->rcvd_s += h2c->dpl;
3602 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003603 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3604
Willy Tarreau39d68502018-03-02 12:26:37 +01003605 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003606 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003607 h2s->cs->flags |= CS_FL_REOS;
3608 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003609 if (htx)
3610 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003611 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003612 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003613 if (htx)
3614 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003615 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003616}
3617
Willy Tarreau5dd17352018-06-14 13:33:30 +02003618/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3619 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3620 * number of bytes sent. The caller must check the stream's status to detect
3621 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003622 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003623static 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 +02003624{
3625 struct http_hdr list[MAX_HTTP_HDR];
3626 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003627 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003628 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003629 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003630 int es_now = 0;
3631 int ret = 0;
3632 int hdr;
3633
3634 if (h2c_mux_busy(h2c, h2s)) {
3635 h2s->flags |= H2_SF_BLK_MBUSY;
3636 return 0;
3637 }
3638
Willy Tarreau44e973f2018-03-01 17:49:30 +01003639 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003640 h2c->flags |= H2_CF_MUX_MALLOC;
3641 h2s->flags |= H2_SF_BLK_MROOM;
3642 return 0;
3643 }
3644
3645 /* First, try to parse the H1 response and index it into <list>.
3646 * NOTE! Since it comes from haproxy, we *know* that a response header
3647 * block does not wrap and we can safely read it this way without
3648 * having to realign the buffer.
3649 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003650 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003651 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003652 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003653 /* incomplete or invalid response, this is abnormal coming from
3654 * haproxy and may only result in a bad errorfile or bad Lua code
3655 * so that won't be fixed, raise an error now.
3656 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003657 * FIXME: we should instead add the ability to only return a
3658 * 502 bad gateway. But in theory this is not supposed to
3659 * happen.
3660 */
3661 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3662 ret = 0;
3663 goto end;
3664 }
3665
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003666 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003667
3668 /* certain statuses have no body or an empty one, regardless of
3669 * what the headers say.
3670 */
3671 if (sl.st.status >= 100 && sl.st.status < 200) {
3672 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3673 h1m->curr_len = h1m->body_len = 0;
3674 }
3675 else if (sl.st.status == 204 || sl.st.status == 304) {
3676 /* no contents, claim c-len is present and set to zero */
3677 h1m->flags &= ~H1_MF_CHNK;
3678 h1m->flags |= H1_MF_CLEN;
3679 h1m->curr_len = h1m->body_len = 0;
3680 }
3681
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003682 chunk_reset(&outbuf);
3683
3684 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003685 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003686 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003687 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003688
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003689 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003690 break;
3691 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003692 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003693 }
3694
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003695 if (outbuf.size < 9)
3696 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003697
3698 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003699 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3700 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3701 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003702
3703 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003704 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003705 /* this is an unparsable response */
3706 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3707 ret = 0;
3708 goto end;
3709 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003710
3711 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003712 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003713 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003714 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003715 }
3716
3717 /* encode all headers, stop at empty name */
3718 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003719 /* these ones do not exist in H2 and must be dropped. */
3720 if (isteq(list[hdr].n, ist("connection")) ||
3721 isteq(list[hdr].n, ist("proxy-connection")) ||
3722 isteq(list[hdr].n, ist("keep-alive")) ||
3723 isteq(list[hdr].n, ist("upgrade")) ||
3724 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003725 continue;
3726
3727 if (isteq(list[hdr].n, ist("")))
3728 break; // end
3729
3730 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3731 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003732 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003733 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003734 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003735 }
3736 }
3737
3738 /* we may need to add END_STREAM */
3739 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3740 es_now = 1;
3741
3742 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003743 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003744
3745 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003747
3748 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003749 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003750
3751 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003752 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003753 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003754
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003755 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003756 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003757 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003758
Willy Tarreau801250e2018-09-11 11:45:04 +02003759 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003760 h2s->flags |= H2_SF_ES_SENT;
3761 if (h2s->st == H2_SS_OPEN)
3762 h2s->st = H2_SS_HLOC;
3763 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003764 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003765 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003766 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003767 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003768 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003769 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003770 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003771 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003772 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003773
3774 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003775
3776 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003777 //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 +02003778 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003779 full:
3780 h1m_init_res(h1m);
3781 h1m->err_pos = -1; // don't care about errors on the response path
3782 h2c->flags |= H2_CF_MUX_MFULL;
3783 h2s->flags |= H2_SF_BLK_MROOM;
3784 ret = 0;
3785 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003786}
3787
Willy Tarreau5dd17352018-06-14 13:33:30 +02003788/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3789 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3790 * the number of bytes sent. The caller must check the stream's status to
3791 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003792 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003793static 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 +02003794{
3795 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003796 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003797 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003798 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003799 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003800 int es_now = 0;
3801 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003802 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003803 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003804
3805 if (h2c_mux_busy(h2c, h2s)) {
3806 h2s->flags |= H2_SF_BLK_MBUSY;
3807 goto end;
3808 }
3809
Willy Tarreau44e973f2018-03-01 17:49:30 +01003810 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003811 h2c->flags |= H2_CF_MUX_MALLOC;
3812 h2s->flags |= H2_SF_BLK_MROOM;
3813 goto end;
3814 }
3815
3816 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003817 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003818 goto end;
3819
3820 chunk_reset(&outbuf);
3821
3822 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003823 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003824 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003825 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003826
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003827 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003828 break;
3829 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003830 /* If there are pending data in the output buffer, and we have
3831 * less than 1/4 of the mbuf's size and everything fits, we'll
3832 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3833 * is full and wait, to save some slow realign calls.
3834 */
3835 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3836 h2c->flags |= H2_CF_MUX_MFULL;
3837 h2s->flags |= H2_SF_BLK_MROOM;
3838 goto end;
3839 }
3840
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003841 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003842 }
3843
3844 if (outbuf.size < 9) {
3845 h2c->flags |= H2_CF_MUX_MFULL;
3846 h2s->flags |= H2_SF_BLK_MROOM;
3847 goto end;
3848 }
3849
3850 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003851 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3852 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3853 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003854
3855 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3856 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003857 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003858 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003859 break;
3860 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003861 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003862 if ((long long)size > h1m->curr_len)
3863 size = h1m->curr_len;
3864 break;
3865 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003866 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003867 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003868 if (!ret)
3869 goto end;
3870
3871 if (ret < 0) {
3872 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003873 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003874 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3875 goto end;
3876 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003877 max -= ret;
3878 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003879 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003880 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003881 }
3882
Willy Tarreau801250e2018-09-11 11:45:04 +02003883 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003884 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003885 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003886 if (!ret)
3887 goto end;
3888
3889 if (ret < 0) {
3890 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003891 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003892 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3893 goto end;
3894 }
3895
3896 size = chunk;
3897 h1m->curr_len = chunk;
3898 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003899 max -= ret;
3900 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003901 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003902 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003903 if (!size)
3904 goto send_empty;
3905 }
3906
3907 /* in MSG_DATA state, continue below */
3908 size = h1m->curr_len;
3909 break;
3910 }
3911
3912 /* we have in <size> the exact number of bytes we need to copy from
3913 * the H1 buffer. We need to check this against the connection's and
3914 * the stream's send windows, and to ensure that this fits in the max
3915 * frame size and in the buffer's available space minus 9 bytes (for
3916 * the frame header). The connection's flow control is applied last so
3917 * that we can use a separate list of streams which are immediately
3918 * unblocked on window opening. Note: we don't implement padding.
3919 */
3920
Willy Tarreau5dd17352018-06-14 13:33:30 +02003921 if (size > max)
3922 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003923
3924 if (size > h2s->mws)
3925 size = h2s->mws;
3926
3927 if (size <= 0) {
3928 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003929 if (h2s->send_wait) {
3930 LIST_DEL(&h2s->list);
3931 LIST_INIT(&h2s->list);
3932 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003933 goto end;
3934 }
3935
3936 if (h2c->mfs && size > h2c->mfs)
3937 size = h2c->mfs;
3938
3939 if (size + 9 > outbuf.size) {
3940 /* we have an opportunity for enlarging the too small
3941 * available space, let's try.
3942 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003943 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003944 goto realign_again;
3945 size = outbuf.size - 9;
3946 }
3947
3948 if (size <= 0) {
3949 h2c->flags |= H2_CF_MUX_MFULL;
3950 h2s->flags |= H2_SF_BLK_MROOM;
3951 goto end;
3952 }
3953
3954 if (size > h2c->mws)
3955 size = h2c->mws;
3956
3957 if (size <= 0) {
3958 h2s->flags |= H2_SF_BLK_MFCTL;
3959 goto end;
3960 }
3961
3962 /* copy whatever we can */
3963 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003964 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003965 if (ret == 1)
3966 len2 = 0;
3967
3968 if (!ret || len1 + len2 < size) {
3969 /* FIXME: must normally never happen */
3970 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3971 goto end;
3972 }
3973
3974 /* limit len1/len2 to size */
3975 if (len1 + len2 > size) {
3976 int sub = len1 + len2 - size;
3977
3978 if (len2 > sub)
3979 len2 -= sub;
3980 else {
3981 sub -= len2;
3982 len2 = 0;
3983 len1 -= sub;
3984 }
3985 }
3986
3987 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003988 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003989 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003990 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003991
3992 send_empty:
3993 /* we may need to add END_STREAM */
3994 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3995 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003996 *
3997 * FIXME: what we do here is not correct because we send end_stream
3998 * before knowing if we'll have to send a HEADERS frame for the
3999 * trailers. More importantly we're not consuming the trailing CRLF
4000 * after the end of trailers, so it will be left to the caller to
4001 * eat it. The right way to do it would be to measure trailers here
4002 * and to send ES only if there are no trailers.
4003 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004004 */
4005 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004006 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004007 es_now = 1;
4008
4009 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004010 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004011
4012 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004013 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004014
4015 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004016 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004017
4018 /* consume incoming H1 response */
4019 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004020 max -= size;
4021 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004022 total += size;
4023 h1m->curr_len -= size;
4024 h2s->mws -= size;
4025 h2c->mws -= size;
4026
4027 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004028 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004029 goto new_frame;
4030 }
4031 }
4032
4033 if (es_now) {
4034 if (h2s->st == H2_SS_OPEN)
4035 h2s->st = H2_SS_HLOC;
4036 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004037 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004038
Willy Tarreau35a62702018-02-27 15:37:25 +01004039 if (!(h1m->flags & H1_MF_CHNK)) {
4040 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004041 total += max;
4042 ofs += max;
4043 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004044
Willy Tarreau801250e2018-09-11 11:45:04 +02004045 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004046 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004047
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004048 h2s->flags |= H2_SF_ES_SENT;
4049 }
4050
4051 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004052 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 +02004053 return total;
4054}
4055
Willy Tarreau115e83b2018-12-01 19:17:53 +01004056/* Try to send a HEADERS frame matching HTX response present in HTX message
4057 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4058 * must check the stream's status to detect any error which might have happened
4059 * subsequently to a successful send. The htx blocks are automatically removed
4060 * from the message. The htx message is assumed to be valid since produced from
4061 * the internal code, hence it contains a start line, an optional series of
4062 * header blocks and an end of header, otherwise an invalid frame could be
4063 * emitted and the resulting htx message could be left in an inconsistent state.
4064 */
4065static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4066{
4067 struct http_hdr list[MAX_HTTP_HDR];
4068 struct h2c *h2c = h2s->h2c;
4069 struct htx_blk *blk;
4070 struct htx_blk *blk_end;
4071 struct buffer outbuf;
4072 struct htx_sl *sl;
4073 enum htx_blk_type type;
4074 int es_now = 0;
4075 int ret = 0;
4076 int hdr;
4077 int idx;
4078
4079 if (h2c_mux_busy(h2c, h2s)) {
4080 h2s->flags |= H2_SF_BLK_MBUSY;
4081 return 0;
4082 }
4083
4084 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4085 h2c->flags |= H2_CF_MUX_MALLOC;
4086 h2s->flags |= H2_SF_BLK_MROOM;
4087 return 0;
4088 }
4089
4090 /* determine the first block which must not be deleted, blk_end may
4091 * be NULL if all blocks have to be deleted.
4092 */
4093 idx = htx_get_head(htx);
4094 blk_end = NULL;
4095 while (idx != -1) {
4096 type = htx_get_blk_type(htx_get_blk(htx, idx));
4097 idx = htx_get_next(htx, idx);
4098 if (type == HTX_BLK_EOH) {
4099 if (idx != -1)
4100 blk_end = htx_get_blk(htx, idx);
4101 break;
4102 }
4103 }
4104
4105 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004106 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004107 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004108 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004109 if (h2s->status < 100 || h2s->status > 999)
4110 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004111
4112 /* and the rest of the headers, that we dump starting at header 0 */
4113 hdr = 0;
4114
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004115 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004116 while ((idx = htx_get_next(htx, idx)) != -1) {
4117 blk = htx_get_blk(htx, idx);
4118 type = htx_get_blk_type(blk);
4119
4120 if (type == HTX_BLK_UNUSED)
4121 continue;
4122
4123 if (type != HTX_BLK_HDR)
4124 break;
4125
4126 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4127 goto fail;
4128
4129 list[hdr].n = htx_get_blk_name(htx, blk);
4130 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004131 hdr++;
4132 }
4133
4134 /* marker for end of headers */
4135 list[hdr].n = ist("");
4136
4137 if (h2s->status == 204 || h2s->status == 304) {
4138 /* no contents, claim c-len is present and set to zero */
4139 es_now = 1;
4140 }
4141
4142 chunk_reset(&outbuf);
4143
4144 while (1) {
4145 outbuf.area = b_tail(&h2c->mbuf);
4146 outbuf.size = b_contig_space(&h2c->mbuf);
4147 outbuf.data = 0;
4148
4149 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4150 break;
4151 realign_again:
4152 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4153 }
4154
4155 if (outbuf.size < 9)
4156 goto full;
4157
4158 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4159 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4160 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4161 outbuf.data = 9;
4162
4163 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004164 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004165 if (b_space_wraps(&h2c->mbuf))
4166 goto realign_again;
4167 goto full;
4168 }
4169
4170 /* encode all headers, stop at empty name */
4171 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4172 /* these ones do not exist in H2 and must be dropped. */
4173 if (isteq(list[hdr].n, ist("connection")) ||
4174 isteq(list[hdr].n, ist("proxy-connection")) ||
4175 isteq(list[hdr].n, ist("keep-alive")) ||
4176 isteq(list[hdr].n, ist("upgrade")) ||
4177 isteq(list[hdr].n, ist("transfer-encoding")))
4178 continue;
4179
4180 if (isteq(list[hdr].n, ist("")))
4181 break; // end
4182
4183 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4184 /* output full */
4185 if (b_space_wraps(&h2c->mbuf))
4186 goto realign_again;
4187 goto full;
4188 }
4189 }
4190
4191 /* we may need to add END_STREAM.
4192 * FIXME: we should also set it when we know for sure that the
4193 * content-length is zero as well as on 204/304
4194 */
4195 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4196 es_now = 1;
4197
4198 if (h2s->cs->flags & CS_FL_SHW)
4199 es_now = 1;
4200
4201 /* update the frame's size */
4202 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4203
4204 if (es_now)
4205 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4206
4207 /* commit the H2 response */
4208 b_add(&h2c->mbuf, outbuf.data);
4209 h2s->flags |= H2_SF_HEADERS_SENT;
4210
Willy Tarreau115e83b2018-12-01 19:17:53 +01004211 if (es_now) {
4212 h2s->flags |= H2_SF_ES_SENT;
4213 if (h2s->st == H2_SS_OPEN)
4214 h2s->st = H2_SS_HLOC;
4215 else
4216 h2s_close(h2s);
4217 }
4218
4219 /* OK we could properly deliver the response */
4220
4221 /* remove all header blocks including the EOH and compute the
4222 * corresponding size.
4223 *
4224 * FIXME: We should remove everything when es_now is set.
4225 */
4226 ret = 0;
4227 idx = htx_get_head(htx);
4228 blk = htx_get_blk(htx, idx);
4229 while (blk != blk_end) {
4230 ret += htx_get_blksz(blk);
4231 blk = htx_remove_blk(htx, blk);
4232 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004233
4234 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4235 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004236 end:
4237 return ret;
4238 full:
4239 h2c->flags |= H2_CF_MUX_MFULL;
4240 h2s->flags |= H2_SF_BLK_MROOM;
4241 ret = 0;
4242 goto end;
4243 fail:
4244 /* unparsable HTX messages, too large ones to be produced in the local
4245 * list etc go here (unrecoverable errors).
4246 */
4247 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4248 ret = 0;
4249 goto end;
4250}
4251
Willy Tarreau80739692018-10-05 11:35:57 +02004252/* Try to send a HEADERS frame matching HTX request present in HTX message
4253 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4254 * must check the stream's status to detect any error which might have happened
4255 * subsequently to a successful send. The htx blocks are automatically removed
4256 * from the message. The htx message is assumed to be valid since produced from
4257 * the internal code, hence it contains a start line, an optional series of
4258 * header blocks and an end of header, otherwise an invalid frame could be
4259 * emitted and the resulting htx message could be left in an inconsistent state.
4260 */
4261static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4262{
4263 struct http_hdr list[MAX_HTTP_HDR];
4264 struct h2c *h2c = h2s->h2c;
4265 struct htx_blk *blk;
4266 struct htx_blk *blk_end;
4267 struct buffer outbuf;
4268 struct htx_sl *sl;
4269 struct ist meth, path;
4270 enum htx_blk_type type;
4271 int es_now = 0;
4272 int ret = 0;
4273 int hdr;
4274 int idx;
4275
4276 if (h2c_mux_busy(h2c, h2s)) {
4277 h2s->flags |= H2_SF_BLK_MBUSY;
4278 return 0;
4279 }
4280
4281 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4282 h2c->flags |= H2_CF_MUX_MALLOC;
4283 h2s->flags |= H2_SF_BLK_MROOM;
4284 return 0;
4285 }
4286
4287 /* determine the first block which must not be deleted, blk_end may
4288 * be NULL if all blocks have to be deleted.
4289 */
4290 idx = htx_get_head(htx);
4291 blk_end = NULL;
4292 while (idx != -1) {
4293 type = htx_get_blk_type(htx_get_blk(htx, idx));
4294 idx = htx_get_next(htx, idx);
4295 if (type == HTX_BLK_EOH) {
4296 if (idx != -1)
4297 blk_end = htx_get_blk(htx, idx);
4298 break;
4299 }
4300 }
4301
4302 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004303 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004304 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004305 meth = htx_sl_req_meth(sl);
4306 path = htx_sl_req_uri(sl);
4307
4308 /* and the rest of the headers, that we dump starting at header 0 */
4309 hdr = 0;
4310
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004311 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004312 while ((idx = htx_get_next(htx, idx)) != -1) {
4313 blk = htx_get_blk(htx, idx);
4314 type = htx_get_blk_type(blk);
4315
4316 if (type == HTX_BLK_UNUSED)
4317 continue;
4318
4319 if (type != HTX_BLK_HDR)
4320 break;
4321
4322 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4323 goto fail;
4324
4325 list[hdr].n = htx_get_blk_name(htx, blk);
4326 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004327 hdr++;
4328 }
4329
4330 /* marker for end of headers */
4331 list[hdr].n = ist("");
4332
4333 chunk_reset(&outbuf);
4334
4335 while (1) {
4336 outbuf.area = b_tail(&h2c->mbuf);
4337 outbuf.size = b_contig_space(&h2c->mbuf);
4338 outbuf.data = 0;
4339
4340 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4341 break;
4342 realign_again:
4343 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4344 }
4345
4346 if (outbuf.size < 9)
4347 goto full;
4348
4349 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4350 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4351 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4352 outbuf.data = 9;
4353
4354 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004355 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004356 if (b_space_wraps(&h2c->mbuf))
4357 goto realign_again;
4358 goto full;
4359 }
4360
4361 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004362 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4363 /* output full */
4364 if (b_space_wraps(&h2c->mbuf))
4365 goto realign_again;
4366 goto full;
4367 }
Willy Tarreau80739692018-10-05 11:35:57 +02004368
4369 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004370 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004371 /* output full */
4372 if (b_space_wraps(&h2c->mbuf))
4373 goto realign_again;
4374 goto full;
4375 }
4376
4377 /* encode all headers, stop at empty name */
4378 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4379 /* these ones do not exist in H2 and must be dropped. */
4380 if (isteq(list[hdr].n, ist("connection")) ||
4381 isteq(list[hdr].n, ist("proxy-connection")) ||
4382 isteq(list[hdr].n, ist("keep-alive")) ||
4383 isteq(list[hdr].n, ist("upgrade")) ||
4384 isteq(list[hdr].n, ist("transfer-encoding")))
4385 continue;
4386
4387 if (isteq(list[hdr].n, ist("")))
4388 break; // end
4389
4390 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4391 /* output full */
4392 if (b_space_wraps(&h2c->mbuf))
4393 goto realign_again;
4394 goto full;
4395 }
4396 }
4397
4398 /* we may need to add END_STREAM if we have no body :
4399 * - request already closed, or :
4400 * - no transfer-encoding, and :
4401 * - no content-length or content-length:0
4402 * Fixme: this doesn't take into account CONNECT requests.
4403 */
4404 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4405 es_now = 1;
4406
4407 if (sl->flags & HTX_SL_F_BODYLESS)
4408 es_now = 1;
4409
4410 if (h2s->cs->flags & CS_FL_SHW)
4411 es_now = 1;
4412
4413 /* update the frame's size */
4414 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4415
4416 if (es_now)
4417 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4418
4419 /* commit the H2 response */
4420 b_add(&h2c->mbuf, outbuf.data);
4421 h2s->flags |= H2_SF_HEADERS_SENT;
4422 h2s->st = H2_SS_OPEN;
4423
Willy Tarreau80739692018-10-05 11:35:57 +02004424 if (es_now) {
4425 // trim any possibly pending data (eg: inconsistent content-length)
4426 h2s->flags |= H2_SF_ES_SENT;
4427 h2s->st = H2_SS_HLOC;
4428 }
4429
4430 /* remove all header blocks including the EOH and compute the
4431 * corresponding size.
4432 *
4433 * FIXME: We should remove everything when es_now is set.
4434 */
4435 ret = 0;
4436 idx = htx_get_head(htx);
4437 blk = htx_get_blk(htx, idx);
4438 while (blk != blk_end) {
4439 ret += htx_get_blksz(blk);
4440 blk = htx_remove_blk(htx, blk);
4441 }
4442
4443 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4444 htx_remove_blk(htx, blk_end);
4445
4446 end:
4447 return ret;
4448 full:
4449 h2c->flags |= H2_CF_MUX_MFULL;
4450 h2s->flags |= H2_SF_BLK_MROOM;
4451 ret = 0;
4452 goto end;
4453 fail:
4454 /* unparsable HTX messages, too large ones to be produced in the local
4455 * list etc go here (unrecoverable errors).
4456 */
4457 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4458 ret = 0;
4459 goto end;
4460}
4461
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004462/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004463 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4464 * caller must check the stream's status to detect any error which might have
4465 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004466 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4467 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004468static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004469{
4470 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004471 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004472 struct buffer outbuf;
4473 size_t total = 0;
4474 int es_now = 0;
4475 int bsize; /* htx block size */
4476 int fsize; /* h2 frame size */
4477 struct htx_blk *blk;
4478 enum htx_blk_type type;
4479 int idx;
4480
4481 if (h2c_mux_busy(h2c, h2s)) {
4482 h2s->flags |= H2_SF_BLK_MBUSY;
4483 goto end;
4484 }
4485
4486 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4487 h2c->flags |= H2_CF_MUX_MALLOC;
4488 h2s->flags |= H2_SF_BLK_MROOM;
4489 goto end;
4490 }
4491
Willy Tarreau98de12a2018-12-12 07:03:00 +01004492 htx = htx_from_buf(buf);
4493
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004494 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4495 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4496 * the caller to handle.
4497 */
4498
4499 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004500 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004501 goto end;
4502
4503 idx = htx_get_head(htx);
4504 blk = htx_get_blk(htx, idx);
4505 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4506 bsize = htx_get_blksz(blk);
4507 fsize = bsize;
4508
4509 if (type == HTX_BLK_EOD) {
4510 /* if we have an EOD, we're dealing with chunked data. We may
4511 * have a set of trailers after us that the caller will want to
4512 * deal with. Let's simply remove the EOD and return.
4513 */
4514 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004515 total++; // EOD counts as one byte
4516 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004517 goto end;
4518 }
4519
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004520 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4521 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004522
4523 /* Perform some optimizations to reduce the number of buffer copies.
4524 * First, if the mux's buffer is empty and the htx area contains
4525 * exactly one data block of the same size as the requested count, and
4526 * this count fits within the frame size, the stream's window size, and
4527 * the connection's window size, then it's possible to simply swap the
4528 * caller's buffer with the mux's output buffer and adjust offsets and
4529 * length to match the entire DATA HTX block in the middle. In this
4530 * case we perform a true zero-copy operation from end-to-end. This is
4531 * the situation that happens all the time with large files. Second, if
4532 * this is not possible, but the mux's output buffer is empty, we still
4533 * have an opportunity to avoid the copy to the intermediary buffer, by
4534 * making the intermediary buffer's area point to the output buffer's
4535 * area. In this case we want to skip the HTX header to make sure that
4536 * copies remain aligned and that this operation remains possible all
4537 * the time. This goes for headers, data blocks and any data extracted
4538 * from the HTX blocks.
4539 */
4540 if (unlikely(fsize == count &&
4541 htx->used == 1 && type == HTX_BLK_DATA &&
4542 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4543 void *old_area = h2c->mbuf.area;
4544
4545 if (b_data(&h2c->mbuf)) {
4546 /* too bad there are data left there. If we have less
4547 * than 1/4 of the mbuf's size and everything fits,
4548 * we'll perform a copy anyway. Otherwise we'll pretend
4549 * the mbuf is full and wait.
4550 */
4551 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4552 goto copy;
4553 h2c->flags |= H2_CF_MUX_MFULL;
4554 h2s->flags |= H2_SF_BLK_MROOM;
4555 goto end;
4556 }
4557
4558 /* map an H2 frame to the HTX block so that we can put the
4559 * frame header there.
4560 */
4561 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004562 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004563 h2c->mbuf.data = fsize + 9;
4564 outbuf.area = b_head(&h2c->mbuf);
4565
4566 /* prepend an H2 DATA frame header just before the DATA block */
4567 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4568 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4569 h2_set_frame_size(outbuf.area, fsize);
4570
4571 /* update windows */
4572 h2s->mws -= fsize;
4573 h2c->mws -= fsize;
4574
4575 /* and exchange with our old area */
4576 buf->area = old_area;
4577 buf->data = buf->head = 0;
4578 total += fsize;
4579 goto end;
4580 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004581
Willy Tarreau98de12a2018-12-12 07:03:00 +01004582 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004583 /* for DATA and EOM we'll have to emit a frame, even if empty */
4584
4585 while (1) {
4586 outbuf.area = b_tail(&h2c->mbuf);
4587 outbuf.size = b_contig_space(&h2c->mbuf);
4588 outbuf.data = 0;
4589
4590 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4591 break;
4592 realign_again:
4593 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4594 }
4595
4596 if (outbuf.size < 9) {
4597 h2c->flags |= H2_CF_MUX_MFULL;
4598 h2s->flags |= H2_SF_BLK_MROOM;
4599 goto end;
4600 }
4601
4602 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4603 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4604 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4605 outbuf.data = 9;
4606
4607 /* we have in <fsize> the exact number of bytes we need to copy from
4608 * the HTX buffer. We need to check this against the connection's and
4609 * the stream's send windows, and to ensure that this fits in the max
4610 * frame size and in the buffer's available space minus 9 bytes (for
4611 * the frame header). The connection's flow control is applied last so
4612 * that we can use a separate list of streams which are immediately
4613 * unblocked on window opening. Note: we don't implement padding.
4614 */
4615
4616 /* EOM is presented with bsize==1 but would lead to the emission of an
4617 * empty frame, thus we force it to zero here.
4618 */
4619 if (type == HTX_BLK_EOM)
4620 bsize = fsize = 0;
4621
4622 if (!fsize)
4623 goto send_empty;
4624
4625 if (h2s->mws <= 0) {
4626 h2s->flags |= H2_SF_BLK_SFCTL;
4627 if (h2s->send_wait) {
4628 LIST_DEL(&h2s->list);
4629 LIST_INIT(&h2s->list);
4630 }
4631 goto end;
4632 }
4633
Willy Tarreauee573762018-12-04 15:25:57 +01004634 if (fsize > count)
4635 fsize = count;
4636
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004637 if (fsize > h2s->mws)
4638 fsize = h2s->mws; // >0
4639
4640 if (h2c->mfs && fsize > h2c->mfs)
4641 fsize = h2c->mfs; // >0
4642
4643 if (fsize + 9 > outbuf.size) {
4644 /* we have an opportunity for enlarging the too small
4645 * available space, let's try.
4646 * FIXME: is this really interesting to do? Maybe we'll
4647 * spend lots of time realigning instead of using two
4648 * frames.
4649 */
4650 if (b_space_wraps(&h2c->mbuf))
4651 goto realign_again;
4652 fsize = outbuf.size - 9;
4653
4654 if (fsize <= 0) {
4655 /* no need to send an empty frame here */
4656 h2c->flags |= H2_CF_MUX_MFULL;
4657 h2s->flags |= H2_SF_BLK_MROOM;
4658 goto end;
4659 }
4660 }
4661
4662 if (h2c->mws <= 0) {
4663 h2s->flags |= H2_SF_BLK_MFCTL;
4664 goto end;
4665 }
4666
4667 if (fsize > h2c->mws)
4668 fsize = h2c->mws;
4669
4670 /* now let's copy this this into the output buffer */
4671 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004672 h2s->mws -= fsize;
4673 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004674 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004675
4676 send_empty:
4677 /* update the frame's size */
4678 h2_set_frame_size(outbuf.area, fsize);
4679
4680 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4681 * meeting EOM. We should optimize this later.
4682 */
4683 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004684 total++; // EOM counts as one byte
4685 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004686 es_now = 1;
4687 }
4688
4689 if (es_now)
4690 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4691
4692 /* commit the H2 response */
4693 b_add(&h2c->mbuf, fsize + 9);
4694
4695 /* consume incoming HTX block, including EOM */
4696 total += fsize;
4697 if (fsize == bsize) {
4698 htx_remove_blk(htx, blk);
4699 if (fsize)
4700 goto new_frame;
4701 } else {
4702 /* we've truncated this block */
4703 htx_cut_data_blk(htx, blk, fsize);
4704 }
4705
4706 if (es_now) {
4707 if (h2s->st == H2_SS_OPEN)
4708 h2s->st = H2_SS_HLOC;
4709 else
4710 h2s_close(h2s);
4711
4712 h2s->flags |= H2_SF_ES_SENT;
4713 }
4714
4715 end:
4716 return total;
4717}
4718
Olivier Houchard6ff20392018-07-17 18:46:31 +02004719/* Called from the upper layer, to subscribe to events, such as being able to send */
4720static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4721{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004722 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004723 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004724 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004725
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004726 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004727 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004728 if (!(sw->events & SUB_RETRY_RECV)) {
4729 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004730 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004731 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004732 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004733 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004734 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004735 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004736 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004737 if (!(sw->events & SUB_RETRY_SEND)) {
4738 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004739 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004740 h2s->send_wait = sw;
4741 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4742 if (h2s->flags & H2_SF_BLK_MFCTL)
4743 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4744 else
4745 LIST_ADDQ(&h2c->send_list, &h2s->list);
4746 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004747 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004748 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004749 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004750 if (event_type != 0)
4751 return -1;
4752 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004753
4754
4755}
4756
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004757static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4758{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004759 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004760 struct h2s *h2s = cs->ctx;
4761
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004762 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004763 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004764 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004765 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004766 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004767 }
4768 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004769 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004770 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004771 if (h2s->send_wait == sw) {
4772 LIST_DEL(&h2s->list);
4773 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004774 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004775 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004776 }
4777 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004778 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4779 sw = param;
4780 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004781 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004782 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01004783 LIST_DEL(&h2s->list);
4784 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004785 }
4786 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004787 return 0;
4788}
4789
4790
Olivier Houchard511efea2018-08-16 15:30:32 +02004791/* Called from the upper layer, to receive data */
4792static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4793{
Olivier Houchard638b7992018-08-16 15:41:52 +02004794 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004795 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004796 struct htx *h2s_htx = NULL;
4797 struct htx *buf_htx = NULL;
4798 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004799 size_t ret = 0;
4800
4801 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004802 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4803 /* in HTX mode we ignore the count argument */
4804 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004805 if (htx_is_empty(h2s_htx)) {
4806 if (cs->flags & CS_FL_REOS)
4807 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004808 if (cs->flags & CS_FL_ERR_PENDING)
4809 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004810 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004811 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004812
4813 buf_htx = htx_from_buf(buf);
4814 count = htx_free_space(buf_htx);
4815
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004816 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004817
4818 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004819 htx_to_buf(buf_htx, buf);
4820 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004821 ret = htx_ret.ret;
4822 }
4823 else {
4824 ret = b_xfer(buf, &h2s->rxbuf, count);
4825 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004826
Olivier Houchard638b7992018-08-16 15:41:52 +02004827 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004828 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004829 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004830 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004831 if (cs->flags & CS_FL_REOS)
4832 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004833 if (cs->flags & CS_FL_ERR_PENDING)
4834 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004835 if (b_size(&h2s->rxbuf)) {
4836 b_free(&h2s->rxbuf);
4837 offer_buffers(NULL, tasks_run_queue);
4838 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004839 }
4840
Willy Tarreau082f5592018-11-25 08:03:32 +01004841 if (ret && h2c->dsi == h2s->id) {
4842 /* demux is blocking on this stream's buffer */
4843 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01004844 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01004845 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004846end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004847 return ret;
4848}
4849
Olivier Houchardd846c262018-10-19 17:24:29 +02004850static void h2_stop_senders(struct h2c *h2c)
4851{
4852 struct h2s *h2s, *h2s_back;
4853
4854 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4855 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4856 if (h2c->msi == h2s_id(h2s))
4857 continue;
4858 LIST_DEL(&h2s->list);
4859 LIST_INIT(&h2s->list);
4860 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004861 h2s->send_wait->events |= SUB_RETRY_SEND;
4862 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004863 LIST_ADD(&h2c->send_list, &h2s->list);
4864 }
4865}
4866
Willy Tarreau62f52692017-10-08 23:01:42 +02004867/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004868static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004869{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004870 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004871 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004872 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004873 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004874 struct htx *htx;
4875 struct htx_blk *blk;
4876 enum htx_blk_type btype;
4877 uint32_t bsize;
4878 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004879
Olivier Houchardd846c262018-10-19 17:24:29 +02004880 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004881 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02004882 h2s->send_wait = NULL;
4883 LIST_DEL(&h2s->list);
4884 LIST_INIT(&h2s->list);
4885 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004886 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4887 return 0;
4888
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004889 /* htx will be enough to decide if we're using HTX or legacy */
4890 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4891
Willy Tarreau0bad0432018-06-14 16:54:01 +02004892 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004893 h2s->flags |= H2_SF_OUTGOING_DATA;
4894
Willy Tarreau751f2d02018-10-05 09:35:00 +02004895 if (h2s->id == 0) {
4896 int32_t id = h2c_get_next_sid(h2s->h2c);
4897
4898 if (id < 0) {
4899 cs->ctx = NULL;
4900 cs->flags |= CS_FL_ERROR;
4901 h2s_destroy(h2s);
4902 return 0;
4903 }
4904
4905 eb32_delete(&h2s->by_id);
4906 h2s->by_id.key = h2s->id = id;
4907 h2s->h2c->max_id = id;
4908 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4909 }
4910
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004911 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004912 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4913 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004914 idx = htx_get_head(htx);
4915 blk = htx_get_blk(htx, idx);
4916 btype = htx_get_blk_type(blk);
4917 bsize = htx_get_blksz(blk);
4918
4919 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004920 case HTX_BLK_REQ_SL:
4921 /* start-line before headers */
4922 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4923 if (ret > 0) {
4924 total += ret;
4925 count -= ret;
4926 if (ret < bsize)
4927 goto done;
4928 }
4929 break;
4930
Willy Tarreau115e83b2018-12-01 19:17:53 +01004931 case HTX_BLK_RES_SL:
4932 /* start-line before headers */
4933 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4934 if (ret > 0) {
4935 total += ret;
4936 count -= ret;
4937 if (ret < bsize)
4938 goto done;
4939 }
4940 break;
4941
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004942 case HTX_BLK_DATA:
4943 case HTX_BLK_EOD:
4944 case HTX_BLK_EOM:
4945 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004946 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004947 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004948 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004949 total += ret;
4950 count -= ret;
4951 if (ret < bsize)
4952 goto done;
4953 }
4954 break;
4955
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004956 default:
4957 htx_remove_blk(htx, blk);
4958 total += bsize;
4959 count -= bsize;
4960 break;
4961 }
4962 }
4963 goto done;
4964 }
4965
4966 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004967 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004968 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004969 if (h2s->h2c->flags & H2_CF_IS_BACK)
4970 ret = -1;
4971 else
4972 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004973 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004974 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004975 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004976 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004977 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004978 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004979 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004980
Willy Tarreau5dd17352018-06-14 13:33:30 +02004981 if (unlikely((int)ret <= 0)) {
4982 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004983 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4984 break;
4985 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004986 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004987 total += count;
4988 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004989 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004990 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004991 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004992 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01004993 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004994 break;
4995 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004996
4997 total += ret;
4998 count -= ret;
4999
5000 if (h2s->st >= H2_SS_ERROR)
5001 break;
5002
5003 if (h2s->flags & H2_SF_BLK_ANY)
5004 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005005 }
5006
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005007 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005008 if (h2s->st >= H2_SS_ERROR) {
5009 /* trim any possibly pending data after we close (extra CR-LF,
5010 * unprocessed trailers, abnormal extra data, ...)
5011 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005012 total += count;
5013 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005014 }
5015
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005016 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005017 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005018 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005019 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005020 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005021 }
5022
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005023 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005024 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005025 } else {
5026 b_del(buf, total);
5027 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005028
5029 /* The mux is full, cancel the pending tasks */
5030 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5031 (h2s->flags & H2_SF_BLK_MBUSY))
5032 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005033
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005034 /* If we're running HTX, and we read the whole buffer, then pretend
5035 * we read exactly what the caller specified, as with HTX the caller
5036 * will always give the buffer size, instead of the amount of data
5037 * available.
5038 */
5039 if (htx && !b_data(buf))
5040 total = orig_count;
5041
Olivier Houchard7505f942018-08-21 18:10:44 +02005042 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005043 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005044 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005045
Olivier Houchard7505f942018-08-21 18:10:44 +02005046 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005047 /* If we're waiting for flow control, and we got a shutr on the
5048 * connection, we will never be unlocked, so add an error on
5049 * the conn_stream.
5050 */
5051 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5052 !b_data(&h2s->h2c->dbuf) &&
5053 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5054 if (cs->flags & CS_FL_EOS)
5055 cs->flags |= CS_FL_ERROR;
5056 else
5057 cs->flags |= CS_FL_ERR_PENDING;
5058 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005059 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005060}
5061
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005062/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005063static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005064{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005065 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005066 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005067 struct eb32_node *node;
5068 int fctl_cnt = 0;
5069 int send_cnt = 0;
5070 int tree_cnt = 0;
5071 int orph_cnt = 0;
5072
5073 if (!h2c)
5074 return;
5075
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005076 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005077 fctl_cnt++;
5078
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005079 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005080 send_cnt++;
5081
Willy Tarreau3af37712018-12-18 14:34:41 +01005082 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005083 node = eb32_first(&h2c->streams_by_id);
5084 while (node) {
5085 h2s = container_of(node, struct h2s, by_id);
5086 tree_cnt++;
5087 if (!h2s->cs)
5088 orph_cnt++;
5089 node = eb32_next(node);
5090 }
5091
Willy Tarreau987c0632018-12-18 10:32:05 +01005092 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5093 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5094 " .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 +02005095 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5096 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005097 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005098 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5099 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5100 h2c->msi,
5101 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5102 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5103
5104 if (h2s) {
5105 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5106 h2s, h2s->id, h2s->flags,
5107 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5108 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5109 h2s->cs);
5110 if (h2s->cs)
5111 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5112 h2s->cs->flags, h2s->cs->data);
5113 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005114}
Willy Tarreau62f52692017-10-08 23:01:42 +02005115
5116/*******************************************************/
5117/* functions below are dedicated to the config parsers */
5118/*******************************************************/
5119
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005120/* config parser for global "tune.h2.header-table-size" */
5121static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5122 struct proxy *defpx, const char *file, int line,
5123 char **err)
5124{
5125 if (too_many_args(1, args, err, NULL))
5126 return -1;
5127
5128 h2_settings_header_table_size = atoi(args[1]);
5129 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5130 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5131 return -1;
5132 }
5133 return 0;
5134}
Willy Tarreau62f52692017-10-08 23:01:42 +02005135
Willy Tarreaue6baec02017-07-27 11:45:11 +02005136/* config parser for global "tune.h2.initial-window-size" */
5137static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5138 struct proxy *defpx, const char *file, int line,
5139 char **err)
5140{
5141 if (too_many_args(1, args, err, NULL))
5142 return -1;
5143
5144 h2_settings_initial_window_size = atoi(args[1]);
5145 if (h2_settings_initial_window_size < 0) {
5146 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5147 return -1;
5148 }
5149 return 0;
5150}
5151
Willy Tarreau5242ef82017-07-27 11:47:28 +02005152/* config parser for global "tune.h2.max-concurrent-streams" */
5153static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5154 struct proxy *defpx, const char *file, int line,
5155 char **err)
5156{
5157 if (too_many_args(1, args, err, NULL))
5158 return -1;
5159
5160 h2_settings_max_concurrent_streams = atoi(args[1]);
5161 if (h2_settings_max_concurrent_streams < 0) {
5162 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5163 return -1;
5164 }
5165 return 0;
5166}
5167
Willy Tarreau62f52692017-10-08 23:01:42 +02005168
5169/****************************************/
5170/* MUX initialization and instanciation */
5171/***************************************/
5172
5173/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005174static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005175 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005176 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005177 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005178 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005179 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005180 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005181 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005182 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005183 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005184 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005185 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01005186 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005187 .shutr = h2_shutr,
5188 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005189 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005190 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005191 .name = "H2",
5192};
5193
Christopher Faulet32f61c02018-04-10 14:33:41 +02005194/* PROTO selection : this mux registers PROTO token "h2" */
5195static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005196 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005197
Willy Tarreau0108d902018-11-25 19:14:37 +01005198INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5199
Willy Tarreauf8957272018-10-03 10:25:20 +02005200static struct mux_proto_list mux_proto_h2_htx =
5201 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5202
5203INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5204
Willy Tarreau62f52692017-10-08 23:01:42 +02005205/* config keyword parsers */
5206static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005207 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005208 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005209 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02005210 { 0, NULL, NULL }
5211}};
5212
Willy Tarreau0108d902018-11-25 19:14:37 +01005213INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);