blob: 14efcb8290b3254d6bfd827470188b031c969827 [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 Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020020#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010023#include <proto/http_htx.h>
24#include <proto/htx.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010026#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020027#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020028
29
Willy Tarreau2a856182017-05-16 15:20:39 +020030/* dummy streams returned for idle and closed states */
31static const struct h2s *h2_closed_stream;
32static const struct h2s *h2_idle_stream;
33
Willy Tarreau5ab6b572017-09-22 08:05:00 +020034/* Connection flags (32 bit), in h2c->flags */
35#define H2_CF_NONE 0x00000000
36
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020037/* Flags indicating why writing to the mux is blocked. */
38#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
39#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
40#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
41
Willy Tarreau315d8072017-12-10 22:17:57 +010042/* Flags indicating why writing to the demux is blocked.
43 * The first two ones directly affect the ability for the mux to receive data
44 * from the connection. The other ones affect the mux's ability to demux
45 * received data.
46 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020047#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
48#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010049
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
51#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
52#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
53#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020054#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
55#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020056
Willy Tarreau081d4722017-05-16 21:51:05 +020057/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
59#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
60#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 +020061#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020062
Willy Tarreau5ab6b572017-09-22 08:05:00 +020063/* H2 connection state, in h2c->st0 */
64enum h2_cs {
65 H2_CS_PREFACE, // init done, waiting for connection preface
66 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
67 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
68 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010069 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
70 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020071 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020091 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020092
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010097 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
98 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020099 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
100
101 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103 int32_t msi; /* mux stream ID (<0 = idle) */
104 int32_t mfl; /* mux frame length (if dsi >= 0) */
105 int8_t mft; /* mux frame type (if dsi >= 0) */
106 int8_t mff; /* mux frame flags (if dsi >= 0) */
107 /* 16 bit hole here */
108 int32_t miw; /* mux initial window size for all new streams */
109 int32_t mws; /* mux window size. Can be negative. */
110 int32_t mfs; /* mux's max frame size */
111
Willy Tarreauea392822017-10-31 10:02:25 +0100112 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100113 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100114 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200115 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200116 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200121 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200123 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200177 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200178 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200184 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200185 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200186 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
188 struct wait_event *send_wait; /* The streeam is waiting for flow control */
189 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200190};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200191
Willy Tarreauc6405142017-09-21 20:23:50 +0200192/* descriptor for an h2 frame header */
193struct h2_fh {
194 uint32_t len; /* length, host order, 24 bits */
195 uint32_t sid; /* stream id, host order, 31 bits */
196 uint8_t ft; /* frame type */
197 uint8_t ff; /* frame flags */
198};
199
Willy Tarreau8ceae722018-11-26 11:58:30 +0100200/* the h2c connection pool */
201DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
202
203/* the h2s stream pool */
204DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
205
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200206/* a few settings from the global section */
207static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200208static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200209static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200210
Willy Tarreau2a856182017-05-16 15:20:39 +0200211/* a dmumy closed stream */
212static const struct h2s *h2_closed_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100217 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
221/* and a dummy idle stream for use with any unannounced stream */
222static const struct h2s *h2_idle_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200227 .id = 0,
228};
229
Olivier Houchard9f6af332018-05-25 14:04:04 +0200230static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200231static int h2_send(struct h2c *h2c);
232static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200233static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200234static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100235static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100236static int h2_frt_decode_headers(struct h2s *h2s);
237static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200238static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200239static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200240
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241/*****************************************************/
242/* functions below are for dynamic buffer management */
243/*****************************************************/
244
Willy Tarreau315d8072017-12-10 22:17:57 +0100245/* indicates whether or not the we may call the h2_recv() function to attempt
246 * to receive data into the buffer and/or demux pending data. The condition is
247 * a bit complex due to some API limits for now. The rules are the following :
248 * - if an error or a shutdown was detected on the connection and the buffer
249 * is empty, we must not attempt to receive
250 * - if the demux buf failed to be allocated, we must not try to receive and
251 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100252 * - if no flag indicates a blocking condition, we may attempt to receive,
253 * regardless of whether the demux buffer is full or not, so that only
254 * de demux part decides whether or not to block. This is needed because
255 * the connection API indeed prevents us from re-enabling receipt that is
256 * already enabled in a polled state, so we must always immediately stop
257 * as soon as the demux can't proceed so as never to hit an end of read
258 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100259 * - otherwise must may not attempt
260 */
261static inline int h2_recv_allowed(const struct h2c *h2c)
262{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200263 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100264 (h2c->st0 >= H2_CS_ERROR ||
265 h2c->conn->flags & CO_FL_ERROR ||
266 conn_xprt_read0_pending(h2c->conn)))
267 return 0;
268
269 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100270 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 return 1;
272
273 return 0;
274}
275
Willy Tarreauf2101912018-07-19 10:11:38 +0200276/* returns true if the connection has too many conn_streams attached */
277static inline int h2_has_too_many_cs(const struct h2c *h2c)
278{
279 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
280}
281
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
283 * flags are used to figure what buffer was requested. It returns 1 if the
284 * allocation succeeds, in which case the connection is woken up, or 0 if it's
285 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200286 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100287static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200288{
289 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100290 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200294 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200295 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296 return 1;
297 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
300 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301
302 if (h2c->flags & H2_CF_DEM_MROOM) {
303 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200304 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200305 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200306 }
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 1;
308 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100309
310 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
311 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200312 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200314 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200315 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100316 return 1;
317 }
318
Willy Tarreau14398122017-09-22 14:26:04 +0200319 return 0;
320}
321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200323{
324 struct buffer *buf = NULL;
325
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
327 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
328 h2c->buf_wait.target = h2c;
329 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100331 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200333 __conn_xprt_stop_recv(h2c->conn);
334 }
335 return buf;
336}
337
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200338static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200339{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200342 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200343 }
344}
345
Olivier Houchardd540b362018-11-05 18:37:53 +0100346static int h2_avail_streams(struct connection *conn)
347{
348 struct h2c *h2c = conn->mux_ctx;
349
350 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
351}
352
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200353
Willy Tarreau62f52692017-10-08 23:01:42 +0200354/*****************************************************************/
355/* functions below are dedicated to the mux setup and management */
356/*****************************************************************/
357
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200358/* Initialize the mux once it's attached. For outgoing connections, the context
359 * is already initialized before installing the mux, so we detect incoming
360 * connections from the fact that the context is still NULL. Returns < 0 on
361 * error.
362 */
363static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364{
365 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100366 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200367
Willy Tarreaubafbe012017-11-24 17:34:44 +0100368 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200369 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200370 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371
Willy Tarreau01b44822018-10-03 14:26:37 +0200372 if (conn->mux_ctx) {
373 h2c->flags = H2_CF_IS_BACK;
374 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
375 if (tick_isset(prx->timeout.serverfin))
376 h2c->shut_timeout = prx->timeout.serverfin;
377 } else {
378 h2c->flags = H2_CF_NONE;
379 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
380 if (tick_isset(prx->timeout.clientfin))
381 h2c->shut_timeout = prx->timeout.clientfin;
382 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100383
Willy Tarreau0b37d652018-10-03 10:33:02 +0200384 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100385 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100386 if (tick_isset(h2c->timeout)) {
387 t = task_new(tid_bit);
388 if (!t)
389 goto fail;
390
391 h2c->task = t;
392 t->process = h2_timeout_task;
393 t->context = h2c;
394 t->expire = tick_add(now_ms, h2c->timeout);
395 }
Willy Tarreauea392822017-10-31 10:02:25 +0100396
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200397 h2c->wait_event.task = tasklet_new();
398 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200399 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200400 h2c->wait_event.task->process = h2_io_cb;
401 h2c->wait_event.task->context = h2c;
402 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200403
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
405 if (!h2c->ddht)
406 goto fail;
407
408 /* Initialise the context. */
409 h2c->st0 = H2_CS_PREFACE;
410 h2c->conn = conn;
411 h2c->max_id = -1;
412 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413 h2c->rcvd_c = 0;
414 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100415 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200416 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200418 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200419 h2c->dsi = -1;
420 h2c->msi = -1;
421 h2c->last_sid = -1;
422
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200423 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424 h2c->miw = 65535; /* mux initial window size */
425 h2c->mws = 65535; /* mux window size */
426 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200427 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200428 LIST_INIT(&h2c->send_list);
429 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200430 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100431 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432
Willy Tarreau3f133572017-10-31 19:21:06 +0100433 if (t)
434 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100435
Willy Tarreau01b44822018-10-03 14:26:37 +0200436 if (h2c->flags & H2_CF_IS_BACK) {
437 /* FIXME: this is temporary, for outgoing connections we need
438 * to immediately allocate a stream until the code is modified
439 * so that the caller calls ->attach(). For now the outgoing cs
440 * is stored as conn->mux_ctx by the caller.
441 */
442 struct h2s *h2s;
443
444 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
445 if (!h2s)
446 goto fail_stream;
447 }
448
449 conn->mux_ctx = h2c;
450
Willy Tarreau0f383582018-10-03 14:22:21 +0200451 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200452 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200453 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200454 fail_stream:
455 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200456 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100457 if (t)
458 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200459 if (h2c->wait_event.task)
460 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100461 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200462 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200463 return -1;
464}
465
Willy Tarreau751f2d02018-10-05 09:35:00 +0200466/* returns the next allocatable outgoing stream ID for the H2 connection, or
467 * -1 if no more is allocatable.
468 */
469static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
470{
471 int32_t id = (h2c->max_id + 1) | 1;
472 if (id & 0x80000000U)
473 id = -1;
474 return id;
475}
476
Willy Tarreau2373acc2017-10-12 17:35:14 +0200477/* returns the stream associated with id <id> or NULL if not found */
478static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
479{
480 struct eb32_node *node;
481
Willy Tarreau751f2d02018-10-05 09:35:00 +0200482 if (id == 0)
483 return (struct h2s *)h2_closed_stream;
484
Willy Tarreau2a856182017-05-16 15:20:39 +0200485 if (id > h2c->max_id)
486 return (struct h2s *)h2_idle_stream;
487
Willy Tarreau2373acc2017-10-12 17:35:14 +0200488 node = eb32_lookup(&h2c->streams_by_id, id);
489 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200490 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200491
492 return container_of(node, struct h2s, by_id);
493}
494
Willy Tarreau62f52692017-10-08 23:01:42 +0200495/* release function for a connection. This one should be called to free all
496 * resources allocated to the mux.
497 */
498static void h2_release(struct connection *conn)
499{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200500 struct h2c *h2c = conn->mux_ctx;
501
502 LIST_DEL(&conn->list);
503
504 if (h2c) {
505 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200506
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100507 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100508 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100509 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200510
Willy Tarreau44e973f2018-03-01 17:49:30 +0100511 h2_release_buf(h2c, &h2c->dbuf);
512 h2_release_buf(h2c, &h2c->mbuf);
513
Willy Tarreauea392822017-10-31 10:02:25 +0100514 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200515 h2c->task->context = NULL;
516 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100517 h2c->task = NULL;
518 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200519 if (h2c->wait_event.task)
520 tasklet_free(h2c->wait_event.task);
521 if (h2c->wait_event.wait_reason != 0)
522 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
523 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100524
Willy Tarreaubafbe012017-11-24 17:34:44 +0100525 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 }
527
528 conn->mux = NULL;
529 conn->mux_ctx = NULL;
530
531 conn_stop_tracking(conn);
532 conn_full_close(conn);
533 if (conn->destroy_cb)
534 conn->destroy_cb(conn);
535 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200536}
537
538
Willy Tarreau71681172017-10-23 14:39:06 +0200539/******************************************************/
540/* functions below are for the H2 protocol processing */
541/******************************************************/
542
543/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100544static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200545{
546 return h2s ? h2s->id : 0;
547}
548
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200549/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100550static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200551{
552 if (h2c->msi < 0)
553 return 0;
554
555 if (h2c->msi == h2s_id(h2s))
556 return 0;
557
558 return 1;
559}
560
Willy Tarreau741d6df2017-10-17 08:00:59 +0200561/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100562static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200563{
564 h2c->errcode = err;
565 h2c->st0 = H2_CS_ERROR;
566}
567
Willy Tarreau2e43f082017-10-17 08:03:59 +0200568/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200570{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200571 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200572 h2s->errcode = err;
573 h2s->st = H2_SS_ERROR;
574 if (h2s->cs)
575 h2s->cs->flags |= CS_FL_ERROR;
576 }
577}
578
Willy Tarreaue4820742017-07-27 13:37:23 +0200579/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200581{
582 uint8_t *out = frame;
583
584 *out = len >> 16;
585 write_n16(out + 1, len);
586}
587
Willy Tarreau54c15062017-10-10 17:10:03 +0200588/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
589 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
590 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200591 * available in the buffer's input prior to calling this function. The buffer
592 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200593 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100594static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200595 const struct buffer *b, int o)
596{
Willy Tarreau591d4452018-06-15 17:21:00 +0200597 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 +0200598}
599
Willy Tarreau1f094672017-11-20 21:27:45 +0100600static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200601{
Willy Tarreau591d4452018-06-15 17:21:00 +0200602 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200603}
604
Willy Tarreau1f094672017-11-20 21:27:45 +0100605static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200606{
Willy Tarreau591d4452018-06-15 17:21:00 +0200607 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200608}
609
Willy Tarreau1f094672017-11-20 21:27:45 +0100610static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200611{
Willy Tarreau591d4452018-06-15 17:21:00 +0200612 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200613}
614
615
Willy Tarreau715d5312017-07-11 15:20:24 +0200616/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
617 * is not obvious. It turns out that H2 headers are neither aligned nor do they
618 * use regular sizes. And to add to the trouble, the buffer may wrap so each
619 * byte read must be checked. The header is formed like this :
620 *
621 * b0 b1 b2 b3 b4 b5..b8
622 * +----------+---------+--------+----+----+----------------------+
623 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
624 * +----------+---------+--------+----+----+----------------------+
625 *
626 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
627 * we get the sid properly aligned and ordered, and 16 bits of len properly
628 * ordered as well. The type and flags can be extracted using bit shifts from
629 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200630 * Returns zero if some bytes are missing, otherwise non-zero on success. The
631 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200632 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100633static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200634{
635 uint64_t w;
636
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200637 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200638 return 0;
639
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200640 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200641 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200642 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
643 h->ff = w >> 32;
644 h->ft = w >> 40;
645 h->len += w >> 48;
646 return 1;
647}
648
649/* skip the next 9 bytes corresponding to the frame header possibly parsed by
650 * h2_peek_frame_hdr() above.
651 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100652static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200653{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200654 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200655}
656
657/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100658static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200659{
660 int ret;
661
662 ret = h2_peek_frame_hdr(b, h);
663 if (ret > 0)
664 h2_skip_frame_hdr(b);
665 return ret;
666}
667
Willy Tarreau00dd0782018-03-01 16:31:34 +0100668/* marks stream <h2s> as CLOSED and decrement the number of active streams for
669 * its connection if the stream was not yet closed. Please use this exclusively
670 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100671 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100672static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100673{
674 if (h2s->st != H2_SS_CLOSED)
675 h2s->h2c->nb_streams--;
676 h2s->st = H2_SS_CLOSED;
677}
678
Willy Tarreau71049cc2018-03-28 13:56:39 +0200679/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
680static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100681{
682 h2s_close(h2s);
683 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200684 if (b_size(&h2s->rxbuf)) {
685 b_free(&h2s->rxbuf);
686 offer_buffers(NULL, tasks_run_queue);
687 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200688 if (h2s->send_wait != NULL)
689 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
690 if (h2s->recv_wait != NULL)
691 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
692 /* There's no need to explicitely call unsubscribe here, the only
693 * reference left would be in the h2c send_list/fctl_list, and if
694 * we're in it, we're getting out anyway
695 */
696 LIST_DEL(&h2s->list);
697 LIST_INIT(&h2s->list);
698 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100699 pool_free(pool_head_h2s, h2s);
700}
701
Willy Tarreaua8e49542018-10-03 18:53:55 +0200702/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
703 * stream tree. In case of error, nothing is added and NULL is returned. The
704 * causes of errors can be any failed memory allocation. The caller is
705 * responsible for checking if the connection may support an extra stream
706 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200707 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200708static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200709{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200710 struct h2s *h2s;
711
Willy Tarreaubafbe012017-11-24 17:34:44 +0100712 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200713 if (!h2s)
714 goto out;
715
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200716 h2s->wait_event.task = tasklet_new();
717 if (!h2s->wait_event.task) {
718 pool_free(pool_head_h2s, h2s);
719 goto out;
720 }
721 h2s->send_wait = NULL;
722 h2s->recv_wait = NULL;
723 h2s->wait_event.task->process = h2_deferred_shut;
724 h2s->wait_event.task->context = h2s;
725 h2s->wait_event.handle = NULL;
726 h2s->wait_event.wait_reason = 0;
727 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200728 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200729 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730 h2s->mws = h2c->miw;
731 h2s->flags = H2_SF_NONE;
732 h2s->errcode = H2_ERR_NO_ERROR;
733 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200734 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200735 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200736
737 if (h2c->flags & H2_CF_IS_BACK) {
738 h1m_init_req(&h2s->h1m);
739 h2s->h1m.err_pos = -1; // don't care about errors on the request path
740 h2s->h1m.flags |= H1_MF_TOLOWER;
741 } else {
742 h1m_init_res(&h2s->h1m);
743 h2s->h1m.err_pos = -1; // don't care about errors on the response path
744 h2s->h1m.flags |= H1_MF_TOLOWER;
745 }
746
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200747 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200748 if (id > 0)
749 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200750
751 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100752 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200753
754 return h2s;
755
756 out_free_h2s:
757 pool_free(pool_head_h2s, h2s);
758 out:
759 return NULL;
760}
761
762/* creates a new stream <id> on the h2c connection and returns it, or NULL in
763 * case of memory allocation error.
764 */
765static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
766{
767 struct session *sess = h2c->conn->owner;
768 struct conn_stream *cs;
769 struct h2s *h2s;
770
771 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
772 goto out;
773
774 h2s = h2s_new(h2c, id);
775 if (!h2s)
776 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200777
778 cs = cs_new(h2c->conn);
779 if (!cs)
780 goto out_close;
781
782 h2s->cs = cs;
783 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200784 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200785
786 if (stream_create_from_cs(cs) < 0)
787 goto out_free_cs;
788
Willy Tarreau590a0512018-09-05 11:56:48 +0200789 /* We want the accept date presented to the next stream to be the one
790 * we have now, the handshake time to be null (since the next stream
791 * is not delayed by a handshake), and the idle time to count since
792 * right now.
793 */
794 sess->accept_date = date;
795 sess->tv_accept = now;
796 sess->t_handshake = 0;
797
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200798 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200799 if (h2_has_too_many_cs(h2c))
800 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200801 return h2s;
802
803 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200804 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 cs_free(cs);
806 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200807 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200809 sess_log(sess);
810 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200811}
812
Willy Tarreau751f2d02018-10-05 09:35:00 +0200813/* allocates a new stream associated to conn_stream <cs> on the h2c connection
814 * and returns it, or NULL in case of memory allocation error or if the highest
815 * possible stream ID was reached.
816 */
817static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
818{
819 struct h2s *h2s = NULL;
820
821 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
822 goto out;
823
824 /* Defer choosing the ID until we send the first message to create the stream */
825 h2s = h2s_new(h2c, 0);
826 if (!h2s)
827 goto out;
828
829 h2s->cs = cs;
830 cs->ctx = h2s;
831 h2c->nb_cs++;
832
833 /* OK done, the stream lives its own life now */
834 if (h2_has_too_many_cs(h2c))
835 h2c->flags |= H2_CF_DEM_TOOMANY;
836 out:
837 return h2s;
838}
839
Willy Tarreaube5b7152017-09-25 16:25:39 +0200840/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
841 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
842 * the various settings codes.
843 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200844static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200845{
846 struct buffer *res;
847 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200848 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200849 int ret;
850
851 if (h2c_mux_busy(h2c, NULL)) {
852 h2c->flags |= H2_CF_DEM_MBUSY;
853 return 0;
854 }
855
Willy Tarreau44e973f2018-03-01 17:49:30 +0100856 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200857 if (!res) {
858 h2c->flags |= H2_CF_MUX_MALLOC;
859 h2c->flags |= H2_CF_DEM_MROOM;
860 return 0;
861 }
862
863 chunk_init(&buf, buf_data, sizeof(buf_data));
864 chunk_memcpy(&buf,
865 "\x00\x00\x00" /* length : 0 for now */
866 "\x04\x00" /* type : 4 (settings), flags : 0 */
867 "\x00\x00\x00\x00", /* stream ID : 0 */
868 9);
869
870 if (h2_settings_header_table_size != 4096) {
871 char str[6] = "\x00\x01"; /* header_table_size */
872
873 write_n32(str + 2, h2_settings_header_table_size);
874 chunk_memcat(&buf, str, 6);
875 }
876
877 if (h2_settings_initial_window_size != 65535) {
878 char str[6] = "\x00\x04"; /* initial_window_size */
879
880 write_n32(str + 2, h2_settings_initial_window_size);
881 chunk_memcat(&buf, str, 6);
882 }
883
884 if (h2_settings_max_concurrent_streams != 0) {
885 char str[6] = "\x00\x03"; /* max_concurrent_streams */
886
887 /* Note: 0 means "unlimited" for haproxy's config but not for
888 * the protocol, so never send this value!
889 */
890 write_n32(str + 2, h2_settings_max_concurrent_streams);
891 chunk_memcat(&buf, str, 6);
892 }
893
894 if (global.tune.bufsize != 16384) {
895 char str[6] = "\x00\x05"; /* max_frame_size */
896
897 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
898 * match bufsize - rewrite size, but at the moment it seems
899 * that clients don't take care of it.
900 */
901 write_n32(str + 2, global.tune.bufsize);
902 chunk_memcat(&buf, str, 6);
903 }
904
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200905 h2_set_frame_size(buf.area, buf.data - 9);
906 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200907 if (unlikely(ret <= 0)) {
908 if (!ret) {
909 h2c->flags |= H2_CF_MUX_MFULL;
910 h2c->flags |= H2_CF_DEM_MROOM;
911 return 0;
912 }
913 else {
914 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
915 return 0;
916 }
917 }
918 return ret;
919}
920
Willy Tarreau52eed752017-09-22 15:05:09 +0200921/* Try to receive a connection preface, then upon success try to send our
922 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
923 * missing data. It may return an error in h2c.
924 */
925static int h2c_frt_recv_preface(struct h2c *h2c)
926{
927 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200928 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200929
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200930 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200931
932 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200933 if (ret1 < 0)
934 sess_log(h2c->conn->owner);
935
Willy Tarreau52eed752017-09-22 15:05:09 +0200936 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
937 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
938 return 0;
939 }
940
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200941 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200942 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200943 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200944
Willy Tarreaube5b7152017-09-25 16:25:39 +0200945 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200946}
947
Willy Tarreau01b44822018-10-03 14:26:37 +0200948/* Try to send a connection preface, then upon success try to send our
949 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
950 * missing data. It may return an error in h2c.
951 */
952static int h2c_bck_send_preface(struct h2c *h2c)
953{
954 struct buffer *res;
955
956 if (h2c_mux_busy(h2c, NULL)) {
957 h2c->flags |= H2_CF_DEM_MBUSY;
958 return 0;
959 }
960
961 res = h2_get_buf(h2c, &h2c->mbuf);
962 if (!res) {
963 h2c->flags |= H2_CF_MUX_MALLOC;
964 h2c->flags |= H2_CF_DEM_MROOM;
965 return 0;
966 }
967
968 if (!b_data(res)) {
969 /* preface not yet sent */
970 b_istput(res, ist(H2_CONN_PREFACE));
971 }
972
973 return h2c_send_settings(h2c);
974}
975
Willy Tarreau081d4722017-05-16 21:51:05 +0200976/* try to send a GOAWAY frame on the connection to report an error or a graceful
977 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
978 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
979 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
980 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
981 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
982 * on unrecoverable failure. It will not attempt to send one again in this last
983 * case so that it is safe to use h2c_error() to report such errors.
984 */
985static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
986{
987 struct buffer *res;
988 char str[17];
989 int ret;
990
991 if (h2c->flags & H2_CF_GOAWAY_FAILED)
992 return 1; // claim that it worked
993
994 if (h2c_mux_busy(h2c, h2s)) {
995 if (h2s)
996 h2s->flags |= H2_SF_BLK_MBUSY;
997 else
998 h2c->flags |= H2_CF_DEM_MBUSY;
999 return 0;
1000 }
1001
Willy Tarreau44e973f2018-03-01 17:49:30 +01001002 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001003 if (!res) {
1004 h2c->flags |= H2_CF_MUX_MALLOC;
1005 if (h2s)
1006 h2s->flags |= H2_SF_BLK_MROOM;
1007 else
1008 h2c->flags |= H2_CF_DEM_MROOM;
1009 return 0;
1010 }
1011
1012 /* len: 8, type: 7, flags: none, sid: 0 */
1013 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1014
1015 if (h2c->last_sid < 0)
1016 h2c->last_sid = h2c->max_id;
1017
1018 write_n32(str + 9, h2c->last_sid);
1019 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001020 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001021 if (unlikely(ret <= 0)) {
1022 if (!ret) {
1023 h2c->flags |= H2_CF_MUX_MFULL;
1024 if (h2s)
1025 h2s->flags |= H2_SF_BLK_MROOM;
1026 else
1027 h2c->flags |= H2_CF_DEM_MROOM;
1028 return 0;
1029 }
1030 else {
1031 /* we cannot report this error using GOAWAY, so we mark
1032 * it and claim a success.
1033 */
1034 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1035 h2c->flags |= H2_CF_GOAWAY_FAILED;
1036 return 1;
1037 }
1038 }
1039 h2c->flags |= H2_CF_GOAWAY_SENT;
1040 return ret;
1041}
1042
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001043/* Try to send an RST_STREAM frame on the connection for the indicated stream
1044 * during mux operations. This stream must be valid and cannot be closed
1045 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1046 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1047 * not yet.
1048 *
1049 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1050 * to write the message, it subscribes the stream to future notifications.
1051 */
1052static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1053{
1054 struct buffer *res;
1055 char str[13];
1056 int ret;
1057
1058 if (!h2s || h2s->st == H2_SS_CLOSED)
1059 return 1;
1060
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001061 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1062 * RST_STREAM in response to a RST_STREAM frame.
1063 */
1064 if (h2c->dft == H2_FT_RST_STREAM) {
1065 ret = 1;
1066 goto ignore;
1067 }
1068
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001069 if (h2c_mux_busy(h2c, h2s)) {
1070 h2s->flags |= H2_SF_BLK_MBUSY;
1071 return 0;
1072 }
1073
Willy Tarreau44e973f2018-03-01 17:49:30 +01001074 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001075 if (!res) {
1076 h2c->flags |= H2_CF_MUX_MALLOC;
1077 h2s->flags |= H2_SF_BLK_MROOM;
1078 return 0;
1079 }
1080
1081 /* len: 4, type: 3, flags: none */
1082 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1083 write_n32(str + 5, h2s->id);
1084 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001085 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001086
1087 if (unlikely(ret <= 0)) {
1088 if (!ret) {
1089 h2c->flags |= H2_CF_MUX_MFULL;
1090 h2s->flags |= H2_SF_BLK_MROOM;
1091 return 0;
1092 }
1093 else {
1094 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1095 return 0;
1096 }
1097 }
1098
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001099 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001100 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001101 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001102 return ret;
1103}
1104
1105/* Try to send an RST_STREAM frame on the connection for the stream being
1106 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1107 * error code unless the stream's state already is IDLE or CLOSED in which
1108 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1109 * it was not yet.
1110 *
1111 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1112 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001113 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001114 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001115 */
1116static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1117{
1118 struct buffer *res;
1119 char str[13];
1120 int ret;
1121
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001122 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1123 * RST_STREAM in response to a RST_STREAM frame.
1124 */
1125 if (h2c->dft == H2_FT_RST_STREAM) {
1126 ret = 1;
1127 goto ignore;
1128 }
1129
Willy Tarreau27a84c92017-10-17 08:10:17 +02001130 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001131 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001132 return 0;
1133 }
1134
Willy Tarreau44e973f2018-03-01 17:49:30 +01001135 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001136 if (!res) {
1137 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001138 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001139 return 0;
1140 }
1141
1142 /* len: 4, type: 3, flags: none */
1143 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001144
Willy Tarreau27a84c92017-10-17 08:10:17 +02001145 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001146 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001147 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001148
Willy Tarreau27a84c92017-10-17 08:10:17 +02001149 if (unlikely(ret <= 0)) {
1150 if (!ret) {
1151 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001152 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001153 return 0;
1154 }
1155 else {
1156 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1157 return 0;
1158 }
1159 }
1160
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001161 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001162 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001163 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001164 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001165 }
1166
Willy Tarreau27a84c92017-10-17 08:10:17 +02001167 return ret;
1168}
1169
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001170/* try to send an empty DATA frame with the ES flag set to notify about the
1171 * end of stream and match a shutdown(write). If an ES was already sent as
1172 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1173 * on success or zero if nothing was done. In case of lack of room to write the
1174 * message, it subscribes the requesting stream to future notifications.
1175 */
1176static int h2_send_empty_data_es(struct h2s *h2s)
1177{
1178 struct h2c *h2c = h2s->h2c;
1179 struct buffer *res;
1180 char str[9];
1181 int ret;
1182
Willy Tarreau721c9742017-11-07 11:05:42 +01001183 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001184 return 1;
1185
1186 if (h2c_mux_busy(h2c, h2s)) {
1187 h2s->flags |= H2_SF_BLK_MBUSY;
1188 return 0;
1189 }
1190
Willy Tarreau44e973f2018-03-01 17:49:30 +01001191 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001192 if (!res) {
1193 h2c->flags |= H2_CF_MUX_MALLOC;
1194 h2s->flags |= H2_SF_BLK_MROOM;
1195 return 0;
1196 }
1197
1198 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1199 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1200 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001201 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001202 if (likely(ret > 0)) {
1203 h2s->flags |= H2_SF_ES_SENT;
1204 }
1205 else if (!ret) {
1206 h2c->flags |= H2_CF_MUX_MFULL;
1207 h2s->flags |= H2_SF_BLK_MROOM;
1208 return 0;
1209 }
1210 else {
1211 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1212 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001213 }
1214 return ret;
1215}
1216
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001217/* wake the streams attached to the connection, whose id is greater than <last>,
1218 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001219 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1220 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001221 */
1222static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1223{
1224 struct eb32_node *node;
1225 struct h2s *h2s;
1226
1227 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1228 flags |= CS_FL_ERROR;
1229
1230 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001231 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001232
1233 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1234 while (node) {
1235 h2s = container_of(node, struct h2s, by_id);
1236 if (h2s->id <= last)
1237 break;
1238 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001239
1240 if (!h2s->cs) {
1241 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001242 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001243 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001244 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001245
1246 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001247 if (h2s->recv_wait) {
1248 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001249 sw->wait_reason &= ~SUB_CAN_RECV;
1250 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001251 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001252 } else if (h2s->cs->data_cb->wake != NULL)
1253 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001254
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001255 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1256 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001257 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001258 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001259 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001260 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001261 }
1262}
1263
Willy Tarreau3421aba2017-07-27 15:41:03 +02001264/* Increase all streams' outgoing window size by the difference passed in
1265 * argument. This is needed upon receipt of the settings frame if the initial
1266 * window size is different. The difference may be negative and the resulting
1267 * window size as well, for the time it takes to receive some window updates.
1268 */
1269static void h2c_update_all_ws(struct h2c *h2c, int diff)
1270{
1271 struct h2s *h2s;
1272 struct eb32_node *node;
1273
1274 if (!diff)
1275 return;
1276
1277 node = eb32_first(&h2c->streams_by_id);
1278 while (node) {
1279 h2s = container_of(node, struct h2s, by_id);
1280 h2s->mws += diff;
1281 node = eb32_next(node);
1282 }
1283}
1284
1285/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1286 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1287 * return an error in h2c. Described in RFC7540#6.5.
1288 */
1289static int h2c_handle_settings(struct h2c *h2c)
1290{
1291 unsigned int offset;
1292 int error;
1293
1294 if (h2c->dff & H2_F_SETTINGS_ACK) {
1295 if (h2c->dfl) {
1296 error = H2_ERR_FRAME_SIZE_ERROR;
1297 goto fail;
1298 }
1299 return 1;
1300 }
1301
1302 if (h2c->dsi != 0) {
1303 error = H2_ERR_PROTOCOL_ERROR;
1304 goto fail;
1305 }
1306
1307 if (h2c->dfl % 6) {
1308 error = H2_ERR_FRAME_SIZE_ERROR;
1309 goto fail;
1310 }
1311
1312 /* that's the limit we can process */
1313 if (h2c->dfl > global.tune.bufsize) {
1314 error = H2_ERR_FRAME_SIZE_ERROR;
1315 goto fail;
1316 }
1317
1318 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001319 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001320 return 0;
1321
1322 /* parse the frame */
1323 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001324 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1325 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001326
1327 switch (type) {
1328 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1329 /* we need to update all existing streams with the
1330 * difference from the previous iws.
1331 */
1332 if (arg < 0) { // RFC7540#6.5.2
1333 error = H2_ERR_FLOW_CONTROL_ERROR;
1334 goto fail;
1335 }
1336 h2c_update_all_ws(h2c, arg - h2c->miw);
1337 h2c->miw = arg;
1338 break;
1339 case H2_SETTINGS_MAX_FRAME_SIZE:
1340 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1341 error = H2_ERR_PROTOCOL_ERROR;
1342 goto fail;
1343 }
1344 h2c->mfs = arg;
1345 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001346 case H2_SETTINGS_ENABLE_PUSH:
1347 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1348 error = H2_ERR_PROTOCOL_ERROR;
1349 goto fail;
1350 }
1351 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001352 }
1353 }
1354
1355 /* need to ACK this frame now */
1356 h2c->st0 = H2_CS_FRAME_A;
1357 return 1;
1358 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001359 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001360 h2c_error(h2c, error);
1361 return 0;
1362}
1363
1364/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1365 * success or one of the h2_status values.
1366 */
1367static int h2c_ack_settings(struct h2c *h2c)
1368{
1369 struct buffer *res;
1370 char str[9];
1371 int ret = -1;
1372
1373 if (h2c_mux_busy(h2c, NULL)) {
1374 h2c->flags |= H2_CF_DEM_MBUSY;
1375 return 0;
1376 }
1377
Willy Tarreau44e973f2018-03-01 17:49:30 +01001378 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001379 if (!res) {
1380 h2c->flags |= H2_CF_MUX_MALLOC;
1381 h2c->flags |= H2_CF_DEM_MROOM;
1382 return 0;
1383 }
1384
1385 memcpy(str,
1386 "\x00\x00\x00" /* length : 0 (no data) */
1387 "\x04" "\x01" /* type : 4, flags : ACK */
1388 "\x00\x00\x00\x00" /* stream ID */, 9);
1389
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001390 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001391 if (unlikely(ret <= 0)) {
1392 if (!ret) {
1393 h2c->flags |= H2_CF_MUX_MFULL;
1394 h2c->flags |= H2_CF_DEM_MROOM;
1395 return 0;
1396 }
1397 else {
1398 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1399 return 0;
1400 }
1401 }
1402 return ret;
1403}
1404
Willy Tarreaucf68c782017-10-10 17:11:41 +02001405/* processes a PING frame and schedules an ACK if needed. The caller must pass
1406 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1407 * missing data. It may return an error in h2c.
1408 */
1409static int h2c_handle_ping(struct h2c *h2c)
1410{
1411 /* frame length must be exactly 8 */
1412 if (h2c->dfl != 8) {
1413 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1414 return 0;
1415 }
1416
1417 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001418 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001419 h2c->st0 = H2_CS_FRAME_A;
1420 return 1;
1421}
1422
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001423/* Try to send a window update for stream id <sid> and value <increment>.
1424 * Returns > 0 on success or zero on missing room or failure. It may return an
1425 * error in h2c.
1426 */
1427static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1428{
1429 struct buffer *res;
1430 char str[13];
1431 int ret = -1;
1432
1433 if (h2c_mux_busy(h2c, NULL)) {
1434 h2c->flags |= H2_CF_DEM_MBUSY;
1435 return 0;
1436 }
1437
Willy Tarreau44e973f2018-03-01 17:49:30 +01001438 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001439 if (!res) {
1440 h2c->flags |= H2_CF_MUX_MALLOC;
1441 h2c->flags |= H2_CF_DEM_MROOM;
1442 return 0;
1443 }
1444
1445 /* length: 4, type: 8, flags: none */
1446 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1447 write_n32(str + 5, sid);
1448 write_n32(str + 9, increment);
1449
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001450 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001451
1452 if (unlikely(ret <= 0)) {
1453 if (!ret) {
1454 h2c->flags |= H2_CF_MUX_MFULL;
1455 h2c->flags |= H2_CF_DEM_MROOM;
1456 return 0;
1457 }
1458 else {
1459 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1460 return 0;
1461 }
1462 }
1463 return ret;
1464}
1465
1466/* try to send pending window update for the connection. It's safe to call it
1467 * with no pending updates. Returns > 0 on success or zero on missing room or
1468 * failure. It may return an error in h2c.
1469 */
1470static int h2c_send_conn_wu(struct h2c *h2c)
1471{
1472 int ret = 1;
1473
1474 if (h2c->rcvd_c <= 0)
1475 return 1;
1476
1477 /* send WU for the connection */
1478 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1479 if (ret > 0)
1480 h2c->rcvd_c = 0;
1481
1482 return ret;
1483}
1484
1485/* try to send pending window update for the current dmux stream. It's safe to
1486 * call it with no pending updates. Returns > 0 on success or zero on missing
1487 * room or failure. It may return an error in h2c.
1488 */
1489static int h2c_send_strm_wu(struct h2c *h2c)
1490{
1491 int ret = 1;
1492
1493 if (h2c->rcvd_s <= 0)
1494 return 1;
1495
1496 /* send WU for the stream */
1497 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1498 if (ret > 0)
1499 h2c->rcvd_s = 0;
1500
1501 return ret;
1502}
1503
Willy Tarreaucf68c782017-10-10 17:11:41 +02001504/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1505 * success, 0 on missing data or one of the h2_status values.
1506 */
1507static int h2c_ack_ping(struct h2c *h2c)
1508{
1509 struct buffer *res;
1510 char str[17];
1511 int ret = -1;
1512
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001513 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001514 return 0;
1515
1516 if (h2c_mux_busy(h2c, NULL)) {
1517 h2c->flags |= H2_CF_DEM_MBUSY;
1518 return 0;
1519 }
1520
Willy Tarreau44e973f2018-03-01 17:49:30 +01001521 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001522 if (!res) {
1523 h2c->flags |= H2_CF_MUX_MALLOC;
1524 h2c->flags |= H2_CF_DEM_MROOM;
1525 return 0;
1526 }
1527
1528 memcpy(str,
1529 "\x00\x00\x08" /* length : 8 (same payload) */
1530 "\x06" "\x01" /* type : 6, flags : ACK */
1531 "\x00\x00\x00\x00" /* stream ID */, 9);
1532
1533 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001534 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001535
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001536 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001537 if (unlikely(ret <= 0)) {
1538 if (!ret) {
1539 h2c->flags |= H2_CF_MUX_MFULL;
1540 h2c->flags |= H2_CF_DEM_MROOM;
1541 return 0;
1542 }
1543 else {
1544 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1545 return 0;
1546 }
1547 }
1548 return ret;
1549}
1550
Willy Tarreau26f95952017-07-27 17:18:30 +02001551/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1552 * Returns > 0 on success or zero on missing data. It may return an error in
1553 * h2c or h2s. Described in RFC7540#6.9.
1554 */
1555static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1556{
1557 int32_t inc;
1558 int error;
1559
1560 if (h2c->dfl != 4) {
1561 error = H2_ERR_FRAME_SIZE_ERROR;
1562 goto conn_err;
1563 }
1564
1565 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001566 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001567 return 0;
1568
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001569 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001570
1571 if (h2c->dsi != 0) {
1572 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001573
1574 /* it's not an error to receive WU on a closed stream */
1575 if (h2s->st == H2_SS_CLOSED)
1576 return 1;
1577
1578 if (!inc) {
1579 error = H2_ERR_PROTOCOL_ERROR;
1580 goto strm_err;
1581 }
1582
1583 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1584 error = H2_ERR_FLOW_CONTROL_ERROR;
1585 goto strm_err;
1586 }
1587
1588 h2s->mws += inc;
1589 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1590 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001591 if (h2s->send_wait)
1592 LIST_ADDQ(&h2c->send_list, &h2s->list);
1593
Willy Tarreau26f95952017-07-27 17:18:30 +02001594 }
1595 }
1596 else {
1597 /* connection window update */
1598 if (!inc) {
1599 error = H2_ERR_PROTOCOL_ERROR;
1600 goto conn_err;
1601 }
1602
1603 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1604 error = H2_ERR_FLOW_CONTROL_ERROR;
1605 goto conn_err;
1606 }
1607
1608 h2c->mws += inc;
1609 }
1610
1611 return 1;
1612
1613 conn_err:
1614 h2c_error(h2c, error);
1615 return 0;
1616
1617 strm_err:
1618 if (h2s) {
1619 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001620 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001621 }
1622 else
1623 h2c_error(h2c, error);
1624 return 0;
1625}
1626
Willy Tarreaue96b0922017-10-30 00:28:29 +01001627/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1628 * the last ID. Returns > 0 on success or zero on missing data. It may return
1629 * an error in h2c. Described in RFC7540#6.8.
1630 */
1631static int h2c_handle_goaway(struct h2c *h2c)
1632{
1633 int error;
1634 int last;
1635
1636 if (h2c->dsi != 0) {
1637 error = H2_ERR_PROTOCOL_ERROR;
1638 goto conn_err;
1639 }
1640
1641 if (h2c->dfl < 8) {
1642 error = H2_ERR_FRAME_SIZE_ERROR;
1643 goto conn_err;
1644 }
1645
1646 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001647 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001648 return 0;
1649
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001650 last = h2_get_n32(&h2c->dbuf, 0);
1651 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001652 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001653 if (h2c->last_sid < 0)
1654 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001655 return 1;
1656
1657 conn_err:
1658 h2c_error(h2c, error);
1659 return 0;
1660}
1661
Willy Tarreau92153fc2017-12-03 19:46:19 +01001662/* processes a PRIORITY frame, and either skips it or rejects if it is
1663 * invalid. Returns > 0 on success or zero on missing data. It may return
1664 * an error in h2c. Described in RFC7540#6.3.
1665 */
1666static int h2c_handle_priority(struct h2c *h2c)
1667{
1668 int error;
1669
1670 if (h2c->dsi == 0) {
1671 error = H2_ERR_PROTOCOL_ERROR;
1672 goto conn_err;
1673 }
1674
1675 if (h2c->dfl != 5) {
1676 error = H2_ERR_FRAME_SIZE_ERROR;
1677 goto conn_err;
1678 }
1679
1680 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001681 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001682 return 0;
1683
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001684 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001685 /* 7540#5.3 : can't depend on itself */
1686 error = H2_ERR_PROTOCOL_ERROR;
1687 goto conn_err;
1688 }
1689 return 1;
1690
1691 conn_err:
1692 h2c_error(h2c, error);
1693 return 0;
1694}
1695
Willy Tarreaucd234e92017-08-18 10:59:39 +02001696/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1697 * Returns > 0 on success or zero on missing data. It may return an error in
1698 * h2c. Described in RFC7540#6.4.
1699 */
1700static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1701{
1702 int error;
1703
1704 if (h2c->dsi == 0) {
1705 error = H2_ERR_PROTOCOL_ERROR;
1706 goto conn_err;
1707 }
1708
Willy Tarreaucd234e92017-08-18 10:59:39 +02001709 if (h2c->dfl != 4) {
1710 error = H2_ERR_FRAME_SIZE_ERROR;
1711 goto conn_err;
1712 }
1713
1714 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001715 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001716 return 0;
1717
1718 /* late RST, already handled */
1719 if (h2s->st == H2_SS_CLOSED)
1720 return 1;
1721
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001722 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001723 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001724
1725 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001726 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001727 if (h2s->recv_wait) {
1728 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001729
1730 sw->wait_reason &= ~SUB_CAN_RECV;
1731 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001732 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001733 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001734 }
1735
1736 h2s->flags |= H2_SF_RST_RCVD;
1737 return 1;
1738
1739 conn_err:
1740 h2c_error(h2c, error);
1741 return 0;
1742}
1743
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001744/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1745 * It may return an error in h2c or h2s. The caller must consider that the
1746 * return value is the new h2s in case one was allocated (most common case).
1747 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001748 * errors here are reported as connection errors since it's impossible to
1749 * recover from such errors after the compression context has been altered.
1750 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001751static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001752{
1753 int error;
1754
1755 if (!h2c->dfl) {
1756 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001757 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001758 goto strm_err;
1759 }
1760
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001761 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001762 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001763
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001764 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001765 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001766
Willy Tarreauf2101912018-07-19 10:11:38 +02001767 if (h2c->flags & H2_CF_DEM_TOOMANY)
1768 return 0; // too many cs still present
1769
Willy Tarreau13278b42017-10-13 19:23:14 +02001770 /* now either the frame is complete or the buffer is complete */
1771 if (h2s->st != H2_SS_IDLE) {
1772 /* FIXME: stream already exists, this is only allowed for
1773 * trailers (not supported for now).
1774 */
1775 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001776 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001777 goto conn_err;
1778 }
1779 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1780 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1781 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001782 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001783 goto conn_err;
1784 }
1785
Willy Tarreau22de8d32018-09-05 19:55:58 +02001786 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001787 * positively from h2c_frt_stream_new(), the stream will report the error,
1788 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001789 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001790 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001791 if (!h2s) {
1792 error = H2_ERR_INTERNAL_ERROR;
1793 goto conn_err;
1794 }
1795
1796 h2s->st = H2_SS_OPEN;
1797 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1798 h2s->st = H2_SS_HREM;
1799 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001800 /* note: cs cannot be null for now (just created above) */
1801 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001802 }
1803
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001804 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001805 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001806
Willy Tarreau8f650c32017-11-21 19:36:21 +01001807 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001808 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001809
Willy Tarreau721c9742017-11-07 11:05:42 +01001810 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001811 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001812 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001813 }
1814 else {
1815 /* update the max stream ID if the request is being processed */
1816 if (h2s->id > h2c->max_id)
1817 h2c->max_id = h2s->id;
1818 }
1819
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001820 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001821
1822 conn_err:
1823 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001824 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001825
1826 strm_err:
1827 if (h2s) {
1828 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001829 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001830 }
1831 else
1832 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001833 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001834}
1835
Willy Tarreau454f9052017-10-26 19:40:35 +02001836/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1837 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1838 */
1839static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1840{
1841 int error;
1842
1843 /* note that empty DATA frames are perfectly valid and sometimes used
1844 * to signal an end of stream (with the ES flag).
1845 */
1846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001847 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001848 return 0; // empty buffer
1849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001851 return 0; // incomplete frame
1852
1853 /* now either the frame is complete or the buffer is complete */
1854
1855 if (!h2c->dsi) {
1856 /* RFC7540#6.1 */
1857 error = H2_ERR_PROTOCOL_ERROR;
1858 goto conn_err;
1859 }
1860
1861 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1862 /* RFC7540#6.1 */
1863 error = H2_ERR_STREAM_CLOSED;
1864 goto strm_err;
1865 }
1866
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001867 if (!h2_frt_transfer_data(h2s))
1868 return 0;
1869
Willy Tarreau454f9052017-10-26 19:40:35 +02001870 /* call the upper layers to process the frame, then let the upper layer
1871 * notify the stream about any change.
1872 */
1873 if (!h2s->cs) {
1874 error = H2_ERR_STREAM_CLOSED;
1875 goto strm_err;
1876 }
1877
Willy Tarreau8f650c32017-11-21 19:36:21 +01001878 if (h2c->st0 >= H2_CS_ERROR)
1879 return 0;
1880
Willy Tarreau721c9742017-11-07 11:05:42 +01001881 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001882 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001883 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001884 }
1885
1886 /* check for completion : the callee will change this to FRAME_A or
1887 * FRAME_H once done.
1888 */
1889 if (h2c->st0 == H2_CS_FRAME_P)
1890 return 0;
1891
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001892
1893 /* last frame */
1894 if (h2c->dff & H2_F_DATA_END_STREAM) {
1895 h2s->st = H2_SS_HREM;
1896 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001897 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001898 }
1899
Willy Tarreau454f9052017-10-26 19:40:35 +02001900 return 1;
1901
1902 conn_err:
1903 h2c_error(h2c, error);
1904 return 0;
1905
1906 strm_err:
1907 if (h2s) {
1908 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001909 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001910 }
1911 else
1912 h2c_error(h2c, error);
1913 return 0;
1914}
1915
Willy Tarreaubc933932017-10-09 16:21:43 +02001916/* process Rx frames to be demultiplexed */
1917static void h2_process_demux(struct h2c *h2c)
1918{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001919 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001920
Willy Tarreau081d4722017-05-16 21:51:05 +02001921 if (h2c->st0 >= H2_CS_ERROR)
1922 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001923
1924 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1925 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001926 if (h2c->flags & H2_CF_IS_BACK)
1927 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001928 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1929 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001930 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001931 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001932 sess_log(h2c->conn->owner);
1933 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001934 goto fail;
1935 }
1936
1937 h2c->max_id = 0;
1938 h2c->st0 = H2_CS_SETTINGS1;
1939 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001940
1941 if (h2c->st0 == H2_CS_SETTINGS1) {
1942 struct h2_fh hdr;
1943
1944 /* ensure that what is pending is a valid SETTINGS frame
1945 * without an ACK.
1946 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001947 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001948 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001949 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001950 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001951 sess_log(h2c->conn->owner);
1952 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001953 goto fail;
1954 }
1955
1956 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1957 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1958 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1959 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001960 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001961 goto fail;
1962 }
1963
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001964 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001965 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1966 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1967 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001968 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001969 goto fail;
1970 }
1971
1972 /* that's OK, switch to FRAME_P to process it */
1973 h2c->dfl = hdr.len;
1974 h2c->dsi = hdr.sid;
1975 h2c->dft = hdr.ft;
1976 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001977 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001978 h2c->st0 = H2_CS_FRAME_P;
1979 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001980 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001981
1982 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001984 int ret = 0;
1985
1986 if (h2c->st0 >= H2_CS_ERROR)
1987 break;
1988
1989 if (h2c->st0 == H2_CS_FRAME_H) {
1990 struct h2_fh hdr;
1991
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001992 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001993 break;
1994
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001995 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001996 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1997 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001998 if (!h2c->nb_streams) {
1999 /* only log if no other stream can report the error */
2000 sess_log(h2c->conn->owner);
2001 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002002 break;
2003 }
2004
2005 h2c->dfl = hdr.len;
2006 h2c->dsi = hdr.sid;
2007 h2c->dft = hdr.ft;
2008 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002009 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002010 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002011 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002012 }
2013
2014 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002015 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2016
Olivier Houchard638b7992018-08-16 15:41:52 +02002017 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002018 /* we may have to signal the upper layers */
2019 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002020 if (h2s->recv_wait) {
2021 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2022 tasklet_wakeup(h2s->recv_wait->task);
2023 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002024 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002025 }
2026 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002027
Willy Tarreaud7901432017-12-29 11:34:40 +01002028 if (h2c->st0 == H2_CS_FRAME_E)
2029 goto strm_err;
2030
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002031 if (h2s->st == H2_SS_IDLE &&
2032 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2033 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2034 * this state MUST be treated as a connection error
2035 */
2036 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2037 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002038 if (!h2c->nb_streams) {
2039 /* only log if no other stream can report the error */
2040 sess_log(h2c->conn->owner);
2041 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002042 break;
2043 }
2044
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002045 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2046 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2047 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2048 * this state MUST be treated as a stream error
2049 */
2050 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2051 goto strm_err;
2052 }
2053
Willy Tarreauab837502017-12-27 15:07:30 +01002054 /* Below the management of frames received in closed state is a
2055 * bit hackish because the spec makes strong differences between
2056 * streams closed by receiving RST, sending RST, and seeing ES
2057 * in both directions. In addition to this, the creation of a
2058 * new stream reusing the identifier of a closed one will be
2059 * detected here. Given that we cannot keep track of all closed
2060 * streams forever, we consider that unknown closed streams were
2061 * closed on RST received, which allows us to respond with an
2062 * RST without breaking the connection (eg: to abort a transfer).
2063 * Some frames have to be silently ignored as well.
2064 */
2065 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2066 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2067 /* #5.1.1: The identifier of a newly
2068 * established stream MUST be numerically
2069 * greater than all streams that the initiating
2070 * endpoint has opened or reserved. This
2071 * governs streams that are opened using a
2072 * HEADERS frame and streams that are reserved
2073 * using PUSH_PROMISE. An endpoint that
2074 * receives an unexpected stream identifier
2075 * MUST respond with a connection error.
2076 */
2077 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2078 goto strm_err;
2079 }
2080
2081 if (h2s->flags & H2_SF_RST_RCVD) {
2082 /* RFC7540#5.1:closed: an endpoint that
2083 * receives any frame other than PRIORITY after
2084 * receiving a RST_STREAM MUST treat that as a
2085 * stream error of type STREAM_CLOSED.
2086 *
2087 * Note that old streams fall into this category
2088 * and will lead to an RST being sent.
2089 */
2090 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2091 h2c->st0 = H2_CS_FRAME_E;
2092 goto strm_err;
2093 }
2094
2095 /* RFC7540#5.1:closed: if this state is reached as a
2096 * result of sending a RST_STREAM frame, the peer that
2097 * receives the RST_STREAM might have already sent
2098 * frames on the stream that cannot be withdrawn. An
2099 * endpoint MUST ignore frames that it receives on
2100 * closed streams after it has sent a RST_STREAM
2101 * frame. An endpoint MAY choose to limit the period
2102 * over which it ignores frames and treat frames that
2103 * arrive after this time as being in error.
2104 */
2105 if (!(h2s->flags & H2_SF_RST_SENT)) {
2106 /* RFC7540#5.1:closed: any frame other than
2107 * PRIO/WU/RST in this state MUST be treated as
2108 * a connection error
2109 */
2110 if (h2c->dft != H2_FT_RST_STREAM &&
2111 h2c->dft != H2_FT_PRIORITY &&
2112 h2c->dft != H2_FT_WINDOW_UPDATE) {
2113 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2114 goto strm_err;
2115 }
2116 }
2117 }
2118
Willy Tarreauc0da1962017-10-30 18:38:00 +01002119#if 0
2120 // problem below: it is not possible to completely ignore such
2121 // streams as we need to maintain the compression state as well
2122 // and for this we need to completely process these frames (eg:
2123 // HEADERS frames) as well as counting DATA frames to emit
2124 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2125 // This is a typical case of layer violation where the
2126 // transported contents are critical to the connection's
2127 // validity and must be ignored at the same time :-(
2128
2129 /* graceful shutdown, ignore streams whose ID is higher than
2130 * the one advertised in GOAWAY. RFC7540#6.8.
2131 */
2132 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002133 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2134 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002135 h2c->dfl -= ret;
2136 ret = h2c->dfl == 0;
2137 goto strm_err;
2138 }
2139#endif
2140
Willy Tarreau7e98c052017-10-10 15:56:59 +02002141 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002142 case H2_FT_SETTINGS:
2143 if (h2c->st0 == H2_CS_FRAME_P)
2144 ret = h2c_handle_settings(h2c);
2145
2146 if (h2c->st0 == H2_CS_FRAME_A)
2147 ret = h2c_ack_settings(h2c);
2148 break;
2149
Willy Tarreaucf68c782017-10-10 17:11:41 +02002150 case H2_FT_PING:
2151 if (h2c->st0 == H2_CS_FRAME_P)
2152 ret = h2c_handle_ping(h2c);
2153
2154 if (h2c->st0 == H2_CS_FRAME_A)
2155 ret = h2c_ack_ping(h2c);
2156 break;
2157
Willy Tarreau26f95952017-07-27 17:18:30 +02002158 case H2_FT_WINDOW_UPDATE:
2159 if (h2c->st0 == H2_CS_FRAME_P)
2160 ret = h2c_handle_window_update(h2c, h2s);
2161 break;
2162
Willy Tarreau61290ec2017-10-17 08:19:21 +02002163 case H2_FT_CONTINUATION:
2164 /* we currently don't support CONTINUATION frames since
2165 * we have nowhere to store the partial HEADERS frame.
2166 * Let's abort the stream on an INTERNAL_ERROR here.
2167 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002168 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002169 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002170 h2c->st0 = H2_CS_FRAME_E;
2171 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002172 break;
2173
Willy Tarreau13278b42017-10-13 19:23:14 +02002174 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002175 if (h2c->st0 == H2_CS_FRAME_P) {
2176 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2177 if (tmp_h2s) {
2178 h2s = tmp_h2s;
2179 ret = 1;
2180 }
2181 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002182 break;
2183
Willy Tarreau454f9052017-10-26 19:40:35 +02002184 case H2_FT_DATA:
2185 if (h2c->st0 == H2_CS_FRAME_P)
2186 ret = h2c_frt_handle_data(h2c, h2s);
2187
2188 if (h2c->st0 == H2_CS_FRAME_A)
2189 ret = h2c_send_strm_wu(h2c);
2190 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002191
Willy Tarreau92153fc2017-12-03 19:46:19 +01002192 case H2_FT_PRIORITY:
2193 if (h2c->st0 == H2_CS_FRAME_P)
2194 ret = h2c_handle_priority(h2c);
2195 break;
2196
Willy Tarreaucd234e92017-08-18 10:59:39 +02002197 case H2_FT_RST_STREAM:
2198 if (h2c->st0 == H2_CS_FRAME_P)
2199 ret = h2c_handle_rst_stream(h2c, h2s);
2200 break;
2201
Willy Tarreaue96b0922017-10-30 00:28:29 +01002202 case H2_FT_GOAWAY:
2203 if (h2c->st0 == H2_CS_FRAME_P)
2204 ret = h2c_handle_goaway(h2c);
2205 break;
2206
Willy Tarreau1c661982017-10-30 13:52:01 +01002207 case H2_FT_PUSH_PROMISE:
2208 /* not permitted here, RFC7540#5.1 */
2209 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002210 if (!h2c->nb_streams) {
2211 /* only log if no other stream can report the error */
2212 sess_log(h2c->conn->owner);
2213 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002214 break;
2215
2216 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002217 default:
2218 /* drop frames that we ignore. They may be larger than
2219 * the buffer so we drain all of their contents until
2220 * we reach the end.
2221 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002222 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2223 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002224 h2c->dfl -= ret;
2225 ret = h2c->dfl == 0;
2226 }
2227
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002228 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002229 /* We may have to send an RST if not done yet */
2230 if (h2s->st == H2_SS_ERROR)
2231 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002232
Willy Tarreaua20a5192017-12-27 11:02:06 +01002233 if (h2c->st0 == H2_CS_FRAME_E)
2234 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002235
Willy Tarreau7e98c052017-10-10 15:56:59 +02002236 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002237 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002238 break;
2239
2240 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002241 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002242 h2c->st0 = H2_CS_FRAME_H;
2243 }
2244 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002245
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002246 if (h2c->rcvd_c > 0 &&
2247 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2248 h2c_send_conn_wu(h2c);
2249
Willy Tarreau52eed752017-09-22 15:05:09 +02002250 fail:
2251 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002252 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002253 /* we may have to signal the upper layers */
2254 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002255 if (h2s->recv_wait) {
2256 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2257 tasklet_wakeup(h2s->recv_wait->task);
2258 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002259 }
2260 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002261
2262 if (h2_recv_allowed(h2c))
2263 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002264}
2265
2266/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2267 * the end.
2268 */
2269static int h2_process_mux(struct h2c *h2c)
2270{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002271 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002272
Willy Tarreau01b44822018-10-03 14:26:37 +02002273 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2274 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2275 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2276 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2277 if (h2c->st0 == H2_CS_ERROR) {
2278 h2c->st0 = H2_CS_ERROR2;
2279 sess_log(h2c->conn->owner);
2280 }
2281 goto fail;
2282 }
2283 h2c->st0 = H2_CS_SETTINGS1;
2284 }
2285 /* need to wait for the other side */
2286 if (h2c->st0 == H2_CS_SETTINGS1)
2287 return 1;
2288 }
2289
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002290 /* start by sending possibly pending window updates */
2291 if (h2c->rcvd_c > 0 &&
2292 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2293 h2c_send_conn_wu(h2c) < 0)
2294 goto fail;
2295
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002296 /* First we always process the flow control list because the streams
2297 * waiting there were already elected for immediate emission but were
2298 * blocked just on this.
2299 */
2300
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002301 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002302 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2303 h2c->st0 >= H2_CS_ERROR)
2304 break;
2305
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002306 h2s->flags &= ~H2_SF_BLK_ANY;
2307 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002308 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002309 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002310 LIST_DEL(&h2s->list);
2311 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002312 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002313 }
2314
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002315 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002316 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2317 break;
2318
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002319 h2s->flags &= ~H2_SF_BLK_ANY;
2320 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002321 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002322 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002323 LIST_DEL(&h2s->list);
2324 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002325 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002326 }
2327
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002328 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002329 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002330 if (h2c->st0 == H2_CS_ERROR) {
2331 if (h2c->max_id >= 0) {
2332 h2c_send_goaway_error(h2c, NULL);
2333 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2334 return 0;
2335 }
2336
2337 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2338 }
2339 return 1;
2340 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002341 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002342}
2343
Willy Tarreau62f52692017-10-08 23:01:42 +02002344
Willy Tarreau479998a2018-11-18 06:30:59 +01002345/* Attempt to read data, and subscribe if none available.
2346 * The function returns 1 if data has been received, otherwise zero.
2347 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002348static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002349{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002350 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002351 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002352 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002353 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002354
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002355 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002356 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002357
Willy Tarreau315d8072017-12-10 22:17:57 +01002358 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002359 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002360
Willy Tarreau44e973f2018-03-01 17:49:30 +01002361 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002362 if (!buf) {
2363 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002364 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002365 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002366
Olivier Houchard7505f942018-08-21 18:10:44 +02002367 do {
2368 max = buf->size - b_data(buf);
2369 if (max)
2370 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2371 else
2372 ret = 0;
2373 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002374
Olivier Houchard53216e72018-10-10 15:46:36 +02002375 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002376 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002377
Olivier Houcharda1411e62018-08-17 18:42:48 +02002378 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002379 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002380 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002381 }
2382
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002383 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002384 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002385 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002386}
2387
Willy Tarreau479998a2018-11-18 06:30:59 +01002388/* Try to send data if possible.
2389 * The function returns 1 if data have been sent, otherwise zero.
2390 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002391static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002392{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002393 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002394 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002395 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002396
2397 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002398 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002399
Olivier Houchard7505f942018-08-21 18:10:44 +02002400
Willy Tarreaua2af5122017-10-09 11:56:46 +02002401 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2402 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002403 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002404 }
2405
Willy Tarreaubc933932017-10-09 16:21:43 +02002406 /* This loop is quite simple : it tries to fill as much as it can from
2407 * pending streams into the existing buffer until it's reportedly full
2408 * or the end of send requests is reached. Then it tries to send this
2409 * buffer's contents out, marks it not full if at least one byte could
2410 * be sent, and tries again.
2411 *
2412 * The snd_buf() function normally takes a "flags" argument which may
2413 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2414 * data immediately comes and CO_SFL_STREAMER to indicate that the
2415 * connection is streaming lots of data (used to increase TLS record
2416 * size at the expense of latency). The former can be sent any time
2417 * there's a buffer full flag, as it indicates at least one stream
2418 * attempted to send and failed so there are pending data. An
2419 * alternative would be to set it as long as there's an active stream
2420 * but that would be problematic for ACKs until we have an absolute
2421 * guarantee that all waiters have at least one byte to send. The
2422 * latter should possibly not be set for now.
2423 */
2424
2425 done = 0;
2426 while (!done) {
2427 unsigned int flags = 0;
2428
2429 /* fill as much as we can into the current buffer */
2430 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2431 done = h2_process_mux(h2c);
2432
2433 if (conn->flags & CO_FL_ERROR)
2434 break;
2435
2436 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2437 flags |= CO_SFL_MSG_MORE;
2438
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002439 if (b_data(&h2c->mbuf)) {
2440 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002441 if (!ret)
2442 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002443 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002444 b_del(&h2c->mbuf, ret);
2445 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002446 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002447
2448 /* wrote at least one byte, the buffer is not full anymore */
2449 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2450 }
2451
Willy Tarreaua2af5122017-10-09 11:56:46 +02002452 if (conn->flags & CO_FL_SOCK_WR_SH) {
2453 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002455 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002456 /* We're not full anymore, so we can wake any task that are waiting
2457 * for us.
2458 */
2459 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002460 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002461 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2462 struct h2s *, list);
2463 LIST_DEL(&h2s->list);
2464 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002465 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002466 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002467 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002468 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002469 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002470 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002471 /* We're done, no more to send */
2472 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002473 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002474schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002475 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2476 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002477 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002478}
2479
2480static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2481{
2482 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002483 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002484
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002485 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002486 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002487 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002488 ret |= h2_recv(h2c);
2489 if (ret)
2490 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002491 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002492}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002493
Willy Tarreau62f52692017-10-08 23:01:42 +02002494/* callback called on any event by the connection handler.
2495 * It applies changes and returns zero, or < 0 if it wants immediate
2496 * destruction of the connection (which normally doesn not happen in h2).
2497 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002498static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002499{
Olivier Houchard7505f942018-08-21 18:10:44 +02002500 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002501
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002502 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002503 h2_process_demux(h2c);
2504
2505 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002506 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002507
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002508 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002509 h2c->flags &= ~H2_CF_DEM_DFULL;
2510 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002511 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002512
Willy Tarreau0b37d652018-10-03 10:33:02 +02002513 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002514 /* frontend is stopping, reload likely in progress, let's try
2515 * to announce a graceful shutdown if not yet done. We don't
2516 * care if it fails, it will be tried again later.
2517 */
2518 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2519 if (h2c->last_sid < 0)
2520 h2c->last_sid = (1U << 31) - 1;
2521 h2c_send_goaway_error(h2c, NULL);
2522 }
2523 }
2524
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002525 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002526 * If we received early data, and the handshake is done, wake
2527 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002528 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002529 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2530 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2531 struct eb32_node *node;
2532 struct h2s *h2s;
2533
2534 h2c->flags |= H2_CF_WAIT_FOR_HS;
2535 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2536
2537 while (node) {
2538 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002539 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002540 h2s->recv_wait) {
2541 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002542 sw->wait_reason &= ~SUB_CAN_RECV;
2543 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002544 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002545 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002546 node = eb32_next(node);
2547 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002548 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002549
Willy Tarreau26bd7612017-10-09 16:47:04 +02002550 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002551 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2552 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2553 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002554 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002555
2556 if (eb_is_empty(&h2c->streams_by_id)) {
2557 /* no more stream, kill the connection now */
2558 h2_release(conn);
2559 return -1;
2560 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002561 }
2562
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002563 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002564 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002565
Olivier Houchard53216e72018-10-10 15:46:36 +02002566 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2567 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2568 (h2c->st0 != H2_CS_ERROR &&
2569 !b_data(&h2c->mbuf) &&
2570 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2571 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002572 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002573
Willy Tarreau3f133572017-10-31 19:21:06 +01002574 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002575 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002576 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002577 task_queue(h2c->task);
2578 }
2579 else
2580 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002581 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002582
Olivier Houchard7505f942018-08-21 18:10:44 +02002583 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002584 return 0;
2585}
2586
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002587static int h2_wake(struct connection *conn)
2588{
2589 struct h2c *h2c = conn->mux_ctx;
2590
2591 return (h2_process(h2c));
2592}
2593
Willy Tarreauea392822017-10-31 10:02:25 +01002594/* Connection timeout management. The principle is that if there's no receipt
2595 * nor sending for a certain amount of time, the connection is closed. If the
2596 * MUX buffer still has lying data or is not allocatable, the connection is
2597 * immediately killed. If it's allocatable and empty, we attempt to send a
2598 * GOAWAY frame.
2599 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002600static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002601{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002602 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002603 int expired = tick_is_expired(t->expire, now_ms);
2604
Willy Tarreau0975f112018-03-29 15:22:59 +02002605 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002606 return t;
2607
Willy Tarreau0975f112018-03-29 15:22:59 +02002608 task_delete(t);
2609 task_free(t);
2610
2611 if (!h2c) {
2612 /* resources were already deleted */
2613 return NULL;
2614 }
2615
2616 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002617 h2c_error(h2c, H2_ERR_NO_ERROR);
2618 h2_wake_some_streams(h2c, 0, 0);
2619
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002620 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002621 /* don't even try to send a GOAWAY, the buffer is stuck */
2622 h2c->flags |= H2_CF_GOAWAY_FAILED;
2623 }
2624
2625 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002626 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002627 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2628 h2c->flags |= H2_CF_GOAWAY_FAILED;
2629
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002630 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2631 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002632 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002633 b_del(&h2c->mbuf, ret);
2634 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002635 }
2636 }
Willy Tarreauea392822017-10-31 10:02:25 +01002637
Willy Tarreau0975f112018-03-29 15:22:59 +02002638 /* either we can release everything now or it will be done later once
2639 * the last stream closes.
2640 */
2641 if (eb_is_empty(&h2c->streams_by_id))
2642 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002643
Willy Tarreauea392822017-10-31 10:02:25 +01002644 return NULL;
2645}
2646
2647
Willy Tarreau62f52692017-10-08 23:01:42 +02002648/*******************************************/
2649/* functions below are used by the streams */
2650/*******************************************/
2651
2652/*
2653 * Attach a new stream to a connection
2654 * (Used for outgoing connections)
2655 */
2656static struct conn_stream *h2_attach(struct connection *conn)
2657{
2658 return NULL;
2659}
2660
Willy Tarreaufafd3982018-11-18 21:29:20 +01002661/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2662 * We have to scan because we may have some orphan streams. It might be
2663 * beneficial to scan backwards from the end to reduce the likeliness to find
2664 * orphans.
2665 */
2666static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2667{
2668 struct h2c *h2c = conn->mux_ctx;
2669 struct h2s *h2s;
2670 struct eb32_node *node;
2671
2672 node = eb32_first(&h2c->streams_by_id);
2673 while (node) {
2674 h2s = container_of(node, struct h2s, by_id);
2675 if (h2s->cs)
2676 return h2s->cs;
2677 node = eb32_next(node);
2678 }
2679 return NULL;
2680}
2681
Willy Tarreau62f52692017-10-08 23:01:42 +02002682/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002683 * Destroy the mux and the associated connection, if it is no longer used
2684 */
2685static void h2_destroy(struct connection *conn)
2686{
2687 struct h2c *h2c = conn->mux_ctx;
2688
2689 if (eb_is_empty(&h2c->streams_by_id))
2690 h2_release(h2c->conn);
2691}
2692
2693/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002694 * Detach the stream from the connection and possibly release the connection.
2695 */
2696static void h2_detach(struct conn_stream *cs)
2697{
Willy Tarreau60935142017-10-16 18:11:19 +02002698 struct h2s *h2s = cs->ctx;
2699 struct h2c *h2c;
2700
2701 cs->ctx = NULL;
2702 if (!h2s)
2703 return;
2704
2705 h2c = h2s->h2c;
2706 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002707 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002708 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2709 !h2_has_too_many_cs(h2c)) {
2710 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002711 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002712 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002713 }
Willy Tarreau60935142017-10-16 18:11:19 +02002714
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002715 /* this stream may be blocked waiting for some data to leave (possibly
2716 * an ES or RST frame), so orphan it in this case.
2717 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002718 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002719 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002720 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002721 return;
2722
Willy Tarreau45f752e2017-10-30 15:44:59 +01002723 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2724 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2725 /* unblock the connection if it was blocked on this
2726 * stream.
2727 */
2728 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2729 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002730 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002731 }
2732
Willy Tarreau71049cc2018-03-28 13:56:39 +02002733 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002734
Willy Tarreaue323f342018-03-28 13:51:45 +02002735 /* We don't want to close right now unless we're removing the
2736 * last stream, and either the connection is in error, or it
2737 * reached the ID already specified in a GOAWAY frame received
2738 * or sent (as seen by last_sid >= 0).
2739 */
2740 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2741 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002742 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002743 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002744 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002745 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002746 (conn_xprt_read0_pending(h2c->conn) ||
2747 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2748 /* no more stream will come, kill it now */
2749 h2_release(h2c->conn);
2750 }
2751 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002752 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002753 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2754 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002755 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002756 else
2757 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002758 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002759}
2760
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002761static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002762{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002763 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002764 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002765
Willy Tarreau721c9742017-11-07 11:05:42 +01002766 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002767 return;
2768
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002769 /* if no outgoing data was seen on this stream, it means it was
2770 * closed with a "tcp-request content" rule that is normally
2771 * used to kill the connection ASAP (eg: limit abuse). In this
2772 * case we send a goaway to close the connection.
2773 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002774 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002775 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002776 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002777
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002778 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2779 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002780 h2c_send_goaway_error(h2c, h2s) <= 0)
2781 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002782
Willy Tarreau00dd0782018-03-01 16:31:34 +01002783 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002784
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002785 return;
2786add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002787 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002788 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002789 if (h2s->flags & H2_SF_BLK_MFCTL) {
2790 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2791 h2s->send_wait = sw;
2792 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2793 h2s->send_wait = sw;
2794 LIST_ADDQ(&h2c->send_list, &h2s->list);
2795 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002796 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002797 /* Let the handler know we want shutr */
2798 sw->handle = (void *)((long)sw->handle | 1);
2799
Willy Tarreau62f52692017-10-08 23:01:42 +02002800}
2801
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002802static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002803{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002804 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002805 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002806
Willy Tarreau721c9742017-11-07 11:05:42 +01002807 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002808 return;
2809
Willy Tarreau67434202017-11-06 20:20:51 +01002810 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002811 /* we can cleanly close using an empty data frame only after headers */
2812
2813 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2814 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002815 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002816
2817 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002818 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002819 else
2820 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002821 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002822 /* if no outgoing data was seen on this stream, it means it was
2823 * closed with a "tcp-request content" rule that is normally
2824 * used to kill the connection ASAP (eg: limit abuse). In this
2825 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002826 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002827 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002828 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002829 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002830
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002831 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2832 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002833 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002834 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002835
Willy Tarreau00dd0782018-03-01 16:31:34 +01002836 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002837 }
2838
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002839
2840 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002841 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002842 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002843 if (h2s->flags & H2_SF_BLK_MFCTL) {
2844 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2845 h2s->send_wait = sw;
2846 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2847 h2s->send_wait = sw;
2848 LIST_ADDQ(&h2c->send_list, &h2s->list);
2849 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002850 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002851 /* let the handler know we want to shutw */
2852 sw->handle = (void *)((long)(sw->handle) | 2);
2853
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002854}
2855
2856static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2857{
2858 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002859 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002860
2861 if (reason & 1)
2862 h2_do_shutr(h2s);
2863 if (reason & 2)
2864 h2_do_shutw(h2s);
2865
2866 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002867}
2868
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002869static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2870{
2871 struct h2s *h2s = cs->ctx;
2872
2873 if (!mode)
2874 return;
2875
2876 h2_do_shutr(h2s);
2877}
2878
2879static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2880{
2881 struct h2s *h2s = cs->ctx;
2882
2883 h2_do_shutw(h2s);
2884}
2885
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002886/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
2887 * HTX request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2888 * proceed. Stream errors are reported in h2s->errcode and connection errors in
2889 * h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002890 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002891static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002892{
2893 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002895 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002896 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002897 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002898 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002899 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002900 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002901 int flen = h2c->dfl;
2902 int outlen = 0;
2903 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002904 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002905
2906 if (!h2c->dfl) {
2907 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002908 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002909 return 0;
2910 }
2911
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002912 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002913 return 0; // incomplete input frame
2914
Willy Tarreau13278b42017-10-13 19:23:14 +02002915 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002916 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002917 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002918 copy = alloc_trash_chunk();
2919 if (!copy) {
2920 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2921 goto fail;
2922 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002923 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2924 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2925 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002926 }
2927
2928 /* The padlen is the first byte before data, and the padding appears
2929 * after data. padlen+data+padding are included in flen.
2930 */
2931 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002932 h2c->dpl = *hdrs;
2933 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002934 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2935 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002936 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002937 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002938 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002939 hdrs += 1; // skip Pad Length
2940 }
2941
2942 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2943 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002944 if (read_n32(hdrs) == h2s->id) {
2945 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2946 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002947 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002948 }
2949
Willy Tarreau13278b42017-10-13 19:23:14 +02002950 hdrs += 5; // stream dep = 4, weight = 1
2951 flen -= 5;
2952 }
2953
2954 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2955 * don't support this for now and can't even decompress so we have to
2956 * break the connection.
2957 */
2958 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2959 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002960 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002961 }
2962
Olivier Houchard638b7992018-08-16 15:41:52 +02002963 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002964 if (!csbuf) {
2965 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002966 goto fail;
2967 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002968
Willy Tarreau937f7602018-02-26 15:22:17 +01002969 /* we can't retry a failed decompression operation so we must be very
2970 * careful not to take any risks. In practice the output buffer is
2971 * always empty except maybe for trailers, in which case we simply have
2972 * to wait for the upper layer to finish consuming what is available.
2973 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002974
2975 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
2976 htx = htx_from_buf(&h2s->rxbuf);
2977 if (!htx_is_empty(htx))
2978 goto fail;
2979 } else {
2980 if (b_data(csbuf))
2981 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002982
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002983 csbuf->head = 0;
2984 try = b_size(csbuf);
2985 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002986
2987 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2988 sizeof(list)/sizeof(list[0]), tmp);
2989 if (outlen < 0) {
2990 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2991 goto fail;
2992 }
2993
2994 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002995 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002996
2997 if (htx)
2998 outlen = h2_make_htx_request(list, htx, &msgf);
2999 else
3000 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003001
3002 if (outlen < 0) {
3003 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3004 goto fail;
3005 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003006
Willy Tarreau174b06a2018-04-25 18:13:58 +02003007 if (msgf & H2_MSGF_BODY) {
3008 /* a payload is present */
3009 if (msgf & H2_MSGF_BODY_CL)
3010 h2s->flags |= H2_SF_DATA_CLEN;
3011 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
3012 h2s->flags |= H2_SF_DATA_CHNK;
3013 }
3014
Willy Tarreau13278b42017-10-13 19:23:14 +02003015 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003016 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003017 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003018 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003019
Willy Tarreau39d68502018-03-02 12:26:37 +01003020 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003021 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003022 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003023 if (htx)
3024 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003025 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003026
Willy Tarreau68dd9852017-07-03 14:44:26 +02003027 leave:
3028 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003029 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003030 fail:
3031 outlen = 0;
3032 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003033}
3034
Willy Tarreau454f9052017-10-26 19:40:35 +02003035/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3036 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3037 * in use, a new chunk is emitted for each frame. This is supposed to fit
3038 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3039 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3040 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003041 * parser state is automatically updated. Returns > 0 if it could completely
3042 * send the current frame, 0 if it couldn't complete, in which case
3043 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3044 * DATA frame can return 0 as a valid result). Stream errors are reported in
3045 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3046 * have checked the frame header and ensured that the frame was complete or the
3047 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003048 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003049static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003050{
3051 struct h2c *h2c = h2s->h2c;
3052 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003053 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003054 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003055 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003056 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003057
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003058 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003059
3060 /* The padlen is the first byte before data, and the padding appears
3061 * after data. padlen+data+padding are included in flen.
3062 */
Willy Tarreau79127812017-12-03 21:06:59 +01003063 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003064 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003065 return 0;
3066
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003067 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003068 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003069 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3070 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003071 return 0;
3072 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003073
3074 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003075 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003076 h2c->dfl--;
3077 h2c->rcvd_c++; h2c->rcvd_s++;
3078 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003079 }
3080
Olivier Houchard638b7992018-08-16 15:41:52 +02003081 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003082 if (!csbuf) {
3083 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003084 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003085 }
3086
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003087try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003088 flen = h2c->dfl - h2c->dpl;
3089 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003090 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003091
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 if (flen > b_data(&h2c->dbuf)) {
3093 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003094 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003095 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003096 }
3097
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003098 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3099 htx = htx_from_buf(csbuf);
3100 block1 = htx_free_data_space(htx);
3101 if (!block1) {
3102 h2c->flags |= H2_CF_DEM_SFULL;
3103 goto fail;
3104 }
3105 if (flen > block1)
3106 flen = block1;
3107
3108 /* here, flen is the max we can copy into the output buffer */
3109 block1 = b_contig_data(&h2c->dbuf, 0);
3110 if (flen > block1)
3111 flen = block1;
3112
3113 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3114 h2c->flags |= H2_CF_DEM_SFULL;
3115 goto fail;
3116 }
3117
3118 b_del(&h2c->dbuf, flen);
3119 h2c->dfl -= flen;
3120 h2c->rcvd_c += flen;
3121 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3122 goto try_again;
3123 }
3124 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003125 /* it doesn't fit and the buffer is fragmented,
3126 * so let's defragment it and try again.
3127 */
3128 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003129 }
3130
Willy Tarreaueba10f22018-04-25 20:44:22 +02003131 /* chunked-encoding requires more room */
3132 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003133 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003134 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3135 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3136 (chklen < 1048576) ? 4 : 8;
3137 chklen += 4; // CRLF, CRLF
3138 }
3139
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003140 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003141 if (flen + chklen > b_room(csbuf)) {
3142 if (chklen >= b_room(csbuf)) {
3143 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003144 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003145 }
3146 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003147 }
3148
3149 if (h2s->flags & H2_SF_DATA_CHNK) {
3150 /* emit the chunk size */
3151 unsigned int chksz = flen;
3152 char str[10];
3153 char *beg;
3154
3155 beg = str + sizeof(str);
3156 *--beg = '\n';
3157 *--beg = '\r';
3158 do {
3159 *--beg = hextab[chksz & 0xF];
3160 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003161 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003162 }
3163
Willy Tarreau454f9052017-10-26 19:40:35 +02003164 /* Block1 is the length of the first block before the buffer wraps,
3165 * block2 is the optional second block to reach the end of the frame.
3166 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003167 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003168 if (block1 > flen)
3169 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003170 block2 = flen - block1;
3171
3172 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003173 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003174
3175 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003176 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003177
Willy Tarreaueba10f22018-04-25 20:44:22 +02003178 if (h2s->flags & H2_SF_DATA_CHNK) {
3179 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003180 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003181 }
3182
Willy Tarreau454f9052017-10-26 19:40:35 +02003183 /* now mark the input data as consumed (will be deleted from the buffer
3184 * by the caller when seeing FRAME_A after sending the window update).
3185 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003186 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003187 h2c->dfl -= flen;
3188 h2c->rcvd_c += flen;
3189 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3190
3191 if (h2c->dfl > h2c->dpl) {
3192 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003193 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003194 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003195 }
3196
Willy Tarreau4a28da12018-01-04 14:41:00 +01003197 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003198 /* here we're done with the frame, all the payload (except padding) was
3199 * transferred.
3200 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003201
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003202 if (h2c->dff & H2_F_DATA_END_STREAM) {
3203 if (htx) {
3204 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3205 h2c->flags |= H2_CF_DEM_SFULL;
3206 goto fail;
3207 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003208 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003209 else if (h2s->flags & H2_SF_DATA_CHNK) {
3210 /* emit the trailing 0 CRLF CRLF */
3211 if (b_room(csbuf) < 5) {
3212 h2c->flags |= H2_CF_DEM_SFULL;
3213 goto fail;
3214 }
3215 chklen += 5;
3216 b_putblk(csbuf, "0\r\n\r\n", 5);
3217 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003218 }
3219
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003220 h2c->rcvd_c += h2c->dpl;
3221 h2c->rcvd_s += h2c->dpl;
3222 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003223 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3224
Willy Tarreau39d68502018-03-02 12:26:37 +01003225 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003226 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003227 h2s->cs->flags |= CS_FL_REOS;
3228 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003229
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003230 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003231 fail:
3232 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003233}
3234
Willy Tarreau5dd17352018-06-14 13:33:30 +02003235/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3236 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3237 * number of bytes sent. The caller must check the stream's status to detect
3238 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003239 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003240static 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 +02003241{
3242 struct http_hdr list[MAX_HTTP_HDR];
3243 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003244 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003245 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003246 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003247 int es_now = 0;
3248 int ret = 0;
3249 int hdr;
3250
3251 if (h2c_mux_busy(h2c, h2s)) {
3252 h2s->flags |= H2_SF_BLK_MBUSY;
3253 return 0;
3254 }
3255
Willy Tarreau44e973f2018-03-01 17:49:30 +01003256 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003257 h2c->flags |= H2_CF_MUX_MALLOC;
3258 h2s->flags |= H2_SF_BLK_MROOM;
3259 return 0;
3260 }
3261
3262 /* First, try to parse the H1 response and index it into <list>.
3263 * NOTE! Since it comes from haproxy, we *know* that a response header
3264 * block does not wrap and we can safely read it this way without
3265 * having to realign the buffer.
3266 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003267 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003268 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003269 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003270 /* incomplete or invalid response, this is abnormal coming from
3271 * haproxy and may only result in a bad errorfile or bad Lua code
3272 * so that won't be fixed, raise an error now.
3273 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003274 * FIXME: we should instead add the ability to only return a
3275 * 502 bad gateway. But in theory this is not supposed to
3276 * happen.
3277 */
3278 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3279 ret = 0;
3280 goto end;
3281 }
3282
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003283 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003284
3285 /* certain statuses have no body or an empty one, regardless of
3286 * what the headers say.
3287 */
3288 if (sl.st.status >= 100 && sl.st.status < 200) {
3289 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3290 h1m->curr_len = h1m->body_len = 0;
3291 }
3292 else if (sl.st.status == 204 || sl.st.status == 304) {
3293 /* no contents, claim c-len is present and set to zero */
3294 h1m->flags &= ~H1_MF_CHNK;
3295 h1m->flags |= H1_MF_CLEN;
3296 h1m->curr_len = h1m->body_len = 0;
3297 }
3298
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003299 chunk_reset(&outbuf);
3300
3301 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003303 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003304 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003305
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003306 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003307 break;
3308 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003309 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003310 }
3311
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003312 if (outbuf.size < 9)
3313 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003314
3315 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003316 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3317 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3318 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003319
3320 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003321 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003322 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003323 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003325 else if (unlikely(list[0].v.len != 3)) {
3326 /* this is an unparsable response */
3327 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3328 ret = 0;
3329 goto end;
3330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003331 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003332 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003333 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3334 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3335 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3336 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3337 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003338 }
3339 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003340 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003341 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003342 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003343 }
3344
3345 /* encode all headers, stop at empty name */
3346 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003347 /* these ones do not exist in H2 and must be dropped. */
3348 if (isteq(list[hdr].n, ist("connection")) ||
3349 isteq(list[hdr].n, ist("proxy-connection")) ||
3350 isteq(list[hdr].n, ist("keep-alive")) ||
3351 isteq(list[hdr].n, ist("upgrade")) ||
3352 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003353 continue;
3354
3355 if (isteq(list[hdr].n, ist("")))
3356 break; // end
3357
3358 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3359 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003360 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003361 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003362 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003363 }
3364 }
3365
3366 /* we may need to add END_STREAM */
3367 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3368 es_now = 1;
3369
3370 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003371 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003372
3373 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003374 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003375
3376 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003377 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003378
3379 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003380 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003381 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003382
3383 /* for now we don't implemented CONTINUATION, so we wait for a
3384 * body or directly end in TRL2.
3385 */
3386 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003387 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003388 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003389
Willy Tarreau801250e2018-09-11 11:45:04 +02003390 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003391 h2s->flags |= H2_SF_ES_SENT;
3392 if (h2s->st == H2_SS_OPEN)
3393 h2s->st = H2_SS_HLOC;
3394 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003395 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003396 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003397 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003398 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003399 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003400 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003401 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003402 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003403 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003404
3405 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003406
3407 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003408 //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 +02003409 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003410 full:
3411 h1m_init_res(h1m);
3412 h1m->err_pos = -1; // don't care about errors on the response path
3413 h2c->flags |= H2_CF_MUX_MFULL;
3414 h2s->flags |= H2_SF_BLK_MROOM;
3415 ret = 0;
3416 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003417}
3418
Willy Tarreau5dd17352018-06-14 13:33:30 +02003419/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3420 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3421 * the number of bytes sent. The caller must check the stream's status to
3422 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003423 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003424static 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 +02003425{
3426 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003427 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003428 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003429 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003430 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003431 int es_now = 0;
3432 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003433 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003434 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003435
3436 if (h2c_mux_busy(h2c, h2s)) {
3437 h2s->flags |= H2_SF_BLK_MBUSY;
3438 goto end;
3439 }
3440
Willy Tarreau44e973f2018-03-01 17:49:30 +01003441 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003442 h2c->flags |= H2_CF_MUX_MALLOC;
3443 h2s->flags |= H2_SF_BLK_MROOM;
3444 goto end;
3445 }
3446
3447 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003448 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003449 goto end;
3450
3451 chunk_reset(&outbuf);
3452
3453 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003454 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003455 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003456 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003457
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003458 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003459 break;
3460 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003462 }
3463
3464 if (outbuf.size < 9) {
3465 h2c->flags |= H2_CF_MUX_MFULL;
3466 h2s->flags |= H2_SF_BLK_MROOM;
3467 goto end;
3468 }
3469
3470 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003471 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3472 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3473 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003474
3475 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3476 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003477 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003478 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003479 break;
3480 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003481 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003482 if ((long long)size > h1m->curr_len)
3483 size = h1m->curr_len;
3484 break;
3485 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003486 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003487 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003488 if (!ret)
3489 goto end;
3490
3491 if (ret < 0) {
3492 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003493 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003494 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3495 goto end;
3496 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003497 max -= ret;
3498 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003499 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003500 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003501 }
3502
Willy Tarreau801250e2018-09-11 11:45:04 +02003503 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003505 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003506 if (!ret)
3507 goto end;
3508
3509 if (ret < 0) {
3510 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003511 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003512 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3513 goto end;
3514 }
3515
3516 size = chunk;
3517 h1m->curr_len = chunk;
3518 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003519 max -= ret;
3520 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003521 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003522 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523 if (!size)
3524 goto send_empty;
3525 }
3526
3527 /* in MSG_DATA state, continue below */
3528 size = h1m->curr_len;
3529 break;
3530 }
3531
3532 /* we have in <size> the exact number of bytes we need to copy from
3533 * the H1 buffer. We need to check this against the connection's and
3534 * the stream's send windows, and to ensure that this fits in the max
3535 * frame size and in the buffer's available space minus 9 bytes (for
3536 * the frame header). The connection's flow control is applied last so
3537 * that we can use a separate list of streams which are immediately
3538 * unblocked on window opening. Note: we don't implement padding.
3539 */
3540
Willy Tarreau5dd17352018-06-14 13:33:30 +02003541 if (size > max)
3542 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003543
3544 if (size > h2s->mws)
3545 size = h2s->mws;
3546
3547 if (size <= 0) {
3548 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003549 if (h2s->send_wait) {
3550 LIST_DEL(&h2s->list);
3551 LIST_INIT(&h2s->list);
3552 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003553 goto end;
3554 }
3555
3556 if (h2c->mfs && size > h2c->mfs)
3557 size = h2c->mfs;
3558
3559 if (size + 9 > outbuf.size) {
3560 /* we have an opportunity for enlarging the too small
3561 * available space, let's try.
3562 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003563 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003564 goto realign_again;
3565 size = outbuf.size - 9;
3566 }
3567
3568 if (size <= 0) {
3569 h2c->flags |= H2_CF_MUX_MFULL;
3570 h2s->flags |= H2_SF_BLK_MROOM;
3571 goto end;
3572 }
3573
3574 if (size > h2c->mws)
3575 size = h2c->mws;
3576
3577 if (size <= 0) {
3578 h2s->flags |= H2_SF_BLK_MFCTL;
3579 goto end;
3580 }
3581
3582 /* copy whatever we can */
3583 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003584 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003585 if (ret == 1)
3586 len2 = 0;
3587
3588 if (!ret || len1 + len2 < size) {
3589 /* FIXME: must normally never happen */
3590 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3591 goto end;
3592 }
3593
3594 /* limit len1/len2 to size */
3595 if (len1 + len2 > size) {
3596 int sub = len1 + len2 - size;
3597
3598 if (len2 > sub)
3599 len2 -= sub;
3600 else {
3601 sub -= len2;
3602 len2 = 0;
3603 len1 -= sub;
3604 }
3605 }
3606
3607 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003608 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003609 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003610 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003611
3612 send_empty:
3613 /* we may need to add END_STREAM */
3614 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3615 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003616 *
3617 * FIXME: what we do here is not correct because we send end_stream
3618 * before knowing if we'll have to send a HEADERS frame for the
3619 * trailers. More importantly we're not consuming the trailing CRLF
3620 * after the end of trailers, so it will be left to the caller to
3621 * eat it. The right way to do it would be to measure trailers here
3622 * and to send ES only if there are no trailers.
3623 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003624 */
3625 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003626 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003627 es_now = 1;
3628
3629 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003630 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003631
3632 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003633 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003634
3635 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003636 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003637
3638 /* consume incoming H1 response */
3639 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003640 max -= size;
3641 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003642 total += size;
3643 h1m->curr_len -= size;
3644 h2s->mws -= size;
3645 h2c->mws -= size;
3646
3647 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003648 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003649 goto new_frame;
3650 }
3651 }
3652
3653 if (es_now) {
3654 if (h2s->st == H2_SS_OPEN)
3655 h2s->st = H2_SS_HLOC;
3656 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003657 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003658
Willy Tarreau35a62702018-02-27 15:37:25 +01003659 if (!(h1m->flags & H1_MF_CHNK)) {
3660 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003661 total += max;
3662 ofs += max;
3663 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003664
Willy Tarreau801250e2018-09-11 11:45:04 +02003665 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003666 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003667
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003668 h2s->flags |= H2_SF_ES_SENT;
3669 }
3670
3671 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003672 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 +02003673 return total;
3674}
3675
Willy Tarreau115e83b2018-12-01 19:17:53 +01003676/* Try to send a HEADERS frame matching HTX response present in HTX message
3677 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3678 * must check the stream's status to detect any error which might have happened
3679 * subsequently to a successful send. The htx blocks are automatically removed
3680 * from the message. The htx message is assumed to be valid since produced from
3681 * the internal code, hence it contains a start line, an optional series of
3682 * header blocks and an end of header, otherwise an invalid frame could be
3683 * emitted and the resulting htx message could be left in an inconsistent state.
3684 */
3685static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3686{
3687 struct http_hdr list[MAX_HTTP_HDR];
3688 struct h2c *h2c = h2s->h2c;
3689 struct htx_blk *blk;
3690 struct htx_blk *blk_end;
3691 struct buffer outbuf;
3692 struct htx_sl *sl;
3693 enum htx_blk_type type;
3694 int es_now = 0;
3695 int ret = 0;
3696 int hdr;
3697 int idx;
3698
3699 if (h2c_mux_busy(h2c, h2s)) {
3700 h2s->flags |= H2_SF_BLK_MBUSY;
3701 return 0;
3702 }
3703
3704 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3705 h2c->flags |= H2_CF_MUX_MALLOC;
3706 h2s->flags |= H2_SF_BLK_MROOM;
3707 return 0;
3708 }
3709
3710 /* determine the first block which must not be deleted, blk_end may
3711 * be NULL if all blocks have to be deleted.
3712 */
3713 idx = htx_get_head(htx);
3714 blk_end = NULL;
3715 while (idx != -1) {
3716 type = htx_get_blk_type(htx_get_blk(htx, idx));
3717 idx = htx_get_next(htx, idx);
3718 if (type == HTX_BLK_EOH) {
3719 if (idx != -1)
3720 blk_end = htx_get_blk(htx, idx);
3721 break;
3722 }
3723 }
3724
3725 /* get the start line, we do have one */
3726 blk = htx_get_blk(htx, htx->sl_off);
3727 sl = htx_get_blk_ptr(htx, blk);
3728 h2s->status = sl->info.res.status;
3729
3730 /* and the rest of the headers, that we dump starting at header 0 */
3731 hdr = 0;
3732
Willy Tarreaufab9bb02018-12-02 12:11:16 +01003733 idx = htx->sl_off;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003734 while ((idx = htx_get_next(htx, idx)) != -1) {
3735 blk = htx_get_blk(htx, idx);
3736 type = htx_get_blk_type(blk);
3737
3738 if (type == HTX_BLK_UNUSED)
3739 continue;
3740
3741 if (type != HTX_BLK_HDR)
3742 break;
3743
3744 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3745 goto fail;
3746
3747 list[hdr].n = htx_get_blk_name(htx, blk);
3748 list[hdr].v = htx_get_blk_value(htx, blk);
3749
3750#if 1
3751 {
3752 /* FIXME: header names MUST be lower case in H2. For now it's
3753 * not granted by HTX so let's force them now.
3754 */
3755 char *p;
3756 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
3757 if (unlikely(isupper(*p)))
3758 *p = tolower(*p);
3759 }
3760#endif
3761 hdr++;
3762 }
3763
3764 /* marker for end of headers */
3765 list[hdr].n = ist("");
3766
3767 if (h2s->status == 204 || h2s->status == 304) {
3768 /* no contents, claim c-len is present and set to zero */
3769 es_now = 1;
3770 }
3771
3772 chunk_reset(&outbuf);
3773
3774 while (1) {
3775 outbuf.area = b_tail(&h2c->mbuf);
3776 outbuf.size = b_contig_space(&h2c->mbuf);
3777 outbuf.data = 0;
3778
3779 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3780 break;
3781 realign_again:
3782 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3783 }
3784
3785 if (outbuf.size < 9)
3786 goto full;
3787
3788 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3789 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3790 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3791 outbuf.data = 9;
3792
3793 /* encode status, which necessarily is the first one */
3794 if (outbuf.data < outbuf.size && h2s->status == 200)
3795 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3796 else if (outbuf.data < outbuf.size && h2s->status == 304)
3797 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
3798 else if (unlikely(h2s->status < 100 || h2s->status > 999)) {
3799 /* this is an unparsable response */
3800 goto fail;
3801 }
3802 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
3803 /* basic encoding of the status code */
3804 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3805 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3806 outbuf.area[outbuf.data++] = '0' + h2s->status / 100;
3807 outbuf.area[outbuf.data++] = '0' + h2s->status / 10 % 10;
3808 outbuf.area[outbuf.data++] = '0' + h2s->status % 10;
3809 }
3810 else {
3811 if (b_space_wraps(&h2c->mbuf))
3812 goto realign_again;
3813 goto full;
3814 }
3815
3816 /* encode all headers, stop at empty name */
3817 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3818 /* these ones do not exist in H2 and must be dropped. */
3819 if (isteq(list[hdr].n, ist("connection")) ||
3820 isteq(list[hdr].n, ist("proxy-connection")) ||
3821 isteq(list[hdr].n, ist("keep-alive")) ||
3822 isteq(list[hdr].n, ist("upgrade")) ||
3823 isteq(list[hdr].n, ist("transfer-encoding")))
3824 continue;
3825
3826 if (isteq(list[hdr].n, ist("")))
3827 break; // end
3828
3829 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3830 /* output full */
3831 if (b_space_wraps(&h2c->mbuf))
3832 goto realign_again;
3833 goto full;
3834 }
3835 }
3836
3837 /* we may need to add END_STREAM.
3838 * FIXME: we should also set it when we know for sure that the
3839 * content-length is zero as well as on 204/304
3840 */
3841 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3842 es_now = 1;
3843
3844 if (h2s->cs->flags & CS_FL_SHW)
3845 es_now = 1;
3846
3847 /* update the frame's size */
3848 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3849
3850 if (es_now)
3851 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3852
3853 /* commit the H2 response */
3854 b_add(&h2c->mbuf, outbuf.data);
3855 h2s->flags |= H2_SF_HEADERS_SENT;
3856
3857 /* for now we don't implemented CONTINUATION, so we wait for a
3858 * body or directly end in TRL2.
3859 */
3860 if (es_now) {
3861 h2s->flags |= H2_SF_ES_SENT;
3862 if (h2s->st == H2_SS_OPEN)
3863 h2s->st = H2_SS_HLOC;
3864 else
3865 h2s_close(h2s);
3866 }
3867
3868 /* OK we could properly deliver the response */
3869
3870 /* remove all header blocks including the EOH and compute the
3871 * corresponding size.
3872 *
3873 * FIXME: We should remove everything when es_now is set.
3874 */
3875 ret = 0;
3876 idx = htx_get_head(htx);
3877 blk = htx_get_blk(htx, idx);
3878 while (blk != blk_end) {
3879 ret += htx_get_blksz(blk);
3880 blk = htx_remove_blk(htx, blk);
3881 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003882
3883 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3884 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003885 end:
3886 return ret;
3887 full:
3888 h2c->flags |= H2_CF_MUX_MFULL;
3889 h2s->flags |= H2_SF_BLK_MROOM;
3890 ret = 0;
3891 goto end;
3892 fail:
3893 /* unparsable HTX messages, too large ones to be produced in the local
3894 * list etc go here (unrecoverable errors).
3895 */
3896 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3897 ret = 0;
3898 goto end;
3899}
3900
Willy Tarreau0c535fd2018-12-01 19:25:56 +01003901/* Try to send a DATA frame matching HTTP response present in HTX structure
3902 * <htx>, for stream <h2s>. Returns the number of bytes sent. The caller must
3903 * check the stream's status to detect any error which might have happened
3904 * subsequently to a successful send. Returns the number of data bytes
3905 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
3906 */
3907static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct htx *htx)
3908{
3909 struct h2c *h2c = h2s->h2c;
3910 struct buffer outbuf;
3911 size_t total = 0;
3912 int es_now = 0;
3913 int bsize; /* htx block size */
3914 int fsize; /* h2 frame size */
3915 struct htx_blk *blk;
3916 enum htx_blk_type type;
3917 int idx;
3918
3919 if (h2c_mux_busy(h2c, h2s)) {
3920 h2s->flags |= H2_SF_BLK_MBUSY;
3921 goto end;
3922 }
3923
3924 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3925 h2c->flags |= H2_CF_MUX_MALLOC;
3926 h2s->flags |= H2_SF_BLK_MROOM;
3927 goto end;
3928 }
3929
3930 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
3931 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
3932 * the caller to handle.
3933 */
3934
3935 new_frame:
3936 if (htx_is_empty(htx))
3937 goto end;
3938
3939 idx = htx_get_head(htx);
3940 blk = htx_get_blk(htx, idx);
3941 type = htx_get_blk_type(blk); // DATA or EOD or EOM
3942 bsize = htx_get_blksz(blk);
3943 fsize = bsize;
3944
3945 if (type == HTX_BLK_EOD) {
3946 /* if we have an EOD, we're dealing with chunked data. We may
3947 * have a set of trailers after us that the caller will want to
3948 * deal with. Let's simply remove the EOD and return.
3949 */
3950 htx_remove_blk(htx, blk);
3951 // FIXME, it seems we must not return it in the total bytes count?
3952 //total++; // EOD counts as one byte
3953 goto end;
3954 }
3955
3956 /* for DATA and EOM we'll have to emit a frame, even if empty */
3957
3958 while (1) {
3959 outbuf.area = b_tail(&h2c->mbuf);
3960 outbuf.size = b_contig_space(&h2c->mbuf);
3961 outbuf.data = 0;
3962
3963 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3964 break;
3965 realign_again:
3966 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3967 }
3968
3969 if (outbuf.size < 9) {
3970 h2c->flags |= H2_CF_MUX_MFULL;
3971 h2s->flags |= H2_SF_BLK_MROOM;
3972 goto end;
3973 }
3974
3975 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3976 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3977 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3978 outbuf.data = 9;
3979
3980 /* we have in <fsize> the exact number of bytes we need to copy from
3981 * the HTX buffer. We need to check this against the connection's and
3982 * the stream's send windows, and to ensure that this fits in the max
3983 * frame size and in the buffer's available space minus 9 bytes (for
3984 * the frame header). The connection's flow control is applied last so
3985 * that we can use a separate list of streams which are immediately
3986 * unblocked on window opening. Note: we don't implement padding.
3987 */
3988
3989 /* EOM is presented with bsize==1 but would lead to the emission of an
3990 * empty frame, thus we force it to zero here.
3991 */
3992 if (type == HTX_BLK_EOM)
3993 bsize = fsize = 0;
3994
3995 if (!fsize)
3996 goto send_empty;
3997
3998 if (h2s->mws <= 0) {
3999 h2s->flags |= H2_SF_BLK_SFCTL;
4000 if (h2s->send_wait) {
4001 LIST_DEL(&h2s->list);
4002 LIST_INIT(&h2s->list);
4003 }
4004 goto end;
4005 }
4006
4007 if (fsize > h2s->mws)
4008 fsize = h2s->mws; // >0
4009
4010 if (h2c->mfs && fsize > h2c->mfs)
4011 fsize = h2c->mfs; // >0
4012
4013 if (fsize + 9 > outbuf.size) {
4014 /* we have an opportunity for enlarging the too small
4015 * available space, let's try.
4016 * FIXME: is this really interesting to do? Maybe we'll
4017 * spend lots of time realigning instead of using two
4018 * frames.
4019 */
4020 if (b_space_wraps(&h2c->mbuf))
4021 goto realign_again;
4022 fsize = outbuf.size - 9;
4023
4024 if (fsize <= 0) {
4025 /* no need to send an empty frame here */
4026 h2c->flags |= H2_CF_MUX_MFULL;
4027 h2s->flags |= H2_SF_BLK_MROOM;
4028 goto end;
4029 }
4030 }
4031
4032 if (h2c->mws <= 0) {
4033 h2s->flags |= H2_SF_BLK_MFCTL;
4034 goto end;
4035 }
4036
4037 if (fsize > h2c->mws)
4038 fsize = h2c->mws;
4039
4040 /* now let's copy this this into the output buffer */
4041 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
4042
4043 send_empty:
4044 /* update the frame's size */
4045 h2_set_frame_size(outbuf.area, fsize);
4046
4047 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4048 * meeting EOM. We should optimize this later.
4049 */
4050 if (type == HTX_BLK_EOM) {
4051 // FIXME, it seems we must not return it in the total bytes count?
4052 // total++; // EOM counts as one byte
4053 es_now = 1;
4054 }
4055
4056 if (es_now)
4057 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4058
4059 /* commit the H2 response */
4060 b_add(&h2c->mbuf, fsize + 9);
4061
4062 /* consume incoming HTX block, including EOM */
4063 total += fsize;
4064 if (fsize == bsize) {
4065 htx_remove_blk(htx, blk);
4066 if (fsize)
4067 goto new_frame;
4068 } else {
4069 /* we've truncated this block */
4070 htx_cut_data_blk(htx, blk, fsize);
4071 }
4072
4073 if (es_now) {
4074 if (h2s->st == H2_SS_OPEN)
4075 h2s->st = H2_SS_HLOC;
4076 else
4077 h2s_close(h2s);
4078
4079 h2s->flags |= H2_SF_ES_SENT;
4080 }
4081
4082 end:
4083 return total;
4084}
4085
Olivier Houchard6ff20392018-07-17 18:46:31 +02004086/* Called from the upper layer, to subscribe to events, such as being able to send */
4087static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4088{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004089 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004090 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004091 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004092
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004093 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004094 sw = param;
4095 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4096 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004097 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004098 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004099 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004100 event_type &= ~SUB_CAN_RECV;
4101 }
4102 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004103 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004104 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004105 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004106 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004107 h2s->send_wait = sw;
4108 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4109 if (h2s->flags & H2_SF_BLK_MFCTL)
4110 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4111 else
4112 LIST_ADDQ(&h2c->send_list, &h2s->list);
4113 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004114 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004115 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004116 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004117 if (event_type != 0)
4118 return -1;
4119 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004120
4121
4122}
4123
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004124static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4125{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004126 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004127 struct h2s *h2s = cs->ctx;
4128
4129 if (event_type & SUB_CAN_RECV) {
4130 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004131 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004132 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004133 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004134 }
4135 }
4136 if (event_type & SUB_CAN_SEND) {
4137 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004138 if (h2s->send_wait == sw) {
4139 LIST_DEL(&h2s->list);
4140 LIST_INIT(&h2s->list);
4141 sw->wait_reason &= ~SUB_CAN_SEND;
4142 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004143 }
4144 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004145 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4146 sw = param;
4147 if (h2s->send_wait == sw) {
4148 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4149 h2s->send_wait = NULL;
4150 }
4151 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004152 return 0;
4153}
4154
4155
Olivier Houchard511efea2018-08-16 15:30:32 +02004156/* Called from the upper layer, to receive data */
4157static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4158{
Olivier Houchard638b7992018-08-16 15:41:52 +02004159 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004160 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004161 struct htx *h2s_htx = NULL;
4162 struct htx *buf_htx = NULL;
4163 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004164 size_t ret = 0;
4165
4166 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004167 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4168 /* in HTX mode we ignore the count argument */
4169 h2s_htx = htx_from_buf(&h2s->rxbuf);
4170 if (htx_is_empty(h2s_htx))
4171 goto end;
4172
4173 buf_htx = htx_from_buf(buf);
4174 count = htx_free_space(buf_htx);
4175
4176 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, (h2s_htx->sl_off != -1) ? HTX_BLK_EOH : HTX_BLK_EOM);
4177
4178 buf_htx->extra = h2s_htx->extra;
4179 if (htx_is_not_empty(buf_htx))
4180 b_set_data(buf, b_size(buf));
4181 ret = htx_ret.ret;
4182 }
4183 else {
4184 ret = b_xfer(buf, &h2s->rxbuf, count);
4185 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004186
Olivier Houchard638b7992018-08-16 15:41:52 +02004187 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02004188 cs->flags |= CS_FL_RCV_MORE;
4189 else {
4190 cs->flags &= ~CS_FL_RCV_MORE;
4191 if (cs->flags & CS_FL_REOS)
4192 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004193 if (b_size(&h2s->rxbuf)) {
4194 b_free(&h2s->rxbuf);
4195 offer_buffers(NULL, tasks_run_queue);
4196 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004197 }
4198
Willy Tarreau082f5592018-11-25 08:03:32 +01004199 if (ret && h2c->dsi == h2s->id) {
4200 /* demux is blocking on this stream's buffer */
4201 h2c->flags &= ~H2_CF_DEM_SFULL;
4202 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4203 if (h2_recv_allowed(h2c))
4204 tasklet_wakeup(h2c->wait_event.task);
4205 }
4206 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004207end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004208 return ret;
4209}
4210
Olivier Houchardd846c262018-10-19 17:24:29 +02004211static void h2_stop_senders(struct h2c *h2c)
4212{
4213 struct h2s *h2s, *h2s_back;
4214
4215 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4216 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4217 if (h2c->msi == h2s_id(h2s))
4218 continue;
4219 LIST_DEL(&h2s->list);
4220 LIST_INIT(&h2s->list);
4221 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4222 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4223 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4224 LIST_ADD(&h2c->send_list, &h2s->list);
4225 }
4226}
4227
Willy Tarreau62f52692017-10-08 23:01:42 +02004228/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004229static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004230{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004231 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004232 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004233 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004234 struct htx *htx;
4235 struct htx_blk *blk;
4236 enum htx_blk_type btype;
4237 uint32_t bsize;
4238 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004239
Olivier Houchardd846c262018-10-19 17:24:29 +02004240 if (h2s->send_wait) {
4241 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4242 h2s->send_wait = NULL;
4243 LIST_DEL(&h2s->list);
4244 LIST_INIT(&h2s->list);
4245 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004246 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4247 return 0;
4248
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004249 /* htx will be enough to decide if we're using HTX or legacy */
4250 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4251
Willy Tarreau0bad0432018-06-14 16:54:01 +02004252 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004253 h2s->flags |= H2_SF_OUTGOING_DATA;
4254
Willy Tarreau751f2d02018-10-05 09:35:00 +02004255 if (h2s->id == 0) {
4256 int32_t id = h2c_get_next_sid(h2s->h2c);
4257
4258 if (id < 0) {
4259 cs->ctx = NULL;
4260 cs->flags |= CS_FL_ERROR;
4261 h2s_destroy(h2s);
4262 return 0;
4263 }
4264
4265 eb32_delete(&h2s->by_id);
4266 h2s->by_id.key = h2s->id = id;
4267 h2s->h2c->max_id = id;
4268 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4269 }
4270
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004271 if (htx) {
4272 while (count && !htx_is_empty(htx)) {
4273 idx = htx_get_head(htx);
4274 blk = htx_get_blk(htx, idx);
4275 btype = htx_get_blk_type(blk);
4276 bsize = htx_get_blksz(blk);
4277
4278 switch (btype) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004279 case HTX_BLK_RES_SL:
4280 /* start-line before headers */
4281 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4282 if (ret > 0) {
4283 total += ret;
4284 count -= ret;
4285 if (ret < bsize)
4286 goto done;
4287 }
4288 break;
4289
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004290 case HTX_BLK_DATA:
4291 case HTX_BLK_EOD:
4292 case HTX_BLK_EOM:
4293 /* all these cause the emission of a DATA frame (possibly empty) */
4294 ret = h2s_htx_frt_make_resp_data(h2s, htx);
4295 if (ret > 0) {
4296 total += ret;
4297 count -= ret;
4298 if (ret < bsize)
4299 goto done;
4300 }
4301 break;
4302
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004303 default:
4304 htx_remove_blk(htx, blk);
4305 total += bsize;
4306 count -= bsize;
4307 break;
4308 }
4309 }
4310 goto done;
4311 }
4312
4313 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004314 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004315 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004316 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004317 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004318 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004319 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004320 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004321 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004322 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004323 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004324
Willy Tarreau5dd17352018-06-14 13:33:30 +02004325 if (unlikely((int)ret <= 0)) {
4326 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004327 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4328 break;
4329 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004330 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004331 total += count;
4332 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004333 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004334 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004335 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004336 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004337 cs->flags |= CS_FL_ERROR;
4338 break;
4339 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004340
4341 total += ret;
4342 count -= ret;
4343
4344 if (h2s->st >= H2_SS_ERROR)
4345 break;
4346
4347 if (h2s->flags & H2_SF_BLK_ANY)
4348 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004349 }
4350
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004351 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004352 if (h2s->st >= H2_SS_ERROR) {
4353 /* trim any possibly pending data after we close (extra CR-LF,
4354 * unprocessed trailers, abnormal extra data, ...)
4355 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004356 total += count;
4357 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004358 }
4359
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004360 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004361 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004362 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004363 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004364 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004365 }
4366
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004367 if (htx) {
4368 if (htx_is_empty(htx)) {
4369 htx_reset(htx);
4370 b_set_data(buf, 0);
4371 }
4372 } else {
4373 b_del(buf, total);
4374 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004375
4376 /* The mux is full, cancel the pending tasks */
4377 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4378 (h2s->flags & H2_SF_BLK_MBUSY))
4379 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004380
Olivier Houchard7505f942018-08-21 18:10:44 +02004381 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004382 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4383 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004384
Olivier Houchard7505f942018-08-21 18:10:44 +02004385 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004386 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004387}
4388
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004389/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004390static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004391{
4392 struct h2c *h2c = conn->mux_ctx;
4393 struct h2s *h2s;
4394 struct eb32_node *node;
4395 int fctl_cnt = 0;
4396 int send_cnt = 0;
4397 int tree_cnt = 0;
4398 int orph_cnt = 0;
4399
4400 if (!h2c)
4401 return;
4402
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004403 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004404 fctl_cnt++;
4405
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004406 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004407 send_cnt++;
4408
4409 node = eb32_first(&h2c->streams_by_id);
4410 while (node) {
4411 h2s = container_of(node, struct h2s, by_id);
4412 tree_cnt++;
4413 if (!h2s->cs)
4414 orph_cnt++;
4415 node = eb32_next(node);
4416 }
4417
Willy Tarreau616ac812018-07-24 14:12:42 +02004418 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4419 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4420 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4421 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4422 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4423 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004424}
Willy Tarreau62f52692017-10-08 23:01:42 +02004425
4426/*******************************************************/
4427/* functions below are dedicated to the config parsers */
4428/*******************************************************/
4429
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004430/* config parser for global "tune.h2.header-table-size" */
4431static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4432 struct proxy *defpx, const char *file, int line,
4433 char **err)
4434{
4435 if (too_many_args(1, args, err, NULL))
4436 return -1;
4437
4438 h2_settings_header_table_size = atoi(args[1]);
4439 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4440 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4441 return -1;
4442 }
4443 return 0;
4444}
Willy Tarreau62f52692017-10-08 23:01:42 +02004445
Willy Tarreaue6baec02017-07-27 11:45:11 +02004446/* config parser for global "tune.h2.initial-window-size" */
4447static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4448 struct proxy *defpx, const char *file, int line,
4449 char **err)
4450{
4451 if (too_many_args(1, args, err, NULL))
4452 return -1;
4453
4454 h2_settings_initial_window_size = atoi(args[1]);
4455 if (h2_settings_initial_window_size < 0) {
4456 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4457 return -1;
4458 }
4459 return 0;
4460}
4461
Willy Tarreau5242ef82017-07-27 11:47:28 +02004462/* config parser for global "tune.h2.max-concurrent-streams" */
4463static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4464 struct proxy *defpx, const char *file, int line,
4465 char **err)
4466{
4467 if (too_many_args(1, args, err, NULL))
4468 return -1;
4469
4470 h2_settings_max_concurrent_streams = atoi(args[1]);
4471 if (h2_settings_max_concurrent_streams < 0) {
4472 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4473 return -1;
4474 }
4475 return 0;
4476}
4477
Willy Tarreau62f52692017-10-08 23:01:42 +02004478
4479/****************************************/
4480/* MUX initialization and instanciation */
4481/***************************************/
4482
4483/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004484static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004485 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004486 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004487 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004488 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004489 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004490 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004491 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004492 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004493 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004494 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004495 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004496 .shutr = h2_shutr,
4497 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004498 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004499 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004500 .name = "H2",
4501};
4502
Christopher Faulet32f61c02018-04-10 14:33:41 +02004503/* PROTO selection : this mux registers PROTO token "h2" */
4504static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004505 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004506
Willy Tarreau0108d902018-11-25 19:14:37 +01004507INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4508
Willy Tarreauf8957272018-10-03 10:25:20 +02004509static struct mux_proto_list mux_proto_h2_htx =
4510 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4511
4512INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4513
Willy Tarreau62f52692017-10-08 23:01:42 +02004514/* config keyword parsers */
4515static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004516 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004517 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004518 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004519 { 0, NULL, NULL }
4520}};
4521
Willy Tarreau0108d902018-11-25 19:14:37 +01004522INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);