blob: 001d1dad8fe52d3db528c9e46a703e649bdc07b3 [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 Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* Connection flags (32 bit), in h2c->flags */
33#define H2_CF_NONE 0x00000000
34
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020035/* Flags indicating why writing to the mux is blocked. */
36#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
37#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
38#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
39
Willy Tarreau315d8072017-12-10 22:17:57 +010040/* Flags indicating why writing to the demux is blocked.
41 * The first two ones directly affect the ability for the mux to receive data
42 * from the connection. The other ones affect the mux's ability to demux
43 * received data.
44 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020045#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
46#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010047
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020048#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
49#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
50#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
51#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020052#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
53#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054
Willy Tarreau081d4722017-05-16 21:51:05 +020055/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
57#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
58#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 +020059#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020060
Willy Tarreau5ab6b572017-09-22 08:05:00 +020061/* H2 connection state, in h2c->st0 */
62enum h2_cs {
63 H2_CS_PREFACE, // init done, waiting for connection preface
64 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
65 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
66 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010067 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
68 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020069 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
70 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
71 H2_CS_ENTRIES // must be last
72} __attribute__((packed));
73
74/* H2 connection descriptor */
75struct h2c {
76 struct connection *conn;
77
78 enum h2_cs st0; /* mux state */
79 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
80
81 /* 16 bit hole here */
82 uint32_t flags; /* connection flags: H2_CF_* */
83 int32_t max_id; /* highest ID known on this connection, <0 before preface */
84 uint32_t rcvd_c; /* newly received data to ACK for the connection */
85 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
86
87 /* states for the demux direction */
88 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020089 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020090
91 int32_t dsi; /* demux stream ID (<0 = idle) */
92 int32_t dfl; /* demux frame length (if dsi >= 0) */
93 int8_t dft; /* demux frame type (if dsi >= 0) */
94 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010095 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
96 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020097 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
98
99 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200100 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t msi; /* mux stream ID (<0 = idle) */
102 int32_t mfl; /* mux frame length (if dsi >= 0) */
103 int8_t mft; /* mux frame type (if dsi >= 0) */
104 int8_t mff; /* mux frame flags (if dsi >= 0) */
105 /* 16 bit hole here */
106 int32_t miw; /* mux initial window size for all new streams */
107 int32_t mws; /* mux window size. Can be negative. */
108 int32_t mfs; /* mux's max frame size */
109
Willy Tarreauea392822017-10-31 10:02:25 +0100110 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100111 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100112 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200113 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200114 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100115 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200116 struct eb_root streams_by_id; /* all active streams by their ID */
117 struct list send_list; /* list of blocked streams requesting to send */
118 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200119 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100120 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200121 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200122};
123
Willy Tarreau18312642017-10-11 07:57:07 +0200124/* H2 stream state, in h2s->st */
125enum h2_ss {
126 H2_SS_IDLE = 0, // idle
127 H2_SS_RLOC, // reserved(local)
128 H2_SS_RREM, // reserved(remote)
129 H2_SS_OPEN, // open
130 H2_SS_HREM, // half-closed(remote)
131 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200132 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200133 H2_SS_CLOSED, // closed
134 H2_SS_ENTRIES // must be last
135} __attribute__((packed));
136
137/* HTTP/2 stream flags (32 bit), in h2s->flags */
138#define H2_SF_NONE 0x00000000
139#define H2_SF_ES_RCVD 0x00000001
140#define H2_SF_ES_SENT 0x00000002
141
142#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
143#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
144
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200145/* stream flags indicating the reason the stream is blocked */
146#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
147#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
148#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
149#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
150#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
151
Willy Tarreau454f9052017-10-26 19:40:35 +0200152/* stream flags indicating how data is supposed to be sent */
153#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
154#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
155
156/* step we're currently in when sending chunks. This is needed because we may
157 * have to transfer chunks as large as a full buffer so there's no room left
158 * for size nor crlf around.
159 */
160#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
161#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
162#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
163
164#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
165
Willy Tarreau67434202017-11-06 20:20:51 +0100166#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100167#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100168
Willy Tarreau18312642017-10-11 07:57:07 +0200169/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
170 * it is being processed in the internal HTTP representation (H1 for now).
171 */
172struct h2s {
173 struct conn_stream *cs;
174 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200175 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200176 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200177 int32_t id; /* stream ID */
178 uint32_t flags; /* H2_SF_* */
179 int mws; /* mux window size for this stream */
180 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
181 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200182 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200183 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200184 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
185 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
186 struct wait_event *send_wait; /* The streeam is waiting for flow control */
187 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200188};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200189
Willy Tarreauc6405142017-09-21 20:23:50 +0200190/* descriptor for an h2 frame header */
191struct h2_fh {
192 uint32_t len; /* length, host order, 24 bits */
193 uint32_t sid; /* stream id, host order, 31 bits */
194 uint8_t ft; /* frame type */
195 uint8_t ff; /* frame flags */
196};
197
Willy Tarreau8ceae722018-11-26 11:58:30 +0100198/* the h2c connection pool */
199DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
200
201/* the h2s stream pool */
202DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
203
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200204/* a few settings from the global section */
205static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200206static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200207static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200208
Willy Tarreau2a856182017-05-16 15:20:39 +0200209/* a dmumy closed stream */
210static const struct h2s *h2_closed_stream = &(const struct h2s){
211 .cs = NULL,
212 .h2c = NULL,
213 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100214 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100215 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200216 .id = 0,
217};
218
219/* and a dummy idle stream for use with any unannounced stream */
220static const struct h2s *h2_idle_stream = &(const struct h2s){
221 .cs = NULL,
222 .h2c = NULL,
223 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100224 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200225 .id = 0,
226};
227
Olivier Houchard9f6af332018-05-25 14:04:04 +0200228static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200229static int h2_send(struct h2c *h2c);
230static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200231static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200232static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100233static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100234static int h2_frt_decode_headers(struct h2s *h2s);
235static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200236static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200237
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200238/*****************************************************/
239/* functions below are for dynamic buffer management */
240/*****************************************************/
241
Willy Tarreau315d8072017-12-10 22:17:57 +0100242/* indicates whether or not the we may call the h2_recv() function to attempt
243 * to receive data into the buffer and/or demux pending data. The condition is
244 * a bit complex due to some API limits for now. The rules are the following :
245 * - if an error or a shutdown was detected on the connection and the buffer
246 * is empty, we must not attempt to receive
247 * - if the demux buf failed to be allocated, we must not try to receive and
248 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100249 * - if no flag indicates a blocking condition, we may attempt to receive,
250 * regardless of whether the demux buffer is full or not, so that only
251 * de demux part decides whether or not to block. This is needed because
252 * the connection API indeed prevents us from re-enabling receipt that is
253 * already enabled in a polled state, so we must always immediately stop
254 * as soon as the demux can't proceed so as never to hit an end of read
255 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100256 * - otherwise must may not attempt
257 */
258static inline int h2_recv_allowed(const struct h2c *h2c)
259{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200260 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100261 (h2c->st0 >= H2_CS_ERROR ||
262 h2c->conn->flags & CO_FL_ERROR ||
263 conn_xprt_read0_pending(h2c->conn)))
264 return 0;
265
266 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100267 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100268 return 1;
269
270 return 0;
271}
272
Willy Tarreauf2101912018-07-19 10:11:38 +0200273/* returns true if the connection has too many conn_streams attached */
274static inline int h2_has_too_many_cs(const struct h2c *h2c)
275{
276 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
277}
278
Willy Tarreau44e973f2018-03-01 17:49:30 +0100279/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
280 * flags are used to figure what buffer was requested. It returns 1 if the
281 * allocation succeeds, in which case the connection is woken up, or 0 if it's
282 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100284static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200285{
286 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100287 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200288
Willy Tarreau44e973f2018-03-01 17:49:30 +0100289 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200290 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200291 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200292 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200293 return 1;
294 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200295
Willy Tarreau44e973f2018-03-01 17:49:30 +0100296 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
297 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200298
299 if (h2c->flags & H2_CF_DEM_MROOM) {
300 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200301 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200302 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200303 }
Willy Tarreau14398122017-09-22 14:26:04 +0200304 return 1;
305 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100306
307 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
308 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200309 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100310 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200311 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200312 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 return 1;
314 }
315
Willy Tarreau14398122017-09-22 14:26:04 +0200316 return 0;
317}
318
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200319static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200320{
321 struct buffer *buf = NULL;
322
Willy Tarreau44e973f2018-03-01 17:49:30 +0100323 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
324 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
325 h2c->buf_wait.target = h2c;
326 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200330 __conn_xprt_stop_recv(h2c->conn);
331 }
332 return buf;
333}
334
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200335static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200336{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100338 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200339 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200340 }
341}
342
Olivier Houchardd540b362018-11-05 18:37:53 +0100343static int h2_avail_streams(struct connection *conn)
344{
345 struct h2c *h2c = conn->mux_ctx;
346
347 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
348}
349
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200350
Willy Tarreau62f52692017-10-08 23:01:42 +0200351/*****************************************************************/
352/* functions below are dedicated to the mux setup and management */
353/*****************************************************************/
354
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200355/* Initialize the mux once it's attached. For outgoing connections, the context
356 * is already initialized before installing the mux, so we detect incoming
357 * connections from the fact that the context is still NULL. Returns < 0 on
358 * error.
359 */
360static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200361{
362 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100363 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200365 /* we don't support outgoing connections for now */
366 if (conn->mux_ctx)
367 goto fail_no_h2c;
368
Willy Tarreaubafbe012017-11-24 17:34:44 +0100369 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200370 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200371 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372
Willy Tarreau0b37d652018-10-03 10:33:02 +0200373 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
374 if (tick_isset(prx->timeout.clientfin))
375 h2c->shut_timeout = prx->timeout.clientfin;
Willy Tarreau3f133572017-10-31 19:21:06 +0100376
Willy Tarreau0b37d652018-10-03 10:33:02 +0200377 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100378 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100379 if (tick_isset(h2c->timeout)) {
380 t = task_new(tid_bit);
381 if (!t)
382 goto fail;
383
384 h2c->task = t;
385 t->process = h2_timeout_task;
386 t->context = h2c;
387 t->expire = tick_add(now_ms, h2c->timeout);
388 }
Willy Tarreauea392822017-10-31 10:02:25 +0100389
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200390 h2c->wait_event.task = tasklet_new();
391 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200392 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200393 h2c->wait_event.task->process = h2_io_cb;
394 h2c->wait_event.task->context = h2c;
395 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200396
Willy Tarreau32218eb2017-09-22 08:07:25 +0200397 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
398 if (!h2c->ddht)
399 goto fail;
400
401 /* Initialise the context. */
402 h2c->st0 = H2_CS_PREFACE;
403 h2c->conn = conn;
404 h2c->max_id = -1;
405 h2c->errcode = H2_ERR_NO_ERROR;
406 h2c->flags = H2_CF_NONE;
407 h2c->rcvd_c = 0;
408 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100409 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200410 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200412 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413 h2c->dsi = -1;
414 h2c->msi = -1;
415 h2c->last_sid = -1;
416
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200417 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200418 h2c->miw = 65535; /* mux initial window size */
419 h2c->mws = 65535; /* mux window size */
420 h2c->mfs = 16384; /* initial max frame size */
421 h2c->streams_by_id = EB_ROOT_UNIQUE;
422 LIST_INIT(&h2c->send_list);
423 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200424 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100425 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 conn->mux_ctx = h2c;
427
Willy Tarreau3f133572017-10-31 19:21:06 +0100428 if (t)
429 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100430
Willy Tarreau0f383582018-10-03 14:22:21 +0200431 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200432 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200434 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100435 if (t)
436 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200437 if (h2c->wait_event.task)
438 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100439 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200440 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441 return -1;
442}
443
Willy Tarreau2373acc2017-10-12 17:35:14 +0200444/* returns the stream associated with id <id> or NULL if not found */
445static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
446{
447 struct eb32_node *node;
448
Willy Tarreau2a856182017-05-16 15:20:39 +0200449 if (id > h2c->max_id)
450 return (struct h2s *)h2_idle_stream;
451
Willy Tarreau2373acc2017-10-12 17:35:14 +0200452 node = eb32_lookup(&h2c->streams_by_id, id);
453 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200454 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200455
456 return container_of(node, struct h2s, by_id);
457}
458
Willy Tarreau62f52692017-10-08 23:01:42 +0200459/* release function for a connection. This one should be called to free all
460 * resources allocated to the mux.
461 */
462static void h2_release(struct connection *conn)
463{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200464 struct h2c *h2c = conn->mux_ctx;
465
466 LIST_DEL(&conn->list);
467
468 if (h2c) {
469 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200470
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100471 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100472 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200474
Willy Tarreau44e973f2018-03-01 17:49:30 +0100475 h2_release_buf(h2c, &h2c->dbuf);
476 h2_release_buf(h2c, &h2c->mbuf);
477
Willy Tarreauea392822017-10-31 10:02:25 +0100478 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200479 h2c->task->context = NULL;
480 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100481 h2c->task = NULL;
482 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200483 if (h2c->wait_event.task)
484 tasklet_free(h2c->wait_event.task);
485 if (h2c->wait_event.wait_reason != 0)
486 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
487 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100488
Willy Tarreaubafbe012017-11-24 17:34:44 +0100489 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200490 }
491
492 conn->mux = NULL;
493 conn->mux_ctx = NULL;
494
495 conn_stop_tracking(conn);
496 conn_full_close(conn);
497 if (conn->destroy_cb)
498 conn->destroy_cb(conn);
499 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200500}
501
502
Willy Tarreau71681172017-10-23 14:39:06 +0200503/******************************************************/
504/* functions below are for the H2 protocol processing */
505/******************************************************/
506
507/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100508static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200509{
510 return h2s ? h2s->id : 0;
511}
512
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200513/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100514static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200515{
516 if (h2c->msi < 0)
517 return 0;
518
519 if (h2c->msi == h2s_id(h2s))
520 return 0;
521
522 return 1;
523}
524
Willy Tarreau741d6df2017-10-17 08:00:59 +0200525/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100526static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200527{
528 h2c->errcode = err;
529 h2c->st0 = H2_CS_ERROR;
530}
531
Willy Tarreau2e43f082017-10-17 08:03:59 +0200532/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100533static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200534{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200535 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200536 h2s->errcode = err;
537 h2s->st = H2_SS_ERROR;
538 if (h2s->cs)
539 h2s->cs->flags |= CS_FL_ERROR;
540 }
541}
542
Willy Tarreaue4820742017-07-27 13:37:23 +0200543/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100544static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200545{
546 uint8_t *out = frame;
547
548 *out = len >> 16;
549 write_n16(out + 1, len);
550}
551
Willy Tarreau54c15062017-10-10 17:10:03 +0200552/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
553 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
554 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200555 * available in the buffer's input prior to calling this function. The buffer
556 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200557 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100558static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200559 const struct buffer *b, int o)
560{
Willy Tarreau591d4452018-06-15 17:21:00 +0200561 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 +0200562}
563
Willy Tarreau1f094672017-11-20 21:27:45 +0100564static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200565{
Willy Tarreau591d4452018-06-15 17:21:00 +0200566 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200567}
568
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200570{
Willy Tarreau591d4452018-06-15 17:21:00 +0200571 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200572}
573
Willy Tarreau1f094672017-11-20 21:27:45 +0100574static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200575{
Willy Tarreau591d4452018-06-15 17:21:00 +0200576 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200577}
578
579
Willy Tarreau715d5312017-07-11 15:20:24 +0200580/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
581 * is not obvious. It turns out that H2 headers are neither aligned nor do they
582 * use regular sizes. And to add to the trouble, the buffer may wrap so each
583 * byte read must be checked. The header is formed like this :
584 *
585 * b0 b1 b2 b3 b4 b5..b8
586 * +----------+---------+--------+----+----+----------------------+
587 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
588 * +----------+---------+--------+----+----+----------------------+
589 *
590 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
591 * we get the sid properly aligned and ordered, and 16 bits of len properly
592 * ordered as well. The type and flags can be extracted using bit shifts from
593 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200594 * Returns zero if some bytes are missing, otherwise non-zero on success. The
595 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200596 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100597static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200598{
599 uint64_t w;
600
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200601 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200602 return 0;
603
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200604 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200605 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200606 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
607 h->ff = w >> 32;
608 h->ft = w >> 40;
609 h->len += w >> 48;
610 return 1;
611}
612
613/* skip the next 9 bytes corresponding to the frame header possibly parsed by
614 * h2_peek_frame_hdr() above.
615 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100616static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200617{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200618 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200619}
620
621/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100622static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200623{
624 int ret;
625
626 ret = h2_peek_frame_hdr(b, h);
627 if (ret > 0)
628 h2_skip_frame_hdr(b);
629 return ret;
630}
631
Willy Tarreau00dd0782018-03-01 16:31:34 +0100632/* marks stream <h2s> as CLOSED and decrement the number of active streams for
633 * its connection if the stream was not yet closed. Please use this exclusively
634 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100635 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100636static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100637{
638 if (h2s->st != H2_SS_CLOSED)
639 h2s->h2c->nb_streams--;
640 h2s->st = H2_SS_CLOSED;
641}
642
Willy Tarreau71049cc2018-03-28 13:56:39 +0200643/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
644static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100645{
646 h2s_close(h2s);
647 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200648 if (b_size(&h2s->rxbuf)) {
649 b_free(&h2s->rxbuf);
650 offer_buffers(NULL, tasks_run_queue);
651 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200652 if (h2s->send_wait != NULL)
653 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
654 if (h2s->recv_wait != NULL)
655 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
656 /* There's no need to explicitely call unsubscribe here, the only
657 * reference left would be in the h2c send_list/fctl_list, and if
658 * we're in it, we're getting out anyway
659 */
660 LIST_DEL(&h2s->list);
661 LIST_INIT(&h2s->list);
662 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100663 pool_free(pool_head_h2s, h2s);
664}
665
Willy Tarreaua8e49542018-10-03 18:53:55 +0200666/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
667 * stream tree. In case of error, nothing is added and NULL is returned. The
668 * causes of errors can be any failed memory allocation. The caller is
669 * responsible for checking if the connection may support an extra stream
670 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200671 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200672static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674 struct h2s *h2s;
675
Willy Tarreaubafbe012017-11-24 17:34:44 +0100676 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200677 if (!h2s)
678 goto out;
679
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200680 h2s->wait_event.task = tasklet_new();
681 if (!h2s->wait_event.task) {
682 pool_free(pool_head_h2s, h2s);
683 goto out;
684 }
685 h2s->send_wait = NULL;
686 h2s->recv_wait = NULL;
687 h2s->wait_event.task->process = h2_deferred_shut;
688 h2s->wait_event.task->context = h2s;
689 h2s->wait_event.handle = NULL;
690 h2s->wait_event.wait_reason = 0;
691 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200692 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200693 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200694 h2s->mws = h2c->miw;
695 h2s->flags = H2_SF_NONE;
696 h2s->errcode = H2_ERR_NO_ERROR;
697 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200698 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200699 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200700 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200701 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200702 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200703 h2s->by_id.key = h2s->id = id;
704 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200705
706 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100707 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200708
709 return h2s;
710
711 out_free_h2s:
712 pool_free(pool_head_h2s, h2s);
713 out:
714 return NULL;
715}
716
717/* creates a new stream <id> on the h2c connection and returns it, or NULL in
718 * case of memory allocation error.
719 */
720static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
721{
722 struct session *sess = h2c->conn->owner;
723 struct conn_stream *cs;
724 struct h2s *h2s;
725
726 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
727 goto out;
728
729 h2s = h2s_new(h2c, id);
730 if (!h2s)
731 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200732
733 cs = cs_new(h2c->conn);
734 if (!cs)
735 goto out_close;
736
737 h2s->cs = cs;
738 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200739 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200740
741 if (stream_create_from_cs(cs) < 0)
742 goto out_free_cs;
743
Willy Tarreau590a0512018-09-05 11:56:48 +0200744 /* We want the accept date presented to the next stream to be the one
745 * we have now, the handshake time to be null (since the next stream
746 * is not delayed by a handshake), and the idle time to count since
747 * right now.
748 */
749 sess->accept_date = date;
750 sess->tv_accept = now;
751 sess->t_handshake = 0;
752
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200753 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200754 if (h2_has_too_many_cs(h2c))
755 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200756 return h2s;
757
758 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200759 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200760 cs_free(cs);
761 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200762 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200763 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200764 sess_log(sess);
765 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200766}
767
Willy Tarreaube5b7152017-09-25 16:25:39 +0200768/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
769 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
770 * the various settings codes.
771 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200772static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200773{
774 struct buffer *res;
775 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200776 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200777 int ret;
778
779 if (h2c_mux_busy(h2c, NULL)) {
780 h2c->flags |= H2_CF_DEM_MBUSY;
781 return 0;
782 }
783
Willy Tarreau44e973f2018-03-01 17:49:30 +0100784 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200785 if (!res) {
786 h2c->flags |= H2_CF_MUX_MALLOC;
787 h2c->flags |= H2_CF_DEM_MROOM;
788 return 0;
789 }
790
791 chunk_init(&buf, buf_data, sizeof(buf_data));
792 chunk_memcpy(&buf,
793 "\x00\x00\x00" /* length : 0 for now */
794 "\x04\x00" /* type : 4 (settings), flags : 0 */
795 "\x00\x00\x00\x00", /* stream ID : 0 */
796 9);
797
798 if (h2_settings_header_table_size != 4096) {
799 char str[6] = "\x00\x01"; /* header_table_size */
800
801 write_n32(str + 2, h2_settings_header_table_size);
802 chunk_memcat(&buf, str, 6);
803 }
804
805 if (h2_settings_initial_window_size != 65535) {
806 char str[6] = "\x00\x04"; /* initial_window_size */
807
808 write_n32(str + 2, h2_settings_initial_window_size);
809 chunk_memcat(&buf, str, 6);
810 }
811
812 if (h2_settings_max_concurrent_streams != 0) {
813 char str[6] = "\x00\x03"; /* max_concurrent_streams */
814
815 /* Note: 0 means "unlimited" for haproxy's config but not for
816 * the protocol, so never send this value!
817 */
818 write_n32(str + 2, h2_settings_max_concurrent_streams);
819 chunk_memcat(&buf, str, 6);
820 }
821
822 if (global.tune.bufsize != 16384) {
823 char str[6] = "\x00\x05"; /* max_frame_size */
824
825 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
826 * match bufsize - rewrite size, but at the moment it seems
827 * that clients don't take care of it.
828 */
829 write_n32(str + 2, global.tune.bufsize);
830 chunk_memcat(&buf, str, 6);
831 }
832
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 h2_set_frame_size(buf.area, buf.data - 9);
834 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200835 if (unlikely(ret <= 0)) {
836 if (!ret) {
837 h2c->flags |= H2_CF_MUX_MFULL;
838 h2c->flags |= H2_CF_DEM_MROOM;
839 return 0;
840 }
841 else {
842 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
843 return 0;
844 }
845 }
846 return ret;
847}
848
Willy Tarreau52eed752017-09-22 15:05:09 +0200849/* Try to receive a connection preface, then upon success try to send our
850 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
851 * missing data. It may return an error in h2c.
852 */
853static int h2c_frt_recv_preface(struct h2c *h2c)
854{
855 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200856 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200857
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200858 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200859
860 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200861 if (ret1 < 0)
862 sess_log(h2c->conn->owner);
863
Willy Tarreau52eed752017-09-22 15:05:09 +0200864 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
865 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
866 return 0;
867 }
868
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200869 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200870 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200871 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200872
Willy Tarreaube5b7152017-09-25 16:25:39 +0200873 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200874}
875
Willy Tarreau081d4722017-05-16 21:51:05 +0200876/* try to send a GOAWAY frame on the connection to report an error or a graceful
877 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
878 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
879 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
880 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
881 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
882 * on unrecoverable failure. It will not attempt to send one again in this last
883 * case so that it is safe to use h2c_error() to report such errors.
884 */
885static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
886{
887 struct buffer *res;
888 char str[17];
889 int ret;
890
891 if (h2c->flags & H2_CF_GOAWAY_FAILED)
892 return 1; // claim that it worked
893
894 if (h2c_mux_busy(h2c, h2s)) {
895 if (h2s)
896 h2s->flags |= H2_SF_BLK_MBUSY;
897 else
898 h2c->flags |= H2_CF_DEM_MBUSY;
899 return 0;
900 }
901
Willy Tarreau44e973f2018-03-01 17:49:30 +0100902 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200903 if (!res) {
904 h2c->flags |= H2_CF_MUX_MALLOC;
905 if (h2s)
906 h2s->flags |= H2_SF_BLK_MROOM;
907 else
908 h2c->flags |= H2_CF_DEM_MROOM;
909 return 0;
910 }
911
912 /* len: 8, type: 7, flags: none, sid: 0 */
913 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
914
915 if (h2c->last_sid < 0)
916 h2c->last_sid = h2c->max_id;
917
918 write_n32(str + 9, h2c->last_sid);
919 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200920 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200921 if (unlikely(ret <= 0)) {
922 if (!ret) {
923 h2c->flags |= H2_CF_MUX_MFULL;
924 if (h2s)
925 h2s->flags |= H2_SF_BLK_MROOM;
926 else
927 h2c->flags |= H2_CF_DEM_MROOM;
928 return 0;
929 }
930 else {
931 /* we cannot report this error using GOAWAY, so we mark
932 * it and claim a success.
933 */
934 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
935 h2c->flags |= H2_CF_GOAWAY_FAILED;
936 return 1;
937 }
938 }
939 h2c->flags |= H2_CF_GOAWAY_SENT;
940 return ret;
941}
942
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943/* Try to send an RST_STREAM frame on the connection for the indicated stream
944 * during mux operations. This stream must be valid and cannot be closed
945 * already. h2s->id will be used for the stream ID and h2s->errcode will be
946 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
947 * not yet.
948 *
949 * Returns > 0 on success or zero if nothing was done. In case of lack of room
950 * to write the message, it subscribes the stream to future notifications.
951 */
952static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
953{
954 struct buffer *res;
955 char str[13];
956 int ret;
957
958 if (!h2s || h2s->st == H2_SS_CLOSED)
959 return 1;
960
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100961 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
962 * RST_STREAM in response to a RST_STREAM frame.
963 */
964 if (h2c->dft == H2_FT_RST_STREAM) {
965 ret = 1;
966 goto ignore;
967 }
968
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100969 if (h2c_mux_busy(h2c, h2s)) {
970 h2s->flags |= H2_SF_BLK_MBUSY;
971 return 0;
972 }
973
Willy Tarreau44e973f2018-03-01 17:49:30 +0100974 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100975 if (!res) {
976 h2c->flags |= H2_CF_MUX_MALLOC;
977 h2s->flags |= H2_SF_BLK_MROOM;
978 return 0;
979 }
980
981 /* len: 4, type: 3, flags: none */
982 memcpy(str, "\x00\x00\x04\x03\x00", 5);
983 write_n32(str + 5, h2s->id);
984 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200985 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100986
987 if (unlikely(ret <= 0)) {
988 if (!ret) {
989 h2c->flags |= H2_CF_MUX_MFULL;
990 h2s->flags |= H2_SF_BLK_MROOM;
991 return 0;
992 }
993 else {
994 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
995 return 0;
996 }
997 }
998
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100999 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001000 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001001 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001002 return ret;
1003}
1004
1005/* Try to send an RST_STREAM frame on the connection for the stream being
1006 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1007 * error code unless the stream's state already is IDLE or CLOSED in which
1008 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1009 * it was not yet.
1010 *
1011 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1012 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001013 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001014 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001015 */
1016static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1017{
1018 struct buffer *res;
1019 char str[13];
1020 int ret;
1021
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001022 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1023 * RST_STREAM in response to a RST_STREAM frame.
1024 */
1025 if (h2c->dft == H2_FT_RST_STREAM) {
1026 ret = 1;
1027 goto ignore;
1028 }
1029
Willy Tarreau27a84c92017-10-17 08:10:17 +02001030 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001031 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001032 return 0;
1033 }
1034
Willy Tarreau44e973f2018-03-01 17:49:30 +01001035 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001036 if (!res) {
1037 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001038 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001039 return 0;
1040 }
1041
1042 /* len: 4, type: 3, flags: none */
1043 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001044
Willy Tarreau27a84c92017-10-17 08:10:17 +02001045 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001046 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001047 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001048
Willy Tarreau27a84c92017-10-17 08:10:17 +02001049 if (unlikely(ret <= 0)) {
1050 if (!ret) {
1051 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001052 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001053 return 0;
1054 }
1055 else {
1056 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1057 return 0;
1058 }
1059 }
1060
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001061 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001062 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001063 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001064 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001065 }
1066
Willy Tarreau27a84c92017-10-17 08:10:17 +02001067 return ret;
1068}
1069
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001070/* try to send an empty DATA frame with the ES flag set to notify about the
1071 * end of stream and match a shutdown(write). If an ES was already sent as
1072 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1073 * on success or zero if nothing was done. In case of lack of room to write the
1074 * message, it subscribes the requesting stream to future notifications.
1075 */
1076static int h2_send_empty_data_es(struct h2s *h2s)
1077{
1078 struct h2c *h2c = h2s->h2c;
1079 struct buffer *res;
1080 char str[9];
1081 int ret;
1082
Willy Tarreau721c9742017-11-07 11:05:42 +01001083 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001084 return 1;
1085
1086 if (h2c_mux_busy(h2c, h2s)) {
1087 h2s->flags |= H2_SF_BLK_MBUSY;
1088 return 0;
1089 }
1090
Willy Tarreau44e973f2018-03-01 17:49:30 +01001091 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001092 if (!res) {
1093 h2c->flags |= H2_CF_MUX_MALLOC;
1094 h2s->flags |= H2_SF_BLK_MROOM;
1095 return 0;
1096 }
1097
1098 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1099 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1100 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001101 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001102 if (likely(ret > 0)) {
1103 h2s->flags |= H2_SF_ES_SENT;
1104 }
1105 else if (!ret) {
1106 h2c->flags |= H2_CF_MUX_MFULL;
1107 h2s->flags |= H2_SF_BLK_MROOM;
1108 return 0;
1109 }
1110 else {
1111 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1112 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001113 }
1114 return ret;
1115}
1116
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001117/* wake the streams attached to the connection, whose id is greater than <last>,
1118 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001119 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1120 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001121 */
1122static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1123{
1124 struct eb32_node *node;
1125 struct h2s *h2s;
1126
1127 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1128 flags |= CS_FL_ERROR;
1129
1130 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001131 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001132
1133 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1134 while (node) {
1135 h2s = container_of(node, struct h2s, by_id);
1136 if (h2s->id <= last)
1137 break;
1138 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001139
1140 if (!h2s->cs) {
1141 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001142 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001143 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001144 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001145
1146 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001147 if (h2s->recv_wait) {
1148 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001149 sw->wait_reason &= ~SUB_CAN_RECV;
1150 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001151 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001152 } else if (h2s->cs->data_cb->wake != NULL)
1153 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001154
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001155 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1156 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001157 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001158 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001159 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001160 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001161 }
1162}
1163
Willy Tarreau3421aba2017-07-27 15:41:03 +02001164/* Increase all streams' outgoing window size by the difference passed in
1165 * argument. This is needed upon receipt of the settings frame if the initial
1166 * window size is different. The difference may be negative and the resulting
1167 * window size as well, for the time it takes to receive some window updates.
1168 */
1169static void h2c_update_all_ws(struct h2c *h2c, int diff)
1170{
1171 struct h2s *h2s;
1172 struct eb32_node *node;
1173
1174 if (!diff)
1175 return;
1176
1177 node = eb32_first(&h2c->streams_by_id);
1178 while (node) {
1179 h2s = container_of(node, struct h2s, by_id);
1180 h2s->mws += diff;
1181 node = eb32_next(node);
1182 }
1183}
1184
1185/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1186 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1187 * return an error in h2c. Described in RFC7540#6.5.
1188 */
1189static int h2c_handle_settings(struct h2c *h2c)
1190{
1191 unsigned int offset;
1192 int error;
1193
1194 if (h2c->dff & H2_F_SETTINGS_ACK) {
1195 if (h2c->dfl) {
1196 error = H2_ERR_FRAME_SIZE_ERROR;
1197 goto fail;
1198 }
1199 return 1;
1200 }
1201
1202 if (h2c->dsi != 0) {
1203 error = H2_ERR_PROTOCOL_ERROR;
1204 goto fail;
1205 }
1206
1207 if (h2c->dfl % 6) {
1208 error = H2_ERR_FRAME_SIZE_ERROR;
1209 goto fail;
1210 }
1211
1212 /* that's the limit we can process */
1213 if (h2c->dfl > global.tune.bufsize) {
1214 error = H2_ERR_FRAME_SIZE_ERROR;
1215 goto fail;
1216 }
1217
1218 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001219 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001220 return 0;
1221
1222 /* parse the frame */
1223 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001224 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1225 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001226
1227 switch (type) {
1228 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1229 /* we need to update all existing streams with the
1230 * difference from the previous iws.
1231 */
1232 if (arg < 0) { // RFC7540#6.5.2
1233 error = H2_ERR_FLOW_CONTROL_ERROR;
1234 goto fail;
1235 }
1236 h2c_update_all_ws(h2c, arg - h2c->miw);
1237 h2c->miw = arg;
1238 break;
1239 case H2_SETTINGS_MAX_FRAME_SIZE:
1240 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1241 error = H2_ERR_PROTOCOL_ERROR;
1242 goto fail;
1243 }
1244 h2c->mfs = arg;
1245 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001246 case H2_SETTINGS_ENABLE_PUSH:
1247 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1248 error = H2_ERR_PROTOCOL_ERROR;
1249 goto fail;
1250 }
1251 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001252 }
1253 }
1254
1255 /* need to ACK this frame now */
1256 h2c->st0 = H2_CS_FRAME_A;
1257 return 1;
1258 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001259 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001260 h2c_error(h2c, error);
1261 return 0;
1262}
1263
1264/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1265 * success or one of the h2_status values.
1266 */
1267static int h2c_ack_settings(struct h2c *h2c)
1268{
1269 struct buffer *res;
1270 char str[9];
1271 int ret = -1;
1272
1273 if (h2c_mux_busy(h2c, NULL)) {
1274 h2c->flags |= H2_CF_DEM_MBUSY;
1275 return 0;
1276 }
1277
Willy Tarreau44e973f2018-03-01 17:49:30 +01001278 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001279 if (!res) {
1280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 h2c->flags |= H2_CF_DEM_MROOM;
1282 return 0;
1283 }
1284
1285 memcpy(str,
1286 "\x00\x00\x00" /* length : 0 (no data) */
1287 "\x04" "\x01" /* type : 4, flags : ACK */
1288 "\x00\x00\x00\x00" /* stream ID */, 9);
1289
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001290 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001291 if (unlikely(ret <= 0)) {
1292 if (!ret) {
1293 h2c->flags |= H2_CF_MUX_MFULL;
1294 h2c->flags |= H2_CF_DEM_MROOM;
1295 return 0;
1296 }
1297 else {
1298 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1299 return 0;
1300 }
1301 }
1302 return ret;
1303}
1304
Willy Tarreaucf68c782017-10-10 17:11:41 +02001305/* processes a PING frame and schedules an ACK if needed. The caller must pass
1306 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1307 * missing data. It may return an error in h2c.
1308 */
1309static int h2c_handle_ping(struct h2c *h2c)
1310{
1311 /* frame length must be exactly 8 */
1312 if (h2c->dfl != 8) {
1313 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1314 return 0;
1315 }
1316
1317 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001318 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001319 h2c->st0 = H2_CS_FRAME_A;
1320 return 1;
1321}
1322
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001323/* Try to send a window update for stream id <sid> and value <increment>.
1324 * Returns > 0 on success or zero on missing room or failure. It may return an
1325 * error in h2c.
1326 */
1327static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1328{
1329 struct buffer *res;
1330 char str[13];
1331 int ret = -1;
1332
1333 if (h2c_mux_busy(h2c, NULL)) {
1334 h2c->flags |= H2_CF_DEM_MBUSY;
1335 return 0;
1336 }
1337
Willy Tarreau44e973f2018-03-01 17:49:30 +01001338 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001339 if (!res) {
1340 h2c->flags |= H2_CF_MUX_MALLOC;
1341 h2c->flags |= H2_CF_DEM_MROOM;
1342 return 0;
1343 }
1344
1345 /* length: 4, type: 8, flags: none */
1346 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1347 write_n32(str + 5, sid);
1348 write_n32(str + 9, increment);
1349
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001350 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001351
1352 if (unlikely(ret <= 0)) {
1353 if (!ret) {
1354 h2c->flags |= H2_CF_MUX_MFULL;
1355 h2c->flags |= H2_CF_DEM_MROOM;
1356 return 0;
1357 }
1358 else {
1359 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1360 return 0;
1361 }
1362 }
1363 return ret;
1364}
1365
1366/* try to send pending window update for the connection. It's safe to call it
1367 * with no pending updates. Returns > 0 on success or zero on missing room or
1368 * failure. It may return an error in h2c.
1369 */
1370static int h2c_send_conn_wu(struct h2c *h2c)
1371{
1372 int ret = 1;
1373
1374 if (h2c->rcvd_c <= 0)
1375 return 1;
1376
1377 /* send WU for the connection */
1378 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1379 if (ret > 0)
1380 h2c->rcvd_c = 0;
1381
1382 return ret;
1383}
1384
1385/* try to send pending window update for the current dmux stream. It's safe to
1386 * call it with no pending updates. Returns > 0 on success or zero on missing
1387 * room or failure. It may return an error in h2c.
1388 */
1389static int h2c_send_strm_wu(struct h2c *h2c)
1390{
1391 int ret = 1;
1392
1393 if (h2c->rcvd_s <= 0)
1394 return 1;
1395
1396 /* send WU for the stream */
1397 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1398 if (ret > 0)
1399 h2c->rcvd_s = 0;
1400
1401 return ret;
1402}
1403
Willy Tarreaucf68c782017-10-10 17:11:41 +02001404/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1405 * success, 0 on missing data or one of the h2_status values.
1406 */
1407static int h2c_ack_ping(struct h2c *h2c)
1408{
1409 struct buffer *res;
1410 char str[17];
1411 int ret = -1;
1412
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001413 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001414 return 0;
1415
1416 if (h2c_mux_busy(h2c, NULL)) {
1417 h2c->flags |= H2_CF_DEM_MBUSY;
1418 return 0;
1419 }
1420
Willy Tarreau44e973f2018-03-01 17:49:30 +01001421 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001422 if (!res) {
1423 h2c->flags |= H2_CF_MUX_MALLOC;
1424 h2c->flags |= H2_CF_DEM_MROOM;
1425 return 0;
1426 }
1427
1428 memcpy(str,
1429 "\x00\x00\x08" /* length : 8 (same payload) */
1430 "\x06" "\x01" /* type : 6, flags : ACK */
1431 "\x00\x00\x00\x00" /* stream ID */, 9);
1432
1433 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001434 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001435
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001436 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001437 if (unlikely(ret <= 0)) {
1438 if (!ret) {
1439 h2c->flags |= H2_CF_MUX_MFULL;
1440 h2c->flags |= H2_CF_DEM_MROOM;
1441 return 0;
1442 }
1443 else {
1444 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1445 return 0;
1446 }
1447 }
1448 return ret;
1449}
1450
Willy Tarreau26f95952017-07-27 17:18:30 +02001451/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1452 * Returns > 0 on success or zero on missing data. It may return an error in
1453 * h2c or h2s. Described in RFC7540#6.9.
1454 */
1455static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1456{
1457 int32_t inc;
1458 int error;
1459
1460 if (h2c->dfl != 4) {
1461 error = H2_ERR_FRAME_SIZE_ERROR;
1462 goto conn_err;
1463 }
1464
1465 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001466 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001467 return 0;
1468
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001469 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001470
1471 if (h2c->dsi != 0) {
1472 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001473
1474 /* it's not an error to receive WU on a closed stream */
1475 if (h2s->st == H2_SS_CLOSED)
1476 return 1;
1477
1478 if (!inc) {
1479 error = H2_ERR_PROTOCOL_ERROR;
1480 goto strm_err;
1481 }
1482
1483 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1484 error = H2_ERR_FLOW_CONTROL_ERROR;
1485 goto strm_err;
1486 }
1487
1488 h2s->mws += inc;
1489 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1490 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001491 if (h2s->send_wait)
1492 LIST_ADDQ(&h2c->send_list, &h2s->list);
1493
Willy Tarreau26f95952017-07-27 17:18:30 +02001494 }
1495 }
1496 else {
1497 /* connection window update */
1498 if (!inc) {
1499 error = H2_ERR_PROTOCOL_ERROR;
1500 goto conn_err;
1501 }
1502
1503 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1504 error = H2_ERR_FLOW_CONTROL_ERROR;
1505 goto conn_err;
1506 }
1507
1508 h2c->mws += inc;
1509 }
1510
1511 return 1;
1512
1513 conn_err:
1514 h2c_error(h2c, error);
1515 return 0;
1516
1517 strm_err:
1518 if (h2s) {
1519 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001520 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001521 }
1522 else
1523 h2c_error(h2c, error);
1524 return 0;
1525}
1526
Willy Tarreaue96b0922017-10-30 00:28:29 +01001527/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1528 * the last ID. Returns > 0 on success or zero on missing data. It may return
1529 * an error in h2c. Described in RFC7540#6.8.
1530 */
1531static int h2c_handle_goaway(struct h2c *h2c)
1532{
1533 int error;
1534 int last;
1535
1536 if (h2c->dsi != 0) {
1537 error = H2_ERR_PROTOCOL_ERROR;
1538 goto conn_err;
1539 }
1540
1541 if (h2c->dfl < 8) {
1542 error = H2_ERR_FRAME_SIZE_ERROR;
1543 goto conn_err;
1544 }
1545
1546 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001547 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001548 return 0;
1549
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001550 last = h2_get_n32(&h2c->dbuf, 0);
1551 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001552 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001553 if (h2c->last_sid < 0)
1554 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001555 return 1;
1556
1557 conn_err:
1558 h2c_error(h2c, error);
1559 return 0;
1560}
1561
Willy Tarreau92153fc2017-12-03 19:46:19 +01001562/* processes a PRIORITY frame, and either skips it or rejects if it is
1563 * invalid. Returns > 0 on success or zero on missing data. It may return
1564 * an error in h2c. Described in RFC7540#6.3.
1565 */
1566static int h2c_handle_priority(struct h2c *h2c)
1567{
1568 int error;
1569
1570 if (h2c->dsi == 0) {
1571 error = H2_ERR_PROTOCOL_ERROR;
1572 goto conn_err;
1573 }
1574
1575 if (h2c->dfl != 5) {
1576 error = H2_ERR_FRAME_SIZE_ERROR;
1577 goto conn_err;
1578 }
1579
1580 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001581 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001582 return 0;
1583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001584 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001585 /* 7540#5.3 : can't depend on itself */
1586 error = H2_ERR_PROTOCOL_ERROR;
1587 goto conn_err;
1588 }
1589 return 1;
1590
1591 conn_err:
1592 h2c_error(h2c, error);
1593 return 0;
1594}
1595
Willy Tarreaucd234e92017-08-18 10:59:39 +02001596/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1597 * Returns > 0 on success or zero on missing data. It may return an error in
1598 * h2c. Described in RFC7540#6.4.
1599 */
1600static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1601{
1602 int error;
1603
1604 if (h2c->dsi == 0) {
1605 error = H2_ERR_PROTOCOL_ERROR;
1606 goto conn_err;
1607 }
1608
Willy Tarreaucd234e92017-08-18 10:59:39 +02001609 if (h2c->dfl != 4) {
1610 error = H2_ERR_FRAME_SIZE_ERROR;
1611 goto conn_err;
1612 }
1613
1614 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001615 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001616 return 0;
1617
1618 /* late RST, already handled */
1619 if (h2s->st == H2_SS_CLOSED)
1620 return 1;
1621
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001622 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001623 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001624
1625 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001626 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001627 if (h2s->recv_wait) {
1628 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001629
1630 sw->wait_reason &= ~SUB_CAN_RECV;
1631 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001632 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001633 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001634 }
1635
1636 h2s->flags |= H2_SF_RST_RCVD;
1637 return 1;
1638
1639 conn_err:
1640 h2c_error(h2c, error);
1641 return 0;
1642}
1643
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001644/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1645 * It may return an error in h2c or h2s. The caller must consider that the
1646 * return value is the new h2s in case one was allocated (most common case).
1647 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001648 * errors here are reported as connection errors since it's impossible to
1649 * recover from such errors after the compression context has been altered.
1650 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001651static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001652{
1653 int error;
1654
1655 if (!h2c->dfl) {
1656 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001657 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001658 goto strm_err;
1659 }
1660
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001661 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001662 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001663
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001664 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001665 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001666
Willy Tarreauf2101912018-07-19 10:11:38 +02001667 if (h2c->flags & H2_CF_DEM_TOOMANY)
1668 return 0; // too many cs still present
1669
Willy Tarreau13278b42017-10-13 19:23:14 +02001670 /* now either the frame is complete or the buffer is complete */
1671 if (h2s->st != H2_SS_IDLE) {
1672 /* FIXME: stream already exists, this is only allowed for
1673 * trailers (not supported for now).
1674 */
1675 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001676 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001677 goto conn_err;
1678 }
1679 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1680 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1681 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001682 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001683 goto conn_err;
1684 }
1685
Willy Tarreau22de8d32018-09-05 19:55:58 +02001686 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001687 * positively from h2c_frt_stream_new(), the stream will report the error,
1688 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001689 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001690 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001691 if (!h2s) {
1692 error = H2_ERR_INTERNAL_ERROR;
1693 goto conn_err;
1694 }
1695
1696 h2s->st = H2_SS_OPEN;
1697 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1698 h2s->st = H2_SS_HREM;
1699 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001700 /* note: cs cannot be null for now (just created above) */
1701 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001702 }
1703
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001704 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001705 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001706
Willy Tarreau8f650c32017-11-21 19:36:21 +01001707 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001708 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001709
Willy Tarreau721c9742017-11-07 11:05:42 +01001710 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001711 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001712 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001713 }
1714 else {
1715 /* update the max stream ID if the request is being processed */
1716 if (h2s->id > h2c->max_id)
1717 h2c->max_id = h2s->id;
1718 }
1719
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001720 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001721
1722 conn_err:
1723 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001724 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001725
1726 strm_err:
1727 if (h2s) {
1728 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001729 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001730 }
1731 else
1732 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001733 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001734}
1735
Willy Tarreau454f9052017-10-26 19:40:35 +02001736/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1737 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1738 */
1739static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1740{
1741 int error;
1742
1743 /* note that empty DATA frames are perfectly valid and sometimes used
1744 * to signal an end of stream (with the ES flag).
1745 */
1746
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001747 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001748 return 0; // empty buffer
1749
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001750 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001751 return 0; // incomplete frame
1752
1753 /* now either the frame is complete or the buffer is complete */
1754
1755 if (!h2c->dsi) {
1756 /* RFC7540#6.1 */
1757 error = H2_ERR_PROTOCOL_ERROR;
1758 goto conn_err;
1759 }
1760
1761 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1762 /* RFC7540#6.1 */
1763 error = H2_ERR_STREAM_CLOSED;
1764 goto strm_err;
1765 }
1766
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001767 if (!h2_frt_transfer_data(h2s))
1768 return 0;
1769
Willy Tarreau454f9052017-10-26 19:40:35 +02001770 /* call the upper layers to process the frame, then let the upper layer
1771 * notify the stream about any change.
1772 */
1773 if (!h2s->cs) {
1774 error = H2_ERR_STREAM_CLOSED;
1775 goto strm_err;
1776 }
1777
Willy Tarreau8f650c32017-11-21 19:36:21 +01001778 if (h2c->st0 >= H2_CS_ERROR)
1779 return 0;
1780
Willy Tarreau721c9742017-11-07 11:05:42 +01001781 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001782 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001783 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001784 }
1785
1786 /* check for completion : the callee will change this to FRAME_A or
1787 * FRAME_H once done.
1788 */
1789 if (h2c->st0 == H2_CS_FRAME_P)
1790 return 0;
1791
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001792
1793 /* last frame */
1794 if (h2c->dff & H2_F_DATA_END_STREAM) {
1795 h2s->st = H2_SS_HREM;
1796 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001797 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001798 }
1799
Willy Tarreau454f9052017-10-26 19:40:35 +02001800 return 1;
1801
1802 conn_err:
1803 h2c_error(h2c, error);
1804 return 0;
1805
1806 strm_err:
1807 if (h2s) {
1808 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001809 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001810 }
1811 else
1812 h2c_error(h2c, error);
1813 return 0;
1814}
1815
Willy Tarreaubc933932017-10-09 16:21:43 +02001816/* process Rx frames to be demultiplexed */
1817static void h2_process_demux(struct h2c *h2c)
1818{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001819 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001820
Willy Tarreau081d4722017-05-16 21:51:05 +02001821 if (h2c->st0 >= H2_CS_ERROR)
1822 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001823
1824 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1825 if (h2c->st0 == H2_CS_PREFACE) {
1826 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1827 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001828 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001829 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001830 sess_log(h2c->conn->owner);
1831 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001832 goto fail;
1833 }
1834
1835 h2c->max_id = 0;
1836 h2c->st0 = H2_CS_SETTINGS1;
1837 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001838
1839 if (h2c->st0 == H2_CS_SETTINGS1) {
1840 struct h2_fh hdr;
1841
1842 /* ensure that what is pending is a valid SETTINGS frame
1843 * without an ACK.
1844 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001845 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001846 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001847 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001848 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001849 sess_log(h2c->conn->owner);
1850 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001851 goto fail;
1852 }
1853
1854 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1855 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1856 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1857 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001858 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001859 goto fail;
1860 }
1861
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001862 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001863 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1864 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1865 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001866 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001867 goto fail;
1868 }
1869
1870 /* that's OK, switch to FRAME_P to process it */
1871 h2c->dfl = hdr.len;
1872 h2c->dsi = hdr.sid;
1873 h2c->dft = hdr.ft;
1874 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001875 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001876 h2c->st0 = H2_CS_FRAME_P;
1877 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001878 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001879
1880 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001881 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001882 int ret = 0;
1883
1884 if (h2c->st0 >= H2_CS_ERROR)
1885 break;
1886
1887 if (h2c->st0 == H2_CS_FRAME_H) {
1888 struct h2_fh hdr;
1889
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001890 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001891 break;
1892
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001893 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001894 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1895 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001896 if (!h2c->nb_streams) {
1897 /* only log if no other stream can report the error */
1898 sess_log(h2c->conn->owner);
1899 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001900 break;
1901 }
1902
1903 h2c->dfl = hdr.len;
1904 h2c->dsi = hdr.sid;
1905 h2c->dft = hdr.ft;
1906 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001907 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001908 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001909 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001910 }
1911
1912 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001913 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1914
Olivier Houchard638b7992018-08-16 15:41:52 +02001915 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001916 /* we may have to signal the upper layers */
1917 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001918 if (h2s->recv_wait) {
1919 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1920 tasklet_wakeup(h2s->recv_wait->task);
1921 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923 }
1924 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001925
Willy Tarreaud7901432017-12-29 11:34:40 +01001926 if (h2c->st0 == H2_CS_FRAME_E)
1927 goto strm_err;
1928
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001929 if (h2s->st == H2_SS_IDLE &&
1930 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1931 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1932 * this state MUST be treated as a connection error
1933 */
1934 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1935 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001936 if (!h2c->nb_streams) {
1937 /* only log if no other stream can report the error */
1938 sess_log(h2c->conn->owner);
1939 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001940 break;
1941 }
1942
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001943 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1944 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1945 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1946 * this state MUST be treated as a stream error
1947 */
1948 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1949 goto strm_err;
1950 }
1951
Willy Tarreauab837502017-12-27 15:07:30 +01001952 /* Below the management of frames received in closed state is a
1953 * bit hackish because the spec makes strong differences between
1954 * streams closed by receiving RST, sending RST, and seeing ES
1955 * in both directions. In addition to this, the creation of a
1956 * new stream reusing the identifier of a closed one will be
1957 * detected here. Given that we cannot keep track of all closed
1958 * streams forever, we consider that unknown closed streams were
1959 * closed on RST received, which allows us to respond with an
1960 * RST without breaking the connection (eg: to abort a transfer).
1961 * Some frames have to be silently ignored as well.
1962 */
1963 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1964 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1965 /* #5.1.1: The identifier of a newly
1966 * established stream MUST be numerically
1967 * greater than all streams that the initiating
1968 * endpoint has opened or reserved. This
1969 * governs streams that are opened using a
1970 * HEADERS frame and streams that are reserved
1971 * using PUSH_PROMISE. An endpoint that
1972 * receives an unexpected stream identifier
1973 * MUST respond with a connection error.
1974 */
1975 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1976 goto strm_err;
1977 }
1978
1979 if (h2s->flags & H2_SF_RST_RCVD) {
1980 /* RFC7540#5.1:closed: an endpoint that
1981 * receives any frame other than PRIORITY after
1982 * receiving a RST_STREAM MUST treat that as a
1983 * stream error of type STREAM_CLOSED.
1984 *
1985 * Note that old streams fall into this category
1986 * and will lead to an RST being sent.
1987 */
1988 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1989 h2c->st0 = H2_CS_FRAME_E;
1990 goto strm_err;
1991 }
1992
1993 /* RFC7540#5.1:closed: if this state is reached as a
1994 * result of sending a RST_STREAM frame, the peer that
1995 * receives the RST_STREAM might have already sent
1996 * frames on the stream that cannot be withdrawn. An
1997 * endpoint MUST ignore frames that it receives on
1998 * closed streams after it has sent a RST_STREAM
1999 * frame. An endpoint MAY choose to limit the period
2000 * over which it ignores frames and treat frames that
2001 * arrive after this time as being in error.
2002 */
2003 if (!(h2s->flags & H2_SF_RST_SENT)) {
2004 /* RFC7540#5.1:closed: any frame other than
2005 * PRIO/WU/RST in this state MUST be treated as
2006 * a connection error
2007 */
2008 if (h2c->dft != H2_FT_RST_STREAM &&
2009 h2c->dft != H2_FT_PRIORITY &&
2010 h2c->dft != H2_FT_WINDOW_UPDATE) {
2011 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2012 goto strm_err;
2013 }
2014 }
2015 }
2016
Willy Tarreauc0da1962017-10-30 18:38:00 +01002017#if 0
2018 // problem below: it is not possible to completely ignore such
2019 // streams as we need to maintain the compression state as well
2020 // and for this we need to completely process these frames (eg:
2021 // HEADERS frames) as well as counting DATA frames to emit
2022 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2023 // This is a typical case of layer violation where the
2024 // transported contents are critical to the connection's
2025 // validity and must be ignored at the same time :-(
2026
2027 /* graceful shutdown, ignore streams whose ID is higher than
2028 * the one advertised in GOAWAY. RFC7540#6.8.
2029 */
2030 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002031 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2032 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002033 h2c->dfl -= ret;
2034 ret = h2c->dfl == 0;
2035 goto strm_err;
2036 }
2037#endif
2038
Willy Tarreau7e98c052017-10-10 15:56:59 +02002039 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002040 case H2_FT_SETTINGS:
2041 if (h2c->st0 == H2_CS_FRAME_P)
2042 ret = h2c_handle_settings(h2c);
2043
2044 if (h2c->st0 == H2_CS_FRAME_A)
2045 ret = h2c_ack_settings(h2c);
2046 break;
2047
Willy Tarreaucf68c782017-10-10 17:11:41 +02002048 case H2_FT_PING:
2049 if (h2c->st0 == H2_CS_FRAME_P)
2050 ret = h2c_handle_ping(h2c);
2051
2052 if (h2c->st0 == H2_CS_FRAME_A)
2053 ret = h2c_ack_ping(h2c);
2054 break;
2055
Willy Tarreau26f95952017-07-27 17:18:30 +02002056 case H2_FT_WINDOW_UPDATE:
2057 if (h2c->st0 == H2_CS_FRAME_P)
2058 ret = h2c_handle_window_update(h2c, h2s);
2059 break;
2060
Willy Tarreau61290ec2017-10-17 08:19:21 +02002061 case H2_FT_CONTINUATION:
2062 /* we currently don't support CONTINUATION frames since
2063 * we have nowhere to store the partial HEADERS frame.
2064 * Let's abort the stream on an INTERNAL_ERROR here.
2065 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002066 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002067 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002068 h2c->st0 = H2_CS_FRAME_E;
2069 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002070 break;
2071
Willy Tarreau13278b42017-10-13 19:23:14 +02002072 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002073 if (h2c->st0 == H2_CS_FRAME_P) {
2074 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2075 if (tmp_h2s) {
2076 h2s = tmp_h2s;
2077 ret = 1;
2078 }
2079 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002080 break;
2081
Willy Tarreau454f9052017-10-26 19:40:35 +02002082 case H2_FT_DATA:
2083 if (h2c->st0 == H2_CS_FRAME_P)
2084 ret = h2c_frt_handle_data(h2c, h2s);
2085
2086 if (h2c->st0 == H2_CS_FRAME_A)
2087 ret = h2c_send_strm_wu(h2c);
2088 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002089
Willy Tarreau92153fc2017-12-03 19:46:19 +01002090 case H2_FT_PRIORITY:
2091 if (h2c->st0 == H2_CS_FRAME_P)
2092 ret = h2c_handle_priority(h2c);
2093 break;
2094
Willy Tarreaucd234e92017-08-18 10:59:39 +02002095 case H2_FT_RST_STREAM:
2096 if (h2c->st0 == H2_CS_FRAME_P)
2097 ret = h2c_handle_rst_stream(h2c, h2s);
2098 break;
2099
Willy Tarreaue96b0922017-10-30 00:28:29 +01002100 case H2_FT_GOAWAY:
2101 if (h2c->st0 == H2_CS_FRAME_P)
2102 ret = h2c_handle_goaway(h2c);
2103 break;
2104
Willy Tarreau1c661982017-10-30 13:52:01 +01002105 case H2_FT_PUSH_PROMISE:
2106 /* not permitted here, RFC7540#5.1 */
2107 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002108 if (!h2c->nb_streams) {
2109 /* only log if no other stream can report the error */
2110 sess_log(h2c->conn->owner);
2111 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002112 break;
2113
2114 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002115 default:
2116 /* drop frames that we ignore. They may be larger than
2117 * the buffer so we drain all of their contents until
2118 * we reach the end.
2119 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002120 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2121 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002122 h2c->dfl -= ret;
2123 ret = h2c->dfl == 0;
2124 }
2125
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002126 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002127 /* We may have to send an RST if not done yet */
2128 if (h2s->st == H2_SS_ERROR)
2129 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002130
Willy Tarreaua20a5192017-12-27 11:02:06 +01002131 if (h2c->st0 == H2_CS_FRAME_E)
2132 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002133
Willy Tarreau7e98c052017-10-10 15:56:59 +02002134 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002135 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002136 break;
2137
2138 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002139 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002140 h2c->st0 = H2_CS_FRAME_H;
2141 }
2142 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002143
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002144 if (h2c->rcvd_c > 0 &&
2145 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2146 h2c_send_conn_wu(h2c);
2147
Willy Tarreau52eed752017-09-22 15:05:09 +02002148 fail:
2149 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002150 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002151 /* we may have to signal the upper layers */
2152 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002153 if (h2s->recv_wait) {
2154 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2155 tasklet_wakeup(h2s->recv_wait->task);
2156 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002157 }
2158 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002159
2160 if (h2_recv_allowed(h2c))
2161 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002162}
2163
2164/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2165 * the end.
2166 */
2167static int h2_process_mux(struct h2c *h2c)
2168{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002169 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002170
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002171 /* start by sending possibly pending window updates */
2172 if (h2c->rcvd_c > 0 &&
2173 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2174 h2c_send_conn_wu(h2c) < 0)
2175 goto fail;
2176
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002177 /* First we always process the flow control list because the streams
2178 * waiting there were already elected for immediate emission but were
2179 * blocked just on this.
2180 */
2181
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002182 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002183 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2184 h2c->st0 >= H2_CS_ERROR)
2185 break;
2186
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002187 h2s->flags &= ~H2_SF_BLK_ANY;
2188 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002189 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002190 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002191 LIST_DEL(&h2s->list);
2192 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002193 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002194 }
2195
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002196 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002197 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2198 break;
2199
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002200 h2s->flags &= ~H2_SF_BLK_ANY;
2201 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002202 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002203 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002204 LIST_DEL(&h2s->list);
2205 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002206 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002207 }
2208
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002209 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002210 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002211 if (h2c->st0 == H2_CS_ERROR) {
2212 if (h2c->max_id >= 0) {
2213 h2c_send_goaway_error(h2c, NULL);
2214 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2215 return 0;
2216 }
2217
2218 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2219 }
2220 return 1;
2221 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002222 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002223}
2224
Willy Tarreau62f52692017-10-08 23:01:42 +02002225
Willy Tarreau479998a2018-11-18 06:30:59 +01002226/* Attempt to read data, and subscribe if none available.
2227 * The function returns 1 if data has been received, otherwise zero.
2228 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002229static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002230{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002231 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002232 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002233 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002234 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002236 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002237 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002238
Willy Tarreau315d8072017-12-10 22:17:57 +01002239 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002240 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241
Willy Tarreau44e973f2018-03-01 17:49:30 +01002242 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002243 if (!buf) {
2244 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002245 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002246 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002247
Olivier Houchard7505f942018-08-21 18:10:44 +02002248 do {
2249 max = buf->size - b_data(buf);
2250 if (max)
2251 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2252 else
2253 ret = 0;
2254 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002255
Olivier Houchard53216e72018-10-10 15:46:36 +02002256 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002257 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002258
Olivier Houcharda1411e62018-08-17 18:42:48 +02002259 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002260 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002261 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002262 }
2263
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002264 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002265 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002266 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002267}
2268
Willy Tarreau479998a2018-11-18 06:30:59 +01002269/* Try to send data if possible.
2270 * The function returns 1 if data have been sent, otherwise zero.
2271 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002272static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002273{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002274 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002275 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002276 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002277
2278 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002279 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002280
Olivier Houchard7505f942018-08-21 18:10:44 +02002281
Willy Tarreaua2af5122017-10-09 11:56:46 +02002282 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2283 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002284 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002285 }
2286
Willy Tarreaubc933932017-10-09 16:21:43 +02002287 /* This loop is quite simple : it tries to fill as much as it can from
2288 * pending streams into the existing buffer until it's reportedly full
2289 * or the end of send requests is reached. Then it tries to send this
2290 * buffer's contents out, marks it not full if at least one byte could
2291 * be sent, and tries again.
2292 *
2293 * The snd_buf() function normally takes a "flags" argument which may
2294 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2295 * data immediately comes and CO_SFL_STREAMER to indicate that the
2296 * connection is streaming lots of data (used to increase TLS record
2297 * size at the expense of latency). The former can be sent any time
2298 * there's a buffer full flag, as it indicates at least one stream
2299 * attempted to send and failed so there are pending data. An
2300 * alternative would be to set it as long as there's an active stream
2301 * but that would be problematic for ACKs until we have an absolute
2302 * guarantee that all waiters have at least one byte to send. The
2303 * latter should possibly not be set for now.
2304 */
2305
2306 done = 0;
2307 while (!done) {
2308 unsigned int flags = 0;
2309
2310 /* fill as much as we can into the current buffer */
2311 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2312 done = h2_process_mux(h2c);
2313
2314 if (conn->flags & CO_FL_ERROR)
2315 break;
2316
2317 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2318 flags |= CO_SFL_MSG_MORE;
2319
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 if (b_data(&h2c->mbuf)) {
2321 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002322 if (!ret)
2323 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002324 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002325 b_del(&h2c->mbuf, ret);
2326 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002327 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002328
2329 /* wrote at least one byte, the buffer is not full anymore */
2330 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2331 }
2332
Willy Tarreaua2af5122017-10-09 11:56:46 +02002333 if (conn->flags & CO_FL_SOCK_WR_SH) {
2334 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002335 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002336 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002337 /* We're not full anymore, so we can wake any task that are waiting
2338 * for us.
2339 */
2340 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002341 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002342 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2343 struct h2s *, list);
2344 LIST_DEL(&h2s->list);
2345 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002346 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002347 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002348 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002349 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002350 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002351 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002352 /* We're done, no more to send */
2353 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002354 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002355schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002356 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2357 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002358 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002359}
2360
2361static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2362{
2363 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002364 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002365
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002366 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002367 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002368 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002369 ret |= h2_recv(h2c);
2370 if (ret)
2371 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002372 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002373}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002374
Willy Tarreau62f52692017-10-08 23:01:42 +02002375/* callback called on any event by the connection handler.
2376 * It applies changes and returns zero, or < 0 if it wants immediate
2377 * destruction of the connection (which normally doesn not happen in h2).
2378 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002379static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002380{
Olivier Houchard7505f942018-08-21 18:10:44 +02002381 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002382
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002383 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002384 h2_process_demux(h2c);
2385
2386 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002387 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002388
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002389 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002390 h2c->flags &= ~H2_CF_DEM_DFULL;
2391 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002392 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002393
Willy Tarreau0b37d652018-10-03 10:33:02 +02002394 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002395 /* frontend is stopping, reload likely in progress, let's try
2396 * to announce a graceful shutdown if not yet done. We don't
2397 * care if it fails, it will be tried again later.
2398 */
2399 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2400 if (h2c->last_sid < 0)
2401 h2c->last_sid = (1U << 31) - 1;
2402 h2c_send_goaway_error(h2c, NULL);
2403 }
2404 }
2405
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002406 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002407 * If we received early data, and the handshake is done, wake
2408 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002409 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002410 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2411 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2412 struct eb32_node *node;
2413 struct h2s *h2s;
2414
2415 h2c->flags |= H2_CF_WAIT_FOR_HS;
2416 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2417
2418 while (node) {
2419 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002420 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002421 h2s->recv_wait) {
2422 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002423 sw->wait_reason &= ~SUB_CAN_RECV;
2424 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002425 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002426 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002427 node = eb32_next(node);
2428 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002429 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002430
Willy Tarreau26bd7612017-10-09 16:47:04 +02002431 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002432 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2433 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2434 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002435 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002436
2437 if (eb_is_empty(&h2c->streams_by_id)) {
2438 /* no more stream, kill the connection now */
2439 h2_release(conn);
2440 return -1;
2441 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442 }
2443
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002444 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002445 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002446
Olivier Houchard53216e72018-10-10 15:46:36 +02002447 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2448 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2449 (h2c->st0 != H2_CS_ERROR &&
2450 !b_data(&h2c->mbuf) &&
2451 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2452 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002453 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002454
Willy Tarreau3f133572017-10-31 19:21:06 +01002455 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002456 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002457 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002458 task_queue(h2c->task);
2459 }
2460 else
2461 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002462 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002463
Olivier Houchard7505f942018-08-21 18:10:44 +02002464 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002465 return 0;
2466}
2467
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002468static int h2_wake(struct connection *conn)
2469{
2470 struct h2c *h2c = conn->mux_ctx;
2471
2472 return (h2_process(h2c));
2473}
2474
Willy Tarreauea392822017-10-31 10:02:25 +01002475/* Connection timeout management. The principle is that if there's no receipt
2476 * nor sending for a certain amount of time, the connection is closed. If the
2477 * MUX buffer still has lying data or is not allocatable, the connection is
2478 * immediately killed. If it's allocatable and empty, we attempt to send a
2479 * GOAWAY frame.
2480 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002481static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002482{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002483 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002484 int expired = tick_is_expired(t->expire, now_ms);
2485
Willy Tarreau0975f112018-03-29 15:22:59 +02002486 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002487 return t;
2488
Willy Tarreau0975f112018-03-29 15:22:59 +02002489 task_delete(t);
2490 task_free(t);
2491
2492 if (!h2c) {
2493 /* resources were already deleted */
2494 return NULL;
2495 }
2496
2497 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002498 h2c_error(h2c, H2_ERR_NO_ERROR);
2499 h2_wake_some_streams(h2c, 0, 0);
2500
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002501 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002502 /* don't even try to send a GOAWAY, the buffer is stuck */
2503 h2c->flags |= H2_CF_GOAWAY_FAILED;
2504 }
2505
2506 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002507 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002508 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2509 h2c->flags |= H2_CF_GOAWAY_FAILED;
2510
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002511 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2512 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002513 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 b_del(&h2c->mbuf, ret);
2515 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002516 }
2517 }
Willy Tarreauea392822017-10-31 10:02:25 +01002518
Willy Tarreau0975f112018-03-29 15:22:59 +02002519 /* either we can release everything now or it will be done later once
2520 * the last stream closes.
2521 */
2522 if (eb_is_empty(&h2c->streams_by_id))
2523 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002524
Willy Tarreauea392822017-10-31 10:02:25 +01002525 return NULL;
2526}
2527
2528
Willy Tarreau62f52692017-10-08 23:01:42 +02002529/*******************************************/
2530/* functions below are used by the streams */
2531/*******************************************/
2532
2533/*
2534 * Attach a new stream to a connection
2535 * (Used for outgoing connections)
2536 */
2537static struct conn_stream *h2_attach(struct connection *conn)
2538{
2539 return NULL;
2540}
2541
Willy Tarreaufafd3982018-11-18 21:29:20 +01002542/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2543 * We have to scan because we may have some orphan streams. It might be
2544 * beneficial to scan backwards from the end to reduce the likeliness to find
2545 * orphans.
2546 */
2547static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2548{
2549 struct h2c *h2c = conn->mux_ctx;
2550 struct h2s *h2s;
2551 struct eb32_node *node;
2552
2553 node = eb32_first(&h2c->streams_by_id);
2554 while (node) {
2555 h2s = container_of(node, struct h2s, by_id);
2556 if (h2s->cs)
2557 return h2s->cs;
2558 node = eb32_next(node);
2559 }
2560 return NULL;
2561}
2562
Willy Tarreau62f52692017-10-08 23:01:42 +02002563/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002564 * Destroy the mux and the associated connection, if it is no longer used
2565 */
2566static void h2_destroy(struct connection *conn)
2567{
2568 struct h2c *h2c = conn->mux_ctx;
2569
2570 if (eb_is_empty(&h2c->streams_by_id))
2571 h2_release(h2c->conn);
2572}
2573
2574/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002575 * Detach the stream from the connection and possibly release the connection.
2576 */
2577static void h2_detach(struct conn_stream *cs)
2578{
Willy Tarreau60935142017-10-16 18:11:19 +02002579 struct h2s *h2s = cs->ctx;
2580 struct h2c *h2c;
2581
2582 cs->ctx = NULL;
2583 if (!h2s)
2584 return;
2585
2586 h2c = h2s->h2c;
2587 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002588 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002589 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2590 !h2_has_too_many_cs(h2c)) {
2591 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002592 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002593 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002594 }
Willy Tarreau60935142017-10-16 18:11:19 +02002595
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002596 /* this stream may be blocked waiting for some data to leave (possibly
2597 * an ES or RST frame), so orphan it in this case.
2598 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002599 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002600 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002601 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002602 return;
2603
Willy Tarreau45f752e2017-10-30 15:44:59 +01002604 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2605 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2606 /* unblock the connection if it was blocked on this
2607 * stream.
2608 */
2609 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2610 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002611 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002612 }
2613
Willy Tarreau71049cc2018-03-28 13:56:39 +02002614 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002615
Willy Tarreaue323f342018-03-28 13:51:45 +02002616 /* We don't want to close right now unless we're removing the
2617 * last stream, and either the connection is in error, or it
2618 * reached the ID already specified in a GOAWAY frame received
2619 * or sent (as seen by last_sid >= 0).
2620 */
2621 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2622 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002623 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002624 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002625 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002626 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002627 (conn_xprt_read0_pending(h2c->conn) ||
2628 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2629 /* no more stream will come, kill it now */
2630 h2_release(h2c->conn);
2631 }
2632 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002633 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002634 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2635 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002636 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002637 else
2638 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002639 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002640}
2641
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002642static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002643{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002644 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002645 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002646
Willy Tarreau721c9742017-11-07 11:05:42 +01002647 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002648 return;
2649
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002650 /* if no outgoing data was seen on this stream, it means it was
2651 * closed with a "tcp-request content" rule that is normally
2652 * used to kill the connection ASAP (eg: limit abuse). In this
2653 * case we send a goaway to close the connection.
2654 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002655 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002656 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002657 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002658
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002659 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2660 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002661 h2c_send_goaway_error(h2c, h2s) <= 0)
2662 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002663
Willy Tarreau00dd0782018-03-01 16:31:34 +01002664 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002665
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002666 return;
2667add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002668 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002669 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002670 if (h2s->flags & H2_SF_BLK_MFCTL) {
2671 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2672 h2s->send_wait = sw;
2673 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2674 h2s->send_wait = sw;
2675 LIST_ADDQ(&h2c->send_list, &h2s->list);
2676 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002677 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002678 /* Let the handler know we want shutr */
2679 sw->handle = (void *)((long)sw->handle | 1);
2680
Willy Tarreau62f52692017-10-08 23:01:42 +02002681}
2682
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002683static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002684{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002685 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002686 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002687
Willy Tarreau721c9742017-11-07 11:05:42 +01002688 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002689 return;
2690
Willy Tarreau67434202017-11-06 20:20:51 +01002691 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002692 /* we can cleanly close using an empty data frame only after headers */
2693
2694 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2695 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002696 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002697
2698 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002699 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002700 else
2701 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002702 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002703 /* if no outgoing data was seen on this stream, it means it was
2704 * closed with a "tcp-request content" rule that is normally
2705 * used to kill the connection ASAP (eg: limit abuse). In this
2706 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002707 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002708 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002709 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002710 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002711
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002712 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2713 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002714 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002715 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002716
Willy Tarreau00dd0782018-03-01 16:31:34 +01002717 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002718 }
2719
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002720
2721 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002722 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002723 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002724 if (h2s->flags & H2_SF_BLK_MFCTL) {
2725 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2726 h2s->send_wait = sw;
2727 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2728 h2s->send_wait = sw;
2729 LIST_ADDQ(&h2c->send_list, &h2s->list);
2730 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002731 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002732 /* let the handler know we want to shutw */
2733 sw->handle = (void *)((long)(sw->handle) | 2);
2734
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002735}
2736
2737static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2738{
2739 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002740 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002741
2742 if (reason & 1)
2743 h2_do_shutr(h2s);
2744 if (reason & 2)
2745 h2_do_shutw(h2s);
2746
2747 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002748}
2749
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002750static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2751{
2752 struct h2s *h2s = cs->ctx;
2753
2754 if (!mode)
2755 return;
2756
2757 h2_do_shutr(h2s);
2758}
2759
2760static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2761{
2762 struct h2s *h2s = cs->ctx;
2763
2764 h2_do_shutw(h2s);
2765}
2766
Willy Tarreau13278b42017-10-13 19:23:14 +02002767/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2768 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2769 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002770 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002771 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002772static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002773{
2774 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002775 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002776 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002777 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002778 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002779 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002780 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002781 int flen = h2c->dfl;
2782 int outlen = 0;
2783 int wrap;
2784 int try;
2785
2786 if (!h2c->dfl) {
2787 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002788 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002789 return 0;
2790 }
2791
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002792 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002793 return 0; // incomplete input frame
2794
Willy Tarreau13278b42017-10-13 19:23:14 +02002795 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002796 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002797 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002798 copy = alloc_trash_chunk();
2799 if (!copy) {
2800 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2801 goto fail;
2802 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002803 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2804 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2805 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002806 }
2807
2808 /* The padlen is the first byte before data, and the padding appears
2809 * after data. padlen+data+padding are included in flen.
2810 */
2811 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002812 h2c->dpl = *hdrs;
2813 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002814 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2815 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002816 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002817 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002818 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002819 hdrs += 1; // skip Pad Length
2820 }
2821
2822 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2823 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002824 if (read_n32(hdrs) == h2s->id) {
2825 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2826 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002827 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002828 }
2829
Willy Tarreau13278b42017-10-13 19:23:14 +02002830 hdrs += 5; // stream dep = 4, weight = 1
2831 flen -= 5;
2832 }
2833
2834 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2835 * don't support this for now and can't even decompress so we have to
2836 * break the connection.
2837 */
2838 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2839 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002840 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002841 }
2842
Olivier Houchard638b7992018-08-16 15:41:52 +02002843 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002844 if (!csbuf) {
2845 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002846 goto fail;
2847 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002848
Willy Tarreau937f7602018-02-26 15:22:17 +01002849 /* we can't retry a failed decompression operation so we must be very
2850 * careful not to take any risks. In practice the output buffer is
2851 * always empty except maybe for trailers, in which case we simply have
2852 * to wait for the upper layer to finish consuming what is available.
2853 */
2854 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002855 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002856
Willy Tarreau937f7602018-02-26 15:22:17 +01002857 csbuf->head = 0;
2858 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002859
2860 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2861 sizeof(list)/sizeof(list[0]), tmp);
2862 if (outlen < 0) {
2863 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2864 goto fail;
2865 }
2866
2867 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002868 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002869 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002870
2871 if (outlen < 0) {
2872 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2873 goto fail;
2874 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002875
Willy Tarreau174b06a2018-04-25 18:13:58 +02002876 if (msgf & H2_MSGF_BODY) {
2877 /* a payload is present */
2878 if (msgf & H2_MSGF_BODY_CL)
2879 h2s->flags |= H2_SF_DATA_CLEN;
2880 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2881 h2s->flags |= H2_SF_DATA_CHNK;
2882 }
2883
Willy Tarreau13278b42017-10-13 19:23:14 +02002884 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002885 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002886 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002887 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002888
Willy Tarreau39d68502018-03-02 12:26:37 +01002889 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002890 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002891 h2s->cs->flags |= CS_FL_REOS;
2892 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002893
Willy Tarreau68dd9852017-07-03 14:44:26 +02002894 leave:
2895 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002896 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002897 fail:
2898 outlen = 0;
2899 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002900}
2901
Willy Tarreau454f9052017-10-26 19:40:35 +02002902/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2903 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2904 * in use, a new chunk is emitted for each frame. This is supposed to fit
2905 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2906 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2907 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2908 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002909 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2910 * checked to know if some data remain pending (an empty DATA frame can return
2911 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2912 * connection errors in h2c->errcode. The caller must already have checked the
2913 * frame header and ensured that the frame was complete or the buffer full. It
2914 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002915 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002916static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002917{
2918 struct h2c *h2c = h2s->h2c;
2919 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002920 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002921 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002922 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002923
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002924 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002925
2926 /* The padlen is the first byte before data, and the padding appears
2927 * after data. padlen+data+padding are included in flen.
2928 */
Willy Tarreau79127812017-12-03 21:06:59 +01002929 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002931 return 0;
2932
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002933 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002934 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002935 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2936 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002937 return 0;
2938 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002939
2940 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002941 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002942 h2c->dfl--;
2943 h2c->rcvd_c++; h2c->rcvd_s++;
2944 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002945 }
2946
Olivier Houchard638b7992018-08-16 15:41:52 +02002947 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002948 if (!csbuf) {
2949 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002950 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002951 }
2952
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002953 flen = h2c->dfl - h2c->dpl;
2954 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002955 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002956
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002957 if (flen > b_data(&h2c->dbuf)) {
2958 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002959 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002960 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002961 }
2962
2963 if (unlikely(b_space_wraps(csbuf))) {
2964 /* it doesn't fit and the buffer is fragmented,
2965 * so let's defragment it and try again.
2966 */
2967 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002968 }
2969
Willy Tarreaueba10f22018-04-25 20:44:22 +02002970 /* chunked-encoding requires more room */
2971 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002972 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002973 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2974 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2975 (chklen < 1048576) ? 4 : 8;
2976 chklen += 4; // CRLF, CRLF
2977 }
2978
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002979 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002980 if (flen + chklen > b_room(csbuf)) {
2981 if (chklen >= b_room(csbuf)) {
2982 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002983 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002984 }
2985 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002986 }
2987
2988 if (h2s->flags & H2_SF_DATA_CHNK) {
2989 /* emit the chunk size */
2990 unsigned int chksz = flen;
2991 char str[10];
2992 char *beg;
2993
2994 beg = str + sizeof(str);
2995 *--beg = '\n';
2996 *--beg = '\r';
2997 do {
2998 *--beg = hextab[chksz & 0xF];
2999 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003000 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003001 }
3002
Willy Tarreau454f9052017-10-26 19:40:35 +02003003 /* Block1 is the length of the first block before the buffer wraps,
3004 * block2 is the optional second block to reach the end of the frame.
3005 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003006 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003007 if (block1 > flen)
3008 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003009 block2 = flen - block1;
3010
3011 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003012 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003013
3014 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003015 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003016
Willy Tarreaueba10f22018-04-25 20:44:22 +02003017 if (h2s->flags & H2_SF_DATA_CHNK) {
3018 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003019 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003020 }
3021
Willy Tarreau454f9052017-10-26 19:40:35 +02003022 /* now mark the input data as consumed (will be deleted from the buffer
3023 * by the caller when seeing FRAME_A after sending the window update).
3024 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003025 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003026 h2c->dfl -= flen;
3027 h2c->rcvd_c += flen;
3028 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3029
3030 if (h2c->dfl > h2c->dpl) {
3031 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003032 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003033 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003034 }
3035
Willy Tarreau4a28da12018-01-04 14:41:00 +01003036 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003037 /* here we're done with the frame, all the payload (except padding) was
3038 * transferred.
3039 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003040
3041 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3042 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003043 if (b_room(csbuf) < 5) {
3044 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003045 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003046 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003047 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003048 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003049 }
3050
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003051 h2c->rcvd_c += h2c->dpl;
3052 h2c->rcvd_s += h2c->dpl;
3053 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003054 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3055
Willy Tarreau39d68502018-03-02 12:26:37 +01003056 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003057 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003058 h2s->cs->flags |= CS_FL_REOS;
3059 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003060
Willy Tarreau454b57b2018-02-26 15:50:05 +01003061 return flen + chklen;
3062 fail:
3063 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003064}
3065
Willy Tarreau5dd17352018-06-14 13:33:30 +02003066/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3067 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3068 * number of bytes sent. The caller must check the stream's status to detect
3069 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003070 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003071static 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 +02003072{
3073 struct http_hdr list[MAX_HTTP_HDR];
3074 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003075 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003076 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003077 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003078 int es_now = 0;
3079 int ret = 0;
3080 int hdr;
3081
3082 if (h2c_mux_busy(h2c, h2s)) {
3083 h2s->flags |= H2_SF_BLK_MBUSY;
3084 return 0;
3085 }
3086
Willy Tarreau44e973f2018-03-01 17:49:30 +01003087 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003088 h2c->flags |= H2_CF_MUX_MALLOC;
3089 h2s->flags |= H2_SF_BLK_MROOM;
3090 return 0;
3091 }
3092
3093 /* First, try to parse the H1 response and index it into <list>.
3094 * NOTE! Since it comes from haproxy, we *know* that a response header
3095 * block does not wrap and we can safely read it this way without
3096 * having to realign the buffer.
3097 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003098 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003099 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003100 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003101 /* incomplete or invalid response, this is abnormal coming from
3102 * haproxy and may only result in a bad errorfile or bad Lua code
3103 * so that won't be fixed, raise an error now.
3104 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003105 * FIXME: we should instead add the ability to only return a
3106 * 502 bad gateway. But in theory this is not supposed to
3107 * happen.
3108 */
3109 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3110 ret = 0;
3111 goto end;
3112 }
3113
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003114 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003115
3116 /* certain statuses have no body or an empty one, regardless of
3117 * what the headers say.
3118 */
3119 if (sl.st.status >= 100 && sl.st.status < 200) {
3120 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3121 h1m->curr_len = h1m->body_len = 0;
3122 }
3123 else if (sl.st.status == 204 || sl.st.status == 304) {
3124 /* no contents, claim c-len is present and set to zero */
3125 h1m->flags &= ~H1_MF_CHNK;
3126 h1m->flags |= H1_MF_CLEN;
3127 h1m->curr_len = h1m->body_len = 0;
3128 }
3129
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003130 chunk_reset(&outbuf);
3131
3132 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003134 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003135 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003136
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003137 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138 break;
3139 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003140 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003141 }
3142
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003143 if (outbuf.size < 9)
3144 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003145
3146 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3148 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3149 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003150
3151 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003152 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003153 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003154 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003155 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003156 else if (unlikely(list[0].v.len != 3)) {
3157 /* this is an unparsable response */
3158 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3159 ret = 0;
3160 goto end;
3161 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003162 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003163 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003164 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3165 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3166 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3167 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3168 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169 }
3170 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003171 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003172 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003173 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003174 }
3175
3176 /* encode all headers, stop at empty name */
3177 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003178 /* these ones do not exist in H2 and must be dropped. */
3179 if (isteq(list[hdr].n, ist("connection")) ||
3180 isteq(list[hdr].n, ist("proxy-connection")) ||
3181 isteq(list[hdr].n, ist("keep-alive")) ||
3182 isteq(list[hdr].n, ist("upgrade")) ||
3183 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003184 continue;
3185
3186 if (isteq(list[hdr].n, ist("")))
3187 break; // end
3188
3189 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3190 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003191 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003192 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003193 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003194 }
3195 }
3196
3197 /* we may need to add END_STREAM */
3198 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3199 es_now = 1;
3200
3201 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003203
3204 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003205 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003206
3207 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003208 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003209
3210 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003212 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003213
3214 /* for now we don't implemented CONTINUATION, so we wait for a
3215 * body or directly end in TRL2.
3216 */
3217 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003218 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003219 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003220
Willy Tarreau801250e2018-09-11 11:45:04 +02003221 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003222 h2s->flags |= H2_SF_ES_SENT;
3223 if (h2s->st == H2_SS_OPEN)
3224 h2s->st = H2_SS_HLOC;
3225 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003226 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003227 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003228 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003229 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003230 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003231 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003232 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003233 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003234 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003235
3236 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003237
3238 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003239 //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 +02003240 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003241 full:
3242 h1m_init_res(h1m);
3243 h1m->err_pos = -1; // don't care about errors on the response path
3244 h2c->flags |= H2_CF_MUX_MFULL;
3245 h2s->flags |= H2_SF_BLK_MROOM;
3246 ret = 0;
3247 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003248}
3249
Willy Tarreau5dd17352018-06-14 13:33:30 +02003250/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3251 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3252 * the number of bytes sent. The caller must check the stream's status to
3253 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003254 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003255static 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 +02003256{
3257 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003258 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003259 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003260 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003261 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003262 int es_now = 0;
3263 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003264 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003265 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003266
3267 if (h2c_mux_busy(h2c, h2s)) {
3268 h2s->flags |= H2_SF_BLK_MBUSY;
3269 goto end;
3270 }
3271
Willy Tarreau44e973f2018-03-01 17:49:30 +01003272 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003273 h2c->flags |= H2_CF_MUX_MALLOC;
3274 h2s->flags |= H2_SF_BLK_MROOM;
3275 goto end;
3276 }
3277
3278 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003279 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003280 goto end;
3281
3282 chunk_reset(&outbuf);
3283
3284 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003286 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003289 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003290 break;
3291 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 }
3294
3295 if (outbuf.size < 9) {
3296 h2c->flags |= H2_CF_MUX_MFULL;
3297 h2s->flags |= H2_SF_BLK_MROOM;
3298 goto end;
3299 }
3300
3301 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3303 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3304 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003305
3306 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3307 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003308 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003309 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003310 break;
3311 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003312 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003313 if ((long long)size > h1m->curr_len)
3314 size = h1m->curr_len;
3315 break;
3316 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003317 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003318 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003319 if (!ret)
3320 goto end;
3321
3322 if (ret < 0) {
3323 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003324 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3326 goto end;
3327 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003328 max -= ret;
3329 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003330 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003331 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003332 }
3333
Willy Tarreau801250e2018-09-11 11:45:04 +02003334 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003336 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003337 if (!ret)
3338 goto end;
3339
3340 if (ret < 0) {
3341 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003342 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003343 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3344 goto end;
3345 }
3346
3347 size = chunk;
3348 h1m->curr_len = chunk;
3349 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003350 max -= ret;
3351 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003352 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003353 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003354 if (!size)
3355 goto send_empty;
3356 }
3357
3358 /* in MSG_DATA state, continue below */
3359 size = h1m->curr_len;
3360 break;
3361 }
3362
3363 /* we have in <size> the exact number of bytes we need to copy from
3364 * the H1 buffer. We need to check this against the connection's and
3365 * the stream's send windows, and to ensure that this fits in the max
3366 * frame size and in the buffer's available space minus 9 bytes (for
3367 * the frame header). The connection's flow control is applied last so
3368 * that we can use a separate list of streams which are immediately
3369 * unblocked on window opening. Note: we don't implement padding.
3370 */
3371
Willy Tarreau5dd17352018-06-14 13:33:30 +02003372 if (size > max)
3373 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003374
3375 if (size > h2s->mws)
3376 size = h2s->mws;
3377
3378 if (size <= 0) {
3379 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003380 if (h2s->send_wait) {
3381 LIST_DEL(&h2s->list);
3382 LIST_INIT(&h2s->list);
3383 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003384 goto end;
3385 }
3386
3387 if (h2c->mfs && size > h2c->mfs)
3388 size = h2c->mfs;
3389
3390 if (size + 9 > outbuf.size) {
3391 /* we have an opportunity for enlarging the too small
3392 * available space, let's try.
3393 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003394 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003395 goto realign_again;
3396 size = outbuf.size - 9;
3397 }
3398
3399 if (size <= 0) {
3400 h2c->flags |= H2_CF_MUX_MFULL;
3401 h2s->flags |= H2_SF_BLK_MROOM;
3402 goto end;
3403 }
3404
3405 if (size > h2c->mws)
3406 size = h2c->mws;
3407
3408 if (size <= 0) {
3409 h2s->flags |= H2_SF_BLK_MFCTL;
3410 goto end;
3411 }
3412
3413 /* copy whatever we can */
3414 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003415 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003416 if (ret == 1)
3417 len2 = 0;
3418
3419 if (!ret || len1 + len2 < size) {
3420 /* FIXME: must normally never happen */
3421 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3422 goto end;
3423 }
3424
3425 /* limit len1/len2 to size */
3426 if (len1 + len2 > size) {
3427 int sub = len1 + len2 - size;
3428
3429 if (len2 > sub)
3430 len2 -= sub;
3431 else {
3432 sub -= len2;
3433 len2 = 0;
3434 len1 -= sub;
3435 }
3436 }
3437
3438 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003439 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003440 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003441 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003442
3443 send_empty:
3444 /* we may need to add END_STREAM */
3445 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3446 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003447 *
3448 * FIXME: what we do here is not correct because we send end_stream
3449 * before knowing if we'll have to send a HEADERS frame for the
3450 * trailers. More importantly we're not consuming the trailing CRLF
3451 * after the end of trailers, so it will be left to the caller to
3452 * eat it. The right way to do it would be to measure trailers here
3453 * and to send ES only if there are no trailers.
3454 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003455 */
3456 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003457 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003458 es_now = 1;
3459
3460 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003462
3463 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003464 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003465
3466 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003467 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468
3469 /* consume incoming H1 response */
3470 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003471 max -= size;
3472 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003473 total += size;
3474 h1m->curr_len -= size;
3475 h2s->mws -= size;
3476 h2c->mws -= size;
3477
3478 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003479 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003480 goto new_frame;
3481 }
3482 }
3483
3484 if (es_now) {
3485 if (h2s->st == H2_SS_OPEN)
3486 h2s->st = H2_SS_HLOC;
3487 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003488 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003489
Willy Tarreau35a62702018-02-27 15:37:25 +01003490 if (!(h1m->flags & H1_MF_CHNK)) {
3491 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003492 total += max;
3493 ofs += max;
3494 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003495
Willy Tarreau801250e2018-09-11 11:45:04 +02003496 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003497 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003498
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003499 h2s->flags |= H2_SF_ES_SENT;
3500 }
3501
3502 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003503 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 +02003504 return total;
3505}
3506
Olivier Houchard6ff20392018-07-17 18:46:31 +02003507/* Called from the upper layer, to subscribe to events, such as being able to send */
3508static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3509{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003510 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003511 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003512 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003513
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003514 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003515 sw = param;
3516 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3517 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003518 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003519 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003520 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003521 event_type &= ~SUB_CAN_RECV;
3522 }
3523 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003524 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003525 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003526 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003527 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003528 h2s->send_wait = sw;
3529 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3530 if (h2s->flags & H2_SF_BLK_MFCTL)
3531 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3532 else
3533 LIST_ADDQ(&h2c->send_list, &h2s->list);
3534 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003535 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003536 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003537 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003538 if (event_type != 0)
3539 return -1;
3540 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003541
3542
3543}
3544
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003545static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3546{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003547 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003548 struct h2s *h2s = cs->ctx;
3549
3550 if (event_type & SUB_CAN_RECV) {
3551 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003552 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003553 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003554 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003555 }
3556 }
3557 if (event_type & SUB_CAN_SEND) {
3558 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003559 if (h2s->send_wait == sw) {
3560 LIST_DEL(&h2s->list);
3561 LIST_INIT(&h2s->list);
3562 sw->wait_reason &= ~SUB_CAN_SEND;
3563 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003564 }
3565 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003566 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3567 sw = param;
3568 if (h2s->send_wait == sw) {
3569 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3570 h2s->send_wait = NULL;
3571 }
3572 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003573 return 0;
3574}
3575
3576
Olivier Houchard511efea2018-08-16 15:30:32 +02003577/* Called from the upper layer, to receive data */
3578static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3579{
Olivier Houchard638b7992018-08-16 15:41:52 +02003580 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01003581 struct h2c *h2c = h2s->h2c;
Olivier Houchard511efea2018-08-16 15:30:32 +02003582 size_t ret = 0;
3583
3584 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003585 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003586
Olivier Houchard638b7992018-08-16 15:41:52 +02003587 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003588 cs->flags |= CS_FL_RCV_MORE;
3589 else {
3590 cs->flags &= ~CS_FL_RCV_MORE;
3591 if (cs->flags & CS_FL_REOS)
3592 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003593 if (b_size(&h2s->rxbuf)) {
3594 b_free(&h2s->rxbuf);
3595 offer_buffers(NULL, tasks_run_queue);
3596 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003597 }
3598
Willy Tarreau082f5592018-11-25 08:03:32 +01003599 if (ret && h2c->dsi == h2s->id) {
3600 /* demux is blocking on this stream's buffer */
3601 h2c->flags &= ~H2_CF_DEM_SFULL;
3602 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
3603 if (h2_recv_allowed(h2c))
3604 tasklet_wakeup(h2c->wait_event.task);
3605 }
3606 }
3607
Olivier Houchard511efea2018-08-16 15:30:32 +02003608 return ret;
3609}
3610
Olivier Houchardd846c262018-10-19 17:24:29 +02003611static void h2_stop_senders(struct h2c *h2c)
3612{
3613 struct h2s *h2s, *h2s_back;
3614
3615 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3616 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3617 if (h2c->msi == h2s_id(h2s))
3618 continue;
3619 LIST_DEL(&h2s->list);
3620 LIST_INIT(&h2s->list);
3621 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3622 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3623 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3624 LIST_ADD(&h2c->send_list, &h2s->list);
3625 }
3626}
3627
Willy Tarreau62f52692017-10-08 23:01:42 +02003628/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003629static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003630{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003631 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003632 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003633 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003634
Olivier Houchardd846c262018-10-19 17:24:29 +02003635 if (h2s->send_wait) {
3636 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3637 h2s->send_wait = NULL;
3638 LIST_DEL(&h2s->list);
3639 LIST_INIT(&h2s->list);
3640 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003641 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3642 return 0;
3643
Willy Tarreau0bad0432018-06-14 16:54:01 +02003644 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003645 h2s->flags |= H2_SF_OUTGOING_DATA;
3646
Willy Tarreaua40704a2018-09-11 13:52:04 +02003647 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003648 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003649 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003650 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003651 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003652 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003653 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003654 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003655 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003656 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003657
Willy Tarreau5dd17352018-06-14 13:33:30 +02003658 if (unlikely((int)ret <= 0)) {
3659 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003660 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3661 break;
3662 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003663 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003664 total += count;
3665 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003666 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003667 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003668 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003669 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003670 cs->flags |= CS_FL_ERROR;
3671 break;
3672 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003673
3674 total += ret;
3675 count -= ret;
3676
3677 if (h2s->st >= H2_SS_ERROR)
3678 break;
3679
3680 if (h2s->flags & H2_SF_BLK_ANY)
3681 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003682 }
3683
Willy Tarreau00610962018-07-19 10:58:28 +02003684 if (h2s->st >= H2_SS_ERROR) {
3685 /* trim any possibly pending data after we close (extra CR-LF,
3686 * unprocessed trailers, abnormal extra data, ...)
3687 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003688 total += count;
3689 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003690 }
3691
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003692 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003693 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003694 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003695 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003696 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003697 }
3698
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003699 b_del(buf, total);
Olivier Houchardd846c262018-10-19 17:24:29 +02003700
3701 /* The mux is full, cancel the pending tasks */
3702 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3703 (h2s->flags & H2_SF_BLK_MBUSY))
3704 h2_stop_senders(h2s->h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02003705 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003706 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3707 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003708
Olivier Houchard7505f942018-08-21 18:10:44 +02003709 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003710 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003711}
3712
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003713/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003714static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003715{
3716 struct h2c *h2c = conn->mux_ctx;
3717 struct h2s *h2s;
3718 struct eb32_node *node;
3719 int fctl_cnt = 0;
3720 int send_cnt = 0;
3721 int tree_cnt = 0;
3722 int orph_cnt = 0;
3723
3724 if (!h2c)
3725 return;
3726
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003727 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003728 fctl_cnt++;
3729
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003730 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003731 send_cnt++;
3732
3733 node = eb32_first(&h2c->streams_by_id);
3734 while (node) {
3735 h2s = container_of(node, struct h2s, by_id);
3736 tree_cnt++;
3737 if (!h2s->cs)
3738 orph_cnt++;
3739 node = eb32_next(node);
3740 }
3741
Willy Tarreau616ac812018-07-24 14:12:42 +02003742 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3743 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3744 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3745 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3746 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3747 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003748}
Willy Tarreau62f52692017-10-08 23:01:42 +02003749
3750/*******************************************************/
3751/* functions below are dedicated to the config parsers */
3752/*******************************************************/
3753
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003754/* config parser for global "tune.h2.header-table-size" */
3755static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3756 struct proxy *defpx, const char *file, int line,
3757 char **err)
3758{
3759 if (too_many_args(1, args, err, NULL))
3760 return -1;
3761
3762 h2_settings_header_table_size = atoi(args[1]);
3763 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3764 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3765 return -1;
3766 }
3767 return 0;
3768}
Willy Tarreau62f52692017-10-08 23:01:42 +02003769
Willy Tarreaue6baec02017-07-27 11:45:11 +02003770/* config parser for global "tune.h2.initial-window-size" */
3771static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3772 struct proxy *defpx, const char *file, int line,
3773 char **err)
3774{
3775 if (too_many_args(1, args, err, NULL))
3776 return -1;
3777
3778 h2_settings_initial_window_size = atoi(args[1]);
3779 if (h2_settings_initial_window_size < 0) {
3780 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3781 return -1;
3782 }
3783 return 0;
3784}
3785
Willy Tarreau5242ef82017-07-27 11:47:28 +02003786/* config parser for global "tune.h2.max-concurrent-streams" */
3787static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3788 struct proxy *defpx, const char *file, int line,
3789 char **err)
3790{
3791 if (too_many_args(1, args, err, NULL))
3792 return -1;
3793
3794 h2_settings_max_concurrent_streams = atoi(args[1]);
3795 if (h2_settings_max_concurrent_streams < 0) {
3796 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3797 return -1;
3798 }
3799 return 0;
3800}
3801
Willy Tarreau62f52692017-10-08 23:01:42 +02003802
3803/****************************************/
3804/* MUX initialization and instanciation */
3805/***************************************/
3806
3807/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01003808static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02003809 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003810 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003811 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003812 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003813 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003814 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003815 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01003816 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02003817 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01003818 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01003819 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02003820 .shutr = h2_shutr,
3821 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003822 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003823 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003824 .name = "H2",
3825};
3826
Christopher Faulet32f61c02018-04-10 14:33:41 +02003827/* PROTO selection : this mux registers PROTO token "h2" */
3828static struct mux_proto_list mux_proto_h2 =
Willy Tarreau5ae96002018-11-30 16:49:47 +01003829 { .token = IST("h2"), .mode = PROTO_MODE_HTTP | PROTO_MODE_HTX, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003830
Willy Tarreau0108d902018-11-25 19:14:37 +01003831INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
3832
Willy Tarreau62f52692017-10-08 23:01:42 +02003833/* config keyword parsers */
3834static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003835 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003836 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003837 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003838 { 0, NULL, NULL }
3839}};
3840
Willy Tarreau0108d902018-11-25 19:14:37 +01003841INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);